2 * Copyright (C) 1998, 1989 Transarc Corporation - All rights reserved
4 * (C) COPYRIGHT IBM CORPORATION 1987, 1988
5 * LICENSED MATERIALS - PROPERTY OF IBM
10 #include <afs/param.h>
23 long cm_daemonCheckInterval = 30;
24 long cm_daemonTokenCheckInterval = 180;
26 osi_rwlock_t cm_daemonLock;
28 long cm_bkgQueueCount; /* # of queued requests */
30 int cm_bkgWaitingForCount; /* true if someone's waiting for cm_bkgQueueCount to drop */
32 cm_bkgRequest_t *cm_bkgListp; /* first elt in the list of requests */
33 cm_bkgRequest_t *cm_bkgListEndp; /* last elt in the list of requests */
35 void cm_BkgDaemon(long parm)
39 lock_ObtainWrite(&cm_daemonLock);
41 if (!cm_bkgListEndp) {
42 osi_SleepW((long) &cm_bkgListp, &cm_daemonLock);
43 lock_ObtainWrite(&cm_daemonLock);
47 /* we found a request */
49 cm_bkgListEndp = (cm_bkgRequest_t *) osi_QPrev(&rp->q);
50 osi_QRemove((osi_queue_t **) &cm_bkgListp, &rp->q);
51 osi_assert(cm_bkgQueueCount-- > 0);
52 lock_ReleaseWrite(&cm_daemonLock);
54 (*rp->procp)(rp->scp, rp->p1, rp->p2, rp->p3, rp->p4, rp->userp);
56 cm_ReleaseUser(rp->userp);
57 cm_ReleaseSCache(rp->scp);
60 lock_ObtainWrite(&cm_daemonLock);
64 void cm_QueueBKGRequest(cm_scache_t *scp, cm_bkgProc_t *procp, long p1, long p2, long p3, long p4,
69 rp = malloc(sizeof(*rp));
70 memset(rp, 0, sizeof(*rp));
82 lock_ObtainWrite(&cm_daemonLock);
84 osi_QAdd((osi_queue_t **) &cm_bkgListp, &rp->q);
85 if (!cm_bkgListEndp) cm_bkgListEndp = rp;
86 lock_ReleaseWrite(&cm_daemonLock);
88 osi_Wakeup((long) &cm_bkgListp);
91 /* periodic check daemon */
92 void cm_Daemon(long parm)
97 long lastCBExpirationCheck;
98 long lastDownServerCheck;
99 long lastUpServerCheck;
100 long lastTokenCacheCheck;
105 /* ping all file servers, up or down, with unauthenticated connection,
106 * to find out whether we have all our callbacks from the server still.
107 * Also, ping down VLDBs.
110 * Seed the random number generator with our own address, so that
111 * clients starting at the same time don't all do vol checks at the
114 gethostname(thostName, sizeof(thostName));
115 thp = gethostbyname(thostName);
116 memcpy(&code, thp->h_addr_list[0], 4);
120 lastVolCheck = now - 1800 + (rand() % 3600);
121 lastCBExpirationCheck = now - 60 + (rand() % 60);
122 lastLockCheck = now - 60 + (rand() % 60);
123 lastDownServerCheck = now - cm_daemonCheckInterval/2 + (rand() % cm_daemonCheckInterval);
124 lastUpServerCheck = now - 1800 + (rand() % 3600);
125 lastTokenCacheCheck = now - cm_daemonTokenCheckInterval/2 + (rand() % cm_daemonTokenCheckInterval);
128 Sleep(30 * 1000); /* sleep 30 seconds */
130 /* find out what time it is */
133 /* check down servers */
134 if (now > lastDownServerCheck + cm_daemonCheckInterval) {
135 lastDownServerCheck = now;
136 cm_CheckServers(CM_FLAG_CHECKDOWNSERVERS, NULL);
139 /* check up servers */
140 if (now > lastUpServerCheck + 3600) {
141 lastUpServerCheck = now;
142 cm_CheckServers(CM_FLAG_CHECKUPSERVERS, NULL);
145 if (now > lastVolCheck + 3600) {
150 if (now > lastCBExpirationCheck + 60) {
151 lastCBExpirationCheck = now;
152 cm_CheckCBExpiration();
155 if (now > lastLockCheck + 60) {
160 if (now > lastTokenCacheCheck + cm_daemonTokenCheckInterval) {
161 lastTokenCacheCheck = now;
162 cm_CheckTokenCache(now);
167 void cm_InitDaemon(int nDaemons)
169 static osi_once_t once;
174 if (osi_Once(&once)) {
175 lock_InitializeRWLock(&cm_daemonLock, "cm_daemonLock");
178 /* creating pinging daemon */
179 phandle = CreateThread((SECURITY_ATTRIBUTES *) 0, 0,
180 (LPTHREAD_START_ROUTINE) cm_Daemon, 0, 0, &pid);
181 osi_assert(phandle != NULL);
183 CloseHandle(phandle);
184 for(i=0; i < nDaemons; i++) {
185 phandle = CreateThread((SECURITY_ATTRIBUTES *) 0, 0,
186 (LPTHREAD_START_ROUTINE) cm_BkgDaemon, 0, 0, &pid);
187 osi_assert(phandle != NULL);
188 CloseHandle(phandle);