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