daemon.c 10.9 KB
Newer Older
1
2
3
4
5
6
7

/* Revision history
 *
 * 5-Jun-96	hodges
 *	Added locking of new_conn_mutex when traversing the c[] array.
 */

Kurt Zeilenga's avatar
Kurt Zeilenga committed
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <errno.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <signal.h>
#ifdef _AIX
#include <sys/select.h>
#endif
#include "slap.h"
#include "portable.h"
#include "ldapconfig.h"
#ifdef NEED_FILIO
#include <sys/filio.h>
#else /* NEED_FILIO */
#include <sys/ioctl.h>
#endif /* NEED_FILIO */
#ifdef USE_SYSCONF
#include <unistd.h>
#endif /* USE_SYSCONF */

33
34
35
36
37
38
39
#ifdef TCP_WRAPPERS
#include <tcpd.h>

int allow_severity = LOG_INFO;
int deny_severity = LOG_NOTICE;
#endif /* TCP_WRAPPERS */

Kurt Zeilenga's avatar
Kurt Zeilenga committed
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
extern Operation	*op_add();

#ifndef SYSERRLIST_IN_STDIO
extern int		sys_nerr;
extern char		*sys_errlist[];
#endif
extern time_t		currenttime;
extern pthread_mutex_t	currenttime_mutex;
extern int		active_threads;
extern pthread_mutex_t	active_threads_mutex;
extern pthread_mutex_t	new_conn_mutex;
extern int		slapd_shutdown;
extern pthread_t	listener_tid;
extern int		num_conns;
extern pthread_mutex_t	ops_mutex;
extern int		g_argc;
extern char		**g_argv;

int		dtblsize;
Connection	*c;

static void	set_shutdown();
static void	do_nothing();

