]> git.sur5r.net Git - openldap/blob - servers/slapd/main.c
import error message fix from HEAD
[openldap] / servers / slapd / main.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1998-2004 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         {
386                 char *logName;
387 #ifdef HAVE_EBCDIC
388                 logName = ch_strdup( serverName );
389                 __atoe( logName );
390 #else
391                 logName = serverName;
392 #endif
393
394 #ifdef LOG_LOCAL4
395                 openlog( logName, OPENLOG_OPTIONS, syslogUser );
396 #elif LOG_DEBUG
397                 openlog( logName, OPENLOG_OPTIONS );
398 #endif
399 #ifdef HAVE_EBCDIC
400                 free( logName );
401 #endif
402         }
403
404 #ifdef NEW_LOGGING
405         LDAP_LOG( SLAPD, INFO, "%s", Versionstr, 0, 0 );
406 #else
407         Debug( LDAP_DEBUG_ANY, "%s", Versionstr, 0, 0 );
408 #endif
409
410         if( !check_config && slapd_daemon_init( urls ) != 0 ) {
411                 rc = 1;
412                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 16 );
413                 goto stop;
414         }
415
416 #if defined(HAVE_CHROOT)
417         if ( sandbox ) {
418                 if ( chdir( sandbox ) ) {
419                         perror("chdir");
420                         rc = 1;
421                         goto stop;
422                 }
423                 if ( chroot( sandbox ) ) {
424                         perror("chroot");
425                         rc = 1;
426                         goto stop;
427                 }
428         }
429 #endif
430
431 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
432         if ( username != NULL || groupname != NULL ) {
433                 slap_init_user( username, groupname );
434         }
435 #endif
436
437         extops_init();
438         lutil_passwd_init();
439         slap_op_init();
440
441 #ifdef SLAPD_MODULES
442         if ( module_init() != 0 ) {
443                 rc = 1;
444                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 17 );
445                 goto destroy;
446         }
447 #endif
448
449         if ( slap_schema_init( ) != 0 ) {
450 #ifdef NEW_LOGGING
451                 LDAP_LOG( OPERATION, CRIT, "main: schema initialization error\n", 0, 0, 0 );
452 #else
453                 Debug( LDAP_DEBUG_ANY,
454                     "schema initialization error\n",
455                     0, 0, 0 );
456 #endif
457
458                 goto destroy;
459         }
460
461         if ( slap_init( serverMode, serverName ) != 0 ) {
462                 rc = 1;
463                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 18 );
464                 goto destroy;
465         }
466
467         if ( slap_controls_init( ) != 0 ) {
468 #ifdef NEW_LOGGING
469                 LDAP_LOG( OPERATION, CRIT, "main: controls initialization error\n", 0, 0, 0 );
470 #else
471                 Debug( LDAP_DEBUG_ANY,
472                     "controls initialization error\n",
473                     0, 0, 0 );
474 #endif
475
476                 goto destroy;
477         }
478
479 #ifdef HAVE_TLS
480         /* Library defaults to full certificate checking. This is correct when
481          * a client is verifying a server because all servers should have a
482          * valid cert. But few clients have valid certs, so we want our default
483          * to be no checking. The config file can override this as usual.
484          */
485         rc = 0;
486         (void) ldap_pvt_tls_set_option( NULL, LDAP_OPT_X_TLS_REQUIRE_CERT, &rc );
487 #endif
488
489 #ifdef LDAP_SLAPI
490         if ( slapi_int_initialize() != 0 ) {
491 #ifdef NEW_LOGGING
492                 LDAP_LOG( OPERATION, CRIT, "main: slapi initialization error\n", 0, 0, 0 );
493 #else
494                 Debug( LDAP_DEBUG_ANY,
495                     "slapi initialization error\n",
496                     0, 0, 0 );
497 #endif
498
499                 goto destroy;
500         }
501 #endif /* LDAP_SLAPI */
502
503         if ( overlay_init() ) {
504                 goto destroy;
505         }
506
507         if ( read_config( configfile, 0 ) != 0 ) {
508                 rc = 1;
509                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 19 );
510
511                 if ( check_config ) {
512                         fprintf( stderr, "config check failed\n" );
513                 }
514
515                 goto destroy;
516         }
517
518         if ( check_config ) {
519                 rc = 0;
520                 fprintf( stderr, "config check succeeded\n" );
521                 goto destroy;
522         }
523
524         if ( glue_sub_init( ) != 0 ) {
525 #ifdef NEW_LOGGING
526                 LDAP_LOG( SLAPD, CRIT, "main: subordinate config error\n", 0, 0, 0 );
527 #else
528                 Debug( LDAP_DEBUG_ANY,
529                     "subordinate config error\n",
530                     0, 0, 0 );
531 #endif
532                 goto destroy;
533         }
534
535         if ( slap_schema_check( ) != 0 ) {
536 #ifdef NEW_LOGGING
537                 LDAP_LOG( SLAPD, CRIT, "main: schema prep error\n", 0, 0, 0 );
538 #else
539                 Debug( LDAP_DEBUG_ANY,
540                     "schema prep error\n",
541                     0, 0, 0 );
542 #endif
543
544                 goto destroy;
545         }
546
547 #ifdef HAVE_TLS
548         rc = ldap_pvt_tls_init();
549         if( rc != 0) {
550 #ifdef NEW_LOGGING
551                 LDAP_LOG( SLAPD, CRIT, "main: tls init failed: %d\n", rc, 0, 0 );
552 #else
553                 Debug( LDAP_DEBUG_ANY,
554                     "main: TLS init failed: %d\n",
555                     0, 0, 0 );
556 #endif
557                 rc = 1;
558                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
559                 goto destroy;
560         }
561
562         rc = ldap_pvt_tls_init_def_ctx();
563         if( rc != 0) {
564 #ifdef NEW_LOGGING
565                 LDAP_LOG( SLAPD, CRIT, "main: tls init def ctx failed: %d\n", rc, 0, 0 );
566 #else
567                 Debug( LDAP_DEBUG_ANY,
568                     "main: TLS init def ctx failed: %d\n",
569                     rc, 0, 0 );
570 #endif
571                 rc = 1;
572                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
573                 goto destroy;
574         }
575 #endif
576
577         (void) SIGNAL( LDAP_SIGUSR1, slap_sig_wake );
578         (void) SIGNAL( LDAP_SIGUSR2, slap_sig_shutdown );
579
580 #ifdef SIGPIPE
581         (void) SIGNAL( SIGPIPE, SIG_IGN );
582 #endif
583 #ifdef SIGHUP
584         (void) SIGNAL( SIGHUP, slap_sig_shutdown );
585 #endif
586         (void) SIGNAL( SIGINT, slap_sig_shutdown );
587         (void) SIGNAL( SIGTERM, slap_sig_shutdown );
588 #ifdef LDAP_SIGCHLD
589         (void) SIGNAL( LDAP_SIGCHLD, wait4child );
590 #endif
591 #ifdef SIGBREAK
592         /* SIGBREAK is generated when Ctrl-Break is pressed. */
593         (void) SIGNAL( SIGBREAK, slap_sig_shutdown );
594 #endif
595
596 #ifndef HAVE_WINSOCK
597         lutil_detach( no_detach, 0 );
598 #endif /* HAVE_WINSOCK */
599
600 #ifdef CSRIMALLOC
601         mal_leaktrace(1);
602 #endif
603
604         /*
605          * FIXME: moved here from slapd_daemon_task()
606          * because back-monitor db_open() needs it
607          */
608         time( &starttime );
609
610         if ( slap_startup( NULL )  != 0 ) {
611                 rc = 1;
612                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 21 );
613                 goto shutdown;
614         }
615
616 #ifdef NEW_LOGGING
617         LDAP_LOG( SLAPD, INFO, "main: slapd starting.\n", 0, 0, 0 );
618 #else
619         Debug( LDAP_DEBUG_ANY, "slapd starting\n", 0, 0, 0 );
620 #endif
621
622
623         if ( slapd_pid_file != NULL ) {
624                 FILE *fp = fopen( slapd_pid_file, "w" );
625
626                 if( fp != NULL ) {
627                         fprintf( fp, "%d\n", (int) getpid() );
628                         fclose( fp );
629
630                 } else {
631                         free(slapd_pid_file);
632                         slapd_pid_file = NULL;
633                 }
634         }
635
636         if ( slapd_args_file != NULL ) {
637                 FILE *fp = fopen( slapd_args_file, "w" );
638
639                 if( fp != NULL ) {
640                         for ( i = 0; i < g_argc; i++ ) {
641                                 fprintf( fp, "%s ", g_argv[i] );
642                         }
643                         fprintf( fp, "\n" );
644                         fclose( fp );
645                 } else {
646                         free(slapd_args_file);
647                         slapd_args_file = NULL;
648                 }
649         }
650
651 #ifdef HAVE_NT_EVENT_LOG
652         if (is_NT_Service)
653         lutil_LogStartedEvent( serverName, slap_debug, configfile, urls );
654 #endif
655
656         rc = slapd_daemon();
657
658 #ifdef HAVE_NT_SERVICE_MANAGER
659         /* Throw away the event that we used during the startup process. */
660         if ( is_NT_Service )
661                 ldap_pvt_thread_cond_destroy( &started_event );
662 #endif
663
664 shutdown:
665         /* remember an error during shutdown */
666         rc |= slap_shutdown( NULL );
667
668 destroy:
669         /* remember an error during destroy */
670         rc |= slap_destroy();
671
672         while ( !LDAP_STAILQ_EMPTY( &slap_sync_cookie )) {
673                 scp = LDAP_STAILQ_FIRST( &slap_sync_cookie );
674                 LDAP_STAILQ_REMOVE_HEAD( &slap_sync_cookie, sc_next );
675                 ch_free( scp );
676         }
677
678 #ifdef SLAPD_MODULES
679         module_kill();
680 #endif
681
682         slap_op_destroy();
683
684         extops_kill();
685
686 stop:
687 #ifdef HAVE_NT_EVENT_LOG
688         if (is_NT_Service)
689         lutil_LogStoppedEvent( serverName );
690 #endif
691
692 #ifdef NEW_LOGGING
693         LDAP_LOG( SLAPD, CRIT, "main: slapd stopped.\n", 0, 0, 0 );
694 #else
695         Debug( LDAP_DEBUG_ANY, "slapd stopped.\n", 0, 0, 0 );
696 #endif
697
698
699 #ifdef HAVE_NT_SERVICE_MANAGER
700         lutil_ReportShutdownComplete();
701 #endif
702
703 #ifdef LOG_DEBUG
704     closelog();
705 #endif
706         slapd_daemon_destroy();
707
708         controls_destroy();
709
710         schema_destroy();
711
712         lutil_passwd_destroy();
713
714 #ifdef HAVE_TLS
715         ldap_pvt_tls_destroy();
716 #endif
717
718         if ( slapd_pid_file != NULL ) {
719                 unlink( slapd_pid_file );
720         }
721         if ( slapd_args_file != NULL ) {
722                 unlink( slapd_args_file );
723         }
724
725         config_destroy();
726
727 #ifdef CSRIMALLOC
728         mal_dumpleaktrace( leakfile );
729 #endif
730
731         MAIN_RETURN(rc);
732 }
733
734
735 #ifdef LDAP_SIGCHLD
736
737 /*
738  *  Catch and discard terminated child processes, to avoid zombies.
739  */
740
741 static RETSIGTYPE
742 wait4child( int sig )
743 {
744     int save_errno = errno;
745
746 #ifdef WNOHANG
747     errno = 0;
748 #ifdef HAVE_WAITPID
749     while ( waitpid( (pid_t)-1, NULL, WNOHANG ) > 0 || errno == EINTR )
750         ;       /* NULL */
751 #else
752     while ( wait3( NULL, WNOHANG, NULL ) > 0 || errno == EINTR )
753         ;       /* NULL */
754 #endif
755 #else
756     (void) wait( NULL );
757 #endif
758     (void) SIGNAL_REINSTALL( sig, wait4child );
759     errno = save_errno;
760 }
761
762 #endif /* LDAP_SIGCHLD */
763
764
765 #ifdef LOG_LOCAL4
766
767 /*
768  *  Convert a string to an integer by means of a dispatcher table
769  *  if the string is not in the table return the default
770  */
771
772 static int
773 cnvt_str2int( char *stringVal, STRDISP_P dispatcher, int defaultVal )
774 {
775     int        retVal = defaultVal;
776     STRDISP_P  disp;
777
778     for (disp = dispatcher; disp->stringVal; disp++) {
779
780         if (!strncasecmp (stringVal, disp->stringVal, disp->abbr)) {
781
782             retVal = disp->intVal;
783             break;
784
785         }
786     }
787
788     return (retVal);
789 }
790
791 #endif  /* LOG_LOCAL4 */