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 50Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 60Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 70Sstevel@tonic-gate * with the License. 80Sstevel@tonic-gate * 90Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 100Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 110Sstevel@tonic-gate * See the License for the specific language governing permissions 120Sstevel@tonic-gate * and limitations under the License. 130Sstevel@tonic-gate * 140Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 150Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 160Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 170Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 180Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 190Sstevel@tonic-gate * 200Sstevel@tonic-gate * CDDL HEADER END 210Sstevel@tonic-gate */ 220Sstevel@tonic-gate /* 23*407Sjwadams * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 240Sstevel@tonic-gate * Use is subject to license terms. 250Sstevel@tonic-gate */ 260Sstevel@tonic-gate 270Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 280Sstevel@tonic-gate 290Sstevel@tonic-gate #include <assert.h> 300Sstevel@tonic-gate #include <door.h> 310Sstevel@tonic-gate #include <errno.h> 320Sstevel@tonic-gate #include <fcntl.h> 33*407Sjwadams #include <limits.h> 340Sstevel@tonic-gate #include <priv.h> 350Sstevel@tonic-gate #include <procfs.h> 360Sstevel@tonic-gate #include <pthread.h> 370Sstevel@tonic-gate #include <signal.h> 380Sstevel@tonic-gate #include <stdarg.h> 390Sstevel@tonic-gate #include <stdio.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 166*407Sjwadams if (ti != NULL) 167*407Sjwadams 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 2040Sstevel@tonic-gate ucred_t * 2050Sstevel@tonic-gate get_ucred(void) 2060Sstevel@tonic-gate { 2070Sstevel@tonic-gate thread_info_t *ti = thread_self(); 2080Sstevel@tonic-gate ucred_t **ret = &ti->ti_ucred; 2090Sstevel@tonic-gate 2100Sstevel@tonic-gate if (ti->ti_ucred_read) 2110Sstevel@tonic-gate return (*ret); /* cached value */ 2120Sstevel@tonic-gate 2130Sstevel@tonic-gate if (door_ucred(ret) != 0) 2140Sstevel@tonic-gate return (NULL); 2150Sstevel@tonic-gate ti->ti_ucred_read = 1; 2160Sstevel@tonic-gate 2170Sstevel@tonic-gate return (*ret); 2180Sstevel@tonic-gate } 2190Sstevel@tonic-gate 2200Sstevel@tonic-gate int 2210Sstevel@tonic-gate ucred_is_privileged(ucred_t *uc) 2220Sstevel@tonic-gate { 2230Sstevel@tonic-gate const priv_set_t *ps; 2240Sstevel@tonic-gate 2250Sstevel@tonic-gate if ((ps = ucred_getprivset(uc, PRIV_EFFECTIVE)) != NULL) { 2260Sstevel@tonic-gate if (priv_isfullset(ps)) 2270Sstevel@tonic-gate return (1); /* process has all privs */ 2280Sstevel@tonic-gate 2290Sstevel@tonic-gate if (privileged_privs != NULL && 2300Sstevel@tonic-gate priv_issubset(privileged_privs, ps)) 2310Sstevel@tonic-gate return (1); /* process has zone privs */ 2320Sstevel@tonic-gate } 2330Sstevel@tonic-gate 2340Sstevel@tonic-gate return (0); 2350Sstevel@tonic-gate } 2360Sstevel@tonic-gate 2370Sstevel@tonic-gate static void * 2380Sstevel@tonic-gate thread_start(void *arg) 2390Sstevel@tonic-gate { 2400Sstevel@tonic-gate thread_info_t *ti = arg; 2410Sstevel@tonic-gate 2420Sstevel@tonic-gate (void) pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL); 2430Sstevel@tonic-gate 2440Sstevel@tonic-gate (void) pthread_mutex_lock(&thread_lock); 2450Sstevel@tonic-gate num_started++; 2460Sstevel@tonic-gate (void) uu_list_insert_after(thread_list, uu_list_last(thread_list), 2470Sstevel@tonic-gate ti); 2480Sstevel@tonic-gate (void) pthread_mutex_unlock(&thread_lock); 2490Sstevel@tonic-gate (void) pthread_setspecific(thread_info_key, ti); 2500Sstevel@tonic-gate 2510Sstevel@tonic-gate thread_newstate(ti, TI_DOOR_RETURN); 2520Sstevel@tonic-gate 2530Sstevel@tonic-gate /* 2540Sstevel@tonic-gate * Start handling door calls 2550Sstevel@tonic-gate */ 2560Sstevel@tonic-gate (void) door_return(NULL, 0, NULL, 0); 2570Sstevel@tonic-gate return (arg); 2580Sstevel@tonic-gate } 2590Sstevel@tonic-gate 2600Sstevel@tonic-gate static void 2610Sstevel@tonic-gate new_thread_needed(door_info_t *dip) 2620Sstevel@tonic-gate { 2630Sstevel@tonic-gate thread_info_t *ti; 2640Sstevel@tonic-gate 2650Sstevel@tonic-gate sigset_t new, old; 2660Sstevel@tonic-gate 2670Sstevel@tonic-gate assert(dip == NULL); 2680Sstevel@tonic-gate 2690Sstevel@tonic-gate if (!reserve_new_thread()) 2700Sstevel@tonic-gate return; 2710Sstevel@tonic-gate 2720Sstevel@tonic-gate if ((ti = uu_zalloc(sizeof (*ti))) == NULL) 2730Sstevel@tonic-gate goto fail; 2740Sstevel@tonic-gate 2750Sstevel@tonic-gate uu_list_node_init(ti, &ti->ti_node, thread_pool); 2760Sstevel@tonic-gate ti->ti_state = TI_CREATED; 2770Sstevel@tonic-gate ti->ti_prev_state = TI_CREATED; 2780Sstevel@tonic-gate 2790Sstevel@tonic-gate if ((ti->ti_ucred = uu_zalloc(ucred_size())) == NULL) 2800Sstevel@tonic-gate goto fail; 2810Sstevel@tonic-gate 2820Sstevel@tonic-gate (void) sigfillset(&new); 2830Sstevel@tonic-gate (void) pthread_sigmask(SIG_SETMASK, &new, &old); 2840Sstevel@tonic-gate if ((errno = pthread_create(&ti->ti_thread, &thread_attr, thread_start, 2850Sstevel@tonic-gate ti)) != 0) { 2860Sstevel@tonic-gate (void) pthread_sigmask(SIG_SETMASK, &old, NULL); 2870Sstevel@tonic-gate goto fail; 2880Sstevel@tonic-gate } 2890Sstevel@tonic-gate 2900Sstevel@tonic-gate (void) pthread_sigmask(SIG_SETMASK, &old, NULL); 2910Sstevel@tonic-gate return; 2920Sstevel@tonic-gate 2930Sstevel@tonic-gate fail: 2940Sstevel@tonic-gate /* 2950Sstevel@tonic-gate * Since the thread_info structure was never linked onto the 2960Sstevel@tonic-gate * thread list, thread_exiting() can't handle the cleanup. 2970Sstevel@tonic-gate */ 2980Sstevel@tonic-gate thread_exiting(NULL); 2990Sstevel@tonic-gate if (ti != NULL) 3000Sstevel@tonic-gate thread_info_free(ti); 3010Sstevel@tonic-gate } 3020Sstevel@tonic-gate 3030Sstevel@tonic-gate int 3040Sstevel@tonic-gate create_connection(ucred_t *uc, repository_door_request_t *rp, 3050Sstevel@tonic-gate size_t rp_size, int *out_fd) 3060Sstevel@tonic-gate { 3070Sstevel@tonic-gate int flags; 3080Sstevel@tonic-gate int privileged = 0; 3090Sstevel@tonic-gate uint32_t debugflags = 0; 3100Sstevel@tonic-gate psinfo_t info; 3110Sstevel@tonic-gate 3120Sstevel@tonic-gate if (privileged_pid != 0) { 3130Sstevel@tonic-gate /* 3140Sstevel@tonic-gate * in privileged pid mode, we only allow connections from 3150Sstevel@tonic-gate * our original parent -- the psinfo read verifies that 3160Sstevel@tonic-gate * it is the same process which we started with. 3170Sstevel@tonic-gate */ 3180Sstevel@tonic-gate if (ucred_getpid(uc) != privileged_pid || 3190Sstevel@tonic-gate read(privileged_psinfo_fd, &info, sizeof (info)) != 3200Sstevel@tonic-gate sizeof (info)) 3210Sstevel@tonic-gate return (REPOSITORY_DOOR_FAIL_PERMISSION_DENIED); 3220Sstevel@tonic-gate 3230Sstevel@tonic-gate privileged = 1; /* he gets full privileges */ 3240Sstevel@tonic-gate } else if (privileged_user != 0) { 3250Sstevel@tonic-gate /* 3260Sstevel@tonic-gate * in privileged user mode, only one particular user is 3270Sstevel@tonic-gate * allowed to connect to us, and he can do anything. 3280Sstevel@tonic-gate */ 3290Sstevel@tonic-gate if (ucred_geteuid(uc) != privileged_user) 3300Sstevel@tonic-gate return (REPOSITORY_DOOR_FAIL_PERMISSION_DENIED); 3310Sstevel@tonic-gate 3320Sstevel@tonic-gate privileged = 1; 3330Sstevel@tonic-gate } 3340Sstevel@tonic-gate 3350Sstevel@tonic-gate /* 3360Sstevel@tonic-gate * Check that rp, of size rp_size, is large enough to 3370Sstevel@tonic-gate * contain field 'f'. If so, write the value into *out, and return 1. 3380Sstevel@tonic-gate * Otherwise, return 0. 3390Sstevel@tonic-gate */ 3400Sstevel@tonic-gate #define GET_ARG(rp, rp_size, f, out) \ 3410Sstevel@tonic-gate (((rp_size) >= offsetofend(repository_door_request_t, f)) ? \ 3420Sstevel@tonic-gate ((*(out) = (rp)->f), 1) : 0) 3430Sstevel@tonic-gate 3440Sstevel@tonic-gate if (!GET_ARG(rp, rp_size, rdr_flags, &flags)) 3450Sstevel@tonic-gate return (REPOSITORY_DOOR_FAIL_BAD_REQUEST); 3460Sstevel@tonic-gate 3470Sstevel@tonic-gate #if (REPOSITORY_DOOR_FLAG_ALL != REPOSITORY_DOOR_FLAG_DEBUG) 3480Sstevel@tonic-gate #error Need to update flag checks 3490Sstevel@tonic-gate #endif 3500Sstevel@tonic-gate 3510Sstevel@tonic-gate if (flags & ~REPOSITORY_DOOR_FLAG_ALL) 3520Sstevel@tonic-gate return (REPOSITORY_DOOR_FAIL_BAD_FLAG); 3530Sstevel@tonic-gate 3540Sstevel@tonic-gate if (flags & REPOSITORY_DOOR_FLAG_DEBUG) 3550Sstevel@tonic-gate if (!GET_ARG(rp, rp_size, rdr_debug, &debugflags)) 3560Sstevel@tonic-gate return (REPOSITORY_DOOR_FAIL_BAD_REQUEST); 3570Sstevel@tonic-gate #undef GET_ARG 3580Sstevel@tonic-gate 3590Sstevel@tonic-gate return (create_client(ucred_getpid(uc), debugflags, privileged, 3600Sstevel@tonic-gate out_fd)); 3610Sstevel@tonic-gate } 3620Sstevel@tonic-gate 3630Sstevel@tonic-gate void 3640Sstevel@tonic-gate configd_vcritical(const char *message, va_list args) 3650Sstevel@tonic-gate { 3660Sstevel@tonic-gate if (log_to_syslog) 3670Sstevel@tonic-gate vsyslog(LOG_CRIT, message, args); 3680Sstevel@tonic-gate else { 3690Sstevel@tonic-gate flockfile(stderr); 3700Sstevel@tonic-gate (void) fprintf(stderr, "svc.configd: Fatal error: "); 3710Sstevel@tonic-gate (void) vfprintf(stderr, message, args); 3720Sstevel@tonic-gate if (message[0] == 0 || message[strlen(message) - 1] != '\n') 3730Sstevel@tonic-gate (void) fprintf(stderr, "\n"); 3740Sstevel@tonic-gate funlockfile(stderr); 3750Sstevel@tonic-gate } 3760Sstevel@tonic-gate } 3770Sstevel@tonic-gate 3780Sstevel@tonic-gate void 3790Sstevel@tonic-gate configd_critical(const char *message, ...) 3800Sstevel@tonic-gate { 3810Sstevel@tonic-gate va_list args; 3820Sstevel@tonic-gate va_start(args, message); 3830Sstevel@tonic-gate configd_vcritical(message, args); 3840Sstevel@tonic-gate va_end(args); 3850Sstevel@tonic-gate } 3860Sstevel@tonic-gate 3870Sstevel@tonic-gate static void 3880Sstevel@tonic-gate usage(const char *prog, int ret) 3890Sstevel@tonic-gate { 3900Sstevel@tonic-gate (void) fprintf(stderr, 3910Sstevel@tonic-gate "usage: %s [-np] [-d door_path] [-r repository_path]\n" 3920Sstevel@tonic-gate " [-t nonpersist_repository]\n", prog); 3930Sstevel@tonic-gate exit(ret); 3940Sstevel@tonic-gate } 3950Sstevel@tonic-gate 3960Sstevel@tonic-gate /*ARGSUSED*/ 3970Sstevel@tonic-gate static void 3980Sstevel@tonic-gate handler(int sig, siginfo_t *info, void *data) 3990Sstevel@tonic-gate { 4000Sstevel@tonic-gate finished = 1; 4010Sstevel@tonic-gate } 4020Sstevel@tonic-gate 4030Sstevel@tonic-gate static int pipe_fd = -1; 4040Sstevel@tonic-gate 4050Sstevel@tonic-gate static int 4060Sstevel@tonic-gate daemonize_start(void) 4070Sstevel@tonic-gate { 4080Sstevel@tonic-gate char data; 4090Sstevel@tonic-gate int status; 4100Sstevel@tonic-gate 4110Sstevel@tonic-gate int filedes[2]; 4120Sstevel@tonic-gate pid_t pid; 4130Sstevel@tonic-gate 4140Sstevel@tonic-gate (void) close(0); 4150Sstevel@tonic-gate (void) dup2(2, 1); /* stderr only */ 4160Sstevel@tonic-gate 4170Sstevel@tonic-gate if (pipe(filedes) < 0) 4180Sstevel@tonic-gate return (-1); 4190Sstevel@tonic-gate 4200Sstevel@tonic-gate if ((pid = fork1()) < 0) 4210Sstevel@tonic-gate return (-1); 4220Sstevel@tonic-gate 4230Sstevel@tonic-gate if (pid != 0) { 4240Sstevel@tonic-gate /* 4250Sstevel@tonic-gate * parent 4260Sstevel@tonic-gate */ 4270Sstevel@tonic-gate struct sigaction act; 4280Sstevel@tonic-gate 4290Sstevel@tonic-gate act.sa_sigaction = SIG_DFL; 4300Sstevel@tonic-gate (void) sigemptyset(&act.sa_mask); 4310Sstevel@tonic-gate act.sa_flags = 0; 4320Sstevel@tonic-gate 4330Sstevel@tonic-gate (void) sigaction(SIGPIPE, &act, NULL); /* ignore SIGPIPE */ 4340Sstevel@tonic-gate 4350Sstevel@tonic-gate (void) close(filedes[1]); 4360Sstevel@tonic-gate if (read(filedes[0], &data, 1) == 1) { 4370Sstevel@tonic-gate /* presume success */ 4380Sstevel@tonic-gate _exit(CONFIGD_EXIT_OKAY); 4390Sstevel@tonic-gate } 4400Sstevel@tonic-gate 4410Sstevel@tonic-gate status = -1; 4420Sstevel@tonic-gate (void) wait4(pid, &status, 0, NULL); 4430Sstevel@tonic-gate if (WIFEXITED(status)) 4440Sstevel@tonic-gate _exit(WEXITSTATUS(status)); 4450Sstevel@tonic-gate else 4460Sstevel@tonic-gate _exit(-1); 4470Sstevel@tonic-gate } 4480Sstevel@tonic-gate 4490Sstevel@tonic-gate /* 4500Sstevel@tonic-gate * child 4510Sstevel@tonic-gate */ 4520Sstevel@tonic-gate pipe_fd = filedes[1]; 4530Sstevel@tonic-gate (void) close(filedes[0]); 4540Sstevel@tonic-gate 4550Sstevel@tonic-gate /* 4560Sstevel@tonic-gate * generic Unix setup 4570Sstevel@tonic-gate */ 4580Sstevel@tonic-gate (void) setsid(); 4590Sstevel@tonic-gate (void) umask(0077); 4600Sstevel@tonic-gate 4610Sstevel@tonic-gate return (0); 4620Sstevel@tonic-gate } 4630Sstevel@tonic-gate 4640Sstevel@tonic-gate static void 4650Sstevel@tonic-gate daemonize_ready(void) 4660Sstevel@tonic-gate { 4670Sstevel@tonic-gate char data = '\0'; 4680Sstevel@tonic-gate 4690Sstevel@tonic-gate /* 4700Sstevel@tonic-gate * wake the parent 4710Sstevel@tonic-gate */ 4720Sstevel@tonic-gate (void) write(pipe_fd, &data, 1); 4730Sstevel@tonic-gate (void) close(pipe_fd); 4740Sstevel@tonic-gate } 4750Sstevel@tonic-gate 476*407Sjwadams const char * 477*407Sjwadams regularize_path(const char *dir, const char *base, char *tmpbuf) 478*407Sjwadams { 479*407Sjwadams if (base == NULL) 480*407Sjwadams return (NULL); 481*407Sjwadams if (base[0] == '/') 482*407Sjwadams return (base); 483*407Sjwadams 484*407Sjwadams if (snprintf(tmpbuf, PATH_MAX, "%s/%s", dir, base) >= PATH_MAX) { 485*407Sjwadams (void) fprintf(stderr, "svc.configd: %s/%s: path too long\n", 486*407Sjwadams dir, base); 487*407Sjwadams exit(CONFIGD_EXIT_BAD_ARGS); 488*407Sjwadams } 489*407Sjwadams 490*407Sjwadams return (tmpbuf); 491*407Sjwadams } 492*407Sjwadams 4930Sstevel@tonic-gate int 4940Sstevel@tonic-gate main(int argc, char *argv[]) 4950Sstevel@tonic-gate { 4960Sstevel@tonic-gate thread_info_t *ti = &main_thread_info; 4970Sstevel@tonic-gate 4980Sstevel@tonic-gate char pidpath[sizeof ("/proc/" "/psinfo") + 10]; 4990Sstevel@tonic-gate 5000Sstevel@tonic-gate struct rlimit fd_new; 5010Sstevel@tonic-gate 5020Sstevel@tonic-gate const char *endptr; 5030Sstevel@tonic-gate sigset_t myset; 5040Sstevel@tonic-gate int c; 5050Sstevel@tonic-gate int ret; 5060Sstevel@tonic-gate int fd; 507*407Sjwadams 508*407Sjwadams char curdir[PATH_MAX]; 509*407Sjwadams char dbtmp[PATH_MAX]; 510*407Sjwadams char npdbtmp[PATH_MAX]; 511*407Sjwadams char doortmp[PATH_MAX]; 512*407Sjwadams 5130Sstevel@tonic-gate const char *dbpath = NULL; 5140Sstevel@tonic-gate const char *npdbpath = NULL; 5150Sstevel@tonic-gate const char *doorpath = REPOSITORY_DOOR_NAME; 5160Sstevel@tonic-gate struct sigaction act; 5170Sstevel@tonic-gate 5180Sstevel@tonic-gate int daemonize = 1; /* default to daemonizing */ 5190Sstevel@tonic-gate int have_npdb = 1; 5200Sstevel@tonic-gate 5210Sstevel@tonic-gate closefrom(3); /* get rid of extraneous fds */ 5220Sstevel@tonic-gate 523*407Sjwadams if (getcwd(curdir, sizeof (curdir)) == NULL) { 524*407Sjwadams (void) fprintf(stderr, 525*407Sjwadams "%s: unable to get current directory: %s\n", 526*407Sjwadams argv[0], strerror(errno)); 527*407Sjwadams exit(CONFIGD_EXIT_INIT_FAILED); 528*407Sjwadams } 529*407Sjwadams 5300Sstevel@tonic-gate while ((c = getopt(argc, argv, "Dnpd:r:t:")) != -1) { 5310Sstevel@tonic-gate switch (c) { 5320Sstevel@tonic-gate case 'n': 5330Sstevel@tonic-gate daemonize = 0; 5340Sstevel@tonic-gate break; 5350Sstevel@tonic-gate case 'd': 536*407Sjwadams doorpath = regularize_path(curdir, optarg, doortmp); 5370Sstevel@tonic-gate is_main_repository = 0; 5380Sstevel@tonic-gate have_npdb = 0; /* default to no non-persist */ 5390Sstevel@tonic-gate break; 5400Sstevel@tonic-gate case 'p': 5410Sstevel@tonic-gate log_to_syslog = 0; /* don't use syslog */ 5420Sstevel@tonic-gate is_main_repository = 0; 5430Sstevel@tonic-gate 5440Sstevel@tonic-gate /* 5450Sstevel@tonic-gate * If our parent exits while we're opening its /proc 5460Sstevel@tonic-gate * psinfo, we're vulnerable to a pid wrapping. To 5470Sstevel@tonic-gate * protect against that, re-check our ppid after 5480Sstevel@tonic-gate * opening it. 5490Sstevel@tonic-gate */ 5500Sstevel@tonic-gate privileged_pid = getppid(); 5510Sstevel@tonic-gate (void) snprintf(pidpath, sizeof (pidpath), 5520Sstevel@tonic-gate "/proc/%d/psinfo", privileged_pid); 5530Sstevel@tonic-gate if ((fd = open(pidpath, O_RDONLY)) < 0 || 5540Sstevel@tonic-gate getppid() != privileged_pid) { 5550Sstevel@tonic-gate (void) fprintf(stderr, 5560Sstevel@tonic-gate "%s: unable to get parent info\n", argv[0]); 5570Sstevel@tonic-gate exit(CONFIGD_EXIT_BAD_ARGS); 5580Sstevel@tonic-gate } 5590Sstevel@tonic-gate privileged_psinfo_fd = fd; 5600Sstevel@tonic-gate break; 5610Sstevel@tonic-gate case 'r': 562*407Sjwadams dbpath = regularize_path(curdir, optarg, dbtmp); 5630Sstevel@tonic-gate is_main_repository = 0; 5640Sstevel@tonic-gate break; 5650Sstevel@tonic-gate case 't': 566*407Sjwadams npdbpath = regularize_path(curdir, optarg, npdbtmp); 5670Sstevel@tonic-gate is_main_repository = 0; 5680Sstevel@tonic-gate break; 5690Sstevel@tonic-gate default: 5700Sstevel@tonic-gate usage(argv[0], CONFIGD_EXIT_BAD_ARGS); 5710Sstevel@tonic-gate break; 5720Sstevel@tonic-gate } 5730Sstevel@tonic-gate } 5740Sstevel@tonic-gate 5750Sstevel@tonic-gate /* 5760Sstevel@tonic-gate * If we're not running as root, allow our euid full access, and 5770Sstevel@tonic-gate * everyone else no access. 5780Sstevel@tonic-gate */ 5790Sstevel@tonic-gate if (privileged_pid == 0 && geteuid() != 0) { 5800Sstevel@tonic-gate privileged_user = geteuid(); 5810Sstevel@tonic-gate } 5820Sstevel@tonic-gate 5830Sstevel@tonic-gate privileged_privs = priv_str_to_set("zone", "", &endptr); 5840Sstevel@tonic-gate if (endptr != NULL && privileged_privs != NULL) { 5850Sstevel@tonic-gate priv_freeset(privileged_privs); 5860Sstevel@tonic-gate privileged_privs = NULL; 5870Sstevel@tonic-gate } 5880Sstevel@tonic-gate 5890Sstevel@tonic-gate openlog("svc.configd", LOG_PID | LOG_CONS, LOG_DAEMON); 5900Sstevel@tonic-gate (void) setlogmask(LOG_UPTO(LOG_NOTICE)); 5910Sstevel@tonic-gate 5920Sstevel@tonic-gate /* 5930Sstevel@tonic-gate * if a non-persist db is specified, always enable it 5940Sstevel@tonic-gate */ 5950Sstevel@tonic-gate if (npdbpath) 5960Sstevel@tonic-gate have_npdb = 1; 5970Sstevel@tonic-gate 5980Sstevel@tonic-gate if (optind != argc) 5990Sstevel@tonic-gate usage(argv[0], CONFIGD_EXIT_BAD_ARGS); 6000Sstevel@tonic-gate 6010Sstevel@tonic-gate if (daemonize) { 6020Sstevel@tonic-gate if (getuid() == 0) 6030Sstevel@tonic-gate (void) chdir("/"); 6040Sstevel@tonic-gate if (daemonize_start() < 0) { 6050Sstevel@tonic-gate (void) perror("unable to daemonize"); 6060Sstevel@tonic-gate exit(CONFIGD_EXIT_INIT_FAILED); 6070Sstevel@tonic-gate } 6080Sstevel@tonic-gate } 6090Sstevel@tonic-gate if (getuid() == 0) 6100Sstevel@tonic-gate (void) core_set_process_path(CONFIGD_CORE, 6110Sstevel@tonic-gate strlen(CONFIGD_CORE) + 1, getpid()); 6120Sstevel@tonic-gate 6130Sstevel@tonic-gate /* 6140Sstevel@tonic-gate * this should be enabled once we can drop privileges and still get 6150Sstevel@tonic-gate * a core dump. 6160Sstevel@tonic-gate */ 6170Sstevel@tonic-gate #if 0 6180Sstevel@tonic-gate /* turn off basic privileges we do not need */ 6190Sstevel@tonic-gate (void) priv_set(PRIV_OFF, PRIV_PERMITTED, PRIV_FILE_LINK_ANY, 6200Sstevel@tonic-gate PRIV_PROC_EXEC, PRIV_PROC_FORK, PRIV_PROC_SESSION, NULL); 6210Sstevel@tonic-gate #endif 6220Sstevel@tonic-gate 6230Sstevel@tonic-gate /* not that we can exec, but to be safe, shut them all off... */ 6240Sstevel@tonic-gate (void) priv_set(PRIV_SET, PRIV_INHERITABLE, NULL); 6250Sstevel@tonic-gate 6260Sstevel@tonic-gate (void) sigfillset(&act.sa_mask); 6270Sstevel@tonic-gate 6280Sstevel@tonic-gate /* signals to ignore */ 6290Sstevel@tonic-gate act.sa_sigaction = SIG_IGN; 6300Sstevel@tonic-gate act.sa_flags = 0; 6310Sstevel@tonic-gate (void) sigaction(SIGPIPE, &act, NULL); 6320Sstevel@tonic-gate (void) sigaction(SIGALRM, &act, NULL); 6330Sstevel@tonic-gate (void) sigaction(SIGUSR1, &act, NULL); 6340Sstevel@tonic-gate (void) sigaction(SIGUSR2, &act, NULL); 6350Sstevel@tonic-gate (void) sigaction(SIGPOLL, &act, NULL); 6360Sstevel@tonic-gate 6370Sstevel@tonic-gate /* signals to abort on */ 6380Sstevel@tonic-gate act.sa_sigaction = (void (*)(int, siginfo_t *, void *))&abort_handler; 6390Sstevel@tonic-gate act.sa_flags = SA_SIGINFO; 6400Sstevel@tonic-gate 6410Sstevel@tonic-gate (void) sigaction(SIGABRT, &act, NULL); 6420Sstevel@tonic-gate 6430Sstevel@tonic-gate /* signals to handle */ 6440Sstevel@tonic-gate act.sa_sigaction = &handler; 6450Sstevel@tonic-gate act.sa_flags = SA_SIGINFO; 6460Sstevel@tonic-gate 6470Sstevel@tonic-gate (void) sigaction(SIGHUP, &act, NULL); 6480Sstevel@tonic-gate (void) sigaction(SIGINT, &act, NULL); 6490Sstevel@tonic-gate (void) sigaction(SIGTERM, &act, NULL); 6500Sstevel@tonic-gate 6510Sstevel@tonic-gate (void) sigemptyset(&myset); 6520Sstevel@tonic-gate (void) sigaddset(&myset, SIGHUP); 6530Sstevel@tonic-gate (void) sigaddset(&myset, SIGINT); 6540Sstevel@tonic-gate (void) sigaddset(&myset, SIGTERM); 6550Sstevel@tonic-gate 6560Sstevel@tonic-gate if ((errno = pthread_attr_init(&thread_attr)) != 0) { 6570Sstevel@tonic-gate (void) perror("initializing"); 6580Sstevel@tonic-gate exit(CONFIGD_EXIT_INIT_FAILED); 6590Sstevel@tonic-gate } 6600Sstevel@tonic-gate 6610Sstevel@tonic-gate /* 6620Sstevel@tonic-gate * Set the hard and soft limits to CONFIGD_MAX_FDS. 6630Sstevel@tonic-gate */ 6640Sstevel@tonic-gate fd_new.rlim_max = fd_new.rlim_cur = CONFIGD_MAX_FDS; 6650Sstevel@tonic-gate (void) setrlimit(RLIMIT_NOFILE, &fd_new); 6660Sstevel@tonic-gate 6670Sstevel@tonic-gate if ((ret = backend_init(dbpath, npdbpath, have_npdb)) != 6680Sstevel@tonic-gate CONFIGD_EXIT_OKAY) 6690Sstevel@tonic-gate exit(ret); 6700Sstevel@tonic-gate 6710Sstevel@tonic-gate if (!client_init()) 6720Sstevel@tonic-gate exit(CONFIGD_EXIT_INIT_FAILED); 6730Sstevel@tonic-gate 6740Sstevel@tonic-gate if (!rc_node_init()) 6750Sstevel@tonic-gate exit(CONFIGD_EXIT_INIT_FAILED); 6760Sstevel@tonic-gate 6770Sstevel@tonic-gate (void) pthread_attr_setdetachstate(&thread_attr, 6780Sstevel@tonic-gate PTHREAD_CREATE_DETACHED); 6790Sstevel@tonic-gate (void) pthread_attr_setscope(&thread_attr, PTHREAD_SCOPE_SYSTEM); 6800Sstevel@tonic-gate 6810Sstevel@tonic-gate if ((errno = pthread_key_create(&thread_info_key, 6820Sstevel@tonic-gate thread_exiting)) != 0) { 6830Sstevel@tonic-gate perror("pthread_key_create"); 6840Sstevel@tonic-gate exit(CONFIGD_EXIT_INIT_FAILED); 6850Sstevel@tonic-gate } 6860Sstevel@tonic-gate 6870Sstevel@tonic-gate if ((thread_pool = uu_list_pool_create("thread_pool", 6880Sstevel@tonic-gate sizeof (thread_info_t), offsetof(thread_info_t, ti_node), 6890Sstevel@tonic-gate NULL, UU_LIST_POOL_DEBUG)) == NULL) { 6900Sstevel@tonic-gate configd_critical("uu_list_pool_create: %s\n", 6910Sstevel@tonic-gate uu_strerror(uu_error())); 6920Sstevel@tonic-gate exit(CONFIGD_EXIT_INIT_FAILED); 6930Sstevel@tonic-gate } 6940Sstevel@tonic-gate 6950Sstevel@tonic-gate if ((thread_list = uu_list_create(thread_pool, NULL, 0)) == NULL) { 6960Sstevel@tonic-gate configd_critical("uu_list_create: %s\n", 6970Sstevel@tonic-gate uu_strerror(uu_error())); 6980Sstevel@tonic-gate exit(CONFIGD_EXIT_INIT_FAILED); 6990Sstevel@tonic-gate } 7000Sstevel@tonic-gate 7010Sstevel@tonic-gate (void) memset(ti, '\0', sizeof (*ti)); 7020Sstevel@tonic-gate uu_list_node_init(ti, &ti->ti_node, thread_pool); 7030Sstevel@tonic-gate (void) uu_list_insert_before(thread_list, uu_list_first(thread_list), 7040Sstevel@tonic-gate ti); 7050Sstevel@tonic-gate 7060Sstevel@tonic-gate ti->ti_thread = pthread_self(); 7070Sstevel@tonic-gate ti->ti_state = TI_SIGNAL_WAIT; 7080Sstevel@tonic-gate ti->ti_prev_state = TI_SIGNAL_WAIT; 7090Sstevel@tonic-gate 7100Sstevel@tonic-gate (void) pthread_setspecific(thread_info_key, ti); 7110Sstevel@tonic-gate 7120Sstevel@tonic-gate (void) door_server_create(new_thread_needed); 7130Sstevel@tonic-gate 7140Sstevel@tonic-gate if (!setup_main_door(doorpath)) { 7150Sstevel@tonic-gate configd_critical("Setting up main door failed.\n"); 7160Sstevel@tonic-gate exit(CONFIGD_EXIT_DOOR_INIT_FAILED); 7170Sstevel@tonic-gate } 7180Sstevel@tonic-gate 7190Sstevel@tonic-gate if (daemonize) 7200Sstevel@tonic-gate daemonize_ready(); 7210Sstevel@tonic-gate 7220Sstevel@tonic-gate (void) pthread_sigmask(SIG_BLOCK, &myset, NULL); 7230Sstevel@tonic-gate while (!finished) { 7240Sstevel@tonic-gate int sig = sigwait(&myset); 7250Sstevel@tonic-gate if (sig > 0) { 7260Sstevel@tonic-gate break; 7270Sstevel@tonic-gate } 7280Sstevel@tonic-gate } 7290Sstevel@tonic-gate 7300Sstevel@tonic-gate backend_fini(); 7310Sstevel@tonic-gate 7320Sstevel@tonic-gate return (CONFIGD_EXIT_OKAY); 7330Sstevel@tonic-gate } 734