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>
22 static RETSIGTYPE wait4child( int sig );
25 #ifdef HAVE_NT_SERVICE_MANAGER
26 #define MAIN_RETURN(x) return
27 struct sockaddr_in bind_addr;
30 LDAP_LUTIL_V(SERVICE_STATUS) SLAPDServiceStatus;
31 LDAP_LUTIL_V(SERVICE_STATUS_HANDLE) hSLAPDServiceStatus;
32 extern ldap_pvt_thread_cond_t started_event, stopped_event;
33 extern int is_NT_Service;
35 void CommenceStartupProcessing( LPCTSTR serverName,
37 void ReportSlapdShutdownComplete( void );
38 void *getRegParam( char *svc, char *value );
40 #define SERVICE_EXIT( e, n ) do { \
41 if ( is_NT_Service ) { \
42 SLAPDServiceStatus.dwWin32ExitCode = (e); \
43 SLAPDServiceStatus.dwServiceSpecificExitCode = (n); \
48 #define SERVICE_EXIT( e, n )
49 #define MAIN_RETURN(x) return(x)
52 #ifdef HAVE_NT_EVENT_LOG
53 void LogSlapdStartedEvent( char *svc, int slap_debug, char *configfile, char *urls );
54 void LogSlapdStoppedEvent( char *svc );
58 * when more than one slapd is running on one machine, each one might have
59 * it's own LOCAL for syslogging and must have its own pid/args files
62 #ifndef HAVE_MKVERSION
63 const char Versionstr[] =
64 OPENLDAP_PACKAGE " " OPENLDAP_VERSION " Standalone LDAP Server (slapd)";
69 #define DEFAULT_SYSLOG_USER LOG_LOCAL4
71 typedef struct _str2intDispatch {
75 } STRDISP, *STRDISP_P;
78 /* table to compute syslog-options to integer */
79 static STRDISP syslog_types[] = {
80 { "LOCAL0", sizeof("LOCAL0"), LOG_LOCAL0 },
81 { "LOCAL1", sizeof("LOCAL1"), LOG_LOCAL1 },
82 { "LOCAL2", sizeof("LOCAL2"), LOG_LOCAL2 },
83 { "LOCAL3", sizeof("LOCAL3"), LOG_LOCAL3 },
84 { "LOCAL4", sizeof("LOCAL4"), LOG_LOCAL4 },
85 { "LOCAL5", sizeof("LOCAL5"), LOG_LOCAL5 },
86 { "LOCAL6", sizeof("LOCAL6"), LOG_LOCAL6 },
87 { "LOCAL7", sizeof("LOCAL7"), LOG_LOCAL7 },
91 static int cnvt_str2int( char *, STRDISP_P, int );
93 #endif /* LOG_LOCAL4 */
100 "usage: %s options\n", name );
102 "\t-d level\tDebug Level" "\n"
103 "\t-f filename\tConfiguration File\n"
104 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
105 "\t-g group\tGroup (id or name) to ran as\n"
107 "\t-h URLs\tList of URLs to serve\n"
109 "\t-l sysloguser\tSyslog User (default: LOCAL4)\n"
111 "\t-n serverName\tservice name\n"
115 "\t-s level\tSyslog Level\n"
116 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
117 "\t-u user\tUser (id or name) to ran as\n"
122 #ifdef HAVE_NT_SERVICE_MANAGER
123 void WINAPI ServiceMain( DWORD argc, LPTSTR *argv )
125 int main( int argc, char **argv )
128 int i, no_detach = 0;
131 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
132 char *username = NULL;
133 char *groupname = NULL;
135 #if defined(HAVE_CHROOT)
136 char *sandbox = NULL;
139 int syslogUser = DEFAULT_SYSLOG_USER;
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 ) {
160 #ifdef HAVE_NT_SERVICE_MANAGER
165 char *regService = NULL;
167 if ( is_NT_Service ) {
168 serverName = argv[0];
169 CommenceStartupProcessing( serverName, slap_sig_shutdown );
170 if ( strcmp(serverName, SERVICE_NAME) )
171 regService = serverName;
174 i = (int*)getRegParam( regService, "DebugLevel" );
178 Debug( LDAP_DEBUG_ANY, "new debug level from registry is: %d\n", slap_debug, 0, 0 );
181 newUrls = (char *) getRegParam(regService, "Urls");
187 urls = ch_strdup(newUrls);
188 Debug(LDAP_DEBUG_ANY, "new urls from registry: %s\n",
192 newConfigFile = (char*)getRegParam( regService, "ConfigFile" );
193 if ( newConfigFile != NULL )
195 configfile = newConfigFile;
196 Debug ( LDAP_DEBUG_ANY, "new config file from registry is: %s\n", configfile, 0, 0 );
201 while ( (i = getopt( argc, argv,
209 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
214 case 'h': /* listen URLs */
215 if ( urls != NULL ) free( urls );
216 urls = ch_strdup( optarg );
219 case 'd': /* set debug level and 'do not detach' flag */
222 slap_debug |= atoi( optarg );
224 if ( atoi( optarg ) != 0 )
225 fputs( "must compile with LDAP_DEBUG for debugging\n",
230 case 'f': /* read config file */
231 configfile = ch_strdup( optarg );
234 case 's': /* set syslog level */
235 ldap_syslog = atoi( optarg );
239 case 'l': /* set syslog local user */
240 syslogUser = cnvt_str2int( optarg,
241 syslog_types, DEFAULT_SYSLOG_USER );
247 if( sandbox ) free(sandbox);
248 sandbox = ch_strdup( optarg );
252 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
253 case 'u': /* user name */
254 if( username ) free(username);
255 username = ch_strdup( optarg );
258 case 'g': /* group name */
259 if( groupname ) free(groupname);
260 groupname = ch_strdup( optarg );
262 #endif /* SETUID && GETUID */
264 case 'n': /* NT service name */
265 if( serverName != NULL ) free( serverName );
266 serverName = ch_strdup( optarg );
272 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 15 );
277 ber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &slap_debug);
278 ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &slap_debug);
279 ldif_debug = slap_debug;
281 Debug( LDAP_DEBUG_TRACE, "%s", Versionstr, 0, 0 );
283 if( serverName == NULL ) {
284 if ( (serverName = strrchr( argv[0], *LDAP_DIRSEP )) == NULL ) {
285 serverName = ch_strdup( argv[0] );
287 serverName = ch_strdup( serverName + 1 );
292 openlog( serverName, OPENLOG_OPTIONS, syslogUser );
294 openlog( serverName, OPENLOG_OPTIONS );
297 if( slapd_daemon_init( urls ) != 0 ) {
299 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 16 );
303 #if defined(HAVE_CHROOT)
305 if ( chdir( sandbox ) ) {
310 if ( chroot( sandbox ) ) {
318 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
319 if ( username != NULL || groupname != NULL ) {
320 slap_init_user( username, groupname );
327 if ( module_init() != 0 ) {
329 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 17 );
334 if ( slap_init( serverMode, serverName ) != 0 ) {
336 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 18 );
340 if ( schema_init( ) != 0 ) {
341 Debug( LDAP_DEBUG_ANY,
342 "schema initialization error\n",
347 if ( read_config( configfile ) != 0 ) {
349 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 19 );
353 if ( schema_prep( ) != 0 ) {
354 Debug( LDAP_DEBUG_ANY,
355 "schema prep error\n",
361 rc = ldap_pvt_tls_init();
363 if (rc || ldap_pvt_tls_init_def_ctx() != 0) {
365 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
370 (void) SIGNAL( LDAP_SIGUSR1, slap_sig_wake );
371 (void) SIGNAL( LDAP_SIGUSR2, slap_sig_shutdown );
374 (void) SIGNAL( SIGPIPE, SIG_IGN );
377 (void) SIGNAL( SIGHUP, slap_sig_shutdown );
379 (void) SIGNAL( SIGINT, slap_sig_shutdown );
380 (void) SIGNAL( SIGTERM, slap_sig_shutdown );
382 (void) SIGNAL( LDAP_SIGCHLD, wait4child );
385 /* SIGBREAK is generated when Ctrl-Break is pressed. */
386 (void) SIGNAL( SIGBREAK, slap_sig_shutdown );
390 lutil_detach( no_detach, 0 );
391 #endif /* HAVE_WINSOCK */
397 if ( slap_startup( NULL ) != 0 ) {
399 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 21 );
406 Debug( LDAP_DEBUG_ANY, "slapd starting\n", 0, 0, 0 );
408 if (( slapd_pid_file != NULL ) &&
409 (( fp = fopen( slapd_pid_file, "w" )) != NULL ))
411 fprintf( fp, "%d\n", (int) getpid() );
415 if (( slapd_args_file != NULL ) &&
416 (( fp = fopen( slapd_args_file, "w" )) != NULL ))
418 for ( i = 0; i < g_argc; i++ ) {
419 fprintf( fp, "%s ", g_argv[i] );
426 #ifdef HAVE_NT_EVENT_LOG
428 LogSlapdStartedEvent( serverName, slap_debug, configfile, urls );
433 #ifdef HAVE_NT_SERVICE_MANAGER
434 /* Throw away the event that we used during the startup process. */
436 ldap_pvt_thread_cond_destroy( &started_event );
440 /* remember an error during shutdown */
441 rc |= slap_shutdown( NULL );
444 /* remember an error during destroy */
445 rc |= slap_destroy();
454 #ifdef HAVE_NT_EVENT_LOG
456 LogSlapdStoppedEvent( serverName );
459 Debug( LDAP_DEBUG_ANY, "slapd stopped.\n", 0, 0, 0 );
461 #ifdef HAVE_NT_SERVICE_MANAGER
462 ReportSlapdShutdownComplete();
468 slapd_daemon_destroy();
471 mal_dumpleaktrace( leakfile );
481 * Catch and discard terminated child processes, to avoid zombies.
485 wait4child( int sig )
487 int save_errno = errno;
492 while ( waitpid( (pid_t)-1, NULL, WNOHANG ) >= 0 || errno == EINTR )
495 while ( wait3( NULL, WNOHANG, NULL ) >= 0 || errno == EINTR )
501 (void) SIGNAL_REINSTALL( sig, wait4child );
505 #endif /* SIGCHLD || SIGCLD */
511 * Convert a string to an integer by means of a dispatcher table
512 * if the string is not in the table return the default
516 cnvt_str2int( char *stringVal, STRDISP_P dispatcher, int defaultVal )
518 int retVal = defaultVal;
521 for (disp = dispatcher; disp->stringVal; disp++) {
523 if (!strncasecmp (stringVal, disp->stringVal, disp->abbr)) {
525 retVal = disp->intVal;
534 #endif /* LOG_LOCAL4 */