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);
86 osi_Log2(afsd_logp,"cm_BkgDaemon (before) scp 0x%x ref %d",rp->scp, rp->scp->refCount);
88 (*rp->procp)(rp->scp, rp->p1, rp->p2, rp->p3, rp->p4, rp->userp);
90 osi_Log2(afsd_logp,"cm_BkgDaemon (after) scp 0x%x ref %d",rp->scp, rp->scp->refCount);
92 cm_ReleaseUser(rp->userp);
93 cm_ReleaseSCache(rp->scp);
96 lock_ObtainWrite(&cm_daemonLock);
98 lock_ReleaseWrite(&cm_daemonLock);
101 void cm_QueueBKGRequest(cm_scache_t *scp, cm_bkgProc_t *procp, afs_uint32 p1, afs_uint32 p2, afs_uint32 p3, afs_uint32 p4,
106 rp = malloc(sizeof(*rp));
107 memset(rp, 0, sizeof(*rp));
119 lock_ObtainWrite(&cm_daemonLock);
121 osi_QAdd((osi_queue_t **) &cm_bkgListp, &rp->q);
124 lock_ReleaseWrite(&cm_daemonLock);
126 osi_Wakeup((LONG_PTR) &cm_bkgListp);
130 IsWindowsFirewallPresent(void)
135 BOOLEAN result = FALSE;
136 LPQUERY_SERVICE_CONFIG pConfig = NULL;
140 /* Open services manager */
141 scm = OpenSCManager(NULL, NULL, GENERIC_READ);
142 if (!scm) return FALSE;
144 /* Open Windows Firewall service */
145 svc = OpenService(scm, "MpsSvc", SERVICE_QUERY_CONFIG);
147 afsi_log("MpsSvc Service could not be opened for query: 0x%x", GetLastError());
148 svc = OpenService(scm, "SharedAccess", SERVICE_QUERY_CONFIG);
150 afsi_log("SharedAccess Service could not be opened for query: 0x%x", GetLastError());
155 /* Query Windows Firewall service config, first just to get buffer size */
156 /* Expected to fail, so don't test return value */
157 (void) QueryServiceConfig(svc, NULL, 0, &BufSize);
158 status = GetLastError();
159 if (status != ERROR_INSUFFICIENT_BUFFER)
162 /* Allocate buffer */
163 pConfig = (LPQUERY_SERVICE_CONFIG)GlobalAlloc(GMEM_FIXED,BufSize);
167 /* Query Windows Firewall service config, this time for real */
168 flag = QueryServiceConfig(svc, pConfig, BufSize, &BufSize);
170 afsi_log("QueryServiceConfig failed: 0x%x", GetLastError());
174 /* Is it autostart? */
175 afsi_log("AutoStart 0x%x", pConfig->dwStartType);
176 if (pConfig->dwStartType < SERVICE_DEMAND_START)
182 CloseServiceHandle(svc);
184 CloseServiceHandle(scm);
190 cm_DaemonCheckInit(void)
197 code = RegOpenKeyEx(HKEY_LOCAL_MACHINE, AFSREG_CLT_SVC_PARAM_SUBKEY,
198 0, KEY_QUERY_VALUE, &parmKey);
202 dummyLen = sizeof(DWORD);
203 code = RegQueryValueEx(parmKey, "DownServerCheckInterval", NULL, NULL,
204 (BYTE *) &dummy, &dummyLen);
205 if (code == ERROR_SUCCESS)
206 cm_daemonCheckDownInterval = dummy;
208 dummyLen = sizeof(DWORD);
209 code = RegQueryValueEx(parmKey, "UpServerCheckInterval", NULL, NULL,
210 (BYTE *) &dummy, &dummyLen);
211 if (code == ERROR_SUCCESS)
212 cm_daemonCheckUpInterval = dummy;
214 dummyLen = sizeof(DWORD);
215 code = RegQueryValueEx(parmKey, "VolumeCheckInterval", NULL, NULL,
216 (BYTE *) &dummy, &dummyLen);
217 if (code == ERROR_SUCCESS)
218 cm_daemonCheckVolInterval = dummy;
220 dummyLen = sizeof(DWORD);
221 code = RegQueryValueEx(parmKey, "CallbackCheckInterval", NULL, NULL,
222 (BYTE *) &dummy, &dummyLen);
223 if (code == ERROR_SUCCESS)
224 cm_daemonCheckCBInterval = dummy;
226 dummyLen = sizeof(DWORD);
227 code = RegQueryValueEx(parmKey, "LockCheckInterval", NULL, NULL,
228 (BYTE *) &dummy, &dummyLen);
229 if (code == ERROR_SUCCESS)
230 cm_daemonCheckLockInterval = dummy;
232 dummyLen = sizeof(DWORD);
233 code = RegQueryValueEx(parmKey, "TokenCheckInterval", NULL, NULL,
234 (BYTE *) &dummy, &dummyLen);
235 if (code == ERROR_SUCCESS)
236 cm_daemonTokenCheckInterval = dummy;
238 RegCloseKey(parmKey);
241 /* periodic check daemon */
242 void cm_Daemon(long parm)
245 time_t lastLockCheck;
247 time_t lastCBExpirationCheck;
248 time_t lastDownServerCheck;
249 time_t lastUpServerCheck;
250 time_t lastTokenCacheCheck;
255 int configureFirewall = IsWindowsFirewallPresent();
257 if (!configureFirewall) {
258 afsi_log("No Windows Firewall detected");
261 /* ping all file servers, up or down, with unauthenticated connection,
262 * to find out whether we have all our callbacks from the server still.
263 * Also, ping down VLDBs.
266 * Seed the random number generator with our own address, so that
267 * clients starting at the same time don't all do vol checks at the
270 gethostname(thostName, sizeof(thostName));
271 thp = gethostbyname(thostName);
272 if (thp == NULL) /* In djgpp, gethostname returns the netbios
273 name of the machine. gethostbyname will fail
274 looking this up if it differs from DNS name. */
277 memcpy(&code, thp->h_addr_list[0], 4);
281 cm_DaemonCheckInit();
284 lastVolCheck = now - cm_daemonCheckVolInterval/2 + (rand() % cm_daemonCheckVolInterval);
285 lastCBExpirationCheck = now - cm_daemonCheckCBInterval/2 + (rand() % cm_daemonCheckCBInterval);
286 lastLockCheck = now - cm_daemonCheckLockInterval/2 + (rand() % cm_daemonCheckLockInterval);
287 lastDownServerCheck = now - cm_daemonCheckDownInterval/2 + (rand() % cm_daemonCheckDownInterval);
288 lastUpServerCheck = now - cm_daemonCheckUpInterval/2 + (rand() % cm_daemonCheckUpInterval);
289 lastTokenCacheCheck = now - cm_daemonTokenCheckInterval/2 + (rand() % cm_daemonTokenCheckInterval);
291 while (daemon_ShutdownFlag == 0) {
292 if (configureFirewall) {
293 /* Open Microsoft Firewall to allow in port 7001 */
294 switch (icf_CheckAndAddAFSPorts(AFS_PORTSET_CLIENT)) {
296 afsi_log("Windows Firewall Configuration succeeded");
297 configureFirewall = 0;
300 afsi_log("Invalid Windows Firewall Port Set");
303 afsi_log("Unable to open Windows Firewall Profile");
306 afsi_log("Unable to create/modify Windows Firewall Port entries");
309 afsi_log("Unknown Windows Firewall Configuration error");
313 /* find out what time it is */
316 /* check down servers */
317 if (now > lastDownServerCheck + cm_daemonCheckDownInterval) {
318 lastDownServerCheck = now;
319 osi_Log0(afsd_logp, "cm_Daemon CheckDownServers");
320 cm_CheckServers(CM_FLAG_CHECKDOWNSERVERS, NULL);
324 /* check up servers */
325 if (now > lastUpServerCheck + cm_daemonCheckUpInterval) {
326 lastUpServerCheck = now;
327 osi_Log0(afsd_logp, "cm_Daemon CheckUpServers");
328 cm_CheckServers(CM_FLAG_CHECKUPSERVERS, NULL);
332 if (now > lastVolCheck + cm_daemonCheckVolInterval) {
338 if (now > lastCBExpirationCheck + cm_daemonCheckCBInterval) {
339 lastCBExpirationCheck = now;
340 cm_CheckCBExpiration();
344 if (now > lastLockCheck + cm_daemonCheckLockInterval) {
350 if (now > lastTokenCacheCheck + cm_daemonTokenCheckInterval) {
351 lastTokenCacheCheck = now;
352 cm_CheckTokenCache(now);
356 /* allow an exit to be called prior to stopping the service */
357 hHookDll = LoadLibrary(AFSD_HOOK_DLL);
361 AfsdDaemonHook daemonHook = ( AfsdDaemonHook ) GetProcAddress(hHookDll, AFSD_DAEMON_HOOK);
364 hookRc = daemonHook();
366 FreeLibrary(hHookDll);
371 SetEvent(WaitToTerminate);
375 thrd_Sleep(30 * 1000); /* sleep 30 seconds */
376 if (daemon_ShutdownFlag == 1)
381 void cm_DaemonShutdown(void)
383 daemon_ShutdownFlag = 1;
386 void cm_InitDaemon(int nDaemons)
388 static osi_once_t once;
393 if (osi_Once(&once)) {
394 lock_InitializeRWLock(&cm_daemonLock, "cm_daemonLock");
397 /* creating IP Address Change monitor daemon */
398 phandle = thrd_Create((SecurityAttrib) 0, 0,
399 (ThreadFunc) cm_IpAddrDaemon, 0, 0, &pid, "cm_IpAddrDaemon");
400 osi_assert(phandle != NULL);
401 thrd_CloseHandle(phandle);
403 /* creating pinging daemon */
404 phandle = thrd_Create((SecurityAttrib) 0, 0,
405 (ThreadFunc) cm_Daemon, 0, 0, &pid, "cm_Daemon");
406 osi_assert(phandle != NULL);
407 thrd_CloseHandle(phandle);
409 for(i=0; i < nDaemons; i++) {
410 phandle = thrd_Create((SecurityAttrib) 0, 0,
411 (ThreadFunc) cm_BkgDaemon, 0, 0, &pid,
413 osi_assert(phandle != NULL);
414 thrd_CloseHandle(phandle);