]> git.sur5r.net Git - openldap/blob - servers/slapd/daemon.c
Fix unprotected (and bad) FD_SET asserts.
[openldap] / servers / slapd / daemon.c
1 #include "portable.h"
2
3 #include <stdio.h>
4
5 #include <ac/ctype.h>
6 #include <ac/errno.h>
7 #include <ac/signal.h>
8 #include <ac/socket.h>
9 #include <ac/string.h>
10 #include <ac/time.h>
11 #include <ac/unistd.h>
12
13 #include "ldapconfig.h"
14 #include "slap.h"
15
16 #ifdef HAVE_TCPD
17 #include <tcpd.h>
18
19 int allow_severity = LOG_INFO;
20 int deny_severity = LOG_NOTICE;
21 #endif /* TCP Wrappers */
22
23 /* globals */
24 int dtblsize;
25
26 #ifdef HAVE_WINSOCK2
27 /* forward reference */
28 void hit_socket();
29 /* In wsa_err.c */
30 char *WSAGetLastErrorString();
31
32 #define WAKE_LISTENER \
33 do {\
34     if( wake ) {\
35         ldap_pvt_thread_kill( listener_tid, LDAP_SIGUSR1 );\
36         hit_socket();\
37     }\
38 } while(0)
39 #else
40 #define WAKE_LISTENER \
41 do {\
42     if( wake ) {\
43         ldap_pvt_thread_kill( listener_tid, LDAP_SIGUSR1 );\
44     }\
45 } while(0)
46 #endif
47
48 static int daemon_initialized = 0;
49 static ldap_pvt_thread_t        listener_tid;
50 static volatile sig_atomic_t slapd_shutdown = 0;
51 static volatile sig_atomic_t slapd_listener = 0;
52 void sockinit();
53
54 struct slap_daemon {
55         ldap_pvt_thread_mutex_t sd_mutex;
56
57         int sd_nactives;
58
59 #ifndef HAVE_WINSOCK
60         /* In winsock, accept() returns values higher than dtblsize
61                 so don't bother with this optimization */
62         int sd_nfds;
63 #endif
64
65         fd_set sd_actives;
66         fd_set sd_readers;
67         fd_set sd_writers;
68 } slap_daemon; 
69
70 /*
71  * Add a descriptor to daemon control
72  */
73 static void slapd_add(int s) {
74         ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
75
76         assert( !FD_ISSET( s, &slap_daemon.sd_actives ));
77         assert( !FD_ISSET( s, &slap_daemon.sd_readers ));
78         assert( !FD_ISSET( s, &slap_daemon.sd_writers ));
79
80 #ifndef HAVE_WINSOCK
81         if (s >= slap_daemon.sd_nfds) {
82                 slap_daemon.sd_nfds = s + 1;
83         }
84 #endif
85
86         FD_SET( (unsigned) s, &slap_daemon.sd_actives );
87         FD_SET( (unsigned) s, &slap_daemon.sd_readers );
88
89         Debug( LDAP_DEBUG_CONNS, "daemon: added %d%s%s\n", s,
90             FD_ISSET(s, &slap_daemon.sd_readers) ? "r" : "",
91                 FD_ISSET(s, &slap_daemon.sd_writers) ? "w" : "" );
92
93         ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
94 }
95
96 /*
97  * Remove the descriptor from daemon control
98  */
99 void slapd_remove(int s) {
100         ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
101
102         assert( FD_ISSET( s, &slap_daemon.sd_actives ));
103
104         Debug( LDAP_DEBUG_CONNS, "daemon: removing %d%s%s\n", s,
105             FD_ISSET(s, &slap_daemon.sd_readers) ? "r" : "",
106                 FD_ISSET(s, &slap_daemon.sd_writers) ? "w" : "" );
107
108         FD_CLR( (unsigned) s, &slap_daemon.sd_actives );
109         FD_CLR( (unsigned) s, &slap_daemon.sd_readers );
110         FD_CLR( (unsigned) s, &slap_daemon.sd_writers );
111
112         ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
113 }
114
115 void slapd_clr_write(int s, int wake) {
116         ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
117         WAKE_LISTENER;
118
119         assert( FD_ISSET( (unsigned) s, &slap_daemon.sd_actives) );
120         FD_CLR( (unsigned) s, &slap_daemon.sd_writers );
121
122         ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
123 }
124
125 void slapd_set_write(int s, int wake) {
126         ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
127     WAKE_LISTENER;
128
129
130         assert( FD_ISSET( s, &slap_daemon.sd_actives) );
131         FD_SET( (unsigned) s, &slap_daemon.sd_writers );
132
133         ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
134 }
135
136 void slapd_clr_read(int s, int wake) {
137         ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
138     WAKE_LISTENER;
139
140         assert( FD_ISSET( s, &slap_daemon.sd_actives) );
141         FD_CLR( (unsigned) s, &slap_daemon.sd_readers );
142
143         ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
144
145 }
146
147 void slapd_set_read(int s, int wake) {
148         ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
149     WAKE_LISTENER;
150
151         assert( FD_ISSET( s, &slap_daemon.sd_actives) );
152         FD_SET( (unsigned) s, &slap_daemon.sd_readers );
153
154         ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
155 }
156
157 static void slapd_close(int s) {
158         slapd_remove(s);
159
160         Debug( LDAP_DEBUG_CONNS, "daemon: closing %d\n", s, 0, 0 );
161         tcp_close(s);
162 }
163
164 int
165 set_socket( struct sockaddr_in *addr )
166 {
167         int     tcps = -1;
168     if ( !daemon_initialized ) sockinit();
169
170 #ifdef HAVE_SYSCONF
171         dtblsize = sysconf( _SC_OPEN_MAX );
172 #elif HAVE_GETDTABLESIZE
173         dtblsize = getdtablesize();
174 #else
175         dtblsize = FD_SETSIZE;
176 #endif
177
178 #ifdef FD_SETSIZE
179         if(dtblsize > FD_SETSIZE) {
180                 dtblsize = FD_SETSIZE;
181         }
182 #endif  /* !FD_SETSIZE */
183
184         if( addr != NULL ) {
185                 int     tmp;
186
187                 if ( (tcps = socket( AF_INET, SOCK_STREAM, 0 )) == -1 ) {
188 #ifndef HAVE_WINSOCK
189                         int err = errno;
190                         Debug( LDAP_DEBUG_ANY,
191                                 "daemon: socket() failed errno %d (%s)\n", err,
192                         err > -1 && err < sys_nerr ? sys_errlist[err] :
193                         "unknown", 0 );
194 #else
195                         Debug( LDAP_DEBUG_ANY, 
196                                 "daemon: socket() failed errno %d (%s)\n",
197                                 WSAGetLastError(),
198                         WSAGetLastErrorString(), 0 );
199 #endif
200                         exit( 1 );
201                 }
202
203 #ifndef HAVE_WINSOCK
204                 if ( tcps >= dtblsize ) {
205                         Debug( LDAP_DEBUG_ANY,
206                                 "daemon: listener descriptor %d is too great\n",
207                                 tcps, dtblsize, 0 );
208                         exit( 1 );
209                 }
210 #endif
211
212 #ifdef SO_REUSEADDR
213                 tmp = 1;
214                 if ( setsockopt( tcps, SOL_SOCKET, SO_REUSEADDR,
215                         (char *) &tmp, sizeof(tmp) ) == -1 )
216                 {
217                         int err = errno;
218                         Debug( LDAP_DEBUG_ANY,
219                                "slapd(%d): setsockopt() failed errno %d (%s)\n",
220                         tcps, err,
221                                 err > -1 && err < sys_nerr
222                                         ? sys_errlist[err] : "unknown" );
223                 }
224 #endif
225 #ifdef SO_KEEPALIVE
226                 tmp = 1;
227                 if ( setsockopt( tcps, SOL_SOCKET, SO_KEEPALIVE,
228                         (char *) &tmp, sizeof(tmp) ) == -1 )
229                 {
230                         int err = errno;
231                         Debug( LDAP_DEBUG_ANY,
232                                 "slapd(%d): setsockopt(KEEPALIVE) failed errno %d (%s)\n",
233                         tcps, err,
234                                 err > -1 && err < sys_nerr
235                                         ? sys_errlist[err] : "unknown" );
236                 }
237 #endif
238
239
240                 if ( bind( tcps, (struct sockaddr *) addr, sizeof(*addr) ) == -1 ) {
241                         int err = errno;
242                         Debug( LDAP_DEBUG_ANY, "daemon: bind(%d) failed errno %d (%s)\n",
243                         tcps, err,
244                                 err > -1 && err < sys_nerr
245                                         ? sys_errlist[err] : "unknown" );
246                         exit( 1 );
247                 }
248         }
249
250         return tcps;
251 }
252
253 static void *
254 slapd_daemon_task(
255         void *ptr
256 )
257 {
258         int inetd = ((int *)ptr) [0];
259         int tcps  = ((int *)ptr) [1];
260         free( ptr );
261
262     if ( !daemon_initialized ) sockinit();
263
264         slapd_listener=1;
265
266         ldap_pvt_thread_mutex_init( &slap_daemon.sd_mutex );
267         FD_ZERO( &slap_daemon.sd_readers );
268         FD_ZERO( &slap_daemon.sd_writers );
269
270         if( !inetd ) {
271                 if ( listen( tcps, 5 ) == -1 ) {
272                         int err = errno;
273                         Debug( LDAP_DEBUG_ANY,
274                                 "daemon: listen(%d, 5) failed errno %d (%s)\n",
275                             tcps, err,
276                                 err > -1 && err < sys_nerr
277                                         ? sys_errlist[err] : "unknown" );
278                         exit( 1 );
279                 }
280
281                 slapd_add( tcps );
282
283         } else {
284                 if( connection_init( 0, NULL, NULL ) ) {
285                         Debug( LDAP_DEBUG_ANY,
286                                 "connection_init(%d) failed.\n",
287                                 0, 0, 0 );
288
289                         exit( 1 );
290                 }
291
292                 slapd_add( 0 );
293         }
294
295         while ( !slapd_shutdown ) {
296                 unsigned int i;
297                 int ns, nfds;
298
299                 fd_set                  readfds;
300                 fd_set                  writefds;
301
302                 struct sockaddr_in      from;
303 #if defined(SLAPD_RLOOKUPS) || defined(HAVE_TCPD)
304         struct hostent          *hp;
305 #endif
306         struct timeval          zero;
307                 struct timeval          *tvp;
308
309                 char    *client_name;
310                 char    *client_addr;
311
312                 FD_ZERO( &writefds );
313                 FD_ZERO( &readfds );
314
315                 zero.tv_sec = 0;
316                 zero.tv_usec = 0;
317
318                 ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
319
320 #ifdef FD_SET_MANUAL_COPY
321                 for( s = 0; s < nfds; s++ ) {
322                         if(FD_ISSET( &slap_sd_writers, s )) {
323                                 FD_SET( &writefds, s );
324                         }
325                         if(FD_ISSET( &slap_sd_writers, s )) {
326                                 FD_SET( &writefds, s );
327                         }
328                 }
329 #else
330                 memcpy( &readfds, &slap_daemon.sd_readers, sizeof(fd_set) );
331                 memcpy( &writefds, &slap_daemon.sd_writers, sizeof(fd_set) );
332 #endif
333
334                 FD_SET( (unsigned) tcps, &readfds );
335
336 #ifndef HAVE_WINSOCK
337                 nfds = slap_daemon.sd_nfds;
338 #else
339                 nfds = dtblsize;
340 #endif
341
342                 ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
343
344                 ldap_pvt_thread_mutex_lock( &active_threads_mutex );
345 #if defined( HAVE_YIELDING_SELECT ) || defined( NO_THREADS )
346                 tvp = NULL;
347 #else
348                 tvp = active_threads ? &zero : NULL;
349 #endif
350
351                 Debug( LDAP_DEBUG_CONNS,
352                         "daemon: select: tcps=%d active_threads=%d tvp=%s\n",
353                     tcps, active_threads,
354                         tvp == NULL ? "NULL" : "zero" );
355            
356
357                 ldap_pvt_thread_mutex_unlock( &active_threads_mutex );
358
359                 switch(ns = select( nfds, &readfds, &writefds, 0, tvp )) {
360                 case -1: {      /* failure - try again */
361                                 int err = errno;
362                                 if( err != EINTR ) {
363                                         Debug( LDAP_DEBUG_CONNS,
364                                                 "daemon: select failed (%d): %s\n",
365                                                 err,
366                                                 err >= 0 && err < sys_nerr
367                                                         ? sys_errlist[err] : "unknown",
368                                                 0 );
369
370                                         slapd_shutdown = -1;
371                                 }
372                         }
373                         continue;
374
375                 case 0:         /* timeout - let threads run */
376                         Debug( LDAP_DEBUG_CONNS, "daemon: select timeout - yielding\n",
377                             0, 0, 0 );
378                 ldap_pvt_thread_yield();
379                         continue;
380
381                 default:        /* something happened - deal with it */
382                         Debug( LDAP_DEBUG_CONNS, "daemon: activity on %d descriptors\n",
383                                 ns, 0, 0 );
384                         /* FALL THRU */
385                 }
386
387                 if ( FD_ISSET( tcps, &readfds ) ) {
388                         int s;
389                         int len = sizeof(from);
390                         long id;
391
392                         if ( (s = accept( tcps,
393                                 (struct sockaddr *) &from, &len )) == -1 )
394                         {
395                                 int err = errno;
396                                 Debug( LDAP_DEBUG_ANY,
397                                     "daemon: accept(%d) failed errno %d (%s)\n", err,
398                                     tcps, err >= 0 && err < sys_nerr ?
399                                     sys_errlist[err] : "unknown");
400                                 continue;
401                         }
402
403 #ifdef LDAP_DEBUG
404                         ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
405
406                         /* newly accepted stream should not be in any of the FD SETS */
407
408                         assert( !FD_ISSET( s, &slap_daemon.sd_actives) );
409                         assert( !FD_ISSET( s, &slap_daemon.sd_readers) );
410                         assert( !FD_ISSET( s, &slap_daemon.sd_writers) );
411
412                         ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
413 #endif
414
415 #ifndef HAVE_WINSOCK
416                         /* make sure descriptor number isn't too great */
417                         if ( s >= dtblsize ) {
418                                 Debug( LDAP_DEBUG_ANY,
419                                         "daemon: %d beyond descriptor table size %d\n",
420                                         s, dtblsize, 0 );
421                                 tcp_close(s);
422                                 continue;
423                         }
424 #endif
425                    
426                         Debug( LDAP_DEBUG_CONNS, "daemon: new connection on %d\n",
427                                 s, 0, 0 );
428
429                         len = sizeof(from);
430                         if ( getpeername( s, (struct sockaddr *) &from, &len ) == 0 ) {
431                                 client_addr = inet_ntoa( from.sin_addr );
432
433 #if defined(SLAPD_RLOOKUPS) || defined(HAVE_TCPD)
434                                 hp = gethostbyaddr( (char *)
435                                     &(from.sin_addr.s_addr),
436                                     sizeof(from.sin_addr.s_addr), AF_INET );
437
438                                 if(hp) {
439                                         char *p;
440                                         client_name = hp->h_name;
441
442                                         /* normalize the domain */
443                                         for ( p = client_name; *p; p++ ) {
444                                                 *p = TOLOWER( (unsigned char) *p );
445                                         }
446
447                                 } else {
448                                         client_name = NULL;
449                                 }
450 #else
451                                 client_name = NULL;
452 #endif
453
454                         } else {
455                                 client_name = NULL;;
456                                 client_addr = NULL;
457                         }
458
459 #ifdef HAVE_TCPD
460                         if(!hosts_ctl("slapd",
461                                 client_name != NULL ? client_name : STRING_UNKNOWN,
462                                 client_addr != NULL ? client_addr : STRING_UNKNOWN,
463                                 STRING_UNKNOWN))
464                         {
465                                 /* DENY ACCESS */
466                                 Statslog( LDAP_DEBUG_ANY,
467                                  "fd=%d connection from %s (%s) denied.\n",
468                                         s,
469                                         client_name == NULL ? "unknown" : client_name,
470                                         client_addr == NULL ? "unknown" : client_addr,
471                                   0, 0 );
472
473                                 tcp_close(s);
474                                 continue;
475                         }
476 #endif /* HAVE_TCPD */
477
478
479                         if( (id = connection_init(s, client_name, client_addr)) < 0 ) {
480                                 Debug( LDAP_DEBUG_ANY,
481                                         "daemon: connection_init(%d, %s, %s) failed.\n",
482                                         s,
483                                         client_name == NULL ? "unknown" : client_name,
484                                         client_addr == NULL ? "unknown" : client_addr);
485                                 tcp_close(s);
486                                 continue;
487                         }
488
489                         Statslog( LDAP_DEBUG_STATS,
490                                 "daemon: conn=%d fd=%d connection from %s (%s) accepted.\n",
491                                 id, s,
492                                 client_name == NULL ? "unknown" : client_name,
493                                 client_addr == NULL ? "unknown" : client_addr,
494                                 0 );
495
496                         slapd_add( s );
497                         continue;
498                 }
499
500 #ifdef LDAP_DEBUG
501                 Debug( LDAP_DEBUG_CONNS, "daemon: activity on:", 0, 0, 0 );
502 #ifdef HAVE_WINSOCK
503                 for ( i = 0; i < readfds.fd_count; i++ ) {
504                         Debug( LDAP_DEBUG_CONNS, " %d%s", readfds.fd_array[i], "r" );
505                 }
506                 for ( i = 0; i < writefds.fd_count; i++ ) {
507                         Debug( LDAP_DEBUG_CONNS, " %d%s", writefds.fd_array[i], "w" );
508                 }
509 #else
510                 for ( i = 0; i < nfds; i++ ) {
511                         int     a, r, w;
512
513                         r = FD_ISSET( i, &readfds );
514                         w = FD_ISSET( i, &writefds );
515                         if ( i != tcps && (r || w) ) {
516                                 Debug( LDAP_DEBUG_CONNS, " %d%s%s", i,
517                                     r ? "r" : "", w ? "w" : "" );
518                         }
519                 }
520 #endif
521                 Debug( LDAP_DEBUG_CONNS, "\n", 0, 0, 0 );
522 #endif
523
524                 /* loop through the writers */
525 #ifdef HAVE_WINSOCK
526                 for ( i = 0; i < writefds.fd_count; i++ ) {
527                         int wd = writefds.fd_array[i];
528
529                         if ( wd == tcps ) {
530                                 continue;
531                         }
532
533                         Debug( LDAP_DEBUG_CONNS,
534                                 "daemon: signalling write waiter on %d\n",
535                                 wd, 0, 0 );
536
537                         slapd_clr_write( wd, 0 );
538                         if ( connection_write( wd ) < 0 ) {
539                                 FD_CLR( (unsigned) wd, &readfds );
540                                 slapd_close( wd );
541                         }
542                 }
543 #else
544                 for ( i = 0; i < nfds; i++ ) {
545                         if ( i == tcps ) {
546                                 continue;
547                         }
548                         if ( FD_ISSET( i, &writefds ) ) {
549                                 Debug( LDAP_DEBUG_CONNS,
550                                     "daemon: signaling write waiter on %d\n", i, 0, 0 );
551
552                                 /* clear the write flag */
553                                 slapd_clr_write( i, 0 );
554                                 
555                                 if( connection_write( i ) < 0 ) { 
556                                         FD_CLR( i, &readfds );
557                                         slapd_close( i );
558                                 }
559                         }
560                 }
561 #endif
562
563 #ifdef HAVE_WINSOCK
564                 for ( i = 0; i < readfds.fd_count; i++ ) {
565                         int rd = readfds.fd_array[i];
566                         if ( rd == tcps ) {
567                                 continue;
568                         }
569                         Debug ( LDAP_DEBUG_CONNS,
570                                 "daemon: read activity on %d\n", rd, 0, 0 );
571
572                         ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
573                         assert( FD_ISSET( rd, &slap_daemon.sd_actives) );
574                         ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
575
576                         if ( connection_read( rd ) < 0 ) {
577                                 slapd_close( rd );
578                         }
579                 }
580 #else
581                 for ( i = 0; i < nfds; i++ ) {
582                         if ( i == tcps ) {
583                                 continue;
584                         }
585
586                         if ( FD_ISSET( i, &readfds ) ) {
587                                 Debug( LDAP_DEBUG_CONNS,
588                                     "daemon: read activity on %d\n", i, 0, 0 );
589
590                                 assert( FD_ISSET( i, &slap_daemon.sd_actives) );
591
592                                 if( connection_read( i ) < 0) {
593                                         slapd_close( i );
594                                 }
595                         }
596                 }
597 #endif
598                 ldap_pvt_thread_yield();
599         }
600
601         if( slapd_shutdown > 0 ) {
602                 Debug( LDAP_DEBUG_TRACE,
603                         "daemon: shutdown requested (%d) and initiated.\n",
604                         (int) slapd_shutdown, 0, 0 );
605
606         } else if ( slapd_shutdown < 0 ) {
607                 Debug( LDAP_DEBUG_TRACE,
608                         "daemon: abnormal condition (%d), shutdown initiated.\n",
609                         (int) slapd_shutdown, 0, 0 );
610         } else {
611                 Debug( LDAP_DEBUG_TRACE,
612                         "daemon: no active streams, shutdown initiated.\n",
613                         0, 0, 0 );
614         }
615
616         if( tcps >= 0 ) {
617                 tcp_close( tcps );
618         }
619
620         /* we only implement "quick" shutdown */
621         connections_shutdown();
622
623         ldap_pvt_thread_mutex_lock( &active_threads_mutex );
624         Debug( LDAP_DEBUG_ANY,
625             "slapd shutdown: waiting for %d threads to terminate\n",
626             active_threads, 0, 0 );
627         while ( active_threads > 0 ) {
628                 ldap_pvt_thread_cond_wait(&active_threads_cond, &active_threads_mutex);
629         }
630         ldap_pvt_thread_mutex_unlock( &active_threads_mutex );
631
632         slapd_listener = 0;
633         return NULL;
634 }
635
636 int slapd_daemon( int inetd, int tcps )
637 {
638         int rc;
639         int *args = ch_malloc( sizeof( int[2] ) );
640         args[0] = inetd;
641         args[1] = tcps;
642
643     if ( !daemon_initialized ) sockinit();
644
645         connections_init();
646
647 #define SLAPD_LISTENER_THREAD 1
648 #if defined( SLAPD_LISTENER_THREAD ) || !defined(HAVE_PTHREADS)
649
650         /* listener as a separate THREAD */
651         rc = ldap_pvt_thread_create( &listener_tid,
652                 0, slapd_daemon_task, args );
653
654         if ( rc != 0 ) {
655                 Debug( LDAP_DEBUG_ANY,
656                     "listener ldap_pvt_thread_create failed (%d)\n", rc, 0, 0 );
657                 goto destory;
658         }
659
660         /* wait for the listener thread to complete */
661         ldap_pvt_thread_join( listener_tid, (void *) NULL );
662 #else
663         /* expermimental code */
664         listener_tid = pthread_self();
665         slapd_daemon_task( args );
666 #endif
667
668         rc = 0;
669
670 destory:
671         connections_destroy();
672
673 #ifdef HAVE_WINSOCK
674     WSACleanup( );
675 #endif
676
677         return rc;
678 }
679
680 #ifdef HAVE_WINSOCK2
681 void sockinit()
682 {
683     WORD wVersionRequested;
684         WSADATA wsaData;
685         int err;
686  
687         wVersionRequested = MAKEWORD( 2, 0 );
688  
689         err = WSAStartup( wVersionRequested, &wsaData );
690         if ( err != 0 ) {
691                 /* Tell the user that we couldn't find a usable */
692                 /* WinSock DLL.                                  */
693                 return;
694         }
695  
696         /* Confirm that the WinSock DLL supports 2.0.*/
697         /* Note that if the DLL supports versions greater    */
698         /* than 2.0 in addition to 2.0, it will still return */
699         /* 2.0 in wVersion since that is the version we      */
700         /* requested.                                        */
701  
702         if ( LOBYTE( wsaData.wVersion ) != 2 ||
703                 HIBYTE( wsaData.wVersion ) != 0 )
704         {
705             /* Tell the user that we couldn't find a usable */
706             /* WinSock DLL.                                  */
707             WSACleanup( );
708             return; 
709         }
710     daemon_initialized = 1;
711 }       /* The WinSock DLL is acceptable. Proceed. */
712
713 void hit_socket( void )
714 {
715         int s, on = 1;
716         extern struct sockaddr_in       bind_addr;
717
718         /* throw something at the socket to terminate the select() in the daemon thread. */
719         if (( s = socket( AF_INET, SOCK_STREAM, 0 )) == INVALID_SOCKET )
720                 Debug( LDAP_DEBUG_TRACE,
721                         "slap_set_shutdown: socket failed\n\tWSAGetLastError=%d (%s)\n",
722                         WSAGetLastError(), WSAGetLastErrorString(), 0 );
723
724         if ( ioctlsocket( s, FIONBIO, &on ) == -1 ) 
725                 Debug( LDAP_DEBUG_TRACE,
726                         "slap_set_shutdown:FIONBIO ioctl on %d faled\n\tWSAGetLastError=%d (%s)\n",
727                         s, WSAGetLastError(), WSAGetLastError() );
728
729         bind_addr.sin_addr.s_addr = htonl( INADDR_LOOPBACK );
730
731         if ( connect( s, (struct sockaddr *)&bind_addr, sizeof( struct sockaddr_in )) == SOCKET_ERROR ) {
732                 /* we can probably expect some error to occur here, mostly WSAEWOULDBLOCK */
733         }
734
735         tcp_close(s);
736 }
737
738 #elif HAVE_WINSOCK
739 void sockinit()
740 {       WSADATA wsaData;
741         if ( WSAStartup( 0x0101, &wsaData ) != 0 ) {
742             return( NULL );
743         }
744     daemon_initialized = 1;
745 }
746 #else
747 void sockinit()
748 {
749     daemon_initialized = 1;
750     return;
751 }
752 #endif
753
754 void
755 slap_set_shutdown( int sig )
756 {
757         slapd_shutdown = sig;
758 #ifndef HAVE_WINSOCK
759         if(slapd_listener) {
760                 ldap_pvt_thread_kill( listener_tid, LDAP_SIGUSR1 );
761         }
762 #else
763         hit_socket();
764 #endif
765         /* reinstall self */
766         (void) SIGNAL( sig, slap_set_shutdown );
767 }
768
769 void
770 slap_do_nothing( int sig )
771 {
772         /* reinstall self */
773         (void) SIGNAL( sig, slap_do_nothing );
774 }