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