2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
10 #include <afs/param.h>
17 #include "afsd_init.h"
21 #include <winnt/osi_malloc.h>
26 //#define NOTSERVICE
\r
29 extern void afsi_log(char *pattern, ...);
31 extern char AFSConfigKeyName[];
33 HANDLE WaitToTerminate;
37 unsigned int MainThreadId;
40 extern int traceOnPanic;
43 * Notifier function for use by osi_panic
45 static void afsd_notifier(char *msgp, char *filep, long line)
52 sprintf(tbuffer, "Error at file %s, line %d: %s",
55 sprintf(tbuffer, "Error at unknown location: %s", msgp);
57 h = RegisterEventSource(NULL, AFS_DAEMON_EVENT_NAME);
59 ReportEvent(h, EVENTLOG_ERROR_TYPE, 0, line, NULL, 1, 0, ptbuf, NULL);
60 DeregisterEventSource(h);
64 osi_LogEnable(afsd_logp);
66 afsd_ForceTrace(TRUE);
72 SetEvent(WaitToTerminate);
74 if (GetCurrentThreadId() == MainThreadId)
75 longjmp(notifier_jmp, 1);
81 * For use miscellaneously in smb.c; need to do better
83 static int DummyMessageBox(HWND h, LPCTSTR l1, LPCTSTR l2, UINT ui)
88 static SERVICE_STATUS ServiceStatus;
89 static SERVICE_STATUS_HANDLE StatusHandle;
91 void afsd_ServiceControlHandler(DWORD ctrlCode)
94 DWORD dummyLen, doTrace;
98 case SERVICE_CONTROL_STOP:
100 RpcMgmtStopServerListening(NULL);
102 /* Force trace if requested */
103 code = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
105 0, KEY_QUERY_VALUE, &parmKey);
106 if (code != ERROR_SUCCESS)
109 dummyLen = sizeof(doTrace);
110 code = RegQueryValueEx(parmKey, "TraceOnShutdown",
112 (BYTE *) &doTrace, &dummyLen);
113 RegCloseKey (parmKey);
114 if (code != ERROR_SUCCESS)
117 afsd_ForceTrace(FALSE);
120 ServiceStatus.dwCurrentState = SERVICE_STOP_PENDING;
121 ServiceStatus.dwWin32ExitCode = NO_ERROR;
122 ServiceStatus.dwCheckPoint = 1;
123 ServiceStatus.dwWaitHint = 10000;
124 ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP;
125 SetServiceStatus(StatusHandle, &ServiceStatus);
126 SetEvent(WaitToTerminate);
128 case SERVICE_CONTROL_INTERROGATE:
129 ServiceStatus.dwCurrentState = SERVICE_RUNNING;
130 ServiceStatus.dwWin32ExitCode = NO_ERROR;
131 ServiceStatus.dwCheckPoint = 0;
132 ServiceStatus.dwWaitHint = 0;
133 ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP;
134 SetServiceStatus(StatusHandle, &ServiceStatus);
136 /* XXX handle system shutdown */
137 /* XXX handle pause & continue */
142 /* This code was moved to Drivemap.cpp*/
143 /* Mount a drive into AFS if the user wants us to */
144 void CheckMountDrive()
146 char szAfsPath[_MAX_PATH];
147 char szDriveToMapTo[5];
153 DWORD dwSubMountSize;
154 char szSubMount[256];
157 sprintf(szKeyName, "%s\\GlobalAutoMapper", AFSConfigKeyName);
159 dwResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE, szKeyName, 0, KEY_QUERY_VALUE, &hKey);
160 if (dwResult != ERROR_SUCCESS)
164 dwDriveSize = sizeof(szDriveToMapTo);
165 dwSubMountSize = sizeof(szSubMount);
166 dwResult = RegEnumValue(hKey, dwIndex++, szDriveToMapTo, &dwDriveSize, 0, &dwType, szSubMount, &dwSubMountSize);
167 if (dwResult != ERROR_MORE_DATA) {
168 if (dwResult != ERROR_SUCCESS) {
169 if (dwResult != ERROR_NO_MORE_ITEMS)
170 afsi_log("Failed to read GlobalAutoMapper values: %d\n", dwResult);
175 sprintf(szAfsPath, "\\Device\\LanmanRedirector\\%s\\%s-AFS\\%s", szDriveToMapTo, cm_HostName, szSubMount);
177 dwResult = DefineDosDevice(DDD_RAW_TARGET_PATH, szDriveToMapTo, szAfsPath);
178 afsi_log("GlobalAutoMap of %s to %s %s", szDriveToMapTo, szSubMount, dwResult ? "succeeded" : "failed");
191 osi_InitPanic(afsd_notifier);
192 osi_InitTraceOption();
196 WaitToTerminate = CreateEvent(NULL, TRUE, FALSE, NULL);
199 StatusHandle = RegisterServiceCtrlHandler(AFS_DAEMON_SERVICE_NAME,
200 (LPHANDLER_FUNCTION) afsd_ServiceControlHandler);
202 ServiceStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
203 ServiceStatus.dwServiceSpecificExitCode = 0;
204 ServiceStatus.dwCurrentState = SERVICE_START_PENDING;
205 ServiceStatus.dwWin32ExitCode = NO_ERROR;
206 ServiceStatus.dwCheckPoint = 1;
207 ServiceStatus.dwWaitHint = 15000;
208 ServiceStatus.dwControlsAccepted = 0;
209 SetServiceStatus(StatusHandle, &ServiceStatus);
212 HANDLE h; char *ptbuf[1];
213 h = RegisterEventSource(NULL, AFS_DAEMON_EVENT_NAME);
214 ptbuf[0] = "AFS start pending";
215 ReportEvent(h, EVENTLOG_INFORMATION_TYPE, 0, 0, NULL, 1, 0, ptbuf, NULL);
216 DeregisterEventSource(h);
221 MainThreadId = GetCurrentThreadId();
222 jmpret = setjmp(notifier_jmp);
225 code = afsd_InitCM(&reason);
227 osi_panic(reason, __FILE__, __LINE__);
229 code = afsd_InitDaemons(&reason);
231 osi_panic(reason, __FILE__, __LINE__);
233 code = afsd_InitSMB(&reason, DummyMessageBox);
235 osi_panic(reason, __FILE__, __LINE__);
238 ServiceStatus.dwCurrentState = SERVICE_RUNNING;
239 ServiceStatus.dwWin32ExitCode = NO_ERROR;
240 ServiceStatus.dwCheckPoint = 0;
241 ServiceStatus.dwWaitHint = 0;
242 ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP;
243 SetServiceStatus(StatusHandle, &ServiceStatus);
246 HANDLE h; char *ptbuf[1];
247 h = RegisterEventSource(NULL, AFS_DAEMON_EVENT_NAME);
248 ptbuf[0] = "AFS running";
249 ReportEvent(h, EVENTLOG_INFORMATION_TYPE, 0, 0, NULL, 1, 0, ptbuf, NULL);
250 DeregisterEventSource(h);
254 /* Check if we should mount a drive into AFS */
255 /* CheckMountDrive();*/
257 WaitForSingleObject(WaitToTerminate, INFINITE);
260 HANDLE h; char *ptbuf[1];
261 h = RegisterEventSource(NULL, AFS_DAEMON_EVENT_NAME);
262 ptbuf[0] = "AFS quitting";
264 GlobalStatus ? EVENTLOG_ERROR_TYPE : EVENTLOG_INFORMATION_TYPE,
265 0, 0, NULL, 1, 0, ptbuf, NULL);
266 DeregisterEventSource(h);
269 ServiceStatus.dwCurrentState = SERVICE_STOPPED;
270 ServiceStatus.dwWin32ExitCode = NO_ERROR;
271 ServiceStatus.dwCheckPoint = 0;
272 ServiceStatus.dwWaitHint = 0;
273 ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP;
274 SetServiceStatus(StatusHandle, &ServiceStatus);
287 LONG status = ERROR_SUCCESS;
288 SERVICE_TABLE_ENTRY dispatchTable[] = {
289 {AFS_DAEMON_SERVICE_NAME, (LPSERVICE_MAIN_FUNCTION) afsd_Main},
293 if (!StartServiceCtrlDispatcher(dispatchTable))
294 status = GetLastError();