more-missing-crtdbg-includes-20031203
[openafs.git] / src / WINNT / afsd / afsd_service.c
index 0ad3327..671c1ba 100644 (file)
 #include <winsock2.h>
 
 #include <osi.h>
-\r
-#ifdef DEBUG\r
-//#define NOTSERVICE\r
-#endif\r
+
+#ifdef DEBUG
+//#define NOTSERVICE
+#endif
+#ifdef _DEBUG
+#include <crtdbg.h>
+#endif
 
 extern void afsi_log(char *pattern, ...);
 
@@ -137,7 +140,10 @@ doneTrace:
        }
 }
 
+#if 1
+/* This code was moved to Drivemap.cpp*/
 /* Mount a drive into AFS if the user wants us to */
+/* DEE Could check first if we are run as SYSTEM */
 void CheckMountDrive()
 {
         char szAfsPath[_MAX_PATH];
@@ -169,20 +175,50 @@ void CheckMountDrive()
                         }
                 }
                 
+#if 0
                 sprintf(szAfsPath, "\\Device\\LanmanRedirector\\%s\\%s-AFS\\%s", szDriveToMapTo, cm_HostName, szSubMount);
         
                 dwResult = DefineDosDevice(DDD_RAW_TARGET_PATH, szDriveToMapTo, szAfsPath);
+#else
+               {
+                   NETRESOURCE nr;
+                   memset (&nr, 0x00, sizeof(NETRESOURCE));
+                   sprintf(szAfsPath,"\\\\%s-AFS\\%s",cm_HostName,szSubMount);
+                   
+                   nr.dwScope = RESOURCE_GLOBALNET;
+                   nr.dwType=RESOURCETYPE_DISK;
+                   nr.lpLocalName=szDriveToMapTo;
+                   nr.lpRemoteName=szAfsPath;
+                   nr.dwDisplayType = RESOURCEDISPLAYTYPE_SHARE;
+                   nr.dwUsage = RESOURCEUSAGE_CONNECTABLE;
+
+                   dwResult = WNetAddConnection2(&nr,NULL,NULL,FALSE);
+               }
+#endif
                 afsi_log("GlobalAutoMap of %s to %s %s", szDriveToMapTo, szSubMount, dwResult ? "succeeded" : "failed");
         }        
 
         RegCloseKey(hKey);
 }
+#endif
+
+typedef BOOL ( APIENTRY * AfsdInitHook )(void);
+#define AFSD_INIT_HOOK "AfsdInitHook"
+#define AFSD_HOOK_DLL  "afsdhook.dll"
 
 void afsd_Main()
 {
        long code;
        char *reason;
        int jmpret;
+    HANDLE hInitHookDll;
+    AfsdInitHook initHook;
+
+#ifdef _DEBUG
+    _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF /*| _CRTDBG_CHECK_ALWAYS_DF*/ | 
+                   _CRTDBG_CHECK_CRT_DF /* | _CRTDBG_DELAY_FREE_MEM_DF */ );
+#endif 
 
        osi_InitPanic(afsd_notifier);
        osi_InitTraceOption();
@@ -191,7 +227,7 @@ void afsd_Main()
 
        WaitToTerminate = CreateEvent(NULL, TRUE, FALSE, NULL);
 
-#ifndef NOTSERVICE\r
+#ifndef NOTSERVICE
        StatusHandle = RegisterServiceCtrlHandler(AFS_DAEMON_SERVICE_NAME,
                        (LPHANDLER_FUNCTION) afsd_ServiceControlHandler);
 
@@ -204,17 +240,55 @@ void afsd_Main()
        ServiceStatus.dwControlsAccepted = 0;
        SetServiceStatus(StatusHandle, &ServiceStatus);
 #endif
-{       
-        HANDLE h; char *ptbuf[1];
-       h = RegisterEventSource(NULL, AFS_DAEMON_EVENT_NAME);
-       ptbuf[0] = "AFS start pending";
-       ReportEvent(h, EVENTLOG_INFORMATION_TYPE, 0, 0, NULL, 1, 0, ptbuf, NULL);
-       DeregisterEventSource(h);
-}
+    {       
+    HANDLE h; char *ptbuf[1];
+    h = RegisterEventSource(NULL, AFS_DAEMON_EVENT_NAME);
+    ptbuf[0] = "AFS start pending";
+    ReportEvent(h, EVENTLOG_INFORMATION_TYPE, 0, 0, NULL, 1, 0, ptbuf, NULL);
+    DeregisterEventSource(h);
+    }
 
        afsi_start();
 
