DEVEL15-windows-bkgdaemon-retry-failed-requests-20070203
[openafs.git] / src / WINNT / afsd / cm_daemon.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 #ifndef DJGPP
14 #include <windows.h>
15 #include <winsock2.h>
16 #include <iphlpapi.h>
17 #else
18 #include <netdb.h>
19 #endif /* !DJGPP */
20 #include <stdlib.h>
21 #include <malloc.h>
22 #include <string.h>
23
24 #include "afsd.h"
25
26 #include <rx/rx.h>
27 #include <rx/rx_prototypes.h>
28 #include <WINNT/afsreg.h>
29
30 #include "afsicf.h"
31
32 /* in seconds */
33 long cm_daemonCheckDownInterval  = 180;
34 long cm_daemonCheckUpInterval    = 600;
35 long cm_daemonCheckVolInterval   = 3600;
36 long cm_daemonCheckCBInterval    = 60;
37 long cm_daemonCheckLockInterval  = 60;
38 long cm_daemonTokenCheckInterval = 180;
39
40 osi_rwlock_t cm_daemonLock;
41
42 long cm_bkgQueueCount;          /* # of queued requests */
43
44 int cm_bkgWaitingForCount;      /* true if someone's waiting for cm_bkgQueueCount to drop */
45
46 cm_bkgRequest_t *cm_bkgListp;           /* first elt in the list of requests */
47 cm_bkgRequest_t *cm_bkgListEndp;        /* last elt in the list of requests */
48
49 static int daemon_ShutdownFlag = 0;
50
51 #ifndef DJGPP
52 void cm_IpAddrDaemon(long parm)
53 {
54     extern void smb_CheckVCs(void);
55
56     rx_StartClientThread();
57
58     while (daemon_ShutdownFlag == 0) {
59         DWORD Result = NotifyAddrChange(NULL,NULL);
60         if (Result == NO_ERROR && daemon_ShutdownFlag == 0) {
61             osi_Log0(afsd_logp, "cm_IpAddrDaemon CheckDownServers");
62             Sleep(2500);
63             cm_ForceNewConnectionsAllServers();
64             cm_CheckServers(CM_FLAG_CHECKUPSERVERS | CM_FLAG_CHECKDOWNSERVERS, NULL);
65             smb_CheckVCs();
66         }       
67     }
68 }
69 #endif
70
71 void cm_BkgDaemon(long parm)
72 {
73     cm_bkgRequest_t *rp;
74     afs_int32 code;
75
76     rx_StartClientThread();
77
78     lock_ObtainWrite(&cm_daemonLock);
79     while (daemon_ShutdownFlag == 0) {
80         if (!cm_bkgListEndp) {
81             osi_SleepW((LONG_PTR)&cm_bkgListp, &cm_daemonLock);
82             lock_ObtainWrite(&cm_daemonLock);
83             continue;
84         }
85                 
86         /* we found a request */
87         for (rp = cm_bkgListEndp; rp; rp = (cm_bkgRequest_t *) osi_QPrev(&rp->q))
88         {
89             if (cm_ServerAvailable(&rp->scp->fid, rp->userp))
90                 break;
91         }
92         if (rp == NULL) {
93             /* we couldn't find a request that we could process at the current time */
94             lock_ReleaseWrite(&cm_daemonLock);
95             Sleep(1000);
96             lock_ObtainWrite(&cm_daemonLock);
97             continue;
98         }
99
100         osi_QRemoveHT((osi_queue_t **) &cm_bkgListp, (osi_queue_t **) &cm_bkgListEndp, &rp->q);
101         osi_assert(cm_bkgQueueCount-- > 0);
102         lock_ReleaseWrite(&cm_daemonLock);
103
104 #ifdef DEBUG_REFCOUNT
105         osi_Log2(afsd_logp,"cm_BkgDaemon (before) scp 0x%x ref %d",rp->scp, rp->scp->refCount);
106 #endif
107         code = (*rp->procp)(rp->scp, rp->p1, rp->p2, rp->p3, rp->p4, rp->userp);
108 #ifdef DEBUG_REFCOUNT                
109         osi_Log2(afsd_logp,"cm_BkgDaemon (after) scp 0x%x ref %d",rp->scp, rp->scp->refCount);
110 #endif
111         if (code == 0) {
112             cm_ReleaseUser(rp->userp);
113             cm_ReleaseSCache(rp->scp);
114             free(rp);
115         }
116
117         lock_ObtainWrite(&cm_daemonLock);
118
119         switch ( code ) {
120         case CM_ERROR_TIMEDOUT:
121         case CM_ERROR_RETRY:
122         case CM_ERROR_WOULDBLOCK:
123         case CM_ERROR_ALLBUSY:
124         case CM_ERROR_ALLDOWN:
125         case CM_ERROR_ALLOFFLINE:
126         case CM_ERROR_PARTIALWRITE:
127             osi_Log2(afsd_logp,"cm_BkgDaemon re-queueing failed request 0x%p code 0x%x",
128                      rp, code);
129             cm_bkgQueueCount++;
130             osi_QAddT((osi_queue_t **) &cm_bkgListp, (osi_queue_t **)&cm_bkgListEndp, &rp->q);
131             break;
132         default:
133             osi_Log2(afsd_logp,"cm_BkgDaemon failed request dropped 0x%p code 0x%x",
134                      rp, code);
135         }
136     }
137     lock_ReleaseWrite(&cm_daemonLock);
138 }
139
140 void cm_QueueBKGRequest(cm_scache_t *scp, cm_bkgProc_t *procp, afs_uint32 p1, afs_uint32 p2, afs_uint32 p3, afs_uint32 p4,
141         cm_user_t *userp)
142 {
143     cm_bkgRequest_t *rp;
144         
145     rp = malloc(sizeof(*rp));
146     memset(rp, 0, sizeof(*rp));
147         
148     cm_HoldSCache(scp);
149     rp->scp = scp;
150     cm_HoldUser(userp);
151     rp->userp = userp;
152     rp->procp = procp;
153     rp->p1 = p1;
154     rp->p2 = p2;
155     rp->p3 = p3;
156     rp->p4 = p4;
157
158     lock_ObtainWrite(&cm_daemonLock);
159     cm_bkgQueueCount++;
160     osi_QAdd((osi_queue_t **) &cm_bkgListp, &rp->q);
161     if (!cm_bkgListEndp) 
162         cm_bkgListEndp = rp;
163     lock_ReleaseWrite(&cm_daemonLock);
164
165     osi_Wakeup((LONG_PTR) &cm_bkgListp);
166 }
167
168 static int
169 IsWindowsFirewallPresent(void)
170 {
171     SC_HANDLE scm;
172     SC_HANDLE svc;
173     BOOLEAN flag;
174     BOOLEAN result = FALSE;
175     LPQUERY_SERVICE_CONFIG pConfig = NULL;
176     DWORD BufSize;
177     LONG status;
178
179     /* Open services manager */
180     scm = OpenSCManager(NULL, NULL, GENERIC_READ);
181     if (!scm) return FALSE;
182
183     /* Open Windows Firewall service */
184     svc = OpenService(scm, "MpsSvc", SERVICE_QUERY_CONFIG);
185     if (!svc) {
186         afsi_log("MpsSvc Service could not be opened for query: 0x%x", GetLastError());
187         svc = OpenService(scm, "SharedAccess", SERVICE_QUERY_CONFIG);
188         if (!svc)
189             afsi_log("SharedAccess Service could not be opened for query: 0x%x", GetLastError());
190     }
191     if (!svc)
192         goto close_scm;
193
194     /* Query Windows Firewall service config, first just to get buffer size */
195     /* Expected to fail, so don't test return value */
196     (void) QueryServiceConfig(svc, NULL, 0, &BufSize);
197     status = GetLastError();
198     if (status != ERROR_INSUFFICIENT_BUFFER)
199         goto close_svc;
200
201     /* Allocate buffer */
202     pConfig = (LPQUERY_SERVICE_CONFIG)GlobalAlloc(GMEM_FIXED,BufSize);
203     if (!pConfig)
204         goto close_svc;
205
206     /* Query Windows Firewall service config, this time for real */
207     flag = QueryServiceConfig(svc, pConfig, BufSize, &BufSize);
208     if (!flag) {
209         afsi_log("QueryServiceConfig failed: 0x%x", GetLastError());
210         goto free_pConfig;
211     }
212
213     /* Is it autostart? */
214     afsi_log("AutoStart 0x%x", pConfig->dwStartType);
215     if (pConfig->dwStartType < SERVICE_DEMAND_START)
216         result = TRUE;
217
218   free_pConfig:
219     GlobalFree(pConfig);
220   close_svc:
221     CloseServiceHandle(svc);
222   close_scm:
223     CloseServiceHandle(scm);
224
225     return result;
226 }
227
228 void
229 cm_DaemonCheckInit(void)
230 {
231     HKEY parmKey;
232     DWORD dummyLen;
233     DWORD dummy;
234     DWORD code;
235
236     code = RegOpenKeyEx(HKEY_LOCAL_MACHINE, AFSREG_CLT_SVC_PARAM_SUBKEY,
237                          0, KEY_QUERY_VALUE, &parmKey);
238     if (code)
239         return;
240
241     dummyLen = sizeof(DWORD);
242     code = RegQueryValueEx(parmKey, "DownServerCheckInterval", NULL, NULL,
243                             (BYTE *) &dummy, &dummyLen);
244     if (code == ERROR_SUCCESS)
245         cm_daemonCheckDownInterval = dummy;
246     
247     dummyLen = sizeof(DWORD);
248     code = RegQueryValueEx(parmKey, "UpServerCheckInterval", NULL, NULL,
249                             (BYTE *) &dummy, &dummyLen);
250     if (code == ERROR_SUCCESS)
251         cm_daemonCheckUpInterval = dummy;
252     
253     dummyLen = sizeof(DWORD);
254     code = RegQueryValueEx(parmKey, "VolumeCheckInterval", NULL, NULL,
255                             (BYTE *) &dummy, &dummyLen);
256     if (code == ERROR_SUCCESS)
257         cm_daemonCheckVolInterval = dummy;
258     
259     dummyLen = sizeof(DWORD);
260     code = RegQueryValueEx(parmKey, "CallbackCheckInterval", NULL, NULL,
261                             (BYTE *) &dummy, &dummyLen);
262     if (code == ERROR_SUCCESS)
263         cm_daemonCheckCBInterval = dummy;
264     
265     dummyLen = sizeof(DWORD);
266     code = RegQueryValueEx(parmKey, "LockCheckInterval", NULL, NULL,
267                             (BYTE *) &dummy, &dummyLen);
268     if (code == ERROR_SUCCESS)
269         cm_daemonCheckLockInterval = dummy;
270     
271     dummyLen = sizeof(DWORD);
272     code = RegQueryValueEx(parmKey, "TokenCheckInterval", NULL, NULL,
273                             (BYTE *) &dummy, &dummyLen);
274     if (code == ERROR_SUCCESS)
275         cm_daemonTokenCheckInterval = dummy;
276     
277     RegCloseKey(parmKey);
278 }
279
280 /* periodic check daemon */
281 void cm_Daemon(long parm)
282 {
283     time_t now;
284     time_t lastLockCheck;
285     time_t lastVolCheck;
286     time_t lastCBExpirationCheck;
287     time_t lastDownServerCheck;
288     time_t lastUpServerCheck;
289     time_t lastTokenCacheCheck;
290     char thostName[200];
291     unsigned long code;
292     struct hostent *thp;
293     HMODULE hHookDll;
294     int configureFirewall = IsWindowsFirewallPresent();
295
296     if (!configureFirewall) {
297         afsi_log("No Windows Firewall detected");
298     }
299
300     /* ping all file servers, up or down, with unauthenticated connection,
301      * to find out whether we have all our callbacks from the server still.
302      * Also, ping down VLDBs.
303      */
304     /*
305      * Seed the random number generator with our own address, so that
306      * clients starting at the same time don't all do vol checks at the
307      * same time.
308      */
309     gethostname(thostName, sizeof(thostName));
310     thp = gethostbyname(thostName);
311     if (thp == NULL)    /* In djgpp, gethostname returns the netbios
312                            name of the machine.  gethostbyname will fail
313                            looking this up if it differs from DNS name. */
314         code = 0;
315     else
316         memcpy(&code, thp->h_addr_list[0], 4);
317     
318     srand(ntohl(code));
319
320     cm_DaemonCheckInit();
321
322     now = osi_Time();
323     lastVolCheck = now - cm_daemonCheckVolInterval/2 + (rand() % cm_daemonCheckVolInterval);
324     lastCBExpirationCheck = now - cm_daemonCheckCBInterval/2 + (rand() % cm_daemonCheckCBInterval);
325     lastLockCheck = now - cm_daemonCheckLockInterval/2 + (rand() % cm_daemonCheckLockInterval);
326     lastDownServerCheck = now - cm_daemonCheckDownInterval/2 + (rand() % cm_daemonCheckDownInterval);
327     lastUpServerCheck = now - cm_daemonCheckUpInterval/2 + (rand() % cm_daemonCheckUpInterval);
328     lastTokenCacheCheck = now - cm_daemonTokenCheckInterval/2 + (rand() % cm_daemonTokenCheckInterval);
329
330     while (daemon_ShutdownFlag == 0) {
331         /* check to see if the listener threads halted due to network 
332          * disconnect or other issues.  If so, attempt to restart them.
333          */
334         smb_RestartListeners();
335
336         if (configureFirewall) {
337             /* Open Microsoft Firewall to allow in port 7001 */
338             switch (icf_CheckAndAddAFSPorts(AFS_PORTSET_CLIENT)) {
339             case 0:
340                 afsi_log("Windows Firewall Configuration succeeded");
341                 configureFirewall = 0;
342                 break;
343             case 1:
344                 afsi_log("Invalid Windows Firewall Port Set");
345                 break;
346             case 2:
347                 afsi_log("Unable to open Windows Firewall Profile");
348                 break;
349             case 3:
350                 afsi_log("Unable to create/modify Windows Firewall Port entries");
351                 break;
352             default:
353                 afsi_log("Unknown Windows Firewall Configuration error");
354             }
355         } 
356
357         /* find out what time it is */
358         now = osi_Time();
359
360         /* check down servers */
361         if (now > lastDownServerCheck + cm_daemonCheckDownInterval) {
362             lastDownServerCheck = now;
363             osi_Log0(afsd_logp, "cm_Daemon CheckDownServers");
364             cm_CheckServers(CM_FLAG_CHECKDOWNSERVERS, NULL);
365             now = osi_Time();
366         }
367
368         /* check up servers */
369         if (now > lastUpServerCheck + cm_daemonCheckUpInterval) {
370             lastUpServerCheck = now;
371             osi_Log0(afsd_logp, "cm_Daemon CheckUpServers");
372             cm_CheckServers(CM_FLAG_CHECKUPSERVERS, NULL);
373             now = osi_Time();
374         }
375
376         if (now > lastVolCheck + cm_daemonCheckVolInterval) {
377             lastVolCheck = now;
378             cm_RefreshVolumes();
379             now = osi_Time();
380         }
381
382         if (now > lastCBExpirationCheck + cm_daemonCheckCBInterval) {
383             lastCBExpirationCheck = now;
384             cm_CheckCBExpiration();
385             now = osi_Time();
386         }
387
388         if (now > lastLockCheck + cm_daemonCheckLockInterval) {
389             lastLockCheck = now;
390             cm_CheckLocks();
391             now = osi_Time();
392         }
393
394         if (now > lastTokenCacheCheck + cm_daemonTokenCheckInterval) {
395             lastTokenCacheCheck = now;
396             cm_CheckTokenCache(now);
397             now = osi_Time();
398         }
399
400         /* allow an exit to be called prior to stopping the service */
401         hHookDll = LoadLibrary(AFSD_HOOK_DLL);
402         if (hHookDll)
403         {
404             BOOL hookRc = TRUE;
405             AfsdDaemonHook daemonHook = ( AfsdDaemonHook ) GetProcAddress(hHookDll, AFSD_DAEMON_HOOK);
406             if (daemonHook)
407             {
408                 hookRc = daemonHook();
409             }
410             FreeLibrary(hHookDll);
411             hHookDll = NULL;
412
413             if (hookRc == FALSE)
414             {
415                 SetEvent(WaitToTerminate);
416             }
417         }
418
419         thrd_Sleep(30 * 1000);          /* sleep 30 seconds */
420         if (daemon_ShutdownFlag == 1)
421             return;
422     }
423 }       
424
425 void cm_DaemonShutdown(void)
426 {
427     daemon_ShutdownFlag = 1;
428 }
429
430 void cm_InitDaemon(int nDaemons)
431 {
432     static osi_once_t once;
433     long pid;
434     thread_t phandle;
435     int i;
436         
437     if (osi_Once(&once)) {
438         lock_InitializeRWLock(&cm_daemonLock, "cm_daemonLock");
439         osi_EndOnce(&once);
440
441 #ifndef DJGPP
442         /* creating IP Address Change monitor daemon */
443         phandle = thrd_Create((SecurityAttrib) 0, 0,
444                                (ThreadFunc) cm_IpAddrDaemon, 0, 0, &pid, "cm_IpAddrDaemon");
445         osi_assert(phandle != NULL);
446         thrd_CloseHandle(phandle);
447 #endif /* DJGPP */
448
449         /* creating pinging daemon */
450         phandle = thrd_Create((SecurityAttrib) 0, 0,
451                                (ThreadFunc) cm_Daemon, 0, 0, &pid, "cm_Daemon");
452         osi_assert(phandle != NULL);
453         thrd_CloseHandle(phandle);
454
455         for(i=0; i < nDaemons; i++) {
456             phandle = thrd_Create((SecurityAttrib) 0, 0,
457                                    (ThreadFunc) cm_BkgDaemon, 0, 0, &pid,
458                                    "cm_BkgDaemon");
459             osi_assert(phandle != NULL);
460             thrd_CloseHandle(phandle);
461         }
462     }
463 }