xref: /titanic_44/usr/src/cmd/svc/configd/configd.c (revision 6e1d2b428379e5270ccda99d403eb225e8699c3e)
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
5004388ebScasper  * Common Development and Distribution License (the "License").
6004388ebScasper  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
225b7f77adStw21770  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <assert.h>
297c478bd9Sstevel@tonic-gate #include <door.h>
307c478bd9Sstevel@tonic-gate #include <errno.h>
317c478bd9Sstevel@tonic-gate #include <fcntl.h>
328918dff3Sjwadams #include <limits.h>
337c478bd9Sstevel@tonic-gate #include <priv.h>
347c478bd9Sstevel@tonic-gate #include <procfs.h>
357c478bd9Sstevel@tonic-gate #include <pthread.h>
367c478bd9Sstevel@tonic-gate #include <signal.h>
377c478bd9Sstevel@tonic-gate #include <stdarg.h>
387c478bd9Sstevel@tonic-gate #include <stdio.h>
39004388ebScasper #include <stdio_ext.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 
1668918dff3Sjwadams 	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 
2045b7f77adStw21770 /*
2055b7f77adStw21770  * get_ucred() returns NULL if it was unable to get the credential
2065b7f77adStw21770  * information.
2075b7f77adStw21770  */
2087c478bd9Sstevel@tonic-gate ucred_t *
2097c478bd9Sstevel@tonic-gate get_ucred(void)
2107c478bd9Sstevel@tonic-gate {
2117c478bd9Sstevel@tonic-gate 	thread_info_t *ti = thread_self();
2127c478bd9Sstevel@tonic-gate 	ucred_t **ret = &ti->ti_ucred;
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate 	if (ti->ti_ucred_read)
2157c478bd9Sstevel@tonic-gate 		return (*ret);			/* cached value */
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate 	if (door_ucred(ret) != 0)
2187c478bd9Sstevel@tonic-gate 		return (NULL);
2197c478bd9Sstevel@tonic-gate 	ti->ti_ucred_read = 1;
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate 	return (*ret);
2227c478bd9Sstevel@tonic-gate }
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate int
2257c478bd9Sstevel@tonic-gate ucred_is_privileged(ucred_t *uc)
2267c478bd9Sstevel@tonic-gate {
2277c478bd9Sstevel@tonic-gate 	const priv_set_t *ps;
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate 	if ((ps = ucred_getprivset(uc, PRIV_EFFECTIVE)) != NULL) {
2307c478bd9Sstevel@tonic-gate 		if (priv_isfullset(ps))
2317c478bd9Sstevel@tonic-gate 			return (1);		/* process has all privs */
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate 		if (privileged_privs != NULL &&
2347c478bd9Sstevel@tonic-gate 		    priv_issubset(privileged_privs, ps))
2357c478bd9Sstevel@tonic-gate 			return (1);		/* process has zone privs */
2367c478bd9Sstevel@tonic-gate 	}
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate 	return (0);
2397c478bd9Sstevel@tonic-gate }
2407c478bd9Sstevel@tonic-gate 
2415b7f77adStw21770 /*
2425b7f77adStw21770  * The purpose of this function is to get the audit session data for use in
2435b7f77adStw21770  * generating SMF audit events.  We use a single audit session per client.
2445b7f77adStw21770  *
2455b7f77adStw21770  * get_audit_session() may return NULL.  It is legal to use a NULL pointer
2465b7f77adStw21770  * in subsequent calls to adt_* functions.
2475b7f77adStw21770  */
2485b7f77adStw21770 adt_session_data_t *
2495b7f77adStw21770 get_audit_session(void)
2505b7f77adStw21770 {
2515b7f77adStw21770 	thread_info_t	*ti = thread_self();
2525b7f77adStw21770 
2535b7f77adStw21770 	return (ti->ti_active_client->rc_adt_session);
2545b7f77adStw21770 }
2555b7f77adStw21770 
2567c478bd9Sstevel@tonic-gate static void *
2577c478bd9Sstevel@tonic-gate thread_start(void *arg)
2587c478bd9Sstevel@tonic-gate {
2597c478bd9Sstevel@tonic-gate 	thread_info_t *ti = arg;
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate 	(void) pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&thread_lock);
2647c478bd9Sstevel@tonic-gate 	num_started++;
2657c478bd9Sstevel@tonic-gate 	(void) uu_list_insert_after(thread_list, uu_list_last(thread_list),
2667c478bd9Sstevel@tonic-gate 	    ti);
2677c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&thread_lock);
2687c478bd9Sstevel@tonic-gate 	(void) pthread_setspecific(thread_info_key, ti);
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate 	thread_newstate(ti, TI_DOOR_RETURN);
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate 	/*
2737c478bd9Sstevel@tonic-gate 	 * Start handling door calls
2747c478bd9Sstevel@tonic-gate 	 */
2757c478bd9Sstevel@tonic-gate 	(void) door_return(NULL, 0, NULL, 0);
2767c478bd9Sstevel@tonic-gate 	return (arg);
2777c478bd9Sstevel@tonic-gate }
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate static void
2807c478bd9Sstevel@tonic-gate new_thread_needed(door_info_t *dip)
2817c478bd9Sstevel@tonic-gate {
2827c478bd9Sstevel@tonic-gate 	thread_info_t *ti;
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate 	sigset_t new, old;
2857c478bd9Sstevel@tonic-gate 
2867c478bd9Sstevel@tonic-gate 	assert(dip == NULL);
2877c478bd9Sstevel@tonic-gate 
2887c478bd9Sstevel@tonic-gate 	if (!reserve_new_thread())
2897c478bd9Sstevel@tonic-gate 		return;
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate 	if ((ti = uu_zalloc(sizeof (*ti))) == NULL)
2927c478bd9Sstevel@tonic-gate 		goto fail;
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate 	uu_list_node_init(ti, &ti->ti_node, thread_pool);
2957c478bd9Sstevel@tonic-gate 	ti->ti_state = TI_CREATED;
2967c478bd9Sstevel@tonic-gate 	ti->ti_prev_state = TI_CREATED;
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate 	if ((ti->ti_ucred = uu_zalloc(ucred_size())) == NULL)
2997c478bd9Sstevel@tonic-gate 		goto fail;
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate 	(void) sigfillset(&new);
3027c478bd9Sstevel@tonic-gate 	(void) pthread_sigmask(SIG_SETMASK, &new, &old);
3037c478bd9Sstevel@tonic-gate 	if ((errno = pthread_create(&ti->ti_thread, &thread_attr, thread_start,
3047c478bd9Sstevel@tonic-gate 	    ti)) != 0) {
3057c478bd9Sstevel@tonic-gate 		(void) pthread_sigmask(SIG_SETMASK, &old, NULL);
3067c478bd9Sstevel@tonic-gate 		goto fail;
3077c478bd9Sstevel@tonic-gate 	}
3087c478bd9Sstevel@tonic-gate 
3097c478bd9Sstevel@tonic-gate 	(void) pthread_sigmask(SIG_SETMASK, &old, NULL);
3107c478bd9Sstevel@tonic-gate 	return;
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate fail:
3137c478bd9Sstevel@tonic-gate 	/*
3147c478bd9Sstevel@tonic-gate 	 * Since the thread_info structure was never linked onto the
3157c478bd9Sstevel@tonic-gate 	 * thread list, thread_exiting() can't handle the cleanup.
3167c478bd9Sstevel@tonic-gate 	 */
3177c478bd9Sstevel@tonic-gate 	thread_exiting(NULL);
3187c478bd9Sstevel@tonic-gate 	if (ti != NULL)
3197c478bd9Sstevel@tonic-gate 		thread_info_free(ti);
3207c478bd9Sstevel@tonic-gate }
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate int
3237c478bd9Sstevel@tonic-gate create_connection(ucred_t *uc, repository_door_request_t *rp,
3247c478bd9Sstevel@tonic-gate     size_t rp_size, int *out_fd)
3257c478bd9Sstevel@tonic-gate {
3267c478bd9Sstevel@tonic-gate 	int flags;
3277c478bd9Sstevel@tonic-gate 	int privileged = 0;
3287c478bd9Sstevel@tonic-gate 	uint32_t debugflags = 0;
3297c478bd9Sstevel@tonic-gate 	psinfo_t info;
3307c478bd9Sstevel@tonic-gate 
3317c478bd9Sstevel@tonic-gate 	if (privileged_pid != 0) {
3327c478bd9Sstevel@tonic-gate 		/*
3337c478bd9Sstevel@tonic-gate 		 * in privileged pid mode, we only allow connections from
3347c478bd9Sstevel@tonic-gate 		 * our original parent -- the psinfo read verifies that
3357c478bd9Sstevel@tonic-gate 		 * it is the same process which we started with.
3367c478bd9Sstevel@tonic-gate 		 */
3377c478bd9Sstevel@tonic-gate 		if (ucred_getpid(uc) != privileged_pid ||
3387c478bd9Sstevel@tonic-gate 		    read(privileged_psinfo_fd, &info, sizeof (info)) !=
3397c478bd9Sstevel@tonic-gate 		    sizeof (info))
3407c478bd9Sstevel@tonic-gate 			return (REPOSITORY_DOOR_FAIL_PERMISSION_DENIED);
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate 		privileged = 1;			/* he gets full privileges */
3437c478bd9Sstevel@tonic-gate 	} else if (privileged_user != 0) {
3447c478bd9Sstevel@tonic-gate 		/*
3457c478bd9Sstevel@tonic-gate 		 * in privileged user mode, only one particular user is
3467c478bd9Sstevel@tonic-gate 		 * allowed to connect to us, and he can do anything.
3477c478bd9Sstevel@tonic-gate 		 */
3487c478bd9Sstevel@tonic-gate 		if (ucred_geteuid(uc) != privileged_user)
3497c478bd9Sstevel@tonic-gate 			return (REPOSITORY_DOOR_FAIL_PERMISSION_DENIED);
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate 		privileged = 1;
3527c478bd9Sstevel@tonic-gate 	}
3537c478bd9Sstevel@tonic-gate 
3547c478bd9Sstevel@tonic-gate 	/*
3557c478bd9Sstevel@tonic-gate 	 * Check that rp, of size rp_size, is large enough to
3567c478bd9Sstevel@tonic-gate 	 * contain field 'f'.  If so, write the value into *out, and return 1.
3577c478bd9Sstevel@tonic-gate 	 * Otherwise, return 0.
3587c478bd9Sstevel@tonic-gate 	 */
3597c478bd9Sstevel@tonic-gate #define	GET_ARG(rp, rp_size, f, out)					\
3607c478bd9Sstevel@tonic-gate 	(((rp_size) >= offsetofend(repository_door_request_t, f)) ?	\
3617c478bd9Sstevel@tonic-gate 	    ((*(out) = (rp)->f), 1) : 0)
3627c478bd9Sstevel@tonic-gate 
3637c478bd9Sstevel@tonic-gate 	if (!GET_ARG(rp, rp_size, rdr_flags, &flags))
3647c478bd9Sstevel@tonic-gate 		return (REPOSITORY_DOOR_FAIL_BAD_REQUEST);
3657c478bd9Sstevel@tonic-gate 
3667c478bd9Sstevel@tonic-gate #if (REPOSITORY_DOOR_FLAG_ALL != REPOSITORY_DOOR_FLAG_DEBUG)
3677c478bd9Sstevel@tonic-gate #error Need to update flag checks
3687c478bd9Sstevel@tonic-gate #endif
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate 	if (flags & ~REPOSITORY_DOOR_FLAG_ALL)
3717c478bd9Sstevel@tonic-gate 		return (REPOSITORY_DOOR_FAIL_BAD_FLAG);
3727c478bd9Sstevel@tonic-gate 
3737c478bd9Sstevel@tonic-gate 	if (flags & REPOSITORY_DOOR_FLAG_DEBUG)
3747c478bd9Sstevel@tonic-gate 		if (!GET_ARG(rp, rp_size, rdr_debug, &debugflags))
3757c478bd9Sstevel@tonic-gate 			return (REPOSITORY_DOOR_FAIL_BAD_REQUEST);
3767c478bd9Sstevel@tonic-gate #undef GET_ARG
3777c478bd9Sstevel@tonic-gate 
3787c478bd9Sstevel@tonic-gate 	return (create_client(ucred_getpid(uc), debugflags, privileged,
3797c478bd9Sstevel@tonic-gate 	    out_fd));
3807c478bd9Sstevel@tonic-gate }
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate void
383*6e1d2b42Samaguire configd_vlog(int severity, const char *prefix, const char *message,
384*6e1d2b42Samaguire     va_list args)
3857c478bd9Sstevel@tonic-gate {
3867c478bd9Sstevel@tonic-gate 	if (log_to_syslog)
387*6e1d2b42Samaguire 		vsyslog(severity, message, args);
3887c478bd9Sstevel@tonic-gate 	else {
3897c478bd9Sstevel@tonic-gate 		flockfile(stderr);
390*6e1d2b42Samaguire 		if (prefix != NULL)
391*6e1d2b42Samaguire 			(void) fprintf(stderr, "%s", prefix);
3927c478bd9Sstevel@tonic-gate 		(void) vfprintf(stderr, message, args);
3937c478bd9Sstevel@tonic-gate 		if (message[0] == 0 || message[strlen(message) - 1] != '\n')
3947c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "\n");
3957c478bd9Sstevel@tonic-gate 		funlockfile(stderr);
3967c478bd9Sstevel@tonic-gate 	}
3977c478bd9Sstevel@tonic-gate }
3987c478bd9Sstevel@tonic-gate 
3997c478bd9Sstevel@tonic-gate void
400*6e1d2b42Samaguire configd_vcritical(const char *message, va_list args)
401*6e1d2b42Samaguire {
402*6e1d2b42Samaguire 	configd_vlog(LOG_CRIT, "svc.configd: Fatal error: ", message, args);
403*6e1d2b42Samaguire }
404*6e1d2b42Samaguire 
405*6e1d2b42Samaguire void
4067c478bd9Sstevel@tonic-gate configd_critical(const char *message, ...)
4077c478bd9Sstevel@tonic-gate {
4087c478bd9Sstevel@tonic-gate 	va_list args;
4097c478bd9Sstevel@tonic-gate 	va_start(args, message);
4107c478bd9Sstevel@tonic-gate 	configd_vcritical(message, args);
4117c478bd9Sstevel@tonic-gate 	va_end(args);
4127c478bd9Sstevel@tonic-gate }
4137c478bd9Sstevel@tonic-gate 
414*6e1d2b42Samaguire void
415*6e1d2b42Samaguire configd_info(const char *message, ...)
416*6e1d2b42Samaguire {
417*6e1d2b42Samaguire 	va_list args;
418*6e1d2b42Samaguire 	va_start(args, message);
419*6e1d2b42Samaguire 	configd_vlog(LOG_INFO, "svc.configd: ", message, args);
420*6e1d2b42Samaguire 	va_end(args);
421*6e1d2b42Samaguire }
422*6e1d2b42Samaguire 
4237c478bd9Sstevel@tonic-gate static void
4247c478bd9Sstevel@tonic-gate usage(const char *prog, int ret)
4257c478bd9Sstevel@tonic-gate {
4267c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
4277c478bd9Sstevel@tonic-gate 	    "usage: %s [-np] [-d door_path] [-r repository_path]\n"
4287c478bd9Sstevel@tonic-gate 	    "    [-t nonpersist_repository]\n", prog);
4297c478bd9Sstevel@tonic-gate 	exit(ret);
4307c478bd9Sstevel@tonic-gate }
4317c478bd9Sstevel@tonic-gate 
4327c478bd9Sstevel@tonic-gate /*ARGSUSED*/
4337c478bd9Sstevel@tonic-gate static void
4347c478bd9Sstevel@tonic-gate handler(int sig, siginfo_t *info, void *data)
4357c478bd9Sstevel@tonic-gate {
4367c478bd9Sstevel@tonic-gate 	finished = 1;
4377c478bd9Sstevel@tonic-gate }
4387c478bd9Sstevel@tonic-gate 
4397c478bd9Sstevel@tonic-gate static int pipe_fd = -1;
4407c478bd9Sstevel@tonic-gate 
4417c478bd9Sstevel@tonic-gate static int
4427c478bd9Sstevel@tonic-gate daemonize_start(void)
4437c478bd9Sstevel@tonic-gate {
4447c478bd9Sstevel@tonic-gate 	char data;
4457c478bd9Sstevel@tonic-gate 	int status;
4467c478bd9Sstevel@tonic-gate 
4477c478bd9Sstevel@tonic-gate 	int filedes[2];
4487c478bd9Sstevel@tonic-gate 	pid_t pid;
4497c478bd9Sstevel@tonic-gate 
4507c478bd9Sstevel@tonic-gate 	(void) close(0);
4517c478bd9Sstevel@tonic-gate 	(void) dup2(2, 1);		/* stderr only */
4527c478bd9Sstevel@tonic-gate 
4537c478bd9Sstevel@tonic-gate 	if (pipe(filedes) < 0)
4547c478bd9Sstevel@tonic-gate 		return (-1);
4557c478bd9Sstevel@tonic-gate 
4567c478bd9Sstevel@tonic-gate 	if ((pid = fork1()) < 0)
4577c478bd9Sstevel@tonic-gate 		return (-1);
4587c478bd9Sstevel@tonic-gate 
4597c478bd9Sstevel@tonic-gate 	if (pid != 0) {
4607c478bd9Sstevel@tonic-gate 		/*
4617c478bd9Sstevel@tonic-gate 		 * parent
4627c478bd9Sstevel@tonic-gate 		 */
4637c478bd9Sstevel@tonic-gate 		struct sigaction act;
4647c478bd9Sstevel@tonic-gate 
4657c478bd9Sstevel@tonic-gate 		act.sa_sigaction = SIG_DFL;
4667c478bd9Sstevel@tonic-gate 		(void) sigemptyset(&act.sa_mask);
4677c478bd9Sstevel@tonic-gate 		act.sa_flags = 0;
4687c478bd9Sstevel@tonic-gate 
4697c478bd9Sstevel@tonic-gate 		(void) sigaction(SIGPIPE, &act, NULL);	/* ignore SIGPIPE */
4707c478bd9Sstevel@tonic-gate 
4717c478bd9Sstevel@tonic-gate 		(void) close(filedes[1]);
4727c478bd9Sstevel@tonic-gate 		if (read(filedes[0], &data, 1) == 1) {
4737c478bd9Sstevel@tonic-gate 			/* presume success */
4747c478bd9Sstevel@tonic-gate 			_exit(CONFIGD_EXIT_OKAY);
4757c478bd9Sstevel@tonic-gate 		}
4767c478bd9Sstevel@tonic-gate 
4777c478bd9Sstevel@tonic-gate 		status = -1;
4787c478bd9Sstevel@tonic-gate 		(void) wait4(pid, &status, 0, NULL);
4797c478bd9Sstevel@tonic-gate 		if (WIFEXITED(status))
4807c478bd9Sstevel@tonic-gate 			_exit(WEXITSTATUS(status));
4817c478bd9Sstevel@tonic-gate 		else
4827c478bd9Sstevel@tonic-gate 			_exit(-1);
4837c478bd9Sstevel@tonic-gate 	}
4847c478bd9Sstevel@tonic-gate 
4857c478bd9Sstevel@tonic-gate 	/*
4867c478bd9Sstevel@tonic-gate 	 * child
4877c478bd9Sstevel@tonic-gate 	 */
4887c478bd9Sstevel@tonic-gate 	pipe_fd = filedes[1];
4897c478bd9Sstevel@tonic-gate 	(void) close(filedes[0]);
4907c478bd9Sstevel@tonic-gate 
4917c478bd9Sstevel@tonic-gate 	/*
4927c478bd9Sstevel@tonic-gate 	 * generic Unix setup
4937c478bd9Sstevel@tonic-gate 	 */
4947c478bd9Sstevel@tonic-gate 	(void) setsid();
4957c478bd9Sstevel@tonic-gate 	(void) umask(0077);
4967c478bd9Sstevel@tonic-gate 
4977c478bd9Sstevel@tonic-gate 	return (0);
4987c478bd9Sstevel@tonic-gate }
4997c478bd9Sstevel@tonic-gate 
5007c478bd9Sstevel@tonic-gate static void
5017c478bd9Sstevel@tonic-gate daemonize_ready(void)
5027c478bd9Sstevel@tonic-gate {
5037c478bd9Sstevel@tonic-gate 	char data = '\0';
5047c478bd9Sstevel@tonic-gate 
5057c478bd9Sstevel@tonic-gate 	/*
5067c478bd9Sstevel@tonic-gate 	 * wake the parent
5077c478bd9Sstevel@tonic-gate 	 */
5087c478bd9Sstevel@tonic-gate 	(void) write(pipe_fd, &data, 1);
5097c478bd9Sstevel@tonic-gate 	(void) close(pipe_fd);
5107c478bd9Sstevel@tonic-gate }
5117c478bd9Sstevel@tonic-gate 
5128918dff3Sjwadams const char *
5138918dff3Sjwadams regularize_path(const char *dir, const char *base, char *tmpbuf)
5148918dff3Sjwadams {
5158918dff3Sjwadams 	if (base == NULL)
5168918dff3Sjwadams 		return (NULL);
5178918dff3Sjwadams 	if (base[0] == '/')
5188918dff3Sjwadams 		return (base);
5198918dff3Sjwadams 
5208918dff3Sjwadams 	if (snprintf(tmpbuf, PATH_MAX, "%s/%s", dir, base) >= PATH_MAX) {
5218918dff3Sjwadams 		(void) fprintf(stderr, "svc.configd: %s/%s: path too long\n",
5228918dff3Sjwadams 		    dir, base);
5238918dff3Sjwadams 		exit(CONFIGD_EXIT_BAD_ARGS);
5248918dff3Sjwadams 	}
5258918dff3Sjwadams 
5268918dff3Sjwadams 	return (tmpbuf);
5278918dff3Sjwadams }
5288918dff3Sjwadams 
5297c478bd9Sstevel@tonic-gate int
5307c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
5317c478bd9Sstevel@tonic-gate {
5327c478bd9Sstevel@tonic-gate 	thread_info_t *ti = &main_thread_info;
5337c478bd9Sstevel@tonic-gate 
5347c478bd9Sstevel@tonic-gate 	char pidpath[sizeof ("/proc/" "/psinfo") + 10];
5357c478bd9Sstevel@tonic-gate 
5367c478bd9Sstevel@tonic-gate 	struct rlimit fd_new;
5377c478bd9Sstevel@tonic-gate 
5387c478bd9Sstevel@tonic-gate 	const char *endptr;
5397c478bd9Sstevel@tonic-gate 	sigset_t myset;
5407c478bd9Sstevel@tonic-gate 	int c;
5417c478bd9Sstevel@tonic-gate 	int ret;
5427c478bd9Sstevel@tonic-gate 	int fd;
5438918dff3Sjwadams 
5448918dff3Sjwadams 	char curdir[PATH_MAX];
5458918dff3Sjwadams 	char dbtmp[PATH_MAX];
5468918dff3Sjwadams 	char npdbtmp[PATH_MAX];
5478918dff3Sjwadams 	char doortmp[PATH_MAX];
5488918dff3Sjwadams 
5497c478bd9Sstevel@tonic-gate 	const char *dbpath = NULL;
5507c478bd9Sstevel@tonic-gate 	const char *npdbpath = NULL;
5517c478bd9Sstevel@tonic-gate 	const char *doorpath = REPOSITORY_DOOR_NAME;
5527c478bd9Sstevel@tonic-gate 	struct sigaction act;
5537c478bd9Sstevel@tonic-gate 
5547c478bd9Sstevel@tonic-gate 	int daemonize = 1;		/* default to daemonizing */
5557c478bd9Sstevel@tonic-gate 	int have_npdb = 1;
5567c478bd9Sstevel@tonic-gate 
5577c478bd9Sstevel@tonic-gate 	closefrom(3);			/* get rid of extraneous fds */
5587c478bd9Sstevel@tonic-gate 
5598918dff3Sjwadams 	if (getcwd(curdir, sizeof (curdir)) == NULL) {
5608918dff3Sjwadams 		(void) fprintf(stderr,
5618918dff3Sjwadams 		    "%s: unable to get current directory: %s\n",
5628918dff3Sjwadams 		    argv[0], strerror(errno));
5638918dff3Sjwadams 		exit(CONFIGD_EXIT_INIT_FAILED);
5648918dff3Sjwadams 	}
5658918dff3Sjwadams 
5667c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "Dnpd:r:t:")) != -1) {
5677c478bd9Sstevel@tonic-gate 		switch (c) {
5687c478bd9Sstevel@tonic-gate 		case 'n':
5697c478bd9Sstevel@tonic-gate 			daemonize = 0;
5707c478bd9Sstevel@tonic-gate 			break;
5717c478bd9Sstevel@tonic-gate 		case 'd':
5728918dff3Sjwadams 			doorpath = regularize_path(curdir, optarg, doortmp);
5737c478bd9Sstevel@tonic-gate 			have_npdb = 0;		/* default to no non-persist */
5747c478bd9Sstevel@tonic-gate 			break;
5757c478bd9Sstevel@tonic-gate 		case 'p':
5767c478bd9Sstevel@tonic-gate 			log_to_syslog = 0;	/* don't use syslog */
5777c478bd9Sstevel@tonic-gate 
5787c478bd9Sstevel@tonic-gate 			/*
5797c478bd9Sstevel@tonic-gate 			 * If our parent exits while we're opening its /proc
5807c478bd9Sstevel@tonic-gate 			 * psinfo, we're vulnerable to a pid wrapping.  To
5817c478bd9Sstevel@tonic-gate 			 * protect against that, re-check our ppid after
5827c478bd9Sstevel@tonic-gate 			 * opening it.
5837c478bd9Sstevel@tonic-gate 			 */
5847c478bd9Sstevel@tonic-gate 			privileged_pid = getppid();
5857c478bd9Sstevel@tonic-gate 			(void) snprintf(pidpath, sizeof (pidpath),
5867c478bd9Sstevel@tonic-gate 			    "/proc/%d/psinfo", privileged_pid);
5877c478bd9Sstevel@tonic-gate 			if ((fd = open(pidpath, O_RDONLY)) < 0 ||
5887c478bd9Sstevel@tonic-gate 			    getppid() != privileged_pid) {
5897c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
5907c478bd9Sstevel@tonic-gate 				    "%s: unable to get parent info\n", argv[0]);
5917c478bd9Sstevel@tonic-gate 				exit(CONFIGD_EXIT_BAD_ARGS);
5927c478bd9Sstevel@tonic-gate 			}
5937c478bd9Sstevel@tonic-gate 			privileged_psinfo_fd = fd;
5947c478bd9Sstevel@tonic-gate 			break;
5957c478bd9Sstevel@tonic-gate 		case 'r':
5968918dff3Sjwadams 			dbpath = regularize_path(curdir, optarg, dbtmp);
5977c478bd9Sstevel@tonic-gate 			is_main_repository = 0;
5987c478bd9Sstevel@tonic-gate 			break;
5997c478bd9Sstevel@tonic-gate 		case 't':
6008918dff3Sjwadams 			npdbpath = regularize_path(curdir, optarg, npdbtmp);
6017c478bd9Sstevel@tonic-gate 			is_main_repository = 0;
6027c478bd9Sstevel@tonic-gate 			break;
6037c478bd9Sstevel@tonic-gate 		default:
6047c478bd9Sstevel@tonic-gate 			usage(argv[0], CONFIGD_EXIT_BAD_ARGS);
6057c478bd9Sstevel@tonic-gate 			break;
6067c478bd9Sstevel@tonic-gate 		}
6077c478bd9Sstevel@tonic-gate 	}
6087c478bd9Sstevel@tonic-gate 
6097c478bd9Sstevel@tonic-gate 	/*
6107c478bd9Sstevel@tonic-gate 	 * If we're not running as root, allow our euid full access, and
6117c478bd9Sstevel@tonic-gate 	 * everyone else no access.
6127c478bd9Sstevel@tonic-gate 	 */
6137c478bd9Sstevel@tonic-gate 	if (privileged_pid == 0 && geteuid() != 0) {
6147c478bd9Sstevel@tonic-gate 		privileged_user = geteuid();
6157c478bd9Sstevel@tonic-gate 	}
6167c478bd9Sstevel@tonic-gate 
6177c478bd9Sstevel@tonic-gate 	privileged_privs = priv_str_to_set("zone", "", &endptr);
6187c478bd9Sstevel@tonic-gate 	if (endptr != NULL && privileged_privs != NULL) {
6197c478bd9Sstevel@tonic-gate 		priv_freeset(privileged_privs);
6207c478bd9Sstevel@tonic-gate 		privileged_privs = NULL;
6217c478bd9Sstevel@tonic-gate 	}
6227c478bd9Sstevel@tonic-gate 
6237c478bd9Sstevel@tonic-gate 	openlog("svc.configd", LOG_PID | LOG_CONS, LOG_DAEMON);
6247c478bd9Sstevel@tonic-gate 	(void) setlogmask(LOG_UPTO(LOG_NOTICE));
6257c478bd9Sstevel@tonic-gate 
6267c478bd9Sstevel@tonic-gate 	/*
6277c478bd9Sstevel@tonic-gate 	 * if a non-persist db is specified, always enable it
6287c478bd9Sstevel@tonic-gate 	 */
6297c478bd9Sstevel@tonic-gate 	if (npdbpath)
6307c478bd9Sstevel@tonic-gate 		have_npdb = 1;
6317c478bd9Sstevel@tonic-gate 
6327c478bd9Sstevel@tonic-gate 	if (optind != argc)
6337c478bd9Sstevel@tonic-gate 		usage(argv[0], CONFIGD_EXIT_BAD_ARGS);
6347c478bd9Sstevel@tonic-gate 
6357c478bd9Sstevel@tonic-gate 	if (daemonize) {
6367c478bd9Sstevel@tonic-gate 		if (getuid() == 0)
6377c478bd9Sstevel@tonic-gate 			(void) chdir("/");
6387c478bd9Sstevel@tonic-gate 		if (daemonize_start() < 0) {
6397c478bd9Sstevel@tonic-gate 			(void) perror("unable to daemonize");
6407c478bd9Sstevel@tonic-gate 			exit(CONFIGD_EXIT_INIT_FAILED);
6417c478bd9Sstevel@tonic-gate 		}
6427c478bd9Sstevel@tonic-gate 	}
6437c478bd9Sstevel@tonic-gate 	if (getuid() == 0)
6447c478bd9Sstevel@tonic-gate 		(void) core_set_process_path(CONFIGD_CORE,
6457c478bd9Sstevel@tonic-gate 		    strlen(CONFIGD_CORE) + 1, getpid());
6467c478bd9Sstevel@tonic-gate 
6477c478bd9Sstevel@tonic-gate 	/*
6487c478bd9Sstevel@tonic-gate 	 * this should be enabled once we can drop privileges and still get
6497c478bd9Sstevel@tonic-gate 	 * a core dump.
6507c478bd9Sstevel@tonic-gate 	 */
6517c478bd9Sstevel@tonic-gate #if 0
6527c478bd9Sstevel@tonic-gate 	/* turn off basic privileges we do not need */
6537c478bd9Sstevel@tonic-gate 	(void) priv_set(PRIV_OFF, PRIV_PERMITTED, PRIV_FILE_LINK_ANY,
6547c478bd9Sstevel@tonic-gate 	    PRIV_PROC_EXEC, PRIV_PROC_FORK, PRIV_PROC_SESSION, NULL);
6557c478bd9Sstevel@tonic-gate #endif
6567c478bd9Sstevel@tonic-gate 
6577c478bd9Sstevel@tonic-gate 	/* not that we can exec, but to be safe, shut them all off... */
6587c478bd9Sstevel@tonic-gate 	(void) priv_set(PRIV_SET, PRIV_INHERITABLE, NULL);
6597c478bd9Sstevel@tonic-gate 
6607c478bd9Sstevel@tonic-gate 	(void) sigfillset(&act.sa_mask);
6617c478bd9Sstevel@tonic-gate 
6627c478bd9Sstevel@tonic-gate 	/* signals to ignore */
6637c478bd9Sstevel@tonic-gate 	act.sa_sigaction = SIG_IGN;
6647c478bd9Sstevel@tonic-gate 	act.sa_flags = 0;
6657c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGPIPE, &act, NULL);
6667c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGALRM, &act, NULL);
6677c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGUSR1, &act, NULL);
6687c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGUSR2, &act, NULL);
6697c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGPOLL, &act, NULL);
6707c478bd9Sstevel@tonic-gate 
6717c478bd9Sstevel@tonic-gate 	/* signals to abort on */
6727c478bd9Sstevel@tonic-gate 	act.sa_sigaction = (void (*)(int, siginfo_t *, void *))&abort_handler;
6737c478bd9Sstevel@tonic-gate 	act.sa_flags = SA_SIGINFO;
6747c478bd9Sstevel@tonic-gate 
6757c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGABRT, &act, NULL);
6767c478bd9Sstevel@tonic-gate 
6777c478bd9Sstevel@tonic-gate 	/* signals to handle */
6787c478bd9Sstevel@tonic-gate 	act.sa_sigaction = &handler;
6797c478bd9Sstevel@tonic-gate 	act.sa_flags = SA_SIGINFO;
6807c478bd9Sstevel@tonic-gate 
6817c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGHUP, &act, NULL);
6827c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGINT, &act, NULL);
6837c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGTERM, &act, NULL);
6847c478bd9Sstevel@tonic-gate 
6857c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&myset);
6867c478bd9Sstevel@tonic-gate 	(void) sigaddset(&myset, SIGHUP);
6877c478bd9Sstevel@tonic-gate 	(void) sigaddset(&myset, SIGINT);
6887c478bd9Sstevel@tonic-gate 	(void) sigaddset(&myset, SIGTERM);
6897c478bd9Sstevel@tonic-gate 
6907c478bd9Sstevel@tonic-gate 	if ((errno = pthread_attr_init(&thread_attr)) != 0) {
6917c478bd9Sstevel@tonic-gate 		(void) perror("initializing");
6927c478bd9Sstevel@tonic-gate 		exit(CONFIGD_EXIT_INIT_FAILED);
6937c478bd9Sstevel@tonic-gate 	}
6947c478bd9Sstevel@tonic-gate 
6957c478bd9Sstevel@tonic-gate 	/*
6967c478bd9Sstevel@tonic-gate 	 * Set the hard and soft limits to CONFIGD_MAX_FDS.
6977c478bd9Sstevel@tonic-gate 	 */
6987c478bd9Sstevel@tonic-gate 	fd_new.rlim_max = fd_new.rlim_cur = CONFIGD_MAX_FDS;
6997c478bd9Sstevel@tonic-gate 	(void) setrlimit(RLIMIT_NOFILE, &fd_new);
7007c478bd9Sstevel@tonic-gate 
701004388ebScasper #ifndef NATIVE_BUILD /* Allow building on snv_38 and earlier; remove later. */
702004388ebScasper 	(void) enable_extended_FILE_stdio(-1, -1);
703004388ebScasper #endif
704004388ebScasper 
7057c478bd9Sstevel@tonic-gate 	if ((ret = backend_init(dbpath, npdbpath, have_npdb)) !=
7067c478bd9Sstevel@tonic-gate 	    CONFIGD_EXIT_OKAY)
7077c478bd9Sstevel@tonic-gate 		exit(ret);
7087c478bd9Sstevel@tonic-gate 
7097c478bd9Sstevel@tonic-gate 	if (!client_init())
7107c478bd9Sstevel@tonic-gate 		exit(CONFIGD_EXIT_INIT_FAILED);
7117c478bd9Sstevel@tonic-gate 
7127c478bd9Sstevel@tonic-gate 	if (!rc_node_init())
7137c478bd9Sstevel@tonic-gate 		exit(CONFIGD_EXIT_INIT_FAILED);
7147c478bd9Sstevel@tonic-gate 
7157c478bd9Sstevel@tonic-gate 	(void) pthread_attr_setdetachstate(&thread_attr,
7167c478bd9Sstevel@tonic-gate 	    PTHREAD_CREATE_DETACHED);
7177c478bd9Sstevel@tonic-gate 	(void) pthread_attr_setscope(&thread_attr, PTHREAD_SCOPE_SYSTEM);
7187c478bd9Sstevel@tonic-gate 
7197c478bd9Sstevel@tonic-gate 	if ((errno = pthread_key_create(&thread_info_key,
7207c478bd9Sstevel@tonic-gate 	    thread_exiting)) != 0) {
7217c478bd9Sstevel@tonic-gate 		perror("pthread_key_create");
7227c478bd9Sstevel@tonic-gate 		exit(CONFIGD_EXIT_INIT_FAILED);
7237c478bd9Sstevel@tonic-gate 	}
7247c478bd9Sstevel@tonic-gate 
7257c478bd9Sstevel@tonic-gate 	if ((thread_pool = uu_list_pool_create("thread_pool",
7267c478bd9Sstevel@tonic-gate 	    sizeof (thread_info_t), offsetof(thread_info_t, ti_node),
7277c478bd9Sstevel@tonic-gate 	    NULL, UU_LIST_POOL_DEBUG)) == NULL) {
7287c478bd9Sstevel@tonic-gate 		configd_critical("uu_list_pool_create: %s\n",
7297c478bd9Sstevel@tonic-gate 		    uu_strerror(uu_error()));
7307c478bd9Sstevel@tonic-gate 		exit(CONFIGD_EXIT_INIT_FAILED);
7317c478bd9Sstevel@tonic-gate 	}
7327c478bd9Sstevel@tonic-gate 
7337c478bd9Sstevel@tonic-gate 	if ((thread_list = uu_list_create(thread_pool, NULL, 0)) == NULL) {
7347c478bd9Sstevel@tonic-gate 		configd_critical("uu_list_create: %s\n",
7357c478bd9Sstevel@tonic-gate 		    uu_strerror(uu_error()));
7367c478bd9Sstevel@tonic-gate 		exit(CONFIGD_EXIT_INIT_FAILED);
7377c478bd9Sstevel@tonic-gate 	}
7387c478bd9Sstevel@tonic-gate 
7397c478bd9Sstevel@tonic-gate 	(void) memset(ti, '\0', sizeof (*ti));
7407c478bd9Sstevel@tonic-gate 	uu_list_node_init(ti, &ti->ti_node, thread_pool);
7417c478bd9Sstevel@tonic-gate 	(void) uu_list_insert_before(thread_list, uu_list_first(thread_list),
7427c478bd9Sstevel@tonic-gate 	    ti);
7437c478bd9Sstevel@tonic-gate 
7447c478bd9Sstevel@tonic-gate 	ti->ti_thread = pthread_self();
7457c478bd9Sstevel@tonic-gate 	ti->ti_state = TI_SIGNAL_WAIT;
7467c478bd9Sstevel@tonic-gate 	ti->ti_prev_state = TI_SIGNAL_WAIT;
7477c478bd9Sstevel@tonic-gate 
7487c478bd9Sstevel@tonic-gate 	(void) pthread_setspecific(thread_info_key, ti);
7497c478bd9Sstevel@tonic-gate 
7507c478bd9Sstevel@tonic-gate 	(void) door_server_create(new_thread_needed);
7517c478bd9Sstevel@tonic-gate 
7527c478bd9Sstevel@tonic-gate 	if (!setup_main_door(doorpath)) {
7537c478bd9Sstevel@tonic-gate 		configd_critical("Setting up main door failed.\n");
7547c478bd9Sstevel@tonic-gate 		exit(CONFIGD_EXIT_DOOR_INIT_FAILED);
7557c478bd9Sstevel@tonic-gate 	}
7567c478bd9Sstevel@tonic-gate 
7577c478bd9Sstevel@tonic-gate 	if (daemonize)
7587c478bd9Sstevel@tonic-gate 		daemonize_ready();
7597c478bd9Sstevel@tonic-gate 
7607c478bd9Sstevel@tonic-gate 	(void) pthread_sigmask(SIG_BLOCK, &myset, NULL);
7617c478bd9Sstevel@tonic-gate 	while (!finished) {
7627c478bd9Sstevel@tonic-gate 		int sig = sigwait(&myset);
7637c478bd9Sstevel@tonic-gate 		if (sig > 0) {
7647c478bd9Sstevel@tonic-gate 			break;
7657c478bd9Sstevel@tonic-gate 		}
7667c478bd9Sstevel@tonic-gate 	}
7677c478bd9Sstevel@tonic-gate 
7687c478bd9Sstevel@tonic-gate 	backend_fini();
7697c478bd9Sstevel@tonic-gate 
7707c478bd9Sstevel@tonic-gate 	return (CONFIGD_EXIT_OKAY);
7717c478bd9Sstevel@tonic-gate }
772