-       MainThreadId = GetCurrentThreadId();
+    /* allow an exit to be called prior to any initialization */
+    hInitHookDll = LoadLibrary(AFSD_HOOK_DLL);
+    if (hInitHookDll)
+    {
+        BOOL hookRc = FALSE;
+        initHook = ( AfsdInitHook ) GetProcAddress(hInitHookDll, AFSD_INIT_HOOK);
+        if (initHook)
+        {
+            hookRc = initHook();
+        }
+        FreeLibrary(hInitHookDll);
+               
+        if (hookRc == FALSE)
+        {
+            ServiceStatus.dwCurrentState = SERVICE_STOPPED;
+            ServiceStatus.dwWin32ExitCode = NO_ERROR;
+            ServiceStatus.dwCheckPoint = 0;
+            ServiceStatus.dwWaitHint = 0;
+            ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP;
+            SetServiceStatus(StatusHandle, &ServiceStatus);
+                       
+            /* exit if initialization failed */
+            return;
+        }
+        else
+        {
+            /* allow another 15 seconds to start */
+            ServiceStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
+            ServiceStatus.dwServiceSpecificExitCode = 0;
+            ServiceStatus.dwCurrentState = SERVICE_START_PENDING;
+            ServiceStatus.dwWin32ExitCode = NO_ERROR;
+            ServiceStatus.dwCheckPoint = 2;
+            ServiceStatus.dwWaitHint = 15000;
+            ServiceStatus.dwControlsAccepted = 0;
+            SetServiceStatus(StatusHandle, &ServiceStatus);
+        }
+    }
+
+    MainThreadId = GetCurrentThreadId();
        jmpret = setjmp(notifier_jmp);
 
        if (jmpret == 0) {
@@ -238,29 +312,28 @@ void afsd_Main()
                ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP;
                SetServiceStatus(StatusHandle, &ServiceStatus);
 #endif
-       {
-               HANDLE h; char *ptbuf[1];
+        {
+           HANDLE h; char *ptbuf[1];
                h = RegisterEventSource(NULL, AFS_DAEMON_EVENT_NAME);
                ptbuf[0] = "AFS running";
                ReportEvent(h, EVENTLOG_INFORMATION_TYPE, 0, 0, NULL, 1, 0, ptbuf, NULL);
                DeregisterEventSource(h);
-       }
+        }
        }
 
-        /* Check if we should mount a drive into AFS */
-        CheckMountDrive();
+    /* Check if we should mount a drive into AFS */
+    CheckMountDrive();
 
        WaitForSingleObject(WaitToTerminate, INFINITE);
        
-{   
-        HANDLE h; char *ptbuf[1];
+    {   
+    HANDLE h; char *ptbuf[1];
        h = RegisterEventSource(NULL, AFS_DAEMON_EVENT_NAME);
        ptbuf[0] = "AFS quitting";
-       ReportEvent(h,
-               GlobalStatus ? EVENTLOG_ERROR_TYPE : EVENTLOG_INFORMATION_TYPE,
-               0, 0, NULL, 1, 0, ptbuf, NULL);
-       DeregisterEventSource(h);
-}
+       ReportEvent(h, GlobalStatus ? EVENTLOG_ERROR_TYPE : EVENTLOG_INFORMATION_TYPE,
+                0, 0, NULL, 1, 0, ptbuf, NULL);
+    DeregisterEventSource(h);
+    }
 
        ServiceStatus.dwCurrentState = SERVICE_STOPPED;
        ServiceStatus.dwWin32ExitCode = NO_ERROR;
@@ -270,23 +343,32 @@ void afsd_Main()
        SetServiceStatus(StatusHandle, &ServiceStatus);
 }
 
-#ifdef NOTSERVICE
-void main()
+DWORD __stdcall afsdMain_thread(void* notUsed)
 {
        afsd_Main();
-       Sleep(1000);
-       return ;
+    return(0);
 }
-#else
-void _CRTAPI1 main()
+
+void main()
 {
-       LONG status = ERROR_SUCCESS;
        SERVICE_TABLE_ENTRY dispatchTable[] = {
                {AFS_DAEMON_SERVICE_NAME, (LPSERVICE_MAIN_FUNCTION) afsd_Main},
                {NULL, NULL}
        };
 
+    afsd_SetUnhandledExceptionFilter();
+
        if (!StartServiceCtrlDispatcher(dispatchTable))
-               status = GetLastError();
+    {
+        LONG status = GetLastError();
+           if (status == ERROR_FAILED_SERVICE_CONTROLLER_CONNECT)
+        {
+            DWORD tid;
+            CreateThread(NULL, 0, afsdMain_thread, 0, 0, &tid);
+               
+            printf("Hit <Enter> to terminate OpenAFS Client Service\n");
+            getchar();              
+            SetEvent(WaitToTerminate);
+        }
+    }
 }
-#endif