From patchwork Sun Feb 24 07:15:44 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Rozman X-Patchwork-Id: 693 Return-Path: Delivered-To: patchwork@openvpn.net Delivered-To: patchwork@openvpn.net Received: from director7.mail.ord1d.rsapps.net ([172.30.191.6]) by backend30.mail.ord1d.rsapps.net with LMTP id ENNqL3/fclzYDAAAIUCqbw for ; Sun, 24 Feb 2019 13:16:31 -0500 Received: from proxy14.mail.ord1d.rsapps.net ([172.30.191.6]) by director7.mail.ord1d.rsapps.net with LMTP id oPM3L3/fclyLVQAAovjBpQ ; Sun, 24 Feb 2019 13:16:31 -0500 Received: from smtp28.gate.ord1d ([172.30.191.6]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) by proxy14.mail.ord1d.rsapps.net with LMTP id 8CMSL3/fclzsdQAAtEH5vw ; Sun, 24 Feb 2019 13:16:31 -0500 X-Spam-Threshold: 95 X-Spam-Score: 0 X-Spam-Flag: NO X-Virus-Scanned: OK X-Orig-To: openvpnslackdevel@openvpn.net X-Originating-Ip: [216.105.38.7] Authentication-Results: smtp28.gate.ord1d.rsapps.net; iprev=pass policy.iprev="216.105.38.7"; spf=pass smtp.mailfrom="openvpn-devel-bounces@lists.sourceforge.net" smtp.helo="lists.sourceforge.net"; dkim=fail (signature verification failed) header.d=sourceforge.net; dkim=fail (signature verification failed) header.d=sf.net; dkim=fail (signature verification failed) header.d=rozman.si; dmarc=fail (p=none; dis=none) header.from=rozman.si X-Suspicious-Flag: YES X-Classification-ID: 4f0089bc-3860-11e9-9665-525400ea129b-1-1 Received: from [216.105.38.7] ([216.105.38.7:41769] helo=lists.sourceforge.net) by smtp28.gate.ord1d.rsapps.net (envelope-from ) (ecelerity 4.2.38.62370 r(:)) with ESMTPS (cipher=DHE-RSA-AES256-GCM-SHA384) id 03/89-27447-E7FD27C5; Sun, 24 Feb 2019 13:16:31 -0500 Received: from [127.0.0.1] (helo=sfs-ml-1.v29.lw.sourceforge.com) by sfs-ml-1.v29.lw.sourceforge.com with esmtp (Exim 4.90_1) (envelope-from ) id 1gxyJg-0001Sj-NE; Sun, 24 Feb 2019 18:15:56 +0000 Received: from [172.30.20.202] (helo=mx.sourceforge.net) by sfs-ml-1.v29.lw.sourceforge.com with esmtps (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.90_1) (envelope-from ) id 1gxyJf-0001SY-QR for openvpn-devel@lists.sourceforge.net; Sun, 24 Feb 2019 18:15:55 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sourceforge.net; s=x; h=Content-Transfer-Encoding:MIME-Version:References: In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=QWyH0nNDKeU937/Rl20A/t7M6Vac0UXH0S02xBbryjs=; b=CMoRCaCO+DPI9xY3UJt9NufnjD 3Q18p5XcZhosxIh9ZbwSPyE/W+L+QZ+65bIjexJwc5EolAjNfe3nn6ZoC40OQAV/NzOhQYJCfmf5G jVb1WxAlazqDaIubaz5Dh2Y8/kcD/h6ck+9UvoYA/YEcHlT4JRp06ewhQvG9HkVjHd+o=; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sf.net; s=x ; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To:Message-Id: Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=QWyH0nNDKeU937/Rl20A/t7M6Vac0UXH0S02xBbryjs=; b=VTBxRCHLFoLDaobQ8i06P05tgC nQ7e5cmWe6hzCL1FOli/NdEO68Rff68QShfm1esVCQPz0WmPpHvlBrtVy2cUxu3TzFdbNhxz85X/H 8iXpQ/em0zC0ApZYXZOMirm6NT8CXeu8IfL55Q56h7CajfUoCmh8ZgiE+cMgbfROTb9Y=; Received: from pub5.amebis.si ([213.250.55.21]) by sfi-mx-1.v28.lw.sourceforge.com with esmtps (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.90_1) id 1gxyJd-004lqq-Np for openvpn-devel@lists.sourceforge.net; Sun, 24 Feb 2019 18:15:55 +0000 Received: by pub5.amebis.si (Postfix, from userid 115) id E7A0920E22; Sun, 24 Feb 2019 19:15:46 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=rozman.si; s=default; t=1551032146; bh=PYMtD8oOCaNndEPMcf93SF1CbpUpSs0A2ucIAfujFNw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=s90+YDQH7CNh+S1v3bVK4m2Bce5USx81yPlWyO4IyjQF6OTyCcTGRoiY8+4nPDhqy XeKXmG7vyZqGDNRHANUnoF+IFbIxQQRzZ4MUAOPxvwUE/6UVyCPA6J3m5+QPHqcgZb nWVRBVLrhEBXHPcXvS5CvQIL/aEzlUmLdKdrnfCI= X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on brana.amebis.doma X-Spam-Level: X-Spam-Status: No, score=-1.1 required=5.0 tests=BAYES_00,RDNS_NONE autolearn=no autolearn_force=no version=3.4.2 Received: from gw0.selo.doma (unknown [IPv6:2a00:ee2:1206:8101::1]) by pub5.amebis.si (Postfix) with ESMTPS id 690A720522; Sun, 24 Feb 2019 19:15:46 +0100 (CET) Received: from s0.selo.doma (s0.selo.doma [IPv6:2a00:ee2:1206:8101::d]) by gw0.selo.doma (Postfix) with ESMTP id 1F8D421B982A; Sun, 24 Feb 2019 19:15:46 +0100 (CET) From: Simon Rozman To: openvpn-devel@lists.sourceforge.net Date: Sun, 24 Feb 2019 19:15:44 +0100 Message-Id: <20190224181544.17232-1-simon@rozman.si> X-Mailer: git-send-email 2.19.0.windows.1 In-Reply-To: <201901181944.x0IJiGuV003728@chekov.greenie.muc.de> References: <201901181944.x0IJiGuV003728@chekov.greenie.muc.de> MIME-Version: 1.0 X-Spam-Report: Spam Filtering performed by mx.sourceforge.net. See http://spamassassin.org/tag/ for more details. 0.0 URIBL_BLOCKED ADMINISTRATOR NOTICE: The query to URIBL was blocked. See http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more information. [URIs: muc.de] -0.0 SPF_PASS SPF: sender matches SPF record -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid 0.0 AWL AWL: Adjusted score from AWL reputation of From: address X-Headers-End: 1gxyJd-004lqq-Np Subject: [Openvpn-devel] [PATCH] Refactor OpenVPNService state detection code X-BeenThere: openvpn-devel@lists.sourceforge.net X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: openvpn-devel-bounces@lists.sourceforge.net X-getmail-retrieved-from-mailbox: Inbox 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 Message-ID: <201901181944.x0IJiGuV003728@chekov.greenie.muc.de> Acked-by: Gert Doering --- src/openvpnmsica/openvpnmsica.c | 59 +++++++++++++++++---------------- 1 file changed, 31 insertions(+), 28 deletions(-) diff --git a/src/openvpnmsica/openvpnmsica.c b/src/openvpnmsica/openvpnmsica.c index 00ed2765..2477e81a 100644 --- a/src/openvpnmsica/openvpnmsica.c +++ b/src/openvpnmsica/openvpnmsica.c @@ -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) {