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