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