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