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