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