xref: /illumos-gate/usr/src/cmd/svc/configd/configd.c (revision 30699046)
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 /*
225b7f77adStw  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
263dd94f79SBryan Cantrill /*
273dd94f79SBryan Cantrill  * Copyright (c) 2012, Joyent, Inc. All rights reserved.
2848bbca81SDaniel Hoffman  * Copyright (c) 2016 by Delphix. All rights reserved.
293dd94f79SBryan Cantrill  */
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #include <assert.h>
327c478bd9Sstevel@tonic-gate #include <door.h>
337c478bd9Sstevel@tonic-gate #include <errno.h>
347c478bd9Sstevel@tonic-gate #include <fcntl.h>
358918dff3Sjwadams #include <limits.h>
367c478bd9Sstevel@tonic-gate #include <priv.h>
377c478bd9Sstevel@tonic-gate #include <procfs.h>
387c478bd9Sstevel@tonic-gate #include <pthread.h>
397c478bd9Sstevel@tonic-gate #include <signal.h>
407c478bd9Sstevel@tonic-gate #include <stdarg.h>
417c478bd9Sstevel@tonic-gate #include <stdio.h>
42004388ebScasper #include <stdio_ext.h>
437c478bd9Sstevel@tonic-gate #include <stdlib.h>
447c478bd9Sstevel@tonic-gate #include <string.h>
457c478bd9Sstevel@tonic-gate #include <syslog.h>
467c478bd9Sstevel@tonic-gate #include <sys/corectl.h>
477c478bd9Sstevel@tonic-gate #include <sys/resource.h>
487c478bd9Sstevel@tonic-gate #include <sys/stat.h>
497c478bd9Sstevel@tonic-gate #include <sys/wait.h>
507c478bd9Sstevel@tonic-gate #include <ucontext.h>
517c478bd9Sstevel@tonic-gate #include <unistd.h>
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate #include "configd.h"
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate /*
567c478bd9Sstevel@tonic-gate  * This file manages the overall startup and shutdown of configd, as well
577c478bd9Sstevel@tonic-gate  * as managing its door thread pool and per-thread datastructures.
587c478bd9Sstevel@tonic-gate  *
597c478bd9Sstevel@tonic-gate  * 1.  Per-thread Datastructures
607c478bd9Sstevel@tonic-gate  * -----------------------------
617c478bd9Sstevel@tonic-gate  * Each configd thread has an associated thread_info_t which contains its
627c478bd9Sstevel@tonic-gate  * current state.  A pointer is kept to this in TSD, keyed by thread_info_key.
637c478bd9Sstevel@tonic-gate  * The thread_info_ts for all threads in configd are kept on a single global
647c478bd9Sstevel@tonic-gate  * list, thread_list.  After creation, the state in the thread_info structure
657c478bd9Sstevel@tonic-gate  * is only modified by the associated thread, so no locking is needed.  A TSD
667c478bd9Sstevel@tonic-gate  * destructor removes the thread_info from the global list and frees it at
677c478bd9Sstevel@tonic-gate  * pthread_exit() time.
687c478bd9Sstevel@tonic-gate  *
697c478bd9Sstevel@tonic-gate  * Threads access their per-thread data using thread_self()
707c478bd9Sstevel@tonic-gate  *
717c478bd9Sstevel@tonic-gate  * The thread_list is protected by thread_lock, a leaf lock.
727c478bd9Sstevel@tonic-gate  *
737c478bd9Sstevel@tonic-gate  * 2. Door Thread Pool Management
747c478bd9Sstevel@tonic-gate  * ------------------------------
757c478bd9Sstevel@tonic-gate  * Whenever door_return(3door) returns from the kernel and there are no
767c478bd9Sstevel@tonic-gate  * other configd threads waiting for requests, libdoor automatically
777c478bd9Sstevel@tonic-gate  * invokes a function registered with door_server_create(), to request a new
787c478bd9Sstevel@tonic-gate  * door server thread.  The default function just creates a thread that calls
797c478bd9Sstevel@tonic-gate  * door_return(3door).  Unfortunately, since it can take a while for the new
807c478bd9Sstevel@tonic-gate  * thread to *get* to door_return(3door), a stream of requests can cause a
817c478bd9Sstevel@tonic-gate  * large number of threads to be created, even though they aren't all needed.
827c478bd9Sstevel@tonic-gate  *
837c478bd9Sstevel@tonic-gate  * In our callback, new_server_needed(), we limit ourself to two new threads
847c478bd9Sstevel@tonic-gate  * at a time -- this logic is handled in reserve_new_thread().  This keeps
857c478bd9Sstevel@tonic-gate  * us from creating an absurd number of threads in response to peaking load.
867c478bd9Sstevel@tonic-gate  */
877c478bd9Sstevel@tonic-gate static pthread_key_t	thread_info_key;
887c478bd9Sstevel@tonic-gate static pthread_attr_t	thread_attr;
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate static pthread_mutex_t	thread_lock = PTHREAD_MUTEX_INITIALIZER;
917c478bd9Sstevel@tonic-gate int			num_started;	/* number actually running */
927c478bd9Sstevel@tonic-gate int			num_servers;	/* number in-progress or running */
937c478bd9Sstevel@tonic-gate static uu_list_pool_t	*thread_pool;
947c478bd9Sstevel@tonic-gate uu_list_t		*thread_list;
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate static thread_info_t	main_thread_info;
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate static int	finished;
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate static pid_t	privileged_pid = 0;
1017c478bd9Sstevel@tonic-gate static int	privileged_psinfo_fd = -1;
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate static int	privileged_user = 0;
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate static priv_set_t *privileged_privs;
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate static int	log_to_syslog = 0;
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate int		is_main_repository = 1;
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate int		max_repository_backups = 4;
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate #define	CONFIGD_MAX_FDS		262144
1147c478bd9Sstevel@tonic-gate 
1153dd94f79SBryan Cantrill const char *
_umem_options_init(void)1163dd94f79SBryan Cantrill _umem_options_init(void)
1173dd94f79SBryan Cantrill {
1183dd94f79SBryan Cantrill 	/*
1193dd94f79SBryan Cantrill 	 * Like svc.startd, we set our UMEM_OPTIONS to indicate that we do not
1203dd94f79SBryan Cantrill 	 * wish to have per-CPU magazines to reduce our memory footprint.  And
1213dd94f79SBryan Cantrill 	 * as with svc.startd, if svc.configd is so MT-hot that this becomes a
1223dd94f79SBryan Cantrill 	 * scalability problem, there are deeper issues...
1233dd94f79SBryan Cantrill 	 */
1243dd94f79SBryan Cantrill 	return ("nomagazines");		/* UMEM_OPTIONS setting */
1253dd94f79SBryan Cantrill }
1263dd94f79SBryan Cantrill 
1277c478bd9Sstevel@tonic-gate /*
1287c478bd9Sstevel@tonic-gate  * Thanks, Mike
1297c478bd9Sstevel@tonic-gate  */
1307c478bd9Sstevel@tonic-gate void
abort_handler(int sig,siginfo_t * sip,ucontext_t * ucp)1317c478bd9Sstevel@tonic-gate abort_handler(int sig, siginfo_t *sip, ucontext_t *ucp)
1327c478bd9Sstevel@tonic-gate {
1337c478bd9Sstevel@tonic-gate 	struct sigaction act;
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&act.sa_mask);
1367c478bd9Sstevel@tonic-gate 	act.sa_handler = SIG_DFL;
1377c478bd9Sstevel@tonic-gate 	act.sa_flags = 0;
1387c478bd9Sstevel@tonic-gate 	(void) sigaction(sig, &act, NULL);
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate 	(void) printstack(2);
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate 	if (sip != NULL && SI_FROMUSER(sip))
1437c478bd9Sstevel@tonic-gate 		(void) pthread_kill(pthread_self(), sig);
1447c478bd9Sstevel@tonic-gate 	(void) sigfillset(&ucp->uc_sigmask);
1457c478bd9Sstevel@tonic-gate 	(void) sigdelset(&ucp->uc_sigmask, sig);
1467c478bd9Sstevel@tonic-gate 	ucp->uc_flags |= UC_SIGMASK;
1477c478bd9Sstevel@tonic-gate 	(void) setcontext(ucp);
1487c478bd9Sstevel@tonic-gate }
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate /*
1517c478bd9Sstevel@tonic-gate  * Don't want to have more than a couple thread creates outstanding
1527c478bd9Sstevel@tonic-gate  */
1537c478bd9Sstevel@tonic-gate static int
reserve_new_thread(void)1547c478bd9Sstevel@tonic-gate reserve_new_thread(void)
1557c478bd9Sstevel@tonic-gate {
1567c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&thread_lock);
1577c478bd9Sstevel@tonic-gate 	assert(num_started >= 0);
1587c478bd9Sstevel@tonic-gate 	if (num_servers > num_started + 1) {
1597c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_unlock(&thread_lock);
1607c478bd9Sstevel@tonic-gate 		return (0);
1617c478bd9Sstevel@tonic-gate 	}
1627c478bd9Sstevel@tonic-gate 	++num_servers;
1637c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&thread_lock);
1647c478bd9Sstevel@tonic-gate 	return (1);
1657c478bd9Sstevel@tonic-gate }
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate static void
thread_info_free(thread_info_t * ti)1687c478bd9Sstevel@tonic-gate thread_info_free(thread_info_t *ti)
1697c478bd9Sstevel@tonic-gate {
1707c478bd9Sstevel@tonic-gate 	uu_list_node_fini(ti, &ti->ti_node, thread_pool);
1717c478bd9Sstevel@tonic-gate 	if (ti->ti_ucred != NULL)
1727c478bd9Sstevel@tonic-gate 		uu_free(ti->ti_ucred);
1737c478bd9Sstevel@tonic-gate 	uu_free(ti);
1747c478bd9Sstevel@tonic-gate }
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate static void
thread_exiting(void * arg)1777c478bd9Sstevel@tonic-gate thread_exiting(void *arg)
1787c478bd9Sstevel@tonic-gate {
1797c478bd9Sstevel@tonic-gate 	thread_info_t *ti = arg;
1807c478bd9Sstevel@tonic-gate 
1818918dff3Sjwadams 	if (ti != NULL)
1828918dff3Sjwadams 		log_enter(&ti->ti_log);
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&thread_lock);
1857c478bd9Sstevel@tonic-gate 	if (ti != NULL) {
1867c478bd9Sstevel@tonic-gate 		num_started--;
1877c478bd9Sstevel@tonic-gate 		uu_list_remove(thread_list, ti);
1887c478bd9Sstevel@tonic-gate 	}
1897c478bd9Sstevel@tonic-gate 	assert(num_servers > 0);
1907c478bd9Sstevel@tonic-gate 	--num_servers;
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate 	if (num_servers == 0) {
1937c478bd9Sstevel@tonic-gate 		configd_critical("no door server threads\n");
1947c478bd9Sstevel@tonic-gate 		abort();
1957c478bd9Sstevel@tonic-gate 	}
1967c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&thread_lock);
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate 	if (ti != NULL && ti != &main_thread_info)
1997c478bd9Sstevel@tonic-gate 		thread_info_free(ti);
2007c478bd9Sstevel@tonic-gate }
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate void
thread_newstate(thread_info_t * ti,thread_state_t newstate)2037c478bd9Sstevel@tonic-gate thread_newstate(thread_info_t *ti, thread_state_t newstate)
2047c478bd9Sstevel@tonic-gate {
2057c478bd9Sstevel@tonic-gate 	ti->ti_ucred_read = 0;			/* invalidate cached ucred */
2067c478bd9Sstevel@tonic-gate 	if (newstate != ti->ti_state) {
2077c478bd9Sstevel@tonic-gate 		ti->ti_prev_state = ti->ti_state;
2087c478bd9Sstevel@tonic-gate 		ti->ti_state = newstate;
2097c478bd9Sstevel@tonic-gate 		ti->ti_lastchange = gethrtime();
2107c478bd9Sstevel@tonic-gate 	}
2117c478bd9Sstevel@tonic-gate }
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate thread_info_t *
thread_self(void)2147c478bd9Sstevel@tonic-gate thread_self(void)
2157c478bd9Sstevel@tonic-gate {
2167c478bd9Sstevel@tonic-gate 	return (pthread_getspecific(thread_info_key));
2177c478bd9Sstevel@tonic-gate }
2187c478bd9Sstevel@tonic-gate 
2195b7f77adStw /*
2205b7f77adStw  * get_ucred() returns NULL if it was unable to get the credential
2215b7f77adStw  * information.
2225b7f77adStw  */
2237c478bd9Sstevel@tonic-gate ucred_t *
get_ucred(void)2247c478bd9Sstevel@tonic-gate get_ucred(void)
2257c478bd9Sstevel@tonic-gate {
2267c478bd9Sstevel@tonic-gate 	thread_info_t *ti = thread_self();
2277c478bd9Sstevel@tonic-gate 	ucred_t **ret = &ti->ti_ucred;
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate 	if (ti->ti_ucred_read)
2307c478bd9Sstevel@tonic-gate 		return (*ret);			/* cached value */
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate 	if (door_ucred(ret) != 0)
2337c478bd9Sstevel@tonic-gate 		return (NULL);
2347c478bd9Sstevel@tonic-gate 	ti->ti_ucred_read = 1;
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate 	return (*ret);
2377c478bd9Sstevel@tonic-gate }
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate int
ucred_is_privileged(ucred_t * uc)2407c478bd9Sstevel@tonic-gate ucred_is_privileged(ucred_t *uc)
2417c478bd9Sstevel@tonic-gate {
2427c478bd9Sstevel@tonic-gate 	const priv_set_t *ps;
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate 	if ((ps = ucred_getprivset(uc, PRIV_EFFECTIVE)) != NULL) {
2457c478bd9Sstevel@tonic-gate 		if (priv_isfullset(ps))
2467c478bd9Sstevel@tonic-gate 			return (1);		/* process has all privs */
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate 		if (privileged_privs != NULL &&
2497c478bd9Sstevel@tonic-gate 		    priv_issubset(privileged_privs, ps))
2507c478bd9Sstevel@tonic-gate 			return (1);		/* process has zone privs */
2517c478bd9Sstevel@tonic-gate 	}
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate 	return (0);
2547c478bd9Sstevel@tonic-gate }
2557c478bd9Sstevel@tonic-gate 
2565b7f77adStw /*
2575b7f77adStw  * The purpose of this function is to get the audit session data for use in
2585b7f77adStw  * generating SMF audit events.  We use a single audit session per client.
2595b7f77adStw  *
2605b7f77adStw  * get_audit_session() may return NULL.  It is legal to use a NULL pointer
2615b7f77adStw  * in subsequent calls to adt_* functions.
2625b7f77adStw  */
2635b7f77adStw adt_session_data_t *
get_audit_session(void)2645b7f77adStw get_audit_session(void)
2655b7f77adStw {
2665b7f77adStw 	thread_info_t	*ti = thread_self();
2675b7f77adStw 
2685b7f77adStw 	return (ti->ti_active_client->rc_adt_session);
2695b7f77adStw }
2705b7f77adStw 
2717c478bd9Sstevel@tonic-gate static void *
thread_start(void * arg)2727c478bd9Sstevel@tonic-gate thread_start(void *arg)
2737c478bd9Sstevel@tonic-gate {
2747c478bd9Sstevel@tonic-gate 	thread_info_t *ti = arg;
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate 	(void) pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&thread_lock);
2797c478bd9Sstevel@tonic-gate 	num_started++;
2807c478bd9Sstevel@tonic-gate 	(void) uu_list_insert_after(thread_list, uu_list_last(thread_list),
2817c478bd9Sstevel@tonic-gate 	    ti);
2827c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&thread_lock);
2837c478bd9Sstevel@tonic-gate 	(void) pthread_setspecific(thread_info_key, ti);
2847c478bd9Sstevel@tonic-gate 
2857c478bd9Sstevel@tonic-gate 	thread_newstate(ti, TI_DOOR_RETURN);
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate 	/*
2887c478bd9Sstevel@tonic-gate 	 * Start handling door calls
2897c478bd9Sstevel@tonic-gate 	 */
2907c478bd9Sstevel@tonic-gate 	(void) door_return(NULL, 0, NULL, 0);
2917c478bd9Sstevel@tonic-gate 	return (arg);
2927c478bd9Sstevel@tonic-gate }
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate static void
new_thread_needed(door_info_t * dip)2957c478bd9Sstevel@tonic-gate new_thread_needed(door_info_t *dip)
2967c478bd9Sstevel@tonic-gate {
2977c478bd9Sstevel@tonic-gate 	thread_info_t *ti;
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate 	sigset_t new, old;
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate 	assert(dip == NULL);
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate 	if (!reserve_new_thread())
3047c478bd9Sstevel@tonic-gate 		return;
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate 	if ((ti = uu_zalloc(sizeof (*ti))) == NULL)
3077c478bd9Sstevel@tonic-gate 		goto fail;
3087c478bd9Sstevel@tonic-gate 
3097c478bd9Sstevel@tonic-gate 	uu_list_node_init(ti, &ti->ti_node, thread_pool);
3107c478bd9Sstevel@tonic-gate 	ti->ti_state = TI_CREATED;
3117c478bd9Sstevel@tonic-gate 	ti->ti_prev_state = TI_CREATED;
3127c478bd9Sstevel@tonic-gate 
3137c478bd9Sstevel@tonic-gate 	if ((ti->ti_ucred = uu_zalloc(ucred_size())) == NULL)
3147c478bd9Sstevel@tonic-gate 		goto fail;
3157c478bd9Sstevel@tonic-gate 
3167c478bd9Sstevel@tonic-gate 	(void) sigfillset(&new);
3177c478bd9Sstevel@tonic-gate 	(void) pthread_sigmask(SIG_SETMASK, &new, &old);
3187c478bd9Sstevel@tonic-gate 	if ((errno = pthread_create(&ti->ti_thread, &thread_attr, thread_start,
3197c478bd9Sstevel@tonic-gate 	    ti)) != 0) {
3207c478bd9Sstevel@tonic-gate 		(void) pthread_sigmask(SIG_SETMASK, &old, NULL);
3217c478bd9Sstevel@tonic-gate 		goto fail;
3227c478bd9Sstevel@tonic-gate 	}
3237c478bd9Sstevel@tonic-gate 
3247c478bd9Sstevel@tonic-gate 	(void) pthread_sigmask(SIG_SETMASK, &old, NULL);
3257c478bd9Sstevel@tonic-gate 	return;
3267c478bd9Sstevel@tonic-gate 
3277c478bd9Sstevel@tonic-gate fail:
3287c478bd9Sstevel@tonic-gate 	/*
3297c478bd9Sstevel@tonic-gate 	 * Since the thread_info structure was never linked onto the
3307c478bd9Sstevel@tonic-gate 	 * thread list, thread_exiting() can't handle the cleanup.
3317c478bd9Sstevel@tonic-gate 	 */
3327c478bd9Sstevel@tonic-gate 	thread_exiting(NULL);
3337c478bd9Sstevel@tonic-gate 	if (ti != NULL)
3347c478bd9Sstevel@tonic-gate 		thread_info_free(ti);
3357c478bd9Sstevel@tonic-gate }
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate int
create_connection(ucred_t * uc,repository_door_request_t * rp,size_t rp_size,int * out_fd)3387c478bd9Sstevel@tonic-gate create_connection(ucred_t *uc, repository_door_request_t *rp,
3397c478bd9Sstevel@tonic-gate     size_t rp_size, int *out_fd)
3407c478bd9Sstevel@tonic-gate {
3417c478bd9Sstevel@tonic-gate 	int flags;
3427c478bd9Sstevel@tonic-gate 	int privileged = 0;
3437c478bd9Sstevel@tonic-gate 	uint32_t debugflags = 0;
3447c478bd9Sstevel@tonic-gate 	psinfo_t info;
3457c478bd9Sstevel@tonic-gate 
3467c478bd9Sstevel@tonic-gate 	if (privileged_pid != 0) {
3477c478bd9Sstevel@tonic-gate 		/*
3487c478bd9Sstevel@tonic-gate 		 * in privileged pid mode, we only allow connections from
3497c478bd9Sstevel@tonic-gate 		 * our original parent -- the psinfo read verifies that
3507c478bd9Sstevel@tonic-gate 		 * it is the same process which we started with.
3517c478bd9Sstevel@tonic-gate 		 */
3527c478bd9Sstevel@tonic-gate 		if (ucred_getpid(uc) != privileged_pid ||
3537c478bd9Sstevel@tonic-gate 		    read(privileged_psinfo_fd, &info, sizeof (info)) !=
3547c478bd9Sstevel@tonic-gate 		    sizeof (info))
3557c478bd9Sstevel@tonic-gate 			return (REPOSITORY_DOOR_FAIL_PERMISSION_DENIED);
3567c478bd9Sstevel@tonic-gate 
35748bbca81SDaniel Hoffman 		privileged = 1;			/* it gets full privileges */
3587c478bd9Sstevel@tonic-gate 	} else if (privileged_user != 0) {
3597c478bd9Sstevel@tonic-gate 		/*
3607c478bd9Sstevel@tonic-gate 		 * in privileged user mode, only one particular user is
36148bbca81SDaniel Hoffman 		 * allowed to connect to us, and they can do anything.
3627c478bd9Sstevel@tonic-gate 		 */
3637c478bd9Sstevel@tonic-gate 		if (ucred_geteuid(uc) != privileged_user)
3647c478bd9Sstevel@tonic-gate 			return (REPOSITORY_DOOR_FAIL_PERMISSION_DENIED);
3657c478bd9Sstevel@tonic-gate 
3667c478bd9Sstevel@tonic-gate 		privileged = 1;
3677c478bd9Sstevel@tonic-gate 	}
3687c478bd9Sstevel@tonic-gate 
3697c478bd9Sstevel@tonic-gate 	/*
3707c478bd9Sstevel@tonic-gate 	 * Check that rp, of size rp_size, is large enough to
3717c478bd9Sstevel@tonic-gate 	 * contain field 'f'.  If so, write the value into *out, and return 1.
3727c478bd9Sstevel@tonic-gate 	 * Otherwise, return 0.
3737c478bd9Sstevel@tonic-gate 	 */
3747c478bd9Sstevel@tonic-gate #define	GET_ARG(rp, rp_size, f, out)					\
3757c478bd9Sstevel@tonic-gate 	(((rp_size) >= offsetofend(repository_door_request_t, f)) ?	\
3767c478bd9Sstevel@tonic-gate 	    ((*(out) = (rp)->f), 1) : 0)
3777c478bd9Sstevel@tonic-gate 
3787c478bd9Sstevel@tonic-gate 	if (!GET_ARG(rp, rp_size, rdr_flags, &flags))
3797c478bd9Sstevel@tonic-gate 		return (REPOSITORY_DOOR_FAIL_BAD_REQUEST);
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate #if (REPOSITORY_DOOR_FLAG_ALL != REPOSITORY_DOOR_FLAG_DEBUG)
3827c478bd9Sstevel@tonic-gate #error Need to update flag checks
3837c478bd9Sstevel@tonic-gate #endif
3847c478bd9Sstevel@tonic-gate 
3857c478bd9Sstevel@tonic-gate 	if (flags & ~REPOSITORY_DOOR_FLAG_ALL)
3867c478bd9Sstevel@tonic-gate 		return (REPOSITORY_DOOR_FAIL_BAD_FLAG);
3877c478bd9Sstevel@tonic-gate 
3887c478bd9Sstevel@tonic-gate 	if (flags & REPOSITORY_DOOR_FLAG_DEBUG)
3897c478bd9Sstevel@tonic-gate 		if (!GET_ARG(rp, rp_size, rdr_debug, &debugflags))
3907c478bd9Sstevel@tonic-gate 			return (REPOSITORY_DOOR_FAIL_BAD_REQUEST);
3917c478bd9Sstevel@tonic-gate #undef GET_ARG
3927c478bd9Sstevel@tonic-gate 
3937c478bd9Sstevel@tonic-gate 	return (create_client(ucred_getpid(uc), debugflags, privileged,
3947c478bd9Sstevel@tonic-gate 	    out_fd));
3957c478bd9Sstevel@tonic-gate }
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate void
configd_vlog(int severity,const char * prefix,const char * message,va_list args)3986e1d2b42Samaguire configd_vlog(int severity, const char *prefix, const char *message,
3996e1d2b42Samaguire     va_list args)
4007c478bd9Sstevel@tonic-gate {
4017c478bd9Sstevel@tonic-gate 	if (log_to_syslog)
4026e1d2b42Samaguire 		vsyslog(severity, message, args);
4037c478bd9Sstevel@tonic-gate 	else {
4047c478bd9Sstevel@tonic-gate 		flockfile(stderr);
4056e1d2b42Samaguire 		if (prefix != NULL)
4066e1d2b42Samaguire 			(void) fprintf(stderr, "%s", prefix);
4077c478bd9Sstevel@tonic-gate 		(void) vfprintf(stderr, message, args);
4087c478bd9Sstevel@tonic-gate 		if (message[0] == 0 || message[strlen(message) - 1] != '\n')
4097c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "\n");
4107c478bd9Sstevel@tonic-gate 		funlockfile(stderr);
4117c478bd9Sstevel@tonic-gate 	}
4127c478bd9Sstevel@tonic-gate }
4137c478bd9Sstevel@tonic-gate 
4146e1d2b42Samaguire void
configd_vcritical(const char * message,va_list args)4156e1d2b42Samaguire configd_vcritical(const char *message, va_list args)
4166e1d2b42Samaguire {
4176e1d2b42Samaguire 	configd_vlog(LOG_CRIT, "svc.configd: Fatal error: ", message, args);
4186e1d2b42Samaguire }
4196e1d2b42Samaguire 
4207c478bd9Sstevel@tonic-gate void
configd_critical(const char * message,...)4217c478bd9Sstevel@tonic-gate configd_critical(const char *message, ...)
4227c478bd9Sstevel@tonic-gate {
4237c478bd9Sstevel@tonic-gate 	va_list args;
4247c478bd9Sstevel@tonic-gate 	va_start(args, message);
4257c478bd9Sstevel@tonic-gate 	configd_vcritical(message, args);
4267c478bd9Sstevel@tonic-gate 	va_end(args);
4277c478bd9Sstevel@tonic-gate }
4287c478bd9Sstevel@tonic-gate 
4296e1d2b42Samaguire void
configd_info(const char * message,...)4306e1d2b42Samaguire configd_info(const char *message, ...)
4316e1d2b42Samaguire {
4326e1d2b42Samaguire 	va_list args;
4336e1d2b42Samaguire 	va_start(args, message);
4346e1d2b42Samaguire 	configd_vlog(LOG_INFO, "svc.configd: ", message, args);
4356e1d2b42Samaguire 	va_end(args);
4366e1d2b42Samaguire }
4376e1d2b42Samaguire 
4387c478bd9Sstevel@tonic-gate static void
usage(const char * prog,int ret)4397c478bd9Sstevel@tonic-gate usage(const char *prog, int ret)
4407c478bd9Sstevel@tonic-gate {
4417c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
4427c478bd9Sstevel@tonic-gate 	    "usage: %s [-np] [-d door_path] [-r repository_path]\n"
4437c478bd9Sstevel@tonic-gate 	    "    [-t nonpersist_repository]\n", prog);
4447c478bd9Sstevel@tonic-gate 	exit(ret);
4457c478bd9Sstevel@tonic-gate }
4467c478bd9Sstevel@tonic-gate 
4477c478bd9Sstevel@tonic-gate /*ARGSUSED*/
4487c478bd9Sstevel@tonic-gate static void
handler(int sig,siginfo_t * info,void * data)4497c478bd9Sstevel@tonic-gate handler(int sig, siginfo_t *info, void *data)
4507c478bd9Sstevel@tonic-gate {
4517c478bd9Sstevel@tonic-gate 	finished = 1;
4527c478bd9Sstevel@tonic-gate }
4537c478bd9Sstevel@tonic-gate 
4547c478bd9Sstevel@tonic-gate static int pipe_fd = -1;
4557c478bd9Sstevel@tonic-gate 
4567c478bd9Sstevel@tonic-gate static int
daemonize_start(void)4577c478bd9Sstevel@tonic-gate daemonize_start(void)
4587c478bd9Sstevel@tonic-gate {
4597c478bd9Sstevel@tonic-gate 	char data;
4607c478bd9Sstevel@tonic-gate 	int status;
4617c478bd9Sstevel@tonic-gate 
4627c478bd9Sstevel@tonic-gate 	int filedes[2];
4637c478bd9Sstevel@tonic-gate 	pid_t pid;
4647c478bd9Sstevel@tonic-gate 
4657c478bd9Sstevel@tonic-gate 	(void) close(0);
4667c478bd9Sstevel@tonic-gate 	(void) dup2(2, 1);		/* stderr only */
4677c478bd9Sstevel@tonic-gate 
4687c478bd9Sstevel@tonic-gate 	if (pipe(filedes) < 0)
4697c478bd9Sstevel@tonic-gate 		return (-1);
4707c478bd9Sstevel@tonic-gate 
4717c478bd9Sstevel@tonic-gate 	if ((pid = fork1()) < 0)
4727c478bd9Sstevel@tonic-gate 		return (-1);
4737c478bd9Sstevel@tonic-gate 
4747c478bd9Sstevel@tonic-gate 	if (pid != 0) {
4757c478bd9Sstevel@tonic-gate 		/*
4767c478bd9Sstevel@tonic-gate 		 * parent
4777c478bd9Sstevel@tonic-gate 		 */
4787c478bd9Sstevel@tonic-gate 		struct sigaction act;
4797c478bd9Sstevel@tonic-gate 
480*30699046SRichard Lowe 		act.sa_handler = SIG_DFL;
4817c478bd9Sstevel@tonic-gate 		(void) sigemptyset(&act.sa_mask);
4827c478bd9Sstevel@tonic-gate 		act.sa_flags = 0;
4837c478bd9Sstevel@tonic-gate 
4847c478bd9Sstevel@tonic-gate 		(void) sigaction(SIGPIPE, &act, NULL);	/* ignore SIGPIPE */
4857c478bd9Sstevel@tonic-gate 
4867c478bd9Sstevel@tonic-gate 		(void) close(filedes[1]);
4877c478bd9Sstevel@tonic-gate 		if (read(filedes[0], &data, 1) == 1) {
4887c478bd9Sstevel@tonic-gate 			/* presume success */
4897c478bd9Sstevel@tonic-gate 			_exit(CONFIGD_EXIT_OKAY);
4907c478bd9Sstevel@tonic-gate 		}
4917c478bd9Sstevel@tonic-gate 
4927c478bd9Sstevel@tonic-gate 		status = -1;
4937c478bd9Sstevel@tonic-gate 		(void) wait4(pid, &status, 0, NULL);
4947c478bd9Sstevel@tonic-gate 		if (WIFEXITED(status))
4957c478bd9Sstevel@tonic-gate 			_exit(WEXITSTATUS(status));
4967c478bd9Sstevel@tonic-gate 		else
4977c478bd9Sstevel@tonic-gate 			_exit(-1);
4987c478bd9Sstevel@tonic-gate 	}
4997c478bd9Sstevel@tonic-gate 
5007c478bd9Sstevel@tonic-gate 	/*
5017c478bd9Sstevel@tonic-gate 	 * child
5027c478bd9Sstevel@tonic-gate 	 */
5037c478bd9Sstevel@tonic-gate 	pipe_fd = filedes[1];
5047c478bd9Sstevel@tonic-gate 	(void) close(filedes[0]);
5057c478bd9Sstevel@tonic-gate 
5067c478bd9Sstevel@tonic-gate 	/*
5077c478bd9Sstevel@tonic-gate 	 * generic Unix setup
5087c478bd9Sstevel@tonic-gate 	 */
5097c478bd9Sstevel@tonic-gate 	(void) setsid();
5107c478bd9Sstevel@tonic-gate 	(void) umask(0077);
5117c478bd9Sstevel@tonic-gate 
5127c478bd9Sstevel@tonic-gate 	return (0);
5137c478bd9Sstevel@tonic-gate }
5147c478bd9Sstevel@tonic-gate 
5157c478bd9Sstevel@tonic-gate static void
daemonize_ready(void)5167c478bd9Sstevel@tonic-gate daemonize_ready(void)
5177c478bd9Sstevel@tonic-gate {
5187c478bd9Sstevel@tonic-gate 	char data = '\0';
5197c478bd9Sstevel@tonic-gate 
5207c478bd9Sstevel@tonic-gate 	/*
5217c478bd9Sstevel@tonic-gate 	 * wake the parent
5227c478bd9Sstevel@tonic-gate 	 */
5237c478bd9Sstevel@tonic-gate 	(void) write(pipe_fd, &data, 1);
5247c478bd9Sstevel@tonic-gate 	(void) close(pipe_fd);
5257c478bd9Sstevel@tonic-gate }
5267c478bd9Sstevel@tonic-gate 
5278918dff3Sjwadams const char *
regularize_path(const char * dir,const char * base,char * tmpbuf)5288918dff3Sjwadams regularize_path(const char *dir, const char *base, char *tmpbuf)
5298918dff3Sjwadams {
5308918dff3Sjwadams 	if (base == NULL)
5318918dff3Sjwadams 		return (NULL);
5328918dff3Sjwadams 	if (base[0] == '/')
5338918dff3Sjwadams 		return (base);
5348918dff3Sjwadams 
5358918dff3Sjwadams 	if (snprintf(tmpbuf, PATH_MAX, "%s/%s", dir, base) >= PATH_MAX) {
5368918dff3Sjwadams 		(void) fprintf(stderr, "svc.configd: %s/%s: path too long\n",
5378918dff3Sjwadams 		    dir, base);
5388918dff3Sjwadams 		exit(CONFIGD_EXIT_BAD_ARGS);
5398918dff3Sjwadams 	}
5408918dff3Sjwadams 
5418918dff3Sjwadams 	return (tmpbuf);
5428918dff3Sjwadams }
5438918dff3Sjwadams 
5447c478bd9Sstevel@tonic-gate int
main(int argc,char * argv[])5457c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
5467c478bd9Sstevel@tonic-gate {
5477c478bd9Sstevel@tonic-gate 	thread_info_t *ti = &main_thread_info;
5487c478bd9Sstevel@tonic-gate 
5497c478bd9Sstevel@tonic-gate 	char pidpath[sizeof ("/proc/" "/psinfo") + 10];
5507c478bd9Sstevel@tonic-gate 
5517c478bd9Sstevel@tonic-gate 	struct rlimit fd_new;
5527c478bd9Sstevel@tonic-gate 
5537c478bd9Sstevel@tonic-gate 	const char *endptr;
5547c478bd9Sstevel@tonic-gate 	sigset_t myset;
5557c478bd9Sstevel@tonic-gate 	int c;
5567c478bd9Sstevel@tonic-gate 	int ret;
5577c478bd9Sstevel@tonic-gate 	int fd;
5588918dff3Sjwadams 
5598918dff3Sjwadams 	char curdir[PATH_MAX];
5608918dff3Sjwadams 	char dbtmp[PATH_MAX];
5618918dff3Sjwadams 	char npdbtmp[PATH_MAX];
5628918dff3Sjwadams 	char doortmp[PATH_MAX];
5638918dff3Sjwadams 
5647c478bd9Sstevel@tonic-gate 	const char *dbpath = NULL;
5657c478bd9Sstevel@tonic-gate 	const char *npdbpath = NULL;
5667c478bd9Sstevel@tonic-gate 	const char *doorpath = REPOSITORY_DOOR_NAME;
5677c478bd9Sstevel@tonic-gate 	struct sigaction act;
5687c478bd9Sstevel@tonic-gate 
5697c478bd9Sstevel@tonic-gate 	int daemonize = 1;		/* default to daemonizing */
5707c478bd9Sstevel@tonic-gate 	int have_npdb = 1;
5717c478bd9Sstevel@tonic-gate 
5727c478bd9Sstevel@tonic-gate 	closefrom(3);			/* get rid of extraneous fds */
5737c478bd9Sstevel@tonic-gate 
5748918dff3Sjwadams 	if (getcwd(curdir, sizeof (curdir)) == NULL) {
5758918dff3Sjwadams 		(void) fprintf(stderr,
5768918dff3Sjwadams 		    "%s: unable to get current directory: %s\n",
5778918dff3Sjwadams 		    argv[0], strerror(errno));
5788918dff3Sjwadams 		exit(CONFIGD_EXIT_INIT_FAILED);
5798918dff3Sjwadams 	}
5808918dff3Sjwadams 
5817c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "Dnpd:r:t:")) != -1) {
5827c478bd9Sstevel@tonic-gate 		switch (c) {
5837c478bd9Sstevel@tonic-gate 		case 'n':
5847c478bd9Sstevel@tonic-gate 			daemonize = 0;
5857c478bd9Sstevel@tonic-gate 			break;
5867c478bd9Sstevel@tonic-gate 		case 'd':
5878918dff3Sjwadams 			doorpath = regularize_path(curdir, optarg, doortmp);
5887c478bd9Sstevel@tonic-gate 			have_npdb = 0;		/* default to no non-persist */
5897c478bd9Sstevel@tonic-gate 			break;
5907c478bd9Sstevel@tonic-gate 		case 'p':
5917c478bd9Sstevel@tonic-gate 			log_to_syslog = 0;	/* don't use syslog */
5927c478bd9Sstevel@tonic-gate 
5937c478bd9Sstevel@tonic-gate 			/*
5947c478bd9Sstevel@tonic-gate 			 * If our parent exits while we're opening its /proc
5957c478bd9Sstevel@tonic-gate 			 * psinfo, we're vulnerable to a pid wrapping.  To
5967c478bd9Sstevel@tonic-gate 			 * protect against that, re-check our ppid after
5977c478bd9Sstevel@tonic-gate 			 * opening it.
5987c478bd9Sstevel@tonic-gate 			 */
5997c478bd9Sstevel@tonic-gate 			privileged_pid = getppid();
6007c478bd9Sstevel@tonic-gate 			(void) snprintf(pidpath, sizeof (pidpath),
6017c478bd9Sstevel@tonic-gate 			    "/proc/%d/psinfo", privileged_pid);
6027c478bd9Sstevel@tonic-gate 			if ((fd = open(pidpath, O_RDONLY)) < 0 ||
6037c478bd9Sstevel@tonic-gate 			    getppid() != privileged_pid) {
6047c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
6057c478bd9Sstevel@tonic-gate 				    "%s: unable to get parent info\n", argv[0]);
6067c478bd9Sstevel@tonic-gate 				exit(CONFIGD_EXIT_BAD_ARGS);
6077c478bd9Sstevel@tonic-gate 			}
6087c478bd9Sstevel@tonic-gate 			privileged_psinfo_fd = fd;
6097c478bd9Sstevel@tonic-gate 			break;
6107c478bd9Sstevel@tonic-gate 		case 'r':
6118918dff3Sjwadams 			dbpath = regularize_path(curdir, optarg, dbtmp);
6127c478bd9Sstevel@tonic-gate 			is_main_repository = 0;
6137c478bd9Sstevel@tonic-gate 			break;
6147c478bd9Sstevel@tonic-gate 		case 't':
6158918dff3Sjwadams 			npdbpath = regularize_path(curdir, optarg, npdbtmp);
6167c478bd9Sstevel@tonic-gate 			is_main_repository = 0;
6177c478bd9Sstevel@tonic-gate 			break;
6187c478bd9Sstevel@tonic-gate 		default:
6197c478bd9Sstevel@tonic-gate 			usage(argv[0], CONFIGD_EXIT_BAD_ARGS);
6207c478bd9Sstevel@tonic-gate 			break;
6217c478bd9Sstevel@tonic-gate 		}
6227c478bd9Sstevel@tonic-gate 	}
6237c478bd9Sstevel@tonic-gate 
6247c478bd9Sstevel@tonic-gate 	/*
6257c478bd9Sstevel@tonic-gate 	 * If we're not running as root, allow our euid full access, and
6267c478bd9Sstevel@tonic-gate 	 * everyone else no access.
6277c478bd9Sstevel@tonic-gate 	 */
6287c478bd9Sstevel@tonic-gate 	if (privileged_pid == 0 && geteuid() != 0) {
6297c478bd9Sstevel@tonic-gate 		privileged_user = geteuid();
6307c478bd9Sstevel@tonic-gate 	}
6317c478bd9Sstevel@tonic-gate 
6327c478bd9Sstevel@tonic-gate 	privileged_privs = priv_str_to_set("zone", "", &endptr);
6337c478bd9Sstevel@tonic-gate 	if (endptr != NULL && privileged_privs != NULL) {
6347c478bd9Sstevel@tonic-gate 		priv_freeset(privileged_privs);
6357c478bd9Sstevel@tonic-gate 		privileged_privs = NULL;
6367c478bd9Sstevel@tonic-gate 	}
6377c478bd9Sstevel@tonic-gate 
6387c478bd9Sstevel@tonic-gate 	openlog("svc.configd", LOG_PID | LOG_CONS, LOG_DAEMON);
6397c478bd9Sstevel@tonic-gate 	(void) setlogmask(LOG_UPTO(LOG_NOTICE));
6407c478bd9Sstevel@tonic-gate 
6417c478bd9Sstevel@tonic-gate 	/*
6427c478bd9Sstevel@tonic-gate 	 * if a non-persist db is specified, always enable it
6437c478bd9Sstevel@tonic-gate 	 */
6447c478bd9Sstevel@tonic-gate 	if (npdbpath)
6457c478bd9Sstevel@tonic-gate 		have_npdb = 1;
6467c478bd9Sstevel@tonic-gate 
6477c478bd9Sstevel@tonic-gate 	if (optind != argc)
6487c478bd9Sstevel@tonic-gate 		usage(argv[0], CONFIGD_EXIT_BAD_ARGS);
6497c478bd9Sstevel@tonic-gate 
6507c478bd9Sstevel@tonic-gate 	if (daemonize) {
6517c478bd9Sstevel@tonic-gate 		if (getuid() == 0)
6527c478bd9Sstevel@tonic-gate 			(void) chdir("/");
6537c478bd9Sstevel@tonic-gate 		if (daemonize_start() < 0) {
6547c478bd9Sstevel@tonic-gate 			(void) perror("unable to daemonize");
6557c478bd9Sstevel@tonic-gate 			exit(CONFIGD_EXIT_INIT_FAILED);
6567c478bd9Sstevel@tonic-gate 		}
6577c478bd9Sstevel@tonic-gate 	}
6587c478bd9Sstevel@tonic-gate 	if (getuid() == 0)
6597c478bd9Sstevel@tonic-gate 		(void) core_set_process_path(CONFIGD_CORE,
6607c478bd9Sstevel@tonic-gate 		    strlen(CONFIGD_CORE) + 1, getpid());
6617c478bd9Sstevel@tonic-gate 
6627c478bd9Sstevel@tonic-gate 	/*
6637c478bd9Sstevel@tonic-gate 	 * this should be enabled once we can drop privileges and still get
6647c478bd9Sstevel@tonic-gate 	 * a core dump.
6657c478bd9Sstevel@tonic-gate 	 */
6667c478bd9Sstevel@tonic-gate #if 0
6677c478bd9Sstevel@tonic-gate 	/* turn off basic privileges we do not need */
6687c478bd9Sstevel@tonic-gate 	(void) priv_set(PRIV_OFF, PRIV_PERMITTED, PRIV_FILE_LINK_ANY,
6697c478bd9Sstevel@tonic-gate 	    PRIV_PROC_EXEC, PRIV_PROC_FORK, PRIV_PROC_SESSION, NULL);
6707c478bd9Sstevel@tonic-gate #endif
6717c478bd9Sstevel@tonic-gate 
6727c478bd9Sstevel@tonic-gate 	/* not that we can exec, but to be safe, shut them all off... */
6737c478bd9Sstevel@tonic-gate 	(void) priv_set(PRIV_SET, PRIV_INHERITABLE, NULL);
6747c478bd9Sstevel@tonic-gate 
6757c478bd9Sstevel@tonic-gate 	(void) sigfillset(&act.sa_mask);
6767c478bd9Sstevel@tonic-gate 
6777c478bd9Sstevel@tonic-gate 	/* signals to ignore */
678*30699046SRichard Lowe 	act.sa_handler = SIG_IGN;
6797c478bd9Sstevel@tonic-gate 	act.sa_flags = 0;
6807c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGPIPE, &act, NULL);
6817c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGALRM, &act, NULL);
6827c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGUSR1, &act, NULL);
6837c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGUSR2, &act, NULL);
6847c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGPOLL, &act, NULL);
6857c478bd9Sstevel@tonic-gate 
6867c478bd9Sstevel@tonic-gate 	/* signals to abort on */
6877c478bd9Sstevel@tonic-gate 	act.sa_sigaction = (void (*)(int, siginfo_t *, void *))&abort_handler;
6887c478bd9Sstevel@tonic-gate 	act.sa_flags = SA_SIGINFO;
6897c478bd9Sstevel@tonic-gate 
6907c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGABRT, &act, NULL);
6917c478bd9Sstevel@tonic-gate 
6927c478bd9Sstevel@tonic-gate 	/* signals to handle */
6937c478bd9Sstevel@tonic-gate 	act.sa_sigaction = &handler;
6947c478bd9Sstevel@tonic-gate 	act.sa_flags = SA_SIGINFO;
6957c478bd9Sstevel@tonic-gate 
6967c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGHUP, &act, NULL);
6977c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGINT, &act, NULL);
6987c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGTERM, &act, NULL);
6997c478bd9Sstevel@tonic-gate 
7007c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&myset);
7017c478bd9Sstevel@tonic-gate 	(void) sigaddset(&myset, SIGHUP);
7027c478bd9Sstevel@tonic-gate 	(void) sigaddset(&myset, SIGINT);
7037c478bd9Sstevel@tonic-gate 	(void) sigaddset(&myset, SIGTERM);
7047c478bd9Sstevel@tonic-gate 
7057c478bd9Sstevel@tonic-gate 	if ((errno = pthread_attr_init(&thread_attr)) != 0) {
7067c478bd9Sstevel@tonic-gate 		(void) perror("initializing");
7077c478bd9Sstevel@tonic-gate 		exit(CONFIGD_EXIT_INIT_FAILED);
7087c478bd9Sstevel@tonic-gate 	}
7097c478bd9Sstevel@tonic-gate 
7107c478bd9Sstevel@tonic-gate 	/*
7117c478bd9Sstevel@tonic-gate 	 * Set the hard and soft limits to CONFIGD_MAX_FDS.
7127c478bd9Sstevel@tonic-gate 	 */
7137c478bd9Sstevel@tonic-gate 	fd_new.rlim_max = fd_new.rlim_cur = CONFIGD_MAX_FDS;
7147c478bd9Sstevel@tonic-gate 	(void) setrlimit(RLIMIT_NOFILE, &fd_new);
7157c478bd9Sstevel@tonic-gate 
716004388ebScasper #ifndef NATIVE_BUILD /* Allow building on snv_38 and earlier; remove later. */
717004388ebScasper 	(void) enable_extended_FILE_stdio(-1, -1);
718004388ebScasper #endif
719004388ebScasper 
7207c478bd9Sstevel@tonic-gate 	if ((ret = backend_init(dbpath, npdbpath, have_npdb)) !=
7217c478bd9Sstevel@tonic-gate 	    CONFIGD_EXIT_OKAY)
7227c478bd9Sstevel@tonic-gate 		exit(ret);
7237c478bd9Sstevel@tonic-gate 
7247c478bd9Sstevel@tonic-gate 	if (!client_init())
7257c478bd9Sstevel@tonic-gate 		exit(CONFIGD_EXIT_INIT_FAILED);
7267c478bd9Sstevel@tonic-gate 
7277c478bd9Sstevel@tonic-gate 	if (!rc_node_init())
7287c478bd9Sstevel@tonic-gate 		exit(CONFIGD_EXIT_INIT_FAILED);
7297c478bd9Sstevel@tonic-gate 
7307c478bd9Sstevel@tonic-gate 	(void) pthread_attr_setdetachstate(&thread_attr,
7317c478bd9Sstevel@tonic-gate 	    PTHREAD_CREATE_DETACHED);
7327c478bd9Sstevel@tonic-gate 	(void) pthread_attr_setscope(&thread_attr, PTHREAD_SCOPE_SYSTEM);
7337c478bd9Sstevel@tonic-gate 
7347c478bd9Sstevel@tonic-gate 	if ((errno = pthread_key_create(&thread_info_key,
7357c478bd9Sstevel@tonic-gate 	    thread_exiting)) != 0) {
7367c478bd9Sstevel@tonic-gate 		perror("pthread_key_create");
7377c478bd9Sstevel@tonic-gate 		exit(CONFIGD_EXIT_INIT_FAILED);
7387c478bd9Sstevel@tonic-gate 	}
7397c478bd9Sstevel@tonic-gate 
7407c478bd9Sstevel@tonic-gate 	if ((thread_pool = uu_list_pool_create("thread_pool",
7417c478bd9Sstevel@tonic-gate 	    sizeof (thread_info_t), offsetof(thread_info_t, ti_node),
7427c478bd9Sstevel@tonic-gate 	    NULL, UU_LIST_POOL_DEBUG)) == NULL) {
7437c478bd9Sstevel@tonic-gate 		configd_critical("uu_list_pool_create: %s\n",
7447c478bd9Sstevel@tonic-gate 		    uu_strerror(uu_error()));
7457c478bd9Sstevel@tonic-gate 		exit(CONFIGD_EXIT_INIT_FAILED);
7467c478bd9Sstevel@tonic-gate 	}
7477c478bd9Sstevel@tonic-gate 
7487c478bd9Sstevel@tonic-gate 	if ((thread_list = uu_list_create(thread_pool, NULL, 0)) == NULL) {
7497c478bd9Sstevel@tonic-gate 		configd_critical("uu_list_create: %s\n",
7507c478bd9Sstevel@tonic-gate 		    uu_strerror(uu_error()));
7517c478bd9Sstevel@tonic-gate 		exit(CONFIGD_EXIT_INIT_FAILED);
7527c478bd9Sstevel@tonic-gate 	}
7537c478bd9Sstevel@tonic-gate 
7547c478bd9Sstevel@tonic-gate 	(void) memset(ti, '\0', sizeof (*ti));
7557c478bd9Sstevel@tonic-gate 	uu_list_node_init(ti, &ti->ti_node, thread_pool);
7567c478bd9Sstevel@tonic-gate 	(void) uu_list_insert_before(thread_list, uu_list_first(thread_list),
7577c478bd9Sstevel@tonic-gate 	    ti);
7587c478bd9Sstevel@tonic-gate 
7597c478bd9Sstevel@tonic-gate 	ti->ti_thread = pthread_self();
7607c478bd9Sstevel@tonic-gate 	ti->ti_state = TI_SIGNAL_WAIT;
7617c478bd9Sstevel@tonic-gate 	ti->ti_prev_state = TI_SIGNAL_WAIT;
7627c478bd9Sstevel@tonic-gate 
7637c478bd9Sstevel@tonic-gate 	(void) pthread_setspecific(thread_info_key, ti);
7647c478bd9Sstevel@tonic-gate 
7657c478bd9Sstevel@tonic-gate 	(void) door_server_create(new_thread_needed);
7667c478bd9Sstevel@tonic-gate 
7677c478bd9Sstevel@tonic-gate 	if (!setup_main_door(doorpath)) {
7687c478bd9Sstevel@tonic-gate 		configd_critical("Setting up main door failed.\n");
7697c478bd9Sstevel@tonic-gate 		exit(CONFIGD_EXIT_DOOR_INIT_FAILED);
7707c478bd9Sstevel@tonic-gate 	}
7717c478bd9Sstevel@tonic-gate 
7727c478bd9Sstevel@tonic-gate 	if (daemonize)
7737c478bd9Sstevel@tonic-gate 		daemonize_ready();
7747c478bd9Sstevel@tonic-gate 
7757c478bd9Sstevel@tonic-gate 	(void) pthread_sigmask(SIG_BLOCK, &myset, NULL);
7767c478bd9Sstevel@tonic-gate 	while (!finished) {
7777c478bd9Sstevel@tonic-gate 		int sig = sigwait(&myset);
7787c478bd9Sstevel@tonic-gate 		if (sig > 0) {
7797c478bd9Sstevel@tonic-gate 			break;
7807c478bd9Sstevel@tonic-gate 		}
7817c478bd9Sstevel@tonic-gate 	}
7827c478bd9Sstevel@tonic-gate 
7837c478bd9Sstevel@tonic-gate 	backend_fini();
7847c478bd9Sstevel@tonic-gate 
7857c478bd9Sstevel@tonic-gate 	return (CONFIGD_EXIT_OKAY);
7867c478bd9Sstevel@tonic-gate }
787