Background
I am designing an Inno Setup installer to install a Cygwin service, and I am puzzled by the behavior I'm seeing from the Windows Restart Manager APIs.
Specifically, when the service is running (started using the cygrunsrv utility), the RmGetList API function returns 2 (RmRebootReasonSessionMismatch) for its lpdwRebootReasons output parameter. This output parameter is an enumeration of type RM_REBOOT_REASON, and the description on MSDN for the RmRebootReasonSessionMismatch value is:
One or more processes are running in another Terminal Services session.
The Inno Setup log file contains lines like the following:
RestartManager found an application using one of our files: <executable name>
RestartManager found an application using one of our files: <service name>
Can use RestartManager to avoid reboot? No (2: Session Mismatch)
Inno Setup then proceeds trying to replace in-use files, as if Restart Manager was not used at all.
I am puzzled by this output value, because on two different machines I tested (Windows 10 1909 x64 and Windows Server 2012 R2), no terminal server/remote desktop users are logged on.
If I stop the service and start another executable (in the set of files to be replaced by the installer), RmGetList returns 0 (RmRebootReasonNone) for lpdwRebootReasons, and Inno Setup displays the normal dialog for in-use files and allows the user to select to automatically close them.
Process Explorer shows both processes (cygrunsrv.exe and the process it starts) running in session 0 and at System integrity level. Both are console subsystem executables.
Questions
Under what conditions does RmGetList return 2 (
RmRebootReasonSessionMismatch) for itslpdwRebootReasonsoutput parameter? (I'm trying to understand why this happens when the service is running.)Does this value cause the entire Restart Manager session to fail, or can Restart Manager proceed even though it thinks applications are running in one or more different sessions?
A Clue from the RestartManager PowerShell Module
The RestartManager PowerShell module (special thanks to Heath Stewart) provides a simple PowerShell interface for the Restart Manager. My commands are as follows:
These commands produce the following output:
For some reason, Restart Manager sees the
cygrunsrv.exeservice process as restartable, but the executable it spawns as not restartable. (I am still curious about why this happens in the first place.)An Imperfect Workaround Attempt
Based on this observed behavior, I first attempted the following workaround:
In the Inno Setup script's
[Setup]section, set the following:The
CloseApplicationsFilterdirective specifies what files get registered with the Restart Manager. Note I do not specify*.exeor*.dllhere; I want to manually specify only certain.exefiles in the[Code]section.Call the Inno Setup
RegisterExtraCloseApplicationsResourcefunction once for each.exefile in the setup that will NOT be spawned bycygrunsrvand put them in theRegisterExtraCloseApplicationsResourcesevent procedure. Example:It's important not to register any executable spawned by
cygrunsrv.exeor any of the Cygwin DLL files, because this will prevent the Restart Manager from taking effect in Inno Setup.This solution is far from perfect, because executables normally started by
cygrunsrv, if started separately, are not detected by Restart Manager (e.g.,sshd.exe). For example, new SSH sessions are spawned in executables that the Restart Manager are not restartable.A Better Solution
I've decided that a better solution is to detect any running executables from code and prompt the user apart from the Restart Manager functionality (which, simply put, doesn't work for Cygwin services).