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