11 #include <ac/signal.h>
14 #include "ldapconfig.h"
16 #include "lutil.h" /* Get lutil_detach() */
19 static RETSIGTYPE wait4child( int sig );
23 * when more than one slapd is running on one machine, each one might have
24 * it's own LOCAL for syslogging and must have its own pid/args files
29 #define DEFAULT_SYSLOG_USER LOG_LOCAL4
31 typedef struct _str2intDispatch {
35 } STRDISP, *STRDISP_P;
38 /* table to compute syslog-options to integer */
39 static STRDISP syslog_types[] = {
40 { "LOCAL0", 6, LOG_LOCAL0 },
41 { "LOCAL1", 6, LOG_LOCAL1 },
42 { "LOCAL2", 6, LOG_LOCAL2 },
43 { "LOCAL3", 6, LOG_LOCAL3 },
44 { "LOCAL4", 6, LOG_LOCAL4 },
45 { "LOCAL5", 6, LOG_LOCAL5 },
46 { "LOCAL6", 6, LOG_LOCAL6 },
47 { "LOCAL7", 6, LOG_LOCAL7 },
51 static int cnvt_str2int( char *, STRDISP_P, int );
53 #endif /* LOG_LOCAL4 */
59 fprintf( stderr, "usage: %s [-d ?|debuglevel] [-f configfile] [-p portnumber] [-s sysloglevel]", name );
61 fprintf( stderr, " [-l sysloguser]" );
63 fprintf( stderr, "\n" );
69 main( int argc, char **argv )
74 struct sockaddr_in bind_addr, *slapd_addr;
77 int syslogUser = DEFAULT_SYSLOG_USER;
81 int serverMode = SLAP_SERVER_MODE;
83 configfile = SLAPD_DEFAULT_CONFIGFILE;
85 (void) memset( (void*) &bind_addr, '\0', sizeof(bind_addr));
86 bind_addr.sin_family = AF_INET;
87 bind_addr.sin_addr.s_addr = htonl(INADDR_ANY);
88 bind_addr.sin_port = htons(LDAP_PORT);
94 while ( (i = getopt( argc, argv, "d:f:ia:p:s:ut" )) != EOF ) {
96 while ( (i = getopt( argc, argv, "d:f:ia:p:s:u" )) != EOF ) {
99 case 'a': /* bind address */
101 if(!(bind_addr.sin_addr.S_un.S_addr = inet_addr(optarg))) {
103 if(!inet_aton(optarg, &bind_addr.sin_addr)) {
105 fprintf(stderr, "invalid address (%s) for -a option", optarg);
110 case 'd': /* turn on debugging */
111 if ( optarg[0] == '?' ) {
112 printf( "Debug levels:\n" );
113 printf( "\tLDAP_DEBUG_TRACE\t%d\n",
115 printf( "\tLDAP_DEBUG_PACKETS\t%d\n",
116 LDAP_DEBUG_PACKETS );
117 printf( "\tLDAP_DEBUG_ARGS\t\t%d\n",
119 printf( "\tLDAP_DEBUG_CONNS\t%d\n",
121 printf( "\tLDAP_DEBUG_BER\t\t%d\n",
123 printf( "\tLDAP_DEBUG_FILTER\t%d\n",
125 printf( "\tLDAP_DEBUG_CONFIG\t%d\n",
127 printf( "\tLDAP_DEBUG_ACL\t\t%d\n",
129 printf( "\tLDAP_DEBUG_STATS\t%d\n",
131 printf( "\tLDAP_DEBUG_STATS2\t%d\n",
133 printf( "\tLDAP_DEBUG_SHELL\t%d\n",
135 printf( "\tLDAP_DEBUG_PARSE\t%d\n",
137 printf( "\tLDAP_DEBUG_ANY\t\t%d\n",
141 slap_debug |= atoi( optarg );
145 case 'd': /* turn on debugging */
147 "must compile with LDAP_DEBUG for debugging\n" );
151 case 'f': /* read config file */
152 configfile = ch_strdup( optarg );
155 case 'i': /* run from inetd */
159 case 'p': { /* port on which to listen */
160 short port = (short)atoi( optarg );
162 fprintf(stderr, "-p %s must be numeric\n", optarg);
164 bind_addr.sin_port = htons(port);
168 case 's': /* set syslog level */
169 ldap_syslog = atoi( optarg );
173 case 'l': /* set syslog local user */
174 syslogUser = cnvt_str2int( optarg, syslog_types,
175 DEFAULT_SYSLOG_USER );
179 case 'u': /* do udp */
184 case 't': /* timed server */
185 serverMode = SLAP_TIMEDSERVER_MODE;
195 lber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &slap_debug);
196 ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &slap_debug);
197 ldif_debug = slap_debug;
199 Debug( LDAP_DEBUG_TRACE, "%s", Versionstr, 0, 0 );
201 if ( (serverName = strrchr( argv[0], '/' )) == NULL ) {
202 serverName = ch_strdup( argv[0] );
204 serverName = ch_strdup( serverName + 1 );
208 openlog( serverName, OPENLOG_OPTIONS, syslogUser );
210 openlog( serverName, OPENLOG_OPTIONS );
213 if ( slap_init( serverMode, serverName ) != 0 ) {
218 if ( read_config( configfile ) != 0 ) {
223 (void) SIGNAL( LDAP_SIGUSR1, slap_do_nothing );
224 (void) SIGNAL( LDAP_SIGUSR2, slap_set_shutdown );
226 (void) SIGNAL( SIGPIPE, SIG_IGN );
229 (void) SIGNAL( SIGHUP, slap_set_shutdown );
231 (void) SIGNAL( SIGINT, slap_set_shutdown );
232 (void) SIGNAL( SIGTERM, slap_set_shutdown );
234 (void) SIGNAL( LDAP_SIGCHLD, wait4child );
240 lutil_detach( ldap_debug, 0 );
242 lutil_detach( 0, 0 );
247 if ( slap_startup(-1) != 0 ) {
255 slapd_addr = &bind_addr;
257 Debug( LDAP_DEBUG_ANY, "slapd starting\n", 0, 0, 0 );
259 if (( slapd_pid_file != NULL ) &&
260 (( fp = fopen( slapd_pid_file, "w" )) != NULL ))
262 fprintf( fp, "%d\n", (int) getpid() );
266 if (( slapd_args_file != NULL ) &&
267 (( fp = fopen( slapd_args_file, "w" )) != NULL ))
269 for ( i = 0; i < g_argc; i++ ) {
270 fprintf( fp, "%s ", g_argv[i] );
282 rc = slapd_daemon( slapd_addr );
285 /* remember an error during shutdown */
286 rc |= slap_shutdown(-1);
288 /* remember an error during destroy */
289 rc |= slap_destroy();
291 Debug( LDAP_DEBUG_ANY, "slapd stopped.\n", 0, 0, 0 );
300 * Catch and discard terminated child processes, to avoid zombies.
304 wait4child( int sig )
306 int save_errno = errno;
309 * ### The wait3 vs. waitpid choice needs improvement.
310 * ### There are apparently systems where waitpid(-1, ...) fails, and
311 * ### others where waitpid should preferred over wait3 for some reason.
312 * ### Now wait3 is only here for reference, configure does not detect it.
314 #if defined(HAVE_WAITPID) && defined(WNOHANG)
315 while ( waitpid( (pid_t)-1, NULL, WNOHANG ) >= 0 || errno == EINTR )
317 #elif defined(HAVE_WAIT3) && defined(WNOHANG)
318 while ( wait3( NULL, WNOHANG, NULL ) >= 0 || errno == EINTR )
323 (void) signal( sig, wait4child );
327 #endif /* SIGCHLD || SIGCLD */
333 * Convert a string to an integer by means of a dispatcher table
334 * if the string is not in the table return the default
338 cnvt_str2int( char *stringVal, STRDISP_P dispatcher, int defaultVal )
340 int retVal = defaultVal;
343 for (disp = dispatcher; disp->stringVal; disp++) {
345 if (!strncasecmp (stringVal, disp->stringVal, disp->abbr)) {
347 retVal = disp->intVal;
357 #endif /* LOG_LOCAL4 */