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 = 600;
31 long cm_daemonCheckVolInterval = 3600;
32 long cm_daemonCheckCBInterval = 60;
33 long cm_daemonCheckLockInterval = 60;
34 long cm_daemonTokenCheckInterval = 180;
36 osi_rwlock_t cm_daemonLock;
38 long cm_bkgQueueCount; /* # of queued requests */
40 int cm_bkgWaitingForCount; /* true if someone's waiting for cm_bkgQueueCount to drop */
42 cm_bkgRequest_t *cm_bkgListp; /* first elt in the list of requests */
43 cm_bkgRequest_t *cm_bkgListEndp; /* last elt in the list of requests */
45 static int daemon_ShutdownFlag = 0;
47 void cm_IpAddrDaemon(long parm)
49 extern void smb_CheckVCs(void);
51 rx_StartClientThread();
53 while (daemon_ShutdownFlag == 0) {
54 DWORD Result = NotifyAddrChange(NULL,NULL);
55 if (Result == NO_ERROR && daemon_ShutdownFlag == 0) {
56 osi_Log0(afsd_logp, "cm_IpAddrDaemon CheckDownServers");
58 cm_ForceNewConnectionsAllServers();
59 cm_CheckServers(CM_FLAG_CHECKUPSERVERS | CM_FLAG_CHECKDOWNSERVERS, NULL);
65 void cm_BkgDaemon(long parm)
69 rx_StartClientThread();
71 lock_ObtainWrite(&cm_daemonLock);
72 while (daemon_ShutdownFlag == 0) {
73 if (!cm_bkgListEndp) {
74 osi_SleepW((LONG_PTR)&cm_bkgListp, &cm_daemonLock);
75 lock_ObtainWrite(&cm_daemonLock);
79 /* we found a request */
81 cm_bkgListEndp = (cm_bkgRequest_t *) osi_QPrev(&rp->q);
82 osi_QRemove((osi_queue_t **) &cm_bkgListp, &rp->q);
83 osi_assert(cm_bkgQueueCount-- > 0);
84 lock_ReleaseWrite(&cm_daemonLock);
87 osi_Log2(afsd_logp,"cm_BkgDaemon (before) scp 0x%x ref %d",rp->scp, rp->scp->refCount);
89 (*rp->procp)(rp->scp, rp->p1, rp->p2, rp->p3, rp->p4, rp->userp);
91 osi_Log2(afsd_logp,"cm_BkgDaemon (after) scp 0x%x ref %d",rp->scp, rp->scp->refCount);
93 cm_ReleaseUser(rp->userp);
94 cm_ReleaseSCache(rp->scp);
97 lock_ObtainWrite(&cm_daemonLock);
99 lock_ReleaseWrite(&cm_daemonLock);
102 void cm_QueueBKGRequest(cm_scache_t *scp, cm_bkgProc_t *procp, afs_uint32 p1, afs_uint32 p2, afs_uint32 p3, afs_uint32 p4,
107 rp = malloc(sizeof(*rp));
108 memset(rp, 0, sizeof(*rp));
120 lock_ObtainWrite(&cm_daemonLock);
122 osi_QAdd((osi_queue_t **) &cm_bkgListp, &rp->q);
125 lock_ReleaseWrite(&cm_daemonLock);
127 osi_Wakeup((LONG_PTR) &cm_bkgListp);
131 IsWindowsFirewallPresent(void)
136 BOOLEAN result = FALSE;
137 LPQUERY_SERVICE_CONFIG pConfig = NULL;
141 /* Open services manager */
142 scm = OpenSCManager(NULL, NULL, GENERIC_READ);
143 if (!scm) return FALSE;
145 /* Open Windows Firewall service */
146 svc = OpenService(scm, "MpsSvc", SERVICE_QUERY_CONFIG);
148 afsi_log("MpsSvc Service could not be opened for query: 0x%x", GetLastError());
149 svc = OpenService(scm, "SharedAccess", SERVICE_QUERY_CONFIG);
151 afsi_log("SharedAccess Service could not be opened for query: 0x%x", GetLastError());
156 /* Query Windows Firewall service config, first just to get buffer size */
157 /* Expected to fail, so don't test return value */
158 (void) QueryServiceConfig(svc, NULL, 0, &BufSize);
159 status = GetLastError();
160 if (status != ERROR_INSUFFICIENT_BUFFER)
163 /* Allocate buffer */
164 pConfig = (LPQUERY_SERVICE_CONFIG)GlobalAlloc(GMEM_FIXED,BufSize);
168 /* Query Windows Firewall service config, this time for real */
169 flag = QueryServiceConfig(svc, pConfig, BufSize, &BufSize);
171 afsi_log("QueryServiceConfig failed: 0x%x", GetLastError());
175 /* Is it autostart? */
176 afsi_log("AutoStart 0x%x", pConfig->dwStartType);
177 if (pConfig->dwStartType < SERVICE_DEMAND_START)
183 CloseServiceHandle(svc);
185 CloseServiceHandle(scm);
191 cm_DaemonCheckInit(void)
198 code = RegOpenKeyEx(HKEY_LOCAL_MACHINE, AFSREG_CLT_SVC_PARAM_SUBKEY,
199 0, KEY_QUERY_VALUE, &parmKey);
203 dummyLen = sizeof(DWORD);
204 code = RegQueryValueEx(parmKey, "DownServerCheckInterval", NULL, NULL,
205 (BYTE *) &dummy, &dummyLen);
206 if (code == ERROR_SUCCESS)
207 cm_daemonCheckDownInterval = dummy;
209 dummyLen = sizeof(DWORD);
210 code = RegQueryValueEx(parmKey, "UpServerCheckInterval", NULL, NULL,
211 (BYTE *) &dummy, &dummyLen);
212 if (code == ERROR_SUCCESS)
213 cm_daemonCheckUpInterval = dummy;
215 dummyLen = sizeof(DWORD);
216 code = RegQueryValueEx(parmKey, "VolumeCheckInterval", NULL, NULL,
217 (BYTE *) &dummy, &dummyLen);
218 if (code == ERROR_SUCCESS)
219 cm_daemonCheckVolInterval = dummy;
221 dummyLen = sizeof(DWORD);
222 code = RegQueryValueEx(parmKey, "CallbackCheckInterval", NULL, NULL,
223 (BYTE *) &dummy, &dummyLen);
224 if (code == ERROR_SUCCESS)
225 cm_daemonCheckCBInterval = dummy;
227 dummyLen = sizeof(DWORD);
228 code = RegQueryValueEx(parmKey, "LockCheckInterval", NULL, NULL,
229 (BYTE *) &dummy, &dummyLen);
230 if (code == ERROR_SUCCESS)
231 cm_daemonCheckLockInterval = dummy;
233 dummyLen = sizeof(DWORD);
234 code = RegQueryValueEx(parmKey, "TokenCheckInterval", NULL, NULL,
235 (BYTE *) &dummy, &dummyLen);
236 if (code == ERROR_SUCCESS)
237 cm_daemonTokenCheckInterval = dummy;
239 RegCloseKey(parmKey);
242 /* periodic check daemon */
243 void cm_Daemon(long parm)
246 time_t lastLockCheck;
248 time_t lastCBExpirationCheck;
249 time_t lastDownServerCheck;
250 time_t lastUpServerCheck;
251 time_t lastTokenCacheCheck;
256 int configureFirewall = IsWindowsFirewallPresent();
258 if (!configureFirewall) {
259 afsi_log("No Windows Firewall detected");
262 /* ping all file servers, up or down, with unauthenticated connection,
263 * to find out whether we have all our callbacks from the server still.
264 * Also, ping down VLDBs.
267 * Seed the random number generator with our own address, so that
268 * clients starting at the same time don't all do vol checks at the
271 gethostname(thostName, sizeof(thostName));
272 thp = gethostbyname(thostName);
273 if (thp == NULL) /* In djgpp, gethostname returns the netbios
274 name of the machine. gethostbyname will fail
275 looking this up if it differs from DNS name. */
278 memcpy(&code, thp->h_addr_list[0], 4);
282 cm_DaemonCheckInit();
285 lastVolCheck = now - cm_daemonCheckVolInterval/2 + (rand() % cm_daemonCheckVolInterval);
286 lastCBExpirationCheck = now - cm_daemonCheckCBInterval/2 + (rand() % cm_daemonCheckCBInterval);
287 lastLockCheck = now - cm_daemonCheckLockInterval/2 + (rand() % cm_daemonCheckLockInterval);
288 lastDownServerCheck = now - cm_daemonCheckDownInterval/2 + (rand() % cm_daemonCheckDownInterval);
289 lastUpServerCheck = now - cm_daemonCheckUpInterval/2 + (rand() % cm_daemonCheckUpInterval);
290 lastTokenCacheCheck = now - cm_daemonTokenCheckInterval/2 + (rand() % cm_daemonTokenCheckInterval);
292 while (daemon_ShutdownFlag == 0) {
293 /* check to see if the listener threads halted due to network
294 * disconnect or other issues. If so, attempt to restart them.
296 smb_RestartListeners();
298 if (configureFirewall) {
299 /* Open Microsoft Firewall to allow in port 7001 */
300 switch (icf_CheckAndAddAFSPorts(AFS_PORTSET_CLIENT)) {
302 afsi_log("Windows Firewall Configuration succeeded");
303 configureFirewall = 0;
306 afsi_log("Invalid Windows Firewall Port Set");
309 afsi_log("Unable to open Windows Firewall Profile");
312 afsi_log("Unable to create/modify Windows Firewall Port entries");
315 afsi_log("Unknown Windows Firewall Configuration error");
319 /* find out what time it is */
322 /* check down servers */
323 if (now > lastDownServerCheck + cm_daemonCheckDownInterval) {
324 lastDownServerCheck = now;
325 osi_Log0(afsd_logp, "cm_Daemon CheckDownServers");
326 cm_CheckServers(CM_FLAG_CHECKDOWNSERVERS, NULL);
330 /* check up servers */
331 if (now > lastUpServerCheck + cm_daemonCheckUpInterval) {
332 lastUpServerCheck = now;
333 osi_Log0(afsd_logp, "cm_Daemon CheckUpServers");
334 cm_CheckServers(CM_FLAG_CHECKUPSERVERS, NULL);
338 if (now > lastVolCheck + cm_daemonCheckVolInterval) {
344 if (now > lastCBExpirationCheck + cm_daemonCheckCBInterval) {
345 lastCBExpirationCheck = now;
346 cm_CheckCBExpiration();
350 if (now > lastLockCheck + cm_daemonCheckLockInterval) {
356 if (now > lastTokenCacheCheck + cm_daemonTokenCheckInterval) {
357 lastTokenCacheCheck = now;
358 cm_CheckTokenCache(now);
362 /* allow an exit to be called prior to stopping the service */
363 hHookDll = LoadLibrary(AFSD_HOOK_DLL);
367 AfsdDaemonHook daemonHook = ( AfsdDaemonHook ) GetProcAddress(hHookDll, AFSD_DAEMON_HOOK);
370 hookRc = daemonHook();
372 FreeLibrary(hHookDll);
377 SetEvent(WaitToTerminate);
381 thrd_Sleep(30 * 1000); /* sleep 30 seconds */
382 if (daemon_ShutdownFlag == 1)
387 void cm_DaemonShutdown(void)
389 daemon_ShutdownFlag = 1;
392 void cm_InitDaemon(int nDaemons)
394 static osi_once_t once;
399 if (osi_Once(&once)) {
400 lock_InitializeRWLock(&cm_daemonLock, "cm_daemonLock");
403 /* creating IP Address Change monitor daemon */
404 phandle = thrd_Create((SecurityAttrib) 0, 0,
405 (ThreadFunc) cm_IpAddrDaemon, 0, 0, &pid, "cm_IpAddrDaemon");
406 osi_assert(phandle != NULL);
407 thrd_CloseHandle(phandle);
409 /* creating pinging daemon */
410 phandle = thrd_Create((SecurityAttrib) 0, 0,
411 (ThreadFunc) cm_Daemon, 0, 0, &pid, "cm_Daemon");
412 osi_assert(phandle != NULL);
413 thrd_CloseHandle(phandle);
415 for(i=0; i < nDaemons; i++) {
416 phandle = thrd_Create((SecurityAttrib) 0, 0,
417 (ThreadFunc) cm_BkgDaemon, 0, 0, &pid,
419 osi_assert(phandle != NULL);
420 thrd_CloseHandle(phandle);