]> git.sur5r.net Git - openldap/blob - servers/slapd/main.c
Updated notices
[openldap] / servers / slapd / main.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1998-2003 The OpenLDAP Foundation.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted only as authorized by the OpenLDAP
9  * Public License.
10  *
11  * A copy of this license is available in the file LICENSE in the
12  * top-level directory of the distribution or, alternatively, at
13  * <http://www.OpenLDAP.org/license.html>.
14  */
15 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
16  * All rights reserved.
17  *
18  * Redistribution and use in source and binary forms are permitted
19  * provided that this notice is preserved and that due credit is given
20  * to the University of Michigan at Ann Arbor. The name of the University
21  * may not be used to endorse or promote products derived from this
22  * software without specific prior written permission. This software
23  * is provided ``as is'' without express or implied warranty.
24  */
25
26 #include "portable.h"
27
28 #include <stdio.h>
29
30 #include <ac/socket.h>
31 #include <ac/string.h>
32 #include <ac/time.h>
33 #include <ac/unistd.h>
34 #include <ac/wait.h>
35 #include <ac/errno.h>
36
37 #include "ldap_pvt.h"
38
39 #include "slap.h"
40 #include "lutil.h"
41 #include "ldif.h"
42
43 #ifdef LDAP_SLAPI
44 #include "slapi.h"
45 #endif
46
47 #ifdef LDAP_SIGCHLD
48 static RETSIGTYPE wait4child( int sig );
49 #endif
50
51 #ifdef HAVE_NT_SERVICE_MANAGER
52 #define MAIN_RETURN(x) return
53 static struct sockaddr_in       bind_addr;
54
55 #define SERVICE_EXIT( e, n )    do { \
56         if ( is_NT_Service ) { \
57                 lutil_ServiceStatus.dwWin32ExitCode                             = (e); \
58                 lutil_ServiceStatus.dwServiceSpecificExitCode   = (n); \
59         } \
60 } while ( 0 )
61
62 #else
63 #define SERVICE_EXIT( e, n )
64 #define MAIN_RETURN(x) return(x)
65 #endif
66
67 /*
68  * when more than one slapd is running on one machine, each one might have
69  * it's own LOCAL for syslogging and must have its own pid/args files
70  */
71
72 #ifndef HAVE_MKVERSION
73 const char Versionstr[] =
74         OPENLDAP_PACKAGE " " OPENLDAP_VERSION " Standalone LDAP Server (slapd)";
75 #endif
76
77 #ifdef LOG_LOCAL4
78
79 #define DEFAULT_SYSLOG_USER  LOG_LOCAL4
80
81 typedef struct _str2intDispatch {
82         char    *stringVal;
83         int      abbr;
84         int      intVal;
85 } STRDISP, *STRDISP_P;
86
87
88 /* table to compute syslog-options to integer */
89 static STRDISP  syslog_types[] = {
90         { "LOCAL0", sizeof("LOCAL0"), LOG_LOCAL0 },
91         { "LOCAL1", sizeof("LOCAL1"), LOG_LOCAL1 },
92         { "LOCAL2", sizeof("LOCAL2"), LOG_LOCAL2 },
93         { "LOCAL3", sizeof("LOCAL3"), LOG_LOCAL3 },
94         { "LOCAL4", sizeof("LOCAL4"), LOG_LOCAL4 },
95         { "LOCAL5", sizeof("LOCAL5"), LOG_LOCAL5 },
96         { "LOCAL6", sizeof("LOCAL6"), LOG_LOCAL6 },
97         { "LOCAL7", sizeof("LOCAL7"), LOG_LOCAL7 },
98         { NULL, 0, 0 }
99 };
100
101 static int   cnvt_str2int( char *, STRDISP_P, int );
102
103 #endif  /* LOG_LOCAL4 */
104
105 static int check_config = 0;
106
107 static void
108 usage( char *name )
109 {
110         fprintf( stderr,
111                 "usage: %s options\n", name );
112         fprintf( stderr,
113                 "\t-4\t\tIPv4 only\n"
114                 "\t-6\t\tIPv6 only\n"
115                 "\t-c cookie\tSync cookie of consumer\n"
116                 "\t-d level\tDebug level" "\n"
117                 "\t-f filename\tConfiguration file\n"
118 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
119                 "\t-g group\tGroup (id or name) to run as\n"
120 #endif
121                 "\t-h URLs\t\tList of URLs to serve\n"
122 #ifdef LOG_LOCAL4
123                 "\t-l facility\tSyslog facility (default: LOCAL4)\n"
124 #endif
125                 "\t-n serverName\tService name\n"
126 #ifdef HAVE_CHROOT
127                 "\t-r directory\tSandbox directory to chroot to\n"
128 #endif
129                 "\t-s level\tSyslog level\n"
130                 "\t-t\t\tCheck configuration file and exit\n"
131 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
132                 "\t-u user\t\tUser (id or name) to run as\n"
133 #endif
134     );
135 }
136
137 #ifdef HAVE_NT_SERVICE_MANAGER
138 void WINAPI ServiceMain( DWORD argc, LPTSTR *argv )
139 #else
140 int main( int argc, char **argv )
141 #endif
142 {
143         int             i, no_detach = 0;
144         int             rc = 1;
145         char *urls = NULL;
146 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
147         char *username = NULL;
148         char *groupname = NULL;
149 #endif
150 #if defined(HAVE_CHROOT)
151         char *sandbox = NULL;
152 #endif
153 #ifdef LOG_LOCAL4
154     int     syslogUser = DEFAULT_SYSLOG_USER;
155 #endif
156         
157         int g_argc = argc;
158         char **g_argv = argv;
159
160 #ifdef HAVE_NT_SERVICE_MANAGER
161         char            *configfile = ".\\slapd.conf";
162 #else
163         char            *configfile = SLAPD_DEFAULT_CONFIGFILE;
164 #endif
165         char        *serverName = NULL;
166         int         serverMode = SLAP_SERVER_MODE;
167
168         struct berval cookie = { 0, NULL };
169         struct sync_cookie *scp = NULL;
170         struct sync_cookie *scp_entry = NULL;
171
172 #ifdef CSRIMALLOC
173         FILE *leakfile;
174         if( ( leakfile = fopen( "slapd.leak", "w" )) == NULL ) {
175                 leakfile = stderr;
176         }
177 #endif
178
179         sl_mem_init();
180
181 #ifdef HAVE_NT_SERVICE_MANAGER
182         {
183                 int *i;
184                 char *newConfigFile;
185                 char *newUrls;
186                 char *regService = NULL;
187
188                 if ( is_NT_Service ) {
189                         serverName = argv[0];
190                         lutil_CommenceStartupProcessing( serverName, slap_sig_shutdown );
191                         if ( strcmp(serverName, SERVICE_NAME) )
192                             regService = serverName;
193                 }
194
195                 i = (int*)lutil_getRegParam( regService, "DebugLevel" );
196                 if ( i != NULL ) 
197                 {
198                         slap_debug = *i;
199 #ifdef NEW_LOGGING
200                         lutil_log_initialize( argc, argv );
201                         LDAP_LOG( SLAPD, INFO, 
202                                 "main: new debug level from registry is: %d\n", 
203                                 slap_debug, 0, 0 );
204 #else
205                         Debug( LDAP_DEBUG_ANY, "new debug level from registry is: %d\n", slap_debug, 0, 0 );
206 #endif
207                 }
208
209                 newUrls = (char *) lutil_getRegParam(regService, "Urls");
210                 if (newUrls)
211                 {
212                     if (urls)
213                         ch_free(urls);
214
215                     urls = ch_strdup(newUrls);
216 #ifdef NEW_LOGGING
217                     LDAP_LOG( SLAPD, INFO, 
218                                 "main: new urls from registry: %s\n", urls, 0, 0 );
219 #else
220                     Debug(LDAP_DEBUG_ANY, "new urls from registry: %s\n",
221                           urls, 0, 0);
222 #endif
223
224                 }
225
226                 newConfigFile = (char*)lutil_getRegParam( regService, "ConfigFile" );
227                 if ( newConfigFile != NULL ) 
228                 {
229                         configfile = newConfigFile;
230 #ifdef NEW_LOGGING
231                         LDAP_LOG( SLAPD, INFO, 
232                                 "main: new config file from registry is: %s\n", configfile, 0, 0 );
233 #else
234                         Debug ( LDAP_DEBUG_ANY, "new config file from registry is: %s\n", configfile, 0, 0 );
235 #endif
236
237                 }
238         }
239 #endif
240
241         while ( (i = getopt( argc, argv,
242                              "c:d:f:h:s:n:t"
243 #if LDAP_PF_INET6
244                                 "46"
245 #endif
246 #ifdef HAVE_CHROOT
247                                 "r:"
248 #endif
249 #ifdef LOG_LOCAL4
250                              "l:"
251 #endif
252 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
253                              "u:g:"
254 #endif
255                              )) != EOF ) {
256                 switch ( i ) {
257 #ifdef LDAP_PF_INET6
258                 case '4':
259                         slap_inet4or6 = AF_INET;
260                         break;
261                 case '6':
262                         slap_inet4or6 = AF_INET6;
263                         break;
264 #endif
265
266                 case 'h':       /* listen URLs */
267                         if ( urls != NULL ) free( urls );
268                         urls = ch_strdup( optarg );
269                         break;
270
271                 case 'c':       /* provide sync cookie, override if exist in replica */
272                         scp = (struct sync_cookie *) ch_calloc( 1,
273                                                                                 sizeof( struct sync_cookie ));
274                         ber_str2bv( optarg, strlen( optarg ), 1, &cookie );
275                         ber_bvarray_add( &scp->octet_str, &cookie );
276                         slap_parse_sync_cookie( scp );
277
278                         LDAP_STAILQ_FOREACH( scp_entry, &slap_sync_cookie, sc_next ) {
279                                 if ( scp->rid == scp_entry->rid ) {
280 #ifdef NEW_LOGGING
281                                         LDAP_LOG( OPERATION, CRIT,
282                                                         "main: duplicated replica id in cookies\n",
283                                                         0, 0, 0 );
284 #else
285                                         Debug( LDAP_DEBUG_ANY,
286                                                     "main: duplicated replica id in cookies\n",
287                                                         0, 0, 0 );
288 #endif
289                                         slap_sync_cookie_free( scp, 1 );
290                                         goto destroy;
291                                 }
292                         }
293                         LDAP_STAILQ_INSERT_TAIL( &slap_sync_cookie, scp, sc_next );
294                         break;
295
296                 case 'd':       /* set debug level and 'do not detach' flag */
297                         no_detach = 1;
298 #ifdef LDAP_DEBUG
299                         slap_debug |= atoi( optarg );
300 #else
301                         if ( atoi( optarg ) != 0 )
302                                 fputs( "must compile with LDAP_DEBUG for debugging\n",
303                                        stderr );
304 #endif
305                         break;
306
307                 case 'f':       /* read config file */
308                         configfile = ch_strdup( optarg );
309                         break;
310
311                 case 's':       /* set syslog level */
312                         ldap_syslog = atoi( optarg );
313                         break;
314
315 #ifdef LOG_LOCAL4
316                 case 'l':       /* set syslog local user */
317                         syslogUser = cnvt_str2int( optarg,
318                                 syslog_types, DEFAULT_SYSLOG_USER );
319                         break;
320 #endif
321
322 #ifdef HAVE_CHROOT
323                 case 'r':
324                         if( sandbox ) free(sandbox);
325                         sandbox = ch_strdup( optarg );
326                         break;
327 #endif
328
329 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
330                 case 'u':       /* user name */
331                         if( username ) free(username);
332                         username = ch_strdup( optarg );
333                         break;
334
335                 case 'g':       /* group name */
336                         if( groupname ) free(groupname);
337                         groupname = ch_strdup( optarg );
338                         break;
339 #endif /* SETUID && GETUID */
340
341                 case 'n':  /* NT service name */
342                         if( serverName != NULL ) free( serverName );
343                         serverName = ch_strdup( optarg );
344                         break;
345
346                 case 't':
347                         check_config++;
348                         break;
349
350                 default:
351                         usage( argv[0] );
352                         rc = 1;
353                         SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 15 );
354                         goto stop;
355                 }
356         }
357
358 #ifdef NEW_LOGGING
359         lutil_log_initialize( argc, argv );
360 #else
361         lutil_set_debug_level( "slapd", slap_debug );
362         ber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &slap_debug);
363         ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &slap_debug);
364         ldif_debug = slap_debug;
365 #endif
366
367 #ifdef NEW_LOGGING
368         LDAP_LOG( SLAPD, INFO, "%s", Versionstr, 0, 0 );
369 #else
370         Debug( LDAP_DEBUG_TRACE, "%s", Versionstr, 0, 0 );
371 #endif
372
373         if( serverName == NULL ) {
374                 if ( (serverName = strrchr( argv[0], *LDAP_DIRSEP )) == NULL ) {
375                         serverName = argv[0];
376                 } else {
377                         serverName = serverName + 1;
378                 }
379         }
380
381 #ifdef LOG_LOCAL4
382         openlog( serverName, OPENLOG_OPTIONS, syslogUser );
383 #elif LOG_DEBUG
384         openlog( serverName, OPENLOG_OPTIONS );
385 #endif
386
387         if( !check_config && slapd_daemon_init( urls ) != 0 ) {
388                 rc = 1;
389                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 16 );
390                 goto stop;
391         }
392
393 #if defined(HAVE_CHROOT)
394         if ( sandbox ) {
395                 if ( chdir( sandbox ) ) {
396                         perror("chdir");
397                         rc = 1;
398                         goto stop;
399                 }
400                 if ( chroot( sandbox ) ) {
401                         perror("chroot");
402                         rc = 1;
403                         goto stop;
404                 }
405         }
406 #endif
407
408 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
409         if ( username != NULL || groupname != NULL ) {
410                 slap_init_user( username, groupname );
411         }
412 #endif
413
414         extops_init();
415         lutil_passwd_init();
416         slap_op_init();
417
418 #ifdef SLAPD_MODULES
419         if ( module_init() != 0 ) {
420                 rc = 1;
421                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 17 );
422                 goto destroy;
423         }
424 #endif
425
426         if ( slap_schema_init( ) != 0 ) {
427 #ifdef NEW_LOGGING
428                 LDAP_LOG( OPERATION, CRIT, "main: schema initialization error\n", 0, 0, 0 );
429 #else
430                 Debug( LDAP_DEBUG_ANY,
431                     "schema initialization error\n",
432                     0, 0, 0 );
433 #endif
434
435                 goto destroy;
436         }
437
438         if ( slap_init( serverMode, serverName ) != 0 ) {
439                 rc = 1;
440                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 18 );
441                 goto destroy;
442         }
443
444         if ( slap_controls_init( ) != 0 ) {
445 #ifdef NEW_LOGGING
446                 LDAP_LOG( OPERATION, CRIT, "main: controls initialization error\n", 0, 0, 0 );
447 #else
448                 Debug( LDAP_DEBUG_ANY,
449                     "controls initialization error\n",
450                     0, 0, 0 );
451 #endif
452
453                 goto destroy;
454         }
455
456 #ifdef HAVE_TLS
457         /* Library defaults to full certificate checking. This is correct when
458          * a client is verifying a server because all servers should have a
459          * valid cert. But few clients have valid certs, so we want our default
460          * to be no checking. The config file can override this as usual.
461          */
462         rc = 0;
463         (void) ldap_pvt_tls_set_option( NULL, LDAP_OPT_X_TLS_REQUIRE_CERT, &rc );
464 #endif
465
466 #ifdef LDAP_SLAPI
467         if ( slapi_init() != 0 ) {
468 #ifdef NEW_LOGGING
469                 LDAP_LOG( OPERATION, CRIT, "main: slapi initialization error\n", 0, 0, 0 );
470 #else
471                 Debug( LDAP_DEBUG_ANY,
472                     "slapi initialization error\n",
473                     0, 0, 0 );
474 #endif
475
476                 goto destroy;
477         }
478 #endif /* LDAP_SLAPI */
479
480         if ( read_config( configfile, 0 ) != 0 ) {
481                 rc = 1;
482                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 19 );
483
484                 if ( check_config ) {
485                         fprintf( stderr, "config check failed\n" );
486                 }
487
488                 goto destroy;
489         }
490
491         if ( check_config ) {
492                 rc = 0;
493                 fprintf( stderr, "config check succeeded\n" );
494                 goto destroy;
495         }
496
497         if ( glue_sub_init( ) != 0 ) {
498 #ifdef NEW_LOGGING
499                 LDAP_LOG( SLAPD, CRIT, "main: subordinate config error\n", 0, 0, 0 );
500 #else
501                 Debug( LDAP_DEBUG_ANY,
502                     "subordinate config error\n",
503                     0, 0, 0 );
504 #endif
505                 goto destroy;
506         }
507
508         if ( slap_schema_check( ) != 0 ) {
509 #ifdef NEW_LOGGING
510                 LDAP_LOG( SLAPD, CRIT, "main: schema prep error\n", 0, 0, 0 );
511 #else
512                 Debug( LDAP_DEBUG_ANY,
513                     "schema prep error\n",
514                     0, 0, 0 );
515 #endif
516
517                 goto destroy;
518         }
519
520 #ifdef HAVE_TLS
521         rc = ldap_pvt_tls_init();
522         if( rc != 0) {
523 #ifdef NEW_LOGGING
524                 LDAP_LOG( SLAPD, CRIT, "main: tls init failed: %d\n", rc, 0, 0 );
525 #else
526                 Debug( LDAP_DEBUG_ANY,
527                     "main: TLS init failed: %d\n",
528                     0, 0, 0 );
529 #endif
530                 rc = 1;
531                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
532                 goto destroy;
533         }
534
535         rc = ldap_pvt_tls_init_def_ctx();
536         if( rc != 0) {
537 #ifdef NEW_LOGGING
538                 LDAP_LOG( SLAPD, CRIT, "main: tls init def ctx failed: %d\n", rc, 0, 0 );
539 #else
540                 Debug( LDAP_DEBUG_ANY,
541                     "main: TLS init def ctx failed: %d\n",
542                     rc, 0, 0 );
543 #endif
544                 rc = 1;
545                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
546                 goto destroy;
547         }
548 #endif
549
550         (void) SIGNAL( LDAP_SIGUSR1, slap_sig_wake );
551         (void) SIGNAL( LDAP_SIGUSR2, slap_sig_shutdown );
552
553 #ifdef SIGPIPE
554         (void) SIGNAL( SIGPIPE, SIG_IGN );
555 #endif
556 #ifdef SIGHUP
557         (void) SIGNAL( SIGHUP, slap_sig_shutdown );
558 #endif
559         (void) SIGNAL( SIGINT, slap_sig_shutdown );
560         (void) SIGNAL( SIGTERM, slap_sig_shutdown );
561 #ifdef LDAP_SIGCHLD
562         (void) SIGNAL( LDAP_SIGCHLD, wait4child );
563 #endif
564 #ifdef SIGBREAK
565         /* SIGBREAK is generated when Ctrl-Break is pressed. */
566         (void) SIGNAL( SIGBREAK, slap_sig_shutdown );
567 #endif
568
569 #ifndef HAVE_WINSOCK
570         lutil_detach( no_detach, 0 );
571 #endif /* HAVE_WINSOCK */
572
573 #ifdef CSRIMALLOC
574         mal_leaktrace(1);
575 #endif
576
577         /*
578          * FIXME: moved here from slapd_daemon_task()
579          * because back-monitor db_open() needs it
580          */
581         time( &starttime );
582
583         if ( slap_startup( NULL )  != 0 ) {
584                 rc = 1;
585                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 21 );
586                 goto shutdown;
587         }
588
589 #ifdef NEW_LOGGING
590         LDAP_LOG( SLAPD, INFO, "main: slapd starting.\n", 0, 0, 0 );
591 #else
592         Debug( LDAP_DEBUG_ANY, "slapd starting\n", 0, 0, 0 );
593 #endif
594
595
596         if ( slapd_pid_file != NULL ) {
597                 FILE *fp = fopen( slapd_pid_file, "w" );
598
599                 if( fp != NULL ) {
600                         fprintf( fp, "%d\n", (int) getpid() );
601                         fclose( fp );
602
603                 } else {
604                         free(slapd_pid_file);
605                         slapd_pid_file = NULL;
606                 }
607         }
608
609         if ( slapd_args_file != NULL ) {
610                 FILE *fp = fopen( slapd_args_file, "w" );
611
612                 if( fp != NULL ) {
613                         for ( i = 0; i < g_argc; i++ ) {
614                                 fprintf( fp, "%s ", g_argv[i] );
615                         }
616                         fprintf( fp, "\n" );
617                         fclose( fp );
618                 } else {
619                         free(slapd_args_file);
620                         slapd_args_file = NULL;
621                 }
622         }
623
624 #ifdef HAVE_NT_EVENT_LOG
625         if (is_NT_Service)
626         lutil_LogStartedEvent( serverName, slap_debug, configfile, urls );
627 #endif
628
629         rc = slapd_daemon();
630
631 #ifdef HAVE_NT_SERVICE_MANAGER
632         /* Throw away the event that we used during the startup process. */
633         if ( is_NT_Service )
634                 ldap_pvt_thread_cond_destroy( &started_event );
635 #endif
636
637 shutdown:
638         /* remember an error during shutdown */
639         rc |= slap_shutdown( NULL );
640
641 destroy:
642         /* remember an error during destroy */
643         rc |= slap_destroy();
644
645         while ( !LDAP_STAILQ_EMPTY( &slap_sync_cookie )) {
646                 scp = LDAP_STAILQ_FIRST( &slap_sync_cookie );
647                 LDAP_STAILQ_REMOVE_HEAD( &slap_sync_cookie, sc_next );
648                 ch_free( scp );
649         }
650
651 #ifdef SLAPD_MODULES
652         module_kill();
653 #endif
654
655         slap_op_destroy();
656
657         extops_kill();
658
659 stop:
660 #ifdef HAVE_NT_EVENT_LOG
661         if (is_NT_Service)
662         lutil_LogStoppedEvent( serverName );
663 #endif
664
665 #ifdef NEW_LOGGING
666         LDAP_LOG( SLAPD, CRIT, "main: slapd stopped.\n", 0, 0, 0 );
667 #else
668         Debug( LDAP_DEBUG_ANY, "slapd stopped.\n", 0, 0, 0 );
669 #endif
670
671
672 #ifdef HAVE_NT_SERVICE_MANAGER
673         lutil_ReportShutdownComplete();
674 #endif
675
676 #ifdef LOG_DEBUG
677     closelog();
678 #endif
679         slapd_daemon_destroy();
680
681         controls_destroy();
682
683         schema_destroy();
684
685         lutil_passwd_destroy();
686
687 #ifdef HAVE_TLS
688         ldap_pvt_tls_destroy();
689 #endif
690
691         if ( slapd_pid_file != NULL ) {
692                 unlink( slapd_pid_file );
693         }
694         if ( slapd_args_file != NULL ) {
695                 unlink( slapd_args_file );
696         }
697
698         config_destroy();
699
700 #ifdef CSRIMALLOC
701         mal_dumpleaktrace( leakfile );
702 #endif
703
704         MAIN_RETURN(rc);
705 }
706
707
708 #ifdef LDAP_SIGCHLD
709
710 /*
711  *  Catch and discard terminated child processes, to avoid zombies.
712  */
713
714 static RETSIGTYPE
715 wait4child( int sig )
716 {
717     int save_errno = errno;
718
719 #ifdef WNOHANG
720     errno = 0;
721 #ifdef HAVE_WAITPID
722     while ( waitpid( (pid_t)-1, NULL, WNOHANG ) > 0 || errno == EINTR )
723         ;       /* NULL */
724 #else
725     while ( wait3( NULL, WNOHANG, NULL ) > 0 || errno == EINTR )
726         ;       /* NULL */
727 #endif
728 #else
729     (void) wait( NULL );
730 #endif
731     (void) SIGNAL_REINSTALL( sig, wait4child );
732     errno = save_errno;
733 }
734
735 #endif /* LDAP_SIGCHLD */
736
737
738 #ifdef LOG_LOCAL4
739
740 /*
741  *  Convert a string to an integer by means of a dispatcher table
742  *  if the string is not in the table return the default
743  */
744
745 static int
746 cnvt_str2int( char *stringVal, STRDISP_P dispatcher, int defaultVal )
747 {
748     int        retVal = defaultVal;
749     STRDISP_P  disp;
750
751     for (disp = dispatcher; disp->stringVal; disp++) {
752
753         if (!strncasecmp (stringVal, disp->stringVal, disp->abbr)) {
754
755             retVal = disp->intVal;
756             break;
757
758         }
759     }
760
761     return (retVal);
762 }
763
764 #endif  /* LOG_LOCAL4 */