xref: /titanic_50/usr/src/cmd/svc/configd/configd.c (revision 8918dff3e162b85faa068f02fc6f2ca350150e71)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
23*8918dff3Sjwadams  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include <assert.h>
307c478bd9Sstevel@tonic-gate #include <door.h>
317c478bd9Sstevel@tonic-gate #include <errno.h>
327c478bd9Sstevel@tonic-gate #include <fcntl.h>
33*8918dff3Sjwadams #include <limits.h>
347c478bd9Sstevel@tonic-gate #include <priv.h>
357c478bd9Sstevel@tonic-gate #include <procfs.h>
367c478bd9Sstevel@tonic-gate #include <pthread.h>
377c478bd9Sstevel@tonic-gate #include <signal.h>
387c478bd9Sstevel@tonic-gate #include <stdarg.h>
397c478bd9Sstevel@tonic-gate #include <stdio.h>
407c478bd9Sstevel@tonic-gate #include <stdlib.h>
417c478bd9Sstevel@tonic-gate #include <string.h>
427c478bd9Sstevel@tonic-gate #include <syslog.h>
437c478bd9Sstevel@tonic-gate #include <sys/corectl.h>
447c478bd9Sstevel@tonic-gate #include <sys/resource.h>
457c478bd9Sstevel@tonic-gate #include <sys/stat.h>
467c478bd9Sstevel@tonic-gate #include <sys/wait.h>
477c478bd9Sstevel@tonic-gate #include <ucontext.h>
487c478bd9Sstevel@tonic-gate #include <unistd.h>
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate #include "configd.h"
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate /*
537c478bd9Sstevel@tonic-gate  * This file manages the overall startup and shutdown of configd, as well
547c478bd9Sstevel@tonic-gate  * as managing its door thread pool and per-thread datastructures.
557c478bd9Sstevel@tonic-gate  *
567c478bd9Sstevel@tonic-gate  * 1.  Per-thread Datastructures
577c478bd9Sstevel@tonic-gate  * -----------------------------
587c478bd9Sstevel@tonic-gate  * Each configd thread has an associated thread_info_t which contains its
597c478bd9Sstevel@tonic-gate  * current state.  A pointer is kept to this in TSD, keyed by thread_info_key.
607c478bd9Sstevel@tonic-gate  * The thread_info_ts for all threads in configd are kept on a single global
617c478bd9Sstevel@tonic-gate  * list, thread_list.  After creation, the state in the thread_info structure
627c478bd9Sstevel@tonic-gate  * is only modified by the associated thread, so no locking is needed.  A TSD
637c478bd9Sstevel@tonic-gate  * destructor removes the thread_info from the global list and frees it at
647c478bd9Sstevel@tonic-gate  * pthread_exit() time.
657c478bd9Sstevel@tonic-gate  *
667c478bd9Sstevel@tonic-gate  * Threads access their per-thread data using thread_self()
677c478bd9Sstevel@tonic-gate  *
687c478bd9Sstevel@tonic-gate  * The thread_list is protected by thread_lock, a leaf lock.
697c478bd9Sstevel@tonic-gate  *
707c478bd9Sstevel@tonic-gate  * 2. Door Thread Pool Management
717c478bd9Sstevel@tonic-gate  * ------------------------------
727c478bd9Sstevel@tonic-gate  * Whenever door_return(3door) returns from the kernel and there are no
737c478bd9Sstevel@tonic-gate  * other configd threads waiting for requests, libdoor automatically
747c478bd9Sstevel@tonic-gate  * invokes a function registered with door_server_create(), to request a new
757c478bd9Sstevel@tonic-gate  * door server thread.  The default function just creates a thread that calls
767c478bd9Sstevel@tonic-gate  * door_return(3door).  Unfortunately, since it can take a while for the new
777c478bd9Sstevel@tonic-gate  * thread to *get* to door_return(3door), a stream of requests can cause a
787c478bd9Sstevel@tonic-gate  * large number of threads to be created, even though they aren't all needed.
797c478bd9Sstevel@tonic-gate  *
807c478bd9Sstevel@tonic-gate  * In our callback, new_server_needed(), we limit ourself to two new threads
817c478bd9Sstevel@tonic-gate  * at a time -- this logic is handled in reserve_new_thread().  This keeps
827c478bd9Sstevel@tonic-gate  * us from creating an absurd number of threads in response to peaking load.
837c478bd9Sstevel@tonic-gate  */
847c478bd9Sstevel@tonic-gate static pthread_key_t	thread_info_key;
857c478bd9Sstevel@tonic-gate static pthread_attr_t	thread_attr;
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate static pthread_mutex_t	thread_lock = PTHREAD_MUTEX_INITIALIZER;
887c478bd9Sstevel@tonic-gate int			num_started;	/* number actually running */
897c478bd9Sstevel@tonic-gate int			num_servers;	/* number in-progress or running */
907c478bd9Sstevel@tonic-gate static uu_list_pool_t	*thread_pool;
917c478bd9Sstevel@tonic-gate uu_list_t		*thread_list;
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate static thread_info_t	main_thread_info;
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate static int	finished;
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate static pid_t	privileged_pid = 0;
987c478bd9Sstevel@tonic-gate static int	privileged_psinfo_fd = -1;
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate static int	privileged_user = 0;
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate static priv_set_t *privileged_privs;
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate static int	log_to_syslog = 0;
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate int		is_main_repository = 1;
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate int		max_repository_backups = 4;
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate #define	CONFIGD_MAX_FDS		262144
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate /*
1137c478bd9Sstevel@tonic-gate  * Thanks, Mike
1147c478bd9Sstevel@tonic-gate  */
1157c478bd9Sstevel@tonic-gate void
1167c478bd9Sstevel@tonic-gate abort_handler(int sig, siginfo_t *sip, ucontext_t *ucp)
1177c478bd9Sstevel@tonic-gate {
1187c478bd9Sstevel@tonic-gate 	struct sigaction act;
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&act.sa_mask);
1217c478bd9Sstevel@tonic-gate 	act.sa_handler = SIG_DFL;
1227c478bd9Sstevel@tonic-gate 	act.sa_flags = 0;
1237c478bd9Sstevel@tonic-gate 	(void) sigaction(sig, &act, NULL);
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate 	(void) printstack(2);
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate 	if (sip != NULL && SI_FROMUSER(sip))
1287c478bd9Sstevel@tonic-gate 		(void) pthread_kill(pthread_self(), sig);
1297c478bd9Sstevel@tonic-gate 	(void) sigfillset(&ucp->uc_sigmask);
1307c478bd9Sstevel@tonic-gate 	(void) sigdelset(&ucp->uc_sigmask, sig);
1317c478bd9Sstevel@tonic-gate 	ucp->uc_flags |= UC_SIGMASK;
1327c478bd9Sstevel@tonic-gate 	(void) setcontext(ucp);
1337c478bd9Sstevel@tonic-gate }
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate /*
1367c478bd9Sstevel@tonic-gate  * Don't want to have more than a couple thread creates outstanding
1377c478bd9Sstevel@tonic-gate  */
1387c478bd9Sstevel@tonic-gate static int
1397c478bd9Sstevel@tonic-gate reserve_new_thread(void)
1407c478bd9Sstevel@tonic-gate {
1417c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&thread_lock);
1427c478bd9Sstevel@tonic-gate 	assert(num_started >= 0);
1437c478bd9Sstevel@tonic-gate 	if (num_servers > num_started + 1) {
1447c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_unlock(&thread_lock);
1457c478bd9Sstevel@tonic-gate 		return (0);
1467c478bd9Sstevel@tonic-gate 	}
1477c478bd9Sstevel@tonic-gate 	++num_servers;
1487c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&thread_lock);
1497c478bd9Sstevel@tonic-gate 	return (1);
1507c478bd9Sstevel@tonic-gate }
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate static void
1537c478bd9Sstevel@tonic-gate thread_info_free(thread_info_t *ti)
1547c478bd9Sstevel@tonic-gate {
1557c478bd9Sstevel@tonic-gate 	uu_list_node_fini(ti, &ti->ti_node, thread_pool);
1567c478bd9Sstevel@tonic-gate 	if (ti->ti_ucred != NULL)
1577c478bd9Sstevel@tonic-gate 		uu_free(ti->ti_ucred);
1587c478bd9Sstevel@tonic-gate 	uu_free(ti);
1597c478bd9Sstevel@tonic-gate }
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate static void
1627c478bd9Sstevel@tonic-gate thread_exiting(void *arg)
1637c478bd9Sstevel@tonic-gate {
1647c478bd9Sstevel@tonic-gate 	thread_info_t *ti = arg;
1657c478bd9Sstevel@tonic-gate 
166*8918dff3Sjwadams 	if (ti != NULL)
1677c478bd9Sstevel@tonic-gate 		log_enter(&ti->ti_log);
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&thread_lock);
1707c478bd9Sstevel@tonic-gate 	if (ti != NULL) {
1717c478bd9Sstevel@tonic-gate 		num_started--;
1727c478bd9Sstevel@tonic-gate 		uu_list_remove(thread_list, ti);
1737c478bd9Sstevel@tonic-gate 	}
1747c478bd9Sstevel@tonic-gate 	assert(num_servers > 0);
1757c478bd9Sstevel@tonic-gate 	--num_servers;
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate 	if (num_servers == 0) {
1787c478bd9Sstevel@tonic-gate 		configd_critical("no door server threads\n");
1797c478bd9Sstevel@tonic-gate 		abort();
1807c478bd9Sstevel@tonic-gate 	}
1817c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&thread_lock);
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 	if (ti != NULL && ti != &main_thread_info)
1847c478bd9Sstevel@tonic-gate 		thread_info_free(ti);
1857c478bd9Sstevel@tonic-gate }
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate void
1887c478bd9Sstevel@tonic-gate thread_newstate(thread_info_t *ti, thread_state_t newstate)
1897c478bd9Sstevel@tonic-gate {
1907c478bd9Sstevel@tonic-gate 	ti->ti_ucred_read = 0;			/* invalidate cached ucred */
1917c478bd9Sstevel@tonic-gate 	if (newstate != ti->ti_state) {
1927c478bd9Sstevel@tonic-gate 		ti->ti_prev_state = ti->ti_state;
1937c478bd9Sstevel@tonic-gate 		ti->ti_state = newstate;
1947c478bd9Sstevel@tonic-gate 		ti->ti_lastchange = gethrtime();
1957c478bd9Sstevel@tonic-gate 	}
1967c478bd9Sstevel@tonic-gate }
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate thread_info_t *
1997c478bd9Sstevel@tonic-gate thread_self(void)
2007c478bd9Sstevel@tonic-gate {
2017c478bd9Sstevel@tonic-gate 	return (pthread_getspecific(thread_info_key));
2027c478bd9Sstevel@tonic-gate }
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate ucred_t *
2057c478bd9Sstevel@tonic-gate get_ucred(void)
2067c478bd9Sstevel@tonic-gate {
2077c478bd9Sstevel@tonic-gate 	thread_info_t *ti = thread_self();
2087c478bd9Sstevel@tonic-gate 	ucred_t **ret = &ti->ti_ucred;
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate 	if (ti->ti_ucred_read)
2117c478bd9Sstevel@tonic-gate 		return (*ret);			/* cached value */
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate 	if (door_ucred(ret) != 0)
2147c478bd9Sstevel@tonic-gate 		return (NULL);
2157c478bd9Sstevel@tonic-gate 	ti->ti_ucred_read = 1;
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate 	return (*ret);
2187c478bd9Sstevel@tonic-gate }
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate int
2217c478bd9Sstevel@tonic-gate ucred_is_privileged(ucred_t *uc)
2227c478bd9Sstevel@tonic-gate {
2237c478bd9Sstevel@tonic-gate 	const priv_set_t *ps;
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate 	if ((ps = ucred_getprivset(uc, PRIV_EFFECTIVE)) != NULL) {
2267c478bd9Sstevel@tonic-gate 		if (priv_isfullset(ps))
2277c478bd9Sstevel@tonic-gate 			return (1);		/* process has all privs */
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate 		if (privileged_privs != NULL &&
2307c478bd9Sstevel@tonic-gate 		    priv_issubset(privileged_privs, ps))
2317c478bd9Sstevel@tonic-gate 			return (1);		/* process has zone privs */
2327c478bd9Sstevel@tonic-gate 	}
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate 	return (0);
2357c478bd9Sstevel@tonic-gate }
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate static void *
2387c478bd9Sstevel@tonic-gate thread_start(void *arg)
2397c478bd9Sstevel@tonic-gate {
2407c478bd9Sstevel@tonic-gate 	thread_info_t *ti = arg;
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate 	(void) pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&thread_lock);
2457c478bd9Sstevel@tonic-gate 	num_started++;
2467c478bd9Sstevel@tonic-gate 	(void) uu_list_insert_after(thread_list, uu_list_last(thread_list),
2477c478bd9Sstevel@tonic-gate 	    ti);
2487c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&thread_lock);
2497c478bd9Sstevel@tonic-gate 	(void) pthread_setspecific(thread_info_key, ti);
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate 	thread_newstate(ti, TI_DOOR_RETURN);
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate 	/*
2547c478bd9Sstevel@tonic-gate 	 * Start handling door calls
2557c478bd9Sstevel@tonic-gate 	 */
2567c478bd9Sstevel@tonic-gate 	(void) door_return(NULL, 0, NULL, 0);
2577c478bd9Sstevel@tonic-gate 	return (arg);
2587c478bd9Sstevel@tonic-gate }
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate static void
2617c478bd9Sstevel@tonic-gate new_thread_needed(door_info_t *dip)
2627c478bd9Sstevel@tonic-gate {
2637c478bd9Sstevel@tonic-gate 	thread_info_t *ti;
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate 	sigset_t new, old;
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate 	assert(dip == NULL);
2687c478bd9Sstevel@tonic-gate 
2697c478bd9Sstevel@tonic-gate 	if (!reserve_new_thread())
2707c478bd9Sstevel@tonic-gate 		return;
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate 	if ((ti = uu_zalloc(sizeof (*ti))) == NULL)
2737c478bd9Sstevel@tonic-gate 		goto fail;
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate 	uu_list_node_init(ti, &ti->ti_node, thread_pool);
2767c478bd9Sstevel@tonic-gate 	ti->ti_state = TI_CREATED;
2777c478bd9Sstevel@tonic-gate 	ti->ti_prev_state = TI_CREATED;
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate 	if ((ti->ti_ucred = uu_zalloc(ucred_size())) == NULL)
2807c478bd9Sstevel@tonic-gate 		goto fail;
2817c478bd9Sstevel@tonic-gate 
2827c478bd9Sstevel@tonic-gate 	(void) sigfillset(&new);
2837c478bd9Sstevel@tonic-gate 	(void) pthread_sigmask(SIG_SETMASK, &new, &old);
2847c478bd9Sstevel@tonic-gate 	if ((errno = pthread_create(&ti->ti_thread, &thread_attr, thread_start,
2857c478bd9Sstevel@tonic-gate 	    ti)) != 0) {
2867c478bd9Sstevel@tonic-gate 		(void) pthread_sigmask(SIG_SETMASK, &old, NULL);
2877c478bd9Sstevel@tonic-gate 		goto fail;
2887c478bd9Sstevel@tonic-gate 	}
2897c478bd9Sstevel@tonic-gate 
2907c478bd9Sstevel@tonic-gate 	(void) pthread_sigmask(SIG_SETMASK, &old, NULL);
2917c478bd9Sstevel@tonic-gate 	return;
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate fail:
2947c478bd9Sstevel@tonic-gate 	/*
2957c478bd9Sstevel@tonic-gate 	 * Since the thread_info structure was never linked onto the
2967c478bd9Sstevel@tonic-gate 	 * thread list, thread_exiting() can't handle the cleanup.
2977c478bd9Sstevel@tonic-gate 	 */
2987c478bd9Sstevel@tonic-gate 	thread_exiting(NULL);
2997c478bd9Sstevel@tonic-gate 	if (ti != NULL)
3007c478bd9Sstevel@tonic-gate 		thread_info_free(ti);
3017c478bd9Sstevel@tonic-gate }
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate int
3047c478bd9Sstevel@tonic-gate create_connection(ucred_t *uc, repository_door_request_t *rp,
3057c478bd9Sstevel@tonic-gate     size_t rp_size, int *out_fd)
3067c478bd9Sstevel@tonic-gate {
3077c478bd9Sstevel@tonic-gate 	int flags;
3087c478bd9Sstevel@tonic-gate 	int privileged = 0;
3097c478bd9Sstevel@tonic-gate 	uint32_t debugflags = 0;
3107c478bd9Sstevel@tonic-gate 	psinfo_t info;
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate 	if (privileged_pid != 0) {
3137c478bd9Sstevel@tonic-gate 		/*
3147c478bd9Sstevel@tonic-gate 		 * in privileged pid mode, we only allow connections from
3157c478bd9Sstevel@tonic-gate 		 * our original parent -- the psinfo read verifies that
3167c478bd9Sstevel@tonic-gate 		 * it is the same process which we started with.
3177c478bd9Sstevel@tonic-gate 		 */
3187c478bd9Sstevel@tonic-gate 		if (ucred_getpid(uc) != privileged_pid ||
3197c478bd9Sstevel@tonic-gate 		    read(privileged_psinfo_fd, &info, sizeof (info)) !=
3207c478bd9Sstevel@tonic-gate 		    sizeof (info))
3217c478bd9Sstevel@tonic-gate 			return (REPOSITORY_DOOR_FAIL_PERMISSION_DENIED);
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate 		privileged = 1;			/* he gets full privileges */
3247c478bd9Sstevel@tonic-gate 	} else if (privileged_user != 0) {
3257c478bd9Sstevel@tonic-gate 		/*
3267c478bd9Sstevel@tonic-gate 		 * in privileged user mode, only one particular user is
3277c478bd9Sstevel@tonic-gate 		 * allowed to connect to us, and he can do anything.
3287c478bd9Sstevel@tonic-gate 		 */
3297c478bd9Sstevel@tonic-gate 		if (ucred_geteuid(uc) != privileged_user)
3307c478bd9Sstevel@tonic-gate 			return (REPOSITORY_DOOR_FAIL_PERMISSION_DENIED);
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate 		privileged = 1;
3337c478bd9Sstevel@tonic-gate 	}
3347c478bd9Sstevel@tonic-gate 
3357c478bd9Sstevel@tonic-gate 	/*
3367c478bd9Sstevel@tonic-gate 	 * Check that rp, of size rp_size, is large enough to
3377c478bd9Sstevel@tonic-gate 	 * contain field 'f'.  If so, write the value into *out, and return 1.
3387c478bd9Sstevel@tonic-gate 	 * Otherwise, return 0.
3397c478bd9Sstevel@tonic-gate 	 */
3407c478bd9Sstevel@tonic-gate #define	GET_ARG(rp, rp_size, f, out)					\
3417c478bd9Sstevel@tonic-gate 	(((rp_size) >= offsetofend(repository_door_request_t, f)) ?	\
3427c478bd9Sstevel@tonic-gate 	    ((*(out) = (rp)->f), 1) : 0)
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate 	if (!GET_ARG(rp, rp_size, rdr_flags, &flags))
3457c478bd9Sstevel@tonic-gate 		return (REPOSITORY_DOOR_FAIL_BAD_REQUEST);
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate #if (REPOSITORY_DOOR_FLAG_ALL != REPOSITORY_DOOR_FLAG_DEBUG)
3487c478bd9Sstevel@tonic-gate #error Need to update flag checks
3497c478bd9Sstevel@tonic-gate #endif
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate 	if (flags & ~REPOSITORY_DOOR_FLAG_ALL)
3527c478bd9Sstevel@tonic-gate 		return (REPOSITORY_DOOR_FAIL_BAD_FLAG);
3537c478bd9Sstevel@tonic-gate 
3547c478bd9Sstevel@tonic-gate 	if (flags & REPOSITORY_DOOR_FLAG_DEBUG)
3557c478bd9Sstevel@tonic-gate 		if (!GET_ARG(rp, rp_size, rdr_debug, &debugflags))
3567c478bd9Sstevel@tonic-gate 			return (REPOSITORY_DOOR_FAIL_BAD_REQUEST);
3577c478bd9Sstevel@tonic-gate #undef GET_ARG
3587c478bd9Sstevel@tonic-gate 
3597c478bd9Sstevel@tonic-gate 	return (create_client(ucred_getpid(uc), debugflags, privileged,
3607c478bd9Sstevel@tonic-gate 	    out_fd));
3617c478bd9Sstevel@tonic-gate }
3627c478bd9Sstevel@tonic-gate 
3637c478bd9Sstevel@tonic-gate void
3647c478bd9Sstevel@tonic-gate configd_vcritical(const char *message, va_list args)
3657c478bd9Sstevel@tonic-gate {
3667c478bd9Sstevel@tonic-gate 	if (log_to_syslog)
3677c478bd9Sstevel@tonic-gate 		vsyslog(LOG_CRIT, message, args);
3687c478bd9Sstevel@tonic-gate 	else {
3697c478bd9Sstevel@tonic-gate 		flockfile(stderr);
3707c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "svc.configd: Fatal error: ");
3717c478bd9Sstevel@tonic-gate 		(void) vfprintf(stderr, message, args);
3727c478bd9Sstevel@tonic-gate 		if (message[0] == 0 || message[strlen(message) - 1] != '\n')
3737c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "\n");
3747c478bd9Sstevel@tonic-gate 		funlockfile(stderr);
3757c478bd9Sstevel@tonic-gate 	}
3767c478bd9Sstevel@tonic-gate }
3777c478bd9Sstevel@tonic-gate 
3787c478bd9Sstevel@tonic-gate void
3797c478bd9Sstevel@tonic-gate configd_critical(const char *message, ...)
3807c478bd9Sstevel@tonic-gate {
3817c478bd9Sstevel@tonic-gate 	va_list args;
3827c478bd9Sstevel@tonic-gate 	va_start(args, message);
3837c478bd9Sstevel@tonic-gate 	configd_vcritical(message, args);
3847c478bd9Sstevel@tonic-gate 	va_end(args);
3857c478bd9Sstevel@tonic-gate }
3867c478bd9Sstevel@tonic-gate 
3877c478bd9Sstevel@tonic-gate static void
3887c478bd9Sstevel@tonic-gate usage(const char *prog, int ret)
3897c478bd9Sstevel@tonic-gate {
3907c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
3917c478bd9Sstevel@tonic-gate 	    "usage: %s [-np] [-d door_path] [-r repository_path]\n"
3927c478bd9Sstevel@tonic-gate 	    "    [-t nonpersist_repository]\n", prog);
3937c478bd9Sstevel@tonic-gate 	exit(ret);
3947c478bd9Sstevel@tonic-gate }
3957c478bd9Sstevel@tonic-gate 
3967c478bd9Sstevel@tonic-gate /*ARGSUSED*/
3977c478bd9Sstevel@tonic-gate static void
3987c478bd9Sstevel@tonic-gate handler(int sig, siginfo_t *info, void *data)
3997c478bd9Sstevel@tonic-gate {
4007c478bd9Sstevel@tonic-gate 	finished = 1;
4017c478bd9Sstevel@tonic-gate }
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate static int pipe_fd = -1;
4047c478bd9Sstevel@tonic-gate 
4057c478bd9Sstevel@tonic-gate static int
4067c478bd9Sstevel@tonic-gate daemonize_start(void)
4077c478bd9Sstevel@tonic-gate {
4087c478bd9Sstevel@tonic-gate 	char data;
4097c478bd9Sstevel@tonic-gate 	int status;
4107c478bd9Sstevel@tonic-gate 
4117c478bd9Sstevel@tonic-gate 	int filedes[2];
4127c478bd9Sstevel@tonic-gate 	pid_t pid;
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate 	(void) close(0);
4157c478bd9Sstevel@tonic-gate 	(void) dup2(2, 1);		/* stderr only */
4167c478bd9Sstevel@tonic-gate 
4177c478bd9Sstevel@tonic-gate 	if (pipe(filedes) < 0)
4187c478bd9Sstevel@tonic-gate 		return (-1);
4197c478bd9Sstevel@tonic-gate 
4207c478bd9Sstevel@tonic-gate 	if ((pid = fork1()) < 0)
4217c478bd9Sstevel@tonic-gate 		return (-1);
4227c478bd9Sstevel@tonic-gate 
4237c478bd9Sstevel@tonic-gate 	if (pid != 0) {
4247c478bd9Sstevel@tonic-gate 		/*
4257c478bd9Sstevel@tonic-gate 		 * parent
4267c478bd9Sstevel@tonic-gate 		 */
4277c478bd9Sstevel@tonic-gate 		struct sigaction act;
4287c478bd9Sstevel@tonic-gate 
4297c478bd9Sstevel@tonic-gate 		act.sa_sigaction = SIG_DFL;
4307c478bd9Sstevel@tonic-gate 		(void) sigemptyset(&act.sa_mask);
4317c478bd9Sstevel@tonic-gate 		act.sa_flags = 0;
4327c478bd9Sstevel@tonic-gate 
4337c478bd9Sstevel@tonic-gate 		(void) sigaction(SIGPIPE, &act, NULL);	/* ignore SIGPIPE */
4347c478bd9Sstevel@tonic-gate 
4357c478bd9Sstevel@tonic-gate 		(void) close(filedes[1]);
4367c478bd9Sstevel@tonic-gate 		if (read(filedes[0], &data, 1) == 1) {
4377c478bd9Sstevel@tonic-gate 			/* presume success */
4387c478bd9Sstevel@tonic-gate 			_exit(CONFIGD_EXIT_OKAY);
4397c478bd9Sstevel@tonic-gate 		}
4407c478bd9Sstevel@tonic-gate 
4417c478bd9Sstevel@tonic-gate 		status = -1;
4427c478bd9Sstevel@tonic-gate 		(void) wait4(pid, &status, 0, NULL);
4437c478bd9Sstevel@tonic-gate 		if (WIFEXITED(status))
4447c478bd9Sstevel@tonic-gate 			_exit(WEXITSTATUS(status));
4457c478bd9Sstevel@tonic-gate 		else
4467c478bd9Sstevel@tonic-gate 			_exit(-1);
4477c478bd9Sstevel@tonic-gate 	}
4487c478bd9Sstevel@tonic-gate 
4497c478bd9Sstevel@tonic-gate 	/*
4507c478bd9Sstevel@tonic-gate 	 * child
4517c478bd9Sstevel@tonic-gate 	 */
4527c478bd9Sstevel@tonic-gate 	pipe_fd = filedes[1];
4537c478bd9Sstevel@tonic-gate 	(void) close(filedes[0]);
4547c478bd9Sstevel@tonic-gate 
4557c478bd9Sstevel@tonic-gate 	/*
4567c478bd9Sstevel@tonic-gate 	 * generic Unix setup
4577c478bd9Sstevel@tonic-gate 	 */
4587c478bd9Sstevel@tonic-gate 	(void) setsid();
4597c478bd9Sstevel@tonic-gate 	(void) umask(0077);
4607c478bd9Sstevel@tonic-gate 
4617c478bd9Sstevel@tonic-gate 	return (0);
4627c478bd9Sstevel@tonic-gate }
4637c478bd9Sstevel@tonic-gate 
4647c478bd9Sstevel@tonic-gate static void
4657c478bd9Sstevel@tonic-gate daemonize_ready(void)
4667c478bd9Sstevel@tonic-gate {
4677c478bd9Sstevel@tonic-gate 	char data = '\0';
4687c478bd9Sstevel@tonic-gate 
4697c478bd9Sstevel@tonic-gate 	/*
4707c478bd9Sstevel@tonic-gate 	 * wake the parent
4717c478bd9Sstevel@tonic-gate 	 */
4727c478bd9Sstevel@tonic-gate 	(void) write(pipe_fd, &data, 1);
4737c478bd9Sstevel@tonic-gate 	(void) close(pipe_fd);
4747c478bd9Sstevel@tonic-gate }
4757c478bd9Sstevel@tonic-gate 
476*8918dff3Sjwadams const char *
477*8918dff3Sjwadams regularize_path(const char *dir, const char *base, char *tmpbuf)
478*8918dff3Sjwadams {
479*8918dff3Sjwadams 	if (base == NULL)
480*8918dff3Sjwadams 		return (NULL);
481*8918dff3Sjwadams 	if (base[0] == '/')
482*8918dff3Sjwadams 		return (base);
483*8918dff3Sjwadams 
484*8918dff3Sjwadams 	if (snprintf(tmpbuf, PATH_MAX, "%s/%s", dir, base) >= PATH_MAX) {
485*8918dff3Sjwadams 		(void) fprintf(stderr, "svc.configd: %s/%s: path too long\n",
486*8918dff3Sjwadams 		    dir, base);
487*8918dff3Sjwadams 		exit(CONFIGD_EXIT_BAD_ARGS);
488*8918dff3Sjwadams 	}
489*8918dff3Sjwadams 
490*8918dff3Sjwadams 	return (tmpbuf);
491*8918dff3Sjwadams }
492*8918dff3Sjwadams 
4937c478bd9Sstevel@tonic-gate int
4947c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
4957c478bd9Sstevel@tonic-gate {
4967c478bd9Sstevel@tonic-gate 	thread_info_t *ti = &main_thread_info;
4977c478bd9Sstevel@tonic-gate 
4987c478bd9Sstevel@tonic-gate 	char pidpath[sizeof ("/proc/" "/psinfo") + 10];
4997c478bd9Sstevel@tonic-gate 
5007c478bd9Sstevel@tonic-gate 	struct rlimit fd_new;
5017c478bd9Sstevel@tonic-gate 
5027c478bd9Sstevel@tonic-gate 	const char *endptr;
5037c478bd9Sstevel@tonic-gate 	sigset_t myset;
5047c478bd9Sstevel@tonic-gate 	int c;
5057c478bd9Sstevel@tonic-gate 	int ret;
5067c478bd9Sstevel@tonic-gate 	int fd;
507*8918dff3Sjwadams 
508*8918dff3Sjwadams 	char curdir[PATH_MAX];
509*8918dff3Sjwadams 	char dbtmp[PATH_MAX];
510*8918dff3Sjwadams 	char npdbtmp[PATH_MAX];
511*8918dff3Sjwadams 	char doortmp[PATH_MAX];
512*8918dff3Sjwadams 
5137c478bd9Sstevel@tonic-gate 	const char *dbpath = NULL;
5147c478bd9Sstevel@tonic-gate 	const char *npdbpath = NULL;
5157c478bd9Sstevel@tonic-gate 	const char *doorpath = REPOSITORY_DOOR_NAME;
5167c478bd9Sstevel@tonic-gate 	struct sigaction act;
5177c478bd9Sstevel@tonic-gate 
5187c478bd9Sstevel@tonic-gate 	int daemonize = 1;		/* default to daemonizing */
5197c478bd9Sstevel@tonic-gate 	int have_npdb = 1;
5207c478bd9Sstevel@tonic-gate 
5217c478bd9Sstevel@tonic-gate 	closefrom(3);			/* get rid of extraneous fds */
5227c478bd9Sstevel@tonic-gate 
523*8918dff3Sjwadams 	if (getcwd(curdir, sizeof (curdir)) == NULL) {
524*8918dff3Sjwadams 		(void) fprintf(stderr,
525*8918dff3Sjwadams 		    "%s: unable to get current directory: %s\n",
526*8918dff3Sjwadams 		    argv[0], strerror(errno));
527*8918dff3Sjwadams 		exit(CONFIGD_EXIT_INIT_FAILED);
528*8918dff3Sjwadams 	}
529*8918dff3Sjwadams 
5307c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "Dnpd:r:t:")) != -1) {
5317c478bd9Sstevel@tonic-gate 		switch (c) {
5327c478bd9Sstevel@tonic-gate 		case 'n':
5337c478bd9Sstevel@tonic-gate 			daemonize = 0;
5347c478bd9Sstevel@tonic-gate 			break;
5357c478bd9Sstevel@tonic-gate 		case 'd':
536*8918dff3Sjwadams 			doorpath = regularize_path(curdir, optarg, doortmp);
5377c478bd9Sstevel@tonic-gate 			is_main_repository = 0;
5387c478bd9Sstevel@tonic-gate 			have_npdb = 0;		/* default to no non-persist */
5397c478bd9Sstevel@tonic-gate 			break;
5407c478bd9Sstevel@tonic-gate 		case 'p':
5417c478bd9Sstevel@tonic-gate 			log_to_syslog = 0;	/* don't use syslog */
5427c478bd9Sstevel@tonic-gate 			is_main_repository = 0;
5437c478bd9Sstevel@tonic-gate 
5447c478bd9Sstevel@tonic-gate 			/*
5457c478bd9Sstevel@tonic-gate 			 * If our parent exits while we're opening its /proc
5467c478bd9Sstevel@tonic-gate 			 * psinfo, we're vulnerable to a pid wrapping.  To
5477c478bd9Sstevel@tonic-gate 			 * protect against that, re-check our ppid after
5487c478bd9Sstevel@tonic-gate 			 * opening it.
5497c478bd9Sstevel@tonic-gate 			 */
5507c478bd9Sstevel@tonic-gate 			privileged_pid = getppid();
5517c478bd9Sstevel@tonic-gate 			(void) snprintf(pidpath, sizeof (pidpath),
5527c478bd9Sstevel@tonic-gate 			    "/proc/%d/psinfo", privileged_pid);
5537c478bd9Sstevel@tonic-gate 			if ((fd = open(pidpath, O_RDONLY)) < 0 ||
5547c478bd9Sstevel@tonic-gate 			    getppid() != privileged_pid) {
5557c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
5567c478bd9Sstevel@tonic-gate 				    "%s: unable to get parent info\n", argv[0]);
5577c478bd9Sstevel@tonic-gate 				exit(CONFIGD_EXIT_BAD_ARGS);
5587c478bd9Sstevel@tonic-gate 			}
5597c478bd9Sstevel@tonic-gate 			privileged_psinfo_fd = fd;
5607c478bd9Sstevel@tonic-gate 			break;
5617c478bd9Sstevel@tonic-gate 		case 'r':
562*8918dff3Sjwadams 			dbpath = regularize_path(curdir, optarg, dbtmp);
5637c478bd9Sstevel@tonic-gate 			is_main_repository = 0;
5647c478bd9Sstevel@tonic-gate 			break;
5657c478bd9Sstevel@tonic-gate 		case 't':
566*8918dff3Sjwadams 			npdbpath = regularize_path(curdir, optarg, npdbtmp);
5677c478bd9Sstevel@tonic-gate 			is_main_repository = 0;
5687c478bd9Sstevel@tonic-gate 			break;
5697c478bd9Sstevel@tonic-gate 		default:
5707c478bd9Sstevel@tonic-gate 			usage(argv[0], CONFIGD_EXIT_BAD_ARGS);
5717c478bd9Sstevel@tonic-gate 			break;
5727c478bd9Sstevel@tonic-gate 		}
5737c478bd9Sstevel@tonic-gate 	}
5747c478bd9Sstevel@tonic-gate 
5757c478bd9Sstevel@tonic-gate 	/*
5767c478bd9Sstevel@tonic-gate 	 * If we're not running as root, allow our euid full access, and
5777c478bd9Sstevel@tonic-gate 	 * everyone else no access.
5787c478bd9Sstevel@tonic-gate 	 */
5797c478bd9Sstevel@tonic-gate 	if (privileged_pid == 0 && geteuid() != 0) {
5807c478bd9Sstevel@tonic-gate 		privileged_user = geteuid();
5817c478bd9Sstevel@tonic-gate 	}
5827c478bd9Sstevel@tonic-gate 
5837c478bd9Sstevel@tonic-gate 	privileged_privs = priv_str_to_set("zone", "", &endptr);
5847c478bd9Sstevel@tonic-gate 	if (endptr != NULL && privileged_privs != NULL) {
5857c478bd9Sstevel@tonic-gate 		priv_freeset(privileged_privs);
5867c478bd9Sstevel@tonic-gate 		privileged_privs = NULL;
5877c478bd9Sstevel@tonic-gate 	}
5887c478bd9Sstevel@tonic-gate 
5897c478bd9Sstevel@tonic-gate 	openlog("svc.configd", LOG_PID | LOG_CONS, LOG_DAEMON);
5907c478bd9Sstevel@tonic-gate 	(void) setlogmask(LOG_UPTO(LOG_NOTICE));
5917c478bd9Sstevel@tonic-gate 
5927c478bd9Sstevel@tonic-gate 	/*
5937c478bd9Sstevel@tonic-gate 	 * if a non-persist db is specified, always enable it
5947c478bd9Sstevel@tonic-gate 	 */
5957c478bd9Sstevel@tonic-gate 	if (npdbpath)
5967c478bd9Sstevel@tonic-gate 		have_npdb = 1;
5977c478bd9Sstevel@tonic-gate 
5987c478bd9Sstevel@tonic-gate 	if (optind != argc)
5997c478bd9Sstevel@tonic-gate 		usage(argv[0], CONFIGD_EXIT_BAD_ARGS);
6007c478bd9Sstevel@tonic-gate 
6017c478bd9Sstevel@tonic-gate 	if (daemonize) {
6027c478bd9Sstevel@tonic-gate 		if (getuid() == 0)
6037c478bd9Sstevel@tonic-gate 			(void) chdir("/");
6047c478bd9Sstevel@tonic-gate 		if (daemonize_start() < 0) {
6057c478bd9Sstevel@tonic-gate 			(void) perror("unable to daemonize");
6067c478bd9Sstevel@tonic-gate 			exit(CONFIGD_EXIT_INIT_FAILED);
6077c478bd9Sstevel@tonic-gate 		}
6087c478bd9Sstevel@tonic-gate 	}
6097c478bd9Sstevel@tonic-gate 	if (getuid() == 0)
6107c478bd9Sstevel@tonic-gate 		(void) core_set_process_path(CONFIGD_CORE,
6117c478bd9Sstevel@tonic-gate 		    strlen(CONFIGD_CORE) + 1, getpid());
6127c478bd9Sstevel@tonic-gate 
6137c478bd9Sstevel@tonic-gate 	/*
6147c478bd9Sstevel@tonic-gate 	 * this should be enabled once we can drop privileges and still get
6157c478bd9Sstevel@tonic-gate 	 * a core dump.
6167c478bd9Sstevel@tonic-gate 	 */
6177c478bd9Sstevel@tonic-gate #if 0
6187c478bd9Sstevel@tonic-gate 	/* turn off basic privileges we do not need */
6197c478bd9Sstevel@tonic-gate 	(void) priv_set(PRIV_OFF, PRIV_PERMITTED, PRIV_FILE_LINK_ANY,
6207c478bd9Sstevel@tonic-gate 	    PRIV_PROC_EXEC, PRIV_PROC_FORK, PRIV_PROC_SESSION, NULL);
6217c478bd9Sstevel@tonic-gate #endif
6227c478bd9Sstevel@tonic-gate 
6237c478bd9Sstevel@tonic-gate 	/* not that we can exec, but to be safe, shut them all off... */
6247c478bd9Sstevel@tonic-gate 	(void) priv_set(PRIV_SET, PRIV_INHERITABLE, NULL);
6257c478bd9Sstevel@tonic-gate 
6267c478bd9Sstevel@tonic-gate 	(void) sigfillset(&act.sa_mask);
6277c478bd9Sstevel@tonic-gate 
6287c478bd9Sstevel@tonic-gate 	/* signals to ignore */
6297c478bd9Sstevel@tonic-gate 	act.sa_sigaction = SIG_IGN;
6307c478bd9Sstevel@tonic-gate 	act.sa_flags = 0;
6317c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGPIPE, &act, NULL);
6327c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGALRM, &act, NULL);
6337c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGUSR1, &act, NULL);
6347c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGUSR2, &act, NULL);
6357c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGPOLL, &act, NULL);
6367c478bd9Sstevel@tonic-gate 
6377c478bd9Sstevel@tonic-gate 	/* signals to abort on */
6387c478bd9Sstevel@tonic-gate 	act.sa_sigaction = (void (*)(int, siginfo_t *, void *))&abort_handler;
6397c478bd9Sstevel@tonic-gate 	act.sa_flags = SA_SIGINFO;
6407c478bd9Sstevel@tonic-gate 
6417c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGABRT, &act, NULL);
6427c478bd9Sstevel@tonic-gate 
6437c478bd9Sstevel@tonic-gate 	/* signals to handle */
6447c478bd9Sstevel@tonic-gate 	act.sa_sigaction = &handler;
6457c478bd9Sstevel@tonic-gate 	act.sa_flags = SA_SIGINFO;
6467c478bd9Sstevel@tonic-gate 
6477c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGHUP, &act, NULL);
6487c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGINT, &act, NULL);
6497c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGTERM, &act, NULL);
6507c478bd9Sstevel@tonic-gate 
6517c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&myset);
6527c478bd9Sstevel@tonic-gate 	(void) sigaddset(&myset, SIGHUP);
6537c478bd9Sstevel@tonic-gate 	(void) sigaddset(&myset, SIGINT);
6547c478bd9Sstevel@tonic-gate 	(void) sigaddset(&myset, SIGTERM);
6557c478bd9Sstevel@tonic-gate 
6567c478bd9Sstevel@tonic-gate 	if ((errno = pthread_attr_init(&thread_attr)) != 0) {
6577c478bd9Sstevel@tonic-gate 		(void) perror("initializing");
6587c478bd9Sstevel@tonic-gate 		exit(CONFIGD_EXIT_INIT_FAILED);
6597c478bd9Sstevel@tonic-gate 	}
6607c478bd9Sstevel@tonic-gate 
6617c478bd9Sstevel@tonic-gate 	/*
6627c478bd9Sstevel@tonic-gate 	 * Set the hard and soft limits to CONFIGD_MAX_FDS.
6637c478bd9Sstevel@tonic-gate 	 */
6647c478bd9Sstevel@tonic-gate 	fd_new.rlim_max = fd_new.rlim_cur = CONFIGD_MAX_FDS;
6657c478bd9Sstevel@tonic-gate 	(void) setrlimit(RLIMIT_NOFILE, &fd_new);
6667c478bd9Sstevel@tonic-gate 
6677c478bd9Sstevel@tonic-gate 	if ((ret = backend_init(dbpath, npdbpath, have_npdb)) !=
6687c478bd9Sstevel@tonic-gate 	    CONFIGD_EXIT_OKAY)
6697c478bd9Sstevel@tonic-gate 		exit(ret);
6707c478bd9Sstevel@tonic-gate 
6717c478bd9Sstevel@tonic-gate 	if (!client_init())
6727c478bd9Sstevel@tonic-gate 		exit(CONFIGD_EXIT_INIT_FAILED);
6737c478bd9Sstevel@tonic-gate 
6747c478bd9Sstevel@tonic-gate 	if (!rc_node_init())
6757c478bd9Sstevel@tonic-gate 		exit(CONFIGD_EXIT_INIT_FAILED);
6767c478bd9Sstevel@tonic-gate 
6777c478bd9Sstevel@tonic-gate 	(void) pthread_attr_setdetachstate(&thread_attr,
6787c478bd9Sstevel@tonic-gate 	    PTHREAD_CREATE_DETACHED);
6797c478bd9Sstevel@tonic-gate 	(void) pthread_attr_setscope(&thread_attr, PTHREAD_SCOPE_SYSTEM);
6807c478bd9Sstevel@tonic-gate 
6817c478bd9Sstevel@tonic-gate 	if ((errno = pthread_key_create(&thread_info_key,
6827c478bd9Sstevel@tonic-gate 	    thread_exiting)) != 0) {
6837c478bd9Sstevel@tonic-gate 		perror("pthread_key_create");
6847c478bd9Sstevel@tonic-gate 		exit(CONFIGD_EXIT_INIT_FAILED);
6857c478bd9Sstevel@tonic-gate 	}
6867c478bd9Sstevel@tonic-gate 
6877c478bd9Sstevel@tonic-gate 	if ((thread_pool = uu_list_pool_create("thread_pool",
6887c478bd9Sstevel@tonic-gate 	    sizeof (thread_info_t), offsetof(thread_info_t, ti_node),
6897c478bd9Sstevel@tonic-gate 	    NULL, UU_LIST_POOL_DEBUG)) == NULL) {
6907c478bd9Sstevel@tonic-gate 		configd_critical("uu_list_pool_create: %s\n",
6917c478bd9Sstevel@tonic-gate 		    uu_strerror(uu_error()));
6927c478bd9Sstevel@tonic-gate 		exit(CONFIGD_EXIT_INIT_FAILED);
6937c478bd9Sstevel@tonic-gate 	}
6947c478bd9Sstevel@tonic-gate 
6957c478bd9Sstevel@tonic-gate 	if ((thread_list = uu_list_create(thread_pool, NULL, 0)) == NULL) {
6967c478bd9Sstevel@tonic-gate 		configd_critical("uu_list_create: %s\n",
6977c478bd9Sstevel@tonic-gate 		    uu_strerror(uu_error()));
6987c478bd9Sstevel@tonic-gate 		exit(CONFIGD_EXIT_INIT_FAILED);
6997c478bd9Sstevel@tonic-gate 	}
7007c478bd9Sstevel@tonic-gate 
7017c478bd9Sstevel@tonic-gate 	(void) memset(ti, '\0', sizeof (*ti));
7027c478bd9Sstevel@tonic-gate 	uu_list_node_init(ti, &ti->ti_node, thread_pool);
7037c478bd9Sstevel@tonic-gate 	(void) uu_list_insert_before(thread_list, uu_list_first(thread_list),
7047c478bd9Sstevel@tonic-gate 	    ti);
7057c478bd9Sstevel@tonic-gate 
7067c478bd9Sstevel@tonic-gate 	ti->ti_thread = pthread_self();
7077c478bd9Sstevel@tonic-gate 	ti->ti_state = TI_SIGNAL_WAIT;
7087c478bd9Sstevel@tonic-gate 	ti->ti_prev_state = TI_SIGNAL_WAIT;
7097c478bd9Sstevel@tonic-gate 
7107c478bd9Sstevel@tonic-gate 	(void) pthread_setspecific(thread_info_key, ti);
7117c478bd9Sstevel@tonic-gate 
7127c478bd9Sstevel@tonic-gate 	(void) door_server_create(new_thread_needed);
7137c478bd9Sstevel@tonic-gate 
7147c478bd9Sstevel@tonic-gate 	if (!setup_main_door(doorpath)) {
7157c478bd9Sstevel@tonic-gate 		configd_critical("Setting up main door failed.\n");
7167c478bd9Sstevel@tonic-gate 		exit(CONFIGD_EXIT_DOOR_INIT_FAILED);
7177c478bd9Sstevel@tonic-gate 	}
7187c478bd9Sstevel@tonic-gate 
7197c478bd9Sstevel@tonic-gate 	if (daemonize)
7207c478bd9Sstevel@tonic-gate 		daemonize_ready();
7217c478bd9Sstevel@tonic-gate 
7227c478bd9Sstevel@tonic-gate 	(void) pthread_sigmask(SIG_BLOCK, &myset, NULL);
7237c478bd9Sstevel@tonic-gate 	while (!finished) {
7247c478bd9Sstevel@tonic-gate 		int sig = sigwait(&myset);
7257c478bd9Sstevel@tonic-gate 		if (sig > 0) {
7267c478bd9Sstevel@tonic-gate 			break;
7277c478bd9Sstevel@tonic-gate 		}
7287c478bd9Sstevel@tonic-gate 	}
7297c478bd9Sstevel@tonic-gate 
7307c478bd9Sstevel@tonic-gate 	backend_fini();
7317c478bd9Sstevel@tonic-gate 
7327c478bd9Sstevel@tonic-gate 	return (CONFIGD_EXIT_OKAY);
7337c478bd9Sstevel@tonic-gate }
734