xref: /dflybsd-src/usr.sbin/nscd/nscd.c (revision ed7f5f7abb722ac05a28b013cdf96a1105c2918a)
1*ed5d5720SPeter Avalos /*-
2*ed5d5720SPeter Avalos  * Copyright (c) 2005 Michael Bushkov <bushman@rsu.ru>
3*ed5d5720SPeter Avalos  * All rights reserved.
4*ed5d5720SPeter Avalos  *
5*ed5d5720SPeter Avalos  * Redistribution and use in source and binary forms, with or without
6*ed5d5720SPeter Avalos  * modification, are permitted provided that the following conditions
7*ed5d5720SPeter Avalos  * are met:
8*ed5d5720SPeter Avalos  * 1. Redistributions of source code must retain the above copyright
9*ed5d5720SPeter Avalos  *    notice, this list of conditions and the following disclaimer.
10*ed5d5720SPeter Avalos  * 2. Redistributions in binary form must reproduce the above copyright
11*ed5d5720SPeter Avalos  *    notice, this list of conditions and the following disclaimer in thereg
12*ed5d5720SPeter Avalos  *    documentation and/or other materials provided with the distribution.
13*ed5d5720SPeter Avalos  *
14*ed5d5720SPeter Avalos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15*ed5d5720SPeter Avalos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16*ed5d5720SPeter Avalos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17*ed5d5720SPeter Avalos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18*ed5d5720SPeter Avalos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19*ed5d5720SPeter Avalos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20*ed5d5720SPeter Avalos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21*ed5d5720SPeter Avalos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22*ed5d5720SPeter Avalos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23*ed5d5720SPeter Avalos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24*ed5d5720SPeter Avalos  * SUCH DAMAGE.
25*ed5d5720SPeter Avalos  *
26*ed5d5720SPeter Avalos  * $FreeBSD: src/usr.sbin/nscd/nscd.c,v 1.7 2008/10/23 00:27:35 delphij Exp $
27*ed5d5720SPeter Avalos  */
28*ed5d5720SPeter Avalos 
29*ed5d5720SPeter Avalos #include <sys/types.h>
30*ed5d5720SPeter Avalos #include <sys/event.h>
31*ed5d5720SPeter Avalos #include <sys/socket.h>
32*ed5d5720SPeter Avalos #include <sys/time.h>
33*ed5d5720SPeter Avalos #include <sys/param.h>
34*ed5d5720SPeter Avalos #include <sys/un.h>
35*ed5d5720SPeter Avalos #include <assert.h>
36*ed5d5720SPeter Avalos #include <err.h>
37*ed5d5720SPeter Avalos #include <errno.h>
38*ed5d5720SPeter Avalos #include <fcntl.h>
39*ed5d5720SPeter Avalos #include <libutil.h>
40*ed5d5720SPeter Avalos #include <pthread.h>
41*ed5d5720SPeter Avalos #include <signal.h>
42*ed5d5720SPeter Avalos #include <stdio.h>
43*ed5d5720SPeter Avalos #include <stdlib.h>
44*ed5d5720SPeter Avalos #include <string.h>
45*ed5d5720SPeter Avalos #include <unistd.h>
46*ed5d5720SPeter Avalos 
47*ed5d5720SPeter Avalos #include "agents/passwd.h"
48*ed5d5720SPeter Avalos #include "agents/group.h"
49*ed5d5720SPeter Avalos #include "agents/services.h"
50*ed5d5720SPeter Avalos #include "cachelib.h"
51*ed5d5720SPeter Avalos #include "config.h"
52*ed5d5720SPeter Avalos #include "debug.h"
53*ed5d5720SPeter Avalos #include "log.h"
54*ed5d5720SPeter Avalos #include "nscdcli.h"
55*ed5d5720SPeter Avalos #include "parser.h"
56*ed5d5720SPeter Avalos #include "query.h"
57*ed5d5720SPeter Avalos #include "singletons.h"
58*ed5d5720SPeter Avalos 
59*ed5d5720SPeter Avalos #ifndef CONFIG_PATH
60*ed5d5720SPeter Avalos #define CONFIG_PATH "/etc/nscd.conf"
61*ed5d5720SPeter Avalos #endif
62*ed5d5720SPeter Avalos #define DEFAULT_CONFIG_PATH	"nscd.conf"
63*ed5d5720SPeter Avalos 
64*ed5d5720SPeter Avalos #define MAX_SOCKET_IO_SIZE	4096
65*ed5d5720SPeter Avalos 
66*ed5d5720SPeter Avalos struct processing_thread_args {
67*ed5d5720SPeter Avalos 	cache	the_cache;
68*ed5d5720SPeter Avalos 	struct configuration	*the_configuration;
69*ed5d5720SPeter Avalos 	struct runtime_env		*the_runtime_env;
70*ed5d5720SPeter Avalos };
71*ed5d5720SPeter Avalos 
72*ed5d5720SPeter Avalos static void accept_connection(struct kevent *, struct runtime_env *,
73*ed5d5720SPeter Avalos 	struct configuration *);
74*ed5d5720SPeter Avalos static void destroy_cache_(cache);
75*ed5d5720SPeter Avalos static void destroy_runtime_env(struct runtime_env *);
76*ed5d5720SPeter Avalos static cache init_cache_(struct configuration *);
77*ed5d5720SPeter Avalos static struct runtime_env *init_runtime_env(struct configuration *);
78*ed5d5720SPeter Avalos static void processing_loop(cache, struct runtime_env *,
79*ed5d5720SPeter Avalos 	struct configuration *);
80*ed5d5720SPeter Avalos static void process_socket_event(struct kevent *, struct runtime_env *,
81*ed5d5720SPeter Avalos 	struct configuration *);
82*ed5d5720SPeter Avalos static void process_timer_event(struct kevent *, struct runtime_env *,
83*ed5d5720SPeter Avalos 	struct configuration *);
84*ed5d5720SPeter Avalos static void *processing_thread(void *);
85*ed5d5720SPeter Avalos static void usage(void);
86*ed5d5720SPeter Avalos 
87*ed5d5720SPeter Avalos void get_time_func(struct timeval *);
88*ed5d5720SPeter Avalos 
89*ed5d5720SPeter Avalos static void
usage(void)90*ed5d5720SPeter Avalos usage(void)
91*ed5d5720SPeter Avalos {
92*ed5d5720SPeter Avalos 	fprintf(stderr,
93*ed5d5720SPeter Avalos 	    "usage: nscd [-dnst] [-i cachename] [-I cachename]\n");
94*ed5d5720SPeter Avalos 	exit(1);
95*ed5d5720SPeter Avalos }
96*ed5d5720SPeter Avalos 
97*ed5d5720SPeter Avalos static cache
init_cache_(struct configuration * config)98*ed5d5720SPeter Avalos init_cache_(struct configuration *config)
99*ed5d5720SPeter Avalos {
100*ed5d5720SPeter Avalos 	struct cache_params params;
101*ed5d5720SPeter Avalos 	cache retval;
102*ed5d5720SPeter Avalos 
103*ed5d5720SPeter Avalos 	struct configuration_entry *config_entry;
104*ed5d5720SPeter Avalos 	size_t	size, i;
105*ed5d5720SPeter Avalos 	int res;
106*ed5d5720SPeter Avalos 
107*ed5d5720SPeter Avalos 	TRACE_IN(init_cache_);
108*ed5d5720SPeter Avalos 
109*ed5d5720SPeter Avalos 	memset(&params, 0, sizeof(struct cache_params));
110*ed5d5720SPeter Avalos 	params.get_time_func = get_time_func;
111*ed5d5720SPeter Avalos 	retval = init_cache(&params);
112*ed5d5720SPeter Avalos 
113*ed5d5720SPeter Avalos 	size = configuration_get_entries_size(config);
114*ed5d5720SPeter Avalos 	for (i = 0; i < size; ++i) {
115*ed5d5720SPeter Avalos 		config_entry = configuration_get_entry(config, i);
116*ed5d5720SPeter Avalos 		/*
117*ed5d5720SPeter Avalos 		 * We should register common entries now - multipart entries
118*ed5d5720SPeter Avalos 		 * would be registered automatically during the queries.
119*ed5d5720SPeter Avalos 		 */
120*ed5d5720SPeter Avalos 		res = register_cache_entry(retval, (struct cache_entry_params *)
121*ed5d5720SPeter Avalos 			&config_entry->positive_cache_params);
122*ed5d5720SPeter Avalos 		config_entry->positive_cache_entry = find_cache_entry(retval,
123*ed5d5720SPeter Avalos 			config_entry->positive_cache_params.entry_name);
124*ed5d5720SPeter Avalos 		assert(config_entry->positive_cache_entry !=
125*ed5d5720SPeter Avalos 			INVALID_CACHE_ENTRY);
126*ed5d5720SPeter Avalos 
127*ed5d5720SPeter Avalos 		res = register_cache_entry(retval, (struct cache_entry_params *)
128*ed5d5720SPeter Avalos 			&config_entry->negative_cache_params);
129*ed5d5720SPeter Avalos 		config_entry->negative_cache_entry = find_cache_entry(retval,
130*ed5d5720SPeter Avalos 			config_entry->negative_cache_params.entry_name);
131*ed5d5720SPeter Avalos 		assert(config_entry->negative_cache_entry !=
132*ed5d5720SPeter Avalos 			INVALID_CACHE_ENTRY);
133*ed5d5720SPeter Avalos 	}
134*ed5d5720SPeter Avalos 
135*ed5d5720SPeter Avalos 	LOG_MSG_2("cache", "cache was successfully initialized");
136*ed5d5720SPeter Avalos 	TRACE_OUT(init_cache_);
137*ed5d5720SPeter Avalos 	return (retval);
138*ed5d5720SPeter Avalos }
139*ed5d5720SPeter Avalos 
140*ed5d5720SPeter Avalos static void
destroy_cache_(cache the_cache)141*ed5d5720SPeter Avalos destroy_cache_(cache the_cache)
142*ed5d5720SPeter Avalos {
143*ed5d5720SPeter Avalos 	TRACE_IN(destroy_cache_);
144*ed5d5720SPeter Avalos 	destroy_cache(the_cache);
145*ed5d5720SPeter Avalos 	TRACE_OUT(destroy_cache_);
146*ed5d5720SPeter Avalos }
147*ed5d5720SPeter Avalos 
148*ed5d5720SPeter Avalos /*
149*ed5d5720SPeter Avalos  * Socket and kqueues are prepared here. We have one global queue for both
150*ed5d5720SPeter Avalos  * socket and timers events.
151*ed5d5720SPeter Avalos  */
152*ed5d5720SPeter Avalos static struct runtime_env *
init_runtime_env(struct configuration * config)153*ed5d5720SPeter Avalos init_runtime_env(struct configuration *config)
154*ed5d5720SPeter Avalos {
155*ed5d5720SPeter Avalos 	int serv_addr_len;
156*ed5d5720SPeter Avalos 	struct sockaddr_un serv_addr;
157*ed5d5720SPeter Avalos 
158*ed5d5720SPeter Avalos 	struct kevent eventlist;
159*ed5d5720SPeter Avalos 	struct timespec timeout;
160*ed5d5720SPeter Avalos 
161*ed5d5720SPeter Avalos 	struct runtime_env *retval;
162*ed5d5720SPeter Avalos 
163*ed5d5720SPeter Avalos 	TRACE_IN(init_runtime_env);
164*ed5d5720SPeter Avalos 	retval = (struct runtime_env *)calloc(1, sizeof(struct runtime_env));
165*ed5d5720SPeter Avalos 	assert(retval != NULL);
166*ed5d5720SPeter Avalos 
167*ed5d5720SPeter Avalos 	retval->sockfd = socket(PF_LOCAL, SOCK_STREAM, 0);
168*ed5d5720SPeter Avalos 
169*ed5d5720SPeter Avalos 	if (config->force_unlink == 1)
170*ed5d5720SPeter Avalos 		unlink(config->socket_path);
171*ed5d5720SPeter Avalos 
172*ed5d5720SPeter Avalos 	memset(&serv_addr, 0, sizeof(struct sockaddr_un));
173*ed5d5720SPeter Avalos 	serv_addr.sun_family = PF_LOCAL;
174*ed5d5720SPeter Avalos 	strlcpy(serv_addr.sun_path, config->socket_path,
175*ed5d5720SPeter Avalos 		sizeof(serv_addr.sun_path));
176*ed5d5720SPeter Avalos 	serv_addr_len = sizeof(serv_addr.sun_family) +
177*ed5d5720SPeter Avalos 		strlen(serv_addr.sun_path) + 1;
178*ed5d5720SPeter Avalos 
179*ed5d5720SPeter Avalos 	if (bind(retval->sockfd, (struct sockaddr *)&serv_addr,
180*ed5d5720SPeter Avalos 		serv_addr_len) == -1) {
181*ed5d5720SPeter Avalos 		close(retval->sockfd);
182*ed5d5720SPeter Avalos 		free(retval);
183*ed5d5720SPeter Avalos 
184*ed5d5720SPeter Avalos 		LOG_ERR_2("runtime environment", "can't bind socket to path: "
185*ed5d5720SPeter Avalos 			"%s", config->socket_path);
186*ed5d5720SPeter Avalos 		TRACE_OUT(init_runtime_env);
187*ed5d5720SPeter Avalos 		return (NULL);
188*ed5d5720SPeter Avalos 	}
189*ed5d5720SPeter Avalos 	LOG_MSG_2("runtime environment", "using socket %s",
190*ed5d5720SPeter Avalos 		config->socket_path);
191*ed5d5720SPeter Avalos 
192*ed5d5720SPeter Avalos 	/*
193*ed5d5720SPeter Avalos 	 * Here we're marking socket as non-blocking and setting its backlog
194*ed5d5720SPeter Avalos 	 * to the maximum value
195*ed5d5720SPeter Avalos 	 */
196*ed5d5720SPeter Avalos 	chmod(config->socket_path, config->socket_mode);
197*ed5d5720SPeter Avalos 	listen(retval->sockfd, -1);
198*ed5d5720SPeter Avalos 	fcntl(retval->sockfd, F_SETFL, O_NONBLOCK);
199*ed5d5720SPeter Avalos 
200*ed5d5720SPeter Avalos 	retval->queue = kqueue();
201*ed5d5720SPeter Avalos 	assert(retval->queue != -1);
202*ed5d5720SPeter Avalos 
203*ed5d5720SPeter Avalos 	EV_SET(&eventlist, retval->sockfd, EVFILT_READ, EV_ADD | EV_ONESHOT,
204*ed5d5720SPeter Avalos 		0, 0, 0);
205*ed5d5720SPeter Avalos 	memset(&timeout, 0, sizeof(struct timespec));
206*ed5d5720SPeter Avalos 	kevent(retval->queue, &eventlist, 1, NULL, 0, &timeout);
207*ed5d5720SPeter Avalos 
208*ed5d5720SPeter Avalos 	LOG_MSG_2("runtime environment", "successfully initialized");
209*ed5d5720SPeter Avalos 	TRACE_OUT(init_runtime_env);
210*ed5d5720SPeter Avalos 	return (retval);
211*ed5d5720SPeter Avalos }
212*ed5d5720SPeter Avalos 
213*ed5d5720SPeter Avalos static void
destroy_runtime_env(struct runtime_env * env)214*ed5d5720SPeter Avalos destroy_runtime_env(struct runtime_env *env)
215*ed5d5720SPeter Avalos {
216*ed5d5720SPeter Avalos 	TRACE_IN(destroy_runtime_env);
217*ed5d5720SPeter Avalos 	close(env->queue);
218*ed5d5720SPeter Avalos 	close(env->sockfd);
219*ed5d5720SPeter Avalos 	free(env);
220*ed5d5720SPeter Avalos 	TRACE_OUT(destroy_runtime_env);
221*ed5d5720SPeter Avalos }
222*ed5d5720SPeter Avalos 
223*ed5d5720SPeter Avalos static void
accept_connection(struct kevent * event_data,struct runtime_env * env,struct configuration * config)224*ed5d5720SPeter Avalos accept_connection(struct kevent *event_data, struct runtime_env *env,
225*ed5d5720SPeter Avalos 	struct configuration *config)
226*ed5d5720SPeter Avalos {
227*ed5d5720SPeter Avalos 	struct kevent	eventlist[2];
228*ed5d5720SPeter Avalos 	struct timespec	timeout;
229*ed5d5720SPeter Avalos 	struct query_state	*qstate;
230*ed5d5720SPeter Avalos 
231*ed5d5720SPeter Avalos 	int	fd;
232*ed5d5720SPeter Avalos 	int	res;
233*ed5d5720SPeter Avalos 
234*ed5d5720SPeter Avalos 	uid_t	euid;
235*ed5d5720SPeter Avalos 	gid_t	egid;
236*ed5d5720SPeter Avalos 
237*ed5d5720SPeter Avalos 	TRACE_IN(accept_connection);
238*ed5d5720SPeter Avalos 	fd = accept(event_data->ident, NULL, NULL);
239*ed5d5720SPeter Avalos 	if (fd == -1) {
240*ed5d5720SPeter Avalos 		LOG_ERR_2("accept_connection", "error %d during accept()",
241*ed5d5720SPeter Avalos 		    errno);
242*ed5d5720SPeter Avalos 		TRACE_OUT(accept_connection);
243*ed5d5720SPeter Avalos 		return;
244*ed5d5720SPeter Avalos 	}
245*ed5d5720SPeter Avalos 
246*ed5d5720SPeter Avalos 	if (getpeereid(fd, &euid, &egid) != 0) {
247*ed5d5720SPeter Avalos 		LOG_ERR_2("accept_connection", "error %d during getpeereid()",
248*ed5d5720SPeter Avalos 			errno);
249*ed5d5720SPeter Avalos 		TRACE_OUT(accept_connection);
250*ed5d5720SPeter Avalos 		return;
251*ed5d5720SPeter Avalos 	}
252*ed5d5720SPeter Avalos 
253*ed5d5720SPeter Avalos 	qstate = init_query_state(fd, sizeof(int), euid, egid);
254*ed5d5720SPeter Avalos 	if (qstate == NULL) {
255*ed5d5720SPeter Avalos 		LOG_ERR_2("accept_connection", "can't init query_state");
256*ed5d5720SPeter Avalos 		TRACE_OUT(accept_connection);
257*ed5d5720SPeter Avalos 		return;
258*ed5d5720SPeter Avalos 	}
259*ed5d5720SPeter Avalos 
260*ed5d5720SPeter Avalos 	memset(&timeout, 0, sizeof(struct timespec));
261*ed5d5720SPeter Avalos 	EV_SET(&eventlist[0], fd, EVFILT_TIMER, EV_ADD | EV_ONESHOT,
262*ed5d5720SPeter Avalos 		0, qstate->timeout.tv_sec * 1000, qstate);
263*ed5d5720SPeter Avalos 	EV_SET(&eventlist[1], fd, EVFILT_READ, EV_ADD | EV_ONESHOT,
264*ed5d5720SPeter Avalos 		NOTE_LOWAT, qstate->kevent_watermark, qstate);
265*ed5d5720SPeter Avalos 	res = kevent(env->queue, eventlist, 2, NULL, 0, &timeout);
266*ed5d5720SPeter Avalos 	if (res < 0)
267*ed5d5720SPeter Avalos 		LOG_ERR_2("accept_connection", "kevent error");
268*ed5d5720SPeter Avalos 
269*ed5d5720SPeter Avalos 	TRACE_OUT(accept_connection);
270*ed5d5720SPeter Avalos }
271*ed5d5720SPeter Avalos 
272*ed5d5720SPeter Avalos static void
process_socket_event(struct kevent * event_data,struct runtime_env * env,struct configuration * config)273*ed5d5720SPeter Avalos process_socket_event(struct kevent *event_data, struct runtime_env *env,
274*ed5d5720SPeter Avalos 	struct configuration *config)
275*ed5d5720SPeter Avalos {
276*ed5d5720SPeter Avalos 	struct kevent	eventlist[2];
277*ed5d5720SPeter Avalos 	struct timeval	query_timeout;
278*ed5d5720SPeter Avalos 	struct timespec	kevent_timeout;
279*ed5d5720SPeter Avalos 	int	nevents;
280*ed5d5720SPeter Avalos 	int	eof_res, res;
281*ed5d5720SPeter Avalos 	ssize_t	io_res;
282*ed5d5720SPeter Avalos 	struct query_state *qstate;
283*ed5d5720SPeter Avalos 
284*ed5d5720SPeter Avalos 	TRACE_IN(process_socket_event);
285*ed5d5720SPeter Avalos 	eof_res = event_data->flags & EV_EOF ? 1 : 0;
286*ed5d5720SPeter Avalos 	res = 0;
287*ed5d5720SPeter Avalos 
288*ed5d5720SPeter Avalos 	memset(&kevent_timeout, 0, sizeof(struct timespec));
289*ed5d5720SPeter Avalos 	EV_SET(&eventlist[0], event_data->ident, EVFILT_TIMER, EV_DELETE,
290*ed5d5720SPeter Avalos 		0, 0, NULL);
291*ed5d5720SPeter Avalos 	nevents = kevent(env->queue, eventlist, 1, NULL, 0, &kevent_timeout);
292*ed5d5720SPeter Avalos 	if (nevents == -1) {
293*ed5d5720SPeter Avalos 		if (errno == ENOENT) {
294*ed5d5720SPeter Avalos 			/* the timer is already handling this event */
295*ed5d5720SPeter Avalos 			TRACE_OUT(process_socket_event);
296*ed5d5720SPeter Avalos 			return;
297*ed5d5720SPeter Avalos 		} else {
298*ed5d5720SPeter Avalos 			/* some other error happened */
299*ed5d5720SPeter Avalos 			LOG_ERR_2("process_socket_event", "kevent error, errno"
300*ed5d5720SPeter Avalos 				" is %d", errno);
301*ed5d5720SPeter Avalos 			TRACE_OUT(process_socket_event);
302*ed5d5720SPeter Avalos 			return;
303*ed5d5720SPeter Avalos 		}
304*ed5d5720SPeter Avalos 	}
305*ed5d5720SPeter Avalos 	qstate = (struct query_state *)event_data->udata;
306*ed5d5720SPeter Avalos 
307*ed5d5720SPeter Avalos 	/*
308*ed5d5720SPeter Avalos 	 * If the buffer that is to be send/received is too large,
309*ed5d5720SPeter Avalos 	 * we send it implicitly, by using query_io_buffer_read and
310*ed5d5720SPeter Avalos 	 * query_io_buffer_write functions in the query_state. These functions
311*ed5d5720SPeter Avalos 	 * use the temporary buffer, which is later send/received in parts.
312*ed5d5720SPeter Avalos 	 * The code below implements buffer splitting/mergind for send/receive
313*ed5d5720SPeter Avalos 	 * operations. It also does the actual socket IO operations.
314*ed5d5720SPeter Avalos 	 */
315*ed5d5720SPeter Avalos 	if (((qstate->use_alternate_io == 0) &&
316*ed5d5720SPeter Avalos 		(qstate->kevent_watermark <= event_data->data)) ||
317*ed5d5720SPeter Avalos 		((qstate->use_alternate_io != 0) &&
318*ed5d5720SPeter Avalos 		(qstate->io_buffer_watermark <= event_data->data))) {
319*ed5d5720SPeter Avalos 		if (qstate->use_alternate_io != 0) {
320*ed5d5720SPeter Avalos 			switch (qstate->io_buffer_filter) {
321*ed5d5720SPeter Avalos 			case EVFILT_READ:
322*ed5d5720SPeter Avalos 				io_res = query_socket_read(qstate,
323*ed5d5720SPeter Avalos 					qstate->io_buffer_p,
324*ed5d5720SPeter Avalos 					qstate->io_buffer_watermark);
325*ed5d5720SPeter Avalos 				if (io_res < 0) {
326*ed5d5720SPeter Avalos 					qstate->use_alternate_io = 0;
327*ed5d5720SPeter Avalos 					qstate->process_func = NULL;
328*ed5d5720SPeter Avalos 				} else {
329*ed5d5720SPeter Avalos 					qstate->io_buffer_p += io_res;
330*ed5d5720SPeter Avalos 					if (qstate->io_buffer_p ==
331*ed5d5720SPeter Avalos 						qstate->io_buffer +
332*ed5d5720SPeter Avalos 						qstate->io_buffer_size) {
333*ed5d5720SPeter Avalos 						qstate->io_buffer_p =
334*ed5d5720SPeter Avalos 						    qstate->io_buffer;
335*ed5d5720SPeter Avalos 						qstate->use_alternate_io = 0;
336*ed5d5720SPeter Avalos 					}
337*ed5d5720SPeter Avalos 				}
338*ed5d5720SPeter Avalos 			break;
339*ed5d5720SPeter Avalos 			default:
340*ed5d5720SPeter Avalos 			break;
341*ed5d5720SPeter Avalos 			}
342*ed5d5720SPeter Avalos 		}
343*ed5d5720SPeter Avalos 
344*ed5d5720SPeter Avalos 		if (qstate->use_alternate_io == 0) {
345*ed5d5720SPeter Avalos 			do {
346*ed5d5720SPeter Avalos 				res = qstate->process_func(qstate);
347*ed5d5720SPeter Avalos 			} while ((qstate->kevent_watermark == 0) &&
348*ed5d5720SPeter Avalos 					(qstate->process_func != NULL) &&
349*ed5d5720SPeter Avalos 					(res == 0));
350*ed5d5720SPeter Avalos 
351*ed5d5720SPeter Avalos 			if (res != 0)
352*ed5d5720SPeter Avalos 				qstate->process_func = NULL;
353*ed5d5720SPeter Avalos 		}
354*ed5d5720SPeter Avalos 
355*ed5d5720SPeter Avalos 		if ((qstate->use_alternate_io != 0) &&
356*ed5d5720SPeter Avalos 			(qstate->io_buffer_filter == EVFILT_WRITE)) {
357*ed5d5720SPeter Avalos 			io_res = query_socket_write(qstate, qstate->io_buffer_p,
358*ed5d5720SPeter Avalos 				qstate->io_buffer_watermark);
359*ed5d5720SPeter Avalos 			if (io_res < 0) {
360*ed5d5720SPeter Avalos 				qstate->use_alternate_io = 0;
361*ed5d5720SPeter Avalos 				qstate->process_func = NULL;
362*ed5d5720SPeter Avalos 			} else
363*ed5d5720SPeter Avalos 				qstate->io_buffer_p += io_res;
364*ed5d5720SPeter Avalos 		}
365*ed5d5720SPeter Avalos 	} else {
366*ed5d5720SPeter Avalos 		/* assuming that socket was closed */
367*ed5d5720SPeter Avalos 		qstate->process_func = NULL;
368*ed5d5720SPeter Avalos 		qstate->use_alternate_io = 0;
369*ed5d5720SPeter Avalos 	}
370*ed5d5720SPeter Avalos 
371*ed5d5720SPeter Avalos 	if (((qstate->process_func == NULL) &&
372*ed5d5720SPeter Avalos 		(qstate->use_alternate_io == 0)) ||
373*ed5d5720SPeter Avalos 		(eof_res != 0) || (res != 0)) {
374*ed5d5720SPeter Avalos 		destroy_query_state(qstate);
375*ed5d5720SPeter Avalos 		close(event_data->ident);
376*ed5d5720SPeter Avalos 		TRACE_OUT(process_socket_event);
377*ed5d5720SPeter Avalos 		return;
378*ed5d5720SPeter Avalos 	}
379*ed5d5720SPeter Avalos 
380*ed5d5720SPeter Avalos 	/* updating the query_state lifetime variable */
381*ed5d5720SPeter Avalos 	get_time_func(&query_timeout);
382*ed5d5720SPeter Avalos 	query_timeout.tv_usec = 0;
383*ed5d5720SPeter Avalos 	query_timeout.tv_sec -= qstate->creation_time.tv_sec;
384*ed5d5720SPeter Avalos 	if (query_timeout.tv_sec > qstate->timeout.tv_sec)
385*ed5d5720SPeter Avalos 		query_timeout.tv_sec = 0;
386*ed5d5720SPeter Avalos 	else
387*ed5d5720SPeter Avalos 		query_timeout.tv_sec = qstate->timeout.tv_sec -
388*ed5d5720SPeter Avalos 			query_timeout.tv_sec;
389*ed5d5720SPeter Avalos 
390*ed5d5720SPeter Avalos 	if ((qstate->use_alternate_io != 0) && (qstate->io_buffer_p ==
391*ed5d5720SPeter Avalos 		qstate->io_buffer + qstate->io_buffer_size))
392*ed5d5720SPeter Avalos 		qstate->use_alternate_io = 0;
393*ed5d5720SPeter Avalos 
394*ed5d5720SPeter Avalos 	if (qstate->use_alternate_io == 0) {
395*ed5d5720SPeter Avalos 		/*
396*ed5d5720SPeter Avalos 		 * If we must send/receive the large block of data,
397*ed5d5720SPeter Avalos 		 * we should prepare the query_state's io_XXX fields.
398*ed5d5720SPeter Avalos 		 * We should also substitute its write_func and read_func
399*ed5d5720SPeter Avalos 		 * with the query_io_buffer_write and query_io_buffer_read,
400*ed5d5720SPeter Avalos 		 * which will allow us to implicitly send/receive this large
401*ed5d5720SPeter Avalos 		 * buffer later (in the subsequent calls to the
402*ed5d5720SPeter Avalos 		 * process_socket_event).
403*ed5d5720SPeter Avalos 		 */
404*ed5d5720SPeter Avalos 		if (qstate->kevent_watermark > MAX_SOCKET_IO_SIZE) {
405*ed5d5720SPeter Avalos 			if (qstate->io_buffer != NULL)
406*ed5d5720SPeter Avalos 				free(qstate->io_buffer);
407*ed5d5720SPeter Avalos 
408*ed5d5720SPeter Avalos 			qstate->io_buffer = (char *)calloc(1,
409*ed5d5720SPeter Avalos 				qstate->kevent_watermark);
410*ed5d5720SPeter Avalos 			assert(qstate->io_buffer != NULL);
411*ed5d5720SPeter Avalos 
412*ed5d5720SPeter Avalos 			qstate->io_buffer_p = qstate->io_buffer;
413*ed5d5720SPeter Avalos 			qstate->io_buffer_size = qstate->kevent_watermark;
414*ed5d5720SPeter Avalos 			qstate->io_buffer_filter = qstate->kevent_filter;
415*ed5d5720SPeter Avalos 
416*ed5d5720SPeter Avalos 			qstate->write_func = query_io_buffer_write;
417*ed5d5720SPeter Avalos 			qstate->read_func = query_io_buffer_read;
418*ed5d5720SPeter Avalos 
419*ed5d5720SPeter Avalos 			if (qstate->kevent_filter == EVFILT_READ)
420*ed5d5720SPeter Avalos 				qstate->use_alternate_io = 1;
421*ed5d5720SPeter Avalos 
422*ed5d5720SPeter Avalos 			qstate->io_buffer_watermark = MAX_SOCKET_IO_SIZE;
423*ed5d5720SPeter Avalos 			EV_SET(&eventlist[1], event_data->ident,
424*ed5d5720SPeter Avalos 				qstate->kevent_filter, EV_ADD | EV_ONESHOT,
425*ed5d5720SPeter Avalos 				NOTE_LOWAT, MAX_SOCKET_IO_SIZE, qstate);
426*ed5d5720SPeter Avalos 		} else {
427*ed5d5720SPeter Avalos 			EV_SET(&eventlist[1], event_data->ident,
428*ed5d5720SPeter Avalos 				qstate->kevent_filter, EV_ADD | EV_ONESHOT,
429*ed5d5720SPeter Avalos 				NOTE_LOWAT, qstate->kevent_watermark, qstate);
430*ed5d5720SPeter Avalos 		}
431*ed5d5720SPeter Avalos 	} else {
432*ed5d5720SPeter Avalos 		if (qstate->io_buffer + qstate->io_buffer_size -
433*ed5d5720SPeter Avalos 			qstate->io_buffer_p <
434*ed5d5720SPeter Avalos 			MAX_SOCKET_IO_SIZE) {
435*ed5d5720SPeter Avalos 			qstate->io_buffer_watermark = qstate->io_buffer +
436*ed5d5720SPeter Avalos 				qstate->io_buffer_size - qstate->io_buffer_p;
437*ed5d5720SPeter Avalos 			EV_SET(&eventlist[1], event_data->ident,
438*ed5d5720SPeter Avalos 				qstate->io_buffer_filter,
439*ed5d5720SPeter Avalos 				EV_ADD | EV_ONESHOT, NOTE_LOWAT,
440*ed5d5720SPeter Avalos 				qstate->io_buffer_watermark,
441*ed5d5720SPeter Avalos 				qstate);
442*ed5d5720SPeter Avalos 		} else {
443*ed5d5720SPeter Avalos 			qstate->io_buffer_watermark = MAX_SOCKET_IO_SIZE;
444*ed5d5720SPeter Avalos 			EV_SET(&eventlist[1], event_data->ident,
445*ed5d5720SPeter Avalos 				qstate->io_buffer_filter, EV_ADD | EV_ONESHOT,
446*ed5d5720SPeter Avalos 				NOTE_LOWAT, MAX_SOCKET_IO_SIZE, qstate);
447*ed5d5720SPeter Avalos 		}
448*ed5d5720SPeter Avalos 	}
449*ed5d5720SPeter Avalos 	EV_SET(&eventlist[0], event_data->ident, EVFILT_TIMER,
450*ed5d5720SPeter Avalos 		EV_ADD | EV_ONESHOT, 0, query_timeout.tv_sec * 1000, qstate);
451*ed5d5720SPeter Avalos 	kevent(env->queue, eventlist, 2, NULL, 0, &kevent_timeout);
452*ed5d5720SPeter Avalos 
453*ed5d5720SPeter Avalos 	TRACE_OUT(process_socket_event);
454*ed5d5720SPeter Avalos }
455*ed5d5720SPeter Avalos 
456*ed5d5720SPeter Avalos /*
457*ed5d5720SPeter Avalos  * This routine is called if timer event has been signaled in the kqueue. It
458*ed5d5720SPeter Avalos  * just closes the socket and destroys the query_state.
459*ed5d5720SPeter Avalos  */
460*ed5d5720SPeter Avalos static void
process_timer_event(struct kevent * event_data,struct runtime_env * env,struct configuration * config)461*ed5d5720SPeter Avalos process_timer_event(struct kevent *event_data, struct runtime_env *env,
462*ed5d5720SPeter Avalos 	struct configuration *config)
463*ed5d5720SPeter Avalos {
464*ed5d5720SPeter Avalos 	struct query_state	*qstate;
465*ed5d5720SPeter Avalos 
466*ed5d5720SPeter Avalos 	TRACE_IN(process_timer_event);
467*ed5d5720SPeter Avalos 	qstate = (struct query_state *)event_data->udata;
468*ed5d5720SPeter Avalos 	destroy_query_state(qstate);
469*ed5d5720SPeter Avalos 	close(event_data->ident);
470*ed5d5720SPeter Avalos 	TRACE_OUT(process_timer_event);
471*ed5d5720SPeter Avalos }
472*ed5d5720SPeter Avalos 
473*ed5d5720SPeter Avalos /*
474*ed5d5720SPeter Avalos  * Processing loop is the basic processing routine, that forms a body of each
475*ed5d5720SPeter Avalos  * procssing thread
476*ed5d5720SPeter Avalos  */
477*ed5d5720SPeter Avalos static void
processing_loop(cache the_cache,struct runtime_env * env,struct configuration * config)478*ed5d5720SPeter Avalos processing_loop(cache the_cache, struct runtime_env *env,
479*ed5d5720SPeter Avalos 	struct configuration *config)
480*ed5d5720SPeter Avalos {
481*ed5d5720SPeter Avalos 	struct timespec timeout;
482*ed5d5720SPeter Avalos 	const int eventlist_size = 1;
483*ed5d5720SPeter Avalos 	struct kevent eventlist[eventlist_size];
484*ed5d5720SPeter Avalos 	int nevents, i;
485*ed5d5720SPeter Avalos 
486*ed5d5720SPeter Avalos 	TRACE_MSG("=> processing_loop");
487*ed5d5720SPeter Avalos 	memset(&timeout, 0, sizeof(struct timespec));
488*ed5d5720SPeter Avalos 	memset(&eventlist, 0, sizeof(struct kevent) * eventlist_size);
489*ed5d5720SPeter Avalos 
490*ed5d5720SPeter Avalos 	for (;;) {
491*ed5d5720SPeter Avalos 		nevents = kevent(env->queue, NULL, 0, eventlist,
492*ed5d5720SPeter Avalos 			eventlist_size, NULL);
493*ed5d5720SPeter Avalos 		/*
494*ed5d5720SPeter Avalos 		 * we can only receive 1 event on success
495*ed5d5720SPeter Avalos 		 */
496*ed5d5720SPeter Avalos 		if (nevents == 1) {
497*ed5d5720SPeter Avalos 			struct kevent *event_data;
498*ed5d5720SPeter Avalos 			event_data = &eventlist[0];
499*ed5d5720SPeter Avalos 
500*ed5d5720SPeter Avalos 			if (event_data->ident == env->sockfd) {
501*ed5d5720SPeter Avalos 				for (i = 0; i < event_data->data; ++i)
502*ed5d5720SPeter Avalos 				    accept_connection(event_data, env, config);
503*ed5d5720SPeter Avalos 
504*ed5d5720SPeter Avalos 				EV_SET(eventlist, s_runtime_env->sockfd,
505*ed5d5720SPeter Avalos 				    EVFILT_READ, EV_ADD | EV_ONESHOT,
506*ed5d5720SPeter Avalos 				    0, 0, 0);
507*ed5d5720SPeter Avalos 				memset(&timeout, 0,
508*ed5d5720SPeter Avalos 				    sizeof(struct timespec));
509*ed5d5720SPeter Avalos 				kevent(s_runtime_env->queue, eventlist,
510*ed5d5720SPeter Avalos 				    1, NULL, 0, &timeout);
511*ed5d5720SPeter Avalos 
512*ed5d5720SPeter Avalos 			} else {
513*ed5d5720SPeter Avalos 				switch (event_data->filter) {
514*ed5d5720SPeter Avalos 				case EVFILT_READ:
515*ed5d5720SPeter Avalos 				case EVFILT_WRITE:
516*ed5d5720SPeter Avalos 					process_socket_event(event_data,
517*ed5d5720SPeter Avalos 						env, config);
518*ed5d5720SPeter Avalos 					break;
519*ed5d5720SPeter Avalos 				case EVFILT_TIMER:
520*ed5d5720SPeter Avalos 					process_timer_event(event_data,
521*ed5d5720SPeter Avalos 						env, config);
522*ed5d5720SPeter Avalos 					break;
523*ed5d5720SPeter Avalos 				default:
524*ed5d5720SPeter Avalos 					break;
525*ed5d5720SPeter Avalos 				}
526*ed5d5720SPeter Avalos 			}
527*ed5d5720SPeter Avalos 		} else {
528*ed5d5720SPeter Avalos 			/* this branch shouldn't be currently executed */
529*ed5d5720SPeter Avalos 		}
530*ed5d5720SPeter Avalos 	}
531*ed5d5720SPeter Avalos 
532*ed5d5720SPeter Avalos 	TRACE_MSG("<= processing_loop");
533*ed5d5720SPeter Avalos }
534*ed5d5720SPeter Avalos 
535*ed5d5720SPeter Avalos /*
536*ed5d5720SPeter Avalos  * Wrapper above the processing loop function. It sets the thread signal mask
537*ed5d5720SPeter Avalos  * to avoid SIGPIPE signals (which can happen if the client works incorrectly).
538*ed5d5720SPeter Avalos  */
539*ed5d5720SPeter Avalos static void *
processing_thread(void * data)540*ed5d5720SPeter Avalos processing_thread(void *data)
541*ed5d5720SPeter Avalos {
542*ed5d5720SPeter Avalos 	struct processing_thread_args	*args;
543*ed5d5720SPeter Avalos 	sigset_t new;
544*ed5d5720SPeter Avalos 
545*ed5d5720SPeter Avalos 	TRACE_MSG("=> processing_thread");
546*ed5d5720SPeter Avalos 	args = (struct processing_thread_args *)data;
547*ed5d5720SPeter Avalos 
548*ed5d5720SPeter Avalos 	sigemptyset(&new);
549*ed5d5720SPeter Avalos 	sigaddset(&new, SIGPIPE);
550*ed5d5720SPeter Avalos 	if (pthread_sigmask(SIG_BLOCK, &new, NULL) != 0)
551*ed5d5720SPeter Avalos 		LOG_ERR_1("processing thread",
552*ed5d5720SPeter Avalos 			"thread can't block the SIGPIPE signal");
553*ed5d5720SPeter Avalos 
554*ed5d5720SPeter Avalos 	processing_loop(args->the_cache, args->the_runtime_env,
555*ed5d5720SPeter Avalos 		args->the_configuration);
556*ed5d5720SPeter Avalos 	free(args);
557*ed5d5720SPeter Avalos 	TRACE_MSG("<= processing_thread");
558*ed5d5720SPeter Avalos 
559*ed5d5720SPeter Avalos 	return (NULL);
560*ed5d5720SPeter Avalos }
561*ed5d5720SPeter Avalos 
562*ed5d5720SPeter Avalos void
get_time_func(struct timeval * time)563*ed5d5720SPeter Avalos get_time_func(struct timeval *time)
564*ed5d5720SPeter Avalos {
565*ed5d5720SPeter Avalos 	struct timespec res;
566*ed5d5720SPeter Avalos 	memset(&res, 0, sizeof(struct timespec));
567*ed5d5720SPeter Avalos 	clock_gettime(CLOCK_MONOTONIC, &res);
568*ed5d5720SPeter Avalos 
569*ed5d5720SPeter Avalos 	time->tv_sec = res.tv_sec;
570*ed5d5720SPeter Avalos 	time->tv_usec = 0;
571*ed5d5720SPeter Avalos }
572*ed5d5720SPeter Avalos 
573*ed5d5720SPeter Avalos /*
574*ed5d5720SPeter Avalos  * The idea of _nss_cache_cycle_prevention_function is that nsdispatch will
575*ed5d5720SPeter Avalos  * search for this symbol in the executable. This symbol is the attribute of
576*ed5d5720SPeter Avalos  * the caching daemon. So, if it exists, nsdispatch won't try to connect to
577*ed5d5720SPeter Avalos  * the caching daemon and will just ignore the 'cache' source in the
578*ed5d5720SPeter Avalos  * nsswitch.conf. This method helps to avoid cycles and organize
579*ed5d5720SPeter Avalos  * self-performing requests.
580*ed5d5720SPeter Avalos  */
581*ed5d5720SPeter Avalos void
_nss_cache_cycle_prevention_function(void)582*ed5d5720SPeter Avalos _nss_cache_cycle_prevention_function(void)
583*ed5d5720SPeter Avalos {
584*ed5d5720SPeter Avalos }
585*ed5d5720SPeter Avalos 
586*ed5d5720SPeter Avalos int
main(int argc,char * argv[])587*ed5d5720SPeter Avalos main(int argc, char *argv[])
588*ed5d5720SPeter Avalos {
589*ed5d5720SPeter Avalos 	struct processing_thread_args *thread_args;
590*ed5d5720SPeter Avalos 	pthread_t *threads;
591*ed5d5720SPeter Avalos 
592*ed5d5720SPeter Avalos 	struct pidfh *pidfile;
593*ed5d5720SPeter Avalos 	pid_t pid;
594*ed5d5720SPeter Avalos 
595*ed5d5720SPeter Avalos 	char const *config_file;
596*ed5d5720SPeter Avalos 	char const *error_str;
597*ed5d5720SPeter Avalos 	int error_line;
598*ed5d5720SPeter Avalos 	int i, res;
599*ed5d5720SPeter Avalos 
600*ed5d5720SPeter Avalos 	int trace_mode_enabled;
601*ed5d5720SPeter Avalos 	int force_single_threaded;
602*ed5d5720SPeter Avalos 	int do_not_daemonize;
603*ed5d5720SPeter Avalos 	int clear_user_cache_entries, clear_all_cache_entries;
604*ed5d5720SPeter Avalos 	char *user_config_entry_name, *global_config_entry_name;
605*ed5d5720SPeter Avalos 	int show_statistics;
606*ed5d5720SPeter Avalos 	int daemon_mode, interactive_mode;
607*ed5d5720SPeter Avalos 
608*ed5d5720SPeter Avalos 
609*ed5d5720SPeter Avalos 	/* by default all debug messages are omitted */
610*ed5d5720SPeter Avalos 	TRACE_OFF();
611*ed5d5720SPeter Avalos 
612*ed5d5720SPeter Avalos 	/* parsing command line arguments */
613*ed5d5720SPeter Avalos 	trace_mode_enabled = 0;
614*ed5d5720SPeter Avalos 	force_single_threaded = 0;
615*ed5d5720SPeter Avalos 	do_not_daemonize = 0;
616*ed5d5720SPeter Avalos 	clear_user_cache_entries = 0;
617*ed5d5720SPeter Avalos 	clear_all_cache_entries = 0;
618*ed5d5720SPeter Avalos 	show_statistics = 0;
619*ed5d5720SPeter Avalos 	user_config_entry_name = NULL;
620*ed5d5720SPeter Avalos 	global_config_entry_name = NULL;
621*ed5d5720SPeter Avalos 	while ((res = getopt(argc, argv, "nstdi:I:")) != -1) {
622*ed5d5720SPeter Avalos 		switch (res) {
623*ed5d5720SPeter Avalos 		case 'n':
624*ed5d5720SPeter Avalos 			do_not_daemonize = 1;
625*ed5d5720SPeter Avalos 			break;
626*ed5d5720SPeter Avalos 		case 's':
627*ed5d5720SPeter Avalos 			force_single_threaded = 1;
628*ed5d5720SPeter Avalos 			break;
629*ed5d5720SPeter Avalos 		case 't':
630*ed5d5720SPeter Avalos 			trace_mode_enabled = 1;
631*ed5d5720SPeter Avalos 			break;
632*ed5d5720SPeter Avalos 		case 'i':
633*ed5d5720SPeter Avalos 			clear_user_cache_entries = 1;
634*ed5d5720SPeter Avalos 			if (optarg != NULL)
635*ed5d5720SPeter Avalos 				if (strcmp(optarg, "all") != 0)
636*ed5d5720SPeter Avalos 					user_config_entry_name = strdup(optarg);
637*ed5d5720SPeter Avalos 			break;
638*ed5d5720SPeter Avalos 		case 'I':
639*ed5d5720SPeter Avalos 			clear_all_cache_entries = 1;
640*ed5d5720SPeter Avalos 			if (optarg != NULL)
641*ed5d5720SPeter Avalos 				if (strcmp(optarg, "all") != 0)
642*ed5d5720SPeter Avalos 					global_config_entry_name =
643*ed5d5720SPeter Avalos 						strdup(optarg);
644*ed5d5720SPeter Avalos 			break;
645*ed5d5720SPeter Avalos 		case 'd':
646*ed5d5720SPeter Avalos 			show_statistics = 1;
647*ed5d5720SPeter Avalos 			break;
648*ed5d5720SPeter Avalos 		case '?':
649*ed5d5720SPeter Avalos 		default:
650*ed5d5720SPeter Avalos 			usage();
651*ed5d5720SPeter Avalos 			/* NOT REACHED */
652*ed5d5720SPeter Avalos 		}
653*ed5d5720SPeter Avalos 	}
654*ed5d5720SPeter Avalos 
655*ed5d5720SPeter Avalos 	daemon_mode = do_not_daemonize | force_single_threaded |
656*ed5d5720SPeter Avalos 		trace_mode_enabled;
657*ed5d5720SPeter Avalos 	interactive_mode = clear_user_cache_entries | clear_all_cache_entries |
658*ed5d5720SPeter Avalos 		show_statistics;
659*ed5d5720SPeter Avalos 
660*ed5d5720SPeter Avalos 	if ((daemon_mode != 0) && (interactive_mode != 0)) {
661*ed5d5720SPeter Avalos 		LOG_ERR_1("main", "daemon mode and interactive_mode arguments "
662*ed5d5720SPeter Avalos 			"can't be used together");
663*ed5d5720SPeter Avalos 		usage();
664*ed5d5720SPeter Avalos 	}
665*ed5d5720SPeter Avalos 
666*ed5d5720SPeter Avalos 	if (interactive_mode != 0) {
667*ed5d5720SPeter Avalos 		FILE *pidfin = fopen(DEFAULT_PIDFILE_PATH, "r");
668*ed5d5720SPeter Avalos 		char pidbuf[256];
669*ed5d5720SPeter Avalos 
670*ed5d5720SPeter Avalos 		struct nscd_connection_params connection_params;
671*ed5d5720SPeter Avalos 		nscd_connection connection;
672*ed5d5720SPeter Avalos 
673*ed5d5720SPeter Avalos 		int result;
674*ed5d5720SPeter Avalos 
675*ed5d5720SPeter Avalos 		if (pidfin == NULL)
676*ed5d5720SPeter Avalos 			errx(EXIT_FAILURE, "There is no daemon running.");
677*ed5d5720SPeter Avalos 
678*ed5d5720SPeter Avalos 		memset(pidbuf, 0, sizeof(pidbuf));
679*ed5d5720SPeter Avalos 		fread(pidbuf, sizeof(pidbuf) - 1, 1, pidfin);
680*ed5d5720SPeter Avalos 		fclose(pidfin);
681*ed5d5720SPeter Avalos 
682*ed5d5720SPeter Avalos 		if (ferror(pidfin) != 0)
683*ed5d5720SPeter Avalos 			errx(EXIT_FAILURE, "Can't read from pidfile.");
684*ed5d5720SPeter Avalos 
685*ed5d5720SPeter Avalos 		if (sscanf(pidbuf, "%d", &pid) != 1)
686*ed5d5720SPeter Avalos 			errx(EXIT_FAILURE, "Invalid pidfile.");
687*ed5d5720SPeter Avalos 		LOG_MSG_1("main", "daemon PID is %d", pid);
688*ed5d5720SPeter Avalos 
689*ed5d5720SPeter Avalos 
690*ed5d5720SPeter Avalos 		memset(&connection_params, 0,
691*ed5d5720SPeter Avalos 			sizeof(struct nscd_connection_params));
692*ed5d5720SPeter Avalos 		connection_params.socket_path = DEFAULT_SOCKET_PATH;
693*ed5d5720SPeter Avalos 		connection = open_nscd_connection__(&connection_params);
694*ed5d5720SPeter Avalos 		if (connection == INVALID_NSCD_CONNECTION)
695*ed5d5720SPeter Avalos 			errx(EXIT_FAILURE, "Can't connect to the daemon.");
696*ed5d5720SPeter Avalos 
697*ed5d5720SPeter Avalos 		if (clear_user_cache_entries != 0) {
698*ed5d5720SPeter Avalos 			result = nscd_transform__(connection,
699*ed5d5720SPeter Avalos 				user_config_entry_name, TT_USER);
700*ed5d5720SPeter Avalos 			if (result != 0)
701*ed5d5720SPeter Avalos 				LOG_MSG_1("main",
702*ed5d5720SPeter Avalos 					"user cache transformation failed");
703*ed5d5720SPeter Avalos 			else
704*ed5d5720SPeter Avalos 				LOG_MSG_1("main",
705*ed5d5720SPeter Avalos 					"user cache_transformation "
706*ed5d5720SPeter Avalos 					"succeeded");
707*ed5d5720SPeter Avalos 		}
708*ed5d5720SPeter Avalos 
709*ed5d5720SPeter Avalos 		if (clear_all_cache_entries != 0) {
710*ed5d5720SPeter Avalos 			if (geteuid() != 0)
711*ed5d5720SPeter Avalos 				errx(EXIT_FAILURE, "Only root can initiate "
712*ed5d5720SPeter Avalos 					"global cache transformation.");
713*ed5d5720SPeter Avalos 
714*ed5d5720SPeter Avalos 			result = nscd_transform__(connection,
715*ed5d5720SPeter Avalos 				global_config_entry_name, TT_ALL);
716*ed5d5720SPeter Avalos 			if (result != 0)
717*ed5d5720SPeter Avalos 				LOG_MSG_1("main",
718*ed5d5720SPeter Avalos 					"global cache transformation "
719*ed5d5720SPeter Avalos 					"failed");
720*ed5d5720SPeter Avalos 			else
721*ed5d5720SPeter Avalos 				LOG_MSG_1("main",
722*ed5d5720SPeter Avalos 					"global cache transformation "
723*ed5d5720SPeter Avalos 					"succeeded");
724*ed5d5720SPeter Avalos 		}
725*ed5d5720SPeter Avalos 
726*ed5d5720SPeter Avalos 		close_nscd_connection__(connection);
727*ed5d5720SPeter Avalos 
728*ed5d5720SPeter Avalos 		free(user_config_entry_name);
729*ed5d5720SPeter Avalos 		free(global_config_entry_name);
730*ed5d5720SPeter Avalos 		return (EXIT_SUCCESS);
731*ed5d5720SPeter Avalos 	}
732*ed5d5720SPeter Avalos 
733*ed5d5720SPeter Avalos 	pidfile = pidfile_open(DEFAULT_PIDFILE_PATH, 0644, &pid);
734*ed5d5720SPeter Avalos 	if (pidfile == NULL) {
735*ed5d5720SPeter Avalos 		if (errno == EEXIST)
736*ed5d5720SPeter Avalos 			errx(EXIT_FAILURE, "Daemon already running, pid: %d.",
737*ed5d5720SPeter Avalos 				pid);
738*ed5d5720SPeter Avalos 		warn("Cannot open or create pidfile");
739*ed5d5720SPeter Avalos 	}
740*ed5d5720SPeter Avalos 
741*ed5d5720SPeter Avalos 	if (trace_mode_enabled == 1)
742*ed5d5720SPeter Avalos 		TRACE_ON();
743*ed5d5720SPeter Avalos 
744*ed5d5720SPeter Avalos 	/* blocking the main thread from receiving SIGPIPE signal */
745*ed5d5720SPeter Avalos 	sigblock(sigmask(SIGPIPE));
746*ed5d5720SPeter Avalos 
747*ed5d5720SPeter Avalos 	/* daemonization */
748*ed5d5720SPeter Avalos 	if (do_not_daemonize == 0) {
749*ed5d5720SPeter Avalos 		res = daemon(0, trace_mode_enabled == 0 ? 0 : 1);
750*ed5d5720SPeter Avalos 		if (res != 0) {
751*ed5d5720SPeter Avalos 			LOG_ERR_1("main", "can't daemonize myself: %s",
752*ed5d5720SPeter Avalos 				strerror(errno));
753*ed5d5720SPeter Avalos 			pidfile_remove(pidfile);
754*ed5d5720SPeter Avalos 			goto fin;
755*ed5d5720SPeter Avalos 		} else
756*ed5d5720SPeter Avalos 			LOG_MSG_1("main", "successfully daemonized");
757*ed5d5720SPeter Avalos 	}
758*ed5d5720SPeter Avalos 
759*ed5d5720SPeter Avalos 	pidfile_write(pidfile);
760*ed5d5720SPeter Avalos 
761*ed5d5720SPeter Avalos 	s_agent_table = init_agent_table();
762*ed5d5720SPeter Avalos 	register_agent(s_agent_table, init_passwd_agent());
763*ed5d5720SPeter Avalos 	register_agent(s_agent_table, init_passwd_mp_agent());
764*ed5d5720SPeter Avalos 	register_agent(s_agent_table, init_group_agent());
765*ed5d5720SPeter Avalos 	register_agent(s_agent_table, init_group_mp_agent());
766*ed5d5720SPeter Avalos 	register_agent(s_agent_table, init_services_agent());
767*ed5d5720SPeter Avalos 	register_agent(s_agent_table, init_services_mp_agent());
768*ed5d5720SPeter Avalos 	LOG_MSG_1("main", "request agents registered successfully");
769*ed5d5720SPeter Avalos 
770*ed5d5720SPeter Avalos 	/*
771*ed5d5720SPeter Avalos 	 * Hosts agent can't work properly until we have access to the
772*ed5d5720SPeter Avalos 	 * appropriate dtab structures, which are used in nsdispatch
773*ed5d5720SPeter Avalos 	 * calls
774*ed5d5720SPeter Avalos 	 *
775*ed5d5720SPeter Avalos 	 register_agent(s_agent_table, init_hosts_agent());
776*ed5d5720SPeter Avalos 	*/
777*ed5d5720SPeter Avalos 
778*ed5d5720SPeter Avalos 	/* configuration initialization */
779*ed5d5720SPeter Avalos 	s_configuration = init_configuration();
780*ed5d5720SPeter Avalos 	fill_configuration_defaults(s_configuration);
781*ed5d5720SPeter Avalos 
782*ed5d5720SPeter Avalos 	error_str = NULL;
783*ed5d5720SPeter Avalos 	error_line = 0;
784*ed5d5720SPeter Avalos 	config_file = CONFIG_PATH;
785*ed5d5720SPeter Avalos 
786*ed5d5720SPeter Avalos 	res = parse_config_file(s_configuration, config_file, &error_str,
787*ed5d5720SPeter Avalos 		&error_line);
788*ed5d5720SPeter Avalos 	if ((res != 0) && (error_str == NULL)) {
789*ed5d5720SPeter Avalos 		config_file = DEFAULT_CONFIG_PATH;
790*ed5d5720SPeter Avalos 		res = parse_config_file(s_configuration, config_file,
791*ed5d5720SPeter Avalos 			&error_str, &error_line);
792*ed5d5720SPeter Avalos 	}
793*ed5d5720SPeter Avalos 
794*ed5d5720SPeter Avalos 	if (res != 0) {
795*ed5d5720SPeter Avalos 		if (error_str != NULL) {
796*ed5d5720SPeter Avalos 		LOG_ERR_1("main", "error in configuration file(%s, %d): %s\n",
797*ed5d5720SPeter Avalos 			config_file, error_line, error_str);
798*ed5d5720SPeter Avalos 		} else {
799*ed5d5720SPeter Avalos 		LOG_ERR_1("main", "no configuration file found "
800*ed5d5720SPeter Avalos 			"- was looking for %s and %s",
801*ed5d5720SPeter Avalos 			CONFIG_PATH, DEFAULT_CONFIG_PATH);
802*ed5d5720SPeter Avalos 		}
803*ed5d5720SPeter Avalos 		destroy_configuration(s_configuration);
804*ed5d5720SPeter Avalos 		return (-1);
805*ed5d5720SPeter Avalos 	}
806*ed5d5720SPeter Avalos 
807*ed5d5720SPeter Avalos 	if (force_single_threaded == 1)
808*ed5d5720SPeter Avalos 		s_configuration->threads_num = 1;
809*ed5d5720SPeter Avalos 
810*ed5d5720SPeter Avalos 	/* cache initialization */
811*ed5d5720SPeter Avalos 	s_cache = init_cache_(s_configuration);
812*ed5d5720SPeter Avalos 	if (s_cache == NULL) {
813*ed5d5720SPeter Avalos 		LOG_ERR_1("main", "can't initialize the cache");
814*ed5d5720SPeter Avalos 		destroy_configuration(s_configuration);
815*ed5d5720SPeter Avalos 		return (-1);
816*ed5d5720SPeter Avalos 	}
817*ed5d5720SPeter Avalos 
818*ed5d5720SPeter Avalos 	/* runtime environment initialization */
819*ed5d5720SPeter Avalos 	s_runtime_env = init_runtime_env(s_configuration);
820*ed5d5720SPeter Avalos 	if (s_runtime_env == NULL) {
821*ed5d5720SPeter Avalos 		LOG_ERR_1("main", "can't initialize the runtime environment");
822*ed5d5720SPeter Avalos 		destroy_configuration(s_configuration);
823*ed5d5720SPeter Avalos 		destroy_cache_(s_cache);
824*ed5d5720SPeter Avalos 		return (-1);
825*ed5d5720SPeter Avalos 	}
826*ed5d5720SPeter Avalos 
827*ed5d5720SPeter Avalos 	if (s_configuration->threads_num > 1) {
828*ed5d5720SPeter Avalos 		threads = (pthread_t *)calloc(1, sizeof(pthread_t) *
829*ed5d5720SPeter Avalos 			s_configuration->threads_num);
830*ed5d5720SPeter Avalos 		for (i = 0; i < s_configuration->threads_num; ++i) {
831*ed5d5720SPeter Avalos 			thread_args = (struct processing_thread_args *)malloc(
832*ed5d5720SPeter Avalos 				sizeof(struct processing_thread_args));
833*ed5d5720SPeter Avalos 			thread_args->the_cache = s_cache;
834*ed5d5720SPeter Avalos 			thread_args->the_runtime_env = s_runtime_env;
835*ed5d5720SPeter Avalos 			thread_args->the_configuration = s_configuration;
836*ed5d5720SPeter Avalos 
837*ed5d5720SPeter Avalos 			LOG_MSG_1("main", "thread #%d was successfully created",
838*ed5d5720SPeter Avalos 				i);
839*ed5d5720SPeter Avalos 			pthread_create(&threads[i], NULL, processing_thread,
840*ed5d5720SPeter Avalos 				thread_args);
841*ed5d5720SPeter Avalos 
842*ed5d5720SPeter Avalos 			thread_args = NULL;
843*ed5d5720SPeter Avalos 		}
844*ed5d5720SPeter Avalos 
845*ed5d5720SPeter Avalos 		for (i = 0; i < s_configuration->threads_num; ++i)
846*ed5d5720SPeter Avalos 			pthread_join(threads[i], NULL);
847*ed5d5720SPeter Avalos 	} else {
848*ed5d5720SPeter Avalos 		LOG_MSG_1("main", "working in single-threaded mode");
849*ed5d5720SPeter Avalos 		processing_loop(s_cache, s_runtime_env, s_configuration);
850*ed5d5720SPeter Avalos 	}
851*ed5d5720SPeter Avalos 
852*ed5d5720SPeter Avalos fin:
853*ed5d5720SPeter Avalos 	/* runtime environment destruction */
854*ed5d5720SPeter Avalos 	destroy_runtime_env(s_runtime_env);
855*ed5d5720SPeter Avalos 
856*ed5d5720SPeter Avalos 	/* cache destruction */
857*ed5d5720SPeter Avalos 	destroy_cache_(s_cache);
858*ed5d5720SPeter Avalos 
859*ed5d5720SPeter Avalos 	/* configuration destruction */
860*ed5d5720SPeter Avalos 	destroy_configuration(s_configuration);
861*ed5d5720SPeter Avalos 
862*ed5d5720SPeter Avalos 	/* agents table destruction */
863*ed5d5720SPeter Avalos 	destroy_agent_table(s_agent_table);
864*ed5d5720SPeter Avalos 
865*ed5d5720SPeter Avalos 	pidfile_remove(pidfile);
866*ed5d5720SPeter Avalos 	return (EXIT_SUCCESS);
867*ed5d5720SPeter Avalos }
868