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;
39 osi_rwlock_t cm_daemonLock;
41 long cm_bkgQueueCount; /* # of queued requests */
43 int cm_bkgWaitingForCount; /* true if someone's waiting for cm_bkgQueueCount to drop */
45 cm_bkgRequest_t *cm_bkgListp; /* first elt in the list of requests */
46 cm_bkgRequest_t *cm_bkgListEndp; /* last elt in the list of requests */
48 extern int powerStateSuspended;
49 int daemon_ShutdownFlag = 0;
50 static int cm_nDaemons = 0;
51 static time_t lastIPAddrChange = 0;
53 static EVENT_HANDLE cm_Daemon_ShutdownEvent = NULL;
54 static EVENT_HANDLE cm_IPAddrDaemon_ShutdownEvent = NULL;
55 static EVENT_HANDLE cm_BkgDaemon_ShutdownEvent[CM_MAX_DAEMONS] =
56 {NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL};
58 void cm_IpAddrDaemon(long parm)
60 extern void smb_CheckVCs(void);
61 char * name = "cm_IPAddrDaemon_ShutdownEvent";
63 cm_IPAddrDaemon_ShutdownEvent = thrd_CreateEvent(NULL, FALSE, FALSE, name);
64 if ( GetLastError() == ERROR_ALREADY_EXISTS )
65 afsi_log("Event Object Already Exists: %s", name);
67 rx_StartClientThread();
69 while (daemon_ShutdownFlag == 0) {
72 thrd_SetEvent(cm_IPAddrDaemon_ShutdownEvent);
73 Result = NotifyAddrChange(NULL,NULL);
74 if (Result == NO_ERROR && daemon_ShutdownFlag == 0) {
75 lastIPAddrChange = osi_Time();
76 smb_SetLanAdapterChangeDetected();
77 cm_SetLanAdapterChangeDetected();
78 thrd_ResetEvent(cm_IPAddrDaemon_ShutdownEvent);
82 thrd_SetEvent(cm_IPAddrDaemon_ShutdownEvent);
85 void cm_BkgDaemon(void * parm)
90 long daemonID = (long)parm;
92 snprintf(name, sizeof(name), "cm_BkgDaemon_ShutdownEvent%d", daemonID);
94 cm_BkgDaemon_ShutdownEvent[daemonID] = thrd_CreateEvent(NULL, FALSE, FALSE, name);
95 if ( GetLastError() == ERROR_ALREADY_EXISTS )
96 afsi_log("Event Object Already Exists: %s", name);
98 rx_StartClientThread();
100 lock_ObtainWrite(&cm_daemonLock);
101 while (daemon_ShutdownFlag == 0) {
102 if (powerStateSuspended) {
106 if (!cm_bkgListEndp) {
107 osi_SleepW((LONG_PTR)&cm_bkgListp, &cm_daemonLock);
108 lock_ObtainWrite(&cm_daemonLock);
112 /* we found a request */
113 for (rp = cm_bkgListEndp; rp; rp = (cm_bkgRequest_t *) osi_QPrev(&rp->q))
115 if (cm_ServerAvailable(&rp->scp->fid, rp->userp) &&
116 !(rp->scp->flags & CM_SCACHEFLAG_DATASTORING))
120 /* we couldn't find a request that we could process at the current time */
121 lock_ReleaseWrite(&cm_daemonLock);
123 lock_ObtainWrite(&cm_daemonLock);
127 osi_QRemoveHT((osi_queue_t **) &cm_bkgListp, (osi_queue_t **) &cm_bkgListEndp, &rp->q);
128 osi_assertx(cm_bkgQueueCount-- > 0, "cm_bkgQueueCount 0");
129 lock_ReleaseWrite(&cm_daemonLock);
131 osi_Log1(afsd_logp,"cm_BkgDaemon processing request 0x%p", rp);
133 #ifdef DEBUG_REFCOUNT
134 osi_Log2(afsd_logp,"cm_BkgDaemon (before) scp 0x%x ref %d",rp->scp, rp->scp->refCount);
136 code = (*rp->procp)(rp->scp, rp->p1, rp->p2, rp->p3, rp->p4, rp->userp);
137 #ifdef DEBUG_REFCOUNT
138 osi_Log2(afsd_logp,"cm_BkgDaemon (after) scp 0x%x ref %d",rp->scp, rp->scp->refCount);
142 * Keep the following list synchronized with the
143 * error code list in cm_BkgStore.
144 * cm_SyncOpDone(CM_SCACHESYNC_ASYNCSTORE) will be called there unless
145 * one of these errors has occurred.
148 case CM_ERROR_TIMEDOUT: /* or server restarting */
150 case CM_ERROR_WOULDBLOCK:
151 case CM_ERROR_ALLBUSY:
152 case CM_ERROR_ALLDOWN:
153 case CM_ERROR_ALLOFFLINE:
154 case CM_ERROR_PARTIALWRITE:
155 osi_Log2(afsd_logp,"cm_BkgDaemon re-queueing failed request 0x%p code 0x%x",
157 lock_ObtainWrite(&cm_daemonLock);
159 osi_QAddT((osi_queue_t **) &cm_bkgListp, (osi_queue_t **)&cm_bkgListEndp, &rp->q);
161 case 0: /* success */
162 default: /* other error */
164 osi_Log1(afsd_logp,"cm_BkgDaemon SUCCESS: request 0x%p", rp);
166 osi_Log2(afsd_logp,"cm_BkgDaemon FAILED: request dropped 0x%p code 0x%x",
168 cm_ReleaseUser(rp->userp);
169 cm_ReleaseSCache(rp->scp);
171 lock_ObtainWrite(&cm_daemonLock);
174 lock_ReleaseWrite(&cm_daemonLock);
176 thrd_SetEvent(cm_BkgDaemon_ShutdownEvent[daemonID]);
179 void cm_QueueBKGRequest(cm_scache_t *scp, cm_bkgProc_t *procp, afs_uint32 p1, afs_uint32 p2, afs_uint32 p3, afs_uint32 p4,
184 rp = malloc(sizeof(*rp));
185 memset(rp, 0, sizeof(*rp));
197 lock_ObtainWrite(&cm_daemonLock);
199 osi_QAdd((osi_queue_t **) &cm_bkgListp, &rp->q);
202 lock_ReleaseWrite(&cm_daemonLock);
204 osi_Wakeup((LONG_PTR) &cm_bkgListp);
208 IsWindowsFirewallPresent(void)
213 BOOLEAN result = FALSE;
214 LPQUERY_SERVICE_CONFIG pConfig = NULL;
218 /* Open services manager */
219 scm = OpenSCManager(NULL, NULL, GENERIC_READ);
220 if (!scm) return FALSE;
222 /* Open Windows Firewall service */
223 svc = OpenService(scm, "MpsSvc", SERVICE_QUERY_CONFIG);
225 afsi_log("MpsSvc Service could not be opened for query: 0x%x", GetLastError());
226 svc = OpenService(scm, "SharedAccess", SERVICE_QUERY_CONFIG);
228 afsi_log("SharedAccess Service could not be opened for query: 0x%x", GetLastError());
233 /* Query Windows Firewall service config, first just to get buffer size */
234 /* Expected to fail, so don't test return value */
235 (void) QueryServiceConfig(svc, NULL, 0, &BufSize);
236 status = GetLastError();
237 if (status != ERROR_INSUFFICIENT_BUFFER)
240 /* Allocate buffer */
241 pConfig = (LPQUERY_SERVICE_CONFIG)GlobalAlloc(GMEM_FIXED,BufSize);
245 /* Query Windows Firewall service config, this time for real */
246 flag = QueryServiceConfig(svc, pConfig, BufSize, &BufSize);
248 afsi_log("QueryServiceConfig failed: 0x%x", GetLastError());
252 /* Is it autostart? */
253 afsi_log("AutoStart 0x%x", pConfig->dwStartType);
254 if (pConfig->dwStartType < SERVICE_DEMAND_START)
260 CloseServiceHandle(svc);
262 CloseServiceHandle(scm);
268 cm_DaemonCheckInit(void)
275 code = RegOpenKeyEx(HKEY_LOCAL_MACHINE, AFSREG_CLT_SVC_PARAM_SUBKEY,
276 0, KEY_QUERY_VALUE, &parmKey);
280 dummyLen = sizeof(DWORD);
281 code = RegQueryValueEx(parmKey, "daemonCheckDownInterval", NULL, NULL,
282 (BYTE *) &dummy, &dummyLen);
283 if (code == ERROR_SUCCESS && dummy)
284 cm_daemonCheckDownInterval = dummy;
285 afsi_log("daemonCheckDownInterval is %d", cm_daemonCheckDownInterval);
287 dummyLen = sizeof(DWORD);
288 code = RegQueryValueEx(parmKey, "daemonCheckUpInterval", NULL, NULL,
289 (BYTE *) &dummy, &dummyLen);
290 if (code == ERROR_SUCCESS && dummy)
291 cm_daemonCheckUpInterval = dummy;
292 afsi_log("daemonCheckUpInterval is %d", cm_daemonCheckUpInterval);
294 dummyLen = sizeof(DWORD);
295 code = RegQueryValueEx(parmKey, "daemonCheckVolInterval", NULL, NULL,
296 (BYTE *) &dummy, &dummyLen);
297 if (code == ERROR_SUCCESS && dummy)
298 cm_daemonCheckVolInterval = dummy;
299 afsi_log("daemonCheckVolInterval is %d", cm_daemonCheckVolInterval);
301 dummyLen = sizeof(DWORD);
302 code = RegQueryValueEx(parmKey, "daemonCheckCBInterval", NULL, NULL,
303 (BYTE *) &dummy, &dummyLen);
304 if (code == ERROR_SUCCESS && dummy)
305 cm_daemonCheckCBInterval = dummy;
306 afsi_log("daemonCheckCBInterval is %d", cm_daemonCheckCBInterval);
308 dummyLen = sizeof(DWORD);
309 code = RegQueryValueEx(parmKey, "daemonCheckVolCBInterval", NULL, NULL,
310 (BYTE *) &dummy, &dummyLen);
311 if (code == ERROR_SUCCESS && dummy)
312 cm_daemonCheckVolCBInterval = dummy;
313 afsi_log("daemonCheckVolCBInterval is %d", cm_daemonCheckVolCBInterval);
315 dummyLen = sizeof(DWORD);
316 code = RegQueryValueEx(parmKey, "daemonCheckLockInterval", NULL, NULL,
317 (BYTE *) &dummy, &dummyLen);
318 if (code == ERROR_SUCCESS && dummy)
319 cm_daemonCheckLockInterval = dummy;
320 afsi_log("daemonCheckLockInterval is %d", cm_daemonCheckLockInterval);
322 dummyLen = sizeof(DWORD);
323 code = RegQueryValueEx(parmKey, "daemonCheckTokenInterval", NULL, NULL,
324 (BYTE *) &dummy, &dummyLen);
325 if (code == ERROR_SUCCESS && dummy)
326 cm_daemonTokenCheckInterval = dummy;
327 afsi_log("daemonCheckTokenInterval is %d", cm_daemonTokenCheckInterval);
329 dummyLen = sizeof(DWORD);
330 code = RegQueryValueEx(parmKey, "daemonCheckOfflineVolInterval", NULL, NULL,
331 (BYTE *) &dummy, &dummyLen);
332 if (code == ERROR_SUCCESS && dummy)
333 cm_daemonCheckOfflineVolInterval = dummy;
334 afsi_log("daemonCheckOfflineVolInterval is %d", cm_daemonCheckOfflineVolInterval);
336 dummyLen = sizeof(DWORD);
337 code = RegQueryValueEx(parmKey, "daemonPerformanceTuningInterval", NULL, NULL,
338 (BYTE *) &dummy, &dummyLen);
339 if (code == ERROR_SUCCESS)
340 cm_daemonPerformanceTuningInterval = dummy;
341 afsi_log("daemonPerformanceTuningInterval is %d", cm_daemonPerformanceTuningInterval);
343 RegCloseKey(parmKey);
345 if (cm_daemonPerformanceTuningInterval)
346 cm_PerformanceTuningInit();
349 /* periodic check daemon */
350 void cm_Daemon(long parm)
353 time_t lastLockCheck;
355 time_t lastCBExpirationCheck;
356 time_t lastVolCBRenewalCheck;
357 time_t lastDownServerCheck;
358 time_t lastUpServerCheck;
359 time_t lastTokenCacheCheck;
360 time_t lastBusyVolCheck;
361 time_t lastPerformanceCheck;
366 char * name = "cm_Daemon_ShutdownEvent";
367 int configureFirewall = IsWindowsFirewallPresent();
368 int bAddrChangeCheck = 0;
370 cm_Daemon_ShutdownEvent = thrd_CreateEvent(NULL, FALSE, FALSE, name);
371 if ( GetLastError() == ERROR_ALREADY_EXISTS )
372 afsi_log("Event Object Already Exists: %s", name);
374 if (!configureFirewall) {
375 afsi_log("No Windows Firewall detected");
378 /* ping all file servers, up or down, with unauthenticated connection,
379 * to find out whether we have all our callbacks from the server still.
380 * Also, ping down VLDBs.
383 * Seed the random number generator with our own address, so that
384 * clients starting at the same time don't all do vol checks at the
387 gethostname(thostName, sizeof(thostName));
388 thp = gethostbyname(thostName);
389 if (thp == NULL) /* In djgpp, gethostname returns the netbios
390 name of the machine. gethostbyname will fail
391 looking this up if it differs from DNS name. */
394 memcpy(&code, thp->h_addr_list[0], 4);
398 cm_DaemonCheckInit();
401 lastVolCheck = now - cm_daemonCheckVolInterval/2 + (rand() % cm_daemonCheckVolInterval);
402 lastCBExpirationCheck = now - cm_daemonCheckCBInterval/2 + (rand() % cm_daemonCheckCBInterval);
403 if (cm_daemonCheckVolCBInterval)
404 lastVolCBRenewalCheck = now - cm_daemonCheckVolCBInterval/2 + (rand() % cm_daemonCheckVolCBInterval);
405 lastLockCheck = now - cm_daemonCheckLockInterval/2 + (rand() % cm_daemonCheckLockInterval);
406 lastDownServerCheck = now - cm_daemonCheckDownInterval/2 + (rand() % cm_daemonCheckDownInterval);
407 lastUpServerCheck = now - cm_daemonCheckUpInterval/2 + (rand() % cm_daemonCheckUpInterval);
408 lastTokenCacheCheck = now - cm_daemonTokenCheckInterval/2 + (rand() % cm_daemonTokenCheckInterval);
409 lastBusyVolCheck = now - cm_daemonCheckOfflineVolInterval/2 * (rand() % cm_daemonCheckOfflineVolInterval);
410 if (cm_daemonPerformanceTuningInterval)
411 lastPerformanceCheck = now - cm_daemonPerformanceTuningInterval/2 * (rand() % cm_daemonPerformanceTuningInterval);
413 while (daemon_ShutdownFlag == 0) {
414 if (powerStateSuspended) {
418 /* check to see if the listener threads halted due to network
419 * disconnect or other issues. If so, attempt to restart them.
421 smb_RestartListeners(0);
423 if (daemon_ShutdownFlag == 1)
426 if (configureFirewall) {
427 /* Open Microsoft Firewall to allow in port 7001 */
428 switch (icf_CheckAndAddAFSPorts(AFS_PORTSET_CLIENT)) {
430 afsi_log("Windows Firewall Configuration succeeded");
431 configureFirewall = 0;
434 afsi_log("Invalid Windows Firewall Port Set");
437 afsi_log("Unable to open Windows Firewall Profile");
440 afsi_log("Unable to create/modify Windows Firewall Port entries");
443 afsi_log("Unknown Windows Firewall Configuration error");
447 /* find out what time it is */
450 /* Determine whether an address change took place that we need to respond to */
451 if (bAddrChangeCheck)
452 bAddrChangeCheck = 0;
454 if (lastIPAddrChange != 0 && lastIPAddrChange + 2500 < now) {
455 bAddrChangeCheck = 1;
456 lastIPAddrChange = 0;
459 /* check down servers */
460 if ((bAddrChangeCheck || now > lastDownServerCheck + cm_daemonCheckDownInterval) &&
461 daemon_ShutdownFlag == 0 &&
462 powerStateSuspended == 0) {
463 lastDownServerCheck = now;
464 osi_Log0(afsd_logp, "cm_Daemon CheckDownServers");
465 cm_CheckServers(CM_FLAG_CHECKDOWNSERVERS, NULL);
466 if (daemon_ShutdownFlag == 1)
471 if (bAddrChangeCheck &&
472 daemon_ShutdownFlag == 0 &&
473 powerStateSuspended == 0)
474 cm_ForceNewConnectionsAllServers();
476 /* check up servers */
477 if ((bAddrChangeCheck || now > lastUpServerCheck + cm_daemonCheckUpInterval) &&
478 daemon_ShutdownFlag == 0 &&
479 powerStateSuspended == 0) {
480 lastUpServerCheck = now;
481 osi_Log0(afsd_logp, "cm_Daemon CheckUpServers");
482 cm_CheckServers(CM_FLAG_CHECKUPSERVERS, NULL);
483 if (daemon_ShutdownFlag == 1)
488 if (bAddrChangeCheck &&
489 daemon_ShutdownFlag == 0 &&
490 powerStateSuspended == 0) {
492 cm_VolStatus_Network_Addr_Change();
495 if (now > lastVolCheck + cm_daemonCheckVolInterval &&
496 daemon_ShutdownFlag == 0 &&
497 powerStateSuspended == 0) {
500 if (daemon_ShutdownFlag == 1)
505 if (cm_daemonCheckVolCBInterval &&
506 now > lastVolCBRenewalCheck + cm_daemonCheckVolCBInterval &&
507 daemon_ShutdownFlag == 0 &&
508 powerStateSuspended == 0) {
509 lastVolCBRenewalCheck = now;
510 cm_VolumeRenewROCallbacks();
511 if (daemon_ShutdownFlag == 1)
516 if ((bAddrChangeCheck || now > lastBusyVolCheck + cm_daemonCheckOfflineVolInterval) &&
517 daemon_ShutdownFlag == 0 &&
518 powerStateSuspended == 0) {
520 cm_CheckOfflineVolumes();
521 if (daemon_ShutdownFlag == 1)
526 if (now > lastCBExpirationCheck + cm_daemonCheckCBInterval &&
527 daemon_ShutdownFlag == 0 &&
528 powerStateSuspended == 0) {
529 lastCBExpirationCheck = now;
530 cm_CheckCBExpiration();
531 if (daemon_ShutdownFlag == 1)
536 if (now > lastLockCheck + cm_daemonCheckLockInterval &&
537 daemon_ShutdownFlag == 0 &&
538 powerStateSuspended == 0) {
541 if (daemon_ShutdownFlag == 1)
546 if (now > lastTokenCacheCheck + cm_daemonTokenCheckInterval &&
547 daemon_ShutdownFlag == 0 &&
548 powerStateSuspended == 0) {
549 lastTokenCacheCheck = now;
550 cm_CheckTokenCache(now);
551 if (daemon_ShutdownFlag == 1)
556 /* allow an exit to be called prior to stopping the service */
557 hHookDll = cm_LoadAfsdHookLib();
561 AfsdDaemonHook daemonHook = ( AfsdDaemonHook ) GetProcAddress(hHookDll, AFSD_DAEMON_HOOK);
564 hookRc = daemonHook();
566 FreeLibrary(hHookDll);
571 SetEvent(WaitToTerminate);
574 if (daemon_ShutdownFlag == 1) {
580 if (cm_daemonPerformanceTuningInterval &&
581 now > lastPerformanceCheck + cm_daemonPerformanceTuningInterval &&
582 daemon_ShutdownFlag == 0 &&
583 powerStateSuspended == 0) {
584 lastPerformanceCheck = now;
585 cm_PerformanceTuningCheck();
586 if (daemon_ShutdownFlag == 1)
591 thrd_Sleep(10000); /* sleep 10 seconds */
593 thrd_SetEvent(cm_Daemon_ShutdownEvent);
596 void cm_DaemonShutdown(void)
601 daemon_ShutdownFlag = 1;
602 osi_Wakeup((LONG_PTR) &cm_bkgListp);
604 /* wait for shutdown */
605 if (cm_Daemon_ShutdownEvent)
606 code = thrd_WaitForSingleObject_Event(cm_Daemon_ShutdownEvent, INFINITE);
608 for ( i=0; i<cm_nDaemons; i++) {
609 if (cm_BkgDaemon_ShutdownEvent[i])
610 code = thrd_WaitForSingleObject_Event(cm_BkgDaemon_ShutdownEvent[i], INFINITE);
613 if (cm_IPAddrDaemon_ShutdownEvent)
614 code = thrd_WaitForSingleObject_Event(cm_IPAddrDaemon_ShutdownEvent, INFINITE);
617 void cm_InitDaemon(int nDaemons)
619 static osi_once_t once;
624 cm_nDaemons = (nDaemons > CM_MAX_DAEMONS) ? CM_MAX_DAEMONS : nDaemons;
626 if (osi_Once(&once)) {
627 lock_InitializeRWLock(&cm_daemonLock, "cm_daemonLock",
628 LOCK_HIERARCHY_DAEMON_GLOBAL);
631 /* creating IP Address Change monitor daemon */
632 phandle = thrd_Create((SecurityAttrib) 0, 0,
633 (ThreadFunc) cm_IpAddrDaemon, 0, 0, &pid, "cm_IpAddrDaemon");
634 osi_assertx(phandle != NULL, "cm_IpAddrDaemon thread creation failure");
635 thrd_CloseHandle(phandle);
637 /* creating pinging daemon */
638 phandle = thrd_Create((SecurityAttrib) 0, 0,
639 (ThreadFunc) cm_Daemon, 0, 0, &pid, "cm_Daemon");
640 osi_assertx(phandle != NULL, "cm_Daemon thread creation failure");
641 thrd_CloseHandle(phandle);
643 for(i=0; i < cm_nDaemons; i++) {
644 phandle = thrd_Create((SecurityAttrib) 0, 0,
645 (ThreadFunc) cm_BkgDaemon, (LPVOID)i, 0, &pid,
647 osi_assertx(phandle != NULL, "cm_BkgDaemon thread creation failure");
648 thrd_CloseHandle(phandle);