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 /* 22*5777Stw21770 * 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 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 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 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 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 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 * 1990Sstevel@tonic-gate thread_self(void) 2000Sstevel@tonic-gate { 2010Sstevel@tonic-gate return (pthread_getspecific(thread_info_key)); 2020Sstevel@tonic-gate } 2030Sstevel@tonic-gate 204*5777Stw21770 /* 205*5777Stw21770 * get_ucred() returns NULL if it was unable to get the credential 206*5777Stw21770 * information. 207*5777Stw21770 */ 2080Sstevel@tonic-gate ucred_t * 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 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 241*5777Stw21770 /* 242*5777Stw21770 * The purpose of this function is to get the audit session data for use in 243*5777Stw21770 * generating SMF audit events. We use a single audit session per client. 244*5777Stw21770 * 245*5777Stw21770 * get_audit_session() may return NULL. It is legal to use a NULL pointer 246*5777Stw21770 * in subsequent calls to adt_* functions. 247*5777Stw21770 */ 248*5777Stw21770 adt_session_data_t * 249*5777Stw21770 get_audit_session(void) 250*5777Stw21770 { 251*5777Stw21770 thread_info_t *ti = thread_self(); 252*5777Stw21770 253*5777Stw21770 return (ti->ti_active_client->rc_adt_session); 254*5777Stw21770 } 255*5777Stw21770 2560Sstevel@tonic-gate static void * 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 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 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 3830Sstevel@tonic-gate configd_vcritical(const char *message, va_list args) 3840Sstevel@tonic-gate { 3850Sstevel@tonic-gate if (log_to_syslog) 3860Sstevel@tonic-gate vsyslog(LOG_CRIT, message, args); 3870Sstevel@tonic-gate else { 3880Sstevel@tonic-gate flockfile(stderr); 3890Sstevel@tonic-gate (void) fprintf(stderr, "svc.configd: Fatal error: "); 3900Sstevel@tonic-gate (void) vfprintf(stderr, message, args); 3910Sstevel@tonic-gate if (message[0] == 0 || message[strlen(message) - 1] != '\n') 3920Sstevel@tonic-gate (void) fprintf(stderr, "\n"); 3930Sstevel@tonic-gate funlockfile(stderr); 3940Sstevel@tonic-gate } 3950Sstevel@tonic-gate } 3960Sstevel@tonic-gate 3970Sstevel@tonic-gate void 3980Sstevel@tonic-gate configd_critical(const char *message, ...) 3990Sstevel@tonic-gate { 4000Sstevel@tonic-gate va_list args; 4010Sstevel@tonic-gate va_start(args, message); 4020Sstevel@tonic-gate configd_vcritical(message, args); 4030Sstevel@tonic-gate va_end(args); 4040Sstevel@tonic-gate } 4050Sstevel@tonic-gate 4060Sstevel@tonic-gate static void 4070Sstevel@tonic-gate usage(const char *prog, int ret) 4080Sstevel@tonic-gate { 4090Sstevel@tonic-gate (void) fprintf(stderr, 4100Sstevel@tonic-gate "usage: %s [-np] [-d door_path] [-r repository_path]\n" 4110Sstevel@tonic-gate " [-t nonpersist_repository]\n", prog); 4120Sstevel@tonic-gate exit(ret); 4130Sstevel@tonic-gate } 4140Sstevel@tonic-gate 4150Sstevel@tonic-gate /*ARGSUSED*/ 4160Sstevel@tonic-gate static void 4170Sstevel@tonic-gate handler(int sig, siginfo_t *info, void *data) 4180Sstevel@tonic-gate { 4190Sstevel@tonic-gate finished = 1; 4200Sstevel@tonic-gate } 4210Sstevel@tonic-gate 4220Sstevel@tonic-gate static int pipe_fd = -1; 4230Sstevel@tonic-gate 4240Sstevel@tonic-gate static int 4250Sstevel@tonic-gate daemonize_start(void) 4260Sstevel@tonic-gate { 4270Sstevel@tonic-gate char data; 4280Sstevel@tonic-gate int status; 4290Sstevel@tonic-gate 4300Sstevel@tonic-gate int filedes[2]; 4310Sstevel@tonic-gate pid_t pid; 4320Sstevel@tonic-gate 4330Sstevel@tonic-gate (void) close(0); 4340Sstevel@tonic-gate (void) dup2(2, 1); /* stderr only */ 4350Sstevel@tonic-gate 4360Sstevel@tonic-gate if (pipe(filedes) < 0) 4370Sstevel@tonic-gate return (-1); 4380Sstevel@tonic-gate 4390Sstevel@tonic-gate if ((pid = fork1()) < 0) 4400Sstevel@tonic-gate return (-1); 4410Sstevel@tonic-gate 4420Sstevel@tonic-gate if (pid != 0) { 4430Sstevel@tonic-gate /* 4440Sstevel@tonic-gate * parent 4450Sstevel@tonic-gate */ 4460Sstevel@tonic-gate struct sigaction act; 4470Sstevel@tonic-gate 4480Sstevel@tonic-gate act.sa_sigaction = SIG_DFL; 4490Sstevel@tonic-gate (void) sigemptyset(&act.sa_mask); 4500Sstevel@tonic-gate act.sa_flags = 0; 4510Sstevel@tonic-gate 4520Sstevel@tonic-gate (void) sigaction(SIGPIPE, &act, NULL); /* ignore SIGPIPE */ 4530Sstevel@tonic-gate 4540Sstevel@tonic-gate (void) close(filedes[1]); 4550Sstevel@tonic-gate if (read(filedes[0], &data, 1) == 1) { 4560Sstevel@tonic-gate /* presume success */ 4570Sstevel@tonic-gate _exit(CONFIGD_EXIT_OKAY); 4580Sstevel@tonic-gate } 4590Sstevel@tonic-gate 4600Sstevel@tonic-gate status = -1; 4610Sstevel@tonic-gate (void) wait4(pid, &status, 0, NULL); 4620Sstevel@tonic-gate if (WIFEXITED(status)) 4630Sstevel@tonic-gate _exit(WEXITSTATUS(status)); 4640Sstevel@tonic-gate else 4650Sstevel@tonic-gate _exit(-1); 4660Sstevel@tonic-gate } 4670Sstevel@tonic-gate 4680Sstevel@tonic-gate /* 4690Sstevel@tonic-gate * child 4700Sstevel@tonic-gate */ 4710Sstevel@tonic-gate pipe_fd = filedes[1]; 4720Sstevel@tonic-gate (void) close(filedes[0]); 4730Sstevel@tonic-gate 4740Sstevel@tonic-gate /* 4750Sstevel@tonic-gate * generic Unix setup 4760Sstevel@tonic-gate */ 4770Sstevel@tonic-gate (void) setsid(); 4780Sstevel@tonic-gate (void) umask(0077); 4790Sstevel@tonic-gate 4800Sstevel@tonic-gate return (0); 4810Sstevel@tonic-gate } 4820Sstevel@tonic-gate 4830Sstevel@tonic-gate static void 4840Sstevel@tonic-gate daemonize_ready(void) 4850Sstevel@tonic-gate { 4860Sstevel@tonic-gate char data = '\0'; 4870Sstevel@tonic-gate 4880Sstevel@tonic-gate /* 4890Sstevel@tonic-gate * wake the parent 4900Sstevel@tonic-gate */ 4910Sstevel@tonic-gate (void) write(pipe_fd, &data, 1); 4920Sstevel@tonic-gate (void) close(pipe_fd); 4930Sstevel@tonic-gate } 4940Sstevel@tonic-gate 495407Sjwadams const char * 496407Sjwadams regularize_path(const char *dir, const char *base, char *tmpbuf) 497407Sjwadams { 498407Sjwadams if (base == NULL) 499407Sjwadams return (NULL); 500407Sjwadams if (base[0] == '/') 501407Sjwadams return (base); 502407Sjwadams 503407Sjwadams if (snprintf(tmpbuf, PATH_MAX, "%s/%s", dir, base) >= PATH_MAX) { 504407Sjwadams (void) fprintf(stderr, "svc.configd: %s/%s: path too long\n", 505407Sjwadams dir, base); 506407Sjwadams exit(CONFIGD_EXIT_BAD_ARGS); 507407Sjwadams } 508407Sjwadams 509407Sjwadams return (tmpbuf); 510407Sjwadams } 511407Sjwadams 5120Sstevel@tonic-gate int 5130Sstevel@tonic-gate main(int argc, char *argv[]) 5140Sstevel@tonic-gate { 5150Sstevel@tonic-gate thread_info_t *ti = &main_thread_info; 5160Sstevel@tonic-gate 5170Sstevel@tonic-gate char pidpath[sizeof ("/proc/" "/psinfo") + 10]; 5180Sstevel@tonic-gate 5190Sstevel@tonic-gate struct rlimit fd_new; 5200Sstevel@tonic-gate 5210Sstevel@tonic-gate const char *endptr; 5220Sstevel@tonic-gate sigset_t myset; 5230Sstevel@tonic-gate int c; 5240Sstevel@tonic-gate int ret; 5250Sstevel@tonic-gate int fd; 526407Sjwadams 527407Sjwadams char curdir[PATH_MAX]; 528407Sjwadams char dbtmp[PATH_MAX]; 529407Sjwadams char npdbtmp[PATH_MAX]; 530407Sjwadams char doortmp[PATH_MAX]; 531407Sjwadams 5320Sstevel@tonic-gate const char *dbpath = NULL; 5330Sstevel@tonic-gate const char *npdbpath = NULL; 5340Sstevel@tonic-gate const char *doorpath = REPOSITORY_DOOR_NAME; 5350Sstevel@tonic-gate struct sigaction act; 5360Sstevel@tonic-gate 5370Sstevel@tonic-gate int daemonize = 1; /* default to daemonizing */ 5380Sstevel@tonic-gate int have_npdb = 1; 5390Sstevel@tonic-gate 5400Sstevel@tonic-gate closefrom(3); /* get rid of extraneous fds */ 5410Sstevel@tonic-gate 542407Sjwadams if (getcwd(curdir, sizeof (curdir)) == NULL) { 543407Sjwadams (void) fprintf(stderr, 544407Sjwadams "%s: unable to get current directory: %s\n", 545407Sjwadams argv[0], strerror(errno)); 546407Sjwadams exit(CONFIGD_EXIT_INIT_FAILED); 547407Sjwadams } 548407Sjwadams 5490Sstevel@tonic-gate while ((c = getopt(argc, argv, "Dnpd:r:t:")) != -1) { 5500Sstevel@tonic-gate switch (c) { 5510Sstevel@tonic-gate case 'n': 5520Sstevel@tonic-gate daemonize = 0; 5530Sstevel@tonic-gate break; 5540Sstevel@tonic-gate case 'd': 555407Sjwadams doorpath = regularize_path(curdir, optarg, doortmp); 5560Sstevel@tonic-gate have_npdb = 0; /* default to no non-persist */ 5570Sstevel@tonic-gate break; 5580Sstevel@tonic-gate case 'p': 5590Sstevel@tonic-gate log_to_syslog = 0; /* don't use syslog */ 5600Sstevel@tonic-gate 5610Sstevel@tonic-gate /* 5620Sstevel@tonic-gate * If our parent exits while we're opening its /proc 5630Sstevel@tonic-gate * psinfo, we're vulnerable to a pid wrapping. To 5640Sstevel@tonic-gate * protect against that, re-check our ppid after 5650Sstevel@tonic-gate * opening it. 5660Sstevel@tonic-gate */ 5670Sstevel@tonic-gate privileged_pid = getppid(); 5680Sstevel@tonic-gate (void) snprintf(pidpath, sizeof (pidpath), 5690Sstevel@tonic-gate "/proc/%d/psinfo", privileged_pid); 5700Sstevel@tonic-gate if ((fd = open(pidpath, O_RDONLY)) < 0 || 5710Sstevel@tonic-gate getppid() != privileged_pid) { 5720Sstevel@tonic-gate (void) fprintf(stderr, 5730Sstevel@tonic-gate "%s: unable to get parent info\n", argv[0]); 5740Sstevel@tonic-gate exit(CONFIGD_EXIT_BAD_ARGS); 5750Sstevel@tonic-gate } 5760Sstevel@tonic-gate privileged_psinfo_fd = fd; 5770Sstevel@tonic-gate break; 5780Sstevel@tonic-gate case 'r': 579407Sjwadams dbpath = regularize_path(curdir, optarg, dbtmp); 5800Sstevel@tonic-gate is_main_repository = 0; 5810Sstevel@tonic-gate break; 5820Sstevel@tonic-gate case 't': 583407Sjwadams npdbpath = regularize_path(curdir, optarg, npdbtmp); 5840Sstevel@tonic-gate is_main_repository = 0; 5850Sstevel@tonic-gate break; 5860Sstevel@tonic-gate default: 5870Sstevel@tonic-gate usage(argv[0], CONFIGD_EXIT_BAD_ARGS); 5880Sstevel@tonic-gate break; 5890Sstevel@tonic-gate } 5900Sstevel@tonic-gate } 5910Sstevel@tonic-gate 5920Sstevel@tonic-gate /* 5930Sstevel@tonic-gate * If we're not running as root, allow our euid full access, and 5940Sstevel@tonic-gate * everyone else no access. 5950Sstevel@tonic-gate */ 5960Sstevel@tonic-gate if (privileged_pid == 0 && geteuid() != 0) { 5970Sstevel@tonic-gate privileged_user = geteuid(); 5980Sstevel@tonic-gate } 5990Sstevel@tonic-gate 6000Sstevel@tonic-gate privileged_privs = priv_str_to_set("zone", "", &endptr); 6010Sstevel@tonic-gate if (endptr != NULL && privileged_privs != NULL) { 6020Sstevel@tonic-gate priv_freeset(privileged_privs); 6030Sstevel@tonic-gate privileged_privs = NULL; 6040Sstevel@tonic-gate } 6050Sstevel@tonic-gate 6060Sstevel@tonic-gate openlog("svc.configd", LOG_PID | LOG_CONS, LOG_DAEMON); 6070Sstevel@tonic-gate (void) setlogmask(LOG_UPTO(LOG_NOTICE)); 6080Sstevel@tonic-gate 6090Sstevel@tonic-gate /* 6100Sstevel@tonic-gate * if a non-persist db is specified, always enable it 6110Sstevel@tonic-gate */ 6120Sstevel@tonic-gate if (npdbpath) 6130Sstevel@tonic-gate have_npdb = 1; 6140Sstevel@tonic-gate 6150Sstevel@tonic-gate if (optind != argc) 6160Sstevel@tonic-gate usage(argv[0], CONFIGD_EXIT_BAD_ARGS); 6170Sstevel@tonic-gate 6180Sstevel@tonic-gate if (daemonize) { 6190Sstevel@tonic-gate if (getuid() == 0) 6200Sstevel@tonic-gate (void) chdir("/"); 6210Sstevel@tonic-gate if (daemonize_start() < 0) { 6220Sstevel@tonic-gate (void) perror("unable to daemonize"); 6230Sstevel@tonic-gate exit(CONFIGD_EXIT_INIT_FAILED); 6240Sstevel@tonic-gate } 6250Sstevel@tonic-gate } 6260Sstevel@tonic-gate if (getuid() == 0) 6270Sstevel@tonic-gate (void) core_set_process_path(CONFIGD_CORE, 6280Sstevel@tonic-gate strlen(CONFIGD_CORE) + 1, getpid()); 6290Sstevel@tonic-gate 6300Sstevel@tonic-gate /* 6310Sstevel@tonic-gate * this should be enabled once we can drop privileges and still get 6320Sstevel@tonic-gate * a core dump. 6330Sstevel@tonic-gate */ 6340Sstevel@tonic-gate #if 0 6350Sstevel@tonic-gate /* turn off basic privileges we do not need */ 6360Sstevel@tonic-gate (void) priv_set(PRIV_OFF, PRIV_PERMITTED, PRIV_FILE_LINK_ANY, 6370Sstevel@tonic-gate PRIV_PROC_EXEC, PRIV_PROC_FORK, PRIV_PROC_SESSION, NULL); 6380Sstevel@tonic-gate #endif 6390Sstevel@tonic-gate 6400Sstevel@tonic-gate /* not that we can exec, but to be safe, shut them all off... */ 6410Sstevel@tonic-gate (void) priv_set(PRIV_SET, PRIV_INHERITABLE, NULL); 6420Sstevel@tonic-gate 6430Sstevel@tonic-gate (void) sigfillset(&act.sa_mask); 6440Sstevel@tonic-gate 6450Sstevel@tonic-gate /* signals to ignore */ 6460Sstevel@tonic-gate act.sa_sigaction = SIG_IGN; 6470Sstevel@tonic-gate act.sa_flags = 0; 6480Sstevel@tonic-gate (void) sigaction(SIGPIPE, &act, NULL); 6490Sstevel@tonic-gate (void) sigaction(SIGALRM, &act, NULL); 6500Sstevel@tonic-gate (void) sigaction(SIGUSR1, &act, NULL); 6510Sstevel@tonic-gate (void) sigaction(SIGUSR2, &act, NULL); 6520Sstevel@tonic-gate (void) sigaction(SIGPOLL, &act, NULL); 6530Sstevel@tonic-gate 6540Sstevel@tonic-gate /* signals to abort on */ 6550Sstevel@tonic-gate act.sa_sigaction = (void (*)(int, siginfo_t *, void *))&abort_handler; 6560Sstevel@tonic-gate act.sa_flags = SA_SIGINFO; 6570Sstevel@tonic-gate 6580Sstevel@tonic-gate (void) sigaction(SIGABRT, &act, NULL); 6590Sstevel@tonic-gate 6600Sstevel@tonic-gate /* signals to handle */ 6610Sstevel@tonic-gate act.sa_sigaction = &handler; 6620Sstevel@tonic-gate act.sa_flags = SA_SIGINFO; 6630Sstevel@tonic-gate 6640Sstevel@tonic-gate (void) sigaction(SIGHUP, &act, NULL); 6650Sstevel@tonic-gate (void) sigaction(SIGINT, &act, NULL); 6660Sstevel@tonic-gate (void) sigaction(SIGTERM, &act, NULL); 6670Sstevel@tonic-gate 6680Sstevel@tonic-gate (void) sigemptyset(&myset); 6690Sstevel@tonic-gate (void) sigaddset(&myset, SIGHUP); 6700Sstevel@tonic-gate (void) sigaddset(&myset, SIGINT); 6710Sstevel@tonic-gate (void) sigaddset(&myset, SIGTERM); 6720Sstevel@tonic-gate 6730Sstevel@tonic-gate if ((errno = pthread_attr_init(&thread_attr)) != 0) { 6740Sstevel@tonic-gate (void) perror("initializing"); 6750Sstevel@tonic-gate exit(CONFIGD_EXIT_INIT_FAILED); 6760Sstevel@tonic-gate } 6770Sstevel@tonic-gate 6780Sstevel@tonic-gate /* 6790Sstevel@tonic-gate * Set the hard and soft limits to CONFIGD_MAX_FDS. 6800Sstevel@tonic-gate */ 6810Sstevel@tonic-gate fd_new.rlim_max = fd_new.rlim_cur = CONFIGD_MAX_FDS; 6820Sstevel@tonic-gate (void) setrlimit(RLIMIT_NOFILE, &fd_new); 6830Sstevel@tonic-gate 6841914Scasper #ifndef NATIVE_BUILD /* Allow building on snv_38 and earlier; remove later. */ 6851914Scasper (void) enable_extended_FILE_stdio(-1, -1); 6861914Scasper #endif 6871914Scasper 6880Sstevel@tonic-gate if ((ret = backend_init(dbpath, npdbpath, have_npdb)) != 6890Sstevel@tonic-gate CONFIGD_EXIT_OKAY) 6900Sstevel@tonic-gate exit(ret); 6910Sstevel@tonic-gate 6920Sstevel@tonic-gate if (!client_init()) 6930Sstevel@tonic-gate exit(CONFIGD_EXIT_INIT_FAILED); 6940Sstevel@tonic-gate 6950Sstevel@tonic-gate if (!rc_node_init()) 6960Sstevel@tonic-gate exit(CONFIGD_EXIT_INIT_FAILED); 6970Sstevel@tonic-gate 6980Sstevel@tonic-gate (void) pthread_attr_setdetachstate(&thread_attr, 6990Sstevel@tonic-gate PTHREAD_CREATE_DETACHED); 7000Sstevel@tonic-gate (void) pthread_attr_setscope(&thread_attr, PTHREAD_SCOPE_SYSTEM); 7010Sstevel@tonic-gate 7020Sstevel@tonic-gate if ((errno = pthread_key_create(&thread_info_key, 7030Sstevel@tonic-gate thread_exiting)) != 0) { 7040Sstevel@tonic-gate perror("pthread_key_create"); 7050Sstevel@tonic-gate exit(CONFIGD_EXIT_INIT_FAILED); 7060Sstevel@tonic-gate } 7070Sstevel@tonic-gate 7080Sstevel@tonic-gate if ((thread_pool = uu_list_pool_create("thread_pool", 7090Sstevel@tonic-gate sizeof (thread_info_t), offsetof(thread_info_t, ti_node), 7100Sstevel@tonic-gate NULL, UU_LIST_POOL_DEBUG)) == NULL) { 7110Sstevel@tonic-gate configd_critical("uu_list_pool_create: %s\n", 7120Sstevel@tonic-gate uu_strerror(uu_error())); 7130Sstevel@tonic-gate exit(CONFIGD_EXIT_INIT_FAILED); 7140Sstevel@tonic-gate } 7150Sstevel@tonic-gate 7160Sstevel@tonic-gate if ((thread_list = uu_list_create(thread_pool, NULL, 0)) == NULL) { 7170Sstevel@tonic-gate configd_critical("uu_list_create: %s\n", 7180Sstevel@tonic-gate uu_strerror(uu_error())); 7190Sstevel@tonic-gate exit(CONFIGD_EXIT_INIT_FAILED); 7200Sstevel@tonic-gate } 7210Sstevel@tonic-gate 7220Sstevel@tonic-gate (void) memset(ti, '\0', sizeof (*ti)); 7230Sstevel@tonic-gate uu_list_node_init(ti, &ti->ti_node, thread_pool); 7240Sstevel@tonic-gate (void) uu_list_insert_before(thread_list, uu_list_first(thread_list), 7250Sstevel@tonic-gate ti); 7260Sstevel@tonic-gate 7270Sstevel@tonic-gate ti->ti_thread = pthread_self(); 7280Sstevel@tonic-gate ti->ti_state = TI_SIGNAL_WAIT; 7290Sstevel@tonic-gate ti->ti_prev_state = TI_SIGNAL_WAIT; 7300Sstevel@tonic-gate 7310Sstevel@tonic-gate (void) pthread_setspecific(thread_info_key, ti); 7320Sstevel@tonic-gate 7330Sstevel@tonic-gate (void) door_server_create(new_thread_needed); 7340Sstevel@tonic-gate 7350Sstevel@tonic-gate if (!setup_main_door(doorpath)) { 7360Sstevel@tonic-gate configd_critical("Setting up main door failed.\n"); 7370Sstevel@tonic-gate exit(CONFIGD_EXIT_DOOR_INIT_FAILED); 7380Sstevel@tonic-gate } 7390Sstevel@tonic-gate 7400Sstevel@tonic-gate if (daemonize) 7410Sstevel@tonic-gate daemonize_ready(); 7420Sstevel@tonic-gate 7430Sstevel@tonic-gate (void) pthread_sigmask(SIG_BLOCK, &myset, NULL); 7440Sstevel@tonic-gate while (!finished) { 7450Sstevel@tonic-gate int sig = sigwait(&myset); 7460Sstevel@tonic-gate if (sig > 0) { 7470Sstevel@tonic-gate break; 7480Sstevel@tonic-gate } 7490Sstevel@tonic-gate } 7500Sstevel@tonic-gate 7510Sstevel@tonic-gate backend_fini(); 7520Sstevel@tonic-gate 7530Sstevel@tonic-gate return (CONFIGD_EXIT_OKAY); 7540Sstevel@tonic-gate } 755