]> git.sur5r.net Git - openldap/blob - servers/slapd/main.c
0a560c5ca8dea0749ada3beeb664894c347861c2
[openldap] / servers / slapd / main.c
1 #include "portable.h"
2
3 #include <stdio.h>
4
5 #include <ac/signal.h>
6 #include <ac/socket.h>
7 #include <ac/string.h>
8 #include <ac/time.h>
9 #include <ac/unistd.h>
10 #include <ac/wait.h>
11 #include <ac/signal.h>
12 #include <ac/errno.h>
13
14 #include "ldapconfig.h"
15 #include "slap.h"
16 #include "lutil.h"                      /* Get lutil_detach() */
17
18 #ifdef LDAP_SIGCHLD
19 static RETSIGTYPE wait4child( int sig );
20 #endif
21
22 /*
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
25  */
26
27 #ifdef LOG_LOCAL4
28
29 #define DEFAULT_SYSLOG_USER  LOG_LOCAL4
30
31 typedef struct _str2intDispatch {
32         char    *stringVal;
33         int      abbr;
34         int      intVal;
35 } STRDISP, *STRDISP_P;
36
37
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 },
48     { NULL }
49 };
50
51 static int   cnvt_str2int( char *, STRDISP_P, int );
52
53 #endif  /* LOG_LOCAL4 */
54
55
56 static void
57 usage( char *name )
58 {
59         fprintf( stderr, "usage: %s [-d ?|debuglevel] [-f configfile] [-p portnumber] [-s sysloglevel]", name );
60 #ifdef LOG_LOCAL4
61     fprintf( stderr, " [-l sysloguser]" );
62 #endif
63     fprintf( stderr, "\n" );
64 }
65
66 time_t starttime;
67
68 int
69 main( int argc, char **argv )
70 {
71         int             i;
72         int             inetd = 0;
73         int             rc;
74         struct sockaddr_in      bind_addr, *slapd_addr;
75         int             udp;
76 #ifdef LOG_LOCAL4
77     int     syslogUser = DEFAULT_SYSLOG_USER;
78 #endif
79         char            *configfile;
80         char        *serverName;
81         int         serverMode = SLAP_SERVER_MODE;
82
83         configfile = SLAPD_DEFAULT_CONFIGFILE;
84
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);
89
90         g_argc = argc;
91         g_argv = argv;
92
93 #ifdef SLAPD_BDB2
94         while ( (i = getopt( argc, argv, "d:f:ia:p:s:ut" )) != EOF ) {
95 #else
96         while ( (i = getopt( argc, argv, "d:f:ia:p:s:u" )) != EOF ) {
97 #endif
98                 switch ( i ) {
99                 case 'a':       /* bind address */
100 #ifdef HAVE_WINSOCK
101                         if(!(bind_addr.sin_addr.S_un.S_addr = inet_addr(optarg))) {
102 #else
103                         if(!inet_aton(optarg, &bind_addr.sin_addr)) {
104 #endif
105                                 fprintf(stderr, "invalid address (%s) for -a option", optarg);
106                         }
107             break;
108
109 #ifdef LDAP_DEBUG
110                 case 'd':       /* turn on debugging */
111                         if ( optarg[0] == '?' ) {
112                                 printf( "Debug levels:\n" );
113                                 printf( "\tLDAP_DEBUG_TRACE\t%d\n",
114                                     LDAP_DEBUG_TRACE );
115                                 printf( "\tLDAP_DEBUG_PACKETS\t%d\n",
116                                     LDAP_DEBUG_PACKETS );
117                                 printf( "\tLDAP_DEBUG_ARGS\t\t%d\n",
118                                     LDAP_DEBUG_ARGS );
119                                 printf( "\tLDAP_DEBUG_CONNS\t%d\n",
120                                     LDAP_DEBUG_CONNS );
121                                 printf( "\tLDAP_DEBUG_BER\t\t%d\n",
122                                     LDAP_DEBUG_BER );
123                                 printf( "\tLDAP_DEBUG_FILTER\t%d\n",
124                                     LDAP_DEBUG_FILTER );
125                                 printf( "\tLDAP_DEBUG_CONFIG\t%d\n",
126                                     LDAP_DEBUG_CONFIG );
127                                 printf( "\tLDAP_DEBUG_ACL\t\t%d\n",
128                                     LDAP_DEBUG_ACL );
129                                 printf( "\tLDAP_DEBUG_STATS\t%d\n",
130                                     LDAP_DEBUG_STATS );
131                                 printf( "\tLDAP_DEBUG_STATS2\t%d\n",
132                                     LDAP_DEBUG_STATS2 );
133                                 printf( "\tLDAP_DEBUG_SHELL\t%d\n",
134                                     LDAP_DEBUG_SHELL );
135                                 printf( "\tLDAP_DEBUG_PARSE\t%d\n",
136                                     LDAP_DEBUG_PARSE );
137                                 printf( "\tLDAP_DEBUG_ANY\t\t%d\n",
138                                     LDAP_DEBUG_ANY );
139                                 exit( 0 );
140                         } else {
141                                 slap_debug |= atoi( optarg );
142                         }
143                         break;
144 #else
145                 case 'd':       /* turn on debugging */
146                         fprintf( stderr,
147                             "must compile with LDAP_DEBUG for debugging\n" );
148                         break;
149 #endif
150
151                 case 'f':       /* read config file */
152                         configfile = ch_strdup( optarg );
153                         break;
154
155                 case 'i':       /* run from inetd */
156                         inetd = 1;
157                         break;
158
159                 case 'p': {     /* port on which to listen */
160                                 short port = (short)atoi( optarg );
161                                 if(! port ) {
162                                         fprintf(stderr, "-p %s must be numeric\n", optarg);
163                                 } else {
164                                         bind_addr.sin_port = htons(port);
165                                 }
166                         } break;
167
168                 case 's':       /* set syslog level */
169                         ldap_syslog = atoi( optarg );
170                         break;
171
172 #ifdef LOG_LOCAL4
173                 case 'l':       /* set syslog local user */
174                         syslogUser = cnvt_str2int( optarg, syslog_types,
175                                            DEFAULT_SYSLOG_USER );
176                         break;
177 #endif
178
179                 case 'u':       /* do udp */
180                         udp = 1;
181                         break;
182
183 #ifdef SLAPD_BDB2
184                 case 't':  /* timed server */
185                         serverMode = SLAP_TIMEDSERVER_MODE;
186                         break;
187 #endif
188
189                 default:
190                         usage( argv[0] );
191                         exit( 1 );
192                 }
193         }
194
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;
198
199         Debug( LDAP_DEBUG_TRACE, "%s", Versionstr, 0, 0 );
200
201         if ( (serverName = strrchr( argv[0], '/' )) == NULL ) {
202                 serverName = ch_strdup( argv[0] );
203         } else {
204                 serverName = ch_strdup( serverName + 1 );
205         }
206
207 #ifdef LOG_LOCAL4
208         openlog( serverName, OPENLOG_OPTIONS, syslogUser );
209 #else
210         openlog( serverName, OPENLOG_OPTIONS );
211 #endif
212
213         if ( slap_init( serverMode, serverName ) != 0 ) {
214                 rc = 1;
215                 goto destroy;
216         }
217
218         if ( read_config( configfile ) != 0 ) {
219                 rc = 1;
220                 goto destroy;
221         }
222
223         (void) SIGNAL( LDAP_SIGUSR1, slap_do_nothing );
224         (void) SIGNAL( LDAP_SIGUSR2, slap_set_shutdown );
225 #ifdef SIGPIPE
226         (void) SIGNAL( SIGPIPE, SIG_IGN );
227 #endif
228 #ifdef SIGHUP
229         (void) SIGNAL( SIGHUP, slap_set_shutdown );
230 #endif
231         (void) SIGNAL( SIGINT, slap_set_shutdown );
232         (void) SIGNAL( SIGTERM, slap_set_shutdown );
233 #ifdef LDAP_SIGCHLD
234         (void) SIGNAL( LDAP_SIGCHLD, wait4child );
235 #endif
236
237 #ifndef WIN32
238         if(!inetd) {
239 #ifdef LDAP_DEBUG
240                 lutil_detach( ldap_debug, 0 );
241 #else
242                 lutil_detach( 0, 0 );
243 #endif
244         }
245 #endif /* WIN32 */
246
247         if ( slap_startup(-1)  != 0 ) {
248                 rc = 1;
249                 goto shutdown;
250         }
251
252         if(!inetd) {
253                 FILE *fp;
254
255                 slapd_addr = &bind_addr;
256
257                 Debug( LDAP_DEBUG_ANY, "slapd starting\n", 0, 0, 0 );
258
259                 if (( slapd_pid_file != NULL ) &&
260                         (( fp = fopen( slapd_pid_file, "w" )) != NULL ))
261                 {
262                         fprintf( fp, "%d\n", (int) getpid() );
263                         fclose( fp );
264                 }
265
266                 if (( slapd_args_file != NULL ) &&
267                         (( fp = fopen( slapd_args_file, "w" )) != NULL ))
268                 {
269                         for ( i = 0; i < g_argc; i++ ) {
270                                 fprintf( fp, "%s ", g_argv[i] );
271                         }
272                         fprintf( fp, "\n" );
273                         fclose( fp );
274                 }
275
276         } else {
277                 slapd_addr = NULL;
278         }
279
280         time( &starttime );
281
282         rc = slapd_daemon( slapd_addr );
283
284 shutdown:
285         /* remember an error during shutdown */
286         rc |= slap_shutdown(-1);
287 destroy:
288         /* remember an error during destroy */
289         rc |= slap_destroy();
290
291         Debug( LDAP_DEBUG_ANY, "slapd stopped.\n", 0, 0, 0 );
292
293         return rc;
294 }
295
296
297 #ifdef LDAP_SIGCHLD
298
299 /*
300  *  Catch and discard terminated child processes, to avoid zombies.
301  */
302
303 static RETSIGTYPE
304 wait4child( int sig )
305 {
306     int save_errno = errno;
307     errno = 0;
308     /*
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.
313      */
314 #if defined(HAVE_WAITPID) && defined(WNOHANG)
315     while ( waitpid( (pid_t)-1, NULL, WNOHANG ) >= 0 || errno == EINTR )
316         ;       /* NULL */
317 #elif defined(HAVE_WAIT3) && defined(WNOHANG)
318     while ( wait3( NULL, WNOHANG, NULL ) >= 0 || errno == EINTR )
319         ;       /* NULL */
320 #else
321     (void) wait( NULL );
322 #endif
323     (void) signal( sig, wait4child );
324     errno = save_errno;
325 }
326
327 #endif /* SIGCHLD || SIGCLD */
328
329
330 #ifdef LOG_LOCAL4
331
332 /*
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
335  */
336
337 static int
338 cnvt_str2int( char *stringVal, STRDISP_P dispatcher, int defaultVal )
339 {
340     int        retVal = defaultVal;
341     STRDISP_P  disp;
342
343     for (disp = dispatcher; disp->stringVal; disp++) {
344
345         if (!strncasecmp (stringVal, disp->stringVal, disp->abbr)) {
346
347             retVal = disp->intVal;
348             break;
349
350         }
351     }
352
353     return (retVal);
354
355 } /* cnvt_str2int */
356
357 #endif  /* LOG_LOCAL4 */