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>
24 /* I guess this should be declared in ldap_pvt.h */
25 extern int ldap_pvt_tls_init_def_ctx( void );
28 static RETSIGTYPE wait4child( int sig );
31 #ifdef HAVE_NT_SERVICE_MANAGER
32 #define MAIN_RETURN(x) return
33 struct sockaddr_in bind_addr;
36 LDAP_LUTIL_V(SERVICE_STATUS) SLAPDServiceStatus;
37 LDAP_LUTIL_V(SERVICE_STATUS_HANDLE) hSLAPDServiceStatus;
38 extern ldap_pvt_thread_cond_t started_event, stopped_event;
39 extern int is_NT_Service;
41 void CommenceStartupProcessing( LPCTSTR serverName,
43 void ReportSlapdShutdownComplete( void );
44 void *getRegParam( char *svc, char *value );
46 #define SERVICE_EXIT( e, n ) do { \
47 if ( is_NT_Service ) { \
48 SLAPDServiceStatus.dwWin32ExitCode = (e); \
49 SLAPDServiceStatus.dwServiceSpecificExitCode = (n); \
54 #define SERVICE_EXIT( e, n )
55 #define MAIN_RETURN(x) return(x)
58 #ifdef HAVE_NT_EVENT_LOG
59 void LogSlapdStartedEvent( char *svc, int slap_debug, char *configfile, char *urls );
60 void LogSlapdStoppedEvent( char *svc );
64 * when more than one slapd is running on one machine, each one might have
65 * it's own LOCAL for syslogging and must have its own pid/args files
68 #ifndef HAVE_MKVERSION
69 const char Versionstr[] =
70 OPENLDAP_PACKAGE " " OPENLDAP_VERSION " Standalone LDAP Server (slapd)";
75 #define DEFAULT_SYSLOG_USER LOG_LOCAL4
77 typedef struct _str2intDispatch {
81 } STRDISP, *STRDISP_P;
84 /* table to compute syslog-options to integer */
85 static STRDISP syslog_types[] = {
86 { "LOCAL0", sizeof("LOCAL0"), LOG_LOCAL0 },
87 { "LOCAL1", sizeof("LOCAL1"), LOG_LOCAL1 },
88 { "LOCAL2", sizeof("LOCAL2"), LOG_LOCAL2 },
89 { "LOCAL3", sizeof("LOCAL3"), LOG_LOCAL3 },
90 { "LOCAL4", sizeof("LOCAL4"), LOG_LOCAL4 },
91 { "LOCAL5", sizeof("LOCAL5"), LOG_LOCAL5 },
92 { "LOCAL6", sizeof("LOCAL6"), LOG_LOCAL6 },
93 { "LOCAL7", sizeof("LOCAL7"), LOG_LOCAL7 },
97 static int cnvt_str2int( char *, STRDISP_P, int );
99 #endif /* LOG_LOCAL4 */
106 "usage: %s options\n", name );
108 "\t-d level\tDebug Level" "\n"
109 "\t-f filename\tConfiguration File\n"
110 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
111 "\t-g group\tGroup (id or name) to ran as\n"
113 "\t-h URLs\tList of URLs to serve\n"
115 "\t-l sysloguser\tSyslog User (default: LOCAL4)\n"
117 "\t-n serverName\tservice name\n"
121 "\t-s level\tSyslog Level\n"
122 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
123 "\t-u user\tUser (id or name) to ran as\n"
128 #ifdef HAVE_NT_SERVICE_MANAGER
129 void WINAPI ServiceMain( DWORD argc, LPTSTR *argv )
131 int main( int argc, char **argv )
134 int i, no_detach = 0;
137 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
138 char *username = NULL;
139 char *groupname = NULL;
141 #if defined(HAVE_CHROOT)
142 char *sandbox = NULL;
145 int syslogUser = DEFAULT_SYSLOG_USER;
148 #ifdef HAVE_NT_SERVICE_MANAGER
149 char *configfile = ".\\slapd.conf";
151 char *configfile = SLAPD_DEFAULT_CONFIGFILE;
153 char *serverName = NULL;
154 int serverMode = SLAP_SERVER_MODE;
158 if( ( leakfile = fopen( "slapd.leak", "w" )) == NULL ) {
166 #ifdef HAVE_NT_SERVICE_MANAGER
171 char *regService = NULL;
173 if ( is_NT_Service ) {
174 serverName = argv[0];
175 CommenceStartupProcessing( serverName, slap_sig_shutdown );
176 if ( strcmp(serverName, SERVICE_NAME) )
177 regService = serverName;
180 i = (int*)getRegParam( regService, "DebugLevel" );
185 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
186 "main: new debug level from registry is: %d\n", slap_debug ));
188 Debug( LDAP_DEBUG_ANY, "new debug level from registry is: %d\n", slap_debug, 0, 0 );
192 newUrls = (char *) getRegParam(regService, "Urls");
198 urls = ch_strdup(newUrls);
200 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
201 "main: new urls from registry: %s\n", urls ));
203 Debug(LDAP_DEBUG_ANY, "new urls from registry: %s\n",
209 newConfigFile = (char*)getRegParam( regService, "ConfigFile" );
210 if ( newConfigFile != NULL )
212 configfile = newConfigFile;
214 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
215 "main: new config file from registry is: %s\n", configfile ));
217 Debug ( LDAP_DEBUG_ANY, "new config file from registry is: %s\n", configfile, 0, 0 );
224 while ( (i = getopt( argc, argv,
232 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
237 case 'h': /* listen URLs */
238 if ( urls != NULL ) free( urls );
239 urls = ch_strdup( optarg );
242 case 'd': /* set debug level and 'do not detach' flag */
245 slap_debug |= atoi( optarg );
247 if ( atoi( optarg ) != 0 )
248 fputs( "must compile with LDAP_DEBUG for debugging\n",
253 case 'f': /* read config file */
254 configfile = ch_strdup( optarg );
257 case 's': /* set syslog level */
258 ldap_syslog = atoi( optarg );
262 case 'l': /* set syslog local user */
263 syslogUser = cnvt_str2int( optarg,
264 syslog_types, DEFAULT_SYSLOG_USER );
270 if( sandbox ) free(sandbox);
271 sandbox = ch_strdup( optarg );
275 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
276 case 'u': /* user name */
277 if( username ) free(username);
278 username = ch_strdup( optarg );
281 case 'g': /* group name */
282 if( groupname ) free(groupname);
283 groupname = ch_strdup( optarg );
285 #endif /* SETUID && GETUID */
287 case 'n': /* NT service name */
288 if( serverName != NULL ) free( serverName );
289 serverName = ch_strdup( optarg );
295 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 15 );
301 lutil_log_initialize( argc, argv );
304 lutil_set_debug_level( "slapd", slap_debug );
305 ber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &slap_debug);
306 ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &slap_debug);
307 ldif_debug = slap_debug;
310 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
313 Debug( LDAP_DEBUG_TRACE, "%s", Versionstr, 0, 0 );
317 if( serverName == NULL ) {
318 if ( (serverName = strrchr( argv[0], *LDAP_DIRSEP )) == NULL ) {
319 serverName = ch_strdup( argv[0] );
321 serverName = ch_strdup( serverName + 1 );
326 openlog( serverName, OPENLOG_OPTIONS, syslogUser );
328 openlog( serverName, OPENLOG_OPTIONS );
331 if( slapd_daemon_init( urls ) != 0 ) {
333 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 16 );
337 #if defined(HAVE_CHROOT)
339 if ( chdir( sandbox ) ) {
344 if ( chroot( sandbox ) ) {
352 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
353 if ( username != NULL || groupname != NULL ) {
354 slap_init_user( username, groupname );
361 if ( module_init() != 0 ) {
363 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 17 );
368 if ( slap_init( serverMode, serverName ) != 0 ) {
370 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 18 );
374 if ( schema_init( ) != 0 ) {
376 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
377 "main: schema initialization error\n" ));
379 Debug( LDAP_DEBUG_ANY,
380 "schema initialization error\n",
387 if ( read_config( configfile ) != 0 ) {
389 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 19 );
393 if ( schema_prep( ) != 0 ) {
395 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
396 "main: schema prep error\n"));
398 Debug( LDAP_DEBUG_ANY,
399 "schema prep error\n",
407 rc = ldap_pvt_tls_init();
410 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
411 "main: tls init failed: %d\n", rc ));
413 Debug( LDAP_DEBUG_ANY,
414 "main: TLS init failed: %d\n",
418 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
422 rc = ldap_pvt_tls_init_def_ctx();
425 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
426 "main: tls init def ctx failed: %d\n", rc ));
428 Debug( LDAP_DEBUG_ANY,
429 "main: TLS init def ctx failed: %d\n",
433 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
438 (void) SIGNAL( LDAP_SIGUSR1, slap_sig_wake );
439 (void) SIGNAL( LDAP_SIGUSR2, slap_sig_shutdown );
442 (void) SIGNAL( SIGPIPE, SIG_IGN );
445 (void) SIGNAL( SIGHUP, slap_sig_shutdown );
447 (void) SIGNAL( SIGINT, slap_sig_shutdown );
448 (void) SIGNAL( SIGTERM, slap_sig_shutdown );
450 (void) SIGNAL( LDAP_SIGCHLD, wait4child );
453 /* SIGBREAK is generated when Ctrl-Break is pressed. */
454 (void) SIGNAL( SIGBREAK, slap_sig_shutdown );
458 lutil_detach( no_detach, 0 );
459 #endif /* HAVE_WINSOCK */
465 if ( slap_startup( NULL ) != 0 ) {
467 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 21 );
475 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
476 "main: slapd starting.\n" ));
478 Debug( LDAP_DEBUG_ANY, "slapd starting\n", 0, 0, 0 );
482 if (( slapd_pid_file != NULL ) &&
483 (( fp = fopen( slapd_pid_file, "w" )) != NULL ))
485 fprintf( fp, "%d\n", (int) getpid() );
489 if (( slapd_args_file != NULL ) &&
490 (( fp = fopen( slapd_args_file, "w" )) != NULL ))
492 for ( i = 0; i < g_argc; i++ ) {
493 fprintf( fp, "%s ", g_argv[i] );
500 #ifdef HAVE_NT_EVENT_LOG
502 LogSlapdStartedEvent( serverName, slap_debug, configfile, urls );
507 #ifdef HAVE_NT_SERVICE_MANAGER
508 /* Throw away the event that we used during the startup process. */
510 ldap_pvt_thread_cond_destroy( &started_event );
514 /* remember an error during shutdown */
515 rc |= slap_shutdown( NULL );
518 /* remember an error during destroy */
519 rc |= slap_destroy();
528 #ifdef HAVE_NT_EVENT_LOG
530 LogSlapdStoppedEvent( serverName );
534 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
535 "main: slapd stopped.\n" ));
537 Debug( LDAP_DEBUG_ANY, "slapd stopped.\n", 0, 0, 0 );
541 #ifdef HAVE_NT_SERVICE_MANAGER
542 ReportSlapdShutdownComplete();
548 slapd_daemon_destroy();
553 ldap_pvt_tls_destroy();
557 mal_dumpleaktrace( leakfile );
567 * Catch and discard terminated child processes, to avoid zombies.
571 wait4child( int sig )
573 int save_errno = errno;
578 while ( waitpid( (pid_t)-1, NULL, WNOHANG ) > 0 || errno == EINTR )
581 while ( wait3( NULL, WNOHANG, NULL ) > 0 || errno == EINTR )
587 (void) SIGNAL_REINSTALL( sig, wait4child );
591 #endif /* SIGCHLD || SIGCLD */
597 * Convert a string to an integer by means of a dispatcher table
598 * if the string is not in the table return the default
602 cnvt_str2int( char *stringVal, STRDISP_P dispatcher, int defaultVal )
604 int retVal = defaultVal;
607 for (disp = dispatcher; disp->stringVal; disp++) {
609 if (!strncasecmp (stringVal, disp->stringVal, disp->abbr)) {
611 retVal = disp->intVal;
620 #endif /* LOG_LOCAL4 */