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