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