3 * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
10 #include <ac/signal.h>
11 #include <ac/socket.h>
12 #include <ac/string.h>
14 #include <ac/unistd.h>
25 static RETSIGTYPE wait4child( int sig );
28 #ifdef HAVE_NT_SERVICE_MANAGER
29 #define MAIN_RETURN(x) return
30 static struct sockaddr_in bind_addr;
32 void CommenceStartupProcessing( LPCTSTR serverName,
34 void ReportSlapdShutdownComplete( void );
35 void *getRegParam( char *svc, char *value );
37 #define SERVICE_EXIT( e, n ) do { \
38 if ( is_NT_Service ) { \
39 SLAPDServiceStatus.dwWin32ExitCode = (e); \
40 SLAPDServiceStatus.dwServiceSpecificExitCode = (n); \
45 #define SERVICE_EXIT( e, n )
46 #define MAIN_RETURN(x) return(x)
49 #ifdef HAVE_NT_EVENT_LOG
50 void LogSlapdStartedEvent( char *svc, int slap_debug, char *configfile, char *urls );
51 void LogSlapdStoppedEvent( char *svc );
55 * when more than one slapd is running on one machine, each one might have
56 * it's own LOCAL for syslogging and must have its own pid/args files
59 #ifndef HAVE_MKVERSION
60 const char Versionstr[] =
61 OPENLDAP_PACKAGE " " OPENLDAP_VERSION " Standalone LDAP Server (slapd)";
66 #define DEFAULT_SYSLOG_USER LOG_LOCAL4
68 typedef struct _str2intDispatch {
72 } STRDISP, *STRDISP_P;
75 /* table to compute syslog-options to integer */
76 static STRDISP syslog_types[] = {
77 { "LOCAL0", sizeof("LOCAL0"), LOG_LOCAL0 },
78 { "LOCAL1", sizeof("LOCAL1"), LOG_LOCAL1 },
79 { "LOCAL2", sizeof("LOCAL2"), LOG_LOCAL2 },
80 { "LOCAL3", sizeof("LOCAL3"), LOG_LOCAL3 },
81 { "LOCAL4", sizeof("LOCAL4"), LOG_LOCAL4 },
82 { "LOCAL5", sizeof("LOCAL5"), LOG_LOCAL5 },
83 { "LOCAL6", sizeof("LOCAL6"), LOG_LOCAL6 },
84 { "LOCAL7", sizeof("LOCAL7"), LOG_LOCAL7 },
88 static int cnvt_str2int( char *, STRDISP_P, int );
90 #endif /* LOG_LOCAL4 */
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 ran as\n"
104 "\t-h URLs\tList of URLs to serve\n"
106 "\t-l sysloguser\tSyslog User (default: LOCAL4)\n"
108 "\t-n serverName\tservice name\n"
112 "\t-s level\tSyslog Level\n"
113 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
114 "\t-u user\tUser (id or name) to ran as\n"
119 #ifdef HAVE_NT_SERVICE_MANAGER
120 void WINAPI ServiceMain( DWORD argc, LPTSTR *argv )
122 int main( int argc, char **argv )
125 int i, no_detach = 0;
128 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
129 char *username = NULL;
130 char *groupname = NULL;
132 #if defined(HAVE_CHROOT)
133 char *sandbox = NULL;
136 int syslogUser = DEFAULT_SYSLOG_USER;
140 char **g_argv = argv;
142 #ifdef HAVE_NT_SERVICE_MANAGER
143 char *configfile = ".\\slapd.conf";
145 char *configfile = SLAPD_DEFAULT_CONFIGFILE;
147 char *serverName = NULL;
148 int serverMode = SLAP_SERVER_MODE;
152 if( ( leakfile = fopen( "slapd.leak", "w" )) == NULL ) {
158 #ifdef HAVE_NT_SERVICE_MANAGER
163 char *regService = NULL;
165 if ( is_NT_Service ) {
166 serverName = argv[0];
167 CommenceStartupProcessing( serverName, slap_sig_shutdown );
168 if ( strcmp(serverName, SERVICE_NAME) )
169 regService = serverName;
172 i = (int*)getRegParam( regService, "DebugLevel" );
177 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
178 "main: new debug level from registry is: %d\n", slap_debug ));
180 Debug( LDAP_DEBUG_ANY, "new debug level from registry is: %d\n", slap_debug, 0, 0 );
184 newUrls = (char *) getRegParam(regService, "Urls");
190 urls = ch_strdup(newUrls);
192 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
193 "main: new urls from registry: %s\n", urls ));
195 Debug(LDAP_DEBUG_ANY, "new urls from registry: %s\n",
201 newConfigFile = (char*)getRegParam( regService, "ConfigFile" );
202 if ( newConfigFile != NULL )
204 configfile = newConfigFile;
206 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
207 "main: new config file from registry is: %s\n", configfile ));
209 Debug ( LDAP_DEBUG_ANY, "new config file from registry is: %s\n", configfile, 0, 0 );
216 while ( (i = getopt( argc, argv,
224 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
229 case 'h': /* listen URLs */
230 if ( urls != NULL ) free( urls );
231 urls = ch_strdup( optarg );
234 case 'd': /* set debug level and 'do not detach' flag */
237 slap_debug |= atoi( optarg );
239 if ( atoi( optarg ) != 0 )
240 fputs( "must compile with LDAP_DEBUG for debugging\n",
245 case 'f': /* read config file */
246 configfile = ch_strdup( optarg );
249 case 's': /* set syslog level */
250 ldap_syslog = atoi( optarg );
254 case 'l': /* set syslog local user */
255 syslogUser = cnvt_str2int( optarg,
256 syslog_types, DEFAULT_SYSLOG_USER );
262 if( sandbox ) free(sandbox);
263 sandbox = ch_strdup( optarg );
267 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
268 case 'u': /* user name */
269 if( username ) free(username);
270 username = ch_strdup( optarg );
273 case 'g': /* group name */
274 if( groupname ) free(groupname);
275 groupname = ch_strdup( optarg );
277 #endif /* SETUID && GETUID */
279 case 'n': /* NT service name */
280 if( serverName != NULL ) free( serverName );
281 serverName = ch_strdup( optarg );
287 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 15 );
293 lutil_log_initialize( argc, argv );
296 lutil_set_debug_level( "slapd", slap_debug );
297 ber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &slap_debug);
298 ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &slap_debug);
299 ldif_debug = slap_debug;
302 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
305 Debug( LDAP_DEBUG_TRACE, "%s", Versionstr, 0, 0 );
309 if( serverName == NULL ) {
310 if ( (serverName = strrchr( argv[0], *LDAP_DIRSEP )) == NULL ) {
311 serverName = argv[0];
313 serverName = serverName + 1;
318 openlog( serverName, OPENLOG_OPTIONS, syslogUser );
320 openlog( serverName, OPENLOG_OPTIONS );
323 if( slapd_daemon_init( urls ) != 0 ) {
325 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 16 );
329 #if defined(HAVE_CHROOT)
331 if ( chdir( sandbox ) ) {
336 if ( chroot( sandbox ) ) {
344 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
345 if ( username != NULL || groupname != NULL ) {
346 slap_init_user( username, groupname );
353 if ( module_init() != 0 ) {
355 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 17 );
360 if ( slap_init( serverMode, serverName ) != 0 ) {
362 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 18 );
366 if ( slap_schema_init( ) != 0 ) {
368 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
369 "main: schema initialization error\n" ));
371 Debug( LDAP_DEBUG_ANY,
372 "schema initialization error\n",
379 if ( read_config( configfile ) != 0 ) {
381 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 19 );
385 if ( glue_sub_init( ) != 0 ) {
387 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
388 "main: subordinate config error\n"));
390 Debug( LDAP_DEBUG_ANY,
391 "subordinate config error\n",
397 if ( slap_schema_check( ) != 0 ) {
399 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
400 "main: schema prep error\n"));
402 Debug( LDAP_DEBUG_ANY,
403 "schema prep error\n",
411 rc = ldap_pvt_tls_init();
414 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
415 "main: tls init failed: %d\n", rc ));
417 Debug( LDAP_DEBUG_ANY,
418 "main: TLS init failed: %d\n",
422 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
426 rc = ldap_pvt_tls_init_def_ctx();
429 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
430 "main: tls init def ctx failed: %d\n", rc ));
432 Debug( LDAP_DEBUG_ANY,
433 "main: TLS init def ctx failed: %d\n",
437 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
442 (void) SIGNAL( LDAP_SIGUSR1, slap_sig_wake );
443 (void) SIGNAL( LDAP_SIGUSR2, slap_sig_shutdown );
446 (void) SIGNAL( SIGPIPE, SIG_IGN );
449 (void) SIGNAL( SIGHUP, slap_sig_shutdown );
451 (void) SIGNAL( SIGINT, slap_sig_shutdown );
452 (void) SIGNAL( SIGTERM, slap_sig_shutdown );
454 (void) SIGNAL( LDAP_SIGCHLD, wait4child );
457 /* SIGBREAK is generated when Ctrl-Break is pressed. */
458 (void) SIGNAL( SIGBREAK, slap_sig_shutdown );
462 lutil_detach( no_detach, 0 );
463 #endif /* HAVE_WINSOCK */
469 if ( slap_startup( NULL ) != 0 ) {
471 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 21 );
479 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
480 "main: slapd starting.\n" ));
482 Debug( LDAP_DEBUG_ANY, "slapd starting\n", 0, 0, 0 );
486 if (( slapd_pid_file != NULL ) &&
487 (( fp = fopen( slapd_pid_file, "w" )) != NULL ))
489 fprintf( fp, "%d\n", (int) getpid() );
493 if (( slapd_args_file != NULL ) &&
494 (( fp = fopen( slapd_args_file, "w" )) != NULL ))
496 for ( i = 0; i < g_argc; i++ ) {
497 fprintf( fp, "%s ", g_argv[i] );
504 #ifdef HAVE_NT_EVENT_LOG
506 LogSlapdStartedEvent( serverName, slap_debug, configfile, urls );
511 #ifdef HAVE_NT_SERVICE_MANAGER
512 /* Throw away the event that we used during the startup process. */
514 ldap_pvt_thread_cond_destroy( &started_event );
518 /* remember an error during shutdown */
519 rc |= slap_shutdown( NULL );
522 /* remember an error during destroy */
523 rc |= slap_destroy();
532 #ifdef HAVE_NT_EVENT_LOG
534 LogSlapdStoppedEvent( serverName );
538 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
539 "main: slapd stopped.\n" ));
541 Debug( LDAP_DEBUG_ANY, "slapd stopped.\n", 0, 0, 0 );
545 #ifdef HAVE_NT_SERVICE_MANAGER
546 ReportSlapdShutdownComplete();
552 slapd_daemon_destroy();
557 ldap_pvt_tls_destroy();
563 mal_dumpleaktrace( leakfile );
573 * Catch and discard terminated child processes, to avoid zombies.
577 wait4child( int sig )
579 int save_errno = errno;
584 while ( waitpid( (pid_t)-1, NULL, WNOHANG ) > 0 || errno == EINTR )
587 while ( wait3( NULL, WNOHANG, NULL ) > 0 || errno == EINTR )
593 (void) SIGNAL_REINSTALL( sig, wait4child );
597 #endif /* SIGCHLD || SIGCLD */
603 * Convert a string to an integer by means of a dispatcher table
604 * if the string is not in the table return the default
608 cnvt_str2int( char *stringVal, STRDISP_P dispatcher, int defaultVal )
610 int retVal = defaultVal;
613 for (disp = dispatcher; disp->stringVal; disp++) {
615 if (!strncasecmp (stringVal, disp->stringVal, disp->abbr)) {
617 retVal = disp->intVal;
626 #endif /* LOG_LOCAL4 */