f708b8bc2f01343d2a55ad4dda9b3a9b203c3e4a
[openafs.git] / src / WINNT / afsd / cm_daemon.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
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
8  */
9
10 #include <afs/param.h>
11 #include <afs/stds.h>
12
13 #include <windows.h>
14 #include <winsock2.h>
15 #include <iphlpapi.h>
16 #include <stdlib.h>
17 #include <malloc.h>
18 #include <string.h>
19
20 #include "afsd.h"
21
22 #include <rx/rx.h>
23 #include <rx/rx_prototypes.h>
24 #include <WINNT/afsreg.h>
25
26 #include "afsicf.h"
27
28 /* in seconds */
29 long cm_daemonCheckDownInterval  = 180;
30 long cm_daemonCheckUpInterval    = 240;
31 long cm_daemonCheckVolInterval   = 3600;
32 long cm_daemonCheckCBInterval    = 60;
33 long cm_daemonCheckVolCBInterval = 0;
34 long cm_daemonCheckLockInterval  = 60;
35 long cm_daemonTokenCheckInterval = 180;
36 long cm_daemonCheckOfflineVolInterval = 600;
37
38 osi_rwlock_t cm_daemonLock;
39
40 long cm_bkgQueueCount;          /* # of queued requests */
41
42 int cm_bkgWaitingForCount;      /* true if someone's waiting for cm_bkgQueueCount to drop */
43
44 cm_bkgRequest_t *cm_bkgListp;           /* first elt in the list of requests */
45 cm_bkgRequest_t *cm_bkgListEndp;        /* last elt in the list of requests */
46
47 static int daemon_ShutdownFlag = 0;
48 static int cm_nDaemons = 0;
49
50 static EVENT_HANDLE cm_Daemon_ShutdownEvent = NULL;
51 static EVENT_HANDLE cm_IPAddrDaemon_ShutdownEvent = NULL;
52 static EVENT_HANDLE cm_BkgDaemon_ShutdownEvent[CM_MAX_DAEMONS] = 
53        {NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL};
54
55 void cm_IpAddrDaemon(long parm)
56 {
57     extern void smb_CheckVCs(void);
58     char * name = "cm_IPAddrDaemon_ShutdownEvent";
59
60     cm_IPAddrDaemon_ShutdownEvent = thrd_CreateEvent(NULL, FALSE, FALSE, name);
61     if ( GetLastError() == ERROR_ALREADY_EXISTS )
62         afsi_log("Event Object Already Exists: %s", name);
63
64     rx_StartClientThread();
65
66     while (daemon_ShutdownFlag == 0) {
67         DWORD Result;
68         
69         thrd_SetEvent(cm_IPAddrDaemon_ShutdownEvent);
70         Result = NotifyAddrChange(NULL,NULL);
71         if (Result == NO_ERROR && daemon_ShutdownFlag == 0) {
72             thrd_ResetEvent(cm_IPAddrDaemon_ShutdownEvent);
73             Sleep(2500);
74             if (daemon_ShutdownFlag == 0) {
75                 osi_Log0(afsd_logp, "cm_IpAddrDaemon CheckDownServers");
76                 cm_CheckServers(CM_FLAG_CHECKVLDBSERVERS | CM_FLAG_CHECKUPSERVERS | CM_FLAG_CHECKDOWNSERVERS, NULL);
77                 cm_ForceNewConnectionsAllServers();
78                 cm_CheckServers(CM_FLAG_CHECKFILESERVERS | CM_FLAG_CHECKUPSERVERS | CM_FLAG_CHECKDOWNSERVERS, NULL);
79                 smb_CheckVCs();
80                 cm_VolStatus_Network_Addr_Change();
81             }
82         }       
83     }
84
85     thrd_SetEvent(cm_IPAddrDaemon_ShutdownEvent);
86 }
87
88 void cm_BkgDaemon(void * parm)
89 {
90     cm_bkgRequest_t *rp;
91     afs_int32 code;
92     char name[32] = "";
93     long daemonID = (long)parm;
94
95     snprintf(name, sizeof(name), "cm_BkgDaemon_ShutdownEvent%d", daemonID);
96
97     cm_BkgDaemon_ShutdownEvent[daemonID] = thrd_CreateEvent(NULL, FALSE, FALSE, name);
98     if ( GetLastError() == ERROR_ALREADY_EXISTS )
99         afsi_log("Event Object Already Exists: %s", name);
100
101     rx_StartClientThread();
102
103     lock_ObtainWrite(&cm_daemonLock);
104     while (daemon_ShutdownFlag == 0) {
105         if (!cm_bkgListEndp) {
106             osi_SleepW((LONG_PTR)&cm_bkgListp, &cm_daemonLock);
107             lock_ObtainWrite(&cm_daemonLock);
108             continue;
109         }
110                 
111         /* we found a request */
112         for (rp = cm_bkgListEndp; rp; rp = (cm_bkgRequest_t *) osi_QPrev(&rp->q))
113         {
114             if (cm_ServerAvailable(&rp->scp->fid, rp->userp))
115                 break;
116         }
117         if (rp == NULL) {
118             /* we couldn't find a request that we could process at the current time */
119             lock_ReleaseWrite(&cm_daemonLock);
120             Sleep(1000);
121             lock_ObtainWrite(&cm_daemonLock);
122             continue;
123         }
124
125         osi_QRemoveHT((osi_queue_t **) &cm_bkgListp, (osi_queue_t **) &cm_bkgListEndp, &rp->q);
126         osi_assertx(cm_bkgQueueCount-- > 0, "cm_bkgQueueCount 0");
127         lock_ReleaseWrite(&cm_daemonLock);
128
129         osi_Log1(afsd_logp,"cm_BkgDaemon processing request 0x%p", rp);
130
131 #ifdef DEBUG_REFCOUNT
132         osi_Log2(afsd_logp,"cm_BkgDaemon (before) scp 0x%x ref %d",rp->scp, rp->scp->refCount);
133 #endif
134         code = (*rp->procp)(rp->scp, rp->p1, rp->p2, rp->p3, rp->p4, rp->userp);
135 #ifdef DEBUG_REFCOUNT                
136         osi_Log2(afsd_logp,"cm_BkgDaemon (after) scp 0x%x ref %d",rp->scp, rp->scp->refCount);
137 #endif
138         if (code == 0) {
139             cm_ReleaseUser(rp->userp);
140             cm_ReleaseSCache(rp->scp);
141             free(rp);
142         }
143
144         lock_ObtainWrite(&cm_daemonLock);
145
146         switch ( code ) {
147         case 0: /* success */
148             osi_Log1(afsd_logp,"cm_BkgDaemon SUCCESS: request 0x%p", rp);
149             break;
150         case CM_ERROR_TIMEDOUT: /* or server restarting */
151         case CM_ERROR_RETRY:
152         case CM_ERROR_WOULDBLOCK:
153         case CM_ERROR_ALLBUSY:
154         case CM_ERROR_ALLDOWN:
155         case CM_ERROR_ALLOFFLINE:
156         case CM_ERROR_PARTIALWRITE:
157             osi_Log2(afsd_logp,"cm_BkgDaemon re-queueing failed request 0x%p code 0x%x",
158                      rp, code);
159             cm_bkgQueueCount++;
160             osi_QAddT((osi_queue_t **) &cm_bkgListp, (osi_queue_t **)&cm_bkgListEndp, &rp->q);
161             break;
162         default:
163             osi_Log2(afsd_logp,"cm_BkgDaemon FAILED: request dropped 0x%p code 0x%x",
164                      rp, code);
165         }
166     }
167     lock_ReleaseWrite(&cm_daemonLock);
168
169     thrd_SetEvent(cm_BkgDaemon_ShutdownEvent[daemonID]);
170
171 }
172
173 void cm_QueueBKGRequest(cm_scache_t *scp, cm_bkgProc_t *procp, afs_uint32 p1, afs_uint32 p2, afs_uint32 p3, afs_uint32 p4,
174         cm_user_t *userp)
175 {
176     cm_bkgRequest_t *rp;
177         
178     rp = malloc(sizeof(*rp));
179     memset(rp, 0, sizeof(*rp));
180         
181     cm_HoldSCache(scp);
182     rp->scp = scp;
183     cm_HoldUser(userp);
184     rp->userp = userp;
185     rp->procp = procp;
186     rp->p1 = p1;
187     rp->p2 = p2;
188     rp->p3 = p3;
189     rp->p4 = p4;
190
191     lock_ObtainWrite(&cm_daemonLock);
192     cm_bkgQueueCount++;
193     osi_QAdd((osi_queue_t **) &cm_bkgListp, &rp->q);
194     if (!cm_bkgListEndp) 
195         cm_bkgListEndp = rp;
196     lock_ReleaseWrite(&cm_daemonLock);
197
198     osi_Wakeup((LONG_PTR) &cm_bkgListp);
199 }
200
201 static int
202 IsWindowsFirewallPresent(void)
203 {
204     SC_HANDLE scm;
205     SC_HANDLE svc;
206     BOOLEAN flag;
207     BOOLEAN result = FALSE;
208     LPQUERY_SERVICE_CONFIG pConfig = NULL;
209     DWORD BufSize;
210     LONG status;
211
212     /* Open services manager */
213     scm = OpenSCManager(NULL, NULL, GENERIC_READ);
214     if (!scm) return FALSE;
215
216     /* Open Windows Firewall service */
217     svc = OpenService(scm, "MpsSvc", SERVICE_QUERY_CONFIG);
218     if (!svc) {
219         afsi_log("MpsSvc Service could not be opened for query: 0x%x", GetLastError());
220         svc = OpenService(scm, "SharedAccess", SERVICE_QUERY_CONFIG);
221         if (!svc)
222             afsi_log("SharedAccess Service could not be opened for query: 0x%x", GetLastError());
223     }
224     if (!svc)
225         goto close_scm;
226
227     /* Query Windows Firewall service config, first just to get buffer size */
228     /* Expected to fail, so don't test return value */
229     (void) QueryServiceConfig(svc, NULL, 0, &BufSize);
230     status = GetLastError();
231     if (status != ERROR_INSUFFICIENT_BUFFER)
232         goto close_svc;
233
234     /* Allocate buffer */
235     pConfig = (LPQUERY_SERVICE_CONFIG)GlobalAlloc(GMEM_FIXED,BufSize);
236     if (!pConfig)
237         goto close_svc;
238
239     /* Query Windows Firewall service config, this time for real */
240     flag = QueryServiceConfig(svc, pConfig, BufSize, &BufSize);
241     if (!flag) {
242         afsi_log("QueryServiceConfig failed: 0x%x", GetLastError());
243         goto free_pConfig;
244     }
245
246     /* Is it autostart? */
247     afsi_log("AutoStart 0x%x", pConfig->dwStartType);
248     if (pConfig->dwStartType < SERVICE_DEMAND_START)
249         result = TRUE;
250
251   free_pConfig:
252     GlobalFree(pConfig);
253   close_svc:
254     CloseServiceHandle(svc);
255   close_scm:
256     CloseServiceHandle(scm);
257
258     return result;
259 }
260
261 void
262 cm_DaemonCheckInit(void)
263 {
264     HKEY parmKey;
265     DWORD dummyLen;
266     DWORD dummy;
267     DWORD code;
268
269     code = RegOpenKeyEx(HKEY_LOCAL_MACHINE, AFSREG_CLT_SVC_PARAM_SUBKEY,
270                          0, KEY_QUERY_VALUE, &parmKey);
271     if (code)
272         return;
273
274     dummyLen = sizeof(DWORD);
275     code = RegQueryValueEx(parmKey, "daemonCheckDownInterval", NULL, NULL,
276                             (BYTE *) &dummy, &dummyLen);
277     if (code == ERROR_SUCCESS)
278         cm_daemonCheckDownInterval = dummy;
279     afsi_log("daemonCheckDownInterval is %d", cm_daemonCheckDownInterval);
280
281     dummyLen = sizeof(DWORD);
282     code = RegQueryValueEx(parmKey, "daemonCheckUpInterval", NULL, NULL,
283                             (BYTE *) &dummy, &dummyLen);
284     if (code == ERROR_SUCCESS)
285         cm_daemonCheckUpInterval = dummy;
286     afsi_log("daemonCheckUpInterval is %d", cm_daemonCheckUpInterval);
287
288     dummyLen = sizeof(DWORD);
289     code = RegQueryValueEx(parmKey, "daemonCheckVolInterval", NULL, NULL,
290                             (BYTE *) &dummy, &dummyLen);
291     if (code == ERROR_SUCCESS)
292         cm_daemonCheckVolInterval = dummy;
293     afsi_log("daemonCheckVolInterval is %d", cm_daemonCheckVolInterval);
294
295     dummyLen = sizeof(DWORD);
296     code = RegQueryValueEx(parmKey, "daemonCheckCBInterval", NULL, NULL,
297                             (BYTE *) &dummy, &dummyLen);
298     if (code == ERROR_SUCCESS)
299         cm_daemonCheckCBInterval = dummy;
300     afsi_log("daemonCheckCBInterval is %d", cm_daemonCheckCBInterval);
301
302     dummyLen = sizeof(DWORD);
303     code = RegQueryValueEx(parmKey, "daemonCheckVolCBInterval", NULL, NULL,
304                             (BYTE *) &dummy, &dummyLen);
305     if (code == ERROR_SUCCESS)
306         cm_daemonCheckVolCBInterval = dummy;
307     afsi_log("daemonCheckVolCBInterval is %d", cm_daemonCheckVolCBInterval);
308
309     dummyLen = sizeof(DWORD);
310     code = RegQueryValueEx(parmKey, "daemonCheckLockInterval", NULL, NULL,
311                             (BYTE *) &dummy, &dummyLen);
312     if (code == ERROR_SUCCESS)
313         cm_daemonCheckLockInterval = dummy;
314     afsi_log("daemonCheckLockInterval is %d", cm_daemonCheckLockInterval);
315
316     dummyLen = sizeof(DWORD);
317     code = RegQueryValueEx(parmKey, "daemonCheckTokenInterval", NULL, NULL,
318                             (BYTE *) &dummy, &dummyLen);
319     if (code == ERROR_SUCCESS)
320         cm_daemonTokenCheckInterval = dummy;
321     afsi_log("daemonCheckTokenInterval is %d", cm_daemonTokenCheckInterval);
322
323     dummyLen = sizeof(DWORD);
324     code = RegQueryValueEx(parmKey, "daemonCheckOfflineVolInterval", NULL, NULL,
325                             (BYTE *) &dummy, &dummyLen);
326     if (code == ERROR_SUCCESS)
327         cm_daemonCheckOfflineVolInterval = dummy;
328     afsi_log("daemonCheckOfflineVolInterval is %d", cm_daemonCheckOfflineVolInterval);
329     
330     RegCloseKey(parmKey);
331 }
332
333 /* periodic check daemon */
334 void cm_Daemon(long parm)
335 {
336     time_t now;
337     time_t lastLockCheck;
338     time_t lastVolCheck;
339     time_t lastCBExpirationCheck;
340     time_t lastVolCBRenewalCheck;
341     time_t lastDownServerCheck;
342     time_t lastUpServerCheck;
343     time_t lastTokenCacheCheck;
344     time_t lastBusyVolCheck;
345     char thostName[200];
346     unsigned long code;
347     struct hostent *thp;
348     HMODULE hHookDll;
349     char * name = "cm_Daemon_ShutdownEvent";
350     int configureFirewall = IsWindowsFirewallPresent();
351
352     cm_Daemon_ShutdownEvent = thrd_CreateEvent(NULL, FALSE, FALSE, name);
353     if ( GetLastError() == ERROR_ALREADY_EXISTS )
354         afsi_log("Event Object Already Exists: %s", name);
355
356     if (!configureFirewall) {
357         afsi_log("No Windows Firewall detected");
358     }
359
360     /* ping all file servers, up or down, with unauthenticated connection,
361      * to find out whether we have all our callbacks from the server still.
362      * Also, ping down VLDBs.
363      */
364     /*
365      * Seed the random number generator with our own address, so that
366      * clients starting at the same time don't all do vol checks at the
367      * same time.
368      */
369     gethostname(thostName, sizeof(thostName));
370     thp = gethostbyname(thostName);
371     if (thp == NULL)    /* In djgpp, gethostname returns the netbios
372                            name of the machine.  gethostbyname will fail
373                            looking this up if it differs from DNS name. */
374         code = 0;
375     else
376         memcpy(&code, thp->h_addr_list[0], 4);
377     
378     srand(ntohl(code));
379
380     cm_DaemonCheckInit();
381
382     now = osi_Time();
383     lastVolCheck = now - cm_daemonCheckVolInterval/2 + (rand() % cm_daemonCheckVolInterval);
384     lastCBExpirationCheck = now - cm_daemonCheckCBInterval/2 + (rand() % cm_daemonCheckCBInterval);
385     if (cm_daemonCheckVolCBInterval)
386         lastVolCBRenewalCheck = now - cm_daemonCheckVolCBInterval/2 + (rand() % cm_daemonCheckVolCBInterval);
387     lastLockCheck = now - cm_daemonCheckLockInterval/2 + (rand() % cm_daemonCheckLockInterval);
388     lastDownServerCheck = now - cm_daemonCheckDownInterval/2 + (rand() % cm_daemonCheckDownInterval);
389     lastUpServerCheck = now - cm_daemonCheckUpInterval/2 + (rand() % cm_daemonCheckUpInterval);
390     lastTokenCacheCheck = now - cm_daemonTokenCheckInterval/2 + (rand() % cm_daemonTokenCheckInterval);
391     lastBusyVolCheck = now - cm_daemonCheckOfflineVolInterval/2 * (rand() % cm_daemonCheckOfflineVolInterval);
392
393     while (daemon_ShutdownFlag == 0) {
394         /* check to see if the listener threads halted due to network 
395          * disconnect or other issues.  If so, attempt to restart them.
396          */
397         smb_RestartListeners();
398
399         if (daemon_ShutdownFlag == 1)
400             break;
401
402         if (configureFirewall) {
403             /* Open Microsoft Firewall to allow in port 7001 */
404             switch (icf_CheckAndAddAFSPorts(AFS_PORTSET_CLIENT)) {
405             case 0:
406                 afsi_log("Windows Firewall Configuration succeeded");
407                 configureFirewall = 0;
408                 break;
409             case 1:
410                 afsi_log("Invalid Windows Firewall Port Set");
411                 break;
412             case 2:
413                 afsi_log("Unable to open Windows Firewall Profile");
414                 break;
415             case 3:
416                 afsi_log("Unable to create/modify Windows Firewall Port entries");
417                 break;
418             default:
419                 afsi_log("Unknown Windows Firewall Configuration error");
420             }
421         } 
422
423         /* find out what time it is */
424         now = osi_Time();
425
426         /* check down servers */
427         if (now > lastDownServerCheck + cm_daemonCheckDownInterval &&
428             daemon_ShutdownFlag == 0) {
429             lastDownServerCheck = now;
430             osi_Log0(afsd_logp, "cm_Daemon CheckDownServers");
431             cm_CheckServers(CM_FLAG_CHECKDOWNSERVERS, NULL);
432             if (daemon_ShutdownFlag == 1)
433                 break;
434             now = osi_Time();
435         }
436
437         /* check up servers */
438         if (now > lastUpServerCheck + cm_daemonCheckUpInterval &&
439             daemon_ShutdownFlag == 0) {
440             lastUpServerCheck = now;
441             osi_Log0(afsd_logp, "cm_Daemon CheckUpServers");
442             cm_CheckServers(CM_FLAG_CHECKUPSERVERS, NULL);
443             if (daemon_ShutdownFlag == 1)
444                 break;
445             now = osi_Time();
446         }
447
448         if (now > lastVolCheck + cm_daemonCheckVolInterval &&
449             daemon_ShutdownFlag == 0) {
450             lastVolCheck = now;
451             cm_RefreshVolumes();
452             if (daemon_ShutdownFlag == 1)
453                 break;
454             now = osi_Time();
455         }
456
457         if (cm_daemonCheckVolCBInterval && 
458             now > lastVolCBRenewalCheck + cm_daemonCheckVolCBInterval &&
459             daemon_ShutdownFlag == 0) {
460             lastVolCBRenewalCheck = now;
461             cm_VolumeRenewROCallbacks();
462             if (daemon_ShutdownFlag == 1)
463                 break;
464             now = osi_Time();
465         }
466
467         if (now > lastBusyVolCheck + cm_daemonCheckOfflineVolInterval &&
468             daemon_ShutdownFlag == 0) {
469             lastVolCheck = now;
470             cm_CheckOfflineVolumes();
471             if (daemon_ShutdownFlag == 1)
472                 break;
473             now = osi_Time();
474         }
475
476         if (now > lastCBExpirationCheck + cm_daemonCheckCBInterval &&
477             daemon_ShutdownFlag == 0) {
478             lastCBExpirationCheck = now;
479             cm_CheckCBExpiration();
480             if (daemon_ShutdownFlag == 1)
481                 break;
482             now = osi_Time();
483         }
484
485         if (now > lastLockCheck + cm_daemonCheckLockInterval &&
486             daemon_ShutdownFlag == 0) {
487             lastLockCheck = now;
488             cm_CheckLocks();
489             if (daemon_ShutdownFlag == 1)
490                 break;
491             now = osi_Time();
492         }
493
494         if (now > lastTokenCacheCheck + cm_daemonTokenCheckInterval &&
495             daemon_ShutdownFlag == 0) {
496             lastTokenCacheCheck = now;
497             cm_CheckTokenCache(now);
498             if (daemon_ShutdownFlag == 1)
499                 break;
500             now = osi_Time();
501         }
502
503         /* allow an exit to be called prior to stopping the service */
504         hHookDll = LoadLibrary(AFSD_HOOK_DLL);
505         if (hHookDll)
506         {
507             BOOL hookRc = TRUE;
508             AfsdDaemonHook daemonHook = ( AfsdDaemonHook ) GetProcAddress(hHookDll, AFSD_DAEMON_HOOK);
509             if (daemonHook)
510             {
511                 hookRc = daemonHook();
512             }
513             FreeLibrary(hHookDll);
514             hHookDll = NULL;
515
516             if (hookRc == FALSE)
517             {
518                 SetEvent(WaitToTerminate);
519             }
520         }
521
522         if (daemon_ShutdownFlag == 1) {
523             break;
524         }
525         thrd_Sleep(30 * 1000);          /* sleep 30 seconds */
526     }
527     thrd_SetEvent(cm_Daemon_ShutdownEvent);
528 }       
529
530 void cm_DaemonShutdown(void)
531 {
532     int i;
533     DWORD code;
534
535     daemon_ShutdownFlag = 1;
536     osi_Wakeup((LONG_PTR) &cm_bkgListp);
537
538     /* wait for shutdown */
539     if (cm_Daemon_ShutdownEvent)
540         code = thrd_WaitForSingleObject_Event(cm_Daemon_ShutdownEvent, INFINITE); 
541
542     for ( i=0; i<cm_nDaemons; i++) {
543         if (cm_BkgDaemon_ShutdownEvent[i])
544             code = thrd_WaitForSingleObject_Event(cm_BkgDaemon_ShutdownEvent[i], INFINITE);
545     }
546
547     if (cm_IPAddrDaemon_ShutdownEvent)
548         code = thrd_WaitForSingleObject_Event(cm_IPAddrDaemon_ShutdownEvent, INFINITE);
549 }
550
551 void cm_InitDaemon(int nDaemons)
552 {
553     static osi_once_t once;
554     long pid;
555     thread_t phandle;
556     int i;
557
558     cm_nDaemons = (nDaemons > CM_MAX_DAEMONS) ? CM_MAX_DAEMONS : nDaemons;
559     
560     if (osi_Once(&once)) {
561         lock_InitializeRWLock(&cm_daemonLock, "cm_daemonLock");
562         osi_EndOnce(&once);
563
564         /* creating IP Address Change monitor daemon */
565         phandle = thrd_Create((SecurityAttrib) 0, 0,
566                                (ThreadFunc) cm_IpAddrDaemon, 0, 0, &pid, "cm_IpAddrDaemon");
567         osi_assertx(phandle != NULL, "cm_IpAddrDaemon thread creation failure");
568         thrd_CloseHandle(phandle);
569
570         /* creating pinging daemon */
571         phandle = thrd_Create((SecurityAttrib) 0, 0,
572                                (ThreadFunc) cm_Daemon, 0, 0, &pid, "cm_Daemon");
573         osi_assertx(phandle != NULL, "cm_Daemon thread creation failure");
574         thrd_CloseHandle(phandle);
575
576         for(i=0; i < cm_nDaemons; i++) {
577             phandle = thrd_Create((SecurityAttrib) 0, 0,
578                                    (ThreadFunc) cm_BkgDaemon, (LPVOID)i, 0, &pid,
579                                    "cm_BkgDaemon");
580             osi_assertx(phandle != NULL, "cm_BkgDaemon thread creation failure");
581             thrd_CloseHandle(phandle);
582         }
583     }
584 }