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>
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 /* DEE Could check first if we are run as SYSTEM */
145 void CheckMountDrive()
147 char szAfsPath[_MAX_PATH];
148 char szDriveToMapTo[5];
154 DWORD dwSubMountSize;
155 char szSubMount[256];
158 sprintf(szKeyName, "%s\\GlobalAutoMapper", AFSConfigKeyName);
160 dwResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE, szKeyName, 0, KEY_QUERY_VALUE, &hKey);
161 if (dwResult != ERROR_SUCCESS)
165 dwDriveSize = sizeof(szDriveToMapTo);
166 dwSubMountSize = sizeof(szSubMount);
167 dwResult = RegEnumValue(hKey, dwIndex++, szDriveToMapTo, &dwDriveSize, 0, &dwType, szSubMount, &dwSubMountSize);
168 if (dwResult != ERROR_MORE_DATA) {
169 if (dwResult != ERROR_SUCCESS) {
170 if (dwResult != ERROR_NO_MORE_ITEMS)
171 afsi_log("Failed to read GlobalAutoMapper values: %d\n", dwResult);
177 sprintf(szAfsPath, "\\Device\\LanmanRedirector\\%s\\%s-AFS\\%s", szDriveToMapTo, cm_HostName, szSubMount);
179 dwResult = DefineDosDevice(DDD_RAW_TARGET_PATH, szDriveToMapTo, szAfsPath);
183 memset (&nr, 0x00, sizeof(NETRESOURCE));
185 sprintf(szAfsPath,"\\\\%s-AFS\\%s",cm_HostName,szSubMount);
187 nr.dwScope = RESOURCE_GLOBALNET;
188 nr.dwType=RESOURCETYPE_DISK;
189 nr.lpLocalName=szDriveToMapTo;
190 nr.lpRemoteName=szAfsPath;
191 nr.dwDisplayType = RESOURCEDISPLAYTYPE_SHARE;
192 nr.dwUsage = RESOURCEUSAGE_CONNECTABLE;
194 dwResult = WNetAddConnection2(&nr,NULL,NULL,FALSE);
197 afsi_log("GlobalAutoMap of %s to %s %s", szDriveToMapTo, szSubMount, dwResult ? "succeeded" : "failed");
210 osi_InitPanic(afsd_notifier);
211 osi_InitTraceOption();
215 WaitToTerminate = CreateEvent(NULL, TRUE, FALSE, NULL);
218 StatusHandle = RegisterServiceCtrlHandler(AFS_DAEMON_SERVICE_NAME,
219 (LPHANDLER_FUNCTION) afsd_ServiceControlHandler);
221 ServiceStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
222 ServiceStatus.dwServiceSpecificExitCode = 0;
223 ServiceStatus.dwCurrentState = SERVICE_START_PENDING;
224 ServiceStatus.dwWin32ExitCode = NO_ERROR;
225 ServiceStatus.dwCheckPoint = 1;
226 ServiceStatus.dwWaitHint = 15000;
227 ServiceStatus.dwControlsAccepted = 0;
228 SetServiceStatus(StatusHandle, &ServiceStatus);
231 HANDLE h; char *ptbuf[1];
232 h = RegisterEventSource(NULL, AFS_DAEMON_EVENT_NAME);
233 ptbuf[0] = "AFS start pending";
234 ReportEvent(h, EVENTLOG_INFORMATION_TYPE, 0, 0, NULL, 1, 0, ptbuf, NULL);
235 DeregisterEventSource(h);
240 MainThreadId = GetCurrentThreadId();
241 jmpret = setjmp(notifier_jmp);
244 code = afsd_InitCM(&reason);
246 osi_panic(reason, __FILE__, __LINE__);
248 code = afsd_InitDaemons(&reason);
250 osi_panic(reason, __FILE__, __LINE__);
252 code = afsd_InitSMB(&reason, DummyMessageBox);
254 osi_panic(reason, __FILE__, __LINE__);
257 ServiceStatus.dwCurrentState = SERVICE_RUNNING;
258 ServiceStatus.dwWin32ExitCode = NO_ERROR;
259 ServiceStatus.dwCheckPoint = 0;
260 ServiceStatus.dwWaitHint = 0;
261 ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP;
262 SetServiceStatus(StatusHandle, &ServiceStatus);
265 HANDLE h; char *ptbuf[1];
266 h = RegisterEventSource(NULL, AFS_DAEMON_EVENT_NAME);
267 ptbuf[0] = "AFS running";
268 ReportEvent(h, EVENTLOG_INFORMATION_TYPE, 0, 0, NULL, 1, 0, ptbuf, NULL);
269 DeregisterEventSource(h);
273 /* Check if we should mount a drive into AFS */
276 WaitForSingleObject(WaitToTerminate, INFINITE);
279 HANDLE h; char *ptbuf[1];
280 h = RegisterEventSource(NULL, AFS_DAEMON_EVENT_NAME);
281 ptbuf[0] = "AFS quitting";
283 GlobalStatus ? EVENTLOG_ERROR_TYPE : EVENTLOG_INFORMATION_TYPE,
284 0, 0, NULL, 1, 0, ptbuf, NULL);
285 DeregisterEventSource(h);
288 ServiceStatus.dwCurrentState = SERVICE_STOPPED;
289 ServiceStatus.dwWin32ExitCode = NO_ERROR;
290 ServiceStatus.dwCheckPoint = 0;
291 ServiceStatus.dwWaitHint = 0;
292 ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP;
293 SetServiceStatus(StatusHandle, &ServiceStatus);
306 LONG status = ERROR_SUCCESS;
307 SERVICE_TABLE_ENTRY dispatchTable[] = {
308 {AFS_DAEMON_SERVICE_NAME, (LPSERVICE_MAIN_FUNCTION) afsd_Main},
312 if (!StartServiceCtrlDispatcher(dispatchTable))
313 status = GetLastError();