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 */
100 if(!inet_aton(optarg, &bind_addr.sin_addr)) {
101 fprintf(stderr, "invalid address (%s) for -a option", optarg);
106 case 'd': /* turn on debugging */
107 if ( optarg[0] == '?' ) {
108 printf( "Debug levels:\n" );
109 printf( "\tLDAP_DEBUG_TRACE\t%d\n",
111 printf( "\tLDAP_DEBUG_PACKETS\t%d\n",
112 LDAP_DEBUG_PACKETS );
113 printf( "\tLDAP_DEBUG_ARGS\t\t%d\n",
115 printf( "\tLDAP_DEBUG_CONNS\t%d\n",
117 printf( "\tLDAP_DEBUG_BER\t\t%d\n",
119 printf( "\tLDAP_DEBUG_FILTER\t%d\n",
121 printf( "\tLDAP_DEBUG_CONFIG\t%d\n",
123 printf( "\tLDAP_DEBUG_ACL\t\t%d\n",
125 printf( "\tLDAP_DEBUG_STATS\t%d\n",
127 printf( "\tLDAP_DEBUG_STATS2\t%d\n",
129 printf( "\tLDAP_DEBUG_SHELL\t%d\n",
131 printf( "\tLDAP_DEBUG_PARSE\t%d\n",
133 printf( "\tLDAP_DEBUG_ANY\t\t%d\n",
137 slap_debug |= atoi( optarg );
141 case 'd': /* turn on debugging */
143 "must compile with LDAP_DEBUG for debugging\n" );
147 case 'f': /* read config file */
148 configfile = ch_strdup( optarg );
151 case 'i': /* run from inetd */
155 case 'p': { /* port on which to listen */
156 int port = atoi( optarg );
158 fprintf(stderr, "-p %s must be numeric\n", optarg);
160 bind_addr.sin_port = htons(port);
164 case 's': /* set syslog level */
165 ldap_syslog = atoi( optarg );
169 case 'l': /* set syslog local user */
170 syslogUser = cnvt_str2int( optarg, syslog_types,
171 DEFAULT_SYSLOG_USER );
175 case 'u': /* do udp */
180 case 't': /* timed server */
181 serverMode = SLAP_TIMEDSERVER_MODE;
191 lber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &slap_debug);
192 ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &slap_debug);
193 ldif_debug = slap_debug;
195 Debug( LDAP_DEBUG_TRACE, "%s", Versionstr, 0, 0 );
197 if ( (serverName = strrchr( argv[0], '/' )) == NULL ) {
198 serverName = ch_strdup( argv[0] );
200 serverName = ch_strdup( serverName + 1 );
204 openlog( serverName, OPENLOG_OPTIONS, syslogUser );
206 openlog( serverName, OPENLOG_OPTIONS );
209 if ( slap_init( serverMode, serverName ) != 0 ) {
214 if ( read_config( configfile ) != 0 ) {
219 (void) SIGNAL( LDAP_SIGUSR1, slap_do_nothing );
220 (void) SIGNAL( LDAP_SIGUSR2, slap_set_shutdown );
222 (void) SIGNAL( SIGPIPE, SIG_IGN );
225 (void) SIGNAL( SIGHUP, slap_set_shutdown );
227 (void) SIGNAL( SIGINT, slap_set_shutdown );
228 (void) SIGNAL( SIGTERM, slap_set_shutdown );
230 (void) SIGNAL( LDAP_SIGCHLD, wait4child );
235 lutil_detach( ldap_debug, 0 );
237 lutil_detach( 0, 0 );
241 if ( slap_startup(-1) != 0 ) {
249 slapd_addr = &bind_addr;
251 Debug( LDAP_DEBUG_ANY, "slapd starting\n", 0, 0, 0 );
253 if (( slapd_pid_file != NULL ) &&
254 (( fp = fopen( slapd_pid_file, "w" )) != NULL ))
256 fprintf( fp, "%d\n", (int) getpid() );
260 if (( slapd_args_file != NULL ) &&
261 (( fp = fopen( slapd_args_file, "w" )) != NULL ))
263 for ( i = 0; i < g_argc; i++ ) {
264 fprintf( fp, "%s ", g_argv[i] );
276 rc = slapd_daemon( slapd_addr );
279 /* remember an error during shutdown */
280 rc |= slap_shutdown(-1);
282 /* remember an error during destroy */
283 rc |= slap_destroy();
285 Debug( LDAP_DEBUG_ANY, "slapd stopped.\n", 0, 0, 0 );
294 * Catch and discard terminated child processes, to avoid zombies.
298 wait4child( int sig )
300 int save_errno = errno;
303 * ### The wait3 vs. waitpid choice needs improvement.
304 * ### There are apparently systems where waitpid(-1, ...) fails, and
305 * ### others where waitpid should preferred over wait3 for some reason.
306 * ### Now wait3 is only here for reference, configure does not detect it.
308 #if defined(HAVE_WAITPID) && defined(WNOHANG)
309 while ( waitpid( (pid_t)-1, NULL, WNOHANG ) >= 0 || errno == EINTR )
311 #elif defined(HAVE_WAIT3) && defined(WNOHANG)
312 while ( wait3( NULL, WNOHANG, NULL ) >= 0 || errno == EINTR )
317 (void) signal( sig, wait4child );
321 #endif /* SIGCHLD || SIGCLD */
327 * Convert a string to an integer by means of a dispatcher table
328 * if the string is not in the table return the default
332 cnvt_str2int( char *stringVal, STRDISP_P dispatcher, int defaultVal )
334 int retVal = defaultVal;
337 for (disp = dispatcher; disp->stringVal; disp++) {
339 if (!strncasecmp (stringVal, disp->stringVal, disp->abbr)) {
341 retVal = disp->intVal;
351 #endif /* LOG_LOCAL4 */