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