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 */
91 static int check_config = 0;
97 "usage: %s options\n", name );
99 "\t-d level\tDebug level" "\n"
100 "\t-f filename\tConfiguration file\n"
101 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
102 "\t-g group\tGroup (id or name) to run as\n"
104 "\t-h URLs\t\tList of URLs to serve\n"
106 "\t-l facility\tSyslog facility (default: LOCAL4)\n"
108 "\t-n serverName\tService name\n"
110 "\t-r directory\tSandbox directory to chroot to\n"
112 "\t-s level\tSyslog level\n"
113 "\t-t\t\tCheck configuration file and exit\n"
114 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
115 "\t-u user\t\tUser (id or name) to run as\n"
120 #ifdef HAVE_NT_SERVICE_MANAGER
121 void WINAPI ServiceMain( DWORD argc, LPTSTR *argv )
123 int main( int argc, char **argv )
126 int i, no_detach = 0;
129 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
130 char *username = NULL;
131 char *groupname = NULL;
133 #if defined(HAVE_CHROOT)
134 char *sandbox = NULL;
137 int syslogUser = DEFAULT_SYSLOG_USER;
141 char **g_argv = argv;
143 #ifdef HAVE_NT_SERVICE_MANAGER
144 char *configfile = ".\\slapd.conf";
146 char *configfile = SLAPD_DEFAULT_CONFIGFILE;
148 char *serverName = NULL;
149 int serverMode = SLAP_SERVER_MODE;
153 if( ( leakfile = fopen( "slapd.leak", "w" )) == NULL ) {
159 #ifdef HAVE_NT_SERVICE_MANAGER
164 char *regService = NULL;
166 if ( is_NT_Service ) {
167 serverName = argv[0];
168 CommenceStartupProcessing( serverName, slap_sig_shutdown );
169 if ( strcmp(serverName, SERVICE_NAME) )
170 regService = serverName;
173 i = (int*)getRegParam( regService, "DebugLevel" );
178 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
179 "main: new debug level from registry is: %d\n", slap_debug ));
181 Debug( LDAP_DEBUG_ANY, "new debug level from registry is: %d\n", slap_debug, 0, 0 );
185 newUrls = (char *) getRegParam(regService, "Urls");
191 urls = ch_strdup(newUrls);
193 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
194 "main: new urls from registry: %s\n", urls ));
196 Debug(LDAP_DEBUG_ANY, "new urls from registry: %s\n",
202 newConfigFile = (char*)getRegParam( regService, "ConfigFile" );
203 if ( newConfigFile != NULL )
205 configfile = newConfigFile;
207 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
208 "main: new config file from registry is: %s\n", configfile ));
210 Debug ( LDAP_DEBUG_ANY, "new config file from registry is: %s\n", configfile, 0, 0 );
217 while ( (i = getopt( argc, argv,
225 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
230 case 'h': /* listen URLs */
231 if ( urls != NULL ) free( urls );
232 urls = ch_strdup( optarg );
235 case 'd': /* set debug level and 'do not detach' flag */
238 slap_debug |= atoi( optarg );
240 if ( atoi( optarg ) != 0 )
241 fputs( "must compile with LDAP_DEBUG for debugging\n",
246 case 'f': /* read config file */
247 configfile = ch_strdup( optarg );
250 case 's': /* set syslog level */
251 ldap_syslog = atoi( optarg );
255 case 'l': /* set syslog local user */
256 syslogUser = cnvt_str2int( optarg,
257 syslog_types, DEFAULT_SYSLOG_USER );
263 if( sandbox ) free(sandbox);
264 sandbox = ch_strdup( optarg );
268 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
269 case 'u': /* user name */
270 if( username ) free(username);
271 username = ch_strdup( optarg );
274 case 'g': /* group name */
275 if( groupname ) free(groupname);
276 groupname = ch_strdup( optarg );
278 #endif /* SETUID && GETUID */
280 case 'n': /* NT service name */
281 if( serverName != NULL ) free( serverName );
282 serverName = ch_strdup( optarg );
292 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 15 );
298 lutil_log_initialize( argc, argv );
301 lutil_set_debug_level( "slapd", slap_debug );
302 ber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &slap_debug);
303 ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &slap_debug);
304 ldif_debug = slap_debug;
307 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
310 Debug( LDAP_DEBUG_TRACE, "%s", Versionstr, 0, 0 );
314 if( serverName == NULL ) {
315 if ( (serverName = strrchr( argv[0], *LDAP_DIRSEP )) == NULL ) {
316 serverName = argv[0];
318 serverName = serverName + 1;
323 openlog( serverName, OPENLOG_OPTIONS, syslogUser );
325 openlog( serverName, OPENLOG_OPTIONS );
328 if( !check_config && slapd_daemon_init( urls ) != 0 ) {
330 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 16 );
334 #if defined(HAVE_CHROOT)
336 if ( chdir( sandbox ) ) {
341 if ( chroot( sandbox ) ) {
349 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
350 if ( username != NULL || groupname != NULL ) {
351 slap_init_user( username, groupname );
358 if ( module_init() != 0 ) {
360 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 17 );
365 if ( slap_init( serverMode, serverName ) != 0 ) {
367 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 18 );
371 if ( slap_schema_init( ) != 0 ) {
373 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
374 "main: schema initialization error\n" ));
376 Debug( LDAP_DEBUG_ANY,
377 "schema initialization error\n",
384 if ( read_config( configfile ) != 0 ) {
386 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 19 );
388 if ( check_config ) {
389 fprintf( stderr, "config check failed\n" );
395 if ( check_config ) {
397 fprintf( stderr, "config check succeeded\n" );
401 if ( glue_sub_init( ) != 0 ) {
403 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
404 "main: subordinate config error\n"));
406 Debug( LDAP_DEBUG_ANY,
407 "subordinate config error\n",
413 if ( slap_schema_check( ) != 0 ) {
415 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
416 "main: schema prep error\n"));
418 Debug( LDAP_DEBUG_ANY,
419 "schema prep error\n",
427 rc = ldap_pvt_tls_init();
430 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
431 "main: tls init failed: %d\n", rc ));
433 Debug( LDAP_DEBUG_ANY,
434 "main: TLS init failed: %d\n",
438 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
442 rc = ldap_pvt_tls_init_def_ctx();
445 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
446 "main: tls init def ctx failed: %d\n", rc ));
448 Debug( LDAP_DEBUG_ANY,
449 "main: TLS init def ctx failed: %d\n",
453 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
458 (void) SIGNAL( LDAP_SIGUSR1, slap_sig_wake );
459 (void) SIGNAL( LDAP_SIGUSR2, slap_sig_shutdown );
462 (void) SIGNAL( SIGPIPE, SIG_IGN );
465 (void) SIGNAL( SIGHUP, slap_sig_shutdown );
467 (void) SIGNAL( SIGINT, slap_sig_shutdown );
468 (void) SIGNAL( SIGTERM, slap_sig_shutdown );
470 (void) SIGNAL( LDAP_SIGCHLD, wait4child );
473 /* SIGBREAK is generated when Ctrl-Break is pressed. */
474 (void) SIGNAL( SIGBREAK, slap_sig_shutdown );
478 lutil_detach( no_detach, 0 );
479 #endif /* HAVE_WINSOCK */
485 if ( slap_startup( NULL ) != 0 ) {
487 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 21 );
495 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
496 "main: slapd starting.\n" ));
498 Debug( LDAP_DEBUG_ANY, "slapd starting\n", 0, 0, 0 );
502 if (( slapd_pid_file != NULL ) &&
503 (( fp = fopen( slapd_pid_file, "w" )) != NULL ))
505 fprintf( fp, "%d\n", (int) getpid() );
509 if (( slapd_args_file != NULL ) &&
510 (( fp = fopen( slapd_args_file, "w" )) != NULL ))
512 for ( i = 0; i < g_argc; i++ ) {
513 fprintf( fp, "%s ", g_argv[i] );
520 #ifdef HAVE_NT_EVENT_LOG
522 LogSlapdStartedEvent( serverName, slap_debug, configfile, urls );
527 #ifdef HAVE_NT_SERVICE_MANAGER
528 /* Throw away the event that we used during the startup process. */
530 ldap_pvt_thread_cond_destroy( &started_event );
534 /* remember an error during shutdown */
535 rc |= slap_shutdown( NULL );
538 /* remember an error during destroy */
539 rc |= slap_destroy();
548 #ifdef HAVE_NT_EVENT_LOG
550 LogSlapdStoppedEvent( serverName );
554 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
555 "main: slapd stopped.\n" ));
557 Debug( LDAP_DEBUG_ANY, "slapd stopped.\n", 0, 0, 0 );
561 #ifdef HAVE_NT_SERVICE_MANAGER
562 ReportSlapdShutdownComplete();
568 slapd_daemon_destroy();
573 ldap_pvt_tls_destroy();
579 mal_dumpleaktrace( leakfile );
589 * Catch and discard terminated child processes, to avoid zombies.
593 wait4child( int sig )
595 int save_errno = errno;
600 while ( waitpid( (pid_t)-1, NULL, WNOHANG ) > 0 || errno == EINTR )
603 while ( wait3( NULL, WNOHANG, NULL ) > 0 || errno == EINTR )
609 (void) SIGNAL_REINSTALL( sig, wait4child );
613 #endif /* SIGCHLD || SIGCLD */
619 * Convert a string to an integer by means of a dispatcher table
620 * if the string is not in the table return the default
624 cnvt_str2int( char *stringVal, STRDISP_P dispatcher, int defaultVal )
626 int retVal = defaultVal;
629 for (disp = dispatcher; disp->stringVal; disp++) {
631 if (!strncasecmp (stringVal, disp->stringVal, disp->abbr)) {
633 retVal = disp->intVal;
642 #endif /* LOG_LOCAL4 */