void
Kurt Zeilenga's avatar
Kurt Zeilenga committed
65
slapd_daemon(
Kurt Zeilenga's avatar
Kurt Zeilenga committed
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
    int	port
)
{
	Operation		*o;
	BerElement		ber;
	unsigned long		len, tag, msgid;
	int			i;
	int			tcps, ns;
	struct sockaddr_in	addr;
	fd_set			readfds;
	fd_set			writefds;
	FILE			*fp;
	int			on = 1;

#ifdef USE_SYSCONF
        dtblsize = sysconf( _SC_OPEN_MAX );
#else /* USE_SYSCONF */
        dtblsize = getdtablesize();
#endif /* USE_SYSCONF */
85
86
87
88
89
90
	/*
	 * Add greg@greg.rim.or.jp
	 */
	if(dtblsize > FD_SETSIZE) {
		dtblsize = FD_SETSIZE;
	}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
	c = (Connection *) ch_calloc( 1, dtblsize * sizeof(Connection) );

	for ( i = 0; i < dtblsize; i++ ) {
		c[i].c_dn = NULL;
		c[i].c_addr = NULL;
		c[i].c_domain = NULL;
		c[i].c_ops = NULL;
		c[i].c_sb.sb_sd = -1;
		c[i].c_sb.sb_options = LBER_NO_READ_AHEAD;
		c[i].c_sb.sb_naddr = 0;
		c[i].c_sb.sb_ber.ber_buf = NULL;
		c[i].c_sb.sb_ber.ber_ptr = NULL;
		c[i].c_sb.sb_ber.ber_end = NULL;
		c[i].c_writewaiter = 0;
		c[i].c_connid = 0;
		pthread_mutex_init( &c[i].c_dnmutex,
		    pthread_mutexattr_default );
		pthread_mutex_init( &c[i].c_opsmutex,
		    pthread_mutexattr_default );
		pthread_mutex_init( &c[i].c_pdumutex,
		    pthread_mutexattr_default );
		pthread_cond_init( &c[i].c_wcv, pthread_condattr_default );
	}

	if ( (tcps = socket( AF_INET, SOCK_STREAM, 0 )) == -1 ) {
		Debug( LDAP_DEBUG_ANY, "socket() failed errno %d (%s)", errno,
		    errno > -1 && errno < sys_nerr ? sys_errlist[errno] :
		    "unknown", 0 );
		exit( 1 );
	}

	i = 1;
	if ( setsockopt( tcps, SOL_SOCKET, SO_REUSEADDR, (char *) &i,
	    sizeof(i) ) == -1 ) {
		Debug( LDAP_DEBUG_ANY, "setsockopt() failed errno %d (%s)",
		    errno, errno > -1 && errno < sys_nerr ? sys_errlist[errno] :
		    "unknown", 0 );
	}

	(void) memset( (void *) &addr, '\0', sizeof(addr) );
	addr.sin_family = AF_INET;
	addr.sin_addr.s_addr = INADDR_ANY;
	addr.sin_port = htons( port );
	if ( bind( tcps, (struct sockaddr *) &addr, sizeof(addr) ) == -1 ) {
		Debug( LDAP_DEBUG_ANY, "bind() failed errno %d (%s)\n",
		    errno, errno > -1 && errno < sys_nerr ? sys_errlist[errno] :
		    "unknown", 0 );
		exit( 1 );
	}

	if ( listen( tcps, 5 ) == -1 ) {
		Debug( LDAP_DEBUG_ANY, "listen() failed errno %d (%s)",
		    errno, errno > -1 && errno < sys_nerr ? sys_errlist[errno] :
		    "unknown", 0 );
		exit( 1 );
	}

	(void) SIGNAL( SIGPIPE, SIG_IGN );
149
150
151
#ifdef SIGSTKFLT
	(void) SIGNAL( SIGSTKFLT, (void *) do_nothing );
#else
Kurt Zeilenga's avatar
Kurt Zeilenga committed
152
	(void) SIGNAL( SIGUSR1, (void *) do_nothing );
153
#endif
Kurt Zeilenga's avatar
Kurt Zeilenga committed
154
#ifdef SIGUNUSED
155
156
	(void) SIGNAL( SIGUNUSED, (void *) set_shutdown );
#else
Kurt Zeilenga's avatar
Kurt Zeilenga committed
157
	(void) SIGNAL( SIGUSR2, (void *) set_shutdown );
158
#endif
Kurt Zeilenga's avatar
Kurt Zeilenga committed
159
	(void) SIGNAL( SIGTERM, (void *) set_shutdown );
160
	(void) SIGNAL( SIGINT, (void *) set_shutdown );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
	(void) SIGNAL( SIGHUP, (void *) set_shutdown );

	Debug( LDAP_DEBUG_ANY, "slapd starting\n", 0, 0, 0 );
#ifdef SLAPD_PIDFILE
	if ( (fp = fopen( SLAPD_PIDFILE, "w" )) != NULL ) {
		fprintf( fp, "%d\n", getpid() );
		fclose( fp );
	}
#endif
#ifdef SLAPD_ARGSFILE
	if ( (fp = fopen( SLAPD_ARGSFILE, "w" )) != NULL ) {
		for ( i = 0; i < g_argc; i++ ) {
			fprintf( fp, "%s ", g_argv[i] );
		}
		fprintf( fp, "\n" );
		fclose( fp );
	}
#endif

	while ( !slapd_shutdown ) {
		struct sockaddr_in	from;
		struct hostent		*hp;
		struct timeval		zero;
		struct timeval		*tvp;
		int			len, pid;

187
188
189
		char	*client_name;
		char	*client_addr;

Kurt Zeilenga's avatar
Kurt Zeilenga committed
190
191
192
193
194
195
196
197
		FD_ZERO( &writefds );
		FD_ZERO( &readfds );
		FD_SET( tcps, &readfds );

		pthread_mutex_lock( &active_threads_mutex );
		Debug( LDAP_DEBUG_CONNS,
		    "listening for connections on %d, activity on:",
		    tcps, 0, 0 );
198
199

		pthread_mutex_lock( &new_conn_mutex );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
200
201
202
203
204
205
206
207
208
209
210
211
		for ( i = 0; i < dtblsize; i++ ) {
			if ( c[i].c_sb.sb_sd != -1 ) {
				FD_SET( c[i].c_sb.sb_sd, &readfds );

				if ( c[i].c_writewaiter ) {
					FD_SET( c[i].c_sb.sb_sd, &writefds );
				}
				Debug( LDAP_DEBUG_CONNS, " %dr%s", i,
				    c[i].c_writewaiter ? "w" : "", 0 );
			}
		}
		Debug( LDAP_DEBUG_CONNS, "\n", 0, 0, 0 );
212
		pthread_mutex_unlock( &new_conn_mutex );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261

		zero.tv_sec = 0;
		zero.tv_usec = 0;
		Debug( LDAP_DEBUG_CONNS, "before select active_threads %d\n",
		    active_threads, 0, 0 );
#ifdef PTHREAD_PREEMPTIVE
		tvp = NULL;
#else
		tvp = active_threads ? &zero : NULL;
#endif
		pthread_mutex_unlock( &active_threads_mutex );

		switch ( select( dtblsize, &readfds, &writefds, 0, tvp ) ) {
		case -1:	/* failure - try again */
			Debug( LDAP_DEBUG_CONNS,
			    "select failed errno %d (%s)\n",
			    errno, errno > -1 && errno < sys_nerr ?
			    sys_errlist[errno] : "unknown", 0 );
			continue;

		case 0:		/* timeout - let threads run */
			Debug( LDAP_DEBUG_CONNS, "select timeout - yielding\n",
			    0, 0, 0 );
			pthread_yield();
			continue;

		default:	/* something happened - deal with it */
			Debug( LDAP_DEBUG_CONNS, "select activity\n", 0, 0, 0 );
			;	/* FALL */
		}
		pthread_mutex_lock( &currenttime_mutex );
		time( &currenttime );
		pthread_mutex_unlock( &currenttime_mutex );

		/* new connection */
		pthread_mutex_lock( &new_conn_mutex );
		if ( FD_ISSET( tcps, &readfds ) ) {
			len = sizeof(from);
			if ( (ns = accept( tcps, (struct sockaddr *) &from,
			    &len )) == -1 ) {
				Debug( LDAP_DEBUG_ANY,
				    "accept() failed errno %d (%s)", errno,
				    errno > -1 && errno < sys_nerr ?
				    sys_errlist[errno] : "unknown", 0 );
				pthread_mutex_unlock( &new_conn_mutex );
				continue;
			}
			if ( ioctl( ns, FIONBIO, (caddr_t) &on ) == -1 ) {
				Debug( LDAP_DEBUG_ANY,
262
				    "FIONBIO ioctl on %d failed\n", ns, 0, 0 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
263
			}
264

Kurt Zeilenga's avatar
Kurt Zeilenga committed
265
266
267
268
269
270
271
			c[ns].c_sb.sb_sd = ns;
			Debug( LDAP_DEBUG_CONNS, "new connection on %d\n", ns,
			    0, 0 );

			pthread_mutex_lock( &ops_mutex );
			c[ns].c_connid = num_conns++;
			pthread_mutex_unlock( &ops_mutex );
272

Kurt Zeilenga's avatar
Kurt Zeilenga committed
273
			len = sizeof(from);
274

Kurt Zeilenga's avatar
Kurt Zeilenga committed
275
276
			if ( getpeername( ns, (struct sockaddr *) &from, &len )
			    == 0 ) {
277
278
279
280
				char *s;
				client_addr = inet_ntoa( from.sin_addr );

#if defined(REVERSE_LOOKUP) || defined(TCP_WRAPPERS)
Kurt Zeilenga's avatar
Kurt Zeilenga committed
281
282
283
284
				hp = gethostbyaddr( (char *)
				    &(from.sin_addr.s_addr),
				    sizeof(from.sin_addr.s_addr), AF_INET );

285
286
				if(hp) {
					client_name = hp->h_name;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
287

288
289
290
291
292
293
294
					/* normalize the domain */
					for ( s = client_name; *s; s++ ) {
						*s = TOLOWER( *s );
					}

				} else {
					client_name = NULL;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
295
				}
296
297
298
299
#else
				client_name = NULL;
#endif

Kurt Zeilenga's avatar
Kurt Zeilenga committed
300
			} else {
301
302
303
304
305
306
307
308
309
				client_name = NULL;;
				client_addr = NULL;
			}

#ifdef TCP_WRAPPERS
			if(!hosts_ctl("slapd", client_name, client_addr,
				STRING_UNKNOWN))
			{
				/* DENY ACCESS */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
310
				Statslog( LDAP_DEBUG_STATS,
311
312
313
314
315
316
317
318
319
			   	 "conn=%d fd=%d connection from %s (%s) denied.\n",
			   	 	c[ns].c_connid, ns,
						client_name == NULL ? "unknown" : client_name,
						client_addr == NULL ? "unknown" : client_addr,
			   	  0 );

				close(ns);
				pthread_mutex_unlock( &new_conn_mutex );
				continue;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
320
			}
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#endif /* TCP_WRAPPERS */

			Statslog( LDAP_DEBUG_STATS,
			    "conn=%d fd=%d connection from %s (%s) accepted.\n",
			    	c[ns].c_connid, ns,
					client_name == NULL ? "unknown" : client_name,
					client_addr == NULL ? "unknown" : client_addr,
			     0 );

			if ( c[ns].c_addr != NULL ) {
				free( c[ns].c_addr );
			}
			c[ns].c_addr = strdup( client_addr );

			if ( c[ns].c_domain != NULL ) {
				free( c[ns].c_domain );
			}

			c[ns].c_domain = strdup( client_name == NULL
				? "" : client_name );

Kurt Zeilenga's avatar
Kurt Zeilenga committed
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
			pthread_mutex_lock( &c[ns].c_dnmutex );
			if ( c[ns].c_dn != NULL ) {
				free( c[ns].c_dn );
				c[ns].c_dn = NULL;
			}
			pthread_mutex_unlock( &c[ns].c_dnmutex );
			c[ns].c_starttime = currenttime;
			c[ns].c_opsinitiated = 0;
			c[ns].c_opscompleted = 0;
		}
		pthread_mutex_unlock( &new_conn_mutex );

		Debug( LDAP_DEBUG_CONNS, "activity on:", 0, 0, 0 );
		for ( i = 0; i < dtblsize; i++ ) {
			int	r, w;

			r = FD_ISSET( i, &readfds );
			w = FD_ISSET( i, &writefds );
			if ( i != tcps && (r || w) ) {
				Debug( LDAP_DEBUG_CONNS, " %d%s%s", i,
				    r ? "r" : "", w ? "w" : "" );
			}
		}
		Debug( LDAP_DEBUG_CONNS, "\n", 0, 0, 0 );

		for ( i = 0; i < dtblsize; i++ ) {
			if ( i == tcps || (! FD_ISSET( i, &readfds ) &&
			    ! FD_ISSET( i, &writefds )) ) {
				continue;
			}

			if ( FD_ISSET( i, &writefds ) ) {
				Debug( LDAP_DEBUG_CONNS,
				    "signaling write waiter on %d\n", i, 0, 0 );

				pthread_mutex_lock( &active_threads_mutex );
				pthread_cond_signal( &c[i].c_wcv );
				c[i].c_writewaiter = 0;
				active_threads++;
				pthread_mutex_unlock( &active_threads_mutex );
			}

			if ( FD_ISSET( i, &readfds ) ) {
				Debug( LDAP_DEBUG_CONNS,
				    "read activity on %d\n", i, 0, 0 );

				connection_activity( &c[i] );
			}
		}

		pthread_yield();
	}

	close( tcps );
	pthread_mutex_lock( &active_threads_mutex );
	Debug( LDAP_DEBUG_ANY,
	    "slapd shutting down - waiting for %d threads to terminate\n",
	    active_threads, 0, 0 );
	while ( active_threads > 0 ) {
		pthread_mutex_unlock( &active_threads_mutex );
		pthread_yield();
		pthread_mutex_lock( &active_threads_mutex );
	}
	pthread_mutex_unlock( &active_threads_mutex );

	/* let backends do whatever cleanup they need to do */
	Debug( LDAP_DEBUG_TRACE,
	    "slapd shutting down - waiting for backends to close down\n", 0, 0,
	    0 );
	be_close();
	Debug( LDAP_DEBUG_ANY, "slapd stopping\n", 0, 0, 0 );
}

static void
set_shutdown()
{
	Debug( LDAP_DEBUG_ANY, "slapd got shutdown signal\n", 0, 0, 0 );
	slapd_shutdown = 1;
420
421
422
#ifdef SIGSTKFLT
	pthread_kill( listener_tid, SIGSTKFLT );
#else
Kurt Zeilenga's avatar
Kurt Zeilenga committed
423
	pthread_kill( listener_tid, SIGUSR1 );
424
425
426
427
#endif
#ifdef SIGUNUSED
	(void) SIGNAL( SIGUNUSED, (void *) set_shutdown );
#else
Kurt Zeilenga's avatar
Kurt Zeilenga committed
428
	(void) SIGNAL( SIGUSR2, (void *) set_shutdown );
429
#endif
Kurt Zeilenga's avatar
Kurt Zeilenga committed
430
	(void) SIGNAL( SIGTERM, (void *) set_shutdown );
431
	(void) SIGNAL( SIGINT, (void *) set_shutdown );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
432
433
434
435
436
437
	(void) SIGNAL( SIGHUP, (void *) set_shutdown );
}

static void
do_nothing()
{
438
439
440
441
	Debug( LDAP_DEBUG_TRACE, "slapd got do_nothing signal\n", 0, 0, 0 );
#ifdef SIGSTKFLT
	(void) SIGNAL( SIGSTKFLT, (void *) do_nothing );
#else
Kurt Zeilenga's avatar
Kurt Zeilenga committed
442
	(void) SIGNAL( SIGUSR1, (void *) do_nothing );
443
#endif
Kurt Zeilenga's avatar
Kurt Zeilenga committed
444
}