3 * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
10 #include <ac/socket.h>
11 #include <ac/string.h>
13 #include <ac/unistd.h>
24 static RETSIGTYPE wait4child( int sig );
27 #ifdef HAVE_NT_SERVICE_MANAGER
28 #define MAIN_RETURN(x) return
29 static struct sockaddr_in bind_addr;
31 void CommenceStartupProcessing( LPCTSTR serverName,
33 void ReportSlapdShutdownComplete( void );
34 void *getRegParam( char *svc, char *value );
36 #define SERVICE_EXIT( e, n ) do { \
37 if ( is_NT_Service ) { \
38 SLAPDServiceStatus.dwWin32ExitCode = (e); \
39 SLAPDServiceStatus.dwServiceSpecificExitCode = (n); \
44 #define SERVICE_EXIT( e, n )
45 #define MAIN_RETURN(x) return(x)
48 #ifdef HAVE_NT_EVENT_LOG
49 void LogSlapdStartedEvent( char *svc, int slap_debug, char *configfile, char *urls );
50 void LogSlapdStoppedEvent( char *svc );
54 * when more than one slapd is running on one machine, each one might have
55 * it's own LOCAL for syslogging and must have its own pid/args files
58 #ifndef HAVE_MKVERSION
59 const char Versionstr[] =
60 OPENLDAP_PACKAGE " " OPENLDAP_VERSION " Standalone LDAP Server (slapd)";
65 #define DEFAULT_SYSLOG_USER LOG_LOCAL4
67 typedef struct _str2intDispatch {
71 } STRDISP, *STRDISP_P;
74 /* table to compute syslog-options to integer */
75 static STRDISP syslog_types[] = {
76 { "LOCAL0", sizeof("LOCAL0"), LOG_LOCAL0 },
77 { "LOCAL1", sizeof("LOCAL1"), LOG_LOCAL1 },
78 { "LOCAL2", sizeof("LOCAL2"), LOG_LOCAL2 },
79 { "LOCAL3", sizeof("LOCAL3"), LOG_LOCAL3 },
80 { "LOCAL4", sizeof("LOCAL4"), LOG_LOCAL4 },
81 { "LOCAL5", sizeof("LOCAL5"), LOG_LOCAL5 },
82 { "LOCAL6", sizeof("LOCAL6"), LOG_LOCAL6 },
83 { "LOCAL7", sizeof("LOCAL7"), LOG_LOCAL7 },
87 static int cnvt_str2int( char *, STRDISP_P, int );
89 #endif /* LOG_LOCAL4 */
96 "usage: %s options\n", name );
98 "\t-d level\tDebug Level" "\n"
99 "\t-f filename\tConfiguration File\n"
100 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
101 "\t-g group\tGroup (id or name) to run as\n"
103 "\t-h URLs\tList of URLs to serve\n"
105 "\t-l sysloguser\tSyslog User (default: LOCAL4)\n"
107 "\t-n serverName\tservice name\n"
111 "\t-s level\tSyslog Level\n"
112 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
113 "\t-u user\tUser (id or name) to run as\n"
118 #ifdef HAVE_NT_SERVICE_MANAGER
119 void WINAPI ServiceMain( DWORD argc, LPTSTR *argv )
121 int main( int argc, char **argv )
124 int i, no_detach = 0;
127 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
128 char *username = NULL;
129 char *groupname = NULL;
131 #if defined(HAVE_CHROOT)
132 char *sandbox = NULL;
135 int syslogUser = DEFAULT_SYSLOG_USER;
139 char **g_argv = argv;
141 #ifdef HAVE_NT_SERVICE_MANAGER
142 char *configfile = ".\\slapd.conf";
144 char *configfile = SLAPD_DEFAULT_CONFIGFILE;
146 char *serverName = NULL;
147 int serverMode = SLAP_SERVER_MODE;
151 if( ( leakfile = fopen( "slapd.leak", "w" )) == NULL ) {
157 #ifdef HAVE_NT_SERVICE_MANAGER
162 char *regService = NULL;
164 if ( is_NT_Service ) {
165 serverName = argv[0];
166 CommenceStartupProcessing( serverName, slap_sig_shutdown );
167 if ( strcmp(serverName, SERVICE_NAME) )
168 regService = serverName;
171 i = (int*)getRegParam( regService, "DebugLevel" );
176 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
177 "main: new debug level from registry is: %d\n", slap_debug ));
179 Debug( LDAP_DEBUG_ANY, "new debug level from registry is: %d\n", slap_debug, 0, 0 );
183 newUrls = (char *) getRegParam(regService, "Urls");
189 urls = ch_strdup(newUrls);
191 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
192 "main: new urls from registry: %s\n", urls ));
194 Debug(LDAP_DEBUG_ANY, "new urls from registry: %s\n",
200 newConfigFile = (char*)getRegParam( regService, "ConfigFile" );
201 if ( newConfigFile != NULL )
203 configfile = newConfigFile;
205 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
206 "main: new config file from registry is: %s\n", configfile ));
208 Debug ( LDAP_DEBUG_ANY, "new config file from registry is: %s\n", configfile, 0, 0 );
215 while ( (i = getopt( argc, argv,
223 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
228 case 'h': /* listen URLs */
229 if ( urls != NULL ) free( urls );
230 urls = ch_strdup( optarg );
233 case 'd': /* set debug level and 'do not detach' flag */
236 slap_debug |= atoi( optarg );
238 if ( atoi( optarg ) != 0 )
239 fputs( "must compile with LDAP_DEBUG for debugging\n",
244 case 'f': /* read config file */
245 configfile = ch_strdup( optarg );
248 case 's': /* set syslog level */
249 ldap_syslog = atoi( optarg );
253 case 'l': /* set syslog local user */
254 syslogUser = cnvt_str2int( optarg,
255 syslog_types, DEFAULT_SYSLOG_USER );
261 if( sandbox ) free(sandbox);
262 sandbox = ch_strdup( optarg );
266 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
267 case 'u': /* user name */
268 if( username ) free(username);
269 username = ch_strdup( optarg );
272 case 'g': /* group name */
273 if( groupname ) free(groupname);
274 groupname = ch_strdup( optarg );
276 #endif /* SETUID && GETUID */
278 case 'n': /* NT service name */
279 if( serverName != NULL ) free( serverName );
280 serverName = ch_strdup( optarg );
286 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 15 );
292 lutil_log_initialize( argc, argv );
295 lutil_set_debug_level( "slapd", slap_debug );
296 ber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &slap_debug);
297 ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &slap_debug);
298 ldif_debug = slap_debug;
301 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
304 Debug( LDAP_DEBUG_TRACE, "%s", Versionstr, 0, 0 );
308 if( serverName == NULL ) {
309 if ( (serverName = strrchr( argv[0], *LDAP_DIRSEP )) == NULL ) {
310 serverName = argv[0];
312 serverName = serverName + 1;
317 openlog( serverName, OPENLOG_OPTIONS, syslogUser );
319 openlog( serverName, OPENLOG_OPTIONS );
322 if( slapd_daemon_init( urls ) != 0 ) {
324 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 16 );
328 #if defined(HAVE_CHROOT)
330 if ( chdir( sandbox ) ) {
335 if ( chroot( sandbox ) ) {
343 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
344 if ( username != NULL || groupname != NULL ) {
345 slap_init_user( username, groupname );
352 if ( module_init() != 0 ) {
354 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 17 );
359 if ( slap_init( serverMode, serverName ) != 0 ) {
361 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 18 );
365 if ( slap_schema_init( ) != 0 ) {
367 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
368 "main: schema initialization error\n" ));
370 Debug( LDAP_DEBUG_ANY,
371 "schema initialization error\n",
378 if ( read_config( configfile ) != 0 ) {
380 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 19 );
384 if ( glue_sub_init( ) != 0 ) {
386 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
387 "main: subordinate config error\n"));
389 Debug( LDAP_DEBUG_ANY,
390 "subordinate config error\n",
396 if ( slap_schema_check( ) != 0 ) {
398 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
399 "main: schema prep error\n"));
401 Debug( LDAP_DEBUG_ANY,
402 "schema prep error\n",
410 rc = ldap_pvt_tls_init();
413 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
414 "main: tls init failed: %d\n", rc ));
416 Debug( LDAP_DEBUG_ANY,
417 "main: TLS init failed: %d\n",
421 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
425 rc = ldap_pvt_tls_init_def_ctx();
428 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
429 "main: tls init def ctx failed: %d\n", rc ));
431 Debug( LDAP_DEBUG_ANY,
432 "main: TLS init def ctx failed: %d\n",
436 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
441 (void) SIGNAL( LDAP_SIGUSR1, slap_sig_wake );
442 (void) SIGNAL( LDAP_SIGUSR2, slap_sig_shutdown );
445 (void) SIGNAL( SIGPIPE, SIG_IGN );
448 (void) SIGNAL( SIGHUP, slap_sig_shutdown );
450 (void) SIGNAL( SIGINT, slap_sig_shutdown );
451 (void) SIGNAL( SIGTERM, slap_sig_shutdown );
453 (void) SIGNAL( LDAP_SIGCHLD, wait4child );
456 /* SIGBREAK is generated when Ctrl-Break is pressed. */
457 (void) SIGNAL( SIGBREAK, slap_sig_shutdown );
461 lutil_detach( no_detach, 0 );
462 #endif /* HAVE_WINSOCK */
468 if ( slap_startup( NULL ) != 0 ) {
470 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 21 );
478 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
479 "main: slapd starting.\n" ));
481 Debug( LDAP_DEBUG_ANY, "slapd starting\n", 0, 0, 0 );
485 if (( slapd_pid_file != NULL ) &&
486 (( fp = fopen( slapd_pid_file, "w" )) != NULL ))
488 fprintf( fp, "%d\n", (int) getpid() );
492 if (( slapd_args_file != NULL ) &&
493 (( fp = fopen( slapd_args_file, "w" )) != NULL ))
495 for ( i = 0; i < g_argc; i++ ) {
496 fprintf( fp, "%s ", g_argv[i] );
503 #ifdef HAVE_NT_EVENT_LOG
505 LogSlapdStartedEvent( serverName, slap_debug, configfile, urls );
510 #ifdef HAVE_NT_SERVICE_MANAGER
511 /* Throw away the event that we used during the startup process. */
513 ldap_pvt_thread_cond_destroy( &started_event );
517 /* remember an error during shutdown */
518 rc |= slap_shutdown( NULL );
521 /* remember an error during destroy */
522 rc |= slap_destroy();
531 #ifdef HAVE_NT_EVENT_LOG
533 LogSlapdStoppedEvent( serverName );
537 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
538 "main: slapd stopped.\n" ));
540 Debug( LDAP_DEBUG_ANY, "slapd stopped.\n", 0, 0, 0 );
544 #ifdef HAVE_NT_SERVICE_MANAGER
545 ReportSlapdShutdownComplete();
551 slapd_daemon_destroy();
556 ldap_pvt_tls_destroy();
562 mal_dumpleaktrace( leakfile );
572 * Catch and discard terminated child processes, to avoid zombies.
576 wait4child( int sig )
578 int save_errno = errno;
583 while ( waitpid( (pid_t)-1, NULL, WNOHANG ) > 0 || errno == EINTR )
586 while ( wait3( NULL, WNOHANG, NULL ) > 0 || errno == EINTR )
592 (void) SIGNAL_REINSTALL( sig, wait4child );
596 #endif /* SIGCHLD || SIGCLD */
602 * Convert a string to an integer by means of a dispatcher table
603 * if the string is not in the table return the default
607 cnvt_str2int( char *stringVal, STRDISP_P dispatcher, int defaultVal )
609 int retVal = defaultVal;
612 for (disp = dispatcher; disp->stringVal; disp++) {
614 if (!strncasecmp (stringVal, disp->stringVal, disp->abbr)) {
616 retVal = disp->intVal;
625 #endif /* LOG_LOCAL4 */