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