[2/2] qmgr: Add ServiceMain with test.

Dan Hipschman dsh at linux.ucla.edu
Mon Feb 11 13:09:34 CST 2008


ChangeLog:

Adds ServiceMain to qmgr with test.

From: Roy Shea <roy at cs.hmc.edu>
Date: Thu Dec 20 18:38:31 CST 2007

---
 dlls/qmgr/Makefile.in       |    5 +-
 dlls/qmgr/qmgr.spec         |    1 +
 dlls/qmgr/qmgr_service.c    |  138 +++++++++++++++++++++++++++++++++++++++++++
 dlls/qmgr/tests/Makefile.in |   17 +++++
 dlls/qmgr/tests/qmgr.c      |   55 +++++++++++++++++
 tools/wine.inf              |    2 +
 6 files changed, 216 insertions(+), 2 deletions(-)
 create mode 100644 dlls/qmgr/qmgr_service.c
 create mode 100644 dlls/qmgr/tests/Makefile.in
 create mode 100644 dlls/qmgr/tests/qmgr.c

diff --git a/dlls/qmgr/Makefile.in b/dlls/qmgr/Makefile.in
index 73c161a..fd06116 100644
--- a/dlls/qmgr/Makefile.in
+++ b/dlls/qmgr/Makefile.in
@@ -3,13 +3,14 @@ TOPOBJDIR = ../..
 SRCDIR    = @srcdir@
 VPATH     = @srcdir@
 MODULE    = qmgr.dll
-IMPORTS   = advapi32 advpack kernel32
+IMPORTS   = advapi32 advpack kernel32 ole32
 EXTRALIBS = -luuid
 
 C_SRCS = \
 	factory.c \
 	qmgr.c \
-	qmgr_main.c
+	qmgr_main.c \
+	qmgr_service.c
 
 RC_SRCS = rsrc.rc
 
diff --git a/dlls/qmgr/qmgr.spec b/dlls/qmgr/qmgr.spec
index 0f9b073..2431c63 100644
--- a/dlls/qmgr/qmgr.spec
+++ b/dlls/qmgr/qmgr.spec
@@ -1,2 +1,3 @@
 @ stdcall -private DllRegisterServer()
 @ stdcall -private DllUnregisterServer()
