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