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 long cm_daemonCheckInterval = 30;
28 long cm_daemonTokenCheckInterval = 180;
30 osi_rwlock_t cm_daemonLock;
32 long cm_bkgQueueCount; /* # of queued requests */
34 int cm_bkgWaitingForCount; /* true if someone's waiting for cm_bkgQueueCount to drop */
36 cm_bkgRequest_t *cm_bkgListp; /* first elt in the list of requests */
37 cm_bkgRequest_t *cm_bkgListEndp; /* last elt in the list of requests */
39 void cm_BkgDaemon(long parm)
43 lock_ObtainWrite(&cm_daemonLock);
45 if (!cm_bkgListEndp) {
46 osi_SleepW((long) &cm_bkgListp, &cm_daemonLock);
47 lock_ObtainWrite(&cm_daemonLock);
51 /* we found a request */
53 cm_bkgListEndp = (cm_bkgRequest_t *) osi_QPrev(&rp->q);
54 osi_QRemove((osi_queue_t **) &cm_bkgListp, &rp->q);
55 osi_assert(cm_bkgQueueCount-- > 0);
56 lock_ReleaseWrite(&cm_daemonLock);
58 (*rp->procp)(rp->scp, rp->p1, rp->p2, rp->p3, rp->p4, rp->userp);
60 cm_ReleaseUser(rp->userp);
61 cm_ReleaseSCache(rp->scp);
64 lock_ObtainWrite(&cm_daemonLock);
68 void cm_QueueBKGRequest(cm_scache_t *scp, cm_bkgProc_t *procp, long p1, long p2, long p3, long p4,
73 rp = malloc(sizeof(*rp));
74 memset(rp, 0, sizeof(*rp));
86 lock_ObtainWrite(&cm_daemonLock);
88 osi_QAdd((osi_queue_t **) &cm_bkgListp, &rp->q);
89 if (!cm_bkgListEndp) cm_bkgListEndp = rp;
90 lock_ReleaseWrite(&cm_daemonLock);
92 osi_Wakeup((long) &cm_bkgListp);
95 /* periodic check daemon */
96 void cm_Daemon(long parm)
99 unsigned long lastLockCheck;
100 unsigned long lastVolCheck;
101 unsigned long lastCBExpirationCheck;
102 unsigned long lastDownServerCheck;
103 unsigned long lastUpServerCheck;
104 unsigned long lastTokenCacheCheck;
109 /* ping all file servers, up or down, with unauthenticated connection,
110 * to find out whether we have all our callbacks from the server still.
111 * Also, ping down VLDBs.
114 * Seed the random number generator with our own address, so that
115 * clients starting at the same time don't all do vol checks at the
118 gethostname(thostName, sizeof(thostName));
119 thp = gethostbyname(thostName);
120 if (thp == NULL) /* In djgpp, gethostname returns the netbios
121 name of the machine. gethostbyname will fail
122 looking this up if it differs from DNS name. */
125 memcpy(&code, thp->h_addr_list[0], 4);
129 lastVolCheck = now - 1800 + (rand() % 3600);
130 lastCBExpirationCheck = now - 60 + (rand() % 60);
131 lastLockCheck = now - 60 + (rand() % 60);
132 lastDownServerCheck = now - cm_daemonCheckInterval/2 + (rand() % cm_daemonCheckInterval);
133 lastUpServerCheck = now - 1800 + (rand() % 3600);
134 lastTokenCacheCheck = now - cm_daemonTokenCheckInterval/2 + (rand() % cm_daemonTokenCheckInterval);
137 thrd_Sleep(30 * 1000); /* sleep 30 seconds */
139 /* find out what time it is */
142 /* check down servers */
143 if (now > lastDownServerCheck + cm_daemonCheckInterval) {
144 lastDownServerCheck = now;
145 cm_CheckServers(CM_FLAG_CHECKDOWNSERVERS, NULL);
148 /* check up servers */
149 if (now > lastUpServerCheck + 3600) {
150 lastUpServerCheck = now;
151 cm_CheckServers(CM_FLAG_CHECKUPSERVERS, NULL);
154 if (now > lastVolCheck + 3600) {
159 if (now > lastCBExpirationCheck + 60) {
160 lastCBExpirationCheck = now;
161 cm_CheckCBExpiration();
164 if (now > lastLockCheck + 60) {
169 if (now > lastTokenCacheCheck + cm_daemonTokenCheckInterval) {
170 lastTokenCacheCheck = now;
171 cm_CheckTokenCache(now);
176 void cm_InitDaemon(int nDaemons)
178 static osi_once_t once;
183 if (osi_Once(&once)) {
184 lock_InitializeRWLock(&cm_daemonLock, "cm_daemonLock");
187 /* creating pinging daemon */
188 phandle = thrd_Create((SecurityAttrib) 0, 0,
189 (ThreadFunc) cm_Daemon, 0, 0, &pid, "cm_Daemon");
190 osi_assert(phandle != NULL);
192 thrd_CloseHandle(phandle);
193 for(i=0; i < nDaemons; i++) {
194 phandle = thrd_Create((SecurityAttrib) 0, 0,
195 (ThreadFunc) cm_BkgDaemon, 0, 0, &pid,
197 osi_assert(phandle != NULL);
198 thrd_CloseHandle(phandle);