]> git.sur5r.net Git - openldap/blob - servers/slapd/main.c
unifdef -UNEW_LOGGING
[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, slapacl;
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         {"slapacl", slapacl},
83         /* NOTE: new tools must be added in chronological order,
84          * not in alphabetical order, because for backwards
85          * compatibility name[4] is used to identify the
86          * tools; so name[4]=='a' must refer to "slapadd" and
87          * not to "slapauth".  Alphabetical order can be used
88          * for tools whose name[4] is not used yet */
89         {NULL, NULL}
90 };
91
92 /*
93  * when more than one slapd is running on one machine, each one might have
94  * it's own LOCAL for syslogging and must have its own pid/args files
95  */
96
97 #ifndef HAVE_MKVERSION
98 const char Versionstr[] =
99         OPENLDAP_PACKAGE " " OPENLDAP_VERSION " Standalone LDAP Server (slapd)";
100 #endif
101
102 #ifdef LOG_LOCAL4
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 /* table to compute syslog-options to integer */
112 static STRDISP  syslog_types[] = {
113         { "LOCAL0", sizeof("LOCAL0"), LOG_LOCAL0 },
114         { "LOCAL1", sizeof("LOCAL1"), LOG_LOCAL1 },
115         { "LOCAL2", sizeof("LOCAL2"), LOG_LOCAL2 },
116         { "LOCAL3", sizeof("LOCAL3"), LOG_LOCAL3 },
117         { "LOCAL4", sizeof("LOCAL4"), LOG_LOCAL4 },
118         { "LOCAL5", sizeof("LOCAL5"), LOG_LOCAL5 },
119         { "LOCAL6", sizeof("LOCAL6"), LOG_LOCAL6 },
120         { "LOCAL7", sizeof("LOCAL7"), LOG_LOCAL7 },
121 #ifdef LOG_USER
122         { "USER", sizeof("USER"), LOG_USER },
123 #endif
124 #ifdef LOG_DAEMON
125         { "DAEMON", sizeof("DAEMON"), LOG_DAEMON },
126 #endif
127         { NULL, 0, 0 }
128 };
129
130 static int cnvt_str2int( char *, STRDISP_P, int );
131 #endif  /* LOG_LOCAL4 */
132
133 #define CHECK_NONE      0x00
134 #define CHECK_CONFIG    0x01
135 static int check = CHECK_NONE;
136 static int version = 0;
137
138 void *slap_tls_ctx;
139
140 static int
141 slapd_opt_slp( const char *val, void *arg )
142 {
143 #ifdef HAVE_SLP
144         /* NULL is default */
145         if ( val == NULL || strcasecmp( val, "on" ) == 0 ) {
146                 slapd_register_slp = 1;
147
148         } else if ( strcasecmp( val, "off" ) == 0 ) {
149                 slapd_register_slp = 0;
150
151         /* NOTE: add support for URL specification? */
152
153         } else {
154                 fprintf(stderr, "unrecognized value \"%s\" for SLP option\n", val );
155                 return -1;
156         }
157
158         return 0;
159                 
160 #else
161         fputs( "slapd: SLP support is not available\n", stderr );
162         return 0;
163 #endif
164 }
165
166 /*
167  * Option helper structure:
168  * 
169  * oh_nam       is left-hand part of <option>[=<value>]
170  * oh_fnc       is handler function
171  * oh_arg       is an optional arg to oh_fnc
172  * oh_usage     is the one-line usage string related to the option,
173  *              which is assumed to start with <option>[=<value>]
174  *
175  * please leave valid options in the structure, and optionally #ifdef
176  * their processing inside the helper, so that reasonable and helpful
177  * error messages can be generated if a disabled option is requested.
178  */
179 struct option_helper {
180         struct berval   oh_name;
181         int             (*oh_fnc)(const char *val, void *arg);
182         void            *oh_arg;
183         const char      *oh_usage;
184 } option_helpers[] = {
185         { BER_BVC("slp"),       slapd_opt_slp,  NULL, "slp[={on|off}] enable/disable SLP" },
186         { BER_BVNULL }
187 };
188
189 static void
190 usage( char *name )
191 {
192         fprintf( stderr,
193                 "usage: %s options\n", name );
194         fprintf( stderr,
195                 "\t-4\t\tIPv4 only\n"
196                 "\t-6\t\tIPv6 only\n"
197                 "\t-T {add|auth|cat|dn|index|passwd|test}\n"
198                 "\t\t\tRun in Tool mode\n"
199                 "\t-c cookie\tSync cookie of consumer\n"
200                 "\t-d level\tDebug level" "\n"
201                 "\t-f filename\tConfiguration file\n"
202 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
203                 "\t-g group\tGroup (id or name) to run as\n"
204 #endif
205                 "\t-h URLs\t\tList of URLs to serve\n"
206 #ifdef LOG_LOCAL4
207                 "\t-l facility\tSyslog facility (default: LOCAL4)\n"
208 #endif
209                 "\t-n serverName\tService name\n"
210                 "\t-o <opt>[=val] generic means to specify options" );
211         if ( !BER_BVISNULL( &option_helpers[0].oh_name ) ) {
212                 int     i;
213
214                 fprintf( stderr, "; supported options:\n" );
215                 for ( i = 0; !BER_BVISNULL( &option_helpers[i].oh_name ); i++) {
216                         fprintf( stderr, "\t\t%s\n", option_helpers[i].oh_usage );
217                 }
218         } else {
219                 fprintf( stderr, "\n" );
220         }
221         fprintf( stderr,        
222 #ifdef HAVE_CHROOT
223                 "\t-r directory\tSandbox directory to chroot to\n"
224 #endif
225                 "\t-s level\tSyslog level\n"
226 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
227                 "\t-u user\t\tUser (id or name) to run as\n"
228 #endif
229                 "\t-V\t\tprint version info (-VV only)\n"
230     );
231 }
232
233 #ifdef HAVE_NT_SERVICE_MANAGER
234 void WINAPI ServiceMain( DWORD argc, LPTSTR *argv )
235 #else
236 int main( int argc, char **argv )
237 #endif
238 {
239         int             i, no_detach = 0;
240         int             rc = 1;
241         char *urls = NULL;
242 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
243         char *username = NULL;
244         char *groupname = NULL;
245 #endif
246 #if defined(HAVE_CHROOT)
247         char *sandbox = NULL;
248 #endif
249 #ifdef LOG_LOCAL4
250     int     syslogUser = DEFAULT_SYSLOG_USER;
251 #endif
252         
253         int g_argc = argc;
254         char **g_argv = argv;
255
256 #ifdef HAVE_NT_SERVICE_MANAGER
257         char            *configfile = ".\\slapd.conf";
258 #else
259         char            *configfile = SLAPD_DEFAULT_CONFIGFILE;
260 #endif
261         char        *serverName;
262         int         serverMode = SLAP_SERVER_MODE;
263
264         struct berval cookie = BER_BVNULL;
265         struct sync_cookie *scp = NULL;
266         struct sync_cookie *scp_entry = NULL;
267
268 #ifdef CSRIMALLOC
269         FILE *leakfile;
270         if( ( leakfile = fopen( "slapd.leak", "w" )) == NULL ) {
271                 leakfile = stderr;
272         }
273 #endif
274         char    *serverNamePrefix = "";
275         size_t  l;
276
277         slap_sl_mem_init();
278
279         serverName = lutil_progname( "slapd", argc, argv );
280
281         if ( strcmp( serverName, "slapd" ) ) {
282                 for (i=0; tools[i].name; i++) {
283                         if ( !strcmp( serverName, tools[i].name ) ) {
284                                 rc = tools[i].func(argc, argv);
285                                 MAIN_RETURN(rc);
286                         }
287                 }
288         }
289
290 #ifdef HAVE_NT_SERVICE_MANAGER
291         {
292                 int *i;
293                 char *newConfigFile;
294                 char *newUrls;
295                 char *regService = NULL;
296
297                 if ( is_NT_Service ) {
298                         lutil_CommenceStartupProcessing( serverName, slap_sig_shutdown );
299                         if ( strcmp(serverName, SERVICE_NAME) )
300                             regService = serverName;
301                 }
302
303                 i = (int*)lutil_getRegParam( regService, "DebugLevel" );
304                 if ( i != NULL ) {
305                         slap_debug = *i;
306                         Debug( LDAP_DEBUG_ANY,
307                                 "new debug level from registry is: %d\n", slap_debug, 0, 0 );
308                 }
309
310                 newUrls = (char *) lutil_getRegParam(regService, "Urls");
311                 if (newUrls) {
312                     if (urls)
313                         ch_free(urls);
314
315                     urls = ch_strdup(newUrls);
316                     Debug(LDAP_DEBUG_ANY, "new urls from registry: %s\n",
317                                 urls, 0, 0);
318                 }
319
320                 newConfigFile = (char*)lutil_getRegParam( regService, "ConfigFile" );
321                 if ( newConfigFile != NULL ) {
322                         configfile = newConfigFile;
323                         Debug ( LDAP_DEBUG_ANY, "new config file from registry is: %s\n", configfile, 0, 0 );
324                 }
325         }
326 #endif
327
328         while ( (i = getopt( argc, argv,
329                              "c:d:f:h:n:o:s:StT:V"
330 #if LDAP_PF_INET6
331                                 "46"
332 #endif
333 #ifdef HAVE_CHROOT
334                                 "r:"
335 #endif
336 #ifdef LOG_LOCAL4
337                              "l:"
338 #endif
339 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
340                              "u:g:"
341 #endif
342                              )) != EOF ) {
343                 switch ( i ) {
344 #ifdef LDAP_PF_INET6
345                 case '4':
346                         slap_inet4or6 = AF_INET;
347                         break;
348                 case '6':
349                         slap_inet4or6 = AF_INET6;
350                         break;
351 #endif
352
353                 case 'h':       /* listen URLs */
354                         if ( urls != NULL ) free( urls );
355                         urls = ch_strdup( optarg );
356                         break;
357
358                 case 'c':       /* provide sync cookie, override if exist in replica */
359                         scp = (struct sync_cookie *) ch_calloc( 1,
360                                                                                 sizeof( struct sync_cookie ));
361                         ber_str2bv( optarg, strlen( optarg ), 1, &cookie );
362                         ber_bvarray_add( &scp->octet_str, &cookie );
363                         slap_parse_sync_cookie( scp );
364
365                         LDAP_STAILQ_FOREACH( scp_entry, &slap_sync_cookie, sc_next ) {
366                                 if ( scp->rid == scp_entry->rid ) {
367                                         Debug( LDAP_DEBUG_ANY,
368                                                     "main: duplicated replica id in cookies\n",
369                                                         0, 0, 0 );
370                                         slap_sync_cookie_free( scp, 1 );
371                                         goto destroy;
372                                 }
373                         }
374                         LDAP_STAILQ_INSERT_TAIL( &slap_sync_cookie, scp, sc_next );
375                         break;
376
377                 case 'd':       /* set debug level and 'do not detach' flag */
378                         no_detach = 1;
379 #ifdef LDAP_DEBUG
380                         slap_debug |= atoi( optarg );
381 #else
382                         if ( atoi( optarg ) != 0 )
383                                 fputs( "must compile with LDAP_DEBUG for debugging\n",
384                                        stderr );
385 #endif
386                         break;
387
388                 case 'f':       /* read config file */
389                         configfile = ch_strdup( optarg );
390                         break;
391
392                 case 'o': {
393                         char            *val = strchr( optarg, '=' );
394                         struct berval   opt;
395                         int             i;
396
397                         opt.bv_val = optarg;
398                         
399                         if ( val ) {
400                                 opt.bv_len = ( val - optarg );
401                                 val++;
402                         
403                         } else {
404                                 opt.bv_len = strlen( optarg );
405                         }
406
407                         for ( i = 0; !BER_BVISNULL( &option_helpers[i].oh_name ); i++ ) {
408                                 if ( ber_bvstrcasecmp( &option_helpers[i].oh_name, &opt ) == 0 ) {
409                                         assert( option_helpers[i].oh_fnc != NULL );
410                                         if ( (*option_helpers[i].oh_fnc)( val, option_helpers[i].oh_arg ) == -1 ) {
411                                                 /* we assume the option parsing helper
412                                                  * issues appropriate and self-explanatory
413                                                  * error messages... */
414                                                 goto stop;
415                                         }
416                                         break;
417                                 }
418                         }
419
420                         if ( BER_BVISNULL( &option_helpers[i].oh_name ) ) {
421                                 goto unhandled_option;
422                         }
423                         break;
424                 }
425
426                 case 's':       /* set syslog level */
427                         ldap_syslog = atoi( optarg );
428                         break;
429
430 #ifdef LOG_LOCAL4
431                 case 'l':       /* set syslog local user */
432                         syslogUser = cnvt_str2int( optarg,
433                                 syslog_types, DEFAULT_SYSLOG_USER );
434                         break;
435 #endif
436
437 #ifdef HAVE_CHROOT
438                 case 'r':
439                         if( sandbox ) free(sandbox);
440                         sandbox = ch_strdup( optarg );
441                         break;
442 #endif
443
444 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
445                 case 'u':       /* user name */
446                         if( username ) free(username);
447                         username = ch_strdup( optarg );
448                         break;
449
450                 case 'g':       /* group name */
451                         if( groupname ) free(groupname);
452                         groupname = ch_strdup( optarg );
453                         break;
454 #endif /* SETUID && GETUID */
455
456                 case 'n':  /* NT service name */
457                         serverName = ch_strdup( optarg );
458                         break;
459
460                 case 't':
461                         /* deprecated; use slaptest instead */
462                         fprintf( stderr, "option -t deprecated; "
463                                 "use slaptest command instead\n" );
464                         check |= CHECK_CONFIG;
465                         break;
466
467                 case 'V':
468                         version++;
469                         break;
470
471                 case 'T':
472                         /* try full option string first */
473                         for ( i = 0; tools[i].name; i++ ) {
474                                 if ( strcmp( optarg, &tools[i].name[4] ) == 0 ) {
475                                         rc = tools[i].func( argc, argv );
476                                         MAIN_RETURN( rc );
477                                 }
478                         }
479
480                         /* try bits of option string (backward compatibility for single char) */
481                         l = strlen( optarg );
482                         for ( i = 0; tools[i].name; i++ ) {
483                                 if ( strncmp( optarg, &tools[i].name[4], l ) == 0 ) {
484                                         rc = tools[i].func( argc, argv );
485                                         MAIN_RETURN( rc );
486                                 }
487                         }
488                         
489                         /* issue error */
490                         serverName = optarg;
491                         serverNamePrefix = "slap";
492                         fprintf( stderr, "program name \"%s%s\" unrecognized; "
493                                         "aborting...\n", serverNamePrefix, serverName );
494                         /* FALLTHRU */
495                 default:
496 unhandled_option:;
497                         usage( argv[0] );
498                         rc = 1;
499                         SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 15 );
500                         goto stop;
501                 }
502         }
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 LDAP_SIGCHLD
708         (void) SIGNAL( LDAP_SIGCHLD, wait4child );
709 #endif
710 #ifdef SIGBREAK
711         /* SIGBREAK is generated when Ctrl-Break is pressed. */
712         (void) SIGNAL( SIGBREAK, slap_sig_shutdown );
713 #endif
714
715 #ifndef HAVE_WINSOCK
716         lutil_detach( no_detach, 0 );
717 #endif /* HAVE_WINSOCK */
718
719 #ifdef CSRIMALLOC
720         mal_leaktrace(1);
721 #endif
722
723         /*
724          * FIXME: moved here from slapd_daemon_task()
725          * because back-monitor db_open() needs it
726          */
727         time( &starttime );
728
729         if ( slap_startup( NULL )  != 0 ) {
730                 rc = 1;
731                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 21 );
732                 goto shutdown;
733         }
734
735         Debug( LDAP_DEBUG_ANY, "slapd starting\n", 0, 0, 0 );
736
737
738         if ( slapd_pid_file != NULL ) {
739                 FILE *fp = fopen( slapd_pid_file, "w" );
740
741                 if( fp != NULL ) {
742                         fprintf( fp, "%d\n", (int) getpid() );
743                         fclose( fp );
744
745                 } else {
746                         free(slapd_pid_file);
747                         slapd_pid_file = NULL;
748                 }
749         }
750
751         if ( slapd_args_file != NULL ) {
752                 FILE *fp = fopen( slapd_args_file, "w" );
753
754                 if( fp != NULL ) {
755                         for ( i = 0; i < g_argc; i++ ) {
756                                 fprintf( fp, "%s ", g_argv[i] );
757                         }
758                         fprintf( fp, "\n" );
759                         fclose( fp );
760                 } else {
761                         free(slapd_args_file);
762                         slapd_args_file = NULL;
763                 }
764         }
765
766 #ifdef HAVE_NT_EVENT_LOG
767         if (is_NT_Service)
768         lutil_LogStartedEvent( serverName, slap_debug, configfile, urls );
769 #endif
770
771         rc = slapd_daemon();
772
773 #ifdef HAVE_NT_SERVICE_MANAGER
774         /* Throw away the event that we used during the startup process. */
775         if ( is_NT_Service )
776                 ldap_pvt_thread_cond_destroy( &started_event );
777 #endif
778
779 shutdown:
780         /* remember an error during shutdown */
781         rc |= slap_shutdown( NULL );
782
783 destroy:
784         /* remember an error during destroy */
785         rc |= slap_destroy();
786
787         while ( !LDAP_STAILQ_EMPTY( &slap_sync_cookie )) {
788                 scp = LDAP_STAILQ_FIRST( &slap_sync_cookie );
789                 LDAP_STAILQ_REMOVE_HEAD( &slap_sync_cookie, sc_next );
790                 ch_free( scp );
791         }
792
793 #ifdef SLAPD_MODULES
794         module_kill();
795 #endif
796
797         slap_op_destroy();
798
799         extops_kill();
800
801 stop:
802 #ifdef HAVE_NT_EVENT_LOG
803         if (is_NT_Service)
804         lutil_LogStoppedEvent( serverName );
805 #endif
806
807         Debug( LDAP_DEBUG_ANY, "slapd stopped.\n", 0, 0, 0 );
808
809
810 #ifdef HAVE_NT_SERVICE_MANAGER
811         lutil_ReportShutdownComplete();
812 #endif
813
814 #ifdef LOG_DEBUG
815     closelog();
816 #endif
817         slapd_daemon_destroy();
818
819         controls_destroy();
820
821         schema_destroy();
822
823         lutil_passwd_destroy();
824
825 #ifdef HAVE_TLS
826         ldap_pvt_tls_destroy();
827 #endif
828
829         if ( slapd_pid_file != NULL ) {
830                 unlink( slapd_pid_file );
831         }
832         if ( slapd_args_file != NULL ) {
833                 unlink( slapd_args_file );
834         }
835
836         config_destroy();
837
838 #ifdef CSRIMALLOC
839         mal_dumpleaktrace( leakfile );
840 #endif
841
842         MAIN_RETURN(rc);
843 }
844
845
846 #ifdef LDAP_SIGCHLD
847
848 /*
849  *  Catch and discard terminated child processes, to avoid zombies.
850  */
851
852 static RETSIGTYPE
853 wait4child( int sig )
854 {
855     int save_errno = errno;
856
857 #ifdef WNOHANG
858     errno = 0;
859 #ifdef HAVE_WAITPID
860     while ( waitpid( (pid_t)-1, NULL, WNOHANG ) > 0 || errno == EINTR )
861         ;       /* NULL */
862 #else
863     while ( wait3( NULL, WNOHANG, NULL ) > 0 || errno == EINTR )
864         ;       /* NULL */
865 #endif
866 #else
867     (void) wait( NULL );
868 #endif
869     (void) SIGNAL_REINSTALL( sig, wait4child );
870     errno = save_errno;
871 }
872
873 #endif /* LDAP_SIGCHLD */
874
875
876 #ifdef LOG_LOCAL4
877
878 /*
879  *  Convert a string to an integer by means of a dispatcher table
880  *  if the string is not in the table return the default
881  */
882
883 static int
884 cnvt_str2int( char *stringVal, STRDISP_P dispatcher, int defaultVal )
885 {
886     int        retVal = defaultVal;
887     STRDISP_P  disp;
888
889     for (disp = dispatcher; disp->stringVal; disp++) {
890
891         if (!strncasecmp (stringVal, disp->stringVal, disp->abbr)) {
892
893             retVal = disp->intVal;
894             break;
895
896         }
897     }
898
899     return (retVal);
900 }
901
902 #endif  /* LOG_LOCAL4 */