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