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 #if defined(HAVE_PWD_H) && defined(HAVE_GRP_H)
68 fprintf( stderr, " [-U user] [-G group]" );
70 fprintf( stderr, "\n" );
76 main( int argc, char **argv )
81 struct sockaddr_in bind_addr;
85 int syslogUser = DEFAULT_SYSLOG_USER;
87 #if defined(HAVE_PWD_H) && defined(HAVE_GRP_H)
88 char *username = NULL, *groupname = NULL;
92 int serverMode = SLAP_SERVER_MODE;
94 configfile = SLAPD_DEFAULT_CONFIGFILE;
96 (void) memset( (void*) &bind_addr, '\0', sizeof(bind_addr));
97 bind_addr.sin_family = AF_INET;
98 bind_addr.sin_addr.s_addr = htonl(INADDR_ANY);
99 bind_addr.sin_port = htons(LDAP_PORT);
104 while ( (i = getopt( argc, argv,
112 #if defined(HAVE_PWD_H) && defined(HAVE_GRP_H)
117 case 'a': /* bind address */
119 if(!(bind_addr.sin_addr.S_un.S_addr = inet_addr(optarg)))
121 if(!inet_aton(optarg, &bind_addr.sin_addr))
124 fprintf(stderr, "invalid address (%s) for -a option", optarg);
129 case 'd': /* turn on debugging */
130 if ( optarg[0] == '?' ) {
131 printf( "Debug levels:\n" );
132 printf( "\tLDAP_DEBUG_TRACE\t%d\n",
134 printf( "\tLDAP_DEBUG_PACKETS\t%d\n",
135 LDAP_DEBUG_PACKETS );
136 printf( "\tLDAP_DEBUG_ARGS\t\t%d\n",
138 printf( "\tLDAP_DEBUG_CONNS\t%d\n",
140 printf( "\tLDAP_DEBUG_BER\t\t%d\n",
142 printf( "\tLDAP_DEBUG_FILTER\t%d\n",
144 printf( "\tLDAP_DEBUG_CONFIG\t%d\n",
146 printf( "\tLDAP_DEBUG_ACL\t\t%d\n",
148 printf( "\tLDAP_DEBUG_STATS\t%d\n",
150 printf( "\tLDAP_DEBUG_STATS2\t%d\n",
152 printf( "\tLDAP_DEBUG_SHELL\t%d\n",
154 printf( "\tLDAP_DEBUG_PARSE\t%d\n",
156 printf( "\tLDAP_DEBUG_ANY\t\t%d\n",
160 slap_debug |= atoi( optarg );
164 case 'd': /* turn on debugging */
166 "must compile with LDAP_DEBUG for debugging\n" );
170 case 'f': /* read config file */
171 configfile = ch_strdup( optarg );
174 case 'i': /* run from inetd */
178 case 'p': { /* port on which to listen */
179 short port = (short)atoi( optarg );
181 fprintf(stderr, "-p %s must be numeric\n", optarg);
183 bind_addr.sin_port = htons(port);
187 case 's': /* set syslog level */
188 ldap_syslog = atoi( optarg );
192 case 'l': /* set syslog local user */
193 syslogUser = cnvt_str2int( optarg, syslog_types,
194 DEFAULT_SYSLOG_USER );
198 case 'u': /* do udp */
203 case 't': /* timed server */
204 serverMode = SLAP_TIMEDSERVER_MODE;
208 #if defined(HAVE_PWD_H) && defined(HAVE_GRP_H)
209 case 'U': /* user name */
210 username = ch_strdup( optarg );
213 case 'G': /* group name */
214 groupname = ch_strdup( optarg );
216 #endif /* HAVE_PWD_H && HAVE_GRP_H */
224 lber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &slap_debug);
225 ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &slap_debug);
226 ldif_debug = slap_debug;
228 Debug( LDAP_DEBUG_TRACE, "%s", Versionstr, 0, 0 );
230 if ( (serverName = strrchr( argv[0], '/' )) == NULL ) {
231 serverName = ch_strdup( argv[0] );
233 serverName = ch_strdup( serverName + 1 );
237 openlog( serverName, OPENLOG_OPTIONS, syslogUser );
239 openlog( serverName, OPENLOG_OPTIONS );
242 tcps = set_socket( inetd ? NULL : &bind_addr );
244 #if defined(HAVE_PWD_H) && defined(HAVE_GRP_H)
245 if ( username != NULL || groupname != NULL )
246 slap_init_user( username, groupname );
249 if ( slap_init( serverMode, serverName ) != 0 ) {
254 if ( read_config( configfile ) != 0 ) {
259 (void) SIGNAL( LDAP_SIGUSR1, slap_do_nothing );
260 (void) SIGNAL( LDAP_SIGUSR2, slap_set_shutdown );
262 (void) SIGNAL( SIGPIPE, SIG_IGN );
265 (void) SIGNAL( SIGHUP, slap_set_shutdown );
267 (void) SIGNAL( SIGINT, slap_set_shutdown );
268 (void) SIGNAL( SIGTERM, slap_set_shutdown );
270 (void) SIGNAL( LDAP_SIGCHLD, wait4child );
276 lutil_detach( ldap_debug, 0 );
278 lutil_detach( 0, 0 );
283 if ( slap_startup(-1) != 0 ) {
291 Debug( LDAP_DEBUG_ANY, "slapd starting\n", 0, 0, 0 );
293 if (( slapd_pid_file != NULL ) &&
294 (( fp = fopen( slapd_pid_file, "w" )) != NULL ))
296 fprintf( fp, "%d\n", (int) getpid() );
300 if (( slapd_args_file != NULL ) &&
301 (( fp = fopen( slapd_args_file, "w" )) != NULL ))
303 for ( i = 0; i < g_argc; i++ ) {
304 fprintf( fp, "%s ", g_argv[i] );
313 rc = slapd_daemon( inetd, tcps );
316 /* remember an error during shutdown */
317 rc |= slap_shutdown(-1);
319 /* remember an error during destroy */
320 rc |= slap_destroy();
322 Debug( LDAP_DEBUG_ANY, "slapd stopped.\n", 0, 0, 0 );
335 * Catch and discard terminated child processes, to avoid zombies.
339 wait4child( int sig )
341 int save_errno = errno;
346 while ( waitpid( (pid_t)-1, NULL, WNOHANG ) >= 0 || errno == EINTR )
349 while ( wait3( NULL, WNOHANG, NULL ) >= 0 || errno == EINTR )
355 (void) SIGNAL( sig, wait4child );
359 #endif /* SIGCHLD || SIGCLD */
365 * Convert a string to an integer by means of a dispatcher table
366 * if the string is not in the table return the default
370 cnvt_str2int( char *stringVal, STRDISP_P dispatcher, int defaultVal )
372 int retVal = defaultVal;
375 for (disp = dispatcher; disp->stringVal; disp++) {
377 if (!strncasecmp (stringVal, disp->stringVal, disp->abbr)) {
379 retVal = disp->intVal;
389 #endif /* LOG_LOCAL4 */