Windows: Clear peer RPC stats on IP addr change
[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 <afsconfig.h>
11 #include <afs/param.h>
12 #include <roken.h>
13
14 #include <afs/stds.h>
15
16 #include <windows.h>
17 #include <winsock2.h>
18 #include <iphlpapi.h>
19 #include <stdlib.h>
20 #include <malloc.h>
21 #include <string.h>
22
23 #include "afsd.h"
24 #include "smb.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    = 240;
35 long cm_daemonCheckVolInterval   = 3600;
36 long cm_daemonCheckCBInterval    = 60;
37 long cm_daemonCheckVolCBInterval = 0;
38 long cm_daemonCheckLockInterval  = 60;
39 long cm_daemonTokenCheckInterval = 180;
40 long cm_daemonCheckOfflineVolInterval = 600;
41 long cm_daemonPerformanceTuningInterval = 0;
42 long cm_daemonRankServerInterval = 600;
43 long cm_daemonRDRShakeExtentsInterval = 0;
44 long cm_daemonAfsdHookReloadInterval = 0;
45 long cm_daemonEAccesCheckInterval = 1800;
46
47 osi_rwlock_t *cm_daemonLockp;
48 afs_uint64 *cm_bkgQueueCountp;          /* # of queued requests */
49 cm_bkgRequest_t **cm_bkgListpp;         /* first elt in the list of requests */
50 cm_bkgRequest_t **cm_bkgListEndpp;      /* last elt in the list of requests */
51
52 extern int powerStateSuspended;
53 int daemon_ShutdownFlag = 0;
54 int cm_nDaemons = 0;
55 static time_t lastIPAddrChange = 0;
56
57 static EVENT_HANDLE cm_Daemon_ShutdownEvent = NULL;
58 static EVENT_HANDLE cm_LockDaemon_ShutdownEvent = NULL;
59 static EVENT_HANDLE cm_IPAddrDaemon_ShutdownEvent = NULL;
60 static EVENT_HANDLE cm_BkgDaemon_ShutdownEvent[CM_MAX_DAEMONS] =
61        {NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL};
62
63 void * cm_IpAddrDaemon(void * vparm)
64 {
65     extern void smb_CheckVCs(void);
66     char * name = "cm_IPAddrDaemon_ShutdownEvent";
67
68     cm_IPAddrDaemon_ShutdownEvent = thrd_CreateEvent(NULL, FALSE, FALSE, name);
69     if ( GetLastError() == ERROR_ALREADY_EXISTS )
70         afsi_log("Event Object Already Exists: %s", name);
71
72     rx_StartClientThread();
73
74     while (daemon_ShutdownFlag == 0) {
75         DWORD Result;
76
77         thrd_SetEvent(cm_IPAddrDaemon_ShutdownEvent);
78         Result = NotifyAddrChange(NULL,NULL);
79         if (Result == NO_ERROR && daemon_ShutdownFlag == 0) {
80             lastIPAddrChange = osi_Time();
81             if (smb_Enabled)
82                 smb_SetLanAdapterChangeDetected();
83             cm_SetLanAdapterChangeDetected();
84             thrd_ResetEvent(cm_IPAddrDaemon_ShutdownEvent);
85
86             cm_ServerClearRPCStats();
87         }
88     }
89
90     thrd_SetEvent(cm_IPAddrDaemon_ShutdownEvent);
91     pthread_exit(NULL);
92     return NULL;
93 }
94
95 afs_int32 cm_RequestWillBlock(cm_bkgRequest_t *rp)
96 {
97     afs_int32 willBlock = 0;
98
99     if (rp->procp == cm_BkgStore) {
100         /*
101          * If the datastoring flag is set, it means that another
102          * thread is already performing an exclusive store operation
103          * on this file.  The exclusive state will be cleared once
104          * the file server locks the vnode.  Therefore, at most two
105          * threads can be actively involved in storing data at a time
106          * on a file.
107          */
108         lock_ObtainRead(&rp->scp->rw);
109         willBlock = (rp->scp->flags & CM_SCACHEFLAG_DATASTORING);
110         lock_ReleaseRead(&rp->scp->rw);
111     }
112     else if (rp->procp == RDR_BkgFetch || rp->procp == cm_BkgPrefetch) {
113         /*
114          * Attempt to determine if there is a conflict on the requested
115          * range of the file.  If the first in the range does not exist
116          * in the cache assume there is no conflict.  If the buffer does
117          * exist, check to see if an I/O operation is in progress
118          * by using the writing and reading flags as an indicator.
119          */
120         osi_hyper_t base;
121         cm_buf_t *bufp = NULL;
122
123         base.LowPart = rp->p1;
124         base.HighPart = rp->p2;
125
126         bufp = buf_Find(&rp->scp->fid, &base);
127         if (bufp) {
128             willBlock = (bufp->flags & (CM_BUF_WRITING|CM_BUF_READING));
129             buf_Release(bufp);
130         }
131     }
132
133     return willBlock;
134 }
135
136 void * cm_BkgDaemon(void * vparm)
137 {
138     cm_bkgRequest_t *rp;
139     afs_int32 code;
140     char name[32] = "";
141     long daemonID = (long)(LONG_PTR)vparm;
142
143     snprintf(name, sizeof(name), "cm_BkgDaemon_ShutdownEvent%u", daemonID);
144
145     cm_BkgDaemon_ShutdownEvent[daemonID] = thrd_CreateEvent(NULL, FALSE, FALSE, name);
146     if ( GetLastError() == ERROR_ALREADY_EXISTS )
147         afsi_log("Event Object Already Exists: %s", name);
148
149     rx_StartClientThread();
150
151     lock_ObtainWrite(&cm_daemonLockp[daemonID]);
152     while (daemon_ShutdownFlag == 0) {
153         int willBlock = 0;
154
155         if (powerStateSuspended) {
156             Sleep(1000);
157             continue;
158         }
159         if (!cm_bkgListEndpp[daemonID]) {
160             osi_SleepW((LONG_PTR)&cm_bkgListpp[daemonID], &cm_daemonLockp[daemonID]);
161             lock_ObtainWrite(&cm_daemonLockp[daemonID]);
162             continue;
163         }
164
165         /* we found a request */
166         for (rp = cm_bkgListEndpp[daemonID]; rp; rp = (cm_bkgRequest_t *) osi_QPrev(&rp->q))
167         {
168             if (rp->scp->flags & CM_SCACHEFLAG_DELETED)
169                 break;
170
171             /*
172              * If the request has active I/O such that this worker would
173              * be forced to block, leave the request in the queue and move
174              * on to one that might be available for servicing.
175              */
176             if (cm_RequestWillBlock(rp)) {
177                 willBlock++;
178                 continue;
179             }
180
181             if (cm_ServerAvailable(&rp->scp->fid, rp->userp))
182                 break;
183         }
184
185         if (rp == NULL) {
186             /*
187              * Couldn't find a request that we could process at the
188              * current time.  If there were requests that would cause
189              * the worker to block, sleep for 25ms so it can promptly
190              * respond when it is available.  Otherwise, sleep for 1s.
191              *
192              * This polling cycle needs to be replaced with a proper
193              * producer/consumer dynamic worker pool.
194              */
195             osi_Log2(afsd_logp,"cm_BkgDaemon[%u] sleeping %dms all tasks would block",
196                      daemonID, willBlock ? 100 : 1000);
197
198             lock_ReleaseWrite(&cm_daemonLockp[daemonID]);
199             Sleep(willBlock ? 100 : 1000);
200             lock_ObtainWrite(&cm_daemonLockp[daemonID]);
201             continue;
202         }
203
204         osi_QRemoveHT((osi_queue_t **) &cm_bkgListpp[daemonID], (osi_queue_t **) &cm_bkgListEndpp[daemonID], &rp->q);
205         osi_assertx(cm_bkgQueueCountp[daemonID]-- > 0, "cm_bkgQueueCount 0");
206         lock_ReleaseWrite(&cm_daemonLockp[daemonID]);
207
208         osi_Log2(afsd_logp,"cm_BkgDaemon[%u] processing request 0x%p", daemonID, rp);
209
210         if (rp->scp->flags & CM_SCACHEFLAG_DELETED) {
211             osi_Log2(afsd_logp,"cm_BkgDaemon[%u] DELETED scp 0x%x", daemonID, rp->scp);
212             code = CM_ERROR_BADFD;
213         } else {
214 #ifdef DEBUG_REFCOUNT
215             osi_Log3(afsd_logp,"cm_BkgDaemon[%u] (before) scp 0x%x ref %d", daemonID, rp->scp, rp->scp->refCount);
216 #endif
217             code = (*rp->procp)(rp->scp, rp->p1, rp->p2, rp->p3, rp->p4, rp->userp, &rp->req);
218 #ifdef DEBUG_REFCOUNT
219             osi_Log3(afsd_logp,"cm_BkgDaemon[%u] (after) scp 0x%x ref %d", daemonID, rp->scp, rp->scp->refCount);
220 #endif
221         }
222
223         /*
224          * Keep the following list synchronized with the
225          * error code list in cm_BkgStore.
226          * cm_SyncOpDone(CM_SCACHESYNC_ASYNCSTORE) will be called there unless
227          * one of these errors has occurred.
228          */
229         switch ( code ) {
230         case CM_ERROR_TIMEDOUT: /* or server restarting */
231         case CM_ERROR_RETRY:
232         case CM_ERROR_WOULDBLOCK:
233         case CM_ERROR_ALLBUSY:
234         case CM_ERROR_ALLDOWN:
235         case CM_ERROR_ALLOFFLINE:
236         case CM_ERROR_PARTIALWRITE:
237             if (rp->procp == cm_BkgStore ||
238                 rp->procp == RDR_BkgFetch) {
239                 osi_Log3(afsd_logp,
240                          "cm_BkgDaemon[%u] re-queueing failed request 0x%p code 0x%x",
241                          daemonID, rp, code);
242                 lock_ObtainWrite(&cm_daemonLockp[daemonID]);
243                 cm_bkgQueueCountp[daemonID]++;
244                 osi_QAddT((osi_queue_t **) &cm_bkgListpp[daemonID], (osi_queue_t **)&cm_bkgListEndpp[daemonID], &rp->q);
245                 break;
246             } /* otherwise fall through */
247         case 0:  /* success */
248         default: /* other error */
249             if (code == 0) {
250                 osi_Log2(afsd_logp,"cm_BkgDaemon[%u] SUCCESS: request 0x%p", daemonID, rp);
251             } else {
252                 osi_Log3(afsd_logp,"cm_BkgDaemon[%u] FAILED: request dropped 0x%p code 0x%x",
253                          daemonID, rp, code);
254             }
255             cm_ReleaseUser(rp->userp);
256             cm_ReleaseSCache(rp->scp);
257             free(rp);
258             lock_ObtainWrite(&cm_daemonLockp[daemonID]);
259         }
260     }
261     lock_ReleaseWrite(&cm_daemonLockp[daemonID]);
262     thrd_SetEvent(cm_BkgDaemon_ShutdownEvent[daemonID]);
263     pthread_exit(NULL);
264     return NULL;
265 }
266
267 void cm_QueueBKGRequest(cm_scache_t *scp, cm_bkgProc_t *procp, afs_uint32 p1, afs_uint32 p2, afs_uint32 p3, afs_uint32 p4,
268         cm_user_t *userp, cm_req_t *reqp)
269 {
270     cm_bkgRequest_t *rp, *rpq;
271     afs_uint32 daemonID;
272     int duplicate = 0;
273
274     rp = malloc(sizeof(*rp));
275     memset(rp, 0, sizeof(*rp));
276
277     cm_HoldSCache(scp);
278     rp->scp = scp;
279     cm_HoldUser(userp);
280     rp->userp = userp;
281     rp->procp = procp;
282     rp->p1 = p1;
283     rp->p2 = p2;
284     rp->p3 = p3;
285     rp->p4 = p4;
286     rp->req = *reqp;
287
288     /* Use separate queues for fetch and store operations */
289     daemonID = scp->fid.hash % (cm_nDaemons/2) * 2;
290     if (procp == cm_BkgStore)
291         daemonID++;
292
293     lock_ObtainWrite(&cm_daemonLockp[daemonID]);
294     /* Check to see if this is a duplicate request */
295     for (rpq = cm_bkgListpp[daemonID]; rpq; rpq = (cm_bkgRequest_t *) osi_QNext(&rpq->q))
296     {
297         if ( rpq->p1 == p1 &&
298              rpq->p3 == p3 &&
299              rpq->procp == procp &&
300              rpq->p2 == p2 &&
301              rpq->p4 == p4 &&
302              rpq->scp == scp &&
303              rpq->userp == userp)
304         {
305             /* found a duplicate; update request with latest info */
306             duplicate = 1;
307             break;
308         }
309     }
310
311     if (!duplicate) {
312         cm_bkgQueueCountp[daemonID]++;
313         osi_QAddH((osi_queue_t **) &cm_bkgListpp[daemonID], (osi_queue_t **)&cm_bkgListEndpp[daemonID], &rp->q);
314     }
315     lock_ReleaseWrite(&cm_daemonLockp[daemonID]);
316
317     if (duplicate) {
318         cm_ReleaseSCache(scp);
319         cm_ReleaseUser(userp);
320         free(rp);
321     } else {
322         osi_Wakeup((LONG_PTR) &cm_bkgListpp[daemonID]);
323     }
324 }
325
326 static int
327 IsWindowsFirewallPresent(void)
328 {
329     SC_HANDLE scm;
330     SC_HANDLE svc;
331     BOOLEAN flag;
332     BOOLEAN result = FALSE;
333     LPQUERY_SERVICE_CONFIG pConfig = NULL;
334     DWORD BufSize;
335     LONG status;
336
337     /* Open services manager */
338     scm = OpenSCManager(NULL, NULL, GENERIC_READ);
339     if (!scm) return FALSE;
340
341     /* Open Windows Firewall service */
342     svc = OpenService(scm, "MpsSvc", SERVICE_QUERY_CONFIG);
343     if (!svc) {
344         afsi_log("MpsSvc Service could not be opened for query: 0x%x", GetLastError());
345         svc = OpenService(scm, "SharedAccess", SERVICE_QUERY_CONFIG);
346         if (!svc)
347             afsi_log("SharedAccess Service could not be opened for query: 0x%x", GetLastError());
348     }
349     if (!svc)
350         goto close_scm;
351
352     /* Query Windows Firewall service config, first just to get buffer size */
353     /* Expected to fail, so don't test return value */
354     (void) QueryServiceConfig(svc, NULL, 0, &BufSize);
355     status = GetLastError();
356     if (status != ERROR_INSUFFICIENT_BUFFER)
357         goto close_svc;
358
359     /* Allocate buffer */
360     pConfig = (LPQUERY_SERVICE_CONFIG)GlobalAlloc(GMEM_FIXED,BufSize);
361     if (!pConfig)
362         goto close_svc;
363
364     /* Query Windows Firewall service config, this time for real */
365     flag = QueryServiceConfig(svc, pConfig, BufSize, &BufSize);
366     if (!flag) {
367         afsi_log("QueryServiceConfig failed: 0x%x", GetLastError());
368         goto free_pConfig;
369     }
370
371     /* Is it autostart? */
372     afsi_log("AutoStart 0x%x", pConfig->dwStartType);
373     if (pConfig->dwStartType < SERVICE_DEMAND_START)
374         result = TRUE;
375
376   free_pConfig:
377     GlobalFree(pConfig);
378   close_svc:
379     CloseServiceHandle(svc);
380   close_scm:
381     CloseServiceHandle(scm);
382
383     return result;
384 }
385
386 void
387 cm_DaemonCheckInit(void)
388 {
389     HKEY parmKey;
390     DWORD dummyLen;
391     DWORD dummy;
392     DWORD code;
393
394     code = RegOpenKeyEx(HKEY_LOCAL_MACHINE, AFSREG_CLT_SVC_PARAM_SUBKEY,
395                          0, KEY_QUERY_VALUE, &parmKey);
396     if (code)
397         return;
398
399     dummyLen = sizeof(DWORD);
400     code = RegQueryValueEx(parmKey, "daemonCheckDownInterval", NULL, NULL,
401                             (BYTE *) &dummy, &dummyLen);
402     if (code == ERROR_SUCCESS && dummy)
403         cm_daemonCheckDownInterval = dummy;
404     afsi_log("daemonCheckDownInterval is %d", cm_daemonCheckDownInterval);
405
406     dummyLen = sizeof(DWORD);
407     code = RegQueryValueEx(parmKey, "daemonCheckUpInterval", NULL, NULL,
408                             (BYTE *) &dummy, &dummyLen);
409     if (code == ERROR_SUCCESS && dummy)
410         cm_daemonCheckUpInterval = dummy;
411     afsi_log("daemonCheckUpInterval is %d", cm_daemonCheckUpInterval);
412
413     dummyLen = sizeof(DWORD);
414     code = RegQueryValueEx(parmKey, "daemonCheckVolInterval", NULL, NULL,
415                             (BYTE *) &dummy, &dummyLen);
416     if (code == ERROR_SUCCESS && dummy)
417         cm_daemonCheckVolInterval = dummy;
418     afsi_log("daemonCheckVolInterval is %d", cm_daemonCheckVolInterval);
419
420     dummyLen = sizeof(DWORD);
421     code = RegQueryValueEx(parmKey, "daemonCheckCBInterval", NULL, NULL,
422                             (BYTE *) &dummy, &dummyLen);
423     if (code == ERROR_SUCCESS && dummy)
424         cm_daemonCheckCBInterval = dummy;
425     afsi_log("daemonCheckCBInterval is %d", cm_daemonCheckCBInterval);
426
427     dummyLen = sizeof(DWORD);
428     code = RegQueryValueEx(parmKey, "daemonCheckVolCBInterval", NULL, NULL,
429                             (BYTE *) &dummy, &dummyLen);
430     if (code == ERROR_SUCCESS && dummy)
431         cm_daemonCheckVolCBInterval = dummy;
432     afsi_log("daemonCheckVolCBInterval is %d", cm_daemonCheckVolCBInterval);
433
434     dummyLen = sizeof(DWORD);
435     code = RegQueryValueEx(parmKey, "daemonCheckLockInterval", NULL, NULL,
436                             (BYTE *) &dummy, &dummyLen);
437     if (code == ERROR_SUCCESS && dummy)
438         cm_daemonCheckLockInterval = dummy;
439     afsi_log("daemonCheckLockInterval is %d", cm_daemonCheckLockInterval);
440
441     dummyLen = sizeof(DWORD);
442     code = RegQueryValueEx(parmKey, "daemonCheckTokenInterval", NULL, NULL,
443                             (BYTE *) &dummy, &dummyLen);
444     if (code == ERROR_SUCCESS && dummy)
445         cm_daemonTokenCheckInterval = dummy;
446     afsi_log("daemonCheckTokenInterval is %d", cm_daemonTokenCheckInterval);
447
448     dummyLen = sizeof(DWORD);
449     code = RegQueryValueEx(parmKey, "daemonCheckOfflineVolInterval", NULL, NULL,
450                             (BYTE *) &dummy, &dummyLen);
451     if (code == ERROR_SUCCESS && dummy)
452         cm_daemonCheckOfflineVolInterval = dummy;
453     afsi_log("daemonCheckOfflineVolInterval is %d", cm_daemonCheckOfflineVolInterval);
454
455     dummyLen = sizeof(DWORD);
456     code = RegQueryValueEx(parmKey, "daemonRDRShakeExtentsInterval", NULL, NULL,
457                             (BYTE *) &dummy, &dummyLen);
458     if (code == ERROR_SUCCESS && dummy)
459         cm_daemonRDRShakeExtentsInterval = dummy;
460     afsi_log("daemonRDRShakeExtentsInterval is %d", cm_daemonRDRShakeExtentsInterval);
461
462     dummyLen = sizeof(DWORD);
463     code = RegQueryValueEx(parmKey, "daemonPerformanceTuningInterval", NULL, NULL,
464                             (BYTE *) &dummy, &dummyLen);
465     if (code == ERROR_SUCCESS)
466         cm_daemonPerformanceTuningInterval = dummy;
467     afsi_log("daemonPerformanceTuningInterval is %d", cm_daemonPerformanceTuningInterval);
468
469     dummyLen = sizeof(DWORD);
470     code = RegQueryValueEx(parmKey, "daemonRankServerInterval", NULL, NULL,
471                             (BYTE *) &dummy, &dummyLen);
472     if (code == ERROR_SUCCESS && dummy)
473         cm_daemonRankServerInterval = dummy;
474     afsi_log("daemonRankServerInterval is %d", cm_daemonRankServerInterval);
475
476     dummyLen = sizeof(DWORD);
477     code = RegQueryValueEx(parmKey, "daemonAfsdHookReloadInterval", NULL, NULL,
478                             (BYTE *) &dummy, &dummyLen);
479     if (code == ERROR_SUCCESS && dummy)
480         cm_daemonAfsdHookReloadInterval = dummy;
481     afsi_log("daemonAfsdHookReloadInterval is %d", cm_daemonAfsdHookReloadInterval);
482
483     RegCloseKey(parmKey);
484
485     if (cm_daemonPerformanceTuningInterval)
486         cm_PerformanceTuningInit();
487 }
488
489 /* periodic lock check daemon */
490 void * cm_LockDaemon(void * vparm)
491 {
492     time_t now;
493     time_t lastLockCheck;
494     char * name = "cm_LockDaemon_ShutdownEvent";
495
496     cm_LockDaemon_ShutdownEvent = thrd_CreateEvent(NULL, FALSE, FALSE, name);
497     if ( GetLastError() == ERROR_ALREADY_EXISTS )
498         afsi_log("Event Object Already Exists: %s", name);
499
500     now = osi_Time();
501     lastLockCheck = now - cm_daemonCheckLockInterval/2 + (rand() % cm_daemonCheckLockInterval);
502
503     while (daemon_ShutdownFlag == 0) {
504         if (powerStateSuspended) {
505             Sleep(1000);
506             continue;
507         }
508
509         now = osi_Time();
510
511         if (now > lastLockCheck + cm_daemonCheckLockInterval &&
512             daemon_ShutdownFlag == 0 &&
513             powerStateSuspended == 0) {
514             lastLockCheck = now;
515             cm_CheckLocks();
516             if (daemon_ShutdownFlag == 1)
517                 break;
518         }
519
520         thrd_Sleep(1000);               /* sleep 1 second */
521     }
522     thrd_SetEvent(cm_LockDaemon_ShutdownEvent);
523     pthread_exit(NULL);
524     return NULL;
525 }
526
527 /* periodic check daemon */
528 void * cm_Daemon(void *vparm)
529 {
530     time_t now;
531     time_t lastVolCheck;
532     time_t lastCBExpirationCheck;
533     time_t lastVolCBRenewalCheck;
534     time_t lastDownServerCheck;
535     time_t lastUpServerCheck;
536     time_t lastTokenCacheCheck;
537     time_t lastBusyVolCheck;
538     time_t lastPerformanceCheck;
539     time_t lastServerRankCheck;
540     time_t lastRDRShakeExtents;
541     time_t lastAfsdHookReload;
542     time_t lastEAccesCheck;
543     char thostName[200];
544     unsigned long code;
545     struct hostent *thp;
546     HMODULE hHookDll = NULL;
547     AfsdDaemonHook daemonHook = NULL;
548     char * name = "cm_Daemon_ShutdownEvent";
549     int configureFirewall = IsWindowsFirewallPresent();
550     int bAddrChangeCheck = 0;
551
552     cm_Daemon_ShutdownEvent = thrd_CreateEvent(NULL, FALSE, FALSE, name);
553     if ( GetLastError() == ERROR_ALREADY_EXISTS )
554         afsi_log("Event Object Already Exists: %s", name);
555
556     if (!configureFirewall) {
557         afsi_log("No Windows Firewall detected");
558     }
559
560     if (cm_freelanceEnabled && cm_freelanceImportCellServDB)
561         cm_FreelanceImportCellServDB();
562
563     /* ping all file servers, up or down, with unauthenticated connection,
564      * to find out whether we have all our callbacks from the server still.
565      * Also, ping down VLDBs.
566      */
567     /*
568      * Seed the random number generator with our own address, so that
569      * clients starting at the same time don't all do vol checks at the
570      * same time.
571      */
572     gethostname(thostName, sizeof(thostName));
573     thp = gethostbyname(thostName);
574     if (thp == NULL)    /* In djgpp, gethostname returns the netbios
575                            name of the machine.  gethostbyname will fail
576                            looking this up if it differs from DNS name. */
577         code = 0;
578     else
579         memcpy(&code, thp->h_addr_list[0], 4);
580
581     srand(ntohl(code));
582
583     cm_DaemonCheckInit();
584
585     now = osi_Time();
586     lastVolCheck = now - cm_daemonCheckVolInterval/2 + (rand() % cm_daemonCheckVolInterval);
587     lastCBExpirationCheck = now - cm_daemonCheckCBInterval/2 + (rand() % cm_daemonCheckCBInterval);
588     if (cm_daemonCheckVolCBInterval)
589         lastVolCBRenewalCheck = now - cm_daemonCheckVolCBInterval/2 + (rand() % cm_daemonCheckVolCBInterval);
590     lastDownServerCheck = now - cm_daemonCheckDownInterval/2 + (rand() % cm_daemonCheckDownInterval);
591     lastUpServerCheck = now - cm_daemonCheckUpInterval/2 + (rand() % cm_daemonCheckUpInterval);
592     lastTokenCacheCheck = now - cm_daemonTokenCheckInterval/2 + (rand() % cm_daemonTokenCheckInterval);
593     if (cm_daemonCheckOfflineVolInterval)
594         lastBusyVolCheck = now - cm_daemonCheckOfflineVolInterval/2 * (rand() % cm_daemonCheckOfflineVolInterval);
595     if (cm_daemonPerformanceTuningInterval)
596         lastPerformanceCheck = now - cm_daemonPerformanceTuningInterval/2 * (rand() % cm_daemonPerformanceTuningInterval);
597     lastServerRankCheck = now - cm_daemonRankServerInterval/2 * (rand() % cm_daemonRankServerInterval);
598     if (cm_daemonRDRShakeExtentsInterval)
599         lastRDRShakeExtents = now - cm_daemonRDRShakeExtentsInterval/2 * (rand() % cm_daemonRDRShakeExtentsInterval);
600     if (cm_daemonAfsdHookReloadInterval)
601         lastAfsdHookReload = now;
602     lastEAccesCheck = now;
603
604     hHookDll = cm_LoadAfsdHookLib();
605     if (hHookDll)
606         daemonHook = ( AfsdDaemonHook ) GetProcAddress(hHookDll, AFSD_DAEMON_HOOK);
607
608     while (daemon_ShutdownFlag == 0) {
609         if (powerStateSuspended) {
610             Sleep(1000);
611             continue;
612         }
613         /* check to see if the listener threads halted due to network
614          * disconnect or other issues.  If so, attempt to restart them.
615          */
616         smb_RestartListeners(0);
617
618         if (daemon_ShutdownFlag == 1)
619             break;
620
621         if (configureFirewall) {
622             /* Open Microsoft Firewall to allow in port 7001 */
623             switch (icf_CheckAndAddAFSPorts(AFS_PORTSET_CLIENT)) {
624             case 0:
625                 afsi_log("Windows Firewall Configuration succeeded");
626                 configureFirewall = 0;
627                 break;
628             case 1:
629                 afsi_log("Invalid Windows Firewall Port Set");
630                 break;
631             case 2:
632                 afsi_log("Unable to open Windows Firewall Profile");
633                 break;
634             case 3:
635                 afsi_log("Unable to create/modify Windows Firewall Port entries");
636                 break;
637             default:
638                 afsi_log("Unknown Windows Firewall Configuration error");
639             }
640         }
641
642         /* find out what time it is */
643         now = osi_Time();
644
645         /* Determine whether an address change took place that we need to respond to */
646         if (bAddrChangeCheck)
647             bAddrChangeCheck = 0;
648
649         if (lastIPAddrChange != 0 && lastIPAddrChange + 2500 < now) {
650             bAddrChangeCheck = 1;
651             lastIPAddrChange = 0;
652         }
653
654         /* check down servers */
655         if ((bAddrChangeCheck || now > lastDownServerCheck + cm_daemonCheckDownInterval) &&
656             daemon_ShutdownFlag == 0 &&
657             powerStateSuspended == 0) {
658             lastDownServerCheck = now;
659             osi_Log0(afsd_logp, "cm_Daemon CheckDownServers");
660             cm_CheckServers(CM_FLAG_CHECKDOWNSERVERS, NULL);
661             if (daemon_ShutdownFlag == 1)
662                 break;
663             now = osi_Time();
664         }
665
666         if (bAddrChangeCheck &&
667             daemon_ShutdownFlag == 0 &&
668             powerStateSuspended == 0) {
669             cm_ForceNewConnectionsAllServers();
670         }
671
672         /* check up servers */
673         if ((bAddrChangeCheck || now > lastUpServerCheck + cm_daemonCheckUpInterval) &&
674             daemon_ShutdownFlag == 0 &&
675             powerStateSuspended == 0) {
676             lastUpServerCheck = now;
677             osi_Log0(afsd_logp, "cm_Daemon CheckUpServers");
678             cm_CheckServers(CM_FLAG_CHECKUPSERVERS, NULL);
679             if (daemon_ShutdownFlag == 1)
680                 break;
681             now = osi_Time();
682         }
683
684         if (bAddrChangeCheck &&
685             daemon_ShutdownFlag == 0 &&
686             powerStateSuspended == 0) {
687             smb_CheckVCs();
688             cm_VolStatus_Network_Addr_Change();
689         }
690
691         /*
692          * Once every five minutes inspect the volume list and enforce
693          * the volume location expiration time.
694          */
695         if (now > lastVolCheck + 300 &&
696             daemon_ShutdownFlag == 0 &&
697             powerStateSuspended == 0) {
698             lastVolCheck = now;
699             cm_RefreshVolumes(cm_daemonCheckVolInterval);
700             if (daemon_ShutdownFlag == 1)
701                 break;
702             now = osi_Time();
703         }
704
705         /* Rank all up servers */
706         if ((now > lastServerRankCheck + cm_daemonRankServerInterval) &&
707             daemon_ShutdownFlag == 0 &&
708             powerStateSuspended == 0) {
709             lastServerRankCheck = now;
710             osi_Log0(afsd_logp, "cm_Daemon RankServer");
711             cm_RankUpServers();
712             if(daemon_ShutdownFlag == 1)
713                 break;
714             now = osi_Time();
715         }
716
717         if (cm_daemonCheckVolCBInterval &&
718             now > lastVolCBRenewalCheck + cm_daemonCheckVolCBInterval &&
719             daemon_ShutdownFlag == 0 &&
720             powerStateSuspended == 0) {
721             lastVolCBRenewalCheck = now;
722             cm_VolumeRenewROCallbacks();
723             if (daemon_ShutdownFlag == 1)
724                 break;
725             now = osi_Time();
726         }
727
728         if ((bAddrChangeCheck || (cm_daemonCheckOfflineVolInterval &&
729                                   now > lastBusyVolCheck + cm_daemonCheckOfflineVolInterval)) &&
730             daemon_ShutdownFlag == 0 &&
731             powerStateSuspended == 0) {
732             lastBusyVolCheck = now;
733             cm_CheckOfflineVolumes();
734             if (daemon_ShutdownFlag == 1)
735                 break;
736             now = osi_Time();
737         }
738
739         if (now > lastCBExpirationCheck + cm_daemonCheckCBInterval &&
740             daemon_ShutdownFlag == 0 &&
741             powerStateSuspended == 0) {
742             lastCBExpirationCheck = now;
743             cm_CheckCBExpiration();
744             if (daemon_ShutdownFlag == 1)
745                 break;
746             now = osi_Time();
747         }
748
749         if (now > lastTokenCacheCheck + cm_daemonTokenCheckInterval &&
750             daemon_ShutdownFlag == 0 &&
751             powerStateSuspended == 0) {
752             lastTokenCacheCheck = now;
753             cm_CheckTokenCache(now);
754             if (daemon_ShutdownFlag == 1)
755                 break;
756             now = osi_Time();
757         }
758
759         if (now > lastEAccesCheck + cm_daemonEAccesCheckInterval &&
760              daemon_ShutdownFlag == 0 &&
761              powerStateSuspended == 0) {
762             lastEAccesCheck = now;
763             cm_EAccesClearOutdatedEntries();
764             if (daemon_ShutdownFlag == 1)
765                 break;
766             now = osi_Time();
767         }
768
769         if (cm_daemonRDRShakeExtentsInterval &&
770             now > lastRDRShakeExtents + cm_daemonRDRShakeExtentsInterval &&
771             daemon_ShutdownFlag == 0 &&
772             powerStateSuspended == 0) {
773             cm_req_t req;
774             cm_InitReq(&req);
775             lastRDRShakeExtents = now;
776             if (cm_data.buf_redirCount > cm_data.buf_freeCount)
777                 buf_RDRShakeSomeExtentsFree(&req, FALSE, 10 /* seconds */);
778             if (daemon_ShutdownFlag == 1)
779                 break;
780             now = osi_Time();
781         }
782
783         /* allow an exit to be called prior to stopping the service */
784         if (cm_daemonAfsdHookReloadInterval &&
785             lastAfsdHookReload != 0 && lastAfsdHookReload < now) {
786             if (hHookDll) {
787                 FreeLibrary(hHookDll);
788                 hHookDll = NULL;
789                 daemonHook = NULL;
790             }
791
792             hHookDll = cm_LoadAfsdHookLib();
793             if (hHookDll)
794                 daemonHook = ( AfsdDaemonHook ) GetProcAddress(hHookDll, AFSD_DAEMON_HOOK);
795         }
796
797         if (daemonHook)
798         {
799             BOOL hookRc = daemonHook();
800
801             if (hookRc == FALSE)
802             {
803                 SetEvent(WaitToTerminate);
804             }
805
806             if (daemon_ShutdownFlag == 1) {
807                 break;
808             }
809             now = osi_Time();
810         }
811
812         if (cm_daemonPerformanceTuningInterval &&
813             now > lastPerformanceCheck + cm_daemonPerformanceTuningInterval &&
814             daemon_ShutdownFlag == 0 &&
815             powerStateSuspended == 0) {
816             lastPerformanceCheck = now;
817             cm_PerformanceTuningCheck();
818             if (daemon_ShutdownFlag == 1)
819                 break;
820             now = osi_Time();
821         }
822
823         /*
824          * sleep .5 seconds.  if the thread blocks for a long time
825          * we risk not being able to close the cache before Windows
826          * kills our process during system shutdown.
827          */
828         thrd_Sleep(500);
829     }
830
831     if (hHookDll) {
832         FreeLibrary(hHookDll);
833     }
834
835     thrd_SetEvent(cm_Daemon_ShutdownEvent);
836     pthread_exit(NULL);
837     return NULL;
838 }
839
840 void cm_DaemonShutdown(void)
841 {
842     int i;
843     DWORD code;
844
845     daemon_ShutdownFlag = 1;
846
847     /* wait for shutdown */
848     for ( i=0; i<cm_nDaemons; i++) {
849         osi_Wakeup((LONG_PTR) &cm_bkgListpp[i]);
850         if (cm_BkgDaemon_ShutdownEvent[i])
851             code = thrd_WaitForSingleObject_Event(cm_BkgDaemon_ShutdownEvent[i], INFINITE);
852     }
853
854     if (cm_Daemon_ShutdownEvent)
855         code = thrd_WaitForSingleObject_Event(cm_Daemon_ShutdownEvent, INFINITE);
856
857     if (cm_LockDaemon_ShutdownEvent)
858         code = thrd_WaitForSingleObject_Event(cm_LockDaemon_ShutdownEvent, INFINITE);
859
860 #if 0
861     /*
862      * Do not waste precious time waiting for the ipaddr daemon to shutdown.
863      * When it does it means we have lost our network connection and we need
864      * it during cache shutdown in order to notify the file servers that this
865      * client is giving up all callbacks.
866      */
867     if (cm_IPAddrDaemon_ShutdownEvent)
868         code = thrd_WaitForSingleObject_Event(cm_IPAddrDaemon_ShutdownEvent, INFINITE);
869 #endif
870 }
871
872 void cm_InitDaemon(int nDaemons)
873 {
874     static osi_once_t once;
875     pthread_t phandle;
876     pthread_attr_t tattr;
877     int pstatus;
878     int i;
879
880     pthread_attr_init(&tattr);
881     pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED);
882
883     cm_nDaemons = (nDaemons > CM_MAX_DAEMONS) ? CM_MAX_DAEMONS : nDaemons;
884
885     if (osi_Once(&once)) {
886         /* creating IP Address Change monitor daemon */
887         pstatus = pthread_create(&phandle, &tattr, cm_IpAddrDaemon, 0);
888         osi_assertx(pstatus == 0, "cm_IpAddrDaemon thread creation failure");
889
890         /* creating pinging daemon */
891         pstatus = pthread_create(&phandle, &tattr, cm_Daemon, 0);
892         osi_assertx(pstatus == 0, "cm_Daemon thread creation failure");
893
894         pstatus = pthread_create(&phandle, &tattr, cm_LockDaemon, 0);
895         osi_assertx(pstatus == 0, "cm_LockDaemon thread creation failure");
896
897         cm_bkgListpp = malloc(nDaemons * sizeof(void *));
898         cm_bkgListEndpp = malloc(nDaemons * sizeof(void *));
899         cm_bkgQueueCountp = malloc(nDaemons * sizeof(afs_uint64));
900         cm_daemonLockp = malloc(nDaemons * sizeof(osi_rwlock_t));
901
902         for(i=0; i < cm_nDaemons; i++) {
903             lock_InitializeRWLock(&cm_daemonLockp[i], "cm_daemonLock",
904                                   LOCK_HIERARCHY_DAEMON_GLOBAL);
905             cm_bkgListpp[i] = cm_bkgListEndpp[i] = NULL;
906             cm_bkgQueueCountp[i]=0;
907             pstatus = pthread_create(&phandle, &tattr, cm_BkgDaemon, (LPVOID)(LONG_PTR)i);
908             osi_assertx(pstatus == 0, "cm_BkgDaemon thread creation failure");
909         }
910         osi_EndOnce(&once);
911     }
912
913     pthread_attr_destroy(&tattr);
914 }