3 * Copyright 1998-2000 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 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;
139 #ifdef HAVE_NT_SERVICE_MANAGER
140 char *configfile = ".\\slapd.conf";
142 char *configfile = SLAPD_DEFAULT_CONFIGFILE;
144 char *serverName = NULL;
145 int serverMode = SLAP_SERVER_MODE;
149 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 = ch_strdup( argv[0] );
312 serverName = ch_strdup( 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 ( 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 ( schema_prep( ) != 0 ) {
386 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
387 "main: schema prep error\n"));
389 Debug( LDAP_DEBUG_ANY,
390 "schema prep error\n",
398 rc = ldap_pvt_tls_init();
401 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
402 "main: tls init failed: %d\n", rc ));
404 Debug( LDAP_DEBUG_ANY,
405 "main: TLS init failed: %d\n",
409 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
413 rc = ldap_pvt_tls_init_def_ctx();
416 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
417 "main: tls init def ctx failed: %d\n", rc ));
419 Debug( LDAP_DEBUG_ANY,
420 "main: TLS init def ctx failed: %d\n",
424 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
429 (void) SIGNAL( LDAP_SIGUSR1, slap_sig_wake );
430 (void) SIGNAL( LDAP_SIGUSR2, slap_sig_shutdown );
433 (void) SIGNAL( SIGPIPE, SIG_IGN );
436 (void) SIGNAL( SIGHUP, slap_sig_shutdown );
438 (void) SIGNAL( SIGINT, slap_sig_shutdown );
439 (void) SIGNAL( SIGTERM, slap_sig_shutdown );
441 (void) SIGNAL( LDAP_SIGCHLD, wait4child );
444 /* SIGBREAK is generated when Ctrl-Break is pressed. */
445 (void) SIGNAL( SIGBREAK, slap_sig_shutdown );
449 lutil_detach( no_detach, 0 );
450 #endif /* HAVE_WINSOCK */
456 if ( slap_startup( NULL ) != 0 ) {
458 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 21 );
466 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
467 "main: slapd starting.\n" ));
469 Debug( LDAP_DEBUG_ANY, "slapd starting\n", 0, 0, 0 );
473 if (( slapd_pid_file != NULL ) &&
474 (( fp = fopen( slapd_pid_file, "w" )) != NULL ))
476 fprintf( fp, "%d\n", (int) getpid() );
480 if (( slapd_args_file != NULL ) &&
481 (( fp = fopen( slapd_args_file, "w" )) != NULL ))
483 for ( i = 0; i < g_argc; i++ ) {
484 fprintf( fp, "%s ", g_argv[i] );
491 #ifdef HAVE_NT_EVENT_LOG
493 LogSlapdStartedEvent( serverName, slap_debug, configfile, urls );
498 #ifdef HAVE_NT_SERVICE_MANAGER
499 /* Throw away the event that we used during the startup process. */
501 ldap_pvt_thread_cond_destroy( &started_event );
505 /* remember an error during shutdown */
506 rc |= slap_shutdown( NULL );
509 /* remember an error during destroy */
510 rc |= slap_destroy();
519 #ifdef HAVE_NT_EVENT_LOG
521 LogSlapdStoppedEvent( serverName );
525 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
526 "main: slapd stopped.\n" ));
528 Debug( LDAP_DEBUG_ANY, "slapd stopped.\n", 0, 0, 0 );
532 #ifdef HAVE_NT_SERVICE_MANAGER
533 ReportSlapdShutdownComplete();
539 slapd_daemon_destroy();
544 ldap_pvt_tls_destroy();
548 mal_dumpleaktrace( leakfile );
558 * Catch and discard terminated child processes, to avoid zombies.
562 wait4child( int sig )
564 int save_errno = errno;
569 while ( waitpid( (pid_t)-1, NULL, WNOHANG ) > 0 || errno == EINTR )
572 while ( wait3( NULL, WNOHANG, NULL ) > 0 || errno == EINTR )
578 (void) SIGNAL_REINSTALL( sig, wait4child );
582 #endif /* SIGCHLD || SIGCLD */
588 * Convert a string to an integer by means of a dispatcher table
589 * if the string is not in the table return the default
593 cnvt_str2int( char *stringVal, STRDISP_P dispatcher, int defaultVal )
595 int retVal = defaultVal;
598 for (disp = dispatcher; disp->stringVal; disp++) {
600 if (!strncasecmp (stringVal, disp->stringVal, disp->abbr)) {
602 retVal = disp->intVal;
611 #endif /* LOG_LOCAL4 */