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