]> git.sur5r.net Git - openldap/blob - servers/slapd/daemon.c
LDAPworld P1: DEC and other portability issues
[openldap] / servers / slapd / daemon.c
1
2 /* Revision history
3  *
4  * 5-Jun-96     hodges
5  *      Added locking of new_conn_mutex when traversing the c[] array.
6  */
7
8 #include <stdio.h>
9 #include <string.h>
10 #include <sys/types.h>
11 #include <sys/socket.h>
12 #include <errno.h>
13 #include <sys/time.h>
14 #include <netinet/in.h>
15 #include <arpa/inet.h>
16 #include <netdb.h>
17 #include <signal.h>
18 #ifdef _AIX
19 #include <sys/select.h>
20 #endif
21 #include "slap.h"
22 #include "portable.h"
23 #include "ldapconfig.h"
24 #ifdef NEED_FILIO
25 #include <sys/filio.h>
26 #else /* NEED_FILIO */
27 #include <sys/ioctl.h>
28 #endif /* NEED_FILIO */
29 #ifdef USE_SYSCONF
30 #include <unistd.h>
31 #endif /* USE_SYSCONF */
32
33 extern Operation        *op_add();
34
35 #ifndef SYSERRLIST_IN_STDIO
36 extern int              sys_nerr;
37 extern char             *sys_errlist[];
38 #endif
39 extern time_t           currenttime;
40 extern pthread_mutex_t  currenttime_mutex;
41 extern int              active_threads;
42 extern pthread_mutex_t  active_threads_mutex;
43 extern pthread_mutex_t  new_conn_mutex;
44 extern int              slapd_shutdown;
45 extern pthread_t        listener_tid;
46 extern int              num_conns;
47 extern pthread_mutex_t  ops_mutex;
48 extern int              g_argc;
49 extern char             **g_argv;
50
51 int             dtblsize;
52 Connection      *c;
53
54 static void     set_shutdown();
55 static void     do_nothing();
56
57 void
58 daemon(
59     int port
60 )
61 {
62         Operation               *o;
63         BerElement              ber;
64         unsigned long           len, tag, msgid;
65         int                     i;
66         int                     tcps, ns;
67         struct sockaddr_in      addr;
68         fd_set                  readfds;
69         fd_set                  writefds;
70         FILE                    *fp;
71         int                     on = 1;
72
73 #ifdef USE_SYSCONF
74         dtblsize = sysconf( _SC_OPEN_MAX );
75 #else /* USE_SYSCONF */
76         dtblsize = getdtablesize();
77 #endif /* USE_SYSCONF */
78
79         c = (Connection *) ch_calloc( 1, dtblsize * sizeof(Connection) );
80
81         for ( i = 0; i < dtblsize; i++ ) {
82                 c[i].c_dn = NULL;
83                 c[i].c_addr = NULL;
84                 c[i].c_domain = NULL;
85                 c[i].c_ops = NULL;
86                 c[i].c_sb.sb_sd = -1;
87                 c[i].c_sb.sb_options = LBER_NO_READ_AHEAD;
88                 c[i].c_sb.sb_naddr = 0;
89                 c[i].c_sb.sb_ber.ber_buf = NULL;
90                 c[i].c_sb.sb_ber.ber_ptr = NULL;
91                 c[i].c_sb.sb_ber.ber_end = NULL;
92                 c[i].c_writewaiter = 0;
93                 c[i].c_connid = 0;
94                 pthread_mutex_init( &c[i].c_dnmutex,
95                     pthread_mutexattr_default );
96                 pthread_mutex_init( &c[i].c_opsmutex,
97                     pthread_mutexattr_default );
98                 pthread_mutex_init( &c[i].c_pdumutex,
99                     pthread_mutexattr_default );
100                 pthread_cond_init( &c[i].c_wcv, pthread_condattr_default );
101         }
102
103         if ( (tcps = socket( AF_INET, SOCK_STREAM, 0 )) == -1 ) {
104                 Debug( LDAP_DEBUG_ANY, "socket() failed errno %d (%s)", errno,
105                     errno > -1 && errno < sys_nerr ? sys_errlist[errno] :
106                     "unknown", 0 );
107                 exit( 1 );
108         }
109
110         i = 1;
111         if ( setsockopt( tcps, SOL_SOCKET, SO_REUSEADDR, (char *) &i,
112             sizeof(i) ) == -1 ) {
113                 Debug( LDAP_DEBUG_ANY, "setsockopt() failed errno %d (%s)",
114                     errno, errno > -1 && errno < sys_nerr ? sys_errlist[errno] :
115                     "unknown", 0 );
116         }
117
118         (void) memset( (void *) &addr, '\0', sizeof(addr) );
119         addr.sin_family = AF_INET;
120         addr.sin_addr.s_addr = INADDR_ANY;
121         addr.sin_port = htons( port );
122         if ( bind( tcps, (struct sockaddr *) &addr, sizeof(addr) ) == -1 ) {
123                 Debug( LDAP_DEBUG_ANY, "bind() failed errno %d (%s)\n",
124                     errno, errno > -1 && errno < sys_nerr ? sys_errlist[errno] :
125                     "unknown", 0 );
126                 exit( 1 );
127         }
128
129         if ( listen( tcps, 5 ) == -1 ) {
130                 Debug( LDAP_DEBUG_ANY, "listen() failed errno %d (%s)",
131                     errno, errno > -1 && errno < sys_nerr ? sys_errlist[errno] :
132                     "unknown", 0 );
133                 exit( 1 );
134         }
135
136         (void) SIGNAL( SIGPIPE, SIG_IGN );
137         (void) SIGNAL( SIGUSR1, (void *) do_nothing );
138         (void) SIGNAL( SIGUSR2, (void *) set_shutdown );
139         (void) SIGNAL( SIGTERM, (void *) set_shutdown );
140         (void) SIGNAL( SIGHUP, (void *) set_shutdown );
141
142         Debug( LDAP_DEBUG_ANY, "slapd starting\n", 0, 0, 0 );
143 #ifdef SLAPD_PIDFILE
144         if ( (fp = fopen( SLAPD_PIDFILE, "w" )) != NULL ) {
145                 fprintf( fp, "%d\n", getpid() );
146                 fclose( fp );
147         }
148 #endif
149 #ifdef SLAPD_ARGSFILE
150         if ( (fp = fopen( SLAPD_ARGSFILE, "w" )) != NULL ) {
151                 for ( i = 0; i < g_argc; i++ ) {
152                         fprintf( fp, "%s ", g_argv[i] );
153                 }
154                 fprintf( fp, "\n" );
155                 fclose( fp );
156         }
157 #endif
158
159         while ( !slapd_shutdown ) {
160                 struct sockaddr_in      from;
161                 struct hostent          *hp;
162                 struct timeval          zero;
163                 struct timeval          *tvp;
164                 int                     len, pid;
165
166                 FD_ZERO( &writefds );
167                 FD_ZERO( &readfds );
168                 FD_SET( tcps, &readfds );
169
170                 pthread_mutex_lock( &active_threads_mutex );
171                 Debug( LDAP_DEBUG_CONNS,
172                     "listening for connections on %d, activity on:",
173                     tcps, 0, 0 );
174
175                 pthread_mutex_lock( &new_conn_mutex );
176                 for ( i = 0; i < dtblsize; i++ ) {
177                         if ( c[i].c_sb.sb_sd != -1 ) {
178                                 FD_SET( c[i].c_sb.sb_sd, &readfds );
179
180                                 if ( c[i].c_writewaiter ) {
181                                         FD_SET( c[i].c_sb.sb_sd, &writefds );
182                                 }
183                                 Debug( LDAP_DEBUG_CONNS, " %dr%s", i,
184                                     c[i].c_writewaiter ? "w" : "", 0 );
185                         }
186                 }
187                 Debug( LDAP_DEBUG_CONNS, "\n", 0, 0, 0 );
188                 pthread_mutex_unlock( &new_conn_mutex );
189
190                 zero.tv_sec = 0;
191                 zero.tv_usec = 0;
192                 Debug( LDAP_DEBUG_CONNS, "before select active_threads %d\n",
193                     active_threads, 0, 0 );
194 #ifdef PTHREAD_PREEMPTIVE
195                 tvp = NULL;
196 #else
197                 tvp = active_threads ? &zero : NULL;
198 #endif
199                 pthread_mutex_unlock( &active_threads_mutex );
200
201                 switch ( select( dtblsize, &readfds, &writefds, 0, tvp ) ) {
202                 case -1:        /* failure - try again */
203                         Debug( LDAP_DEBUG_CONNS,
204                             "select failed errno %d (%s)\n",
205                             errno, errno > -1 && errno < sys_nerr ?
206                             sys_errlist[errno] : "unknown", 0 );
207                         continue;
208
209                 case 0:         /* timeout - let threads run */
210                         Debug( LDAP_DEBUG_CONNS, "select timeout - yielding\n",
211                             0, 0, 0 );
212                         pthread_yield();
213                         continue;
214
215                 default:        /* something happened - deal with it */
216                         Debug( LDAP_DEBUG_CONNS, "select activity\n", 0, 0, 0 );
217                         ;       /* FALL */
218                 }
219                 pthread_mutex_lock( &currenttime_mutex );
220                 time( &currenttime );
221                 pthread_mutex_unlock( &currenttime_mutex );
222
223                 /* new connection */
224                 pthread_mutex_lock( &new_conn_mutex );
225                 if ( FD_ISSET( tcps, &readfds ) ) {
226                         len = sizeof(from);
227                         if ( (ns = accept( tcps, (struct sockaddr *) &from,
228                             &len )) == -1 ) {
229                                 Debug( LDAP_DEBUG_ANY,
230                                     "accept() failed errno %d (%s)", errno,
231                                     errno > -1 && errno < sys_nerr ?
232                                     sys_errlist[errno] : "unknown", 0 );
233                                 pthread_mutex_unlock( &new_conn_mutex );
234                                 continue;
235                         }
236                         if ( ioctl( ns, FIONBIO, (caddr_t) &on ) == -1 ) {
237                                 Debug( LDAP_DEBUG_ANY,
238                                     "FIONBIO ioctl on %d faled\n", ns, 0, 0 );
239                         }
240                         c[ns].c_sb.sb_sd = ns;
241                         Debug( LDAP_DEBUG_CONNS, "new connection on %d\n", ns,
242                             0, 0 );
243
244                         pthread_mutex_lock( &ops_mutex );
245                         c[ns].c_connid = num_conns++;
246                         pthread_mutex_unlock( &ops_mutex );
247                         len = sizeof(from);
248                         if ( getpeername( ns, (struct sockaddr *) &from, &len )
249                             == 0 ) {
250                                 char    *s;
251 #ifdef REVERSE_LOOKUP
252                                 hp = gethostbyaddr( (char *)
253                                     &(from.sin_addr.s_addr),
254                                     sizeof(from.sin_addr.s_addr), AF_INET );
255 #else
256                                 hp = NULL;
257 #endif
258
259                                 Statslog( LDAP_DEBUG_STATS,
260                                     "conn=%d fd=%d connection from %s (%s)\n",
261                                     c[ns].c_connid, ns, hp == NULL ? "unknown"
262                                     : hp->h_name, inet_ntoa( from.sin_addr ),
263                                     0 );
264
265                                 if ( c[ns].c_addr != NULL ) {
266                                         free( c[ns].c_addr );
267                                 }
268                                 c[ns].c_addr = strdup( inet_ntoa(
269                                     from.sin_addr ) );
270                                 if ( c[ns].c_domain != NULL ) {
271                                         free( c[ns].c_domain );
272                                 }
273                                 c[ns].c_domain = strdup( hp == NULL ? "" :
274                                     hp->h_name );
275                                 /* normalize the domain */
276                                 for ( s = c[ns].c_domain; *s; s++ ) {
277                                         *s = TOLOWER( *s );
278                                 }
279                         } else {
280                                 Statslog( LDAP_DEBUG_STATS,
281                                     "conn=%d fd=%d connection from unknown\n",
282                                     c[ns].c_connid, ns, 0, 0, 0 );
283                         }
284                         pthread_mutex_lock( &c[ns].c_dnmutex );
285                         if ( c[ns].c_dn != NULL ) {
286                                 free( c[ns].c_dn );
287                                 c[ns].c_dn = NULL;
288                         }
289                         pthread_mutex_unlock( &c[ns].c_dnmutex );
290                         c[ns].c_starttime = currenttime;
291                         c[ns].c_opsinitiated = 0;
292                         c[ns].c_opscompleted = 0;
293                 }
294                 pthread_mutex_unlock( &new_conn_mutex );
295
296                 Debug( LDAP_DEBUG_CONNS, "activity on:", 0, 0, 0 );
297                 for ( i = 0; i < dtblsize; i++ ) {
298                         int     r, w;
299
300                         r = FD_ISSET( i, &readfds );
301                         w = FD_ISSET( i, &writefds );
302                         if ( i != tcps && (r || w) ) {
303                                 Debug( LDAP_DEBUG_CONNS, " %d%s%s", i,
304                                     r ? "r" : "", w ? "w" : "" );
305                         }
306                 }
307                 Debug( LDAP_DEBUG_CONNS, "\n", 0, 0, 0 );
308
309                 for ( i = 0; i < dtblsize; i++ ) {
310                         if ( i == tcps || (! FD_ISSET( i, &readfds ) &&
311                             ! FD_ISSET( i, &writefds )) ) {
312                                 continue;
313                         }
314
315                         if ( FD_ISSET( i, &writefds ) ) {
316                                 Debug( LDAP_DEBUG_CONNS,
317                                     "signaling write waiter on %d\n", i, 0, 0 );
318
319                                 pthread_mutex_lock( &active_threads_mutex );
320                                 pthread_cond_signal( &c[i].c_wcv );
321                                 c[i].c_writewaiter = 0;
322                                 active_threads++;
323                                 pthread_mutex_unlock( &active_threads_mutex );
324                         }
325
326                         if ( FD_ISSET( i, &readfds ) ) {
327                                 Debug( LDAP_DEBUG_CONNS,
328                                     "read activity on %d\n", i, 0, 0 );
329
330                                 connection_activity( &c[i] );
331                         }
332                 }
333
334                 pthread_yield();
335         }
336
337         close( tcps );
338         pthread_mutex_lock( &active_threads_mutex );
339         Debug( LDAP_DEBUG_ANY,
340             "slapd shutting down - waiting for %d threads to terminate\n",
341             active_threads, 0, 0 );
342         while ( active_threads > 0 ) {
343                 pthread_mutex_unlock( &active_threads_mutex );
344                 pthread_yield();
345                 pthread_mutex_lock( &active_threads_mutex );
346         }
347         pthread_mutex_unlock( &active_threads_mutex );
348
349         /* let backends do whatever cleanup they need to do */
350         Debug( LDAP_DEBUG_TRACE,
351             "slapd shutting down - waiting for backends to close down\n", 0, 0,
352             0 );
353         be_close();
354         Debug( LDAP_DEBUG_ANY, "slapd stopping\n", 0, 0, 0 );
355 }
356
357 static void
358 set_shutdown()
359 {
360         Debug( LDAP_DEBUG_ANY, "slapd got shutdown signal\n", 0, 0, 0 );
361         slapd_shutdown = 1;
362         pthread_kill( listener_tid, SIGUSR1 );
363         (void) SIGNAL( SIGUSR2, (void *) set_shutdown );
364         (void) SIGNAL( SIGTERM, (void *) set_shutdown );
365         (void) SIGNAL( SIGHUP, (void *) set_shutdown );
366 }
367
368 static void
369 do_nothing()
370 {
371         Debug( LDAP_DEBUG_TRACE, "slapd got SIGUSR1\n", 0, 0, 0 );
372         (void) SIGNAL( SIGUSR1, (void *) do_nothing );
373 }