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>
24 #include <rx/rx_prototypes.h>
29 long cm_daemonCheckInterval = 30;
30 long cm_daemonTokenCheckInterval = 180;
32 osi_rwlock_t cm_daemonLock;
34 long cm_bkgQueueCount; /* # of queued requests */
36 int cm_bkgWaitingForCount; /* true if someone's waiting for cm_bkgQueueCount to drop */
38 cm_bkgRequest_t *cm_bkgListp; /* first elt in the list of requests */
39 cm_bkgRequest_t *cm_bkgListEndp; /* last elt in the list of requests */
41 static int daemon_ShutdownFlag = 0;
43 void cm_BkgDaemon(long parm)
47 rx_StartClientThread();
49 lock_ObtainWrite(&cm_daemonLock);
50 while (daemon_ShutdownFlag == 0) {
51 if (!cm_bkgListEndp) {
52 osi_SleepW((long) &cm_bkgListp, &cm_daemonLock);
53 lock_ObtainWrite(&cm_daemonLock);
57 /* we found a request */
59 cm_bkgListEndp = (cm_bkgRequest_t *) osi_QPrev(&rp->q);
60 osi_QRemove((osi_queue_t **) &cm_bkgListp, &rp->q);
61 osi_assert(cm_bkgQueueCount-- > 0);
62 lock_ReleaseWrite(&cm_daemonLock);
64 (*rp->procp)(rp->scp, rp->p1, rp->p2, rp->p3, rp->p4, rp->userp);
66 cm_ReleaseUser(rp->userp);
67 cm_ReleaseSCache(rp->scp);
70 lock_ObtainWrite(&cm_daemonLock);
72 lock_ReleaseWrite(&cm_daemonLock);
75 void cm_QueueBKGRequest(cm_scache_t *scp, cm_bkgProc_t *procp, long p1, long p2, long p3, long p4,
80 rp = malloc(sizeof(*rp));
81 memset(rp, 0, sizeof(*rp));
93 lock_ObtainWrite(&cm_daemonLock);
95 osi_QAdd((osi_queue_t **) &cm_bkgListp, &rp->q);
98 lock_ReleaseWrite(&cm_daemonLock);
100 osi_Wakeup((long) &cm_bkgListp);
104 IsWindowsFirewallPresent(void)
109 BOOLEAN result = FALSE;
110 LPQUERY_SERVICE_CONFIG pConfig = NULL;
114 /* Open services manager */
115 scm = OpenSCManager(NULL, NULL, GENERIC_READ);
116 if (!scm) return FALSE;
118 /* Open Windows Firewall service */
119 svc = OpenService(scm, "SharedAccess", SERVICE_QUERY_CONFIG);
123 /* Query Windows Firewall service config, first just to get buffer size */
124 /* Expected to fail, so don't test return value */
125 (void) QueryServiceConfig(svc, NULL, 0, &BufSize);
126 status = GetLastError();
127 if (status != ERROR_INSUFFICIENT_BUFFER)
130 /* Allocate buffer */
131 pConfig = (LPQUERY_SERVICE_CONFIG)GlobalAlloc(GMEM_FIXED,BufSize);
135 /* Query Windows Firewall service config, this time for real */
136 flag = QueryServiceConfig(svc, pConfig, BufSize, &BufSize);
140 /* Is it autostart? */
141 if (pConfig->dwStartType < SERVICE_DEMAND_START)
147 CloseServiceHandle(svc);
149 CloseServiceHandle(scm);
154 /* periodic check daemon */
155 void cm_Daemon(long parm)
158 unsigned long lastLockCheck;
159 unsigned long lastVolCheck;
160 unsigned long lastCBExpirationCheck;
161 unsigned long lastDownServerCheck;
162 unsigned long lastUpServerCheck;
163 unsigned long lastTokenCacheCheck;
168 int configureFirewall = IsWindowsFirewallPresent();
170 /* ping all file servers, up or down, with unauthenticated connection,
171 * to find out whether we have all our callbacks from the server still.
172 * Also, ping down VLDBs.
175 * Seed the random number generator with our own address, so that
176 * clients starting at the same time don't all do vol checks at the
179 gethostname(thostName, sizeof(thostName));
180 thp = gethostbyname(thostName);
181 if (thp == NULL) /* In djgpp, gethostname returns the netbios
182 name of the machine. gethostbyname will fail
183 looking this up if it differs from DNS name. */
186 memcpy(&code, thp->h_addr_list[0], 4);
190 lastVolCheck = now - 1800 + (rand() % 3600);
191 lastCBExpirationCheck = now - 60 + (rand() % 60);
192 lastLockCheck = now - 60 + (rand() % 60);
193 lastDownServerCheck = now - cm_daemonCheckInterval/2 + (rand() % cm_daemonCheckInterval);
194 lastUpServerCheck = now - 1800 + (rand() % 3600);
195 lastTokenCacheCheck = now - cm_daemonTokenCheckInterval/2 + (rand() % cm_daemonTokenCheckInterval);
197 while (daemon_ShutdownFlag == 0) {
198 thrd_Sleep(30 * 1000); /* sleep 30 seconds */
199 if (daemon_ShutdownFlag == 1)
202 if (configureFirewall) {
203 /* Open Microsoft Firewall to allow in port 7001 */
204 switch (icf_CheckAndAddAFSPorts(AFS_PORTSET_CLIENT)) {
206 afsi_log("Windows Firewall Configuration succeeded");
207 configureFirewall = 0;
210 afsi_log("Invalid Windows Firewall Port Set");
213 afsi_log("Unable to open Windows Firewall Profile");
216 afsi_log("Unable to create/modify Windows Firewall Port entries");
219 afsi_log("Unknown Windows Firewall Configuration error");
223 /* find out what time it is */
226 /* check down servers */
227 if (now > lastDownServerCheck + cm_daemonCheckInterval) {
228 lastDownServerCheck = now;
229 cm_CheckServers(CM_FLAG_CHECKDOWNSERVERS, NULL);
232 /* check up servers */
233 if (now > lastUpServerCheck + 3600) {
234 lastUpServerCheck = now;
235 cm_CheckServers(CM_FLAG_CHECKUPSERVERS, NULL);
238 if (now > lastVolCheck + 3600) {
243 if (now > lastCBExpirationCheck + 60) {
244 lastCBExpirationCheck = now;
245 cm_CheckCBExpiration();
248 if (now > lastLockCheck + 60) {
253 if (now > lastTokenCacheCheck + cm_daemonTokenCheckInterval) {
254 lastTokenCacheCheck = now;
255 cm_CheckTokenCache(now);
258 /* allow an exit to be called prior to stopping the service */
259 hHookDll = LoadLibrary(AFSD_HOOK_DLL);
263 AfsdDaemonHook daemonHook = ( AfsdDaemonHook ) GetProcAddress(hHookDll, AFSD_DAEMON_HOOK);
266 hookRc = daemonHook();
268 FreeLibrary(hHookDll);
273 SetEvent(WaitToTerminate);
279 void cm_DaemonShutdown(void)
281 daemon_ShutdownFlag = 1;
284 void cm_InitDaemon(int nDaemons)
286 static osi_once_t once;
291 if (osi_Once(&once)) {
292 lock_InitializeRWLock(&cm_daemonLock, "cm_daemonLock");
295 /* creating pinging daemon */
296 phandle = thrd_Create((SecurityAttrib) 0, 0,
297 (ThreadFunc) cm_Daemon, 0, 0, &pid, "cm_Daemon");
298 osi_assert(phandle != NULL);
300 thrd_CloseHandle(phandle);
301 for(i=0; i < nDaemons; i++) {
302 phandle = thrd_Create((SecurityAttrib) 0, 0,
303 (ThreadFunc) cm_BkgDaemon, 0, 0, &pid,
305 osi_assert(phandle != NULL);
306 thrd_CloseHandle(phandle);