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 <afsconfig.h>
11 #include <afs/param.h>
27 #include <rx/rx_prototypes.h>
28 #include <WINNT/afsreg.h>
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;
45 osi_rwlock_t cm_daemonLock;
47 long cm_bkgQueueCount; /* # of queued requests */
49 int cm_bkgWaitingForCount; /* true if someone's waiting for cm_bkgQueueCount to drop */
51 cm_bkgRequest_t *cm_bkgListp; /* first elt in the list of requests */
52 cm_bkgRequest_t *cm_bkgListEndp; /* last elt in the list of requests */
54 extern int powerStateSuspended;
55 int daemon_ShutdownFlag = 0;
56 static int cm_nDaemons = 0;
57 static time_t lastIPAddrChange = 0;
59 static EVENT_HANDLE cm_Daemon_ShutdownEvent = NULL;
60 static EVENT_HANDLE cm_LockDaemon_ShutdownEvent = NULL;
61 static EVENT_HANDLE cm_IPAddrDaemon_ShutdownEvent = NULL;
62 static EVENT_HANDLE cm_BkgDaemon_ShutdownEvent[CM_MAX_DAEMONS] =
63 {NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL};
65 void cm_IpAddrDaemon(long parm)
67 extern void smb_CheckVCs(void);
68 char * name = "cm_IPAddrDaemon_ShutdownEvent";
70 cm_IPAddrDaemon_ShutdownEvent = thrd_CreateEvent(NULL, FALSE, FALSE, name);
71 if ( GetLastError() == ERROR_ALREADY_EXISTS )
72 afsi_log("Event Object Already Exists: %s", name);
74 rx_StartClientThread();
76 while (daemon_ShutdownFlag == 0) {
79 thrd_SetEvent(cm_IPAddrDaemon_ShutdownEvent);
80 Result = NotifyAddrChange(NULL,NULL);
81 if (Result == NO_ERROR && daemon_ShutdownFlag == 0) {
82 lastIPAddrChange = osi_Time();
84 smb_SetLanAdapterChangeDetected();
85 cm_SetLanAdapterChangeDetected();
86 thrd_ResetEvent(cm_IPAddrDaemon_ShutdownEvent);
90 thrd_SetEvent(cm_IPAddrDaemon_ShutdownEvent);
93 afs_int32 cm_RequestWillBlock(cm_bkgRequest_t *rp)
95 afs_int32 willBlock = 0;
97 if (rp->procp == cm_BkgStore) {
99 * If the datastoring flag is set, it means that another
100 * thread is already performing an exclusive store operation
101 * on this file. The exclusive state will be cleared once
102 * the file server locks the vnode. Therefore, at most two
103 * threads can be actively involved in storing data at a time
106 lock_ObtainRead(&rp->scp);
107 willBlock = (rp->scp->flags & CM_SCACHEFLAG_DATASTORING);
108 lock_ReleaseRead(&rp->scp);
110 else if (rp->procp == RDR_BkgFetch || rp->procp == cm_BkgPrefetch) {
112 * Attempt to determine if there is a conflict on the requested
113 * range of the file. If the first in the range does not exist
114 * in the cache assume there is no conflict. If the buffer does
115 * exist, check to see if an I/O operation is in progress
116 * by using the writing and reading flags as an indicator.
119 cm_buf_t *bufp = NULL;
121 base.LowPart = rp->p1;
122 base.HighPart = rp->p2;
124 bufp = buf_Find(&rp->scp->fid, &base);
126 willBlock = (bufp->flags & (CM_BUF_WRITING|CM_BUF_READING));
134 void cm_BkgDaemon(void * parm)
139 long daemonID = (long)(LONG_PTR)parm;
141 snprintf(name, sizeof(name), "cm_BkgDaemon_ShutdownEvent%u", daemonID);
143 cm_BkgDaemon_ShutdownEvent[daemonID] = thrd_CreateEvent(NULL, FALSE, FALSE, name);
144 if ( GetLastError() == ERROR_ALREADY_EXISTS )
145 afsi_log("Event Object Already Exists: %s", name);
147 rx_StartClientThread();
149 lock_ObtainWrite(&cm_daemonLock);
150 while (daemon_ShutdownFlag == 0) {
153 if (powerStateSuspended) {
157 if (!cm_bkgListEndp) {
158 osi_SleepW((LONG_PTR)&cm_bkgListp, &cm_daemonLock);
159 lock_ObtainWrite(&cm_daemonLock);
163 /* we found a request */
164 for (rp = cm_bkgListEndp; rp; rp = (cm_bkgRequest_t *) osi_QPrev(&rp->q))
166 if (rp->scp->flags & CM_SCACHEFLAG_DELETED)
170 * If the request has active I/O such that this worker would
171 * be forced to block, leave the request in the queue and move
172 * on to one that might be available for servicing.
174 if (cm_RequestWillBlock(rp)) {
179 if (cm_ServerAvailable(&rp->scp->fid, rp->userp))
185 * Couldn't find a request that we could process at the
186 * current time. If there were requests that would cause
187 * the worker to block, sleep for 25ms so it can promptly
188 * respond when it is available. Otherwise, sleep for 1s.
190 * This polling cycle needs to be replaced with a proper
191 * producer/consumer dynamic worker pool.
193 lock_ReleaseWrite(&cm_daemonLock);
194 Sleep(willBlock ? 25 : 1000);
195 lock_ObtainWrite(&cm_daemonLock);
199 osi_QRemoveHT((osi_queue_t **) &cm_bkgListp, (osi_queue_t **) &cm_bkgListEndp, &rp->q);
200 osi_assertx(cm_bkgQueueCount-- > 0, "cm_bkgQueueCount 0");
201 lock_ReleaseWrite(&cm_daemonLock);
203 osi_Log1(afsd_logp,"cm_BkgDaemon processing request 0x%p", rp);
205 if (rp->scp->flags & CM_SCACHEFLAG_DELETED) {
206 osi_Log1(afsd_logp,"cm_BkgDaemon DELETED scp 0x%x",rp->scp);
207 code = CM_ERROR_BADFD;
209 #ifdef DEBUG_REFCOUNT
210 osi_Log2(afsd_logp,"cm_BkgDaemon (before) scp 0x%x ref %d",rp->scp, rp->scp->refCount);
212 code = (*rp->procp)(rp->scp, rp->p1, rp->p2, rp->p3, rp->p4, rp->userp, &rp->req);
213 #ifdef DEBUG_REFCOUNT
214 osi_Log2(afsd_logp,"cm_BkgDaemon (after) scp 0x%x ref %d",rp->scp, rp->scp->refCount);
219 * Keep the following list synchronized with the
220 * error code list in cm_BkgStore.
221 * cm_SyncOpDone(CM_SCACHESYNC_ASYNCSTORE) will be called there unless
222 * one of these errors has occurred.
225 case CM_ERROR_TIMEDOUT: /* or server restarting */
227 case CM_ERROR_WOULDBLOCK:
228 case CM_ERROR_ALLBUSY:
229 case CM_ERROR_ALLDOWN:
230 case CM_ERROR_ALLOFFLINE:
231 case CM_ERROR_PARTIALWRITE:
232 if (rp->procp == cm_BkgStore ||
233 rp->procp == RDR_BkgFetch) {
235 "cm_BkgDaemon re-queueing failed request 0x%p code 0x%x",
237 lock_ObtainWrite(&cm_daemonLock);
239 osi_QAddT((osi_queue_t **) &cm_bkgListp, (osi_queue_t **)&cm_bkgListEndp, &rp->q);
241 } /* otherwise fall through */
242 case 0: /* success */
243 default: /* other error */
245 osi_Log1(afsd_logp,"cm_BkgDaemon SUCCESS: request 0x%p", rp);
247 osi_Log2(afsd_logp,"cm_BkgDaemon FAILED: request dropped 0x%p code 0x%x",
250 cm_ReleaseUser(rp->userp);
251 cm_ReleaseSCache(rp->scp);
253 lock_ObtainWrite(&cm_daemonLock);
256 lock_ReleaseWrite(&cm_daemonLock);
257 thrd_SetEvent(cm_BkgDaemon_ShutdownEvent[daemonID]);
260 void cm_QueueBKGRequest(cm_scache_t *scp, cm_bkgProc_t *procp, afs_uint32 p1, afs_uint32 p2, afs_uint32 p3, afs_uint32 p4,
261 cm_user_t *userp, cm_req_t *reqp)
265 rp = malloc(sizeof(*rp));
266 memset(rp, 0, sizeof(*rp));
279 lock_ObtainWrite(&cm_daemonLock);
281 osi_QAdd((osi_queue_t **) &cm_bkgListp, &rp->q);
284 lock_ReleaseWrite(&cm_daemonLock);
286 osi_Wakeup((LONG_PTR) &cm_bkgListp);
290 IsWindowsFirewallPresent(void)
295 BOOLEAN result = FALSE;
296 LPQUERY_SERVICE_CONFIG pConfig = NULL;
300 /* Open services manager */
301 scm = OpenSCManager(NULL, NULL, GENERIC_READ);
302 if (!scm) return FALSE;
304 /* Open Windows Firewall service */
305 svc = OpenService(scm, "MpsSvc", SERVICE_QUERY_CONFIG);
307 afsi_log("MpsSvc Service could not be opened for query: 0x%x", GetLastError());
308 svc = OpenService(scm, "SharedAccess", SERVICE_QUERY_CONFIG);
310 afsi_log("SharedAccess Service could not be opened for query: 0x%x", GetLastError());
315 /* Query Windows Firewall service config, first just to get buffer size */
316 /* Expected to fail, so don't test return value */
317 (void) QueryServiceConfig(svc, NULL, 0, &BufSize);
318 status = GetLastError();
319 if (status != ERROR_INSUFFICIENT_BUFFER)
322 /* Allocate buffer */
323 pConfig = (LPQUERY_SERVICE_CONFIG)GlobalAlloc(GMEM_FIXED,BufSize);
327 /* Query Windows Firewall service config, this time for real */
328 flag = QueryServiceConfig(svc, pConfig, BufSize, &BufSize);
330 afsi_log("QueryServiceConfig failed: 0x%x", GetLastError());
334 /* Is it autostart? */
335 afsi_log("AutoStart 0x%x", pConfig->dwStartType);
336 if (pConfig->dwStartType < SERVICE_DEMAND_START)
342 CloseServiceHandle(svc);
344 CloseServiceHandle(scm);
350 cm_DaemonCheckInit(void)
357 code = RegOpenKeyEx(HKEY_LOCAL_MACHINE, AFSREG_CLT_SVC_PARAM_SUBKEY,
358 0, KEY_QUERY_VALUE, &parmKey);
362 dummyLen = sizeof(DWORD);
363 code = RegQueryValueEx(parmKey, "daemonCheckDownInterval", NULL, NULL,
364 (BYTE *) &dummy, &dummyLen);
365 if (code == ERROR_SUCCESS && dummy)
366 cm_daemonCheckDownInterval = dummy;
367 afsi_log("daemonCheckDownInterval is %d", cm_daemonCheckDownInterval);
369 dummyLen = sizeof(DWORD);
370 code = RegQueryValueEx(parmKey, "daemonCheckUpInterval", NULL, NULL,
371 (BYTE *) &dummy, &dummyLen);
372 if (code == ERROR_SUCCESS && dummy)
373 cm_daemonCheckUpInterval = dummy;
374 afsi_log("daemonCheckUpInterval is %d", cm_daemonCheckUpInterval);
376 dummyLen = sizeof(DWORD);
377 code = RegQueryValueEx(parmKey, "daemonCheckVolInterval", NULL, NULL,
378 (BYTE *) &dummy, &dummyLen);
379 if (code == ERROR_SUCCESS && dummy)
380 cm_daemonCheckVolInterval = dummy;
381 afsi_log("daemonCheckVolInterval is %d", cm_daemonCheckVolInterval);
383 dummyLen = sizeof(DWORD);
384 code = RegQueryValueEx(parmKey, "daemonCheckCBInterval", NULL, NULL,
385 (BYTE *) &dummy, &dummyLen);
386 if (code == ERROR_SUCCESS && dummy)
387 cm_daemonCheckCBInterval = dummy;
388 afsi_log("daemonCheckCBInterval is %d", cm_daemonCheckCBInterval);
390 dummyLen = sizeof(DWORD);
391 code = RegQueryValueEx(parmKey, "daemonCheckVolCBInterval", NULL, NULL,
392 (BYTE *) &dummy, &dummyLen);
393 if (code == ERROR_SUCCESS && dummy)
394 cm_daemonCheckVolCBInterval = dummy;
395 afsi_log("daemonCheckVolCBInterval is %d", cm_daemonCheckVolCBInterval);
397 dummyLen = sizeof(DWORD);
398 code = RegQueryValueEx(parmKey, "daemonCheckLockInterval", NULL, NULL,
399 (BYTE *) &dummy, &dummyLen);
400 if (code == ERROR_SUCCESS && dummy)
401 cm_daemonCheckLockInterval = dummy;
402 afsi_log("daemonCheckLockInterval is %d", cm_daemonCheckLockInterval);
404 dummyLen = sizeof(DWORD);
405 code = RegQueryValueEx(parmKey, "daemonCheckTokenInterval", NULL, NULL,
406 (BYTE *) &dummy, &dummyLen);
407 if (code == ERROR_SUCCESS && dummy)
408 cm_daemonTokenCheckInterval = dummy;
409 afsi_log("daemonCheckTokenInterval is %d", cm_daemonTokenCheckInterval);
411 dummyLen = sizeof(DWORD);
412 code = RegQueryValueEx(parmKey, "daemonCheckOfflineVolInterval", NULL, NULL,
413 (BYTE *) &dummy, &dummyLen);
414 if (code == ERROR_SUCCESS && dummy)
415 cm_daemonCheckOfflineVolInterval = dummy;
416 afsi_log("daemonCheckOfflineVolInterval is %d", cm_daemonCheckOfflineVolInterval);
418 dummyLen = sizeof(DWORD);
419 code = RegQueryValueEx(parmKey, "daemonRDRShakeExtentsInterval", NULL, NULL,
420 (BYTE *) &dummy, &dummyLen);
421 if (code == ERROR_SUCCESS && dummy)
422 cm_daemonRDRShakeExtentsInterval = dummy;
423 afsi_log("daemonRDRShakeExtentsInterval is %d", cm_daemonRDRShakeExtentsInterval);
425 dummyLen = sizeof(DWORD);
426 code = RegQueryValueEx(parmKey, "daemonPerformanceTuningInterval", NULL, NULL,
427 (BYTE *) &dummy, &dummyLen);
428 if (code == ERROR_SUCCESS)
429 cm_daemonPerformanceTuningInterval = dummy;
430 afsi_log("daemonPerformanceTuningInterval is %d", cm_daemonPerformanceTuningInterval);
432 dummyLen = sizeof(DWORD);
433 code = RegQueryValueEx(parmKey, "daemonRankServerInterval", NULL, NULL,
434 (BYTE *) &dummy, &dummyLen);
435 if (code == ERROR_SUCCESS && dummy)
436 cm_daemonRankServerInterval = dummy;
437 afsi_log("daemonRankServerInterval is %d", cm_daemonRankServerInterval);
439 RegCloseKey(parmKey);
441 if (cm_daemonPerformanceTuningInterval)
442 cm_PerformanceTuningInit();
445 /* periodic lock check daemon */
446 void cm_LockDaemon(long parm)
449 time_t lastLockCheck;
450 char * name = "cm_LockDaemon_ShutdownEvent";
452 cm_LockDaemon_ShutdownEvent = thrd_CreateEvent(NULL, FALSE, FALSE, name);
453 if ( GetLastError() == ERROR_ALREADY_EXISTS )
454 afsi_log("Event Object Already Exists: %s", name);
457 lastLockCheck = now - cm_daemonCheckLockInterval/2 + (rand() % cm_daemonCheckLockInterval);
459 while (daemon_ShutdownFlag == 0) {
460 if (powerStateSuspended) {
467 if (now > lastLockCheck + cm_daemonCheckLockInterval &&
468 daemon_ShutdownFlag == 0 &&
469 powerStateSuspended == 0) {
472 if (daemon_ShutdownFlag == 1)
476 thrd_Sleep(1000); /* sleep 1 second */
478 thrd_SetEvent(cm_LockDaemon_ShutdownEvent);
481 /* periodic check daemon */
482 void cm_Daemon(long parm)
486 time_t lastCBExpirationCheck;
487 time_t lastVolCBRenewalCheck;
488 time_t lastDownServerCheck;
489 time_t lastUpServerCheck;
490 time_t lastTokenCacheCheck;
491 time_t lastBusyVolCheck;
492 time_t lastPerformanceCheck;
493 time_t lastServerRankCheck;
494 time_t lastRDRShakeExtents;
499 char * name = "cm_Daemon_ShutdownEvent";
500 int configureFirewall = IsWindowsFirewallPresent();
501 int bAddrChangeCheck = 0;
503 cm_Daemon_ShutdownEvent = thrd_CreateEvent(NULL, FALSE, FALSE, name);
504 if ( GetLastError() == ERROR_ALREADY_EXISTS )
505 afsi_log("Event Object Already Exists: %s", name);
507 if (!configureFirewall) {
508 afsi_log("No Windows Firewall detected");
511 if (cm_freelanceEnabled && cm_freelanceImportCellServDB)
512 cm_FreelanceImportCellServDB();
514 /* ping all file servers, up or down, with unauthenticated connection,
515 * to find out whether we have all our callbacks from the server still.
516 * Also, ping down VLDBs.
519 * Seed the random number generator with our own address, so that
520 * clients starting at the same time don't all do vol checks at the
523 gethostname(thostName, sizeof(thostName));
524 thp = gethostbyname(thostName);
525 if (thp == NULL) /* In djgpp, gethostname returns the netbios
526 name of the machine. gethostbyname will fail
527 looking this up if it differs from DNS name. */
530 memcpy(&code, thp->h_addr_list[0], 4);
534 cm_DaemonCheckInit();
537 lastVolCheck = now - cm_daemonCheckVolInterval/2 + (rand() % cm_daemonCheckVolInterval);
538 lastCBExpirationCheck = now - cm_daemonCheckCBInterval/2 + (rand() % cm_daemonCheckCBInterval);
539 if (cm_daemonCheckVolCBInterval)
540 lastVolCBRenewalCheck = now - cm_daemonCheckVolCBInterval/2 + (rand() % cm_daemonCheckVolCBInterval);
541 lastDownServerCheck = now - cm_daemonCheckDownInterval/2 + (rand() % cm_daemonCheckDownInterval);
542 lastUpServerCheck = now - cm_daemonCheckUpInterval/2 + (rand() % cm_daemonCheckUpInterval);
543 lastTokenCacheCheck = now - cm_daemonTokenCheckInterval/2 + (rand() % cm_daemonTokenCheckInterval);
544 if (cm_daemonCheckOfflineVolInterval)
545 lastBusyVolCheck = now - cm_daemonCheckOfflineVolInterval/2 * (rand() % cm_daemonCheckOfflineVolInterval);
546 if (cm_daemonPerformanceTuningInterval)
547 lastPerformanceCheck = now - cm_daemonPerformanceTuningInterval/2 * (rand() % cm_daemonPerformanceTuningInterval);
548 lastServerRankCheck = now - cm_daemonRankServerInterval/2 * (rand() % cm_daemonRankServerInterval);
549 if (cm_daemonRDRShakeExtentsInterval)
550 lastRDRShakeExtents = now - cm_daemonRDRShakeExtentsInterval/2 * (rand() % cm_daemonRDRShakeExtentsInterval);
552 while (daemon_ShutdownFlag == 0) {
553 if (powerStateSuspended) {
557 /* check to see if the listener threads halted due to network
558 * disconnect or other issues. If so, attempt to restart them.
560 smb_RestartListeners(0);
562 if (daemon_ShutdownFlag == 1)
565 if (configureFirewall) {
566 /* Open Microsoft Firewall to allow in port 7001 */
567 switch (icf_CheckAndAddAFSPorts(AFS_PORTSET_CLIENT)) {
569 afsi_log("Windows Firewall Configuration succeeded");
570 configureFirewall = 0;
573 afsi_log("Invalid Windows Firewall Port Set");
576 afsi_log("Unable to open Windows Firewall Profile");
579 afsi_log("Unable to create/modify Windows Firewall Port entries");
582 afsi_log("Unknown Windows Firewall Configuration error");
586 /* find out what time it is */
589 /* Determine whether an address change took place that we need to respond to */
590 if (bAddrChangeCheck)
591 bAddrChangeCheck = 0;
593 if (lastIPAddrChange != 0 && lastIPAddrChange + 2500 < now) {
594 bAddrChangeCheck = 1;
595 lastIPAddrChange = 0;
598 /* check down servers */
599 if ((bAddrChangeCheck || now > lastDownServerCheck + cm_daemonCheckDownInterval) &&
600 daemon_ShutdownFlag == 0 &&
601 powerStateSuspended == 0) {
602 lastDownServerCheck = now;
603 osi_Log0(afsd_logp, "cm_Daemon CheckDownServers");
604 cm_CheckServers(CM_FLAG_CHECKDOWNSERVERS, NULL);
605 if (daemon_ShutdownFlag == 1)
610 if (bAddrChangeCheck &&
611 daemon_ShutdownFlag == 0 &&
612 powerStateSuspended == 0) {
613 cm_ForceNewConnectionsAllServers();
616 /* check up servers */
617 if ((bAddrChangeCheck || now > lastUpServerCheck + cm_daemonCheckUpInterval) &&
618 daemon_ShutdownFlag == 0 &&
619 powerStateSuspended == 0) {
620 lastUpServerCheck = now;
621 osi_Log0(afsd_logp, "cm_Daemon CheckUpServers");
622 cm_CheckServers(CM_FLAG_CHECKUPSERVERS, NULL);
623 if (daemon_ShutdownFlag == 1)
628 if (bAddrChangeCheck &&
629 daemon_ShutdownFlag == 0 &&
630 powerStateSuspended == 0) {
632 cm_VolStatus_Network_Addr_Change();
636 * Once every five minutes inspect the volume list and enforce
637 * the volume location expiration time.
639 if (now > lastVolCheck + 300 &&
640 daemon_ShutdownFlag == 0 &&
641 powerStateSuspended == 0) {
643 cm_RefreshVolumes(cm_daemonCheckVolInterval);
644 if (daemon_ShutdownFlag == 1)
649 /* Rank all up servers */
650 if ((now > lastServerRankCheck + cm_daemonRankServerInterval) &&
651 daemon_ShutdownFlag == 0 &&
652 powerStateSuspended == 0) {
653 lastServerRankCheck = now;
654 osi_Log0(afsd_logp, "cm_Daemon RankServer");
656 if(daemon_ShutdownFlag == 1)
661 if (cm_daemonCheckVolCBInterval &&
662 now > lastVolCBRenewalCheck + cm_daemonCheckVolCBInterval &&
663 daemon_ShutdownFlag == 0 &&
664 powerStateSuspended == 0) {
665 lastVolCBRenewalCheck = now;
666 cm_VolumeRenewROCallbacks();
667 if (daemon_ShutdownFlag == 1)
672 if ((bAddrChangeCheck || (cm_daemonCheckOfflineVolInterval &&
673 now > lastBusyVolCheck + cm_daemonCheckOfflineVolInterval)) &&
674 daemon_ShutdownFlag == 0 &&
675 powerStateSuspended == 0) {
676 lastBusyVolCheck = now;
677 cm_CheckOfflineVolumes();
678 if (daemon_ShutdownFlag == 1)
683 if (now > lastCBExpirationCheck + cm_daemonCheckCBInterval &&
684 daemon_ShutdownFlag == 0 &&
685 powerStateSuspended == 0) {
686 lastCBExpirationCheck = now;
687 cm_CheckCBExpiration();
688 if (daemon_ShutdownFlag == 1)
693 if (now > lastTokenCacheCheck + cm_daemonTokenCheckInterval &&
694 daemon_ShutdownFlag == 0 &&
695 powerStateSuspended == 0) {
696 lastTokenCacheCheck = now;
697 cm_CheckTokenCache(now);
698 if (daemon_ShutdownFlag == 1)
703 if (cm_daemonRDRShakeExtentsInterval &&
704 now > lastRDRShakeExtents + cm_daemonRDRShakeExtentsInterval &&
705 daemon_ShutdownFlag == 0 &&
706 powerStateSuspended == 0) {
709 lastRDRShakeExtents = now;
710 if (cm_data.buf_redirCount > cm_data.buf_freeCount)
711 buf_RDRShakeSomeExtentsFree(&req, FALSE, 10 /* seconds */);
712 if (daemon_ShutdownFlag == 1)
717 /* allow an exit to be called prior to stopping the service */
718 hHookDll = cm_LoadAfsdHookLib();
722 AfsdDaemonHook daemonHook = ( AfsdDaemonHook ) GetProcAddress(hHookDll, AFSD_DAEMON_HOOK);
725 hookRc = daemonHook();
727 FreeLibrary(hHookDll);
732 SetEvent(WaitToTerminate);
735 if (daemon_ShutdownFlag == 1) {
741 if (cm_daemonPerformanceTuningInterval &&
742 now > lastPerformanceCheck + cm_daemonPerformanceTuningInterval &&
743 daemon_ShutdownFlag == 0 &&
744 powerStateSuspended == 0) {
745 lastPerformanceCheck = now;
746 cm_PerformanceTuningCheck();
747 if (daemon_ShutdownFlag == 1)
753 * sleep .5 seconds. if the thread blocks for a long time
754 * we risk not being able to close the cache before Windows
755 * kills our process during system shutdown.
760 thrd_SetEvent(cm_Daemon_ShutdownEvent);
763 void cm_DaemonShutdown(void)
768 daemon_ShutdownFlag = 1;
769 osi_Wakeup((LONG_PTR) &cm_bkgListp);
771 /* wait for shutdown */
772 for ( i=0; i<cm_nDaemons; i++) {
773 if (cm_BkgDaemon_ShutdownEvent[i])
774 code = thrd_WaitForSingleObject_Event(cm_BkgDaemon_ShutdownEvent[i], INFINITE);
777 if (cm_Daemon_ShutdownEvent)
778 code = thrd_WaitForSingleObject_Event(cm_Daemon_ShutdownEvent, INFINITE);
780 if (cm_LockDaemon_ShutdownEvent)
781 code = thrd_WaitForSingleObject_Event(cm_LockDaemon_ShutdownEvent, INFINITE);
785 * Do not waste precious time waiting for the ipaddr daemon to shutdown.
786 * When it does it means we have lost our network connection and we need
787 * it during cache shutdown in order to notify the file servers that this
788 * client is giving up all callbacks.
790 if (cm_IPAddrDaemon_ShutdownEvent)
791 code = thrd_WaitForSingleObject_Event(cm_IPAddrDaemon_ShutdownEvent, INFINITE);
795 void cm_InitDaemon(int nDaemons)
797 static osi_once_t once;
802 cm_nDaemons = (nDaemons > CM_MAX_DAEMONS) ? CM_MAX_DAEMONS : nDaemons;
804 if (osi_Once(&once)) {
805 lock_InitializeRWLock(&cm_daemonLock, "cm_daemonLock",
806 LOCK_HIERARCHY_DAEMON_GLOBAL);
809 /* creating IP Address Change monitor daemon */
810 phandle = thrd_Create((SecurityAttrib) 0, 0,
811 (ThreadFunc) cm_IpAddrDaemon, 0, 0, &pid, "cm_IpAddrDaemon");
812 osi_assertx(phandle != NULL, "cm_IpAddrDaemon thread creation failure");
813 thrd_CloseHandle(phandle);
815 /* creating pinging daemon */
816 phandle = thrd_Create((SecurityAttrib) 0, 0,
817 (ThreadFunc) cm_Daemon, 0, 0, &pid, "cm_Daemon");
818 osi_assertx(phandle != NULL, "cm_Daemon thread creation failure");
819 thrd_CloseHandle(phandle);
821 phandle = thrd_Create((SecurityAttrib) 0, 0,
822 (ThreadFunc) cm_LockDaemon, 0, 0, &pid, "cm_LockDaemon");
823 osi_assertx(phandle != NULL, "cm_LockDaemon thread creation failure");
824 thrd_CloseHandle(phandle);
826 for(i=0; i < cm_nDaemons; i++) {
827 phandle = thrd_Create((SecurityAttrib) 0, 0,
828 (ThreadFunc) cm_BkgDaemon, (LPVOID)(LONG_PTR)i, 0, &pid,
830 osi_assertx(phandle != NULL, "cm_BkgDaemon thread creation failure");
831 thrd_CloseHandle(phandle);