Browse code

Refactor OpenVPNService state detection code

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>
Acked-by: Gert Doering <gert@greenie.muc.de>
Message-Id: <20190224181544.17232-1-simon@rozman.si>
URL: https://www.mail-archive.com/openvpn-devel@lists.sourceforge.net/msg18233.html
Signed-off-by: Gert Doering <gert@greenie.muc.de>

Simon Rozman authored on 2019/02/25 03:15:44
Showing 1 changed files
... ...
@@ -365,40 +365,42 @@ openvpnmsica_set_openvpnserv_state(_In_ MSIHANDLE hInstall)
365 365
     /* Query service status. */
366 366
     SERVICE_STATUS_PROCESS ssp;
367 367
     DWORD dwBufSize;
368
-    if (!QueryServiceStatusEx(hService, SC_STATUS_PROCESS_INFO, (LPBYTE)&ssp, sizeof(ssp), &dwBufSize))
368
+    if (QueryServiceStatusEx(hService, SC_STATUS_PROCESS_INFO, (LPBYTE)&ssp, sizeof(ssp), &dwBufSize))
369 369
     {
370
-        uiResult = GetLastError();
371
-        msg(M_NONFATAL | M_ERRNO, "%s: QueryServiceStatusEx(\"OpenVPNService\") failed", __FUNCTION__);
372
-        goto finish_QueryServiceStatusEx;
373
-    }
374
-
375
-    switch (ssp.dwCurrentState)
376
-    {
377
-        case SERVICE_START_PENDING:
378
-        case SERVICE_RUNNING:
379
-        case SERVICE_STOP_PENDING:
380
-        case SERVICE_PAUSE_PENDING:
381
-        case SERVICE_PAUSED:
382
-        case SERVICE_CONTINUE_PENDING:
370
+        switch (ssp.dwCurrentState)
383 371
         {
384
-            /* Set OPENVPNSERVICE property to service PID. */
385
-            TCHAR szPID[10 /*MAXDWORD in decimal*/ + 1 /*terminator*/];
386
-            _stprintf_s(
387
-                szPID, _countof(szPID),
388
-                TEXT("%u"),
389
-                ssp.dwProcessId);
390
-
391
-            uiResult = MsiSetProperty(hInstall, TEXT("OPENVPNSERVICE"), szPID);
392
-            if (uiResult != ERROR_SUCCESS)
372
+            case SERVICE_START_PENDING:
373
+            case SERVICE_RUNNING:
374
+            case SERVICE_STOP_PENDING:
375
+            case SERVICE_PAUSE_PENDING:
376
+            case SERVICE_PAUSED:
377
+            case SERVICE_CONTINUE_PENDING:
393 378
             {
394
-                SetLastError(uiResult); /* MSDN does not mention MsiSetProperty() to set GetLastError(). But we do have an error code. Set last error manually. */
395
-                msg(M_NONFATAL | M_ERRNO, "%s: MsiSetProperty(\"OPENVPNSERVICE\") failed", __FUNCTION__);
379
+                /* Service is started (kind of). Set OPENVPNSERVICE property to service PID. */
380
+                TCHAR szPID[10 /*MAXDWORD in decimal*/ + 1 /*terminator*/];
381
+                _stprintf_s(
382
+                    szPID, _countof(szPID),
383
+                    TEXT("%u"),
384
+                    ssp.dwProcessId);
385
+
386
+                uiResult = MsiSetProperty(hInstall, TEXT("OPENVPNSERVICE"), szPID);
387
+                if (uiResult != ERROR_SUCCESS)
388
+                {
389
+                    SetLastError(uiResult); /* MSDN does not mention MsiSetProperty() to set GetLastError(). But we do have an error code. Set last error manually. */
390
+                    msg(M_NONFATAL | M_ERRNO, "%s: MsiSetProperty(\"OPENVPNSERVICE\") failed", __FUNCTION__);
391
+                }
392
+
393
+                /* We know user is using the service. Skip auto-start setting check. */
394
+                goto cleanup_OpenService;
396 395
             }
397
-            goto cleanup_OpenService;
396
+            break;
398 397
         }
399
-        break;
400 398
     }
401
-finish_QueryServiceStatusEx:;
399
+    else
400
+    {
401
+        uiResult = GetLastError();
402
+        msg(M_NONFATAL | M_ERRNO, "%s: QueryServiceStatusEx(\"OpenVPNService\") failed", __FUNCTION__);
403
+    }
402 404
 
403 405
     /* Service is not started. Is it set to auto-start? */
404 406
     /* MSDN describes the maximum buffer size for QueryServiceConfig() to be 8kB. */
... ...
@@ -415,6 +417,7 @@ finish_QueryServiceStatusEx:;
415 415
 
416 416
     if (pQsc->dwStartType <= SERVICE_AUTO_START)
417 417
     {
418
+        /* Service is set to auto-start. Set OPENVPNSERVICE property to its path. */
418 419
         uiResult = MsiSetProperty(hInstall, TEXT("OPENVPNSERVICE"), pQsc->lpBinaryPathName);
419 420
         if (uiResult != ERROR_SUCCESS)
420 421
         {