]> git.sur5r.net Git - openldap/blob - servers/slapd/daemon.c
Fix typo in last commit.
[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
363                                 if( err != EINTR || err != EBADF) {
364                                         Debug( LDAP_DEBUG_CONNS,
365                                                 "daemon: select failed (%d): %s\n",
366                                                 err,
367                                                 err >= 0 && err < sys_nerr
368                                                         ? sys_errlist[err] : "unknown",
369                                                 0 );
370
371                                         slapd_shutdown = -1;
372                                 }
373                         }
374                         continue;
375
376                 case 0:         /* timeout - let threads run */
377                         Debug( LDAP_DEBUG_CONNS, "daemon: select timeout - yielding\n",
378                             0, 0, 0 );
379                 ldap_pvt_thread_yield();
380                         continue;
381
382                 default:        /* something happened - deal with it */
383                         Debug( LDAP_DEBUG_CONNS, "daemon: activity on %d descriptors\n",
384                                 ns, 0, 0 );
385                         /* FALL THRU */
386                 }
387
388                 if ( FD_ISSET( tcps, &readfds ) ) {
389                         int s;
390                         int len = sizeof(from);
391                         long id;
392
393                         if ( (s = accept( tcps,
394                                 (struct sockaddr *) &from, &len )) == -1 )
395                         {
396                                 int err = errno;
397                                 Debug( LDAP_DEBUG_ANY,
398                                     "daemon: accept(%d) failed errno %d (%s)\n", err,
399                                     tcps, err >= 0 && err < sys_nerr ?
400                                     sys_errlist[err] : "unknown");
401                                 continue;
402                         }
403
404 #ifdef LDAP_DEBUG
405                         ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
406
407                         /* newly accepted stream should not be in any of the FD SETS */
408
409                         assert( !FD_ISSET( s, &slap_daemon.sd_actives) );
410                         assert( !FD_ISSET( s, &slap_daemon.sd_readers) );
411                         assert( !FD_ISSET( s, &slap_daemon.sd_writers) );
412
413                         ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
414 #endif
415
416 #ifndef HAVE_WINSOCK
417                         /* make sure descriptor number isn't too great */
418                         if ( s >= dtblsize ) {
419                                 Debug( LDAP_DEBUG_ANY,
420                                         "daemon: %d beyond descriptor table size %d\n",
421                                         s, dtblsize, 0 );
422                                 tcp_close(s);
423                                 continue;
424                         }
425 #endif
426                    
427                         Debug( LDAP_DEBUG_CONNS, "daemon: new connection on %d\n",
428                                 s, 0, 0 );
429
430                         len = sizeof(from);
431                         if ( getpeername( s, (struct sockaddr *) &from, &len ) == 0 ) {
432                                 client_addr = inet_ntoa( from.sin_addr );
433
434 #if defined(SLAPD_RLOOKUPS) || defined(HAVE_TCPD)
435                                 hp = gethostbyaddr( (char *)
436                                     &(from.sin_addr.s_addr),
437                                     sizeof(from.sin_addr.s_addr), AF_INET );
438
439                                 if(hp) {
440                                         char *p;
441                                         client_name = hp->h_name;
442
443                                         /* normalize the domain */
444                                         for ( p = client_name; *p; p++ ) {
445                                                 *p = TOLOWER( (unsigned char) *p );
446                                         }
447
448                                 } else {
449                                         client_name = NULL;
450                                 }
451 #else
452                                 client_name = NULL;
453 #endif
454
455                         } else {
456                                 client_name = NULL;;
457                                 client_addr = NULL;
458                         }
459
460 #ifdef HAVE_TCPD
461                         if(!hosts_ctl("slapd",
462                                 client_name != NULL ? client_name : STRING_UNKNOWN,
463                                 client_addr != NULL ? client_addr : STRING_UNKNOWN,
464                                 STRING_UNKNOWN))
465                         {
466                                 /* DENY ACCESS */
467                                 Statslog( LDAP_DEBUG_ANY,
468                                  "fd=%d connection from %s (%s) denied.\n",
469                                         s,
470                                         client_name == NULL ? "unknown" : client_name,
471                                         client_addr == NULL ? "unknown" : client_addr,
472                                   0, 0 );
473
474                                 tcp_close(s);
475                                 continue;
476                         }
477 #endif /* HAVE_TCPD */
478
479
480                         if( (id = connection_init(s, client_name, client_addr)) < 0 ) {
481                                 Debug( LDAP_DEBUG_ANY,
482                                         "daemon: connection_init(%d, %s, %s) failed.\n",
483                                         s,
484                                         client_name == NULL ? "unknown" : client_name,
485                                         client_addr == NULL ? "unknown" : client_addr);
486                                 tcp_close(s);
487                                 continue;
488                         }
489
490                         Statslog( LDAP_DEBUG_STATS,
491                                 "daemon: conn=%d fd=%d connection from %s (%s) accepted.\n",
492                                 id, s,
493                                 client_name == NULL ? "unknown" : client_name,
494                                 client_addr == NULL ? "unknown" : client_addr,
495                                 0 );
496
497                         slapd_add( s );
498                         continue;
499                 }
500
501 #ifdef LDAP_DEBUG
502                 Debug( LDAP_DEBUG_CONNS, "daemon: activity on:", 0, 0, 0 );
503 #ifdef HAVE_WINSOCK
504                 for ( i = 0; i < readfds.fd_count; i++ ) {
505                         Debug( LDAP_DEBUG_CONNS, " %d%s", readfds.fd_array[i], "r" );
506                 }
507                 for ( i = 0; i < writefds.fd_count; i++ ) {
508                         Debug( LDAP_DEBUG_CONNS, " %d%s", writefds.fd_array[i], "w" );
509                 }
510 #else
511                 for ( i = 0; i < nfds; i++ ) {
512                         int     a, r, w;
513
514                         r = FD_ISSET( i, &readfds );
515                         w = FD_ISSET( i, &writefds );
516                         if ( i != tcps && (r || w) ) {
517                                 Debug( LDAP_DEBUG_CONNS, " %d%s%s", i,
518                                     r ? "r" : "", w ? "w" : "" );
519                         }
520                 }
521 #endif
522                 Debug( LDAP_DEBUG_CONNS, "\n", 0, 0, 0 );
523 #endif
524
525                 /* loop through the writers */
526 #ifdef HAVE_WINSOCK
527                 for ( i = 0; i < writefds.fd_count; i++ ) {
528                         int wd = writefds.fd_array[i];
529
530                         if ( wd == tcps ) {
531                                 continue;
532                         }
533
534                         Debug( LDAP_DEBUG_CONNS,
535                                 "daemon: signalling write waiter on %d\n",
536                                 wd, 0, 0 );
537
538                         slapd_clr_write( wd, 0 );
539                         if ( connection_write( wd ) < 0 ) {
540                                 FD_CLR( (unsigned) wd, &readfds );
541                                 slapd_close( wd );
542                         }
543                 }
544 #else
545                 for ( i = 0; i < nfds; i++ ) {
546                         if ( i == tcps ) {
547                                 continue;
548                         }
549                         if ( FD_ISSET( i, &writefds ) ) {
550                                 Debug( LDAP_DEBUG_CONNS,
551                                     "daemon: signaling write waiter on %d\n", i, 0, 0 );
552
553                                 /* clear the write flag */
554                                 slapd_clr_write( i, 0 );
555                                 
556                                 if( connection_write( i ) < 0 ) { 
557                                         FD_CLR( i, &readfds );
558                                         slapd_close( i );
559                                 }
560                         }
561                 }
562 #endif
563
564 #ifdef HAVE_WINSOCK
565                 for ( i = 0; i < readfds.fd_count; i++ ) {
566                         int rd = readfds.fd_array[i];
567                         if ( rd == tcps ) {
568                                 continue;
569                         }
570                         Debug ( LDAP_DEBUG_CONNS,
571                                 "daemon: read activity on %d\n", rd, 0, 0 );
572
573                         ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
574                         assert( FD_ISSET( rd, &slap_daemon.sd_actives) );
575                         ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
576
577                         if ( connection_read( rd ) < 0 ) {
578                                 slapd_close( rd );
579                         }
580                 }
581 #else
582                 for ( i = 0; i < nfds; i++ ) {
583                         if ( i == tcps ) {
584                                 continue;
585                         }
586
587                         if ( FD_ISSET( i, &readfds ) ) {
588                                 Debug( LDAP_DEBUG_CONNS,
589                                     "daemon: read activity on %d\n", i, 0, 0 );
590
591                                 assert( FD_ISSET( i, &slap_daemon.sd_actives) );
592
593                                 if( connection_read( i ) < 0) {
594                                         slapd_close( i );
595                                 }
596                         }
597                 }
598 #endif
599                 ldap_pvt_thread_yield();
600         }
601
602         if( slapd_shutdown > 0 ) {
603                 Debug( LDAP_DEBUG_TRACE,
604                         "daemon: shutdown requested (%d) and initiated.\n",
605                         (int) slapd_shutdown, 0, 0 );
606
607         } else if ( slapd_shutdown < 0 ) {
608                 Debug( LDAP_DEBUG_TRACE,
609                         "daemon: abnormal condition (%d), shutdown initiated.\n",
610                         (int) slapd_shutdown, 0, 0 );
611         } else {
612                 Debug( LDAP_DEBUG_TRACE,
613                         "daemon: no active streams, shutdown initiated.\n",
614                         0, 0, 0 );
615         }
616
617         if( tcps >= 0 ) {
618                 tcp_close( tcps );
619         }
620
621         /* we only implement "quick" shutdown */
622         connections_shutdown();
623
624         ldap_pvt_thread_mutex_lock( &active_threads_mutex );
625         Debug( LDAP_DEBUG_ANY,
626             "slapd shutdown: waiting for %d threads to terminate\n",
627             active_threads, 0, 0 );
628         while ( active_threads > 0 ) {
629                 ldap_pvt_thread_cond_wait(&active_threads_cond, &active_threads_mutex);
630         }
631         ldap_pvt_thread_mutex_unlock( &active_threads_mutex );
632
633         slapd_listener = 0;
634         return NULL;
635 }
636
637 int slapd_daemon( int inetd, int tcps )
638 {
639         int rc;
640         int *args = ch_malloc( sizeof( int[2] ) );
641         args[0] = inetd;
642         args[1] = tcps;
643
644     if ( !daemon_initialized ) sockinit();
645
646         connections_init();
647
648 #define SLAPD_LISTENER_THREAD 1
649 #if defined( SLAPD_LISTENER_THREAD ) || !defined(HAVE_PTHREADS)
650
651         /* listener as a separate THREAD */
652         rc = ldap_pvt_thread_create( &listener_tid,
653                 0, slapd_daemon_task, args );
654
655         if ( rc != 0 ) {
656                 Debug( LDAP_DEBUG_ANY,
657                     "listener ldap_pvt_thread_create failed (%d)\n", rc, 0, 0 );
658                 goto destory;
659         }
660
661         /* wait for the listener thread to complete */
662         ldap_pvt_thread_join( listener_tid, (void *) NULL );
663 #else
664         /* expermimental code */
665         listener_tid = pthread_self();
666         slapd_daemon_task( args );
667 #endif
668
669         rc = 0;
670
671 destory:
672         connections_destroy();
673
674 #ifdef HAVE_WINSOCK
675     WSACleanup( );
676 #endif
677
678         return rc;
679 }
680
681 #ifdef HAVE_WINSOCK2
682 void sockinit()
683 {
684     WORD wVersionRequested;
685         WSADATA wsaData;
686         int err;
687  
688         wVersionRequested = MAKEWORD( 2, 0 );
689  
690         err = WSAStartup( wVersionRequested, &wsaData );
691         if ( err != 0 ) {
692                 /* Tell the user that we couldn't find a usable */
693                 /* WinSock DLL.                                  */
694                 return;
695         }
696  
697         /* Confirm that the WinSock DLL supports 2.0.*/
698         /* Note that if the DLL supports versions greater    */
699         /* than 2.0 in addition to 2.0, it will still return */
700         /* 2.0 in wVersion since that is the version we      */
701         /* requested.                                        */
702  
703         if ( LOBYTE( wsaData.wVersion ) != 2 ||
704                 HIBYTE( wsaData.wVersion ) != 0 )
705         {
706             /* Tell the user that we couldn't find a usable */
707             /* WinSock DLL.                                  */
708             WSACleanup( );
709             return; 
710         }
711     daemon_initialized = 1;
712 }       /* The WinSock DLL is acceptable. Proceed. */
713
714 void hit_socket( void )
715 {
716         int s, on = 1;
717         extern struct sockaddr_in       bind_addr;
718
719         /* throw something at the socket to terminate the select() in the daemon thread. */
720         if (( s = socket( AF_INET, SOCK_STREAM, 0 )) == INVALID_SOCKET )
721                 Debug( LDAP_DEBUG_TRACE,
722                         "slap_set_shutdown: socket failed\n\tWSAGetLastError=%d (%s)\n",
723                         WSAGetLastError(), WSAGetLastErrorString(), 0 );
724
725         if ( ioctlsocket( s, FIONBIO, &on ) == -1 ) 
726                 Debug( LDAP_DEBUG_TRACE,
727                         "slap_set_shutdown:FIONBIO ioctl on %d faled\n\tWSAGetLastError=%d (%s)\n",
728                         s, WSAGetLastError(), WSAGetLastError() );
729
730         bind_addr.sin_addr.s_addr = htonl( INADDR_LOOPBACK );
731
732         if ( connect( s, (struct sockaddr *)&bind_addr, sizeof( struct sockaddr_in )) == SOCKET_ERROR ) {
733                 /* we can probably expect some error to occur here, mostly WSAEWOULDBLOCK */
734         }
735
736         tcp_close(s);
737 }
738
739 #elif HAVE_WINSOCK
740 void sockinit()
741 {       WSADATA wsaData;
742         if ( WSAStartup( 0x0101, &wsaData ) != 0 ) {
743             return( NULL );
744         }
745     daemon_initialized = 1;
746 }
747 #else
748 void sockinit()
749 {
750     daemon_initialized = 1;
751     return;
752 }
753 #endif
754
755 void
756 slap_set_shutdown( int sig )
757 {
758         slapd_shutdown = sig;
759 #ifndef HAVE_WINSOCK
760         if(slapd_listener) {
761                 ldap_pvt_thread_kill( listener_tid, LDAP_SIGUSR1 );
762         }
763 #else
764         hit_socket();
765 #endif
766         /* reinstall self */
767         (void) SIGNAL( sig, slap_set_shutdown );
768 }
769
770 void
771 slap_do_nothing( int sig )
772 {
773         /* reinstall self */
774         (void) SIGNAL( sig, slap_do_nothing );
775 }