win-xp-sp2-20040325
[openafs.git] / src / WINNT / afsd / afsd_service.c
index 5580be9..99f7c1a 100644 (file)
@@ -40,6 +40,8 @@
 
 extern void afsi_log(char *pattern, ...);
 
+HANDLE hAFSDMainThread = NULL;
+
 HANDLE WaitToTerminate;
 
 int GlobalStatus;
@@ -189,7 +191,7 @@ afsd_ServiceControlHandlerEx(
         ServiceStatus.dwWin32ExitCode = NO_ERROR;
         ServiceStatus.dwCheckPoint = 1;
         ServiceStatus.dwWaitHint = 10000;
-        ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_POWEREVENT;
+        ServiceStatus.dwControlsAccepted = 0;
         SetServiceStatus(StatusHandle, &ServiceStatus);
         SetEvent(WaitToTerminate);
         dwRet = NO_ERROR;
@@ -317,7 +319,9 @@ void afsd_Main(DWORD argc, LPTSTR *argv)
 {
        long code;
        char *reason;
+#ifdef JUMP
        int jmpret;
+#endif /* JUMP */
     HANDLE hInitHookDll;
     AfsdInitHook initHook;
 
@@ -331,6 +335,8 @@ void afsd_Main(DWORD argc, LPTSTR *argv)
 
        GlobalStatus = 0;
 
+       afsi_start();
+
        WaitToTerminate = CreateEvent(NULL, TRUE, FALSE, TEXT("afsd_service_WaitToTerminate"));
     if ( GetLastError() == ERROR_ALREADY_EXISTS )
         afsi_log("Event Object Already Exists: %s", TEXT("afsd_service_WaitToTerminate"));
@@ -365,8 +371,6 @@ void afsd_Main(DWORD argc, LPTSTR *argv)
     PowerNotificationThreadCreate();
 #endif
 
-       afsi_start();
-
     /* allow an exit to be called prior to any initialization */
     hInitHookDll = LoadLibrary(AFSD_HOOK_DLL);
     if (hInitHookDll)
@@ -459,13 +463,6 @@ void afsd_Main(DWORD argc, LPTSTR *argv)
 
        WaitForSingleObject(WaitToTerminate, INFINITE);
 
-    ServiceStatus.dwCurrentState = SERVICE_STOP_PENDING;
-       ServiceStatus.dwWin32ExitCode = NO_ERROR;
-       ServiceStatus.dwCheckPoint = 0;
-       ServiceStatus.dwWaitHint = 5000;
-       ServiceStatus.dwControlsAccepted = 0;
-       SetServiceStatus(StatusHandle, &ServiceStatus);
-
     {   
     HANDLE h; char *ptbuf[1];
        h = RegisterEventSource(NULL, AFS_DAEMON_EVENT_NAME);
@@ -483,41 +480,48 @@ void afsd_Main(DWORD argc, LPTSTR *argv)
     /* Remove the ExceptionFilter */
     SetUnhandledExceptionFilter(NULL);
 
-       ServiceStatus.dwCurrentState = SERVICE_STOPPED;
+    if ( hInitHookDll )
+        FreeLibrary(hInitHookDll);
+
+    ServiceStatus.dwCurrentState = SERVICE_STOPPED;
        ServiceStatus.dwWin32ExitCode = GlobalStatus ? ERROR_EXCEPTION_IN_SERVICE : NO_ERROR;
        ServiceStatus.dwCheckPoint = 0;
        ServiceStatus.dwWaitHint = 0;
        ServiceStatus.dwControlsAccepted = 0;
        SetServiceStatus(StatusHandle, &ServiceStatus);
-    return;
 }
 
 DWORD __stdcall afsdMain_thread(void* notUsed)
 {
        afsd_Main(0, (LPTSTR*)NULL);
-    return(0);
+    exit(0);
 }
 
-void main()
+int
+main(void)
 {
-       SERVICE_TABLE_ENTRY dispatchTable[] = {
+       static SERVICE_TABLE_ENTRY dispatchTable[] = {
                {AFS_DAEMON_SERVICE_NAME, (LPSERVICE_MAIN_FUNCTION) afsd_Main},
                {NULL, NULL}
        };
 
-    afsd_SetUnhandledExceptionFilter();
-
        if (!StartServiceCtrlDispatcher(dispatchTable))
     {
         LONG status = GetLastError();
            if (status == ERROR_FAILED_SERVICE_CONTROLLER_CONNECT)
         {
             DWORD tid;
-            CreateThread(NULL, 0, afsdMain_thread, 0, 0, &tid);
+            hAFSDMainThread = CreateThread(NULL, 0, afsdMain_thread, 0, 0, &tid);
                
             printf("Hit <Enter> to terminate OpenAFS Client Service\n");
             getchar();              
             SetEvent(WaitToTerminate);
         }
     }
+
+    if ( hAFSDMainThread ) {
+        WaitForSingleObject( hAFSDMainThread, INFINITE );
+        CloseHandle( hAFSDMainThread );
+    }
+    return(0);
 }