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>
23 static RETSIGTYPE wait4child( int sig );
26 #ifdef HAVE_NT_SERVICE_MANAGER
27 #define MAIN_RETURN(x) return
28 struct sockaddr_in bind_addr;
31 LDAP_LUTIL_V(SERVICE_STATUS) SLAPDServiceStatus;
32 LDAP_LUTIL_V(SERVICE_STATUS_HANDLE) hSLAPDServiceStatus;
33 extern ldap_pvt_thread_cond_t started_event, stopped_event;
34 extern int is_NT_Service;
36 void CommenceStartupProcessing( LPCTSTR serverName,
38 void ReportSlapdShutdownComplete( void );
39 void *getRegParam( char *svc, char *value );
41 #define SERVICE_EXIT( e, n ) do { \
42 if ( is_NT_Service ) { \
43 SLAPDServiceStatus.dwWin32ExitCode = (e); \
44 SLAPDServiceStatus.dwServiceSpecificExitCode = (n); \
49 #define SERVICE_EXIT( e, n )
50 #define MAIN_RETURN(x) return(x)
53 #ifdef HAVE_NT_EVENT_LOG
54 void LogSlapdStartedEvent( char *svc, int slap_debug, char *configfile, char *urls );
55 void LogSlapdStoppedEvent( char *svc );
59 * when more than one slapd is running on one machine, each one might have
60 * it's own LOCAL for syslogging and must have its own pid/args files
63 #ifndef HAVE_MKVERSION
64 const char Versionstr[] =
65 OPENLDAP_PACKAGE " " OPENLDAP_VERSION " Standalone LDAP Server (slapd)";
70 #define DEFAULT_SYSLOG_USER LOG_LOCAL4
72 typedef struct _str2intDispatch {
76 } STRDISP, *STRDISP_P;
79 /* table to compute syslog-options to integer */
80 static STRDISP syslog_types[] = {
81 { "LOCAL0", sizeof("LOCAL0"), LOG_LOCAL0 },
82 { "LOCAL1", sizeof("LOCAL1"), LOG_LOCAL1 },
83 { "LOCAL2", sizeof("LOCAL2"), LOG_LOCAL2 },
84 { "LOCAL3", sizeof("LOCAL3"), LOG_LOCAL3 },
85 { "LOCAL4", sizeof("LOCAL4"), LOG_LOCAL4 },
86 { "LOCAL5", sizeof("LOCAL5"), LOG_LOCAL5 },
87 { "LOCAL6", sizeof("LOCAL6"), LOG_LOCAL6 },
88 { "LOCAL7", sizeof("LOCAL7"), LOG_LOCAL7 },
92 static int cnvt_str2int( char *, STRDISP_P, int );
94 #endif /* LOG_LOCAL4 */
101 "usage: %s options\n", name );
103 "\t-d level\tDebug Level" "\n"
104 "\t-f filename\tConfiguration File\n"
105 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
106 "\t-g group\tGroup (id or name) to ran as\n"
108 "\t-h URLs\tList of URLs to serve\n"
110 "\t-l sysloguser\tSyslog User (default: LOCAL4)\n"
112 "\t-n serverName\tservice name\n"
116 "\t-s level\tSyslog Level\n"
117 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
118 "\t-u user\tUser (id or name) to ran as\n"
123 #ifdef HAVE_NT_SERVICE_MANAGER
124 void WINAPI ServiceMain( DWORD argc, LPTSTR *argv )
126 int main( int argc, char **argv )
129 int i, no_detach = 0;
132 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
133 char *username = NULL;
134 char *groupname = NULL;
136 #if defined(HAVE_CHROOT)
137 char *sandbox = NULL;
140 int syslogUser = DEFAULT_SYSLOG_USER;
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 ) {
161 #ifdef HAVE_NT_SERVICE_MANAGER
166 char *regService = NULL;
168 if ( is_NT_Service ) {
169 serverName = argv[0];
170 CommenceStartupProcessing( serverName, slap_sig_shutdown );
171 if ( strcmp(serverName, SERVICE_NAME) )
172 regService = serverName;
175 i = (int*)getRegParam( regService, "DebugLevel" );
180 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
181 "main: new debug level from registry is: %d\n", slap_debug ));
183 Debug( LDAP_DEBUG_ANY, "new debug level from registry is: %d\n", slap_debug, 0, 0 );
187 newUrls = (char *) getRegParam(regService, "Urls");
193 urls = ch_strdup(newUrls);
195 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
196 "main: new urls from registry: %s\n", urls ));
198 Debug(LDAP_DEBUG_ANY, "new urls from registry: %s\n",
204 newConfigFile = (char*)getRegParam( regService, "ConfigFile" );
205 if ( newConfigFile != NULL )
207 configfile = newConfigFile;
209 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
210 "main: new config file from registry is: %s\n", configfile ));
212 Debug ( LDAP_DEBUG_ANY, "new config file from registry is: %s\n", configfile, 0, 0 );
219 while ( (i = getopt( argc, argv,
227 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
232 case 'h': /* listen URLs */
233 if ( urls != NULL ) free( urls );
234 urls = ch_strdup( optarg );
237 case 'd': /* set debug level and 'do not detach' flag */
240 slap_debug |= atoi( optarg );
242 if ( atoi( optarg ) != 0 )
243 fputs( "must compile with LDAP_DEBUG for debugging\n",
248 case 'f': /* read config file */
249 configfile = ch_strdup( optarg );
252 case 's': /* set syslog level */
253 ldap_syslog = atoi( optarg );
257 case 'l': /* set syslog local user */
258 syslogUser = cnvt_str2int( optarg,
259 syslog_types, DEFAULT_SYSLOG_USER );
265 if( sandbox ) free(sandbox);
266 sandbox = ch_strdup( optarg );
270 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
271 case 'u': /* user name */
272 if( username ) free(username);
273 username = ch_strdup( optarg );
276 case 'g': /* group name */
277 if( groupname ) free(groupname);
278 groupname = ch_strdup( optarg );
280 #endif /* SETUID && GETUID */
282 case 'n': /* NT service name */
283 if( serverName != NULL ) free( serverName );
284 serverName = ch_strdup( optarg );
290 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 15 );
296 lutil_log_initialize( argc, argv );
299 lutil_set_debug_level( "slapd", slap_debug );
300 ber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &slap_debug);
301 ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &slap_debug);
302 ldif_debug = slap_debug;
305 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
308 Debug( LDAP_DEBUG_TRACE, "%s", Versionstr, 0, 0 );
312 if( serverName == NULL ) {
313 if ( (serverName = strrchr( argv[0], *LDAP_DIRSEP )) == NULL ) {
314 serverName = ch_strdup( argv[0] );
316 serverName = ch_strdup( serverName + 1 );
321 openlog( serverName, OPENLOG_OPTIONS, syslogUser );
323 openlog( serverName, OPENLOG_OPTIONS );
326 if( slapd_daemon_init( urls ) != 0 ) {
328 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 16 );
332 #if defined(HAVE_CHROOT)
334 if ( chdir( sandbox ) ) {
339 if ( chroot( sandbox ) ) {
347 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
348 if ( username != NULL || groupname != NULL ) {
349 slap_init_user( username, groupname );
356 if ( module_init() != 0 ) {
358 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 17 );
363 if ( slap_init( serverMode, serverName ) != 0 ) {
365 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 18 );
369 if ( schema_init( ) != 0 ) {
371 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
372 "main: schema initialization error\n" ));
374 Debug( LDAP_DEBUG_ANY,
375 "schema initialization error\n",
382 if ( read_config( configfile ) != 0 ) {
384 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 19 );
388 if ( schema_prep( ) != 0 ) {
390 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
391 "main: schema prep error\n"));
393 Debug( LDAP_DEBUG_ANY,
394 "schema prep error\n",
402 rc = ldap_pvt_tls_init();
405 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
406 "main: tls init failed: %d\n", rc ));
408 Debug( LDAP_DEBUG_ANY,
409 "main: TLS init failed: %d\n",
413 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
417 rc = ldap_pvt_tls_init_def_ctx();
420 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
421 "main: tls init def ctx failed: %d\n", rc ));
423 Debug( LDAP_DEBUG_ANY,
424 "main: TLS init def ctx failed: %d\n",
428 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
433 (void) SIGNAL( LDAP_SIGUSR1, slap_sig_wake );
434 (void) SIGNAL( LDAP_SIGUSR2, slap_sig_shutdown );
437 (void) SIGNAL( SIGPIPE, SIG_IGN );
440 (void) SIGNAL( SIGHUP, slap_sig_shutdown );
442 (void) SIGNAL( SIGINT, slap_sig_shutdown );
443 (void) SIGNAL( SIGTERM, slap_sig_shutdown );
445 (void) SIGNAL( LDAP_SIGCHLD, wait4child );
448 /* SIGBREAK is generated when Ctrl-Break is pressed. */
449 (void) SIGNAL( SIGBREAK, slap_sig_shutdown );
453 lutil_detach( no_detach, 0 );
454 #endif /* HAVE_WINSOCK */
460 if ( slap_startup( NULL ) != 0 ) {
462 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 21 );
470 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
471 "main: slapd starting.\n" ));
473 Debug( LDAP_DEBUG_ANY, "slapd starting\n", 0, 0, 0 );
477 if (( slapd_pid_file != NULL ) &&
478 (( fp = fopen( slapd_pid_file, "w" )) != NULL ))
480 fprintf( fp, "%d\n", (int) getpid() );
484 if (( slapd_args_file != NULL ) &&
485 (( fp = fopen( slapd_args_file, "w" )) != NULL ))
487 for ( i = 0; i < g_argc; i++ ) {
488 fprintf( fp, "%s ", g_argv[i] );
495 #ifdef HAVE_NT_EVENT_LOG
497 LogSlapdStartedEvent( serverName, slap_debug, configfile, urls );
502 #ifdef HAVE_NT_SERVICE_MANAGER
503 /* Throw away the event that we used during the startup process. */
505 ldap_pvt_thread_cond_destroy( &started_event );
509 /* remember an error during shutdown */
510 rc |= slap_shutdown( NULL );
513 /* remember an error during destroy */
514 rc |= slap_destroy();
523 #ifdef HAVE_NT_EVENT_LOG
525 LogSlapdStoppedEvent( serverName );
529 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
530 "main: slapd stopped.\n" ));
532 Debug( LDAP_DEBUG_ANY, "slapd stopped.\n", 0, 0, 0 );
536 #ifdef HAVE_NT_SERVICE_MANAGER
537 ReportSlapdShutdownComplete();
543 slapd_daemon_destroy();
546 mal_dumpleaktrace( leakfile );
556 * Catch and discard terminated child processes, to avoid zombies.
560 wait4child( int sig )
562 int save_errno = errno;
567 while ( waitpid( (pid_t)-1, NULL, WNOHANG ) > 0 || errno == EINTR )
570 while ( wait3( NULL, WNOHANG, NULL ) > 0 || errno == EINTR )
576 (void) SIGNAL_REINSTALL( sig, wait4child );
580 #endif /* SIGCHLD || SIGCLD */
586 * Convert a string to an integer by means of a dispatcher table
587 * if the string is not in the table return the default
591 cnvt_str2int( char *stringVal, STRDISP_P dispatcher, int defaultVal )
593 int retVal = defaultVal;
596 for (disp = dispatcher; disp->stringVal; disp++) {
598 if (!strncasecmp (stringVal, disp->stringVal, disp->abbr)) {
600 retVal = disp->intVal;
609 #endif /* LOG_LOCAL4 */