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