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