10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
51914Scasper * Common Development and Distribution License (the "License").
61914Scasper * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
210Sstevel@tonic-gate /*
225777Stw21770 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
230Sstevel@tonic-gate * Use is subject to license terms.
240Sstevel@tonic-gate */
250Sstevel@tonic-gate
260Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
270Sstevel@tonic-gate
280Sstevel@tonic-gate #include <assert.h>
290Sstevel@tonic-gate #include <door.h>
300Sstevel@tonic-gate #include <errno.h>
310Sstevel@tonic-gate #include <fcntl.h>
32407Sjwadams #include <limits.h>
330Sstevel@tonic-gate #include <priv.h>
340Sstevel@tonic-gate #include <procfs.h>
350Sstevel@tonic-gate #include <pthread.h>
360Sstevel@tonic-gate #include <signal.h>
370Sstevel@tonic-gate #include <stdarg.h>
380Sstevel@tonic-gate #include <stdio.h>
391914Scasper #include <stdio_ext.h>
400Sstevel@tonic-gate #include <stdlib.h>
410Sstevel@tonic-gate #include <string.h>
420Sstevel@tonic-gate #include <syslog.h>
430Sstevel@tonic-gate #include <sys/corectl.h>
440Sstevel@tonic-gate #include <sys/resource.h>
450Sstevel@tonic-gate #include <sys/stat.h>
460Sstevel@tonic-gate #include <sys/wait.h>
470Sstevel@tonic-gate #include <ucontext.h>
480Sstevel@tonic-gate #include <unistd.h>
490Sstevel@tonic-gate
500Sstevel@tonic-gate #include "configd.h"
510Sstevel@tonic-gate
520Sstevel@tonic-gate /*
530Sstevel@tonic-gate * This file manages the overall startup and shutdown of configd, as well
540Sstevel@tonic-gate * as managing its door thread pool and per-thread datastructures.
550Sstevel@tonic-gate *
560Sstevel@tonic-gate * 1. Per-thread Datastructures
570Sstevel@tonic-gate * -----------------------------
580Sstevel@tonic-gate * Each configd thread has an associated thread_info_t which contains its
590Sstevel@tonic-gate * current state. A pointer is kept to this in TSD, keyed by thread_info_key.
600Sstevel@tonic-gate * The thread_info_ts for all threads in configd are kept on a single global
610Sstevel@tonic-gate * list, thread_list. After creation, the state in the thread_info structure
620Sstevel@tonic-gate * is only modified by the associated thread, so no locking is needed. A TSD
630Sstevel@tonic-gate * destructor removes the thread_info from the global list and frees it at
640Sstevel@tonic-gate * pthread_exit() time.
650Sstevel@tonic-gate *
660Sstevel@tonic-gate * Threads access their per-thread data using thread_self()
670Sstevel@tonic-gate *
680Sstevel@tonic-gate * The thread_list is protected by thread_lock, a leaf lock.
690Sstevel@tonic-gate *
700Sstevel@tonic-gate * 2. Door Thread Pool Management
710Sstevel@tonic-gate * ------------------------------
720Sstevel@tonic-gate * Whenever door_return(3door) returns from the kernel and there are no
730Sstevel@tonic-gate * other configd threads waiting for requests, libdoor automatically
740Sstevel@tonic-gate * invokes a function registered with door_server_create(), to request a new
750Sstevel@tonic-gate * door server thread. The default function just creates a thread that calls
760Sstevel@tonic-gate * door_return(3door). Unfortunately, since it can take a while for the new
770Sstevel@tonic-gate * thread to *get* to door_return(3door), a stream of requests can cause a
780Sstevel@tonic-gate * large number of threads to be created, even though they aren't all needed.
790Sstevel@tonic-gate *
800Sstevel@tonic-gate * In our callback, new_server_needed(), we limit ourself to two new threads
810Sstevel@tonic-gate * at a time -- this logic is handled in reserve_new_thread(). This keeps
820Sstevel@tonic-gate * us from creating an absurd number of threads in response to peaking load.
830Sstevel@tonic-gate */
840Sstevel@tonic-gate static pthread_key_t thread_info_key;
850Sstevel@tonic-gate static pthread_attr_t thread_attr;
860Sstevel@tonic-gate
870Sstevel@tonic-gate static pthread_mutex_t thread_lock = PTHREAD_MUTEX_INITIALIZER;
880Sstevel@tonic-gate int num_started; /* number actually running */
890Sstevel@tonic-gate int num_servers; /* number in-progress or running */
900Sstevel@tonic-gate static uu_list_pool_t *thread_pool;
910Sstevel@tonic-gate uu_list_t *thread_list;
920Sstevel@tonic-gate
930Sstevel@tonic-gate static thread_info_t main_thread_info;
940Sstevel@tonic-gate
950Sstevel@tonic-gate static int finished;
960Sstevel@tonic-gate
970Sstevel@tonic-gate static pid_t privileged_pid = 0;
980Sstevel@tonic-gate static int privileged_psinfo_fd = -1;
990Sstevel@tonic-gate
1000Sstevel@tonic-gate static int privileged_user = 0;
1010Sstevel@tonic-gate
1020Sstevel@tonic-gate static priv_set_t *privileged_privs;
1030Sstevel@tonic-gate
1040Sstevel@tonic-gate static int log_to_syslog = 0;
1050Sstevel@tonic-gate
1060Sstevel@tonic-gate int is_main_repository = 1;
1070Sstevel@tonic-gate
1080Sstevel@tonic-gate int max_repository_backups = 4;
1090Sstevel@tonic-gate
1100Sstevel@tonic-gate #define CONFIGD_MAX_FDS 262144
1110Sstevel@tonic-gate
1120Sstevel@tonic-gate /*
1130Sstevel@tonic-gate * Thanks, Mike
1140Sstevel@tonic-gate */
1150Sstevel@tonic-gate void
abort_handler(int sig,siginfo_t * sip,ucontext_t * ucp)1160Sstevel@tonic-gate abort_handler(int sig, siginfo_t *sip, ucontext_t *ucp)
1170Sstevel@tonic-gate {
1180Sstevel@tonic-gate struct sigaction act;
1190Sstevel@tonic-gate
1200Sstevel@tonic-gate (void) sigemptyset(&act.sa_mask);
1210Sstevel@tonic-gate act.sa_handler = SIG_DFL;
1220Sstevel@tonic-gate act.sa_flags = 0;
1230Sstevel@tonic-gate (void) sigaction(sig, &act, NULL);
1240Sstevel@tonic-gate
1250Sstevel@tonic-gate (void) printstack(2);
1260Sstevel@tonic-gate
1270Sstevel@tonic-gate if (sip != NULL && SI_FROMUSER(sip))
1280Sstevel@tonic-gate (void) pthread_kill(pthread_self(), sig);
1290Sstevel@tonic-gate (void) sigfillset(&ucp->uc_sigmask);
1300Sstevel@tonic-gate (void) sigdelset(&ucp->uc_sigmask, sig);
1310Sstevel@tonic-gate ucp->uc_flags |= UC_SIGMASK;
1320Sstevel@tonic-gate (void) setcontext(ucp);
1330Sstevel@tonic-gate }
1340Sstevel@tonic-gate
1350Sstevel@tonic-gate /*
1360Sstevel@tonic-gate * Don't want to have more than a couple thread creates outstanding
1370Sstevel@tonic-gate */
1380Sstevel@tonic-gate static int
reserve_new_thread(void)1390Sstevel@tonic-gate reserve_new_thread(void)
1400Sstevel@tonic-gate {
1410Sstevel@tonic-gate (void) pthread_mutex_lock(&thread_lock);
1420Sstevel@tonic-gate assert(num_started >= 0);
1430Sstevel@tonic-gate if (num_servers > num_started + 1) {
1440Sstevel@tonic-gate (void) pthread_mutex_unlock(&thread_lock);
1450Sstevel@tonic-gate return (0);
1460Sstevel@tonic-gate }
1470Sstevel@tonic-gate ++num_servers;
1480Sstevel@tonic-gate (void) pthread_mutex_unlock(&thread_lock);
1490Sstevel@tonic-gate return (1);
1500Sstevel@tonic-gate }
1510Sstevel@tonic-gate
1520Sstevel@tonic-gate static void
thread_info_free(thread_info_t * ti)1530Sstevel@tonic-gate thread_info_free(thread_info_t *ti)
1540Sstevel@tonic-gate {
1550Sstevel@tonic-gate uu_list_node_fini(ti, &ti->ti_node, thread_pool);
1560Sstevel@tonic-gate if (ti->ti_ucred != NULL)
1570Sstevel@tonic-gate uu_free(ti->ti_ucred);
1580Sstevel@tonic-gate uu_free(ti);
1590Sstevel@tonic-gate }
1600Sstevel@tonic-gate
1610Sstevel@tonic-gate static void
thread_exiting(void * arg)1620Sstevel@tonic-gate thread_exiting(void *arg)
1630Sstevel@tonic-gate {
1640Sstevel@tonic-gate thread_info_t *ti = arg;
1650Sstevel@tonic-gate
166407Sjwadams if (ti != NULL)
167407Sjwadams log_enter(&ti->ti_log);
1680Sstevel@tonic-gate
1690Sstevel@tonic-gate (void) pthread_mutex_lock(&thread_lock);
1700Sstevel@tonic-gate if (ti != NULL) {
1710Sstevel@tonic-gate num_started--;
1720Sstevel@tonic-gate uu_list_remove(thread_list, ti);
1730Sstevel@tonic-gate }
1740Sstevel@tonic-gate assert(num_servers > 0);
1750Sstevel@tonic-gate --num_servers;
1760Sstevel@tonic-gate
1770Sstevel@tonic-gate if (num_servers == 0) {
1780Sstevel@tonic-gate configd_critical("no door server threads\n");
1790Sstevel@tonic-gate abort();
1800Sstevel@tonic-gate }
1810Sstevel@tonic-gate (void) pthread_mutex_unlock(&thread_lock);
1820Sstevel@tonic-gate
1830Sstevel@tonic-gate if (ti != NULL && ti != &main_thread_info)
1840Sstevel@tonic-gate thread_info_free(ti);
1850Sstevel@tonic-gate }
1860Sstevel@tonic-gate
1870Sstevel@tonic-gate void
thread_newstate(thread_info_t * ti,thread_state_t newstate)1880Sstevel@tonic-gate thread_newstate(thread_info_t *ti, thread_state_t newstate)
1890Sstevel@tonic-gate {
1900Sstevel@tonic-gate ti->ti_ucred_read = 0; /* invalidate cached ucred */
1910Sstevel@tonic-gate if (newstate != ti->ti_state) {
1920Sstevel@tonic-gate ti->ti_prev_state = ti->ti_state;
1930Sstevel@tonic-gate ti->ti_state = newstate;
1940Sstevel@tonic-gate ti->ti_lastchange = gethrtime();
1950Sstevel@tonic-gate }
1960Sstevel@tonic-gate }
1970Sstevel@tonic-gate
1980Sstevel@tonic-gate thread_info_t *
thread_self(void)1990Sstevel@tonic-gate thread_self(void)
2000Sstevel@tonic-gate {
2010Sstevel@tonic-gate return (pthread_getspecific(thread_info_key));
2020Sstevel@tonic-gate }
2030Sstevel@tonic-gate
2045777Stw21770 /*
2055777Stw21770 * get_ucred() returns NULL if it was unable to get the credential
2065777Stw21770 * information.
2075777Stw21770 */
2080Sstevel@tonic-gate ucred_t *
get_ucred(void)2090Sstevel@tonic-gate get_ucred(void)
2100Sstevel@tonic-gate {
2110Sstevel@tonic-gate thread_info_t *ti = thread_self();
2120Sstevel@tonic-gate ucred_t **ret = &ti->ti_ucred;
2130Sstevel@tonic-gate
2140Sstevel@tonic-gate if (ti->ti_ucred_read)
2150Sstevel@tonic-gate return (*ret); /* cached value */
2160Sstevel@tonic-gate
2170Sstevel@tonic-gate if (door_ucred(ret) != 0)
2180Sstevel@tonic-gate return (NULL);
2190Sstevel@tonic-gate ti->ti_ucred_read = 1;
2200Sstevel@tonic-gate
2210Sstevel@tonic-gate return (*ret);
2220Sstevel@tonic-gate }
2230Sstevel@tonic-gate
2240Sstevel@tonic-gate int
ucred_is_privileged(ucred_t * uc)2250Sstevel@tonic-gate ucred_is_privileged(ucred_t *uc)
2260Sstevel@tonic-gate {
2270Sstevel@tonic-gate const priv_set_t *ps;
2280Sstevel@tonic-gate
2290Sstevel@tonic-gate if ((ps = ucred_getprivset(uc, PRIV_EFFECTIVE)) != NULL) {
2300Sstevel@tonic-gate if (priv_isfullset(ps))
2310Sstevel@tonic-gate return (1); /* process has all privs */
2320Sstevel@tonic-gate
2330Sstevel@tonic-gate if (privileged_privs != NULL &&
2340Sstevel@tonic-gate priv_issubset(privileged_privs, ps))
2350Sstevel@tonic-gate return (1); /* process has zone privs */
2360Sstevel@tonic-gate }
2370Sstevel@tonic-gate
2380Sstevel@tonic-gate return (0);
2390Sstevel@tonic-gate }
2400Sstevel@tonic-gate
2415777Stw21770 /*
2425777Stw21770 * The purpose of this function is to get the audit session data for use in
2435777Stw21770 * generating SMF audit events. We use a single audit session per client.
2445777Stw21770 *
2455777Stw21770 * get_audit_session() may return NULL. It is legal to use a NULL pointer
2465777Stw21770 * in subsequent calls to adt_* functions.
2475777Stw21770 */
2485777Stw21770 adt_session_data_t *
get_audit_session(void)2495777Stw21770 get_audit_session(void)
2505777Stw21770 {
2515777Stw21770 thread_info_t *ti = thread_self();
2525777Stw21770
2535777Stw21770 return (ti->ti_active_client->rc_adt_session);
2545777Stw21770 }
2555777Stw21770
2560Sstevel@tonic-gate static void *
thread_start(void * arg)2570Sstevel@tonic-gate thread_start(void *arg)
2580Sstevel@tonic-gate {
2590Sstevel@tonic-gate thread_info_t *ti = arg;
2600Sstevel@tonic-gate
2610Sstevel@tonic-gate (void) pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
2620Sstevel@tonic-gate
2630Sstevel@tonic-gate (void) pthread_mutex_lock(&thread_lock);
2640Sstevel@tonic-gate num_started++;
2650Sstevel@tonic-gate (void) uu_list_insert_after(thread_list, uu_list_last(thread_list),
2660Sstevel@tonic-gate ti);
2670Sstevel@tonic-gate (void) pthread_mutex_unlock(&thread_lock);
2680Sstevel@tonic-gate (void) pthread_setspecific(thread_info_key, ti);
2690Sstevel@tonic-gate
2700Sstevel@tonic-gate thread_newstate(ti, TI_DOOR_RETURN);
2710Sstevel@tonic-gate
2720Sstevel@tonic-gate /*
2730Sstevel@tonic-gate * Start handling door calls
2740Sstevel@tonic-gate */
2750Sstevel@tonic-gate (void) door_return(NULL, 0, NULL, 0);
2760Sstevel@tonic-gate return (arg);
2770Sstevel@tonic-gate }
2780Sstevel@tonic-gate
2790Sstevel@tonic-gate static void
new_thread_needed(door_info_t * dip)2800Sstevel@tonic-gate new_thread_needed(door_info_t *dip)
2810Sstevel@tonic-gate {
2820Sstevel@tonic-gate thread_info_t *ti;
2830Sstevel@tonic-gate
2840Sstevel@tonic-gate sigset_t new, old;
2850Sstevel@tonic-gate
2860Sstevel@tonic-gate assert(dip == NULL);
2870Sstevel@tonic-gate
2880Sstevel@tonic-gate if (!reserve_new_thread())
2890Sstevel@tonic-gate return;
2900Sstevel@tonic-gate
2910Sstevel@tonic-gate if ((ti = uu_zalloc(sizeof (*ti))) == NULL)
2920Sstevel@tonic-gate goto fail;
2930Sstevel@tonic-gate
2940Sstevel@tonic-gate uu_list_node_init(ti, &ti->ti_node, thread_pool);
2950Sstevel@tonic-gate ti->ti_state = TI_CREATED;
2960Sstevel@tonic-gate ti->ti_prev_state = TI_CREATED;
2970Sstevel@tonic-gate
2980Sstevel@tonic-gate if ((ti->ti_ucred = uu_zalloc(ucred_size())) == NULL)
2990Sstevel@tonic-gate goto fail;
3000Sstevel@tonic-gate
3010Sstevel@tonic-gate (void) sigfillset(&new);
3020Sstevel@tonic-gate (void) pthread_sigmask(SIG_SETMASK, &new, &old);
3030Sstevel@tonic-gate if ((errno = pthread_create(&ti->ti_thread, &thread_attr, thread_start,
3040Sstevel@tonic-gate ti)) != 0) {
3050Sstevel@tonic-gate (void) pthread_sigmask(SIG_SETMASK, &old, NULL);
3060Sstevel@tonic-gate goto fail;
3070Sstevel@tonic-gate }
3080Sstevel@tonic-gate
3090Sstevel@tonic-gate (void) pthread_sigmask(SIG_SETMASK, &old, NULL);
3100Sstevel@tonic-gate return;
3110Sstevel@tonic-gate
3120Sstevel@tonic-gate fail:
3130Sstevel@tonic-gate /*
3140Sstevel@tonic-gate * Since the thread_info structure was never linked onto the
3150Sstevel@tonic-gate * thread list, thread_exiting() can't handle the cleanup.
3160Sstevel@tonic-gate */
3170Sstevel@tonic-gate thread_exiting(NULL);
3180Sstevel@tonic-gate if (ti != NULL)
3190Sstevel@tonic-gate thread_info_free(ti);
3200Sstevel@tonic-gate }
3210Sstevel@tonic-gate
3220Sstevel@tonic-gate int
create_connection(ucred_t * uc,repository_door_request_t * rp,size_t rp_size,int * out_fd)3230Sstevel@tonic-gate create_connection(ucred_t *uc, repository_door_request_t *rp,
3240Sstevel@tonic-gate size_t rp_size, int *out_fd)
3250Sstevel@tonic-gate {
3260Sstevel@tonic-gate int flags;
3270Sstevel@tonic-gate int privileged = 0;
3280Sstevel@tonic-gate uint32_t debugflags = 0;
3290Sstevel@tonic-gate psinfo_t info;
3300Sstevel@tonic-gate
3310Sstevel@tonic-gate if (privileged_pid != 0) {
3320Sstevel@tonic-gate /*
3330Sstevel@tonic-gate * in privileged pid mode, we only allow connections from
3340Sstevel@tonic-gate * our original parent -- the psinfo read verifies that
3350Sstevel@tonic-gate * it is the same process which we started with.
3360Sstevel@tonic-gate */
3370Sstevel@tonic-gate if (ucred_getpid(uc) != privileged_pid ||
3380Sstevel@tonic-gate read(privileged_psinfo_fd, &info, sizeof (info)) !=
3390Sstevel@tonic-gate sizeof (info))
3400Sstevel@tonic-gate return (REPOSITORY_DOOR_FAIL_PERMISSION_DENIED);
3410Sstevel@tonic-gate
3420Sstevel@tonic-gate privileged = 1; /* he gets full privileges */
3430Sstevel@tonic-gate } else if (privileged_user != 0) {
3440Sstevel@tonic-gate /*
3450Sstevel@tonic-gate * in privileged user mode, only one particular user is
3460Sstevel@tonic-gate * allowed to connect to us, and he can do anything.
3470Sstevel@tonic-gate */
3480Sstevel@tonic-gate if (ucred_geteuid(uc) != privileged_user)
3490Sstevel@tonic-gate return (REPOSITORY_DOOR_FAIL_PERMISSION_DENIED);
3500Sstevel@tonic-gate
3510Sstevel@tonic-gate privileged = 1;
3520Sstevel@tonic-gate }
3530Sstevel@tonic-gate
3540Sstevel@tonic-gate /*
3550Sstevel@tonic-gate * Check that rp, of size rp_size, is large enough to
3560Sstevel@tonic-gate * contain field 'f'. If so, write the value into *out, and return 1.
3570Sstevel@tonic-gate * Otherwise, return 0.
3580Sstevel@tonic-gate */
3590Sstevel@tonic-gate #define GET_ARG(rp, rp_size, f, out) \
3600Sstevel@tonic-gate (((rp_size) >= offsetofend(repository_door_request_t, f)) ? \
3610Sstevel@tonic-gate ((*(out) = (rp)->f), 1) : 0)
3620Sstevel@tonic-gate
3630Sstevel@tonic-gate if (!GET_ARG(rp, rp_size, rdr_flags, &flags))
3640Sstevel@tonic-gate return (REPOSITORY_DOOR_FAIL_BAD_REQUEST);
3650Sstevel@tonic-gate
3660Sstevel@tonic-gate #if (REPOSITORY_DOOR_FLAG_ALL != REPOSITORY_DOOR_FLAG_DEBUG)
3670Sstevel@tonic-gate #error Need to update flag checks
3680Sstevel@tonic-gate #endif
3690Sstevel@tonic-gate
3700Sstevel@tonic-gate if (flags & ~REPOSITORY_DOOR_FLAG_ALL)
3710Sstevel@tonic-gate return (REPOSITORY_DOOR_FAIL_BAD_FLAG);
3720Sstevel@tonic-gate
3730Sstevel@tonic-gate if (flags & REPOSITORY_DOOR_FLAG_DEBUG)
3740Sstevel@tonic-gate if (!GET_ARG(rp, rp_size, rdr_debug, &debugflags))
3750Sstevel@tonic-gate return (REPOSITORY_DOOR_FAIL_BAD_REQUEST);
3760Sstevel@tonic-gate #undef GET_ARG
3770Sstevel@tonic-gate
3780Sstevel@tonic-gate return (create_client(ucred_getpid(uc), debugflags, privileged,
3790Sstevel@tonic-gate out_fd));
3800Sstevel@tonic-gate }
3810Sstevel@tonic-gate
3820Sstevel@tonic-gate void
configd_vlog(int severity,const char * prefix,const char * message,va_list args)383*7128Samaguire configd_vlog(int severity, const char *prefix, const char *message,
384*7128Samaguire va_list args)
3850Sstevel@tonic-gate {
3860Sstevel@tonic-gate if (log_to_syslog)
387*7128Samaguire vsyslog(severity, message, args);
3880Sstevel@tonic-gate else {
3890Sstevel@tonic-gate flockfile(stderr);
390*7128Samaguire if (prefix != NULL)
391*7128Samaguire (void) fprintf(stderr, "%s", prefix);
3920Sstevel@tonic-gate (void) vfprintf(stderr, message, args);
3930Sstevel@tonic-gate if (message[0] == 0 || message[strlen(message) - 1] != '\n')
3940Sstevel@tonic-gate (void) fprintf(stderr, "\n");
3950Sstevel@tonic-gate funlockfile(stderr);
3960Sstevel@tonic-gate }
3970Sstevel@tonic-gate }
3980Sstevel@tonic-gate
3990Sstevel@tonic-gate void
configd_vcritical(const char * message,va_list args)400*7128Samaguire configd_vcritical(const char *message, va_list args)
401*7128Samaguire {
402*7128Samaguire configd_vlog(LOG_CRIT, "svc.configd: Fatal error: ", message, args);
403*7128Samaguire }
404*7128Samaguire
405*7128Samaguire void
configd_critical(const char * message,...)4060Sstevel@tonic-gate configd_critical(const char *message, ...)
4070Sstevel@tonic-gate {
4080Sstevel@tonic-gate va_list args;
4090Sstevel@tonic-gate va_start(args, message);
4100Sstevel@tonic-gate configd_vcritical(message, args);
4110Sstevel@tonic-gate va_end(args);
4120Sstevel@tonic-gate }
4130Sstevel@tonic-gate
414*7128Samaguire void
configd_info(const char * message,...)415*7128Samaguire configd_info(const char *message, ...)
416*7128Samaguire {
417*7128Samaguire va_list args;
418*7128Samaguire va_start(args, message);
419*7128Samaguire configd_vlog(LOG_INFO, "svc.configd: ", message, args);
420*7128Samaguire va_end(args);
421*7128Samaguire }
422*7128Samaguire
4230Sstevel@tonic-gate static void
usage(const char * prog,int ret)4240Sstevel@tonic-gate usage(const char *prog, int ret)
4250Sstevel@tonic-gate {
4260Sstevel@tonic-gate (void) fprintf(stderr,
4270Sstevel@tonic-gate "usage: %s [-np] [-d door_path] [-r repository_path]\n"
4280Sstevel@tonic-gate " [-t nonpersist_repository]\n", prog);
4290Sstevel@tonic-gate exit(ret);
4300Sstevel@tonic-gate }
4310Sstevel@tonic-gate
4320Sstevel@tonic-gate /*ARGSUSED*/
4330Sstevel@tonic-gate static void
handler(int sig,siginfo_t * info,void * data)4340Sstevel@tonic-gate handler(int sig, siginfo_t *info, void *data)
4350Sstevel@tonic-gate {
4360Sstevel@tonic-gate finished = 1;
4370Sstevel@tonic-gate }
4380Sstevel@tonic-gate
4390Sstevel@tonic-gate static int pipe_fd = -1;
4400Sstevel@tonic-gate
4410Sstevel@tonic-gate static int
daemonize_start(void)4420Sstevel@tonic-gate daemonize_start(void)
4430Sstevel@tonic-gate {
4440Sstevel@tonic-gate char data;
4450Sstevel@tonic-gate int status;
4460Sstevel@tonic-gate
4470Sstevel@tonic-gate int filedes[2];
4480Sstevel@tonic-gate pid_t pid;
4490Sstevel@tonic-gate
4500Sstevel@tonic-gate (void) close(0);
4510Sstevel@tonic-gate (void) dup2(2, 1); /* stderr only */
4520Sstevel@tonic-gate
4530Sstevel@tonic-gate if (pipe(filedes) < 0)
4540Sstevel@tonic-gate return (-1);
4550Sstevel@tonic-gate
4560Sstevel@tonic-gate if ((pid = fork1()) < 0)
4570Sstevel@tonic-gate return (-1);
4580Sstevel@tonic-gate
4590Sstevel@tonic-gate if (pid != 0) {
4600Sstevel@tonic-gate /*
4610Sstevel@tonic-gate * parent
4620Sstevel@tonic-gate */
4630Sstevel@tonic-gate struct sigaction act;
4640Sstevel@tonic-gate
4650Sstevel@tonic-gate act.sa_sigaction = SIG_DFL;
4660Sstevel@tonic-gate (void) sigemptyset(&act.sa_mask);
4670Sstevel@tonic-gate act.sa_flags = 0;
4680Sstevel@tonic-gate
4690Sstevel@tonic-gate (void) sigaction(SIGPIPE, &act, NULL); /* ignore SIGPIPE */
4700Sstevel@tonic-gate
4710Sstevel@tonic-gate (void) close(filedes[1]);
4720Sstevel@tonic-gate if (read(filedes[0], &data, 1) == 1) {
4730Sstevel@tonic-gate /* presume success */
4740Sstevel@tonic-gate _exit(CONFIGD_EXIT_OKAY);
4750Sstevel@tonic-gate }
4760Sstevel@tonic-gate
4770Sstevel@tonic-gate status = -1;
4780Sstevel@tonic-gate (void) wait4(pid, &status, 0, NULL);
4790Sstevel@tonic-gate if (WIFEXITED(status))
4800Sstevel@tonic-gate _exit(WEXITSTATUS(status));
4810Sstevel@tonic-gate else
4820Sstevel@tonic-gate _exit(-1);
4830Sstevel@tonic-gate }
4840Sstevel@tonic-gate
4850Sstevel@tonic-gate /*
4860Sstevel@tonic-gate * child
4870Sstevel@tonic-gate */
4880Sstevel@tonic-gate pipe_fd = filedes[1];
4890Sstevel@tonic-gate (void) close(filedes[0]);
4900Sstevel@tonic-gate
4910Sstevel@tonic-gate /*
4920Sstevel@tonic-gate * generic Unix setup
4930Sstevel@tonic-gate */
4940Sstevel@tonic-gate (void) setsid();
4950Sstevel@tonic-gate (void) umask(0077);
4960Sstevel@tonic-gate
4970Sstevel@tonic-gate return (0);
4980Sstevel@tonic-gate }
4990Sstevel@tonic-gate
5000Sstevel@tonic-gate static void
daemonize_ready(void)5010Sstevel@tonic-gate daemonize_ready(void)
5020Sstevel@tonic-gate {
5030Sstevel@tonic-gate char data = '\0';
5040Sstevel@tonic-gate
5050Sstevel@tonic-gate /*
5060Sstevel@tonic-gate * wake the parent
5070Sstevel@tonic-gate */
5080Sstevel@tonic-gate (void) write(pipe_fd, &data, 1);
5090Sstevel@tonic-gate (void) close(pipe_fd);
5100Sstevel@tonic-gate }
5110Sstevel@tonic-gate
512407Sjwadams const char *
regularize_path(const char * dir,const char * base,char * tmpbuf)513407Sjwadams regularize_path(const char *dir, const char *base, char *tmpbuf)
514407Sjwadams {
515407Sjwadams if (base == NULL)
516407Sjwadams return (NULL);
517407Sjwadams if (base[0] == '/')
518407Sjwadams return (base);
519407Sjwadams
520407Sjwadams if (snprintf(tmpbuf, PATH_MAX, "%s/%s", dir, base) >= PATH_MAX) {
521407Sjwadams (void) fprintf(stderr, "svc.configd: %s/%s: path too long\n",
522407Sjwadams dir, base);
523407Sjwadams exit(CONFIGD_EXIT_BAD_ARGS);
524407Sjwadams }
525407Sjwadams
526407Sjwadams return (tmpbuf);
527407Sjwadams }
528407Sjwadams
5290Sstevel@tonic-gate int
main(int argc,char * argv[])5300Sstevel@tonic-gate main(int argc, char *argv[])
5310Sstevel@tonic-gate {
5320Sstevel@tonic-gate thread_info_t *ti = &main_thread_info;
5330Sstevel@tonic-gate
5340Sstevel@tonic-gate char pidpath[sizeof ("/proc/" "/psinfo") + 10];
5350Sstevel@tonic-gate
5360Sstevel@tonic-gate struct rlimit fd_new;
5370Sstevel@tonic-gate
5380Sstevel@tonic-gate const char *endptr;
5390Sstevel@tonic-gate sigset_t myset;
5400Sstevel@tonic-gate int c;
5410Sstevel@tonic-gate int ret;
5420Sstevel@tonic-gate int fd;
543407Sjwadams
544407Sjwadams char curdir[PATH_MAX];
545407Sjwadams char dbtmp[PATH_MAX];
546407Sjwadams char npdbtmp[PATH_MAX];
547407Sjwadams char doortmp[PATH_MAX];
548407Sjwadams
5490Sstevel@tonic-gate const char *dbpath = NULL;
5500Sstevel@tonic-gate const char *npdbpath = NULL;
5510Sstevel@tonic-gate const char *doorpath = REPOSITORY_DOOR_NAME;
5520Sstevel@tonic-gate struct sigaction act;
5530Sstevel@tonic-gate
5540Sstevel@tonic-gate int daemonize = 1; /* default to daemonizing */
5550Sstevel@tonic-gate int have_npdb = 1;
5560Sstevel@tonic-gate
5570Sstevel@tonic-gate closefrom(3); /* get rid of extraneous fds */
5580Sstevel@tonic-gate
559407Sjwadams if (getcwd(curdir, sizeof (curdir)) == NULL) {
560407Sjwadams (void) fprintf(stderr,
561407Sjwadams "%s: unable to get current directory: %s\n",
562407Sjwadams argv[0], strerror(errno));
563407Sjwadams exit(CONFIGD_EXIT_INIT_FAILED);
564407Sjwadams }
565407Sjwadams
5660Sstevel@tonic-gate while ((c = getopt(argc, argv, "Dnpd:r:t:")) != -1) {
5670Sstevel@tonic-gate switch (c) {
5680Sstevel@tonic-gate case 'n':
5690Sstevel@tonic-gate daemonize = 0;
5700Sstevel@tonic-gate break;
5710Sstevel@tonic-gate case 'd':
572407Sjwadams doorpath = regularize_path(curdir, optarg, doortmp);
5730Sstevel@tonic-gate have_npdb = 0; /* default to no non-persist */
5740Sstevel@tonic-gate break;
5750Sstevel@tonic-gate case 'p':
5760Sstevel@tonic-gate log_to_syslog = 0; /* don't use syslog */
5770Sstevel@tonic-gate
5780Sstevel@tonic-gate /*
5790Sstevel@tonic-gate * If our parent exits while we're opening its /proc
5800Sstevel@tonic-gate * psinfo, we're vulnerable to a pid wrapping. To
5810Sstevel@tonic-gate * protect against that, re-check our ppid after
5820Sstevel@tonic-gate * opening it.
5830Sstevel@tonic-gate */
5840Sstevel@tonic-gate privileged_pid = getppid();
5850Sstevel@tonic-gate (void) snprintf(pidpath, sizeof (pidpath),
5860Sstevel@tonic-gate "/proc/%d/psinfo", privileged_pid);
5870Sstevel@tonic-gate if ((fd = open(pidpath, O_RDONLY)) < 0 ||
5880Sstevel@tonic-gate getppid() != privileged_pid) {
5890Sstevel@tonic-gate (void) fprintf(stderr,
5900Sstevel@tonic-gate "%s: unable to get parent info\n", argv[0]);
5910Sstevel@tonic-gate exit(CONFIGD_EXIT_BAD_ARGS);
5920Sstevel@tonic-gate }
5930Sstevel@tonic-gate privileged_psinfo_fd = fd;
5940Sstevel@tonic-gate break;
5950Sstevel@tonic-gate case 'r':
596407Sjwadams dbpath = regularize_path(curdir, optarg, dbtmp);
5970Sstevel@tonic-gate is_main_repository = 0;
5980Sstevel@tonic-gate break;
5990Sstevel@tonic-gate case 't':
600407Sjwadams npdbpath = regularize_path(curdir, optarg, npdbtmp);
6010Sstevel@tonic-gate is_main_repository = 0;
6020Sstevel@tonic-gate break;
6030Sstevel@tonic-gate default:
6040Sstevel@tonic-gate usage(argv[0], CONFIGD_EXIT_BAD_ARGS);
6050Sstevel@tonic-gate break;
6060Sstevel@tonic-gate }
6070Sstevel@tonic-gate }
6080Sstevel@tonic-gate
6090Sstevel@tonic-gate /*
6100Sstevel@tonic-gate * If we're not running as root, allow our euid full access, and
6110Sstevel@tonic-gate * everyone else no access.
6120Sstevel@tonic-gate */
6130Sstevel@tonic-gate if (privileged_pid == 0 && geteuid() != 0) {
6140Sstevel@tonic-gate privileged_user = geteuid();
6150Sstevel@tonic-gate }
6160Sstevel@tonic-gate
6170Sstevel@tonic-gate privileged_privs = priv_str_to_set("zone", "", &endptr);
6180Sstevel@tonic-gate if (endptr != NULL && privileged_privs != NULL) {
6190Sstevel@tonic-gate priv_freeset(privileged_privs);
6200Sstevel@tonic-gate privileged_privs = NULL;
6210Sstevel@tonic-gate }
6220Sstevel@tonic-gate
6230Sstevel@tonic-gate openlog("svc.configd", LOG_PID | LOG_CONS, LOG_DAEMON);
6240Sstevel@tonic-gate (void) setlogmask(LOG_UPTO(LOG_NOTICE));
6250Sstevel@tonic-gate
6260Sstevel@tonic-gate /*
6270Sstevel@tonic-gate * if a non-persist db is specified, always enable it
6280Sstevel@tonic-gate */
6290Sstevel@tonic-gate if (npdbpath)
6300Sstevel@tonic-gate have_npdb = 1;
6310Sstevel@tonic-gate
6320Sstevel@tonic-gate if (optind != argc)
6330Sstevel@tonic-gate usage(argv[0], CONFIGD_EXIT_BAD_ARGS);
6340Sstevel@tonic-gate
6350Sstevel@tonic-gate if (daemonize) {
6360Sstevel@tonic-gate if (getuid() == 0)
6370Sstevel@tonic-gate (void) chdir("/");
6380Sstevel@tonic-gate if (daemonize_start() < 0) {
6390Sstevel@tonic-gate (void) perror("unable to daemonize");
6400Sstevel@tonic-gate exit(CONFIGD_EXIT_INIT_FAILED);
6410Sstevel@tonic-gate }
6420Sstevel@tonic-gate }
6430Sstevel@tonic-gate if (getuid() == 0)
6440Sstevel@tonic-gate (void) core_set_process_path(CONFIGD_CORE,
6450Sstevel@tonic-gate strlen(CONFIGD_CORE) + 1, getpid());
6460Sstevel@tonic-gate
6470Sstevel@tonic-gate /*
6480Sstevel@tonic-gate * this should be enabled once we can drop privileges and still get
6490Sstevel@tonic-gate * a core dump.
6500Sstevel@tonic-gate */
6510Sstevel@tonic-gate #if 0
6520Sstevel@tonic-gate /* turn off basic privileges we do not need */
6530Sstevel@tonic-gate (void) priv_set(PRIV_OFF, PRIV_PERMITTED, PRIV_FILE_LINK_ANY,
6540Sstevel@tonic-gate PRIV_PROC_EXEC, PRIV_PROC_FORK, PRIV_PROC_SESSION, NULL);
6550Sstevel@tonic-gate #endif
6560Sstevel@tonic-gate
6570Sstevel@tonic-gate /* not that we can exec, but to be safe, shut them all off... */
6580Sstevel@tonic-gate (void) priv_set(PRIV_SET, PRIV_INHERITABLE, NULL);
6590Sstevel@tonic-gate
6600Sstevel@tonic-gate (void) sigfillset(&act.sa_mask);
6610Sstevel@tonic-gate
6620Sstevel@tonic-gate /* signals to ignore */
6630Sstevel@tonic-gate act.sa_sigaction = SIG_IGN;
6640Sstevel@tonic-gate act.sa_flags = 0;
6650Sstevel@tonic-gate (void) sigaction(SIGPIPE, &act, NULL);
6660Sstevel@tonic-gate (void) sigaction(SIGALRM, &act, NULL);
6670Sstevel@tonic-gate (void) sigaction(SIGUSR1, &act, NULL);
6680Sstevel@tonic-gate (void) sigaction(SIGUSR2, &act, NULL);
6690Sstevel@tonic-gate (void) sigaction(SIGPOLL, &act, NULL);
6700Sstevel@tonic-gate
6710Sstevel@tonic-gate /* signals to abort on */
6720Sstevel@tonic-gate act.sa_sigaction = (void (*)(int, siginfo_t *, void *))&abort_handler;
6730Sstevel@tonic-gate act.sa_flags = SA_SIGINFO;
6740Sstevel@tonic-gate
6750Sstevel@tonic-gate (void) sigaction(SIGABRT, &act, NULL);
6760Sstevel@tonic-gate
6770Sstevel@tonic-gate /* signals to handle */
6780Sstevel@tonic-gate act.sa_sigaction = &handler;
6790Sstevel@tonic-gate act.sa_flags = SA_SIGINFO;
6800Sstevel@tonic-gate
6810Sstevel@tonic-gate (void) sigaction(SIGHUP, &act, NULL);
6820Sstevel@tonic-gate (void) sigaction(SIGINT, &act, NULL);
6830Sstevel@tonic-gate (void) sigaction(SIGTERM, &act, NULL);
6840Sstevel@tonic-gate
6850Sstevel@tonic-gate (void) sigemptyset(&myset);
6860Sstevel@tonic-gate (void) sigaddset(&myset, SIGHUP);
6870Sstevel@tonic-gate (void) sigaddset(&myset, SIGINT);
6880Sstevel@tonic-gate (void) sigaddset(&myset, SIGTERM);
6890Sstevel@tonic-gate
6900Sstevel@tonic-gate if ((errno = pthread_attr_init(&thread_attr)) != 0) {
6910Sstevel@tonic-gate (void) perror("initializing");
6920Sstevel@tonic-gate exit(CONFIGD_EXIT_INIT_FAILED);
6930Sstevel@tonic-gate }
6940Sstevel@tonic-gate
6950Sstevel@tonic-gate /*
6960Sstevel@tonic-gate * Set the hard and soft limits to CONFIGD_MAX_FDS.
6970Sstevel@tonic-gate */
6980Sstevel@tonic-gate fd_new.rlim_max = fd_new.rlim_cur = CONFIGD_MAX_FDS;
6990Sstevel@tonic-gate (void) setrlimit(RLIMIT_NOFILE, &fd_new);
7000Sstevel@tonic-gate
7011914Scasper #ifndef NATIVE_BUILD /* Allow building on snv_38 and earlier; remove later. */
7021914Scasper (void) enable_extended_FILE_stdio(-1, -1);
7031914Scasper #endif
7041914Scasper
7050Sstevel@tonic-gate if ((ret = backend_init(dbpath, npdbpath, have_npdb)) !=
7060Sstevel@tonic-gate CONFIGD_EXIT_OKAY)
7070Sstevel@tonic-gate exit(ret);
7080Sstevel@tonic-gate
7090Sstevel@tonic-gate if (!client_init())
7100Sstevel@tonic-gate exit(CONFIGD_EXIT_INIT_FAILED);
7110Sstevel@tonic-gate
7120Sstevel@tonic-gate if (!rc_node_init())
7130Sstevel@tonic-gate exit(CONFIGD_EXIT_INIT_FAILED);
7140Sstevel@tonic-gate
7150Sstevel@tonic-gate (void) pthread_attr_setdetachstate(&thread_attr,
7160Sstevel@tonic-gate PTHREAD_CREATE_DETACHED);
7170Sstevel@tonic-gate (void) pthread_attr_setscope(&thread_attr, PTHREAD_SCOPE_SYSTEM);
7180Sstevel@tonic-gate
7190Sstevel@tonic-gate if ((errno = pthread_key_create(&thread_info_key,
7200Sstevel@tonic-gate thread_exiting)) != 0) {
7210Sstevel@tonic-gate perror("pthread_key_create");
7220Sstevel@tonic-gate exit(CONFIGD_EXIT_INIT_FAILED);
7230Sstevel@tonic-gate }
7240Sstevel@tonic-gate
7250Sstevel@tonic-gate if ((thread_pool = uu_list_pool_create("thread_pool",
7260Sstevel@tonic-gate sizeof (thread_info_t), offsetof(thread_info_t, ti_node),
7270Sstevel@tonic-gate NULL, UU_LIST_POOL_DEBUG)) == NULL) {
7280Sstevel@tonic-gate configd_critical("uu_list_pool_create: %s\n",
7290Sstevel@tonic-gate uu_strerror(uu_error()));
7300Sstevel@tonic-gate exit(CONFIGD_EXIT_INIT_FAILED);
7310Sstevel@tonic-gate }
7320Sstevel@tonic-gate
7330Sstevel@tonic-gate if ((thread_list = uu_list_create(thread_pool, NULL, 0)) == NULL) {
7340Sstevel@tonic-gate configd_critical("uu_list_create: %s\n",
7350Sstevel@tonic-gate uu_strerror(uu_error()));
7360Sstevel@tonic-gate exit(CONFIGD_EXIT_INIT_FAILED);
7370Sstevel@tonic-gate }
7380Sstevel@tonic-gate
7390Sstevel@tonic-gate (void) memset(ti, '\0', sizeof (*ti));
7400Sstevel@tonic-gate uu_list_node_init(ti, &ti->ti_node, thread_pool);
7410Sstevel@tonic-gate (void) uu_list_insert_before(thread_list, uu_list_first(thread_list),
7420Sstevel@tonic-gate ti);
7430Sstevel@tonic-gate
7440Sstevel@tonic-gate ti->ti_thread = pthread_self();
7450Sstevel@tonic-gate ti->ti_state = TI_SIGNAL_WAIT;
7460Sstevel@tonic-gate ti->ti_prev_state = TI_SIGNAL_WAIT;
7470Sstevel@tonic-gate
7480Sstevel@tonic-gate (void) pthread_setspecific(thread_info_key, ti);
7490Sstevel@tonic-gate
7500Sstevel@tonic-gate (void) door_server_create(new_thread_needed);
7510Sstevel@tonic-gate
7520Sstevel@tonic-gate if (!setup_main_door(doorpath)) {
7530Sstevel@tonic-gate configd_critical("Setting up main door failed.\n");
7540Sstevel@tonic-gate exit(CONFIGD_EXIT_DOOR_INIT_FAILED);
7550Sstevel@tonic-gate }
7560Sstevel@tonic-gate
7570Sstevel@tonic-gate if (daemonize)
7580Sstevel@tonic-gate daemonize_ready();
7590Sstevel@tonic-gate
7600Sstevel@tonic-gate (void) pthread_sigmask(SIG_BLOCK, &myset, NULL);
7610Sstevel@tonic-gate while (!finished) {
7620Sstevel@tonic-gate int sig = sigwait(&myset);
7630Sstevel@tonic-gate if (sig > 0) {
7640Sstevel@tonic-gate break;
7650Sstevel@tonic-gate }
7660Sstevel@tonic-gate }
7670Sstevel@tonic-gate
7680Sstevel@tonic-gate backend_fini();
7690Sstevel@tonic-gate
7700Sstevel@tonic-gate return (CONFIGD_EXIT_OKAY);
7710Sstevel@tonic-gate }
772