windows-updates-including-dont-lose-tokens-20011110
[openafs.git] / src / WINNT / afsd / afsd_service.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
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
8  */
9
10 #include <afs/param.h>
11 #include <afs/stds.h>
12
13 #include <windows.h>
14 #include <string.h>
15 #include <setjmp.h>
16 #include "afsd.h"
17 #include "afsd_init.h"
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <winsock2.h>
21
22 #include <osi.h>
23 \r
24 #ifdef DEBUG\r
25 //#define NOTSERVICE\r
26 #endif\r
27
28 extern void afsi_log(char *pattern, ...);
29
30 extern char AFSConfigKeyName[];
31
32 HANDLE WaitToTerminate;
33
34 int GlobalStatus;
35
36 unsigned int MainThreadId;
37 jmp_buf notifier_jmp;
38
39 extern int traceOnPanic;
40
41 /*
42  * Notifier function for use by osi_panic
43  */
44 static void afsd_notifier(char *msgp, char *filep, long line)
45 {
46         char tbuffer[100];
47         char *ptbuf[1];
48         HANDLE h;
49
50         if (filep)
51                 sprintf(tbuffer, "Error at file %s, line %d: %s",
52                         filep, line, msgp);
53         else
54                 sprintf(tbuffer, "Error at unknown location: %s", msgp);
55
56         h = RegisterEventSource(NULL, AFS_DAEMON_EVENT_NAME);
57         ptbuf[0] = tbuffer;
58         ReportEvent(h, EVENTLOG_ERROR_TYPE, 0, line, NULL, 1, 0, ptbuf, NULL);
59         DeregisterEventSource(h);
60
61         GlobalStatus = line;
62
63         osi_LogEnable(afsd_logp);
64
65         afsd_ForceTrace(TRUE);
66
67         if (traceOnPanic) {
68                 _asm int 3h;
69         }
70
71         SetEvent(WaitToTerminate);
72
73         if (GetCurrentThreadId() == MainThreadId)
74                 longjmp(notifier_jmp, 1);
75         else
76                 ExitThread(1);
77 }
78
79 /*
80  * For use miscellaneously in smb.c; need to do better
81  */
82 static int DummyMessageBox(HWND h, LPCTSTR l1, LPCTSTR l2, UINT ui)
83 {
84         return 0;
85 }
86
87 static SERVICE_STATUS           ServiceStatus;
88 static SERVICE_STATUS_HANDLE    StatusHandle;
89
90 void afsd_ServiceControlHandler(DWORD ctrlCode)
91 {
92         HKEY parmKey;
93         DWORD dummyLen, doTrace;
94         long code;
95
96         switch (ctrlCode) {
97                 case SERVICE_CONTROL_STOP:
98                         /* Shutdown RPC */
99                         RpcMgmtStopServerListening(NULL);
100
101                         /* Force trace if requested */
102                         code = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
103                                             AFSConfigKeyName,
104                                             0, KEY_QUERY_VALUE, &parmKey);
105                         if (code != ERROR_SUCCESS)
106                                 goto doneTrace;
107
108                         dummyLen = sizeof(doTrace);
109                         code = RegQueryValueEx(parmKey, "TraceOnShutdown",
110                                                 NULL, NULL,
111                                                 (BYTE *) &doTrace, &dummyLen);
112                         RegCloseKey (parmKey);
113                         if (code != ERROR_SUCCESS)
114                                 doTrace = 0;
115                         if (doTrace)
116                                 afsd_ForceTrace(FALSE);
117
118 doneTrace:
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);
126                         break;
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);
134                         break;
135                 /* XXX handle system shutdown */
136                 /* XXX handle pause & continue */
137         }
138 }
139
140 /* Mount a drive into AFS if the user wants us to */
141 void CheckMountDrive()
142 {
143         char szAfsPath[_MAX_PATH];
144         char szDriveToMapTo[5];
145         DWORD dwResult;
146         char szKeyName[256];
147         HKEY hKey;
148         DWORD dwIndex = 0;
149         DWORD dwDriveSize;
150         DWORD dwSubMountSize;
151         char szSubMount[256];
152         DWORD dwType;
153
154         sprintf(szKeyName, "%s\\GlobalAutoMapper", AFSConfigKeyName);
155
156         dwResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE, szKeyName, 0, KEY_QUERY_VALUE, &hKey);
157         if (dwResult != ERROR_SUCCESS)
158                 return;
159
160         while (1) {
161                 dwDriveSize = sizeof(szDriveToMapTo);
162                 dwSubMountSize = sizeof(szSubMount);
163                 dwResult = RegEnumValue(hKey, dwIndex++, szDriveToMapTo, &dwDriveSize, 0, &dwType, szSubMount, &dwSubMountSize);
164                 if (dwResult != ERROR_MORE_DATA) {
165                         if (dwResult != ERROR_SUCCESS) {
166                                 if (dwResult != ERROR_NO_MORE_ITEMS)
167                                         afsi_log("Failed to read GlobalAutoMapper values: %d\n", dwResult);
168                                 break;
169                         }
170                 }
171                 
172                 sprintf(szAfsPath, "\\Device\\LanmanRedirector\\%s\\%s-AFS\\%s", szDriveToMapTo, cm_HostName, szSubMount);
173         
174                 dwResult = DefineDosDevice(DDD_RAW_TARGET_PATH, szDriveToMapTo, szAfsPath);
175                 afsi_log("GlobalAutoMap of %s to %s %s", szDriveToMapTo, szSubMount, dwResult ? "succeeded" : "failed");
176         }        
177
178         RegCloseKey(hKey);
179 }
180
181 void afsd_Main()
182 {
183         long code;
184         char *reason;
185         int jmpret;
186
187         osi_InitPanic(afsd_notifier);
188         osi_InitTraceOption();
189
190         GlobalStatus = 0;
191
192         WaitToTerminate = CreateEvent(NULL, TRUE, FALSE, NULL);
193
194 #ifndef NOTSERVICE\r
195         StatusHandle = RegisterServiceCtrlHandler(AFS_DAEMON_SERVICE_NAME,
196                         (LPHANDLER_FUNCTION) afsd_ServiceControlHandler);
197
198         ServiceStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
199         ServiceStatus.dwServiceSpecificExitCode = 0;
200         ServiceStatus.dwCurrentState = SERVICE_START_PENDING;
201         ServiceStatus.dwWin32ExitCode = NO_ERROR;
202         ServiceStatus.dwCheckPoint = 1;
203         ServiceStatus.dwWaitHint = 15000;
204         ServiceStatus.dwControlsAccepted = 0;
205         SetServiceStatus(StatusHandle, &ServiceStatus);
206 #endif
207 {       
208         HANDLE h; char *ptbuf[1];
209         h = RegisterEventSource(NULL, AFS_DAEMON_EVENT_NAME);
210         ptbuf[0] = "AFS start pending";
211         ReportEvent(h, EVENTLOG_INFORMATION_TYPE, 0, 0, NULL, 1, 0, ptbuf, NULL);
212         DeregisterEventSource(h);
213 }
214
215         afsi_start();
216
217         MainThreadId = GetCurrentThreadId();
218         jmpret = setjmp(notifier_jmp);
219
220         if (jmpret == 0) {
221                 code = afsd_InitCM(&reason);
222                 if (code != 0)
223                         osi_panic(reason, __FILE__, __LINE__);
224
225                 code = afsd_InitDaemons(&reason);
226                 if (code != 0)
227                         osi_panic(reason, __FILE__, __LINE__);
228
229                 code = afsd_InitSMB(&reason, DummyMessageBox);
230                 if (code != 0)
231                         osi_panic(reason, __FILE__, __LINE__);
232
233 #ifndef NOTSERVICE
234                 ServiceStatus.dwCurrentState = SERVICE_RUNNING;
235                 ServiceStatus.dwWin32ExitCode = NO_ERROR;
236                 ServiceStatus.dwCheckPoint = 0;
237                 ServiceStatus.dwWaitHint = 0;
238                 ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP;
239                 SetServiceStatus(StatusHandle, &ServiceStatus);
240 #endif
241         {
242                 HANDLE h; char *ptbuf[1];
243                 h = RegisterEventSource(NULL, AFS_DAEMON_EVENT_NAME);
244                 ptbuf[0] = "AFS running";
245                 ReportEvent(h, EVENTLOG_INFORMATION_TYPE, 0, 0, NULL, 1, 0, ptbuf, NULL);
246                 DeregisterEventSource(h);
247         }
248         }
249
250         /* Check if we should mount a drive into AFS */
251         CheckMountDrive();
252
253         WaitForSingleObject(WaitToTerminate, INFINITE);
254         
255 {   
256         HANDLE h; char *ptbuf[1];
257         h = RegisterEventSource(NULL, AFS_DAEMON_EVENT_NAME);
258         ptbuf[0] = "AFS quitting";
259         ReportEvent(h,
260                 GlobalStatus ? EVENTLOG_ERROR_TYPE : EVENTLOG_INFORMATION_TYPE,
261                 0, 0, NULL, 1, 0, ptbuf, NULL);
262         DeregisterEventSource(h);
263 }
264
265         ServiceStatus.dwCurrentState = SERVICE_STOPPED;
266         ServiceStatus.dwWin32ExitCode = NO_ERROR;
267         ServiceStatus.dwCheckPoint = 0;
268         ServiceStatus.dwWaitHint = 0;
269         ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP;
270         SetServiceStatus(StatusHandle, &ServiceStatus);
271 }
272
273 #ifdef NOTSERVICE
274 void main()
275 {
276         afsd_Main();
277         Sleep(1000);
278         return ;
279 }
280 #else
281 void _CRTAPI1 main()
282 {
283         LONG status = ERROR_SUCCESS;
284         SERVICE_TABLE_ENTRY dispatchTable[] = {
285                 {AFS_DAEMON_SERVICE_NAME, (LPSERVICE_MAIN_FUNCTION) afsd_Main},
286                 {NULL, NULL}
287         };
288
289         if (!StartServiceCtrlDispatcher(dispatchTable))
290                 status = GetLastError();
291 }
292 #endif