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>
23 #include <rx/rx_prototypes.h>
24 #include <WINNT/afsreg.h>
29 long cm_daemonCheckDownInterval = 180;
30 long cm_daemonCheckUpInterval = 240;
31 long cm_daemonCheckVolInterval = 3600;
32 long cm_daemonCheckCBInterval = 60;
33 long cm_daemonCheckVolCBInterval = 0;
34 long cm_daemonCheckLockInterval = 60;
35 long cm_daemonTokenCheckInterval = 180;
36 long cm_daemonCheckOfflineVolInterval = 600;
37 long cm_daemonPerformanceTuningInterval = 0;
38 long cm_daemonRankServerInterval = 600;
40 osi_rwlock_t cm_daemonLock;
42 long cm_bkgQueueCount; /* # of queued requests */
44 int cm_bkgWaitingForCount; /* true if someone's waiting for cm_bkgQueueCount to drop */
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 */
49 extern int powerStateSuspended;
50 int daemon_ShutdownFlag = 0;
51 static int cm_nDaemons = 0;
52 static time_t lastIPAddrChange = 0;
54 static EVENT_HANDLE cm_Daemon_ShutdownEvent = NULL;
55 static EVENT_HANDLE cm_IPAddrDaemon_ShutdownEvent = NULL;
56 static EVENT_HANDLE cm_BkgDaemon_ShutdownEvent[CM_MAX_DAEMONS] =
57 {NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL};
59 void cm_IpAddrDaemon(long parm)
61 extern void smb_CheckVCs(void);
62 char * name = "cm_IPAddrDaemon_ShutdownEvent";
64 cm_IPAddrDaemon_ShutdownEvent = thrd_CreateEvent(NULL, FALSE, FALSE, name);
65 if ( GetLastError() == ERROR_ALREADY_EXISTS )
66 afsi_log("Event Object Already Exists: %s", name);
68 rx_StartClientThread();
70 while (daemon_ShutdownFlag == 0) {
73 thrd_SetEvent(cm_IPAddrDaemon_ShutdownEvent);
74 Result = NotifyAddrChange(NULL,NULL);
75 if (Result == NO_ERROR && daemon_ShutdownFlag == 0) {
76 lastIPAddrChange = osi_Time();
77 smb_SetLanAdapterChangeDetected();
78 cm_SetLanAdapterChangeDetected();
79 thrd_ResetEvent(cm_IPAddrDaemon_ShutdownEvent);
83 thrd_SetEvent(cm_IPAddrDaemon_ShutdownEvent);
86 void cm_BkgDaemon(void * parm)
91 long daemonID = (long)parm;
93 snprintf(name, sizeof(name), "cm_BkgDaemon_ShutdownEvent%d", daemonID);
95 cm_BkgDaemon_ShutdownEvent[daemonID] = thrd_CreateEvent(NULL, FALSE, FALSE, name);
96 if ( GetLastError() == ERROR_ALREADY_EXISTS )
97 afsi_log("Event Object Already Exists: %s", name);
99 rx_StartClientThread();
101 lock_ObtainWrite(&cm_daemonLock);
102 while (daemon_ShutdownFlag == 0) {
103 if (powerStateSuspended) {
107 if (!cm_bkgListEndp) {
108 osi_SleepW((LONG_PTR)&cm_bkgListp, &cm_daemonLock);
109 lock_ObtainWrite(&cm_daemonLock);
113 /* we found a request */
114 for (rp = cm_bkgListEndp; rp; rp = (cm_bkgRequest_t *) osi_QPrev(&rp->q))
116 if (cm_ServerAvailable(&rp->scp->fid, rp->userp) &&
117 !(rp->scp->flags & CM_SCACHEFLAG_DATASTORING))
121 /* we couldn't find a request that we could process at the current time */
122 lock_ReleaseWrite(&cm_daemonLock);
124 lock_ObtainWrite(&cm_daemonLock);
128 osi_QRemoveHT((osi_queue_t **) &cm_bkgListp, (osi_queue_t **) &cm_bkgListEndp, &rp->q);
129 osi_assertx(cm_bkgQueueCount-- > 0, "cm_bkgQueueCount 0");
130 lock_ReleaseWrite(&cm_daemonLock);
132 osi_Log1(afsd_logp,"cm_BkgDaemon processing request 0x%p", rp);
134 #ifdef DEBUG_REFCOUNT
135 osi_Log2(afsd_logp,"cm_BkgDaemon (before) scp 0x%x ref %d",rp->scp, rp->scp->refCount);
137 code = (*rp->procp)(rp->scp, rp->p1, rp->p2, rp->p3, rp->p4, rp->userp);
138 #ifdef DEBUG_REFCOUNT
139 osi_Log2(afsd_logp,"cm_BkgDaemon (after) scp 0x%x ref %d",rp->scp, rp->scp->refCount);
143 * Keep the following list synchronized with the
144 * error code list in cm_BkgStore.
145 * cm_SyncOpDone(CM_SCACHESYNC_ASYNCSTORE) will be called there unless
146 * one of these errors has occurred.
149 case CM_ERROR_TIMEDOUT: /* or server restarting */
151 case CM_ERROR_WOULDBLOCK:
152 case CM_ERROR_ALLBUSY:
153 case CM_ERROR_ALLDOWN:
154 case CM_ERROR_ALLOFFLINE:
155 case CM_ERROR_PARTIALWRITE:
156 if (rp->procp == cm_BkgStore) {
158 "cm_BkgDaemon re-queueing failed request 0x%p code 0x%x",
160 lock_ObtainWrite(&cm_daemonLock);
162 osi_QAddT((osi_queue_t **) &cm_bkgListp, (osi_queue_t **)&cm_bkgListEndp, &rp->q);
164 } /* otherwise fall through */
165 case 0: /* success */
166 default: /* other error */
168 osi_Log1(afsd_logp,"cm_BkgDaemon SUCCESS: request 0x%p", rp);
170 osi_Log2(afsd_logp,"cm_BkgDaemon FAILED: request dropped 0x%p code 0x%x",
172 cm_ReleaseUser(rp->userp);
173 cm_ReleaseSCache(rp->scp);
175 lock_ObtainWrite(&cm_daemonLock);
178 lock_ReleaseWrite(&cm_daemonLock);
179 thrd_SetEvent(cm_BkgDaemon_ShutdownEvent[daemonID]);
182 void cm_QueueBKGRequest(cm_scache_t *scp, cm_bkgProc_t *procp, afs_uint32 p1, afs_uint32 p2, afs_uint32 p3, afs_uint32 p4,
187 rp = malloc(sizeof(*rp));
188 memset(rp, 0, sizeof(*rp));
200 lock_ObtainWrite(&cm_daemonLock);
202 osi_QAdd((osi_queue_t **) &cm_bkgListp, &rp->q);
205 lock_ReleaseWrite(&cm_daemonLock);
207 osi_Wakeup((LONG_PTR) &cm_bkgListp);
211 IsWindowsFirewallPresent(void)
216 BOOLEAN result = FALSE;
217 LPQUERY_SERVICE_CONFIG pConfig = NULL;
221 /* Open services manager */
222 scm = OpenSCManager(NULL, NULL, GENERIC_READ);
223 if (!scm) return FALSE;
225 /* Open Windows Firewall service */
226 svc = OpenService(scm, "MpsSvc", SERVICE_QUERY_CONFIG);
228 afsi_log("MpsSvc Service could not be opened for query: 0x%x", GetLastError());
229 svc = OpenService(scm, "SharedAccess", SERVICE_QUERY_CONFIG);
231 afsi_log("SharedAccess Service could not be opened for query: 0x%x", GetLastError());
236 /* Query Windows Firewall service config, first just to get buffer size */
237 /* Expected to fail, so don't test return value */
238 (void) QueryServiceConfig(svc, NULL, 0, &BufSize);
239 status = GetLastError();
240 if (status != ERROR_INSUFFICIENT_BUFFER)
243 /* Allocate buffer */
244 pConfig = (LPQUERY_SERVICE_CONFIG)GlobalAlloc(GMEM_FIXED,BufSize);
248 /* Query Windows Firewall service config, this time for real */
249 flag = QueryServiceConfig(svc, pConfig, BufSize, &BufSize);
251 afsi_log("QueryServiceConfig failed: 0x%x", GetLastError());
255 /* Is it autostart? */
256 afsi_log("AutoStart 0x%x", pConfig->dwStartType);
257 if (pConfig->dwStartType < SERVICE_DEMAND_START)
263 CloseServiceHandle(svc);
265 CloseServiceHandle(scm);
271 cm_DaemonCheckInit(void)
278 code = RegOpenKeyEx(HKEY_LOCAL_MACHINE, AFSREG_CLT_SVC_PARAM_SUBKEY,
279 0, KEY_QUERY_VALUE, &parmKey);
283 dummyLen = sizeof(DWORD);
284 code = RegQueryValueEx(parmKey, "daemonCheckDownInterval", NULL, NULL,
285 (BYTE *) &dummy, &dummyLen);
286 if (code == ERROR_SUCCESS && dummy)
287 cm_daemonCheckDownInterval = dummy;
288 afsi_log("daemonCheckDownInterval is %d", cm_daemonCheckDownInterval);
290 dummyLen = sizeof(DWORD);
291 code = RegQueryValueEx(parmKey, "daemonCheckUpInterval", NULL, NULL,
292 (BYTE *) &dummy, &dummyLen);
293 if (code == ERROR_SUCCESS && dummy)
294 cm_daemonCheckUpInterval = dummy;
295 afsi_log("daemonCheckUpInterval is %d", cm_daemonCheckUpInterval);
297 dummyLen = sizeof(DWORD);
298 code = RegQueryValueEx(parmKey, "daemonCheckVolInterval", NULL, NULL,
299 (BYTE *) &dummy, &dummyLen);
300 if (code == ERROR_SUCCESS && dummy)
301 cm_daemonCheckVolInterval = dummy;
302 afsi_log("daemonCheckVolInterval is %d", cm_daemonCheckVolInterval);
304 dummyLen = sizeof(DWORD);
305 code = RegQueryValueEx(parmKey, "daemonCheckCBInterval", NULL, NULL,
306 (BYTE *) &dummy, &dummyLen);
307 if (code == ERROR_SUCCESS && dummy)
308 cm_daemonCheckCBInterval = dummy;
309 afsi_log("daemonCheckCBInterval is %d", cm_daemonCheckCBInterval);
311 dummyLen = sizeof(DWORD);
312 code = RegQueryValueEx(parmKey, "daemonCheckVolCBInterval", NULL, NULL,
313 (BYTE *) &dummy, &dummyLen);
314 if (code == ERROR_SUCCESS && dummy)
315 cm_daemonCheckVolCBInterval = dummy;
316 afsi_log("daemonCheckVolCBInterval is %d", cm_daemonCheckVolCBInterval);
318 dummyLen = sizeof(DWORD);
319 code = RegQueryValueEx(parmKey, "daemonCheckLockInterval", NULL, NULL,
320 (BYTE *) &dummy, &dummyLen);
321 if (code == ERROR_SUCCESS && dummy)
322 cm_daemonCheckLockInterval = dummy;
323 afsi_log("daemonCheckLockInterval is %d", cm_daemonCheckLockInterval);
325 dummyLen = sizeof(DWORD);
326 code = RegQueryValueEx(parmKey, "daemonCheckTokenInterval", NULL, NULL,
327 (BYTE *) &dummy, &dummyLen);
328 if (code == ERROR_SUCCESS && dummy)
329 cm_daemonTokenCheckInterval = dummy;
330 afsi_log("daemonCheckTokenInterval is %d", cm_daemonTokenCheckInterval);
332 dummyLen = sizeof(DWORD);
333 code = RegQueryValueEx(parmKey, "daemonCheckOfflineVolInterval", NULL, NULL,
334 (BYTE *) &dummy, &dummyLen);
335 if (code == ERROR_SUCCESS && dummy)
336 cm_daemonCheckOfflineVolInterval = dummy;
337 afsi_log("daemonCheckOfflineVolInterval is %d", cm_daemonCheckOfflineVolInterval);
339 dummyLen = sizeof(DWORD);
340 code = RegQueryValueEx(parmKey, "daemonPerformanceTuningInterval", NULL, NULL,
341 (BYTE *) &dummy, &dummyLen);
342 dummyLen = sizeof(DWORD);
343 code = RegQueryValueEx(parmKey, "daemonRankServerInterval", NULL, NULL,
344 (BYTE *) &dummy, &dummyLen);
345 if (code == ERROR_SUCCESS && dummy)
346 cm_daemonRankServerInterval = dummy;
347 afsi_log("daemonRankServerInterval is %d", cm_daemonRankServerInterval);
349 if (code == ERROR_SUCCESS)
350 cm_daemonPerformanceTuningInterval = dummy;
351 afsi_log("daemonPerformanceTuningInterval is %d", cm_daemonPerformanceTuningInterval);
353 RegCloseKey(parmKey);
355 if (cm_daemonPerformanceTuningInterval)
356 cm_PerformanceTuningInit();
359 /* periodic check daemon */
360 void cm_Daemon(long parm)
363 time_t lastLockCheck;
365 time_t lastCBExpirationCheck;
366 time_t lastVolCBRenewalCheck;
367 time_t lastDownServerCheck;
368 time_t lastUpServerCheck;
369 time_t lastTokenCacheCheck;
370 time_t lastBusyVolCheck;
371 time_t lastPerformanceCheck;
372 time_t lastServerRankCheck;
377 char * name = "cm_Daemon_ShutdownEvent";
378 int configureFirewall = IsWindowsFirewallPresent();
379 int bAddrChangeCheck = 0;
381 cm_Daemon_ShutdownEvent = thrd_CreateEvent(NULL, FALSE, FALSE, name);
382 if ( GetLastError() == ERROR_ALREADY_EXISTS )
383 afsi_log("Event Object Already Exists: %s", name);
385 if (!configureFirewall) {
386 afsi_log("No Windows Firewall detected");
389 /* ping all file servers, up or down, with unauthenticated connection,
390 * to find out whether we have all our callbacks from the server still.
391 * Also, ping down VLDBs.
394 * Seed the random number generator with our own address, so that
395 * clients starting at the same time don't all do vol checks at the
398 gethostname(thostName, sizeof(thostName));
399 thp = gethostbyname(thostName);
400 if (thp == NULL) /* In djgpp, gethostname returns the netbios
401 name of the machine. gethostbyname will fail
402 looking this up if it differs from DNS name. */
405 memcpy(&code, thp->h_addr_list[0], 4);
409 cm_DaemonCheckInit();
412 lastVolCheck = now - cm_daemonCheckVolInterval/2 + (rand() % cm_daemonCheckVolInterval);
413 lastCBExpirationCheck = now - cm_daemonCheckCBInterval/2 + (rand() % cm_daemonCheckCBInterval);
414 if (cm_daemonCheckVolCBInterval)
415 lastVolCBRenewalCheck = now - cm_daemonCheckVolCBInterval/2 + (rand() % cm_daemonCheckVolCBInterval);
416 lastLockCheck = now - cm_daemonCheckLockInterval/2 + (rand() % cm_daemonCheckLockInterval);
417 lastDownServerCheck = now - cm_daemonCheckDownInterval/2 + (rand() % cm_daemonCheckDownInterval);
418 lastUpServerCheck = now - cm_daemonCheckUpInterval/2 + (rand() % cm_daemonCheckUpInterval);
419 lastTokenCacheCheck = now - cm_daemonTokenCheckInterval/2 + (rand() % cm_daemonTokenCheckInterval);
420 lastBusyVolCheck = now - cm_daemonCheckOfflineVolInterval/2 * (rand() % cm_daemonCheckOfflineVolInterval);
421 if (cm_daemonPerformanceTuningInterval)
422 lastPerformanceCheck = now - cm_daemonPerformanceTuningInterval/2 * (rand() % cm_daemonPerformanceTuningInterval);
423 lastServerRankCheck = now - cm_daemonRankServerInterval/2 * (rand() % cm_daemonRankServerInterval);
425 while (daemon_ShutdownFlag == 0) {
426 if (powerStateSuspended) {
430 /* check to see if the listener threads halted due to network
431 * disconnect or other issues. If so, attempt to restart them.
433 smb_RestartListeners(0);
435 if (daemon_ShutdownFlag == 1)
438 if (configureFirewall) {
439 /* Open Microsoft Firewall to allow in port 7001 */
440 switch (icf_CheckAndAddAFSPorts(AFS_PORTSET_CLIENT)) {
442 afsi_log("Windows Firewall Configuration succeeded");
443 configureFirewall = 0;
446 afsi_log("Invalid Windows Firewall Port Set");
449 afsi_log("Unable to open Windows Firewall Profile");
452 afsi_log("Unable to create/modify Windows Firewall Port entries");
455 afsi_log("Unknown Windows Firewall Configuration error");
459 /* find out what time it is */
462 /* Determine whether an address change took place that we need to respond to */
463 if (bAddrChangeCheck)
464 bAddrChangeCheck = 0;
466 if (lastIPAddrChange != 0 && lastIPAddrChange + 2500 < now) {
467 bAddrChangeCheck = 1;
468 lastIPAddrChange = 0;
471 /* check down servers */
472 if ((bAddrChangeCheck || now > lastDownServerCheck + cm_daemonCheckDownInterval) &&
473 daemon_ShutdownFlag == 0 &&
474 powerStateSuspended == 0) {
475 lastDownServerCheck = now;
476 osi_Log0(afsd_logp, "cm_Daemon CheckDownServers");
477 cm_CheckServers(CM_FLAG_CHECKDOWNSERVERS, NULL);
478 if (daemon_ShutdownFlag == 1)
483 if (bAddrChangeCheck &&
484 daemon_ShutdownFlag == 0 &&
485 powerStateSuspended == 0)
486 cm_ForceNewConnectionsAllServers();
488 /* check up servers */
489 if ((bAddrChangeCheck || now > lastUpServerCheck + cm_daemonCheckUpInterval) &&
490 daemon_ShutdownFlag == 0 &&
491 powerStateSuspended == 0) {
492 lastUpServerCheck = now;
493 osi_Log0(afsd_logp, "cm_Daemon CheckUpServers");
494 cm_CheckServers(CM_FLAG_CHECKUPSERVERS, NULL);
495 if (daemon_ShutdownFlag == 1)
500 if (bAddrChangeCheck &&
501 daemon_ShutdownFlag == 0 &&
502 powerStateSuspended == 0) {
504 cm_VolStatus_Network_Addr_Change();
507 if (now > lastVolCheck + cm_daemonCheckVolInterval &&
508 daemon_ShutdownFlag == 0 &&
509 powerStateSuspended == 0) {
512 if (daemon_ShutdownFlag == 1)
517 /* Rank all up servers */
518 if ((now > lastServerRankCheck + cm_daemonRankServerInterval) &&
519 daemon_ShutdownFlag == 0 &&
520 powerStateSuspended == 0) {
521 lastServerRankCheck = now;
522 osi_Log0(afsd_logp, "cm_Daemon RankServer");
524 if(daemon_ShutdownFlag == 1)
529 if (cm_daemonCheckVolCBInterval &&
530 now > lastVolCBRenewalCheck + cm_daemonCheckVolCBInterval &&
531 daemon_ShutdownFlag == 0 &&
532 powerStateSuspended == 0) {
533 lastVolCBRenewalCheck = now;
534 cm_VolumeRenewROCallbacks();
535 if (daemon_ShutdownFlag == 1)
540 if ((bAddrChangeCheck || now > lastBusyVolCheck + cm_daemonCheckOfflineVolInterval) &&
541 daemon_ShutdownFlag == 0 &&
542 powerStateSuspended == 0) {
544 cm_CheckOfflineVolumes();
545 if (daemon_ShutdownFlag == 1)
550 if (now > lastCBExpirationCheck + cm_daemonCheckCBInterval &&
551 daemon_ShutdownFlag == 0 &&
552 powerStateSuspended == 0) {
553 lastCBExpirationCheck = now;
554 cm_CheckCBExpiration();
555 if (daemon_ShutdownFlag == 1)
560 if (now > lastLockCheck + cm_daemonCheckLockInterval &&
561 daemon_ShutdownFlag == 0 &&
562 powerStateSuspended == 0) {
565 if (daemon_ShutdownFlag == 1)
570 if (now > lastTokenCacheCheck + cm_daemonTokenCheckInterval &&
571 daemon_ShutdownFlag == 0 &&
572 powerStateSuspended == 0) {
573 lastTokenCacheCheck = now;
574 cm_CheckTokenCache(now);
575 if (daemon_ShutdownFlag == 1)
580 /* allow an exit to be called prior to stopping the service */
581 hHookDll = cm_LoadAfsdHookLib();
585 AfsdDaemonHook daemonHook = ( AfsdDaemonHook ) GetProcAddress(hHookDll, AFSD_DAEMON_HOOK);
588 hookRc = daemonHook();
590 FreeLibrary(hHookDll);
595 SetEvent(WaitToTerminate);
598 if (daemon_ShutdownFlag == 1) {
604 if (cm_daemonPerformanceTuningInterval &&
605 now > lastPerformanceCheck + cm_daemonPerformanceTuningInterval &&
606 daemon_ShutdownFlag == 0 &&
607 powerStateSuspended == 0) {
608 lastPerformanceCheck = now;
609 cm_PerformanceTuningCheck();
610 if (daemon_ShutdownFlag == 1)
615 thrd_Sleep(10000); /* sleep 10 seconds */
617 thrd_SetEvent(cm_Daemon_ShutdownEvent);
620 void cm_DaemonShutdown(void)
625 daemon_ShutdownFlag = 1;
626 osi_Wakeup((LONG_PTR) &cm_bkgListp);
628 /* wait for shutdown */
629 if (cm_Daemon_ShutdownEvent)
630 code = thrd_WaitForSingleObject_Event(cm_Daemon_ShutdownEvent, INFINITE);
632 for ( i=0; i<cm_nDaemons; i++) {
633 if (cm_BkgDaemon_ShutdownEvent[i])
634 code = thrd_WaitForSingleObject_Event(cm_BkgDaemon_ShutdownEvent[i], INFINITE);
637 if (cm_IPAddrDaemon_ShutdownEvent)
638 code = thrd_WaitForSingleObject_Event(cm_IPAddrDaemon_ShutdownEvent, INFINITE);
641 void cm_InitDaemon(int nDaemons)
643 static osi_once_t once;
648 cm_nDaemons = (nDaemons > CM_MAX_DAEMONS) ? CM_MAX_DAEMONS : nDaemons;
650 if (osi_Once(&once)) {
651 lock_InitializeRWLock(&cm_daemonLock, "cm_daemonLock",
652 LOCK_HIERARCHY_DAEMON_GLOBAL);
655 /* creating IP Address Change monitor daemon */
656 phandle = thrd_Create((SecurityAttrib) 0, 0,
657 (ThreadFunc) cm_IpAddrDaemon, 0, 0, &pid, "cm_IpAddrDaemon");
658 osi_assertx(phandle != NULL, "cm_IpAddrDaemon thread creation failure");
659 thrd_CloseHandle(phandle);
661 /* creating pinging daemon */
662 phandle = thrd_Create((SecurityAttrib) 0, 0,
663 (ThreadFunc) cm_Daemon, 0, 0, &pid, "cm_Daemon");
664 osi_assertx(phandle != NULL, "cm_Daemon thread creation failure");
665 thrd_CloseHandle(phandle);
667 for(i=0; i < cm_nDaemons; i++) {
668 phandle = thrd_Create((SecurityAttrib) 0, 0,
669 (ThreadFunc) cm_BkgDaemon, (LPVOID)i, 0, &pid,
671 osi_assertx(phandle != NULL, "cm_BkgDaemon thread creation failure");
672 thrd_CloseHandle(phandle);