xref: /titanic_50/usr/src/cmd/svc/configd/configd.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate #include <assert.h>
30*7c478bd9Sstevel@tonic-gate #include <door.h>
31*7c478bd9Sstevel@tonic-gate #include <errno.h>
32*7c478bd9Sstevel@tonic-gate #include <fcntl.h>
33*7c478bd9Sstevel@tonic-gate #include <priv.h>
34*7c478bd9Sstevel@tonic-gate #include <procfs.h>
35*7c478bd9Sstevel@tonic-gate #include <pthread.h>
36*7c478bd9Sstevel@tonic-gate #include <signal.h>
37*7c478bd9Sstevel@tonic-gate #include <stdarg.h>
38*7c478bd9Sstevel@tonic-gate #include <stdio.h>
39*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
40*7c478bd9Sstevel@tonic-gate #include <string.h>
41*7c478bd9Sstevel@tonic-gate #include <syslog.h>
42*7c478bd9Sstevel@tonic-gate #include <sys/corectl.h>
43*7c478bd9Sstevel@tonic-gate #include <sys/resource.h>
44*7c478bd9Sstevel@tonic-gate #include <sys/stat.h>
45*7c478bd9Sstevel@tonic-gate #include <sys/wait.h>
46*7c478bd9Sstevel@tonic-gate #include <ucontext.h>
47*7c478bd9Sstevel@tonic-gate #include <unistd.h>
48*7c478bd9Sstevel@tonic-gate 
49*7c478bd9Sstevel@tonic-gate #include "configd.h"
50*7c478bd9Sstevel@tonic-gate 
51*7c478bd9Sstevel@tonic-gate /*
52*7c478bd9Sstevel@tonic-gate  * This file manages the overall startup and shutdown of configd, as well
53*7c478bd9Sstevel@tonic-gate  * as managing its door thread pool and per-thread datastructures.
54*7c478bd9Sstevel@tonic-gate  *
55*7c478bd9Sstevel@tonic-gate  * 1.  Per-thread Datastructures
56*7c478bd9Sstevel@tonic-gate  * -----------------------------
57*7c478bd9Sstevel@tonic-gate  * Each configd thread has an associated thread_info_t which contains its
58*7c478bd9Sstevel@tonic-gate  * current state.  A pointer is kept to this in TSD, keyed by thread_info_key.
59*7c478bd9Sstevel@tonic-gate  * The thread_info_ts for all threads in configd are kept on a single global
60*7c478bd9Sstevel@tonic-gate  * list, thread_list.  After creation, the state in the thread_info structure
61*7c478bd9Sstevel@tonic-gate  * is only modified by the associated thread, so no locking is needed.  A TSD
62*7c478bd9Sstevel@tonic-gate  * destructor removes the thread_info from the global list and frees it at
63*7c478bd9Sstevel@tonic-gate  * pthread_exit() time.
64*7c478bd9Sstevel@tonic-gate  *
65*7c478bd9Sstevel@tonic-gate  * Threads access their per-thread data using thread_self()
66*7c478bd9Sstevel@tonic-gate  *
67*7c478bd9Sstevel@tonic-gate  * The thread_list is protected by thread_lock, a leaf lock.
68*7c478bd9Sstevel@tonic-gate  *
69*7c478bd9Sstevel@tonic-gate  * 2. Door Thread Pool Management
70*7c478bd9Sstevel@tonic-gate  * ------------------------------
71*7c478bd9Sstevel@tonic-gate  * Whenever door_return(3door) returns from the kernel and there are no
72*7c478bd9Sstevel@tonic-gate  * other configd threads waiting for requests, libdoor automatically
73*7c478bd9Sstevel@tonic-gate  * invokes a function registered with door_server_create(), to request a new
74*7c478bd9Sstevel@tonic-gate  * door server thread.  The default function just creates a thread that calls
75*7c478bd9Sstevel@tonic-gate  * door_return(3door).  Unfortunately, since it can take a while for the new
76*7c478bd9Sstevel@tonic-gate  * thread to *get* to door_return(3door), a stream of requests can cause a
77*7c478bd9Sstevel@tonic-gate  * large number of threads to be created, even though they aren't all needed.
78*7c478bd9Sstevel@tonic-gate  *
79*7c478bd9Sstevel@tonic-gate  * In our callback, new_server_needed(), we limit ourself to two new threads
80*7c478bd9Sstevel@tonic-gate  * at a time -- this logic is handled in reserve_new_thread().  This keeps
81*7c478bd9Sstevel@tonic-gate  * us from creating an absurd number of threads in response to peaking load.
82*7c478bd9Sstevel@tonic-gate  */
83*7c478bd9Sstevel@tonic-gate static pthread_key_t	thread_info_key;
84*7c478bd9Sstevel@tonic-gate static pthread_attr_t	thread_attr;
85*7c478bd9Sstevel@tonic-gate 
86*7c478bd9Sstevel@tonic-gate static pthread_mutex_t	thread_lock = PTHREAD_MUTEX_INITIALIZER;
87*7c478bd9Sstevel@tonic-gate int			num_started;	/* number actually running */
88*7c478bd9Sstevel@tonic-gate int			num_servers;	/* number in-progress or running */
89*7c478bd9Sstevel@tonic-gate static uu_list_pool_t	*thread_pool;
90*7c478bd9Sstevel@tonic-gate uu_list_t		*thread_list;
91*7c478bd9Sstevel@tonic-gate 
92*7c478bd9Sstevel@tonic-gate static thread_info_t	main_thread_info;
93*7c478bd9Sstevel@tonic-gate 
94*7c478bd9Sstevel@tonic-gate static int	finished;
95*7c478bd9Sstevel@tonic-gate 
96*7c478bd9Sstevel@tonic-gate static pid_t	privileged_pid = 0;
97*7c478bd9Sstevel@tonic-gate static int	privileged_psinfo_fd = -1;
98*7c478bd9Sstevel@tonic-gate 
99*7c478bd9Sstevel@tonic-gate static int	privileged_user = 0;
100*7c478bd9Sstevel@tonic-gate 
101*7c478bd9Sstevel@tonic-gate static priv_set_t *privileged_privs;
102*7c478bd9Sstevel@tonic-gate 
103*7c478bd9Sstevel@tonic-gate static int	log_to_syslog = 0;
104*7c478bd9Sstevel@tonic-gate 
105*7c478bd9Sstevel@tonic-gate int		is_main_repository = 1;
106*7c478bd9Sstevel@tonic-gate 
107*7c478bd9Sstevel@tonic-gate int		max_repository_backups = 4;
108*7c478bd9Sstevel@tonic-gate 
109*7c478bd9Sstevel@tonic-gate #define	CONFIGD_MAX_FDS		262144
110*7c478bd9Sstevel@tonic-gate 
111*7c478bd9Sstevel@tonic-gate /*
112*7c478bd9Sstevel@tonic-gate  * Thanks, Mike
113*7c478bd9Sstevel@tonic-gate  */
114*7c478bd9Sstevel@tonic-gate void
115*7c478bd9Sstevel@tonic-gate abort_handler(int sig, siginfo_t *sip, ucontext_t *ucp)
116*7c478bd9Sstevel@tonic-gate {
117*7c478bd9Sstevel@tonic-gate 	struct sigaction act;
118*7c478bd9Sstevel@tonic-gate 
119*7c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&act.sa_mask);
120*7c478bd9Sstevel@tonic-gate 	act.sa_handler = SIG_DFL;
121*7c478bd9Sstevel@tonic-gate 	act.sa_flags = 0;
122*7c478bd9Sstevel@tonic-gate 	(void) sigaction(sig, &act, NULL);
123*7c478bd9Sstevel@tonic-gate 
124*7c478bd9Sstevel@tonic-gate 	(void) printstack(2);
125*7c478bd9Sstevel@tonic-gate 
126*7c478bd9Sstevel@tonic-gate 	if (sip != NULL && SI_FROMUSER(sip))
127*7c478bd9Sstevel@tonic-gate 		(void) pthread_kill(pthread_self(), sig);
128*7c478bd9Sstevel@tonic-gate 	(void) sigfillset(&ucp->uc_sigmask);
129*7c478bd9Sstevel@tonic-gate 	(void) sigdelset(&ucp->uc_sigmask, sig);
130*7c478bd9Sstevel@tonic-gate 	ucp->uc_flags |= UC_SIGMASK;
131*7c478bd9Sstevel@tonic-gate 	(void) setcontext(ucp);
132*7c478bd9Sstevel@tonic-gate }
133*7c478bd9Sstevel@tonic-gate 
134*7c478bd9Sstevel@tonic-gate /*
135*7c478bd9Sstevel@tonic-gate  * Don't want to have more than a couple thread creates outstanding
136*7c478bd9Sstevel@tonic-gate  */
137*7c478bd9Sstevel@tonic-gate static int
138*7c478bd9Sstevel@tonic-gate reserve_new_thread(void)
139*7c478bd9Sstevel@tonic-gate {
140*7c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&thread_lock);
141*7c478bd9Sstevel@tonic-gate 	assert(num_started >= 0);
142*7c478bd9Sstevel@tonic-gate 	if (num_servers > num_started + 1) {
143*7c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_unlock(&thread_lock);
144*7c478bd9Sstevel@tonic-gate 		return (0);
145*7c478bd9Sstevel@tonic-gate 	}
146*7c478bd9Sstevel@tonic-gate 	++num_servers;
147*7c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&thread_lock);
148*7c478bd9Sstevel@tonic-gate 	return (1);
149*7c478bd9Sstevel@tonic-gate }
150*7c478bd9Sstevel@tonic-gate 
151*7c478bd9Sstevel@tonic-gate static void
152*7c478bd9Sstevel@tonic-gate thread_info_free(thread_info_t *ti)
153*7c478bd9Sstevel@tonic-gate {
154*7c478bd9Sstevel@tonic-gate 	uu_list_node_fini(ti, &ti->ti_node, thread_pool);
155*7c478bd9Sstevel@tonic-gate 	if (ti->ti_ucred != NULL)
156*7c478bd9Sstevel@tonic-gate 		uu_free(ti->ti_ucred);
157*7c478bd9Sstevel@tonic-gate 	uu_free(ti);
158*7c478bd9Sstevel@tonic-gate }
159*7c478bd9Sstevel@tonic-gate 
160*7c478bd9Sstevel@tonic-gate static void
161*7c478bd9Sstevel@tonic-gate thread_exiting(void *arg)
162*7c478bd9Sstevel@tonic-gate {
163*7c478bd9Sstevel@tonic-gate 	thread_info_t *ti = arg;
164*7c478bd9Sstevel@tonic-gate 
165*7c478bd9Sstevel@tonic-gate 	log_enter(&ti->ti_log);
166*7c478bd9Sstevel@tonic-gate 
167*7c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&thread_lock);
168*7c478bd9Sstevel@tonic-gate 	if (ti != NULL) {
169*7c478bd9Sstevel@tonic-gate 		num_started--;
170*7c478bd9Sstevel@tonic-gate 		uu_list_remove(thread_list, ti);
171*7c478bd9Sstevel@tonic-gate 	}
172*7c478bd9Sstevel@tonic-gate 	assert(num_servers > 0);
173*7c478bd9Sstevel@tonic-gate 	--num_servers;
174*7c478bd9Sstevel@tonic-gate 
175*7c478bd9Sstevel@tonic-gate 	if (num_servers == 0) {
176*7c478bd9Sstevel@tonic-gate 		configd_critical("no door server threads\n");
177*7c478bd9Sstevel@tonic-gate 		abort();
178*7c478bd9Sstevel@tonic-gate 	}
179*7c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&thread_lock);
180*7c478bd9Sstevel@tonic-gate 
181*7c478bd9Sstevel@tonic-gate 	if (ti != NULL && ti != &main_thread_info)
182*7c478bd9Sstevel@tonic-gate 		thread_info_free(ti);
183*7c478bd9Sstevel@tonic-gate }
184*7c478bd9Sstevel@tonic-gate 
185*7c478bd9Sstevel@tonic-gate void
186*7c478bd9Sstevel@tonic-gate thread_newstate(thread_info_t *ti, thread_state_t newstate)
187*7c478bd9Sstevel@tonic-gate {
188*7c478bd9Sstevel@tonic-gate 	ti->ti_ucred_read = 0;			/* invalidate cached ucred */
189*7c478bd9Sstevel@tonic-gate 	if (newstate != ti->ti_state) {
190*7c478bd9Sstevel@tonic-gate 		ti->ti_prev_state = ti->ti_state;
191*7c478bd9Sstevel@tonic-gate 		ti->ti_state = newstate;
192*7c478bd9Sstevel@tonic-gate 		ti->ti_lastchange = gethrtime();
193*7c478bd9Sstevel@tonic-gate 	}
194*7c478bd9Sstevel@tonic-gate }
195*7c478bd9Sstevel@tonic-gate 
196*7c478bd9Sstevel@tonic-gate thread_info_t *
197*7c478bd9Sstevel@tonic-gate thread_self(void)
198*7c478bd9Sstevel@tonic-gate {
199*7c478bd9Sstevel@tonic-gate 	return (pthread_getspecific(thread_info_key));
200*7c478bd9Sstevel@tonic-gate }
201*7c478bd9Sstevel@tonic-gate 
202*7c478bd9Sstevel@tonic-gate ucred_t *
203*7c478bd9Sstevel@tonic-gate get_ucred(void)
204*7c478bd9Sstevel@tonic-gate {
205*7c478bd9Sstevel@tonic-gate 	thread_info_t *ti = thread_self();
206*7c478bd9Sstevel@tonic-gate 	ucred_t **ret = &ti->ti_ucred;
207*7c478bd9Sstevel@tonic-gate 
208*7c478bd9Sstevel@tonic-gate 	if (ti->ti_ucred_read)
209*7c478bd9Sstevel@tonic-gate 		return (*ret);			/* cached value */
210*7c478bd9Sstevel@tonic-gate 
211*7c478bd9Sstevel@tonic-gate 	if (door_ucred(ret) != 0)
212*7c478bd9Sstevel@tonic-gate 		return (NULL);
213*7c478bd9Sstevel@tonic-gate 	ti->ti_ucred_read = 1;
214*7c478bd9Sstevel@tonic-gate 
215*7c478bd9Sstevel@tonic-gate 	return (*ret);
216*7c478bd9Sstevel@tonic-gate }
217*7c478bd9Sstevel@tonic-gate 
218*7c478bd9Sstevel@tonic-gate int
219*7c478bd9Sstevel@tonic-gate ucred_is_privileged(ucred_t *uc)
220*7c478bd9Sstevel@tonic-gate {
221*7c478bd9Sstevel@tonic-gate 	const priv_set_t *ps;
222*7c478bd9Sstevel@tonic-gate 
223*7c478bd9Sstevel@tonic-gate 	if ((ps = ucred_getprivset(uc, PRIV_EFFECTIVE)) != NULL) {
224*7c478bd9Sstevel@tonic-gate 		if (priv_isfullset(ps))
225*7c478bd9Sstevel@tonic-gate 			return (1);		/* process has all privs */
226*7c478bd9Sstevel@tonic-gate 
227*7c478bd9Sstevel@tonic-gate 		if (privileged_privs != NULL &&
228*7c478bd9Sstevel@tonic-gate 		    priv_issubset(privileged_privs, ps))
229*7c478bd9Sstevel@tonic-gate 			return (1);		/* process has zone privs */
230*7c478bd9Sstevel@tonic-gate 	}
231*7c478bd9Sstevel@tonic-gate 
232*7c478bd9Sstevel@tonic-gate 	return (0);
233*7c478bd9Sstevel@tonic-gate }
234*7c478bd9Sstevel@tonic-gate 
235*7c478bd9Sstevel@tonic-gate static void *
236*7c478bd9Sstevel@tonic-gate thread_start(void *arg)
237*7c478bd9Sstevel@tonic-gate {
238*7c478bd9Sstevel@tonic-gate 	thread_info_t *ti = arg;
239*7c478bd9Sstevel@tonic-gate 
240*7c478bd9Sstevel@tonic-gate 	(void) pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
241*7c478bd9Sstevel@tonic-gate 
242*7c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&thread_lock);
243*7c478bd9Sstevel@tonic-gate 	num_started++;
244*7c478bd9Sstevel@tonic-gate 	(void) uu_list_insert_after(thread_list, uu_list_last(thread_list),
245*7c478bd9Sstevel@tonic-gate 	    ti);
246*7c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&thread_lock);
247*7c478bd9Sstevel@tonic-gate 	(void) pthread_setspecific(thread_info_key, ti);
248*7c478bd9Sstevel@tonic-gate 
249*7c478bd9Sstevel@tonic-gate 	thread_newstate(ti, TI_DOOR_RETURN);
250*7c478bd9Sstevel@tonic-gate 
251*7c478bd9Sstevel@tonic-gate 	/*
252*7c478bd9Sstevel@tonic-gate 	 * Start handling door calls
253*7c478bd9Sstevel@tonic-gate 	 */
254*7c478bd9Sstevel@tonic-gate 	(void) door_return(NULL, 0, NULL, 0);
255*7c478bd9Sstevel@tonic-gate 	return (arg);
256*7c478bd9Sstevel@tonic-gate }
257*7c478bd9Sstevel@tonic-gate 
258*7c478bd9Sstevel@tonic-gate static void
259*7c478bd9Sstevel@tonic-gate new_thread_needed(door_info_t *dip)
260*7c478bd9Sstevel@tonic-gate {
261*7c478bd9Sstevel@tonic-gate 	thread_info_t *ti;
262*7c478bd9Sstevel@tonic-gate 
263*7c478bd9Sstevel@tonic-gate 	sigset_t new, old;
264*7c478bd9Sstevel@tonic-gate 
265*7c478bd9Sstevel@tonic-gate 	assert(dip == NULL);
266*7c478bd9Sstevel@tonic-gate 
267*7c478bd9Sstevel@tonic-gate 	if (!reserve_new_thread())
268*7c478bd9Sstevel@tonic-gate 		return;
269*7c478bd9Sstevel@tonic-gate 
270*7c478bd9Sstevel@tonic-gate 	if ((ti = uu_zalloc(sizeof (*ti))) == NULL)
271*7c478bd9Sstevel@tonic-gate 		goto fail;
272*7c478bd9Sstevel@tonic-gate 
273*7c478bd9Sstevel@tonic-gate 	uu_list_node_init(ti, &ti->ti_node, thread_pool);
274*7c478bd9Sstevel@tonic-gate 	ti->ti_state = TI_CREATED;
275*7c478bd9Sstevel@tonic-gate 	ti->ti_prev_state = TI_CREATED;
276*7c478bd9Sstevel@tonic-gate 
277*7c478bd9Sstevel@tonic-gate 	if ((ti->ti_ucred = uu_zalloc(ucred_size())) == NULL)
278*7c478bd9Sstevel@tonic-gate 		goto fail;
279*7c478bd9Sstevel@tonic-gate 
280*7c478bd9Sstevel@tonic-gate 	(void) sigfillset(&new);
281*7c478bd9Sstevel@tonic-gate 	(void) pthread_sigmask(SIG_SETMASK, &new, &old);
282*7c478bd9Sstevel@tonic-gate 	if ((errno = pthread_create(&ti->ti_thread, &thread_attr, thread_start,
283*7c478bd9Sstevel@tonic-gate 	    ti)) != 0) {
284*7c478bd9Sstevel@tonic-gate 		(void) pthread_sigmask(SIG_SETMASK, &old, NULL);
285*7c478bd9Sstevel@tonic-gate 		goto fail;
286*7c478bd9Sstevel@tonic-gate 	}
287*7c478bd9Sstevel@tonic-gate 
288*7c478bd9Sstevel@tonic-gate 	(void) pthread_sigmask(SIG_SETMASK, &old, NULL);
289*7c478bd9Sstevel@tonic-gate 	return;
290*7c478bd9Sstevel@tonic-gate 
291*7c478bd9Sstevel@tonic-gate fail:
292*7c478bd9Sstevel@tonic-gate 	/*
293*7c478bd9Sstevel@tonic-gate 	 * Since the thread_info structure was never linked onto the
294*7c478bd9Sstevel@tonic-gate 	 * thread list, thread_exiting() can't handle the cleanup.
295*7c478bd9Sstevel@tonic-gate 	 */
296*7c478bd9Sstevel@tonic-gate 	thread_exiting(NULL);
297*7c478bd9Sstevel@tonic-gate 	if (ti != NULL)
298*7c478bd9Sstevel@tonic-gate 		thread_info_free(ti);
299*7c478bd9Sstevel@tonic-gate }
300*7c478bd9Sstevel@tonic-gate 
301*7c478bd9Sstevel@tonic-gate int
302*7c478bd9Sstevel@tonic-gate create_connection(ucred_t *uc, repository_door_request_t *rp,
303*7c478bd9Sstevel@tonic-gate     size_t rp_size, int *out_fd)
304*7c478bd9Sstevel@tonic-gate {
305*7c478bd9Sstevel@tonic-gate 	int flags;
306*7c478bd9Sstevel@tonic-gate 	int privileged = 0;
307*7c478bd9Sstevel@tonic-gate 	uint32_t debugflags = 0;
308*7c478bd9Sstevel@tonic-gate 	psinfo_t info;
309*7c478bd9Sstevel@tonic-gate 
310*7c478bd9Sstevel@tonic-gate 	if (privileged_pid != 0) {
311*7c478bd9Sstevel@tonic-gate 		/*
312*7c478bd9Sstevel@tonic-gate 		 * in privileged pid mode, we only allow connections from
313*7c478bd9Sstevel@tonic-gate 		 * our original parent -- the psinfo read verifies that
314*7c478bd9Sstevel@tonic-gate 		 * it is the same process which we started with.
315*7c478bd9Sstevel@tonic-gate 		 */
316*7c478bd9Sstevel@tonic-gate 		if (ucred_getpid(uc) != privileged_pid ||
317*7c478bd9Sstevel@tonic-gate 		    read(privileged_psinfo_fd, &info, sizeof (info)) !=
318*7c478bd9Sstevel@tonic-gate 		    sizeof (info))
319*7c478bd9Sstevel@tonic-gate 			return (REPOSITORY_DOOR_FAIL_PERMISSION_DENIED);
320*7c478bd9Sstevel@tonic-gate 
321*7c478bd9Sstevel@tonic-gate 		privileged = 1;			/* he gets full privileges */
322*7c478bd9Sstevel@tonic-gate 	} else if (privileged_user != 0) {
323*7c478bd9Sstevel@tonic-gate 		/*
324*7c478bd9Sstevel@tonic-gate 		 * in privileged user mode, only one particular user is
325*7c478bd9Sstevel@tonic-gate 		 * allowed to connect to us, and he can do anything.
326*7c478bd9Sstevel@tonic-gate 		 */
327*7c478bd9Sstevel@tonic-gate 		if (ucred_geteuid(uc) != privileged_user)
328*7c478bd9Sstevel@tonic-gate 			return (REPOSITORY_DOOR_FAIL_PERMISSION_DENIED);
329*7c478bd9Sstevel@tonic-gate 
330*7c478bd9Sstevel@tonic-gate 		privileged = 1;
331*7c478bd9Sstevel@tonic-gate 	}
332*7c478bd9Sstevel@tonic-gate 
333*7c478bd9Sstevel@tonic-gate 	/*
334*7c478bd9Sstevel@tonic-gate 	 * Check that rp, of size rp_size, is large enough to
335*7c478bd9Sstevel@tonic-gate 	 * contain field 'f'.  If so, write the value into *out, and return 1.
336*7c478bd9Sstevel@tonic-gate 	 * Otherwise, return 0.
337*7c478bd9Sstevel@tonic-gate 	 */
338*7c478bd9Sstevel@tonic-gate #define	GET_ARG(rp, rp_size, f, out)					\
339*7c478bd9Sstevel@tonic-gate 	(((rp_size) >= offsetofend(repository_door_request_t, f)) ?	\
340*7c478bd9Sstevel@tonic-gate 	    ((*(out) = (rp)->f), 1) : 0)
341*7c478bd9Sstevel@tonic-gate 
342*7c478bd9Sstevel@tonic-gate 	if (!GET_ARG(rp, rp_size, rdr_flags, &flags))
343*7c478bd9Sstevel@tonic-gate 		return (REPOSITORY_DOOR_FAIL_BAD_REQUEST);
344*7c478bd9Sstevel@tonic-gate 
345*7c478bd9Sstevel@tonic-gate #if (REPOSITORY_DOOR_FLAG_ALL != REPOSITORY_DOOR_FLAG_DEBUG)
346*7c478bd9Sstevel@tonic-gate #error Need to update flag checks
347*7c478bd9Sstevel@tonic-gate #endif
348*7c478bd9Sstevel@tonic-gate 
349*7c478bd9Sstevel@tonic-gate 	if (flags & ~REPOSITORY_DOOR_FLAG_ALL)
350*7c478bd9Sstevel@tonic-gate 		return (REPOSITORY_DOOR_FAIL_BAD_FLAG);
351*7c478bd9Sstevel@tonic-gate 
352*7c478bd9Sstevel@tonic-gate 	if (flags & REPOSITORY_DOOR_FLAG_DEBUG)
353*7c478bd9Sstevel@tonic-gate 		if (!GET_ARG(rp, rp_size, rdr_debug, &debugflags))
354*7c478bd9Sstevel@tonic-gate 			return (REPOSITORY_DOOR_FAIL_BAD_REQUEST);
355*7c478bd9Sstevel@tonic-gate #undef GET_ARG
356*7c478bd9Sstevel@tonic-gate 
357*7c478bd9Sstevel@tonic-gate 	return (create_client(ucred_getpid(uc), debugflags, privileged,
358*7c478bd9Sstevel@tonic-gate 	    out_fd));
359*7c478bd9Sstevel@tonic-gate }
360*7c478bd9Sstevel@tonic-gate 
361*7c478bd9Sstevel@tonic-gate void
362*7c478bd9Sstevel@tonic-gate configd_vcritical(const char *message, va_list args)
363*7c478bd9Sstevel@tonic-gate {
364*7c478bd9Sstevel@tonic-gate 	if (log_to_syslog)
365*7c478bd9Sstevel@tonic-gate 		vsyslog(LOG_CRIT, message, args);
366*7c478bd9Sstevel@tonic-gate 	else {
367*7c478bd9Sstevel@tonic-gate 		flockfile(stderr);
368*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "svc.configd: Fatal error: ");
369*7c478bd9Sstevel@tonic-gate 		(void) vfprintf(stderr, message, args);
370*7c478bd9Sstevel@tonic-gate 		if (message[0] == 0 || message[strlen(message) - 1] != '\n')
371*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "\n");
372*7c478bd9Sstevel@tonic-gate 		funlockfile(stderr);
373*7c478bd9Sstevel@tonic-gate 	}
374*7c478bd9Sstevel@tonic-gate }
375*7c478bd9Sstevel@tonic-gate 
376*7c478bd9Sstevel@tonic-gate void
377*7c478bd9Sstevel@tonic-gate configd_critical(const char *message, ...)
378*7c478bd9Sstevel@tonic-gate {
379*7c478bd9Sstevel@tonic-gate 	va_list args;
380*7c478bd9Sstevel@tonic-gate 	va_start(args, message);
381*7c478bd9Sstevel@tonic-gate 	configd_vcritical(message, args);
382*7c478bd9Sstevel@tonic-gate 	va_end(args);
383*7c478bd9Sstevel@tonic-gate }
384*7c478bd9Sstevel@tonic-gate 
385*7c478bd9Sstevel@tonic-gate static void
386*7c478bd9Sstevel@tonic-gate usage(const char *prog, int ret)
387*7c478bd9Sstevel@tonic-gate {
388*7c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
389*7c478bd9Sstevel@tonic-gate 	    "usage: %s [-np] [-d door_path] [-r repository_path]\n"
390*7c478bd9Sstevel@tonic-gate 	    "    [-t nonpersist_repository]\n", prog);
391*7c478bd9Sstevel@tonic-gate 	exit(ret);
392*7c478bd9Sstevel@tonic-gate }
393*7c478bd9Sstevel@tonic-gate 
394*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
395*7c478bd9Sstevel@tonic-gate static void
396*7c478bd9Sstevel@tonic-gate handler(int sig, siginfo_t *info, void *data)
397*7c478bd9Sstevel@tonic-gate {
398*7c478bd9Sstevel@tonic-gate 	finished = 1;
399*7c478bd9Sstevel@tonic-gate }
400*7c478bd9Sstevel@tonic-gate 
401*7c478bd9Sstevel@tonic-gate static int pipe_fd = -1;
402*7c478bd9Sstevel@tonic-gate 
403*7c478bd9Sstevel@tonic-gate static int
404*7c478bd9Sstevel@tonic-gate daemonize_start(void)
405*7c478bd9Sstevel@tonic-gate {
406*7c478bd9Sstevel@tonic-gate 	char data;
407*7c478bd9Sstevel@tonic-gate 	int status;
408*7c478bd9Sstevel@tonic-gate 
409*7c478bd9Sstevel@tonic-gate 	int filedes[2];
410*7c478bd9Sstevel@tonic-gate 	pid_t pid;
411*7c478bd9Sstevel@tonic-gate 
412*7c478bd9Sstevel@tonic-gate 	(void) close(0);
413*7c478bd9Sstevel@tonic-gate 	(void) dup2(2, 1);		/* stderr only */
414*7c478bd9Sstevel@tonic-gate 
415*7c478bd9Sstevel@tonic-gate 	if (pipe(filedes) < 0)
416*7c478bd9Sstevel@tonic-gate 		return (-1);
417*7c478bd9Sstevel@tonic-gate 
418*7c478bd9Sstevel@tonic-gate 	if ((pid = fork1()) < 0)
419*7c478bd9Sstevel@tonic-gate 		return (-1);
420*7c478bd9Sstevel@tonic-gate 
421*7c478bd9Sstevel@tonic-gate 	if (pid != 0) {
422*7c478bd9Sstevel@tonic-gate 		/*
423*7c478bd9Sstevel@tonic-gate 		 * parent
424*7c478bd9Sstevel@tonic-gate 		 */
425*7c478bd9Sstevel@tonic-gate 		struct sigaction act;
426*7c478bd9Sstevel@tonic-gate 
427*7c478bd9Sstevel@tonic-gate 		act.sa_sigaction = SIG_DFL;
428*7c478bd9Sstevel@tonic-gate 		(void) sigemptyset(&act.sa_mask);
429*7c478bd9Sstevel@tonic-gate 		act.sa_flags = 0;
430*7c478bd9Sstevel@tonic-gate 
431*7c478bd9Sstevel@tonic-gate 		(void) sigaction(SIGPIPE, &act, NULL);	/* ignore SIGPIPE */
432*7c478bd9Sstevel@tonic-gate 
433*7c478bd9Sstevel@tonic-gate 		(void) close(filedes[1]);
434*7c478bd9Sstevel@tonic-gate 		if (read(filedes[0], &data, 1) == 1) {
435*7c478bd9Sstevel@tonic-gate 			/* presume success */
436*7c478bd9Sstevel@tonic-gate 			_exit(CONFIGD_EXIT_OKAY);
437*7c478bd9Sstevel@tonic-gate 		}
438*7c478bd9Sstevel@tonic-gate 
439*7c478bd9Sstevel@tonic-gate 		status = -1;
440*7c478bd9Sstevel@tonic-gate 		(void) wait4(pid, &status, 0, NULL);
441*7c478bd9Sstevel@tonic-gate 		if (WIFEXITED(status))
442*7c478bd9Sstevel@tonic-gate 			_exit(WEXITSTATUS(status));
443*7c478bd9Sstevel@tonic-gate 		else
444*7c478bd9Sstevel@tonic-gate 			_exit(-1);
445*7c478bd9Sstevel@tonic-gate 	}
446*7c478bd9Sstevel@tonic-gate 
447*7c478bd9Sstevel@tonic-gate 	/*
448*7c478bd9Sstevel@tonic-gate 	 * child
449*7c478bd9Sstevel@tonic-gate 	 */
450*7c478bd9Sstevel@tonic-gate 	pipe_fd = filedes[1];
451*7c478bd9Sstevel@tonic-gate 	(void) close(filedes[0]);
452*7c478bd9Sstevel@tonic-gate 
453*7c478bd9Sstevel@tonic-gate 	/*
454*7c478bd9Sstevel@tonic-gate 	 * generic Unix setup
455*7c478bd9Sstevel@tonic-gate 	 */
456*7c478bd9Sstevel@tonic-gate 	(void) setsid();
457*7c478bd9Sstevel@tonic-gate 	(void) umask(0077);
458*7c478bd9Sstevel@tonic-gate 
459*7c478bd9Sstevel@tonic-gate 	return (0);
460*7c478bd9Sstevel@tonic-gate }
461*7c478bd9Sstevel@tonic-gate 
462*7c478bd9Sstevel@tonic-gate static void
463*7c478bd9Sstevel@tonic-gate daemonize_ready(void)
464*7c478bd9Sstevel@tonic-gate {
465*7c478bd9Sstevel@tonic-gate 	char data = '\0';
466*7c478bd9Sstevel@tonic-gate 
467*7c478bd9Sstevel@tonic-gate 	/*
468*7c478bd9Sstevel@tonic-gate 	 * wake the parent
469*7c478bd9Sstevel@tonic-gate 	 */
470*7c478bd9Sstevel@tonic-gate 	(void) write(pipe_fd, &data, 1);
471*7c478bd9Sstevel@tonic-gate 	(void) close(pipe_fd);
472*7c478bd9Sstevel@tonic-gate }
473*7c478bd9Sstevel@tonic-gate 
474*7c478bd9Sstevel@tonic-gate int
475*7c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
476*7c478bd9Sstevel@tonic-gate {
477*7c478bd9Sstevel@tonic-gate 	thread_info_t *ti = &main_thread_info;
478*7c478bd9Sstevel@tonic-gate 
479*7c478bd9Sstevel@tonic-gate 	char pidpath[sizeof ("/proc/" "/psinfo") + 10];
480*7c478bd9Sstevel@tonic-gate 
481*7c478bd9Sstevel@tonic-gate 	struct rlimit fd_new;
482*7c478bd9Sstevel@tonic-gate 
483*7c478bd9Sstevel@tonic-gate 	const char *endptr;
484*7c478bd9Sstevel@tonic-gate 	sigset_t myset;
485*7c478bd9Sstevel@tonic-gate 	int c;
486*7c478bd9Sstevel@tonic-gate 	int ret;
487*7c478bd9Sstevel@tonic-gate 	int fd;
488*7c478bd9Sstevel@tonic-gate 	const char *dbpath = NULL;
489*7c478bd9Sstevel@tonic-gate 	const char *npdbpath = NULL;
490*7c478bd9Sstevel@tonic-gate 	const char *doorpath = REPOSITORY_DOOR_NAME;
491*7c478bd9Sstevel@tonic-gate 	struct sigaction act;
492*7c478bd9Sstevel@tonic-gate 
493*7c478bd9Sstevel@tonic-gate 	int daemonize = 1;		/* default to daemonizing */
494*7c478bd9Sstevel@tonic-gate 	int have_npdb = 1;
495*7c478bd9Sstevel@tonic-gate 
496*7c478bd9Sstevel@tonic-gate 	closefrom(3);			/* get rid of extraneous fds */
497*7c478bd9Sstevel@tonic-gate 
498*7c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "Dnpd:r:t:")) != -1) {
499*7c478bd9Sstevel@tonic-gate 		switch (c) {
500*7c478bd9Sstevel@tonic-gate 		case 'n':
501*7c478bd9Sstevel@tonic-gate 			daemonize = 0;
502*7c478bd9Sstevel@tonic-gate 			break;
503*7c478bd9Sstevel@tonic-gate 		case 'd':
504*7c478bd9Sstevel@tonic-gate 			doorpath = optarg;
505*7c478bd9Sstevel@tonic-gate 			is_main_repository = 0;
506*7c478bd9Sstevel@tonic-gate 			have_npdb = 0;		/* default to no non-persist */
507*7c478bd9Sstevel@tonic-gate 			break;
508*7c478bd9Sstevel@tonic-gate 		case 'p':
509*7c478bd9Sstevel@tonic-gate 			log_to_syslog = 0;	/* don't use syslog */
510*7c478bd9Sstevel@tonic-gate 			is_main_repository = 0;
511*7c478bd9Sstevel@tonic-gate 
512*7c478bd9Sstevel@tonic-gate 			/*
513*7c478bd9Sstevel@tonic-gate 			 * If our parent exits while we're opening its /proc
514*7c478bd9Sstevel@tonic-gate 			 * psinfo, we're vulnerable to a pid wrapping.  To
515*7c478bd9Sstevel@tonic-gate 			 * protect against that, re-check our ppid after
516*7c478bd9Sstevel@tonic-gate 			 * opening it.
517*7c478bd9Sstevel@tonic-gate 			 */
518*7c478bd9Sstevel@tonic-gate 			privileged_pid = getppid();
519*7c478bd9Sstevel@tonic-gate 			(void) snprintf(pidpath, sizeof (pidpath),
520*7c478bd9Sstevel@tonic-gate 			    "/proc/%d/psinfo", privileged_pid);
521*7c478bd9Sstevel@tonic-gate 			if ((fd = open(pidpath, O_RDONLY)) < 0 ||
522*7c478bd9Sstevel@tonic-gate 			    getppid() != privileged_pid) {
523*7c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
524*7c478bd9Sstevel@tonic-gate 				    "%s: unable to get parent info\n", argv[0]);
525*7c478bd9Sstevel@tonic-gate 				exit(CONFIGD_EXIT_BAD_ARGS);
526*7c478bd9Sstevel@tonic-gate 			}
527*7c478bd9Sstevel@tonic-gate 			privileged_psinfo_fd = fd;
528*7c478bd9Sstevel@tonic-gate 			break;
529*7c478bd9Sstevel@tonic-gate 		case 'r':
530*7c478bd9Sstevel@tonic-gate 			dbpath = optarg;
531*7c478bd9Sstevel@tonic-gate 			is_main_repository = 0;
532*7c478bd9Sstevel@tonic-gate 			break;
533*7c478bd9Sstevel@tonic-gate 		case 't':
534*7c478bd9Sstevel@tonic-gate 			npdbpath = optarg;
535*7c478bd9Sstevel@tonic-gate 			is_main_repository = 0;
536*7c478bd9Sstevel@tonic-gate 			break;
537*7c478bd9Sstevel@tonic-gate 		default:
538*7c478bd9Sstevel@tonic-gate 			usage(argv[0], CONFIGD_EXIT_BAD_ARGS);
539*7c478bd9Sstevel@tonic-gate 			break;
540*7c478bd9Sstevel@tonic-gate 		}
541*7c478bd9Sstevel@tonic-gate 	}
542*7c478bd9Sstevel@tonic-gate 
543*7c478bd9Sstevel@tonic-gate 	/*
544*7c478bd9Sstevel@tonic-gate 	 * If we're not running as root, allow our euid full access, and
545*7c478bd9Sstevel@tonic-gate 	 * everyone else no access.
546*7c478bd9Sstevel@tonic-gate 	 */
547*7c478bd9Sstevel@tonic-gate 	if (privileged_pid == 0 && geteuid() != 0) {
548*7c478bd9Sstevel@tonic-gate 		privileged_user = geteuid();
549*7c478bd9Sstevel@tonic-gate 	}
550*7c478bd9Sstevel@tonic-gate 
551*7c478bd9Sstevel@tonic-gate 	privileged_privs = priv_str_to_set("zone", "", &endptr);
552*7c478bd9Sstevel@tonic-gate 	if (endptr != NULL && privileged_privs != NULL) {
553*7c478bd9Sstevel@tonic-gate 		priv_freeset(privileged_privs);
554*7c478bd9Sstevel@tonic-gate 		privileged_privs = NULL;
555*7c478bd9Sstevel@tonic-gate 	}
556*7c478bd9Sstevel@tonic-gate 
557*7c478bd9Sstevel@tonic-gate 	openlog("svc.configd", LOG_PID | LOG_CONS, LOG_DAEMON);
558*7c478bd9Sstevel@tonic-gate 	(void) setlogmask(LOG_UPTO(LOG_NOTICE));
559*7c478bd9Sstevel@tonic-gate 
560*7c478bd9Sstevel@tonic-gate 	/*
561*7c478bd9Sstevel@tonic-gate 	 * if a non-persist db is specified, always enable it
562*7c478bd9Sstevel@tonic-gate 	 */
563*7c478bd9Sstevel@tonic-gate 	if (npdbpath)
564*7c478bd9Sstevel@tonic-gate 		have_npdb = 1;
565*7c478bd9Sstevel@tonic-gate 
566*7c478bd9Sstevel@tonic-gate 	if (optind != argc)
567*7c478bd9Sstevel@tonic-gate 		usage(argv[0], CONFIGD_EXIT_BAD_ARGS);
568*7c478bd9Sstevel@tonic-gate 
569*7c478bd9Sstevel@tonic-gate 	if (daemonize) {
570*7c478bd9Sstevel@tonic-gate 		if (getuid() == 0)
571*7c478bd9Sstevel@tonic-gate 			(void) chdir("/");
572*7c478bd9Sstevel@tonic-gate 		if (daemonize_start() < 0) {
573*7c478bd9Sstevel@tonic-gate 			(void) perror("unable to daemonize");
574*7c478bd9Sstevel@tonic-gate 			exit(CONFIGD_EXIT_INIT_FAILED);
575*7c478bd9Sstevel@tonic-gate 		}
576*7c478bd9Sstevel@tonic-gate 	}
577*7c478bd9Sstevel@tonic-gate 	if (getuid() == 0)
578*7c478bd9Sstevel@tonic-gate 		(void) core_set_process_path(CONFIGD_CORE,
579*7c478bd9Sstevel@tonic-gate 		    strlen(CONFIGD_CORE) + 1, getpid());
580*7c478bd9Sstevel@tonic-gate 
581*7c478bd9Sstevel@tonic-gate 	/*
582*7c478bd9Sstevel@tonic-gate 	 * this should be enabled once we can drop privileges and still get
583*7c478bd9Sstevel@tonic-gate 	 * a core dump.
584*7c478bd9Sstevel@tonic-gate 	 */
585*7c478bd9Sstevel@tonic-gate #if 0
586*7c478bd9Sstevel@tonic-gate 	/* turn off basic privileges we do not need */
587*7c478bd9Sstevel@tonic-gate 	(void) priv_set(PRIV_OFF, PRIV_PERMITTED, PRIV_FILE_LINK_ANY,
588*7c478bd9Sstevel@tonic-gate 	    PRIV_PROC_EXEC, PRIV_PROC_FORK, PRIV_PROC_SESSION, NULL);
589*7c478bd9Sstevel@tonic-gate #endif
590*7c478bd9Sstevel@tonic-gate 
591*7c478bd9Sstevel@tonic-gate 	/* not that we can exec, but to be safe, shut them all off... */
592*7c478bd9Sstevel@tonic-gate 	(void) priv_set(PRIV_SET, PRIV_INHERITABLE, NULL);
593*7c478bd9Sstevel@tonic-gate 
594*7c478bd9Sstevel@tonic-gate 	(void) sigfillset(&act.sa_mask);
595*7c478bd9Sstevel@tonic-gate 
596*7c478bd9Sstevel@tonic-gate 	/* signals to ignore */
597*7c478bd9Sstevel@tonic-gate 	act.sa_sigaction = SIG_IGN;
598*7c478bd9Sstevel@tonic-gate 	act.sa_flags = 0;
599*7c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGPIPE, &act, NULL);
600*7c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGALRM, &act, NULL);
601*7c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGUSR1, &act, NULL);
602*7c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGUSR2, &act, NULL);
603*7c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGPOLL, &act, NULL);
604*7c478bd9Sstevel@tonic-gate 
605*7c478bd9Sstevel@tonic-gate 	/* signals to abort on */
606*7c478bd9Sstevel@tonic-gate 	act.sa_sigaction = (void (*)(int, siginfo_t *, void *))&abort_handler;
607*7c478bd9Sstevel@tonic-gate 	act.sa_flags = SA_SIGINFO;
608*7c478bd9Sstevel@tonic-gate 
609*7c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGABRT, &act, NULL);
610*7c478bd9Sstevel@tonic-gate 
611*7c478bd9Sstevel@tonic-gate 	/* signals to handle */
612*7c478bd9Sstevel@tonic-gate 	act.sa_sigaction = &handler;
613*7c478bd9Sstevel@tonic-gate 	act.sa_flags = SA_SIGINFO;
614*7c478bd9Sstevel@tonic-gate 
615*7c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGHUP, &act, NULL);
616*7c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGINT, &act, NULL);
617*7c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGTERM, &act, NULL);
618*7c478bd9Sstevel@tonic-gate 
619*7c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&myset);
620*7c478bd9Sstevel@tonic-gate 	(void) sigaddset(&myset, SIGHUP);
621*7c478bd9Sstevel@tonic-gate 	(void) sigaddset(&myset, SIGINT);
622*7c478bd9Sstevel@tonic-gate 	(void) sigaddset(&myset, SIGTERM);
623*7c478bd9Sstevel@tonic-gate 
624*7c478bd9Sstevel@tonic-gate 	if ((errno = pthread_attr_init(&thread_attr)) != 0) {
625*7c478bd9Sstevel@tonic-gate 		(void) perror("initializing");
626*7c478bd9Sstevel@tonic-gate 		exit(CONFIGD_EXIT_INIT_FAILED);
627*7c478bd9Sstevel@tonic-gate 	}
628*7c478bd9Sstevel@tonic-gate 
629*7c478bd9Sstevel@tonic-gate 	/*
630*7c478bd9Sstevel@tonic-gate 	 * Set the hard and soft limits to CONFIGD_MAX_FDS.
631*7c478bd9Sstevel@tonic-gate 	 */
632*7c478bd9Sstevel@tonic-gate 	fd_new.rlim_max = fd_new.rlim_cur = CONFIGD_MAX_FDS;
633*7c478bd9Sstevel@tonic-gate 	(void) setrlimit(RLIMIT_NOFILE, &fd_new);
634*7c478bd9Sstevel@tonic-gate 
635*7c478bd9Sstevel@tonic-gate 	if ((ret = backend_init(dbpath, npdbpath, have_npdb)) !=
636*7c478bd9Sstevel@tonic-gate 	    CONFIGD_EXIT_OKAY)
637*7c478bd9Sstevel@tonic-gate 		exit(ret);
638*7c478bd9Sstevel@tonic-gate 
639*7c478bd9Sstevel@tonic-gate 	if (!client_init())
640*7c478bd9Sstevel@tonic-gate 		exit(CONFIGD_EXIT_INIT_FAILED);
641*7c478bd9Sstevel@tonic-gate 
642*7c478bd9Sstevel@tonic-gate 	if (!rc_node_init())
643*7c478bd9Sstevel@tonic-gate 		exit(CONFIGD_EXIT_INIT_FAILED);
644*7c478bd9Sstevel@tonic-gate 
645*7c478bd9Sstevel@tonic-gate 	(void) pthread_attr_setdetachstate(&thread_attr,
646*7c478bd9Sstevel@tonic-gate 	    PTHREAD_CREATE_DETACHED);
647*7c478bd9Sstevel@tonic-gate 	(void) pthread_attr_setscope(&thread_attr, PTHREAD_SCOPE_SYSTEM);
648*7c478bd9Sstevel@tonic-gate 
649*7c478bd9Sstevel@tonic-gate 	if ((errno = pthread_key_create(&thread_info_key,
650*7c478bd9Sstevel@tonic-gate 	    thread_exiting)) != 0) {
651*7c478bd9Sstevel@tonic-gate 		perror("pthread_key_create");
652*7c478bd9Sstevel@tonic-gate 		exit(CONFIGD_EXIT_INIT_FAILED);
653*7c478bd9Sstevel@tonic-gate 	}
654*7c478bd9Sstevel@tonic-gate 
655*7c478bd9Sstevel@tonic-gate 	if ((thread_pool = uu_list_pool_create("thread_pool",
656*7c478bd9Sstevel@tonic-gate 	    sizeof (thread_info_t), offsetof(thread_info_t, ti_node),
657*7c478bd9Sstevel@tonic-gate 	    NULL, UU_LIST_POOL_DEBUG)) == NULL) {
658*7c478bd9Sstevel@tonic-gate 		configd_critical("uu_list_pool_create: %s\n",
659*7c478bd9Sstevel@tonic-gate 		    uu_strerror(uu_error()));
660*7c478bd9Sstevel@tonic-gate 		exit(CONFIGD_EXIT_INIT_FAILED);
661*7c478bd9Sstevel@tonic-gate 	}
662*7c478bd9Sstevel@tonic-gate 
663*7c478bd9Sstevel@tonic-gate 	if ((thread_list = uu_list_create(thread_pool, NULL, 0)) == NULL) {
664*7c478bd9Sstevel@tonic-gate 		configd_critical("uu_list_create: %s\n",
665*7c478bd9Sstevel@tonic-gate 		    uu_strerror(uu_error()));
666*7c478bd9Sstevel@tonic-gate 		exit(CONFIGD_EXIT_INIT_FAILED);
667*7c478bd9Sstevel@tonic-gate 	}
668*7c478bd9Sstevel@tonic-gate 
669*7c478bd9Sstevel@tonic-gate 	(void) memset(ti, '\0', sizeof (*ti));
670*7c478bd9Sstevel@tonic-gate 	uu_list_node_init(ti, &ti->ti_node, thread_pool);
671*7c478bd9Sstevel@tonic-gate 	(void) uu_list_insert_before(thread_list, uu_list_first(thread_list),
672*7c478bd9Sstevel@tonic-gate 	    ti);
673*7c478bd9Sstevel@tonic-gate 
674*7c478bd9Sstevel@tonic-gate 	ti->ti_thread = pthread_self();
675*7c478bd9Sstevel@tonic-gate 	ti->ti_state = TI_SIGNAL_WAIT;
676*7c478bd9Sstevel@tonic-gate 	ti->ti_prev_state = TI_SIGNAL_WAIT;
677*7c478bd9Sstevel@tonic-gate 
678*7c478bd9Sstevel@tonic-gate 	(void) pthread_setspecific(thread_info_key, ti);
679*7c478bd9Sstevel@tonic-gate 
680*7c478bd9Sstevel@tonic-gate 	(void) door_server_create(new_thread_needed);
681*7c478bd9Sstevel@tonic-gate 
682*7c478bd9Sstevel@tonic-gate 	if (!setup_main_door(doorpath)) {
683*7c478bd9Sstevel@tonic-gate 		configd_critical("Setting up main door failed.\n");
684*7c478bd9Sstevel@tonic-gate 		exit(CONFIGD_EXIT_DOOR_INIT_FAILED);
685*7c478bd9Sstevel@tonic-gate 	}
686*7c478bd9Sstevel@tonic-gate 
687*7c478bd9Sstevel@tonic-gate 	if (daemonize)
688*7c478bd9Sstevel@tonic-gate 		daemonize_ready();
689*7c478bd9Sstevel@tonic-gate 
690*7c478bd9Sstevel@tonic-gate 	(void) pthread_sigmask(SIG_BLOCK, &myset, NULL);
691*7c478bd9Sstevel@tonic-gate 	while (!finished) {
692*7c478bd9Sstevel@tonic-gate 		int sig = sigwait(&myset);
693*7c478bd9Sstevel@tonic-gate 		if (sig > 0) {
694*7c478bd9Sstevel@tonic-gate 			break;
695*7c478bd9Sstevel@tonic-gate 		}
696*7c478bd9Sstevel@tonic-gate 	}
697*7c478bd9Sstevel@tonic-gate 
698*7c478bd9Sstevel@tonic-gate 	backend_fini();
699*7c478bd9Sstevel@tonic-gate 
700*7c478bd9Sstevel@tonic-gate 	return (CONFIGD_EXIT_OKAY);
701*7c478bd9Sstevel@tonic-gate }
702