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;
33 LDAP_LUTIL_V(SERVICE_STATUS) SLAPDServiceStatus;
34 LDAP_LUTIL_V(SERVICE_STATUS_HANDLE) hSLAPDServiceStatus;
35 /* externs are frowned upon, but so is NT :-) */
36 extern ldap_pvt_thread_cond_t started_event, stopped_event;
37 extern int is_NT_Service;
39 void CommenceStartupProcessing( LPCTSTR serverName,
41 void ReportSlapdShutdownComplete( void );
42 void *getRegParam( char *svc, char *value );
44 #define SERVICE_EXIT( e, n ) do { \
45 if ( is_NT_Service ) { \
46 SLAPDServiceStatus.dwWin32ExitCode = (e); \
47 SLAPDServiceStatus.dwServiceSpecificExitCode = (n); \
52 #define SERVICE_EXIT( e, n )
53 #define MAIN_RETURN(x) return(x)
56 #ifdef HAVE_NT_EVENT_LOG
57 void LogSlapdStartedEvent( char *svc, int slap_debug, char *configfile, char *urls );
58 void LogSlapdStoppedEvent( char *svc );
62 * when more than one slapd is running on one machine, each one might have
63 * it's own LOCAL for syslogging and must have its own pid/args files
66 #ifndef HAVE_MKVERSION
67 const char Versionstr[] =
68 OPENLDAP_PACKAGE " " OPENLDAP_VERSION " Standalone LDAP Server (slapd)";
73 #define DEFAULT_SYSLOG_USER LOG_LOCAL4
75 typedef struct _str2intDispatch {
79 } STRDISP, *STRDISP_P;
82 /* table to compute syslog-options to integer */
83 static STRDISP syslog_types[] = {
84 { "LOCAL0", sizeof("LOCAL0"), LOG_LOCAL0 },
85 { "LOCAL1", sizeof("LOCAL1"), LOG_LOCAL1 },
86 { "LOCAL2", sizeof("LOCAL2"), LOG_LOCAL2 },
87 { "LOCAL3", sizeof("LOCAL3"), LOG_LOCAL3 },
88 { "LOCAL4", sizeof("LOCAL4"), LOG_LOCAL4 },
89 { "LOCAL5", sizeof("LOCAL5"), LOG_LOCAL5 },
90 { "LOCAL6", sizeof("LOCAL6"), LOG_LOCAL6 },
91 { "LOCAL7", sizeof("LOCAL7"), LOG_LOCAL7 },
95 static int cnvt_str2int( char *, STRDISP_P, int );
97 #endif /* LOG_LOCAL4 */
104 "usage: %s options\n", name );
106 "\t-d level\tDebug Level" "\n"
107 "\t-f filename\tConfiguration File\n"
108 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
109 "\t-g group\tGroup (id or name) to ran as\n"
111 "\t-h URLs\tList of URLs to serve\n"
113 "\t-l sysloguser\tSyslog User (default: LOCAL4)\n"
115 "\t-n serverName\tservice name\n"
119 "\t-s level\tSyslog Level\n"
120 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
121 "\t-u user\tUser (id or name) to ran as\n"
126 #ifdef HAVE_NT_SERVICE_MANAGER
127 void WINAPI ServiceMain( DWORD argc, LPTSTR *argv )
129 int main( int argc, char **argv )
132 int i, no_detach = 0;
135 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
136 char *username = NULL;
137 char *groupname = NULL;
139 #if defined(HAVE_CHROOT)
140 char *sandbox = NULL;
143 int syslogUser = DEFAULT_SYSLOG_USER;
146 #ifdef HAVE_NT_SERVICE_MANAGER
147 char *configfile = ".\\slapd.conf";
149 char *configfile = SLAPD_DEFAULT_CONFIGFILE;
151 char *serverName = NULL;
152 int serverMode = SLAP_SERVER_MODE;
156 if( ( leakfile = fopen( "slapd.leak", "w" )) == NULL ) {
164 #ifdef HAVE_NT_SERVICE_MANAGER
169 char *regService = NULL;
171 if ( is_NT_Service ) {
172 serverName = argv[0];
173 CommenceStartupProcessing( serverName, slap_sig_shutdown );
174 if ( strcmp(serverName, SERVICE_NAME) )
175 regService = serverName;
178 i = (int*)getRegParam( regService, "DebugLevel" );
183 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
184 "main: new debug level from registry is: %d\n", slap_debug ));
186 Debug( LDAP_DEBUG_ANY, "new debug level from registry is: %d\n", slap_debug, 0, 0 );
190 newUrls = (char *) getRegParam(regService, "Urls");
196 urls = ch_strdup(newUrls);
198 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
199 "main: new urls from registry: %s\n", urls ));
201 Debug(LDAP_DEBUG_ANY, "new urls from registry: %s\n",
207 newConfigFile = (char*)getRegParam( regService, "ConfigFile" );
208 if ( newConfigFile != NULL )
210 configfile = newConfigFile;
212 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
213 "main: new config file from registry is: %s\n", configfile ));
215 Debug ( LDAP_DEBUG_ANY, "new config file from registry is: %s\n", configfile, 0, 0 );
222 while ( (i = getopt( argc, argv,
230 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
235 case 'h': /* listen URLs */
236 if ( urls != NULL ) free( urls );
237 urls = ch_strdup( optarg );
240 case 'd': /* set debug level and 'do not detach' flag */
243 slap_debug |= atoi( optarg );
245 if ( atoi( optarg ) != 0 )
246 fputs( "must compile with LDAP_DEBUG for debugging\n",
251 case 'f': /* read config file */
252 configfile = ch_strdup( optarg );
255 case 's': /* set syslog level */
256 ldap_syslog = atoi( optarg );
260 case 'l': /* set syslog local user */
261 syslogUser = cnvt_str2int( optarg,
262 syslog_types, DEFAULT_SYSLOG_USER );
268 if( sandbox ) free(sandbox);
269 sandbox = ch_strdup( optarg );
273 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
274 case 'u': /* user name */
275 if( username ) free(username);
276 username = ch_strdup( optarg );
279 case 'g': /* group name */
280 if( groupname ) free(groupname);
281 groupname = ch_strdup( optarg );
283 #endif /* SETUID && GETUID */
285 case 'n': /* NT service name */
286 if( serverName != NULL ) free( serverName );
287 serverName = ch_strdup( optarg );
293 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 15 );
299 lutil_log_initialize( argc, argv );
302 lutil_set_debug_level( "slapd", slap_debug );
303 ber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &slap_debug);
304 ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &slap_debug);
305 ldif_debug = slap_debug;
308 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
311 Debug( LDAP_DEBUG_TRACE, "%s", Versionstr, 0, 0 );
315 if( serverName == NULL ) {
316 if ( (serverName = strrchr( argv[0], *LDAP_DIRSEP )) == NULL ) {
317 serverName = ch_strdup( argv[0] );
319 serverName = ch_strdup( serverName + 1 );
324 openlog( serverName, OPENLOG_OPTIONS, syslogUser );
326 openlog( serverName, OPENLOG_OPTIONS );
329 if( slapd_daemon_init( urls ) != 0 ) {
331 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 16 );
335 #if defined(HAVE_CHROOT)
337 if ( chdir( sandbox ) ) {
342 if ( chroot( sandbox ) ) {
350 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
351 if ( username != NULL || groupname != NULL ) {
352 slap_init_user( username, groupname );
359 if ( module_init() != 0 ) {
361 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 17 );
366 if ( slap_init( serverMode, serverName ) != 0 ) {
368 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 18 );
372 if ( schema_init( ) != 0 ) {
374 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
375 "main: schema initialization error\n" ));
377 Debug( LDAP_DEBUG_ANY,
378 "schema initialization error\n",
385 if ( read_config( configfile ) != 0 ) {
387 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 19 );
391 if ( schema_prep( ) != 0 ) {
393 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
394 "main: schema prep error\n"));
396 Debug( LDAP_DEBUG_ANY,
397 "schema prep error\n",
405 rc = ldap_pvt_tls_init();
408 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
409 "main: tls init failed: %d\n", rc ));
411 Debug( LDAP_DEBUG_ANY,
412 "main: TLS init failed: %d\n",
416 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
420 rc = ldap_pvt_tls_init_def_ctx();
423 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
424 "main: tls init def ctx failed: %d\n", rc ));
426 Debug( LDAP_DEBUG_ANY,
427 "main: TLS init def ctx failed: %d\n",
431 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
436 (void) SIGNAL( LDAP_SIGUSR1, slap_sig_wake );
437 (void) SIGNAL( LDAP_SIGUSR2, slap_sig_shutdown );
440 (void) SIGNAL( SIGPIPE, SIG_IGN );
443 (void) SIGNAL( SIGHUP, slap_sig_shutdown );
445 (void) SIGNAL( SIGINT, slap_sig_shutdown );
446 (void) SIGNAL( SIGTERM, slap_sig_shutdown );
448 (void) SIGNAL( LDAP_SIGCHLD, wait4child );
451 /* SIGBREAK is generated when Ctrl-Break is pressed. */
452 (void) SIGNAL( SIGBREAK, slap_sig_shutdown );
456 lutil_detach( no_detach, 0 );
457 #endif /* HAVE_WINSOCK */
463 if ( slap_startup( NULL ) != 0 ) {
465 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 21 );
473 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
474 "main: slapd starting.\n" ));
476 Debug( LDAP_DEBUG_ANY, "slapd starting\n", 0, 0, 0 );
480 if (( slapd_pid_file != NULL ) &&
481 (( fp = fopen( slapd_pid_file, "w" )) != NULL ))
483 fprintf( fp, "%d\n", (int) getpid() );
487 if (( slapd_args_file != NULL ) &&
488 (( fp = fopen( slapd_args_file, "w" )) != NULL ))
490 for ( i = 0; i < g_argc; i++ ) {
491 fprintf( fp, "%s ", g_argv[i] );
498 #ifdef HAVE_NT_EVENT_LOG
500 LogSlapdStartedEvent( serverName, slap_debug, configfile, urls );
505 #ifdef HAVE_NT_SERVICE_MANAGER
506 /* Throw away the event that we used during the startup process. */
508 ldap_pvt_thread_cond_destroy( &started_event );
512 /* remember an error during shutdown */
513 rc |= slap_shutdown( NULL );
516 /* remember an error during destroy */
517 rc |= slap_destroy();
526 #ifdef HAVE_NT_EVENT_LOG
528 LogSlapdStoppedEvent( serverName );
532 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
533 "main: slapd stopped.\n" ));
535 Debug( LDAP_DEBUG_ANY, "slapd stopped.\n", 0, 0, 0 );
539 #ifdef HAVE_NT_SERVICE_MANAGER
540 ReportSlapdShutdownComplete();
546 slapd_daemon_destroy();
551 ldap_pvt_tls_destroy();
555 mal_dumpleaktrace( leakfile );
565 * Catch and discard terminated child processes, to avoid zombies.
569 wait4child( int sig )
571 int save_errno = errno;
576 while ( waitpid( (pid_t)-1, NULL, WNOHANG ) > 0 || errno == EINTR )
579 while ( wait3( NULL, WNOHANG, NULL ) > 0 || errno == EINTR )
585 (void) SIGNAL_REINSTALL( sig, wait4child );
589 #endif /* SIGCHLD || SIGCLD */
595 * Convert a string to an integer by means of a dispatcher table
596 * if the string is not in the table return the default
600 cnvt_str2int( char *stringVal, STRDISP_P dispatcher, int defaultVal )
602 int retVal = defaultVal;
605 for (disp = dispatcher; disp->stringVal; disp++) {
607 if (!strncasecmp (stringVal, disp->stringVal, disp->abbr)) {
609 retVal = disp->intVal;
618 #endif /* LOG_LOCAL4 */