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"
28 extern void afsi_log(char *pattern, ...);
30 extern char AFSConfigKeyName[];
32 HANDLE WaitToTerminate;
36 unsigned int MainThreadId;
39 extern int traceOnPanic;
42 * Notifier function for use by osi_panic
44 static void afsd_notifier(char *msgp, char *filep, long line)
51 sprintf(tbuffer, "Error at file %s, line %d: %s",
54 sprintf(tbuffer, "Error at unknown location: %s", msgp);
56 h = RegisterEventSource(NULL, AFS_DAEMON_EVENT_NAME);
58 ReportEvent(h, EVENTLOG_ERROR_TYPE, 0, line, NULL, 1, 0, ptbuf, NULL);
59 DeregisterEventSource(h);
63 osi_LogEnable(afsd_logp);
65 afsd_ForceTrace(TRUE);
71 SetEvent(WaitToTerminate);
73 if (GetCurrentThreadId() == MainThreadId)
74 longjmp(notifier_jmp, 1);
80 * For use miscellaneously in smb.c; need to do better
82 static int DummyMessageBox(HWND h, LPCTSTR l1, LPCTSTR l2, UINT ui)
87 static SERVICE_STATUS ServiceStatus;
88 static SERVICE_STATUS_HANDLE StatusHandle;
90 void afsd_ServiceControlHandler(DWORD ctrlCode)
93 DWORD dummyLen, doTrace;
97 case SERVICE_CONTROL_STOP:
99 RpcMgmtStopServerListening(NULL);
101 /* Force trace if requested */
102 code = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
104 0, KEY_QUERY_VALUE, &parmKey);
105 if (code != ERROR_SUCCESS)
108 dummyLen = sizeof(doTrace);
109 code = RegQueryValueEx(parmKey, "TraceOnShutdown",
111 (BYTE *) &doTrace, &dummyLen);
112 RegCloseKey (parmKey);
113 if (code != ERROR_SUCCESS)
116 afsd_ForceTrace(FALSE);
119 ServiceStatus.dwCurrentState = SERVICE_STOP_PENDING;
120 ServiceStatus.dwWin32ExitCode = NO_ERROR;
121 ServiceStatus.dwCheckPoint = 1;
122 ServiceStatus.dwWaitHint = 10000;
123 ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP;
124 SetServiceStatus(StatusHandle, &ServiceStatus);
125 SetEvent(WaitToTerminate);
127 case SERVICE_CONTROL_INTERROGATE:
128 ServiceStatus.dwCurrentState = SERVICE_RUNNING;
129 ServiceStatus.dwWin32ExitCode = NO_ERROR;
130 ServiceStatus.dwCheckPoint = 0;
131 ServiceStatus.dwWaitHint = 0;
132 ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP;
133 SetServiceStatus(StatusHandle, &ServiceStatus);
135 /* XXX handle system shutdown */
136 /* XXX handle pause & continue */
141 /* This code was moved to Drivemap.cpp*/
142 /* Mount a drive into AFS if the user wants us to */
143 /* DEE Could check first if we are run as SYSTEM */
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);
176 sprintf(szAfsPath, "\\Device\\LanmanRedirector\\%s\\%s-AFS\\%s", szDriveToMapTo, cm_HostName, szSubMount);
178 dwResult = DefineDosDevice(DDD_RAW_TARGET_PATH, szDriveToMapTo, szAfsPath);
182 memset (&nr, 0x00, sizeof(NETRESOURCE));
184 sprintf(szAfsPath,"\\\\%s-AFS\\%s",cm_HostName,szSubMount);
186 nr.dwScope = RESOURCE_GLOBALNET;
187 nr.dwType=RESOURCETYPE_DISK;
188 nr.lpLocalName=szDriveToMapTo;
189 nr.lpRemoteName=szAfsPath;
190 nr.dwDisplayType = RESOURCEDISPLAYTYPE_SHARE;
191 nr.dwUsage = RESOURCEUSAGE_CONNECTABLE;
193 dwResult = WNetAddConnection2(&nr,NULL,NULL,FALSE);
196 afsi_log("GlobalAutoMap of %s to %s %s", szDriveToMapTo, szSubMount, dwResult ? "succeeded" : "failed");
209 osi_InitPanic(afsd_notifier);
210 osi_InitTraceOption();
214 WaitToTerminate = CreateEvent(NULL, TRUE, FALSE, NULL);
217 StatusHandle = RegisterServiceCtrlHandler(AFS_DAEMON_SERVICE_NAME,
218 (LPHANDLER_FUNCTION) afsd_ServiceControlHandler);
220 ServiceStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
221 ServiceStatus.dwServiceSpecificExitCode = 0;
222 ServiceStatus.dwCurrentState = SERVICE_START_PENDING;
223 ServiceStatus.dwWin32ExitCode = NO_ERROR;
224 ServiceStatus.dwCheckPoint = 1;
225 ServiceStatus.dwWaitHint = 15000;
226 ServiceStatus.dwControlsAccepted = 0;
227 SetServiceStatus(StatusHandle, &ServiceStatus);
230 HANDLE h; char *ptbuf[1];
231 h = RegisterEventSource(NULL, AFS_DAEMON_EVENT_NAME);
232 ptbuf[0] = "AFS start pending";
233 ReportEvent(h, EVENTLOG_INFORMATION_TYPE, 0, 0, NULL, 1, 0, ptbuf, NULL);
234 DeregisterEventSource(h);
239 MainThreadId = GetCurrentThreadId();
240 jmpret = setjmp(notifier_jmp);
243 code = afsd_InitCM(&reason);
245 osi_panic(reason, __FILE__, __LINE__);
247 code = afsd_InitDaemons(&reason);
249 osi_panic(reason, __FILE__, __LINE__);
251 code = afsd_InitSMB(&reason, DummyMessageBox);
253 osi_panic(reason, __FILE__, __LINE__);
256 ServiceStatus.dwCurrentState = SERVICE_RUNNING;
257 ServiceStatus.dwWin32ExitCode = NO_ERROR;
258 ServiceStatus.dwCheckPoint = 0;
259 ServiceStatus.dwWaitHint = 0;
260 ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP;
261 SetServiceStatus(StatusHandle, &ServiceStatus);
264 HANDLE h; char *ptbuf[1];
265 h = RegisterEventSource(NULL, AFS_DAEMON_EVENT_NAME);
266 ptbuf[0] = "AFS running";
267 ReportEvent(h, EVENTLOG_INFORMATION_TYPE, 0, 0, NULL, 1, 0, ptbuf, NULL);
268 DeregisterEventSource(h);
272 /* Check if we should mount a drive into AFS */
275 WaitForSingleObject(WaitToTerminate, INFINITE);
278 HANDLE h; char *ptbuf[1];
279 h = RegisterEventSource(NULL, AFS_DAEMON_EVENT_NAME);
280 ptbuf[0] = "AFS quitting";
282 GlobalStatus ? EVENTLOG_ERROR_TYPE : EVENTLOG_INFORMATION_TYPE,
283 0, 0, NULL, 1, 0, ptbuf, NULL);
284 DeregisterEventSource(h);
287 ServiceStatus.dwCurrentState = SERVICE_STOPPED;
288 ServiceStatus.dwWin32ExitCode = NO_ERROR;
289 ServiceStatus.dwCheckPoint = 0;
290 ServiceStatus.dwWaitHint = 0;
291 ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP;
292 SetServiceStatus(StatusHandle, &ServiceStatus);
305 LONG status = ERROR_SUCCESS;
306 SERVICE_TABLE_ENTRY dispatchTable[] = {
307 {AFS_DAEMON_SERVICE_NAME, (LPSERVICE_MAIN_FUNCTION) afsd_Main},
311 if (!StartServiceCtrlDispatcher(dispatchTable))
312 status = GetLastError();