+@ stdcall -private ServiceMain(long ptr)
diff --git a/dlls/qmgr/qmgr_service.c b/dlls/qmgr/qmgr_service.c
new file mode 100644
index 0000000..5e31b8f
--- /dev/null
+++ b/dlls/qmgr/qmgr_service.c
@@ -0,0 +1,138 @@
+/*
+ * ServiceMain function for qmgr running within svchost
+ *
+ * Copyright 2007 (C) Google (Roy Shea)
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
+ */
+
+#include "windef.h"
+#include "objbase.h"
+#include "winsvc.h"
+#include "bits.h"
+
+#include "qmgr.h"
+#include "wine/debug.h"
+
+WINE_DEFAULT_DEBUG_CHANNEL(qmgr);
+
+static WCHAR qmgr_nameW[] = {'B','I','T','S',0};
+static SERVICE_STATUS_HANDLE status_handle;
+static SERVICE_STATUS status;
+static HANDLE stop_event = NULL;
+
+static VOID
+UpdateStatus(DWORD dwCurrentState, DWORD dwWin32ExitCode, DWORD dwWaitHint)
+{
+    status.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
+    status.dwCurrentState = dwCurrentState;
+    if (dwCurrentState == SERVICE_START_PENDING)
+        status.dwControlsAccepted = 0;
+    else
+        status.dwControlsAccepted
+            = (SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_PAUSE_CONTINUE
+               | SERVICE_ACCEPT_SHUTDOWN);
+    status.dwWin32ExitCode = 0;
+    status.dwServiceSpecificExitCode = 0;
+    status.dwCheckPoint = 0;
+    status.dwWaitHint = dwWaitHint;
+
+    if (!SetServiceStatus(status_handle, &status)) {
+        ERR("failed to set service status\n");
+        SetEvent(stop_event);
+    }
+}
+
+/* Handle incoming ControlService signals */
+static DWORD WINAPI
+ServiceHandler(DWORD ctrl, DWORD event_type, LPVOID event_data, LPVOID context)
+{
+    switch (ctrl) {
+    case SERVICE_CONTROL_STOP:
+    case SERVICE_CONTROL_SHUTDOWN:
+        TRACE("shutting down service\n");
+        UpdateStatus(SERVICE_STOP_PENDING, NO_ERROR, 0);
+        SetEvent(stop_event);
+        break;
+    default:
+        FIXME("ignoring handle service ctrl %x\n", ctrl);
+        UpdateStatus(status.dwCurrentState, NO_ERROR, 0);
+        break;
+    }
+
+    return NO_ERROR;
+}
+
+/* Main thread of the service */
+static BOOL
+StartCount(void)
+{
+    HRESULT hr;
+    DWORD dwReg;
+
+    TRACE("\n");
+
+    hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
+    if (!SUCCEEDED(hr))
+        return FALSE;
+
+    hr = CoInitializeSecurity(NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_NONE,
+                              RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE,
+                              NULL);
+    if (!SUCCEEDED(hr))
+        return FALSE;
+
+    hr = CoRegisterClassObject(&CLSID_BackgroundCopyManager,
+                               (IUnknown *) &BITS_ClassFactory,
+                               CLSCTX_LOCAL_SERVER, REGCLS_MULTIPLEUSE,
+                               &dwReg);
+    if (!SUCCEEDED(hr))
+        return FALSE;
+
+    return TRUE;
+}
+
+/* Service entry point */
+VOID WINAPI
+ServiceMain(DWORD dwArgc, LPWSTR *lpszArgv)
+{
+    TRACE("\n");
+
+    stop_event = CreateEventW(NULL, TRUE, FALSE, NULL);
+    if (!stop_event) {
+        ERR("failed to create stop_event\n");
+        return;
+    }
+
+    status_handle = RegisterServiceCtrlHandlerExW(qmgr_nameW, ServiceHandler, NULL);
+    if (!status_handle) {
+        ERR("failed to register handler: %u\n", GetLastError());
+        return;
+    }
+
+    UpdateStatus(SERVICE_START_PENDING, NO_ERROR, 3000);
+    if (!StartCount()) {
+        ERR("failed starting service thread\n");
+        UpdateStatus(SERVICE_STOPPED, NO_ERROR, 0);
+        return;
+    }
+
+    UpdateStatus(SERVICE_RUNNING, NO_ERROR, 0);
+
+    WaitForSingleObject(stop_event, INFINITE);
+    UpdateStatus(SERVICE_STOPPED, NO_ERROR, 0);
+    CloseHandle(stop_event);
+    TRACE("service stoped\n");
+}
diff --git a/dlls/qmgr/tests/Makefile.in b/dlls/qmgr/tests/Makefile.in
new file mode 100644
index 0000000..cbb0401
--- /dev/null
+++ b/dlls/qmgr/tests/Makefile.in
@@ -0,0 +1,17 @@
+TOPSRCDIR = @top_srcdir@
+TOPOBJDIR = ../../..
+SRCDIR    = @srcdir@
+VPATH     = @srcdir@
+TESTDLL   = qmgr.dll
+IMPORTS   = ole32 kernel32
+MODCFLAGS = @BUILTINFLAG@
+EXTRAINCL = -I$(SRCDIR)/..
+
+CTESTS = \
+	qmgr.c
+
+IDL_I_SRCS = ../qmgr_local.idl
+
+ at MAKE_TEST_RULES@
+
+ at DEPENDENCIES@  # everything below this line is overwritten by make depend
diff --git a/dlls/qmgr/tests/qmgr.c b/dlls/qmgr/tests/qmgr.c
new file mode 100644
index 0000000..7f137a7
--- /dev/null
+++ b/dlls/qmgr/tests/qmgr.c
@@ -0,0 +1,55 @@
+/*
+ * Unit test suite for bits functions
+ *
+ * Copyright 2007 Google (Roy Shea)
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
+ */
+
+#include <stdio.h>
+
+#define COBJMACROS
+
+#include "wine/test.h"
+#include "bits.h"
+
+static void
+test_CreateInstance(void)
+{
+    HRESULT hres;
+    ULONG res;
+    IBackgroundCopyManager *manager = NULL;
+
+    /* Creating BITS instance */
+    hres = CoCreateInstance(&CLSID_BackgroundCopyManager, NULL, CLSCTX_LOCAL_SERVER,
+                            &IID_IBackgroundCopyManager, (void **) &manager);
+    ok(hres == S_OK, "CoCreateInstance failed: %08x\n", hres);
+    if(hres != S_OK) {
+        skip("Unable to create bits instance.\n");
+        return;
+    }
+
+    /* Releasing bits manager */
+    res = IBackgroundCopyManager_Release(manager);
+    ok(res == 0, "Bad ref count on release: %u\n", res);
+
+}
+
+START_TEST(qmgr)
+{
+    CoInitialize(NULL);
+    test_CreateInstance();
+    CoUninitialize();
+}
diff --git a/tools/wine.inf b/tools/wine.inf
index 312d66c..ca74a96 100644
--- a/tools/wine.inf
+++ b/tools/wine.inf
@@ -2195,6 +2195,8 @@ HKLM,%CurrentVersion%\Telephony\Country List\998,"SameAreaRule",,"G"
 11,,msxml3.dll,1
 11,,objsel.dll,1
 11,,qcap.dll,1
+11,,qmgr.dll,1
+11,,qmgrprxy.dll,1
 11,,quartz.dll,1
 11,,rsaenh.dll,1
 11,,shdocvw.dll,1



More information about the wine-patches mailing list