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