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