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 );
60 fprintf( stderr, "\n [-a bind-address] [-i] [-u]" );
62 fprintf( stderr, " [-t]" );
65 fprintf( stderr, " [-l sysloguser]" );
67 fprintf( stderr, "\n" );
73 main( int argc, char **argv )
78 struct sockaddr_in bind_addr, *slapd_addr;
81 int syslogUser = DEFAULT_SYSLOG_USER;
85 int serverMode = SLAP_SERVER_MODE;
87 configfile = SLAPD_DEFAULT_CONFIGFILE;
89 (void) memset( (void*) &bind_addr, '\0', sizeof(bind_addr));
90 bind_addr.sin_family = AF_INET;
91 bind_addr.sin_addr.s_addr = htonl(INADDR_ANY);
92 bind_addr.sin_port = htons(LDAP_PORT);
97 while ( (i = getopt( argc, argv,
107 case 'a': /* bind address */
109 if(!(bind_addr.sin_addr.S_un.S_addr = inet_addr(optarg)))
111 if(!inet_aton(optarg, &bind_addr.sin_addr))
114 fprintf(stderr, "invalid address (%s) for -a option", optarg);
119 case 'd': /* turn on debugging */
120 if ( optarg[0] == '?' ) {
121 printf( "Debug levels:\n" );
122 printf( "\tLDAP_DEBUG_TRACE\t%d\n",
124 printf( "\tLDAP_DEBUG_PACKETS\t%d\n",
125 LDAP_DEBUG_PACKETS );
126 printf( "\tLDAP_DEBUG_ARGS\t\t%d\n",
128 printf( "\tLDAP_DEBUG_CONNS\t%d\n",
130 printf( "\tLDAP_DEBUG_BER\t\t%d\n",
132 printf( "\tLDAP_DEBUG_FILTER\t%d\n",
134 printf( "\tLDAP_DEBUG_CONFIG\t%d\n",
136 printf( "\tLDAP_DEBUG_ACL\t\t%d\n",
138 printf( "\tLDAP_DEBUG_STATS\t%d\n",
140 printf( "\tLDAP_DEBUG_STATS2\t%d\n",
142 printf( "\tLDAP_DEBUG_SHELL\t%d\n",
144 printf( "\tLDAP_DEBUG_PARSE\t%d\n",
146 printf( "\tLDAP_DEBUG_ANY\t\t%d\n",
150 slap_debug |= atoi( optarg );
154 case 'd': /* turn on debugging */
156 "must compile with LDAP_DEBUG for debugging\n" );
160 case 'f': /* read config file */
161 configfile = ch_strdup( optarg );
164 case 'i': /* run from inetd */
168 case 'p': { /* port on which to listen */
169 short port = (short)atoi( optarg );
171 fprintf(stderr, "-p %s must be numeric\n", optarg);
173 bind_addr.sin_port = htons(port);
177 case 's': /* set syslog level */
178 ldap_syslog = atoi( optarg );
182 case 'l': /* set syslog local user */
183 syslogUser = cnvt_str2int( optarg, syslog_types,
184 DEFAULT_SYSLOG_USER );
188 case 'u': /* do udp */
193 case 't': /* timed server */
194 serverMode = SLAP_TIMEDSERVER_MODE;
204 lber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &slap_debug);
205 ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &slap_debug);
206 ldif_debug = slap_debug;
208 Debug( LDAP_DEBUG_TRACE, "%s", Versionstr, 0, 0 );
210 if ( (serverName = strrchr( argv[0], '/' )) == NULL ) {
211 serverName = ch_strdup( argv[0] );
213 serverName = ch_strdup( serverName + 1 );
217 openlog( serverName, OPENLOG_OPTIONS, syslogUser );
219 openlog( serverName, OPENLOG_OPTIONS );
222 if ( slap_init( serverMode, serverName ) != 0 ) {
227 if ( read_config( configfile ) != 0 ) {
232 (void) SIGNAL( LDAP_SIGUSR1, slap_do_nothing );
233 (void) SIGNAL( LDAP_SIGUSR2, slap_set_shutdown );
235 (void) SIGNAL( SIGPIPE, SIG_IGN );
238 (void) SIGNAL( SIGHUP, slap_set_shutdown );
240 (void) SIGNAL( SIGINT, slap_set_shutdown );
241 (void) SIGNAL( SIGTERM, slap_set_shutdown );
243 (void) SIGNAL( LDAP_SIGCHLD, wait4child );
249 lutil_detach( ldap_debug, 0 );
251 lutil_detach( 0, 0 );
256 if ( slap_startup(-1) != 0 ) {
264 slapd_addr = &bind_addr;
266 Debug( LDAP_DEBUG_ANY, "slapd starting\n", 0, 0, 0 );
268 if (( slapd_pid_file != NULL ) &&
269 (( fp = fopen( slapd_pid_file, "w" )) != NULL ))
271 fprintf( fp, "%d\n", (int) getpid() );
275 if (( slapd_args_file != NULL ) &&
276 (( fp = fopen( slapd_args_file, "w" )) != NULL ))
278 for ( i = 0; i < g_argc; i++ ) {
279 fprintf( fp, "%s ", g_argv[i] );
291 rc = slapd_daemon( slapd_addr );
294 /* remember an error during shutdown */
295 rc |= slap_shutdown(-1);
297 /* remember an error during destroy */
298 rc |= slap_destroy();
300 Debug( LDAP_DEBUG_ANY, "slapd stopped.\n", 0, 0, 0 );
309 * Catch and discard terminated child processes, to avoid zombies.
313 wait4child( int sig )
315 int save_errno = errno;
320 while ( waitpid( (pid_t)-1, NULL, WNOHANG ) >= 0 || errno == EINTR )
323 while ( wait3( NULL, WNOHANG, NULL ) >= 0 || errno == EINTR )
329 (void) SIGNAL( sig, wait4child );
333 #endif /* SIGCHLD || SIGCLD */
339 * Convert a string to an integer by means of a dispatcher table
340 * if the string is not in the table return the default
344 cnvt_str2int( char *stringVal, STRDISP_P dispatcher, int defaultVal )
346 int retVal = defaultVal;
349 for (disp = dispatcher; disp->stringVal; disp++) {
351 if (!strncasecmp (stringVal, disp->stringVal, disp->abbr)) {
353 retVal = disp->intVal;
363 #endif /* LOG_LOCAL4 */