afsd-debugging-20040401
[openafs.git] / src / WINNT / afsd / afsd_service.c
index 0cc4387..2ef249d 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)
@@ -414,8 +418,10 @@ void afsd_Main(DWORD argc, LPTSTR *argv)
 #endif /* JUMP */
     {
                code = afsd_InitCM(&reason);
-               if (code != 0)
+               if (code != 0) {
+            afsi_log("afsd_InitCM failed: %s (code = %d)", reason, code);
                        osi_panic(reason, __FILE__, __LINE__);
+        }
 
 #ifndef NOTSERVICE
         ServiceStatus.dwCheckPoint++;
@@ -423,8 +429,10 @@ void afsd_Main(DWORD argc, LPTSTR *argv)
         SetServiceStatus(StatusHandle, &ServiceStatus);
 #endif
                code = afsd_InitDaemons(&reason);
-               if (code != 0)
+               if (code != 0) {
+            afsi_log("afsd_InitDaemons failed: %s (code = %d)", reason, code);
                        osi_panic(reason, __FILE__, __LINE__);
+        }
 
 #ifndef NOTSERVICE
         ServiceStatus.dwCheckPoint++;
@@ -432,8 +440,10 @@ void afsd_Main(DWORD argc, LPTSTR *argv)
         SetServiceStatus(StatusHandle, &ServiceStatus);
 #endif
                code = afsd_InitSMB(&reason, DummyMessageBox);
-               if (code != 0)
+               if (code != 0) {
+            afsi_log("afsd_InitSMB failed: %s (code = %d)", reason, code);
                        osi_panic(reason, __FILE__, __LINE__);
+        }
 
 #ifndef NOTSERVICE
                ServiceStatus.dwCurrentState = SERVICE_RUNNING;
@@ -459,13 +469,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,7 +486,10 @@ 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;
@@ -511,12 +517,17 @@ main(void)
            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);
 }