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-p port\tLDAP Port\n"
121 "\t-P port\tLDAP over TLS Port\n"
123 "\t-s level\tSyslog Level\n"
125 "\t-t\t\tEnable BDB2 timing\n"
127 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
128 "\t-u user\tUser (id or name) to ran as\n"
133 #ifdef HAVE_NT_SERVICE_MANAGER
134 void WINAPI ServiceMain( DWORD argc, LPTSTR *argv )
136 int main( int argc, char **argv )
139 int i, no_detach = 0;
142 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
143 char *username = NULL;
144 char *groupname = NULL;
147 int syslogUser = DEFAULT_SYSLOG_USER;
150 #ifdef HAVE_NT_EVENT_LOG
151 char *NTservice = SERVICE_NAME;
153 #ifdef HAVE_NT_SERVICE_MANAGER
154 char *configfile = ".\\slapd.conf";
156 char *configfile = SLAPD_DEFAULT_CONFIGFILE;
159 int serverMode = SLAP_SERVER_MODE;
161 int port = LDAP_PORT;
163 int tls_port = LDAPS_PORT;
170 if( ( leakfile = fopen( "slapd.leak", "w" )) == NULL ) {
178 #ifdef HAVE_NT_SERVICE_MANAGER
183 if ( is_NT_Service ) {
184 CommenceStartupProcessing( NTservice, slap_sig_shutdown );
187 i = (int*)getRegParam( NULL, "Port" );
191 Debug ( LDAP_DEBUG_ANY, "new port from registry is: %d\n", port, 0, 0 );
194 i = (int*)getRegParam( NULL, "TLSPort" );
198 Debug ( LDAP_DEBUG_ANY, "new TLS port from registry is: %d\n", tls_port, 0, 0 );
201 i = (int*)getRegParam( NULL, "DebugLevel" );
205 Debug( LDAP_DEBUG_ANY, "new debug level from registry is: %d\n", slap_debug, 0, 0 );
207 newConfigFile = (char*)getRegParam( NULL, "ConfigFile" );
208 if ( newConfigFile != NULL )
210 configfile = newConfigFile;
211 Debug ( LDAP_DEBUG_ANY, "new config file from registry is: %s\n", configfile, 0, 0 );
216 while ( (i = getopt( argc, argv,
224 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
227 #ifdef LDAP_CONNECTIONLESS
230 #ifdef HAVE_NT_EVENT_LOG
238 case 'h': /* listen URLs */
239 if ( urls != NULL ) free( urls );
240 urls = ch_strdup( optarg );
243 case 'd': /* set debug level and 'do not detach' flag */
246 slap_debug |= atoi( optarg );
248 if ( atoi( optarg ) != 0 )
249 fputs( "must compile with LDAP_DEBUG for debugging\n",
254 case 'f': /* read config file */
255 configfile = ch_strdup( optarg );
258 case 'p': { /* port on which to listen */
259 int p = atoi( optarg );
261 fprintf(stderr, "-p %s must be numeric\n", optarg);
262 } else if( p < 0 || p >= 1<<16) {
263 fprintf(stderr, "-p %s invalid\n", optarg);
270 case 'P': { /* port on which to listen for TLS */
271 int p = atoi( optarg );
273 fprintf(stderr, "-P %s must be numeric\n", optarg);
274 } else if( p < 0 || p >= 1<<16) {
275 fprintf(stderr, "-P %s invalid\n", optarg);
282 case 's': /* set syslog level */
283 ldap_syslog = atoi( optarg );
287 case 'l': /* set syslog local user */
288 syslogUser = cnvt_str2int( optarg,
289 syslog_types, DEFAULT_SYSLOG_USER );
293 #ifdef LDAP_CONNECTIONLESS
294 case 'c': /* do connectionless (udp) */
296 fprintf( stderr, "connectionless support not supported");
297 exit( EXIT_FAILURE );
302 case 't': /* timed server */
303 serverMode |= SLAP_TIMED_MODE;
307 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
308 case 'u': /* user name */
309 if( username ) free(username);
310 username = ch_strdup( optarg );
313 case 'g': /* group name */
314 if( groupname ) free(groupname);
315 groupname = ch_strdup( optarg );
317 #endif /* SETUID && GETUID */
319 #ifdef HAVE_NT_EVENT_LOG
320 case 'n': /* NT service name */
321 NTservice = ch_strdup( optarg );
327 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 15 );
332 ber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &slap_debug);
333 ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &slap_debug);
334 ldif_debug = slap_debug;
336 Debug( LDAP_DEBUG_TRACE, "%s", Versionstr, 0, 0 );
338 if ( (serverName = strrchr( argv[0], *LDAP_DIRSEP )) == NULL ) {
339 serverName = ch_strdup( argv[0] );
341 serverName = ch_strdup( serverName + 1 );
345 openlog( serverName, OPENLOG_OPTIONS, syslogUser );
347 openlog( serverName, OPENLOG_OPTIONS );
350 if( slapd_daemon_init( urls, port, tls_port ) != 0 ) {
352 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 16 );
356 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
357 if ( username != NULL || groupname != NULL ) {
358 slap_init_user( username, groupname );
363 if ( module_init() != 0 ) {
365 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 17 );
370 if ( slap_init( serverMode, serverName ) != 0 ) {
372 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 18 );
376 if ( read_config( configfile ) != 0 ) {
378 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 19 );
384 ldap_pvt_tls_init_def_ctx();
387 (void) SIGNAL( LDAP_SIGUSR1, slap_sig_wake );
388 (void) SIGNAL( LDAP_SIGUSR2, slap_sig_shutdown );
391 (void) SIGNAL( SIGPIPE, SIG_IGN );
394 (void) SIGNAL( SIGHUP, slap_sig_shutdown );
396 (void) SIGNAL( SIGINT, slap_sig_shutdown );
397 (void) SIGNAL( SIGTERM, slap_sig_shutdown );
399 (void) SIGNAL( LDAP_SIGCHLD, wait4child );
402 /* SIGBREAK is generated when Ctrl-Break is pressed. */
403 (void) SIGNAL( SIGBREAK, slap_sig_shutdown );
407 lutil_detach( no_detach, 0 );
408 #endif /* HAVE_WINSOCK */
414 if ( slap_startup( NULL ) != 0 ) {
416 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
423 Debug( LDAP_DEBUG_ANY, "slapd starting\n", 0, 0, 0 );
425 if (( slapd_pid_file != NULL ) &&
426 (( fp = fopen( slapd_pid_file, "w" )) != NULL ))
428 fprintf( fp, "%d\n", (int) getpid() );
432 if (( slapd_args_file != NULL ) &&
433 (( fp = fopen( slapd_args_file, "w" )) != NULL ))
435 for ( i = 0; i < g_argc; i++ ) {
436 fprintf( fp, "%s ", g_argv[i] );
443 #ifdef HAVE_NT_EVENT_LOG
444 LogSlapdStartedEvent( NTservice, slap_debug, configfile, urls );
449 #ifdef HAVE_NT_SERVICE_MANAGER
450 /* Throw away the event that we used during the startup process. */
452 ldap_pvt_thread_cond_destroy( &started_event );
456 /* remember an error during shutdown */
457 rc |= slap_shutdown( NULL );
460 /* remember an error during destroy */
461 rc |= slap_destroy();
468 #ifdef HAVE_NT_EVENT_LOG
469 LogSlapdStoppedEvent( NTservice );
472 Debug( LDAP_DEBUG_ANY, "slapd stopped.\n", 0, 0, 0 );
474 #ifdef HAVE_NT_SERVICE_MANAGER
475 ReportSlapdShutdownComplete();
481 slapd_daemon_destroy();
484 mal_dumpleaktrace( leakfile );
494 * Catch and discard terminated child processes, to avoid zombies.
498 wait4child( int sig )
500 int save_errno = errno;
505 while ( waitpid( (pid_t)-1, NULL, WNOHANG ) >= 0 || errno == EINTR )
508 while ( wait3( NULL, WNOHANG, NULL ) >= 0 || errno == EINTR )
514 (void) SIGNAL( sig, wait4child );
518 #endif /* SIGCHLD || SIGCLD */
524 * Convert a string to an integer by means of a dispatcher table
525 * if the string is not in the table return the default
529 cnvt_str2int( char *stringVal, STRDISP_P dispatcher, int defaultVal )
531 int retVal = defaultVal;
534 for (disp = dispatcher; disp->stringVal; disp++) {
536 if (!strncasecmp (stringVal, disp->stringVal, disp->abbr)) {
538 retVal = disp->intVal;
547 #endif /* LOG_LOCAL4 */