Clean up RPCSS.
authorDmitry Gorbachev <gorbachev@reactos.org>
Mon, 22 Feb 2010 21:36:07 +0000 (21:36 +0000)
committerDmitry Gorbachev <gorbachev@reactos.org>
Mon, 22 Feb 2010 21:36:07 +0000 (21:36 +0000)
svn path=/trunk/; revision=45661

reactos/base/services/rpcss/rpcss_main.c
reactos/base/services/rpcss/rpcss_ros.diff
reactos/base/services/rpcss/service_main.c

index 2df13c7..8e6ec1f 100644 (file)
@@ -58,7 +58,7 @@
 
 WINE_DEFAULT_DEBUG_CHANNEL(ole);
 
-static HANDLE exit_event;
+HANDLE exit_event;
 
 //extern HANDLE __wine_make_process_system(void);
 
index 02d8226..f23583a 100644 (file)
@@ -1,8 +1,13 @@
 Index: rpcss_main.c
 ===================================================================
---- rpcss_main.c       (revision 37240)
-+++ rpcss_main.c       (working copy)
-@@ -62,7 +62,7 @@
+--- rpcss_main.c
++++ rpcss_main.c
+@@ -58,11 +58,11 @@
+ WINE_DEFAULT_DEBUG_CHANNEL(ole);
+-static HANDLE exit_event;
++HANDLE exit_event;
  
  //extern HANDLE __wine_make_process_system(void);
  
index 7fe84a5..501e2a4 100644 (file)
 
 #include "rpcss.h"
 
-BOOL RPCSS_Initialize(void);
-BOOL RPCSS_Shutdown(void);
+#define NDEBUG
+#include <debug.h>
 
-static WCHAR ServiceName[] = L"RpcSs";
+extern BOOL RPCSS_Initialize(void);
+extern BOOL RPCSS_Shutdown(void);
+extern HANDLE exit_event;
 
