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