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