]> git.sur5r.net Git - openldap/blob - servers/slapd/main.c
f81df898c3d1d36154fb81df3b6746495b4af21b
[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  * read-only global variables or variables only written by the listener
17  * thread (after they are initialized) - no need to protect them with a mutex.
18  */
19 int             ldap_debug = 0;
20 #ifdef LDAP_DEBUG
21 int             ldap_syslog = LDAP_DEBUG_STATS;
22 #else
23 int             ldap_syslog;
24 #endif
25 int             ldap_syslog_level = LOG_DEBUG;
26 int             udp;
27 char            *default_referral;
28 char            *configfile;
29 time_t          starttime;
30 pthread_t       listener_tid;
31 int             g_argc;
32 char            **g_argv;
33 /*
34  * global variables that need mutex protection
35  */
36 time_t          currenttime;
37 pthread_mutex_t currenttime_mutex;
38 int             active_threads;
39 pthread_mutex_t active_threads_mutex;
40 pthread_mutex_t new_conn_mutex;
41 long            ops_initiated;
42 long            ops_completed;
43 int             num_conns;
44 pthread_mutex_t ops_mutex;
45 long            num_entries_sent;
46 long            num_bytes_sent;
47 pthread_mutex_t num_sent_mutex;
48 /*
49  * these mutexes must be used when calling the entry2str()
50  * routine since it returns a pointer to static data.
51  */
52 pthread_mutex_t entry2str_mutex;
53 pthread_mutex_t replog_mutex;
54
55 static void
56 usage( char *name )
57 {
58         fprintf( stderr, "usage: %s [-d ?|debuglevel] [-f configfile] [-p portnumber] [-s sysloglevel]\n", name );
59 }
60
61 int
62 main( int argc, char **argv )
63 {
64         int             i;
65         int             inetd = 0;
66         int             port;
67         char            *myname;
68         Backend         *be = NULL;
69         FILE            *fp = NULL;
70
71         configfile = SLAPD_DEFAULT_CONFIGFILE;
72         port = LDAP_PORT;
73         g_argc = argc;
74         g_argv = argv;
75
76         while ( (i = getopt( argc, argv, "d:f:ip:s:u" )) != EOF ) {
77                 switch ( i ) {
78 #ifdef LDAP_DEBUG
79                 case 'd':       /* turn on debugging */
80                         if ( optarg[0] == '?' ) {
81                                 printf( "Debug levels:\n" );
82                                 printf( "\tLDAP_DEBUG_TRACE\t%d\n",
83                                     LDAP_DEBUG_TRACE );
84                                 printf( "\tLDAP_DEBUG_PACKETS\t%d\n",
85                                     LDAP_DEBUG_PACKETS );
86                                 printf( "\tLDAP_DEBUG_ARGS\t\t%d\n",
87                                     LDAP_DEBUG_ARGS );
88                                 printf( "\tLDAP_DEBUG_CONNS\t%d\n",
89                                     LDAP_DEBUG_CONNS );
90                                 printf( "\tLDAP_DEBUG_BER\t\t%d\n",
91                                     LDAP_DEBUG_BER );
92                                 printf( "\tLDAP_DEBUG_FILTER\t%d\n",
93                                     LDAP_DEBUG_FILTER );
94                                 printf( "\tLDAP_DEBUG_CONFIG\t%d\n",
95                                     LDAP_DEBUG_CONFIG );
96                                 printf( "\tLDAP_DEBUG_ACL\t\t%d\n",
97                                     LDAP_DEBUG_ACL );
98                                 printf( "\tLDAP_DEBUG_STATS\t%d\n",
99                                     LDAP_DEBUG_STATS );
100                                 printf( "\tLDAP_DEBUG_STATS2\t%d\n",
101                                     LDAP_DEBUG_STATS2 );
102                                 printf( "\tLDAP_DEBUG_SHELL\t%d\n",
103                                     LDAP_DEBUG_SHELL );
104                                 printf( "\tLDAP_DEBUG_PARSE\t%d\n",
105                                     LDAP_DEBUG_PARSE );
106                                 printf( "\tLDAP_DEBUG_ANY\t\t%d\n",
107                                     LDAP_DEBUG_ANY );
108                                 exit( 0 );
109                         } else {
110                                 ldap_debug |= atoi( optarg );
111                                 lber_debug = (ldap_debug & LDAP_DEBUG_BER);
112                         }
113                         break;
114 #else
115                 case 'd':       /* turn on debugging */
116                         fprintf( stderr,
117                             "must compile with LDAP_DEBUG for debugging\n" );
118                         break;
119 #endif
120
121                 case 'f':       /* read config file */
122                         configfile = ch_strdup( optarg );
123                         break;
124
125                 case 'i':       /* run from inetd */
126                         inetd = 1;
127                         break;
128
129                 case 'p':       /* port on which to listen */
130                         port = atoi( optarg );
131                         break;
132
133                 case 's':       /* set syslog level */
134                         ldap_syslog = atoi( optarg );
135                         break;
136
137                 case 'u':       /* do udp */
138                         udp = 1;
139                         break;
140
141                 default:
142                         usage( argv[0] );
143                         exit( 1 );
144                 }
145         }
146
147         Debug( LDAP_DEBUG_TRACE, "%s", Versionstr, 0, 0 );
148
149         if ( (myname = strrchr( argv[0], '/' )) == NULL ) {
150                 myname = ch_strdup( argv[0] );
151         } else {
152                 myname = ch_strdup( myname + 1 );
153         }
154
155         if ( ! inetd ) {
156                 /* pre-open config file before detach in case it is a relative path */
157                 fp = fopen( configfile, "r" );
158 #ifdef LDAP_DEBUG
159                 lutil_detach( ldap_debug, 0 );
160 #else
161                 lutil_detach( 0, 0 );
162 #endif
163         }
164 #ifdef LOG_LOCAL4
165         openlog( myname, OPENLOG_OPTIONS, LOG_LOCAL4 );
166 #else
167         openlog( myname, OPENLOG_OPTIONS );
168 #endif
169
170         init();
171         read_config( configfile, &be, fp );
172
173         if ( ! inetd ) {
174                 pthread_attr_t  attr;
175                 int             status;
176
177                 time( &starttime );
178                 pthread_attr_init( &attr );
179 #ifdef DETACH_LISTENER_THREAD
180                 /* we should detach it if we're going to join with it */
181                 pthread_attr_setdetachstate( &attr, PTHREAD_CREATE_DETACHED );
182 #endif
183
184 #if !defined(HAVE_PTHREADS_D4)
185                 /* POSIX_THREADS or compatible
186                  * This is a draft 10 or standard pthreads implementation
187                  */
188                 if ( pthread_create( &listener_tid, &attr, slapd_daemon,
189                     (void *) port ) != 0 ) {
190                         Debug( LDAP_DEBUG_ANY,
191                             "listener pthread_create failed\n", 0, 0, 0 );
192                         exit( 1 );
193                 }
194 #else   /* draft4 */
195                 /*
196                  * This is a draft 4 or earlier pthreads implementation
197                  */
198                 if ( pthread_create( &listener_tid, attr, slapd_daemon,
199                     (void *) port ) != 0 ) {
200                         Debug( LDAP_DEBUG_ANY,
201                             "listener pthread_create failed\n", 0, 0, 0 );
202                         exit( 1 );
203                 }
204 #endif  /* !draft4 */
205                 pthread_attr_destroy( &attr );
206 #ifdef HAVE_PHREADS_FINAL
207                 pthread_join( listener_tid, (void *) NULL );
208 #else
209                 pthread_join( listener_tid, (void *) &status );
210 #endif
211                 pthread_exit( 0 );
212         } else {
213                 Connection              c;
214                 Operation               *o;
215                 BerElement              ber;
216                 unsigned long           len, tag;
217                 long                    msgid;
218                 int                     flen;
219                 struct sockaddr_in      from;
220                 struct hostent          *hp;
221
222                 c.c_dn = NULL;
223                 c.c_ops = NULL;
224                 c.c_sb.sb_sd = 0;
225                 c.c_sb.sb_options = 0;
226                 c.c_sb.sb_naddr = udp ? 1 : 0;
227                 c.c_sb.sb_ber.ber_buf = NULL;
228                 c.c_sb.sb_ber.ber_ptr = NULL;
229                 c.c_sb.sb_ber.ber_end = NULL;
230                 pthread_mutex_init( &c.c_dnmutex, pthread_mutexattr_default );
231                 pthread_mutex_init( &c.c_opsmutex, pthread_mutexattr_default );
232                 pthread_mutex_init( &c.c_pdumutex, pthread_mutexattr_default );
233 #ifdef notdefcldap
234                 c.c_sb.sb_addrs = (void **) saddrlist;
235                 c.c_sb.sb_fromaddr = &faddr;
236                 c.c_sb.sb_useaddr = saddrlist[ 0 ] = &saddr;
237 #endif
238                 flen = sizeof(from);
239                 if ( getpeername( 0, (struct sockaddr *) &from, &flen ) == 0 ) {
240 #ifdef SLAPD_RLOOKUPS
241                         hp = gethostbyaddr( (char *) &(from.sin_addr.s_addr),
242                             sizeof(from.sin_addr.s_addr), AF_INET );
243 #else
244                         hp = NULL;
245 #endif
246
247                         Debug( LDAP_DEBUG_ARGS, "connection from %s (%s)\n",
248                             hp == NULL ? "unknown" : hp->h_name,
249                             inet_ntoa( from.sin_addr ), 0 );
250
251                         c.c_addr = inet_ntoa( from.sin_addr );
252                         c.c_domain = ch_strdup( hp == NULL ? "" : hp->h_name );
253                 } else {
254                         Debug( LDAP_DEBUG_ARGS, "connection from unknown\n",
255                             0, 0, 0 );
256                 }
257
258                 ber_init_w_nullc( &ber, 0 );
259
260                 while ( (tag = ber_get_next( &c.c_sb, &len, &ber ))
261                     == LDAP_TAG_MESSAGE ) {
262                         pthread_mutex_lock( &currenttime_mutex );
263                         time( &currenttime );
264                         pthread_mutex_unlock( &currenttime_mutex );
265
266                         if ( (tag = ber_get_int( &ber, &msgid ))
267                             != LDAP_TAG_MSGID ) {
268                                 /* log and send error */
269                                 Debug( LDAP_DEBUG_ANY,
270                                    "ber_get_int returns 0x%lx\n", tag, 0, 0 );
271                                 return 1;
272                         }
273
274                         if ( (tag = ber_peek_tag( &ber, &len ))
275                             == LBER_ERROR ) {
276                                 /* log, close and send error */
277                                 Debug( LDAP_DEBUG_ANY,
278                                    "ber_peek_tag returns 0x%lx\n", tag, 0, 0 );
279                                 ber_free( &ber, 1 );
280                                 close( c.c_sb.sb_sd );
281                                 c.c_sb.sb_sd = -1;
282                                 return 1;
283                         }
284
285                         connection_activity( &c );
286
287                         ber_free( &ber, 1 );
288                 }
289         }
290         return 1;
291 }