@@ -365,40 +365,42 @@ openvpnmsica_set_openvpnserv_state(_In_ MSIHANDLE hInstall)
/* Query service status. */
SERVICE_STATUS_PROCESS ssp;
DWORD dwBufSize;
- if (!QueryServiceStatusEx(hService, SC_STATUS_PROCESS_INFO, (LPBYTE)&ssp, sizeof(ssp), &dwBufSize))
+ if (QueryServiceStatusEx(hService, SC_STATUS_PROCESS_INFO, (LPBYTE)&ssp, sizeof(ssp), &dwBufSize))
{
- uiResult = GetLastError();
- msg(M_NONFATAL | M_ERRNO, "%s: QueryServiceStatusEx(\"OpenVPNService\") failed", __FUNCTION__);
- goto finish_QueryServiceStatusEx;
- }
-
- switch (ssp.dwCurrentState)
- {
- case SERVICE_START_PENDING:
- case SERVICE_RUNNING:
- case SERVICE_STOP_PENDING:
- case SERVICE_PAUSE_PENDING:
- case SERVICE_PAUSED:
- case SERVICE_CONTINUE_PENDING:
+ switch (ssp.dwCurrentState)
{
- /* Set OPENVPNSERVICE property to service PID. */
- TCHAR szPID[10 /*MAXDWORD in decimal*/ + 1 /*terminator*/];
- _stprintf_s(
- szPID, _countof(szPID),
- TEXT("%u"),
- ssp.dwProcessId);
-
- uiResult = MsiSetProperty(hInstall, TEXT("OPENVPNSERVICE"), szPID);
- if (uiResult != ERROR_SUCCESS)
+ case SERVICE_START_PENDING:
+ case SERVICE_RUNNING:
+ case SERVICE_STOP_PENDING:
+ case SERVICE_PAUSE_PENDING:
+ case SERVICE_PAUSED:
+ case SERVICE_CONTINUE_PENDING:
{
- SetLastError(uiResult); /* MSDN does not mention MsiSetProperty() to set GetLastError(). But we do have an error code. Set last error manually. */
- msg(M_NONFATAL | M_ERRNO, "%s: MsiSetProperty(\"OPENVPNSERVICE\") failed", __FUNCTION__);
+ /* Service is started (kind of). Set OPENVPNSERVICE property to service PID. */
+ TCHAR szPID[10 /*MAXDWORD in decimal*/ + 1 /*terminator*/];
+ _stprintf_s(
+ szPID, _countof(szPID),
+ TEXT("%u"),
+ ssp.dwProcessId);
+
+ uiResult = MsiSetProperty(hInstall, TEXT("OPENVPNSERVICE"), szPID);
+ if (uiResult != ERROR_SUCCESS)
+ {
+ SetLastError(uiResult); /* MSDN does not mention MsiSetProperty() to set GetLastError(). But we do have an error code. Set last error manually. */
+ msg(M_NONFATAL | M_ERRNO, "%s: MsiSetProperty(\"OPENVPNSERVICE\") failed", __FUNCTION__);
+ }
+
+ /* We know user is using the service. Skip auto-start setting check. */
+ goto cleanup_OpenService;
}
- goto cleanup_OpenService;
+ break;
}
- break;
}
-finish_QueryServiceStatusEx:;
+ else
+ {
+ uiResult = GetLastError();
+ msg(M_NONFATAL | M_ERRNO, "%s: QueryServiceStatusEx(\"OpenVPNService\") failed", __FUNCTION__);
+ }
/* Service is not started. Is it set to auto-start? */
/* MSDN describes the maximum buffer size for QueryServiceConfig() to be 8kB. */
@@ -415,6 +417,7 @@ finish_QueryServiceStatusEx:;
if (pQsc->dwStartType <= SERVICE_AUTO_START)
{
+ /* Service is set to auto-start. Set OPENVPNSERVICE property to its path. */
uiResult = MsiSetProperty(hInstall, TEXT("OPENVPNSERVICE"), pQsc->lpBinaryPathName);
if (uiResult != ERROR_SUCCESS)
{
The code was standardized to avoid "E1072: a declaration cannot have a label" warning of Visual Studio 2017 IntelliSense. Furthermore, a comment explaining what `dwStartType <= SERVICE_AUTO_START` condition is about. This patch follows Gert's recommendations from [openvpn-devel]. Signed-off-by: Simon Rozman <simon@rozman.si> Message-ID: <201901181944.x0IJiGuV003728@chekov.greenie.muc.de> --- src/openvpnmsica/openvpnmsica.c | 59 +++++++++++++++++---------------- 1 file changed, 31 insertions(+), 28 deletions(-)