-typedef struct _ServiceInfo
+static VOID WINAPI ServiceMain(DWORD, LPWSTR *);
+static WCHAR ServiceName[] = L"RpcSs";
+SERVICE_TABLE_ENTRYW ServiceTable[] =
 {
-    SERVICE_STATUS servStatus;
-    SERVICE_STATUS_HANDLE hStatus;
-} SERVICEINFO, *PSERVICEINFO;
-
-
-static VOID
-UpdateStatus(PSERVICEINFO pServInfo,
-             DWORD NewStatus,
-             DWORD Check)
+    { ServiceName, ServiceMain },
+    { NULL,        NULL }
+};
+
+static SERVICE_STATUS ServiceStatus;
+static SERVICE_STATUS_HANDLE ServiceStatusHandle;
+
+DWORD WINAPI
+ServiceControlHandler(DWORD dwControl,
+                      DWORD dwEventType,
+                      LPVOID lpEventData,
+                      LPVOID lpContext)
 {
-    if (Check > 0)
-    {
-        pServInfo->servStatus.dwCheckPoint += Check;
-    }
-    else
-    {
-        pServInfo->servStatus.dwCheckPoint = Check;
-    }
-
-    if (NewStatus > 0)
-    {
-        pServInfo->servStatus.dwCurrentState = NewStatus;
-    }
-
-    SetServiceStatus(pServInfo->hStatus, &pServInfo->servStatus);
-}
-
-
-static BOOL
-RunService(PSERVICEINFO pServInfo)
-{
-    return RPCSS_Initialize();
-}
-
-VOID WINAPI
-ServerCtrlHandler(DWORD dwControl,
-                  DWORD dwEventType,
-                  LPVOID lpEventData,
-                  LPVOID lpContext)
-{
-    PSERVICEINFO pServInfo = (PSERVICEINFO)lpContext;
-
     switch (dwControl)
     {
         case SERVICE_CONTROL_SHUTDOWN:
         case SERVICE_CONTROL_STOP:
-            RPCSS_Shutdown();
-            pServInfo->servStatus.dwWin32ExitCode = 0;
-            pServInfo->servStatus.dwWaitHint = 0;
-            UpdateStatus(pServInfo, SERVICE_STOP_PENDING, 1);
-            break;
+            SetEvent(exit_event);
+            return NO_ERROR;
+
+        case SERVICE_CONTROL_INTERROGATE:
+            return NO_ERROR;
 
         default:
-            break;
+            return ERROR_CALL_NOT_IMPLEMENTED;
     }
 }
 
 VOID WINAPI
 ServiceMain(DWORD argc, LPWSTR argv[])
 {
-    SERVICEINFO servInfo;
-    HANDLE hThread;
-    DWORD dwThreadId;
-
-    servInfo.servStatus.dwServiceType      = SERVICE_WIN32_OWN_PROCESS;
-    servInfo.servStatus.dwCurrentState     = SERVICE_STOPPED;
-    servInfo.servStatus.dwControlsAccepted = SERVICE_ACCEPT_SHUTDOWN;
-    servInfo.servStatus.dwWin32ExitCode    = ERROR_SERVICE_SPECIFIC_ERROR;
-    servInfo.servStatus.dwServiceSpecificExitCode = 0;
-    servInfo.servStatus.dwCheckPoint       = 0;
-    servInfo.servStatus.dwWaitHint         = 1000;
-
-    servInfo.hStatus = RegisterServiceCtrlHandlerExW(ServiceName,
-                                                     (LPHANDLER_FUNCTION_EX)ServerCtrlHandler,
-                                                     &servInfo);
-    if (!servInfo.hStatus) return;
-
-    UpdateStatus(&servInfo, SERVICE_START_PENDING, 1);
-
-    /* Create worker thread */
-    hThread = CreateThread(NULL,
-                           0,
-                           (LPTHREAD_START_ROUTINE)RunService,
-                           &servInfo,
-                           0,
-                           &dwThreadId);
-    if (!hThread) return;
-
-    /* Set service status to running */
-    UpdateStatus(&servInfo, SERVICE_RUNNING, 0);
-
-    /* Wait until thread has terminated */
-    WaitForSingleObject(hThread, INFINITE);
-
-    CloseHandle(hThread);
-
-    UpdateStatus(&servInfo, SERVICE_STOPPED, 0);
-}
+    DWORD dwError;
+
+    ServiceStatusHandle = RegisterServiceCtrlHandlerExW(ServiceName,
+                                                        ServiceControlHandler,
+                                                        NULL);
+    if (!ServiceStatusHandle)
+    {
+        dwError = GetLastError();
+        DPRINT1("RegisterServiceCtrlHandlerW() failed! (Error %lu)\n", dwError);
+        return;
+    }
+
+    ServiceStatus.dwServiceType      = SERVICE_WIN32_OWN_PROCESS;
+    ServiceStatus.dwCurrentState     = SERVICE_START_PENDING;
+    ServiceStatus.dwControlsAccepted = 0;
+    ServiceStatus.dwWin32ExitCode    = NO_ERROR;
+    ServiceStatus.dwServiceSpecificExitCode = 0;
+    ServiceStatus.dwCheckPoint       = 0;
+    ServiceStatus.dwWaitHint         = 1000;
+    SetServiceStatus(ServiceStatusHandle, &ServiceStatus);
+
+    if (RPCSS_Initialize())
+    {
+        ServiceStatus.dwCurrentState = SERVICE_RUNNING;
+        ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
+        SetServiceStatus(ServiceStatusHandle, &ServiceStatus);
 
+        WaitForSingleObject(exit_event, INFINITE);
 
-int wmain(int argc, LPWSTR argv [])
+        ServiceStatus.dwCurrentState = SERVICE_STOPPED;
+        SetServiceStatus(ServiceStatusHandle, &ServiceStatus);
+        RPCSS_Shutdown();
+    }
+}
+
+int wmain(int argc, LPWSTR argv[])
 {
-    SERVICE_TABLE_ENTRYW ServiceTable[] =
+    if (!StartServiceCtrlDispatcherW(ServiceTable))
     {
-        {ServiceName, ServiceMain},
-        {NULL,        NULL }
-    };
+        DPRINT1("StartServiceCtrlDispatcherW() failed\n");
+        return 1;
+    }
 
-    return (int)(StartServiceCtrlDispatcherW(ServiceTable) != TRUE);
+    return 0;
 }