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