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