3 * Copyright 1998-1999 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>
16 #include <ac/signal.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 LIBLUTIL_F (SERVICE_STATUS) SLAPDServiceStatus;
32 LIBLUTIL_F (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 serviceName,
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 #if LDAP_CONNECTIONLESS
104 "\t-c\t\tEnable (experimental) Connectionless LDAP\n"
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 #ifdef HAVE_NT_EVENT_LOG
116 "\t-n NTserviceName\tNT service name\n"
119 "\t-s level\tSyslog Level\n"
121 "\t-t\t\tEnable BDB2 timing\n"
123 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
124 "\t-u user\tUser (id or name) to ran as\n"
129 #ifdef HAVE_NT_SERVICE_MANAGER
130 void WINAPI ServiceMain( DWORD argc, LPTSTR *argv )
132 int main( int argc, char **argv )
135 int i, no_detach = 0;
138 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
139 char *username = NULL;
140 char *groupname = NULL;
143 int syslogUser = DEFAULT_SYSLOG_USER;
146 #ifdef HAVE_NT_EVENT_LOG
147 char *NTservice = SERVICE_NAME;
149 #ifdef HAVE_NT_SERVICE_MANAGER
150 char *configfile = ".\\slapd.conf";
152 char *configfile = SLAPD_DEFAULT_CONFIGFILE;
155 int serverMode = SLAP_SERVER_MODE;
159 if( ( leakfile = fopen( "slapd.leak", "w" )) == NULL ) {
167 #ifdef HAVE_NT_SERVICE_MANAGER
172 if ( is_NT_Service ) {
173 CommenceStartupProcessing( NTservice, slap_sig_shutdown );
176 i = (int*)getRegParam( NULL, "DebugLevel" );
180 Debug( LDAP_DEBUG_ANY, "new debug level from registry is: %d\n", slap_debug, 0, 0 );
182 newConfigFile = (char*)getRegParam( NULL, "ConfigFile" );
183 if ( newConfigFile != NULL )
185 configfile = newConfigFile;
186 Debug ( LDAP_DEBUG_ANY, "new config file from registry is: %s\n", configfile, 0, 0 );
191 while ( (i = getopt( argc, argv,
199 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
202 #ifdef LDAP_CONNECTIONLESS
205 #ifdef HAVE_NT_EVENT_LOG
210 case 'h': /* listen URLs */
211 if ( urls != NULL ) free( urls );
212 urls = ch_strdup( optarg );
215 case 'd': /* set debug level and 'do not detach' flag */
218 slap_debug |= atoi( optarg );
220 if ( atoi( optarg ) != 0 )
221 fputs( "must compile with LDAP_DEBUG for debugging\n",
226 case 'f': /* read config file */
227 configfile = ch_strdup( optarg );
230 case 's': /* set syslog level */
231 ldap_syslog = atoi( optarg );
235 case 'l': /* set syslog local user */
236 syslogUser = cnvt_str2int( optarg,
237 syslog_types, DEFAULT_SYSLOG_USER );
241 #ifdef LDAP_CONNECTIONLESS
242 case 'c': /* do connectionless (udp) */
244 fprintf( stderr, "connectionless support not supported");
245 exit( EXIT_FAILURE );
250 case 't': /* timed server */
251 serverMode |= SLAP_TIMED_MODE;
255 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
256 case 'u': /* user name */
257 if( username ) free(username);
258 username = ch_strdup( optarg );
261 case 'g': /* group name */
262 if( groupname ) free(groupname);
263 groupname = ch_strdup( optarg );
265 #endif /* SETUID && GETUID */
267 #ifdef HAVE_NT_EVENT_LOG
268 case 'n': /* NT service name */
269 NTservice = ch_strdup( optarg );
275 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 15 );
280 ber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &slap_debug);
281 ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &slap_debug);
282 ldif_debug = slap_debug;
284 Debug( LDAP_DEBUG_TRACE, "%s", Versionstr, 0, 0 );
286 if ( (serverName = strrchr( argv[0], *LDAP_DIRSEP )) == NULL ) {
287 serverName = ch_strdup( argv[0] );
289 serverName = ch_strdup( serverName + 1 );
293 openlog( serverName, OPENLOG_OPTIONS, syslogUser );
295 openlog( serverName, OPENLOG_OPTIONS );
298 if( slapd_daemon_init( urls ) != 0 ) {
300 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 16 );
304 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
305 if ( username != NULL || groupname != NULL ) {
306 slap_init_user( username, groupname );
313 if ( module_init() != 0 ) {
315 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 17 );
320 if ( slap_init( serverMode, serverName ) != 0 ) {
322 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 18 );
326 if ( read_config( configfile ) != 0 ) {
328 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 19 );
334 ldap_pvt_tls_init_def_ctx();
337 (void) SIGNAL( LDAP_SIGUSR1, slap_sig_wake );
338 (void) SIGNAL( LDAP_SIGUSR2, slap_sig_shutdown );
341 (void) SIGNAL( SIGPIPE, SIG_IGN );
344 (void) SIGNAL( SIGHUP, slap_sig_shutdown );
346 (void) SIGNAL( SIGINT, slap_sig_shutdown );
347 (void) SIGNAL( SIGTERM, slap_sig_shutdown );
349 (void) SIGNAL( LDAP_SIGCHLD, wait4child );
352 /* SIGBREAK is generated when Ctrl-Break is pressed. */
353 (void) SIGNAL( SIGBREAK, slap_sig_shutdown );
357 lutil_detach( no_detach, 0 );
358 #endif /* HAVE_WINSOCK */
364 if ( slap_startup( NULL ) != 0 ) {
366 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
373 Debug( LDAP_DEBUG_ANY, "slapd starting\n", 0, 0, 0 );
375 if (( slapd_pid_file != NULL ) &&
376 (( fp = fopen( slapd_pid_file, "w" )) != NULL ))
378 fprintf( fp, "%d\n", (int) getpid() );
382 if (( slapd_args_file != NULL ) &&
383 (( fp = fopen( slapd_args_file, "w" )) != NULL ))
385 for ( i = 0; i < g_argc; i++ ) {
386 fprintf( fp, "%s ", g_argv[i] );
393 #ifdef HAVE_NT_EVENT_LOG
394 LogSlapdStartedEvent( NTservice, slap_debug, configfile, urls );
399 #ifdef HAVE_NT_SERVICE_MANAGER
400 /* Throw away the event that we used during the startup process. */
402 ldap_pvt_thread_cond_destroy( &started_event );
406 /* remember an error during shutdown */
407 rc |= slap_shutdown( NULL );
410 /* remember an error during destroy */
411 rc |= slap_destroy();
420 #ifdef HAVE_NT_EVENT_LOG
421 LogSlapdStoppedEvent( NTservice );
424 Debug( LDAP_DEBUG_ANY, "slapd stopped.\n", 0, 0, 0 );
426 #ifdef HAVE_NT_SERVICE_MANAGER
427 ReportSlapdShutdownComplete();
433 slapd_daemon_destroy();
436 mal_dumpleaktrace( leakfile );
446 * Catch and discard terminated child processes, to avoid zombies.
450 wait4child( int sig )
452 int save_errno = errno;
457 while ( waitpid( (pid_t)-1, NULL, WNOHANG ) >= 0 || errno == EINTR )
460 while ( wait3( NULL, WNOHANG, NULL ) >= 0 || errno == EINTR )
466 (void) SIGNAL( sig, wait4child );
470 #endif /* SIGCHLD || SIGCLD */
476 * Convert a string to an integer by means of a dispatcher table
477 * if the string is not in the table return the default
481 cnvt_str2int( char *stringVal, STRDISP_P dispatcher, int defaultVal )
483 int retVal = defaultVal;
486 for (disp = dispatcher; disp->stringVal; disp++) {
488 if (!strncasecmp (stringVal, disp->stringVal, disp->abbr)) {
490 retVal = disp->intVal;
499 #endif /* LOG_LOCAL4 */