]> git.sur5r.net Git - openldap/blob - servers/slapd/main.c
Reap back-shell children processes using SIGCHLD handler.
[openldap] / servers / slapd / main.c
1 #include "portable.h"
2
3 #include <stdio.h>
4
5 #include <ac/errno.h>
6 #include <ac/signal.h>
7 #include <ac/socket.h>
8 #include <ac/string.h>
9 #include <ac/time.h>
10 #include <ac/unistd.h>
11
12 #include "ldapconfig.h"
13 #include "slap.h"
14 #include "lutil.h"                      /* Get lutil_detach() */
15
16 #ifdef LDAP_SIGCHLD
17 static void wait4child( int sig );
18 #endif
19
20 /*
21  * when more than one slapd is running on one machine, each one might have
22  * it's own LOCAL for syslogging and must have its own pid/args files
23  */
24
25 #ifdef LOG_LOCAL4
26
27 #define DEFAULT_SYSLOG_USER  LOG_LOCAL4
28
29 typedef struct _str2intDispatch {
30
31         char    *stringVal;
32         int      abbr;
33         int      intVal;
34
35 } STRDISP, *STRDISP_P;
36
37
38 /* table to compute syslog-options to integer */
39 static STRDISP  syslog_types[] = {
40
41     { "LOCAL0",         6, LOG_LOCAL0 },
42     { "LOCAL1",         6, LOG_LOCAL1 },
43     { "LOCAL2",         6, LOG_LOCAL2 },
44     { "LOCAL3",         6, LOG_LOCAL3 },
45     { "LOCAL4",         6, LOG_LOCAL4 },
46     { "LOCAL5",         6, LOG_LOCAL5 },
47     { "LOCAL6",         6, LOG_LOCAL6 },
48     { "LOCAL7",         6, LOG_LOCAL7 },
49     NULL
50
51 };
52
53 static int   cnvt_str2int();
54
55 #endif  /* LOG_LOCAL4 */
56
57
58 static void
59 usage( char *name )
60 {
61         fprintf( stderr, "usage: %s [-d ?|debuglevel] [-f configfile] [-p portnumber] [-s sysloglevel]", name );
62 #ifdef LOG_LOCAL4
63     fprintf( stderr, " [-l sysloguser]" );
64 #endif
65     fprintf( stderr, "\n" );
66 }
67
68 int
69 main( int argc, char **argv )
70 {
71         int             i;
72         int             inetd = 0;
73         int             port;
74         int             udp;
75         Backend         *be = NULL;
76         FILE            *fp = NULL;
77         char            *configfile;
78         char    *serverName;
79 #ifdef LOG_LOCAL4
80         char *optstring = "d:f:ip:s:ul:";
81         int     syslogUser = DEFAULT_SYSLOG_USER;
82 #else
83         char *optstring = "d:f:ip:s:u";
84 #endif
85
86         configfile = SLAPD_DEFAULT_CONFIGFILE;
87         port = LDAP_PORT;
88         g_argc = argc;
89         g_argv = argv;
90
91         while ( (i = getopt( argc, argv, optstring )) != EOF ) {
92                 switch ( i ) {
93 #ifdef LDAP_DEBUG
94                 case 'd':       /* turn on debugging */
95                         if ( optarg[0] == '?' ) {
96                                 printf( "Debug levels:\n" );
97                                 printf( "\tLDAP_DEBUG_TRACE\t%d\n",
98                                     LDAP_DEBUG_TRACE );
99                                 printf( "\tLDAP_DEBUG_PACKETS\t%d\n",
100                                     LDAP_DEBUG_PACKETS );
101                                 printf( "\tLDAP_DEBUG_ARGS\t\t%d\n",
102                                     LDAP_DEBUG_ARGS );
103                                 printf( "\tLDAP_DEBUG_CONNS\t%d\n",
104                                     LDAP_DEBUG_CONNS );
105                                 printf( "\tLDAP_DEBUG_BER\t\t%d\n",
106                                     LDAP_DEBUG_BER );
107                                 printf( "\tLDAP_DEBUG_FILTER\t%d\n",
108                                     LDAP_DEBUG_FILTER );
109                                 printf( "\tLDAP_DEBUG_CONFIG\t%d\n",
110                                     LDAP_DEBUG_CONFIG );
111                                 printf( "\tLDAP_DEBUG_ACL\t\t%d\n",
112                                     LDAP_DEBUG_ACL );
113                                 printf( "\tLDAP_DEBUG_STATS\t%d\n",
114                                     LDAP_DEBUG_STATS );
115                                 printf( "\tLDAP_DEBUG_STATS2\t%d\n",
116                                     LDAP_DEBUG_STATS2 );
117                                 printf( "\tLDAP_DEBUG_SHELL\t%d\n",
118                                     LDAP_DEBUG_SHELL );
119                                 printf( "\tLDAP_DEBUG_PARSE\t%d\n",
120                                     LDAP_DEBUG_PARSE );
121                                 printf( "\tLDAP_DEBUG_ANY\t\t%d\n",
122                                     LDAP_DEBUG_ANY );
123                                 exit( 0 );
124                         } else {
125                                 ldap_debug |= atoi( optarg );
126                                 lber_debug = (ldap_debug & LDAP_DEBUG_BER);
127                         }
128                         break;
129 #else
130                 case 'd':       /* turn on debugging */
131                         fprintf( stderr,
132                             "must compile with LDAP_DEBUG for debugging\n" );
133                         break;
134 #endif
135
136                 case 'f':       /* read config file */
137                         configfile = ch_strdup( optarg );
138                         break;
139
140                 case 'i':       /* run from inetd */
141                         inetd = 1;
142                         break;
143
144                 case 'p':       /* port on which to listen */
145                         port = atoi( optarg );
146                         break;
147
148                 case 's':       /* set syslog level */
149                         ldap_syslog = atoi( optarg );
150                         break;
151
152 #ifdef LOG_LOCAL4
153                 case 'l':       /* set syslog local user */
154                         syslogUser = cnvt_str2int( optarg, syslog_types,
155                                            DEFAULT_SYSLOG_USER );
156                         break;
157 #endif
158
159                 case 'u':       /* do udp */
160                         udp = 1;
161                         break;
162
163                 default:
164                         usage( argv[0] );
165                         exit( 1 );
166                 }
167         }
168
169         Debug( LDAP_DEBUG_TRACE, "%s", Versionstr, 0, 0 );
170
171         if ( (serverName = strrchr( argv[0], '/' )) == NULL ) {
172                 serverName = ch_strdup( argv[0] );
173         } else {
174                 serverName = ch_strdup( serverName + 1 );
175         }
176
177         if ( ! inetd ) {
178                 /* pre-open config file before detach in case it is a relative path */
179                 fp = fopen( configfile, "r" );
180 #ifdef LDAP_DEBUG
181                 lutil_detach( ldap_debug, 0 );
182 #else
183                 lutil_detach( 0, 0 );
184 #endif
185         }
186
187 #ifdef LOG_LOCAL4
188         openlog( serverName, OPENLOG_OPTIONS, syslogUser );
189 #else
190         openlog( serverName, OPENLOG_OPTIONS );
191 #endif
192
193         init();
194         read_config( configfile, &be, fp );
195
196         if ( ! inetd ) {
197                 int             status;
198
199                 (void) SIGNAL( SIGPIPE, SIG_IGN );
200                 (void) SIGNAL( LDAP_SIGUSR1, slap_do_nothing );
201                 (void) SIGNAL( LDAP_SIGUSR2, slap_set_shutdown );
202                 (void) SIGNAL( SIGTERM, slap_set_shutdown );
203                 (void) SIGNAL( SIGINT, slap_set_shutdown );
204                 (void) SIGNAL( SIGHUP, slap_set_shutdown );
205 #ifdef LDAP_SIGCHLD
206                 (void) SIGNAL( LDAP_SIGCHLD, wait4child );
207 #endif
208
209                 time( &starttime );
210
211                 if ( status = ldap_pvt_thread_create( &listener_tid, 0,
212                         slapd_daemon, (void *) port ) != 0 )
213                 {
214                         Debug( LDAP_DEBUG_ANY,
215                             "listener ldap_pvt_thread_create failed (%d)\n", status, 0, 0 );
216                         exit( 1 );
217                 }
218
219                 ldap_pvt_thread_join( listener_tid, (void *) NULL );
220
221                 return 0;
222
223         } else {
224                 Connection              c;
225                 Operation               *o;
226                 BerElement              ber;
227                 unsigned long           len, tag;
228                 long                    msgid;
229                 int                     flen;
230                 struct sockaddr_in      from;
231                 struct hostent          *hp;
232
233                 c.c_dn = NULL;
234                 c.c_cdn = NULL;
235                 c.c_ops = NULL;
236                 c.c_sb.sb_sd = 0;
237                 c.c_sb.sb_options = 0;
238                 c.c_sb.sb_naddr = udp ? 1 : 0;
239                 c.c_sb.sb_ber.ber_buf = NULL;
240                 c.c_sb.sb_ber.ber_ptr = NULL;
241                 c.c_sb.sb_ber.ber_end = NULL;
242                 ldap_pvt_thread_mutex_init( &c.c_dnmutex );
243                 ldap_pvt_thread_mutex_init( &c.c_opsmutex );
244                 ldap_pvt_thread_mutex_init( &c.c_pdumutex );
245 #ifdef notdefcldap
246                 c.c_sb.sb_addrs = (void **) saddrlist;
247                 c.c_sb.sb_fromaddr = &faddr;
248                 c.c_sb.sb_useaddr = saddrlist[ 0 ] = &saddr;
249 #endif
250                 flen = sizeof(from);
251                 if ( getpeername( 0, (struct sockaddr *) &from, &flen ) == 0 ) {
252 #ifdef SLAPD_RLOOKUPS
253                         hp = gethostbyaddr( (char *) &(from.sin_addr.s_addr),
254                             sizeof(from.sin_addr.s_addr), AF_INET );
255 #else
256                         hp = NULL;
257 #endif
258
259                         Debug( LDAP_DEBUG_ARGS, "connection from %s (%s)\n",
260                             hp == NULL ? "unknown" : hp->h_name,
261                             inet_ntoa( from.sin_addr ), 0 );
262
263                         c.c_addr = inet_ntoa( from.sin_addr );
264                         c.c_domain = ch_strdup( hp == NULL ? "" : hp->h_name );
265                 } else {
266                         Debug( LDAP_DEBUG_ARGS, "connection from unknown\n",
267                             0, 0, 0 );
268                 }
269
270                 ber_init( &ber, 0 );
271                 while ( (tag = ber_get_next( &c.c_sb, &len, &ber ))
272                     == LDAP_TAG_MESSAGE ) {
273                         ldap_pvt_thread_mutex_lock( &currenttime_mutex );
274                         time( &currenttime );
275                         ldap_pvt_thread_mutex_unlock( &currenttime_mutex );
276
277                         if ( (tag = ber_get_int( &ber, &msgid ))
278                             != LDAP_TAG_MSGID ) {
279                                 /* log and send error */
280                                 Debug( LDAP_DEBUG_ANY,
281                                    "ber_get_int returns 0x%lx\n", tag, 0, 0 );
282                                 ber_free( &ber, 1 );
283                                 return 1;
284                         }
285
286                         if ( (tag = ber_peek_tag( &ber, &len ))
287                             == LBER_ERROR ) {
288                                 /* log, close and send error */
289                                 Debug( LDAP_DEBUG_ANY,
290                                    "ber_peek_tag returns 0x%lx\n", tag, 0, 0 );
291                                 ber_free( &ber, 1 );
292                                 close( c.c_sb.sb_sd );
293                                 c.c_sb.sb_sd = -1;
294                                 return 1;
295                         }
296
297                         connection_activity( &c );
298
299                         ber_free( &ber, 1 );
300                 }
301         }
302         return 1;
303 }
304
305
306 #ifdef LDAP_SIGCHLD
307
308 /*
309  *  Catch and discard terminated child processes, to avoid zombies.
310  */
311
312 static void
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 (stringVal, dispatcher, defaultVal)
345 char      *stringVal;
346 STRDISP_P  dispatcher;
347 int        defaultVal;
348 {
349     int        retVal = defaultVal;
350     STRDISP_P  disp;
351
352     for (disp = dispatcher; disp->stringVal; disp++) {
353
354         if (!strncasecmp (stringVal, disp->stringVal, disp->abbr)) {
355
356             retVal = disp->intVal;
357             break;
358
359         }
360     }
361
362     return (retVal);
363
364 } /* cnvt_str2int */
365
366 #endif  /* LOG_LOCAL4 */
367