]> git.sur5r.net Git - openldap/blob - servers/slapd/main.c
c88876e046dcfbc2c281e7f92d2ed4144a9ffab9
[openldap] / servers / slapd / main.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1998-2005 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 sync_cookie *scp = NULL;
263         struct sync_cookie *scp_entry = NULL;
264
265 #ifdef CSRIMALLOC
266         FILE *leakfile;
267         if( ( leakfile = fopen( "slapd.leak", "w" )) == NULL ) {
268                 leakfile = stderr;
269         }
270 #endif
271         char    *serverNamePrefix = "";
272         size_t  l;
273
274         slap_sl_mem_init();
275
276         serverName = lutil_progname( "slapd", argc, argv );
277
278         if ( strcmp( serverName, "slapd" ) ) {
279                 for (i=0; tools[i].name; i++) {
280                         if ( !strcmp( serverName, tools[i].name ) ) {
281                                 rc = tools[i].func(argc, argv);
282                                 MAIN_RETURN(rc);
283                         }
284                 }
285         }
286
287 #ifdef HAVE_NT_SERVICE_MANAGER
288         {
289                 int *i;
290                 char *newConfigFile;
291                 char *newUrls;
292                 char *regService = NULL;
293
294                 if ( is_NT_Service ) {
295                         lutil_CommenceStartupProcessing( serverName, slap_sig_shutdown );
296                         if ( strcmp(serverName, SERVICE_NAME) )
297                             regService = serverName;
298                 }
299
300                 i = (int*)lutil_getRegParam( regService, "DebugLevel" );
301                 if ( i != NULL ) {
302                         slap_debug = *i;
303                         Debug( LDAP_DEBUG_ANY,
304                                 "new debug level from registry is: %d\n", slap_debug, 0, 0 );
305                 }
306
307                 newUrls = (char *) lutil_getRegParam(regService, "Urls");
308                 if (newUrls) {
309                     if (urls)
310                         ch_free(urls);
311
312                     urls = ch_strdup(newUrls);
313                     Debug(LDAP_DEBUG_ANY, "new urls from registry: %s\n",
314                                 urls, 0, 0);
315                 }
316
317                 newConfigFile = (char*)lutil_getRegParam( regService, "ConfigFile" );
318                 if ( newConfigFile != NULL ) {
319                         configfile = newConfigFile;
320                         Debug ( LDAP_DEBUG_ANY, "new config file from registry is: %s\n", configfile, 0, 0 );
321                 }
322         }
323 #endif
324
325         while ( (i = getopt( argc, argv,
326                              "c:d:f:h:n:o:s:StT:V"
327 #if LDAP_PF_INET6
328                                 "46"
329 #endif
330 #ifdef HAVE_CHROOT
331                                 "r:"
332 #endif
333 #ifdef LOG_LOCAL4
334                              "l:"
335 #endif
336 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
337                              "u:g:"
338 #endif
339                              )) != EOF ) {
340                 switch ( i ) {
341 #ifdef LDAP_PF_INET6
342                 case '4':
343                         slap_inet4or6 = AF_INET;
344                         break;
345                 case '6':
346                         slap_inet4or6 = AF_INET6;
347                         break;
348 #endif
349
350                 case 'h':       /* listen URLs */
351                         if ( urls != NULL ) free( urls );
352                         urls = ch_strdup( optarg );
353                         break;
354
355                 case 'c':       /* provide sync cookie, override if exist in replica */
356                         scp = (struct sync_cookie *) ch_calloc( 1,
357                                                                                 sizeof( struct sync_cookie ));
358                         ber_str2bv( optarg, 0, 1, &scp->octet_str );
359                         slap_parse_sync_cookie( scp );
360
361                         LDAP_STAILQ_FOREACH( scp_entry, &slap_sync_cookie, sc_next ) {
362                                 if ( scp->rid == scp_entry->rid ) {
363                                         Debug( LDAP_DEBUG_ANY,
364                                                     "main: duplicated replica id in cookies\n",
365                                                         0, 0, 0 );
366                                         slap_sync_cookie_free( scp, 1 );
367                                         goto destroy;
368                                 }
369                         }
370                         LDAP_STAILQ_INSERT_TAIL( &slap_sync_cookie, scp, sc_next );
371                         break;
372
373                 case 'd':       /* set debug level and 'do not detach' flag */
374                         no_detach = 1;
375 #ifdef LDAP_DEBUG
376                         slap_debug |= atoi( optarg );
377 #else
378                         if ( atoi( optarg ) != 0 )
379                                 fputs( "must compile with LDAP_DEBUG for debugging\n",
380                                        stderr );
381 #endif
382                         break;
383
384                 case 'f':       /* read config file */
385                         configfile = ch_strdup( optarg );
386                         break;
387
388                 case 'o': {
389                         char            *val = strchr( optarg, '=' );
390                         struct berval   opt;
391                         int             i;
392
393                         opt.bv_val = optarg;
394                         
395                         if ( val ) {
396                                 opt.bv_len = ( val - optarg );
397                                 val++;
398                         
399                         } else {
400                                 opt.bv_len = strlen( optarg );
401                         }
402
403                         for ( i = 0; !BER_BVISNULL( &option_helpers[i].oh_name ); i++ ) {
404                                 if ( ber_bvstrcasecmp( &option_helpers[i].oh_name, &opt ) == 0 ) {
405                                         assert( option_helpers[i].oh_fnc != NULL );
406                                         if ( (*option_helpers[i].oh_fnc)( val, option_helpers[i].oh_arg ) == -1 ) {
407                                                 /* we assume the option parsing helper
408                                                  * issues appropriate and self-explanatory
409                                                  * error messages... */
410                                                 goto stop;
411                                         }
412                                         break;
413                                 }
414                         }
415
416                         if ( BER_BVISNULL( &option_helpers[i].oh_name ) ) {
417                                 goto unhandled_option;
418                         }
419                         break;
420                 }
421
422                 case 's':       /* set syslog level */
423                         ldap_syslog = atoi( optarg );
424                         break;
425
426 #ifdef LOG_LOCAL4
427                 case 'l':       /* set syslog local user */
428                         syslogUser = cnvt_str2int( optarg,
429                                 syslog_types, DEFAULT_SYSLOG_USER );
430                         break;
431 #endif
432
433 #ifdef HAVE_CHROOT
434                 case 'r':
435                         if( sandbox ) free(sandbox);
436                         sandbox = ch_strdup( optarg );
437                         break;
438 #endif
439
440 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
441                 case 'u':       /* user name */
442                         if( username ) free(username);
443                         username = ch_strdup( optarg );
444                         break;
445
446                 case 'g':       /* group name */
447                         if( groupname ) free(groupname);
448                         groupname = ch_strdup( optarg );
449                         break;
450 #endif /* SETUID && GETUID */
451
452                 case 'n':  /* NT service name */
453                         serverName = ch_strdup( optarg );
454                         break;
455
456                 case 't':
457                         /* deprecated; use slaptest instead */
458                         fprintf( stderr, "option -t deprecated; "
459                                 "use slaptest command instead\n" );
460                         check |= CHECK_CONFIG;
461                         break;
462
463                 case 'V':
464                         version++;
465                         break;
466
467                 case 'T':
468                         /* try full option string first */
469                         for ( i = 0; tools[i].name; i++ ) {
470                                 if ( strcmp( optarg, &tools[i].name[4] ) == 0 ) {
471                                         rc = tools[i].func( argc, argv );
472                                         MAIN_RETURN( rc );
473                                 }
474                         }
475
476                         /* try bits of option string (backward compatibility for single char) */
477                         l = strlen( optarg );
478                         for ( i = 0; tools[i].name; i++ ) {
479                                 if ( strncmp( optarg, &tools[i].name[4], l ) == 0 ) {
480                                         rc = tools[i].func( argc, argv );
481                                         MAIN_RETURN( rc );
482                                 }
483                         }
484                         
485                         /* issue error */
486                         serverName = optarg;
487                         serverNamePrefix = "slap";
488                         fprintf( stderr, "program name \"%s%s\" unrecognized; "
489                                         "aborting...\n", serverNamePrefix, serverName );
490                         /* FALLTHRU */
491                 default:
492 unhandled_option:;
493                         usage( argv[0] );
494                         rc = 1;
495                         SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 15 );
496                         goto stop;
497                 }
498         }
499
500         (void) ldap_pvt_thread_initialize();
501
502         ber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &slap_debug);
503         ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &slap_debug);
504         ldif_debug = slap_debug;
505
506         if ( version ) {
507                 fprintf( stderr, "%s\n", Versionstr );
508                 if ( version > 1 ) goto stop;
509         }
510
511         {
512                 char *logName;
513 #ifdef HAVE_EBCDIC
514                 logName = ch_strdup( serverName );
515                 __atoe( logName );
516 #else
517                 logName = serverName;
518 #endif
519
520 #ifdef LOG_LOCAL4
521                 openlog( logName, OPENLOG_OPTIONS, syslogUser );
522 #elif LOG_DEBUG
523                 openlog( logName, OPENLOG_OPTIONS );
524 #endif
525 #ifdef HAVE_EBCDIC
526                 free( logName );
527 #endif
528         }
529
530         Debug( LDAP_DEBUG_ANY, "%s", Versionstr, 0, 0 );
531
532         if( check == CHECK_NONE && slapd_daemon_init( urls ) != 0 ) {
533                 rc = 1;
534                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 16 );
535                 goto stop;
536         }
537
538 #if defined(HAVE_CHROOT)
539         if ( sandbox ) {
540                 if ( chdir( sandbox ) ) {
541                         perror("chdir");
542                         rc = 1;
543                         goto stop;
544                 }
545                 if ( chroot( sandbox ) ) {
546                         perror("chroot");
547                         rc = 1;
548                         goto stop;
549                 }
550         }
551 #endif
552
553 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
554         if ( username != NULL || groupname != NULL ) {
555                 slap_init_user( username, groupname );
556         }
557 #endif
558
559         extops_init();
560         lutil_passwd_init();
561         slap_op_init();
562
563 #ifdef SLAPD_MODULES
564         if ( module_init() != 0 ) {
565                 rc = 1;
566                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 17 );
567                 goto destroy;
568         }
569 #endif
570
571         if ( slap_schema_init( ) != 0 ) {
572                 Debug( LDAP_DEBUG_ANY,
573                     "schema initialization error\n",
574                     0, 0, 0 );
575
576                 goto destroy;
577         }
578
579         if ( slap_init( serverMode, serverName ) != 0 ) {
580                 rc = 1;
581                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 18 );
582                 goto destroy;
583         }
584
585         if ( slap_controls_init( ) != 0 ) {
586                 Debug( LDAP_DEBUG_ANY,
587                     "controls initialization error\n",
588                     0, 0, 0 );
589
590                 goto destroy;
591         }
592
593 #ifdef HAVE_TLS
594         /* Library defaults to full certificate checking. This is correct when
595          * a client is verifying a server because all servers should have a
596          * valid cert. But few clients have valid certs, so we want our default
597          * to be no checking. The config file can override this as usual.
598          */
599         rc = 0;
600         (void) ldap_pvt_tls_set_option( NULL, LDAP_OPT_X_TLS_REQUIRE_CERT, &rc );
601 #endif
602
603 #ifdef LDAP_SLAPI
604         if ( slapi_int_initialize() != 0 ) {
605                 Debug( LDAP_DEBUG_ANY,
606                     "slapi initialization error\n",
607                     0, 0, 0 );
608
609                 goto destroy;
610         }
611 #endif /* LDAP_SLAPI */
612
613         if ( frontend_init() ) {
614                 goto destroy;
615         }
616
617         if ( overlay_init() ) {
618                 goto destroy;
619         }
620
621 #ifdef SLAP_DYNACL
622         if ( acl_init() ) {
623                 goto destroy;
624         }
625 #endif /* SLAP_DYNACL */
626
627         if ( read_config( configfile, 0 ) != 0 ) {
628                 rc = 1;
629                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 19 );
630
631                 if ( check & CHECK_CONFIG ) {
632                         fprintf( stderr, "config check failed\n" );
633                 }
634
635                 goto destroy;
636         }
637
638         if ( check & CHECK_CONFIG ) {
639                 fprintf( stderr, "config check succeeded\n" );
640
641                 check &= ~CHECK_CONFIG;
642                 if ( check == CHECK_NONE ) {
643                         rc = 0;
644                         goto destroy;
645                 }
646         }
647
648         if ( slap_schema_check( ) != 0 ) {
649                 Debug( LDAP_DEBUG_ANY,
650                     "schema prep error\n",
651                     0, 0, 0 );
652
653                 goto destroy;
654         }
655
656 #ifdef HAVE_TLS
657         rc = ldap_pvt_tls_init();
658         if( rc != 0) {
659                 Debug( LDAP_DEBUG_ANY,
660                     "main: TLS init failed: %d\n",
661                     0, 0, 0 );
662                 rc = 1;
663                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
664                 goto destroy;
665         }
666
667         {
668                 void *def_ctx = NULL;
669
670                 /* Save existing default ctx, if any */
671                 ldap_pvt_tls_get_option( NULL, LDAP_OPT_X_TLS_CTX, &def_ctx );
672
673                 /* Force new ctx to be created */
674                 ldap_pvt_tls_set_option( NULL, LDAP_OPT_X_TLS_CTX, NULL );
675
676                 rc = ldap_pvt_tls_init_def_ctx();
677                 if( rc != 0) {
678                         Debug( LDAP_DEBUG_ANY,
679                             "main: TLS init def ctx failed: %d\n",
680                             rc, 0, 0 );
681                         rc = 1;
682                         SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
683                         goto destroy;
684                 }
685                 /* Retrieve slapd's own ctx */
686                 ldap_pvt_tls_get_option( NULL, LDAP_OPT_X_TLS_CTX, &slap_tls_ctx );
687                 /* Restore previous ctx */
688                 ldap_pvt_tls_set_option( NULL, LDAP_OPT_X_TLS_CTX, def_ctx );
689         }
690 #endif
691
692         (void) SIGNAL( LDAP_SIGUSR1, slap_sig_wake );
693         (void) SIGNAL( LDAP_SIGUSR2, slap_sig_shutdown );
694
695 #ifdef SIGPIPE
696         (void) SIGNAL( SIGPIPE, SIG_IGN );
697 #endif
698 #ifdef SIGHUP
699         (void) SIGNAL( SIGHUP, slap_sig_shutdown );
700 #endif
701         (void) SIGNAL( SIGINT, slap_sig_shutdown );
702         (void) SIGNAL( SIGTERM, slap_sig_shutdown );
703 #ifdef SIGTRAP
704         (void) SIGNAL( SIGTRAP, slap_sig_shutdown );
705 #endif
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( &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_pid_file != NULL ) {
738                 FILE *fp = fopen( slapd_pid_file, "w" );
739
740                 if( fp != NULL ) {
741                         fprintf( fp, "%d\n", (int) getpid() );
742                         fclose( fp );
743
744                 } else {
745                         free(slapd_pid_file);
746                         slapd_pid_file = NULL;
747                 }
748         }
749
750         if ( slapd_args_file != NULL ) {
751                 FILE *fp = fopen( slapd_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_args_file);
761                         slapd_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( &slap_sync_cookie )) {
787                 scp = LDAP_STAILQ_FIRST( &slap_sync_cookie );
788                 LDAP_STAILQ_REMOVE_HEAD( &slap_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_pid_file != NULL ) {
829                 unlink( slapd_pid_file );
830         }
831         if ( slapd_args_file != NULL ) {
832                 unlink( slapd_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 */