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>
27 #include <rx/rx_prototypes.h>
28 #include <WINNT/afsreg.h>
33 long cm_daemonCheckDownInterval = 180;
34 long cm_daemonCheckUpInterval = 600;
35 long cm_daemonCheckVolInterval = 3600;
36 long cm_daemonCheckCBInterval = 60;
37 long cm_daemonCheckLockInterval = 60;
38 long cm_daemonTokenCheckInterval = 180;
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 static int daemon_ShutdownFlag = 0;
52 void cm_IpAddrDaemon(long parm)
54 extern void smb_CheckVCs(void);
56 rx_StartClientThread();
58 while (daemon_ShutdownFlag == 0) {
59 DWORD Result = NotifyAddrChange(NULL,NULL);
60 if (Result == NO_ERROR && daemon_ShutdownFlag == 0) {
61 osi_Log0(afsd_logp, "cm_IpAddrDaemon CheckDownServers");
63 cm_ForceNewConnectionsAllServers();
64 cm_CheckServers(CM_FLAG_CHECKUPSERVERS | CM_FLAG_CHECKDOWNSERVERS, NULL);
71 void cm_BkgDaemon(long parm)
76 rx_StartClientThread();
78 lock_ObtainWrite(&cm_daemonLock);
79 while (daemon_ShutdownFlag == 0) {
80 if (!cm_bkgListEndp) {
81 osi_SleepW((LONG_PTR)&cm_bkgListp, &cm_daemonLock);
82 lock_ObtainWrite(&cm_daemonLock);
86 /* we found a request */
87 for (rp = cm_bkgListEndp; rp; rp = (cm_bkgRequest_t *) osi_QPrev(&rp->q))
89 if (cm_ServerAvailable(&rp->scp->fid, rp->userp))
93 /* we couldn't find a request that we could process at the current time */
94 lock_ReleaseWrite(&cm_daemonLock);
96 lock_ObtainWrite(&cm_daemonLock);
100 osi_QRemoveHT((osi_queue_t **) &cm_bkgListp, (osi_queue_t **) &cm_bkgListEndp, &rp->q);
101 osi_assert(cm_bkgQueueCount-- > 0);
102 lock_ReleaseWrite(&cm_daemonLock);
104 #ifdef DEBUG_REFCOUNT
105 osi_Log2(afsd_logp,"cm_BkgDaemon (before) scp 0x%x ref %d",rp->scp, rp->scp->refCount);
107 code = (*rp->procp)(rp->scp, rp->p1, rp->p2, rp->p3, rp->p4, rp->userp);
108 #ifdef DEBUG_REFCOUNT
109 osi_Log2(afsd_logp,"cm_BkgDaemon (after) scp 0x%x ref %d",rp->scp, rp->scp->refCount);
112 cm_ReleaseUser(rp->userp);
113 cm_ReleaseSCache(rp->scp);
117 lock_ObtainWrite(&cm_daemonLock);
120 case CM_ERROR_TIMEDOUT:
122 case CM_ERROR_WOULDBLOCK:
123 case CM_ERROR_ALLBUSY:
124 case CM_ERROR_ALLDOWN:
125 case CM_ERROR_ALLOFFLINE:
126 case CM_ERROR_PARTIALWRITE:
127 osi_Log2(afsd_logp,"cm_BkgDaemon re-queueing failed request 0x%p code 0x%x",
130 osi_QAddT((osi_queue_t **) &cm_bkgListp, (osi_queue_t **)&cm_bkgListEndp, &rp->q);
133 osi_Log2(afsd_logp,"cm_BkgDaemon failed request dropped 0x%p code 0x%x",
137 lock_ReleaseWrite(&cm_daemonLock);
140 void cm_QueueBKGRequest(cm_scache_t *scp, cm_bkgProc_t *procp, afs_uint32 p1, afs_uint32 p2, afs_uint32 p3, afs_uint32 p4,
145 rp = malloc(sizeof(*rp));
146 memset(rp, 0, sizeof(*rp));
158 lock_ObtainWrite(&cm_daemonLock);
160 osi_QAdd((osi_queue_t **) &cm_bkgListp, &rp->q);
163 lock_ReleaseWrite(&cm_daemonLock);
165 osi_Wakeup((LONG_PTR) &cm_bkgListp);
169 IsWindowsFirewallPresent(void)
174 BOOLEAN result = FALSE;
175 LPQUERY_SERVICE_CONFIG pConfig = NULL;
179 /* Open services manager */
180 scm = OpenSCManager(NULL, NULL, GENERIC_READ);
181 if (!scm) return FALSE;
183 /* Open Windows Firewall service */
184 svc = OpenService(scm, "MpsSvc", SERVICE_QUERY_CONFIG);
186 afsi_log("MpsSvc Service could not be opened for query: 0x%x", GetLastError());
187 svc = OpenService(scm, "SharedAccess", SERVICE_QUERY_CONFIG);
189 afsi_log("SharedAccess Service could not be opened for query: 0x%x", GetLastError());
194 /* Query Windows Firewall service config, first just to get buffer size */
195 /* Expected to fail, so don't test return value */
196 (void) QueryServiceConfig(svc, NULL, 0, &BufSize);
197 status = GetLastError();
198 if (status != ERROR_INSUFFICIENT_BUFFER)
201 /* Allocate buffer */
202 pConfig = (LPQUERY_SERVICE_CONFIG)GlobalAlloc(GMEM_FIXED,BufSize);
206 /* Query Windows Firewall service config, this time for real */
207 flag = QueryServiceConfig(svc, pConfig, BufSize, &BufSize);
209 afsi_log("QueryServiceConfig failed: 0x%x", GetLastError());
213 /* Is it autostart? */
214 afsi_log("AutoStart 0x%x", pConfig->dwStartType);
215 if (pConfig->dwStartType < SERVICE_DEMAND_START)
221 CloseServiceHandle(svc);
223 CloseServiceHandle(scm);
229 cm_DaemonCheckInit(void)
236 code = RegOpenKeyEx(HKEY_LOCAL_MACHINE, AFSREG_CLT_SVC_PARAM_SUBKEY,
237 0, KEY_QUERY_VALUE, &parmKey);
241 dummyLen = sizeof(DWORD);
242 code = RegQueryValueEx(parmKey, "DownServerCheckInterval", NULL, NULL,
243 (BYTE *) &dummy, &dummyLen);
244 if (code == ERROR_SUCCESS)
245 cm_daemonCheckDownInterval = dummy;
247 dummyLen = sizeof(DWORD);
248 code = RegQueryValueEx(parmKey, "UpServerCheckInterval", NULL, NULL,
249 (BYTE *) &dummy, &dummyLen);
250 if (code == ERROR_SUCCESS)
251 cm_daemonCheckUpInterval = dummy;
253 dummyLen = sizeof(DWORD);
254 code = RegQueryValueEx(parmKey, "VolumeCheckInterval", NULL, NULL,
255 (BYTE *) &dummy, &dummyLen);
256 if (code == ERROR_SUCCESS)
257 cm_daemonCheckVolInterval = dummy;
259 dummyLen = sizeof(DWORD);
260 code = RegQueryValueEx(parmKey, "CallbackCheckInterval", NULL, NULL,
261 (BYTE *) &dummy, &dummyLen);
262 if (code == ERROR_SUCCESS)
263 cm_daemonCheckCBInterval = dummy;
265 dummyLen = sizeof(DWORD);
266 code = RegQueryValueEx(parmKey, "LockCheckInterval", NULL, NULL,
267 (BYTE *) &dummy, &dummyLen);
268 if (code == ERROR_SUCCESS)
269 cm_daemonCheckLockInterval = dummy;
271 dummyLen = sizeof(DWORD);
272 code = RegQueryValueEx(parmKey, "TokenCheckInterval", NULL, NULL,
273 (BYTE *) &dummy, &dummyLen);
274 if (code == ERROR_SUCCESS)
275 cm_daemonTokenCheckInterval = dummy;
277 RegCloseKey(parmKey);
280 /* periodic check daemon */
281 void cm_Daemon(long parm)
284 time_t lastLockCheck;
286 time_t lastCBExpirationCheck;
287 time_t lastDownServerCheck;
288 time_t lastUpServerCheck;
289 time_t lastTokenCacheCheck;
294 int configureFirewall = IsWindowsFirewallPresent();
296 if (!configureFirewall) {
297 afsi_log("No Windows Firewall detected");
300 /* ping all file servers, up or down, with unauthenticated connection,
301 * to find out whether we have all our callbacks from the server still.
302 * Also, ping down VLDBs.
305 * Seed the random number generator with our own address, so that
306 * clients starting at the same time don't all do vol checks at the
309 gethostname(thostName, sizeof(thostName));
310 thp = gethostbyname(thostName);
311 if (thp == NULL) /* In djgpp, gethostname returns the netbios
312 name of the machine. gethostbyname will fail
313 looking this up if it differs from DNS name. */
316 memcpy(&code, thp->h_addr_list[0], 4);
320 cm_DaemonCheckInit();
323 lastVolCheck = now - cm_daemonCheckVolInterval/2 + (rand() % cm_daemonCheckVolInterval);
324 lastCBExpirationCheck = now - cm_daemonCheckCBInterval/2 + (rand() % cm_daemonCheckCBInterval);
325 lastLockCheck = now - cm_daemonCheckLockInterval/2 + (rand() % cm_daemonCheckLockInterval);
326 lastDownServerCheck = now - cm_daemonCheckDownInterval/2 + (rand() % cm_daemonCheckDownInterval);
327 lastUpServerCheck = now - cm_daemonCheckUpInterval/2 + (rand() % cm_daemonCheckUpInterval);
328 lastTokenCacheCheck = now - cm_daemonTokenCheckInterval/2 + (rand() % cm_daemonTokenCheckInterval);
330 while (daemon_ShutdownFlag == 0) {
331 /* check to see if the listener threads halted due to network
332 * disconnect or other issues. If so, attempt to restart them.
334 smb_RestartListeners();
336 if (configureFirewall) {
337 /* Open Microsoft Firewall to allow in port 7001 */
338 switch (icf_CheckAndAddAFSPorts(AFS_PORTSET_CLIENT)) {
340 afsi_log("Windows Firewall Configuration succeeded");
341 configureFirewall = 0;
344 afsi_log("Invalid Windows Firewall Port Set");
347 afsi_log("Unable to open Windows Firewall Profile");
350 afsi_log("Unable to create/modify Windows Firewall Port entries");
353 afsi_log("Unknown Windows Firewall Configuration error");
357 /* find out what time it is */
360 /* check down servers */
361 if (now > lastDownServerCheck + cm_daemonCheckDownInterval) {
362 lastDownServerCheck = now;
363 osi_Log0(afsd_logp, "cm_Daemon CheckDownServers");
364 cm_CheckServers(CM_FLAG_CHECKDOWNSERVERS, NULL);
368 /* check up servers */
369 if (now > lastUpServerCheck + cm_daemonCheckUpInterval) {
370 lastUpServerCheck = now;
371 osi_Log0(afsd_logp, "cm_Daemon CheckUpServers");
372 cm_CheckServers(CM_FLAG_CHECKUPSERVERS, NULL);
376 if (now > lastVolCheck + cm_daemonCheckVolInterval) {
382 if (now > lastCBExpirationCheck + cm_daemonCheckCBInterval) {
383 lastCBExpirationCheck = now;
384 cm_CheckCBExpiration();
388 if (now > lastLockCheck + cm_daemonCheckLockInterval) {
394 if (now > lastTokenCacheCheck + cm_daemonTokenCheckInterval) {
395 lastTokenCacheCheck = now;
396 cm_CheckTokenCache(now);
400 /* allow an exit to be called prior to stopping the service */
401 hHookDll = LoadLibrary(AFSD_HOOK_DLL);
405 AfsdDaemonHook daemonHook = ( AfsdDaemonHook ) GetProcAddress(hHookDll, AFSD_DAEMON_HOOK);
408 hookRc = daemonHook();
410 FreeLibrary(hHookDll);
415 SetEvent(WaitToTerminate);
419 thrd_Sleep(30 * 1000); /* sleep 30 seconds */
420 if (daemon_ShutdownFlag == 1)
425 void cm_DaemonShutdown(void)
427 daemon_ShutdownFlag = 1;
430 void cm_InitDaemon(int nDaemons)
432 static osi_once_t once;
437 if (osi_Once(&once)) {
438 lock_InitializeRWLock(&cm_daemonLock, "cm_daemonLock");
442 /* creating IP Address Change monitor daemon */
443 phandle = thrd_Create((SecurityAttrib) 0, 0,
444 (ThreadFunc) cm_IpAddrDaemon, 0, 0, &pid, "cm_IpAddrDaemon");
445 osi_assert(phandle != NULL);
446 thrd_CloseHandle(phandle);
449 /* creating pinging daemon */
450 phandle = thrd_Create((SecurityAttrib) 0, 0,
451 (ThreadFunc) cm_Daemon, 0, 0, &pid, "cm_Daemon");
452 osi_assert(phandle != NULL);
453 thrd_CloseHandle(phandle);
455 for(i=0; i < nDaemons; i++) {
456 phandle = thrd_Create((SecurityAttrib) 0, 0,
457 (ThreadFunc) cm_BkgDaemon, 0, 0, &pid,
459 osi_assert(phandle != NULL);
460 thrd_CloseHandle(phandle);