1*0Sstevel@tonic-gate /* 2*0Sstevel@tonic-gate * CDDL HEADER START 3*0Sstevel@tonic-gate * 4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*0Sstevel@tonic-gate * with the License. 8*0Sstevel@tonic-gate * 9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*0Sstevel@tonic-gate * See the License for the specific language governing permissions 12*0Sstevel@tonic-gate * and limitations under the License. 13*0Sstevel@tonic-gate * 14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*0Sstevel@tonic-gate * 20*0Sstevel@tonic-gate * CDDL HEADER END 21*0Sstevel@tonic-gate */ 22*0Sstevel@tonic-gate /* 23*0Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 24*0Sstevel@tonic-gate * Use is subject to license terms. 25*0Sstevel@tonic-gate */ 26*0Sstevel@tonic-gate 27*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 28*0Sstevel@tonic-gate 29*0Sstevel@tonic-gate #include <assert.h> 30*0Sstevel@tonic-gate #include <door.h> 31*0Sstevel@tonic-gate #include <errno.h> 32*0Sstevel@tonic-gate #include <fcntl.h> 33*0Sstevel@tonic-gate #include <priv.h> 34*0Sstevel@tonic-gate #include <procfs.h> 35*0Sstevel@tonic-gate #include <pthread.h> 36*0Sstevel@tonic-gate #include <signal.h> 37*0Sstevel@tonic-gate #include <stdarg.h> 38*0Sstevel@tonic-gate #include <stdio.h> 39*0Sstevel@tonic-gate #include <stdlib.h> 40*0Sstevel@tonic-gate #include <string.h> 41*0Sstevel@tonic-gate #include <syslog.h> 42*0Sstevel@tonic-gate #include <sys/corectl.h> 43*0Sstevel@tonic-gate #include <sys/resource.h> 44*0Sstevel@tonic-gate #include <sys/stat.h> 45*0Sstevel@tonic-gate #include <sys/wait.h> 46*0Sstevel@tonic-gate #include <ucontext.h> 47*0Sstevel@tonic-gate #include <unistd.h> 48*0Sstevel@tonic-gate 49*0Sstevel@tonic-gate #include "configd.h" 50*0Sstevel@tonic-gate 51*0Sstevel@tonic-gate /* 52*0Sstevel@tonic-gate * This file manages the overall startup and shutdown of configd, as well 53*0Sstevel@tonic-gate * as managing its door thread pool and per-thread datastructures. 54*0Sstevel@tonic-gate * 55*0Sstevel@tonic-gate * 1. Per-thread Datastructures 56*0Sstevel@tonic-gate * ----------------------------- 57*0Sstevel@tonic-gate * Each configd thread has an associated thread_info_t which contains its 58*0Sstevel@tonic-gate * current state. A pointer is kept to this in TSD, keyed by thread_info_key. 59*0Sstevel@tonic-gate * The thread_info_ts for all threads in configd are kept on a single global 60*0Sstevel@tonic-gate * list, thread_list. After creation, the state in the thread_info structure 61*0Sstevel@tonic-gate * is only modified by the associated thread, so no locking is needed. A TSD 62*0Sstevel@tonic-gate * destructor removes the thread_info from the global list and frees it at 63*0Sstevel@tonic-gate * pthread_exit() time. 64*0Sstevel@tonic-gate * 65*0Sstevel@tonic-gate * Threads access their per-thread data using thread_self() 66*0Sstevel@tonic-gate * 67*0Sstevel@tonic-gate * The thread_list is protected by thread_lock, a leaf lock. 68*0Sstevel@tonic-gate * 69*0Sstevel@tonic-gate * 2. Door Thread Pool Management 70*0Sstevel@tonic-gate * ------------------------------ 71*0Sstevel@tonic-gate * Whenever door_return(3door) returns from the kernel and there are no 72*0Sstevel@tonic-gate * other configd threads waiting for requests, libdoor automatically 73*0Sstevel@tonic-gate * invokes a function registered with door_server_create(), to request a new 74*0Sstevel@tonic-gate * door server thread. The default function just creates a thread that calls 75*0Sstevel@tonic-gate * door_return(3door). Unfortunately, since it can take a while for the new 76*0Sstevel@tonic-gate * thread to *get* to door_return(3door), a stream of requests can cause a 77*0Sstevel@tonic-gate * large number of threads to be created, even though they aren't all needed. 78*0Sstevel@tonic-gate * 79*0Sstevel@tonic-gate * In our callback, new_server_needed(), we limit ourself to two new threads 80*0Sstevel@tonic-gate * at a time -- this logic is handled in reserve_new_thread(). This keeps 81*0Sstevel@tonic-gate * us from creating an absurd number of threads in response to peaking load. 82*0Sstevel@tonic-gate */ 83*0Sstevel@tonic-gate static pthread_key_t thread_info_key; 84*0Sstevel@tonic-gate static pthread_attr_t thread_attr; 85*0Sstevel@tonic-gate 86*0Sstevel@tonic-gate static pthread_mutex_t thread_lock = PTHREAD_MUTEX_INITIALIZER; 87*0Sstevel@tonic-gate int num_started; /* number actually running */ 88*0Sstevel@tonic-gate int num_servers; /* number in-progress or running */ 89*0Sstevel@tonic-gate static uu_list_pool_t *thread_pool; 90*0Sstevel@tonic-gate uu_list_t *thread_list; 91*0Sstevel@tonic-gate 92*0Sstevel@tonic-gate static thread_info_t main_thread_info; 93*0Sstevel@tonic-gate 94*0Sstevel@tonic-gate static int finished; 95*0Sstevel@tonic-gate 96*0Sstevel@tonic-gate static pid_t privileged_pid = 0; 97*0Sstevel@tonic-gate static int privileged_psinfo_fd = -1; 98*0Sstevel@tonic-gate 99*0Sstevel@tonic-gate static int privileged_user = 0; 100*0Sstevel@tonic-gate 101*0Sstevel@tonic-gate static priv_set_t *privileged_privs; 102*0Sstevel@tonic-gate 103*0Sstevel@tonic-gate static int log_to_syslog = 0; 104*0Sstevel@tonic-gate 105*0Sstevel@tonic-gate int is_main_repository = 1; 106*0Sstevel@tonic-gate 107*0Sstevel@tonic-gate int max_repository_backups = 4; 108*0Sstevel@tonic-gate 109*0Sstevel@tonic-gate #define CONFIGD_MAX_FDS 262144 110*0Sstevel@tonic-gate 111*0Sstevel@tonic-gate /* 112*0Sstevel@tonic-gate * Thanks, Mike 113*0Sstevel@tonic-gate */ 114*0Sstevel@tonic-gate void 115*0Sstevel@tonic-gate abort_handler(int sig, siginfo_t *sip, ucontext_t *ucp) 116*0Sstevel@tonic-gate { 117*0Sstevel@tonic-gate struct sigaction act; 118*0Sstevel@tonic-gate 119*0Sstevel@tonic-gate (void) sigemptyset(&act.sa_mask); 120*0Sstevel@tonic-gate act.sa_handler = SIG_DFL; 121*0Sstevel@tonic-gate act.sa_flags = 0; 122*0Sstevel@tonic-gate (void) sigaction(sig, &act, NULL); 123*0Sstevel@tonic-gate 124*0Sstevel@tonic-gate (void) printstack(2); 125*0Sstevel@tonic-gate 126*0Sstevel@tonic-gate if (sip != NULL && SI_FROMUSER(sip)) 127*0Sstevel@tonic-gate (void) pthread_kill(pthread_self(), sig); 128*0Sstevel@tonic-gate (void) sigfillset(&ucp->uc_sigmask); 129*0Sstevel@tonic-gate (void) sigdelset(&ucp->uc_sigmask, sig); 130*0Sstevel@tonic-gate ucp->uc_flags |= UC_SIGMASK; 131*0Sstevel@tonic-gate (void) setcontext(ucp); 132*0Sstevel@tonic-gate } 133*0Sstevel@tonic-gate 134*0Sstevel@tonic-gate /* 135*0Sstevel@tonic-gate * Don't want to have more than a couple thread creates outstanding 136*0Sstevel@tonic-gate */ 137*0Sstevel@tonic-gate static int 138*0Sstevel@tonic-gate reserve_new_thread(void) 139*0Sstevel@tonic-gate { 140*0Sstevel@tonic-gate (void) pthread_mutex_lock(&thread_lock); 141*0Sstevel@tonic-gate assert(num_started >= 0); 142*0Sstevel@tonic-gate if (num_servers > num_started + 1) { 143*0Sstevel@tonic-gate (void) pthread_mutex_unlock(&thread_lock); 144*0Sstevel@tonic-gate return (0); 145*0Sstevel@tonic-gate } 146*0Sstevel@tonic-gate ++num_servers; 147*0Sstevel@tonic-gate (void) pthread_mutex_unlock(&thread_lock); 148*0Sstevel@tonic-gate return (1); 149*0Sstevel@tonic-gate } 150*0Sstevel@tonic-gate 151*0Sstevel@tonic-gate static void 152*0Sstevel@tonic-gate thread_info_free(thread_info_t *ti) 153*0Sstevel@tonic-gate { 154*0Sstevel@tonic-gate uu_list_node_fini(ti, &ti->ti_node, thread_pool); 155*0Sstevel@tonic-gate if (ti->ti_ucred != NULL) 156*0Sstevel@tonic-gate uu_free(ti->ti_ucred); 157*0Sstevel@tonic-gate uu_free(ti); 158*0Sstevel@tonic-gate } 159*0Sstevel@tonic-gate 160*0Sstevel@tonic-gate static void 161*0Sstevel@tonic-gate thread_exiting(void *arg) 162*0Sstevel@tonic-gate { 163*0Sstevel@tonic-gate thread_info_t *ti = arg; 164*0Sstevel@tonic-gate 165*0Sstevel@tonic-gate log_enter(&ti->ti_log); 166*0Sstevel@tonic-gate 167*0Sstevel@tonic-gate (void) pthread_mutex_lock(&thread_lock); 168*0Sstevel@tonic-gate if (ti != NULL) { 169*0Sstevel@tonic-gate num_started--; 170*0Sstevel@tonic-gate uu_list_remove(thread_list, ti); 171*0Sstevel@tonic-gate } 172*0Sstevel@tonic-gate assert(num_servers > 0); 173*0Sstevel@tonic-gate --num_servers; 174*0Sstevel@tonic-gate 175*0Sstevel@tonic-gate if (num_servers == 0) { 176*0Sstevel@tonic-gate configd_critical("no door server threads\n"); 177*0Sstevel@tonic-gate abort(); 178*0Sstevel@tonic-gate } 179*0Sstevel@tonic-gate (void) pthread_mutex_unlock(&thread_lock); 180*0Sstevel@tonic-gate 181*0Sstevel@tonic-gate if (ti != NULL && ti != &main_thread_info) 182*0Sstevel@tonic-gate thread_info_free(ti); 183*0Sstevel@tonic-gate } 184*0Sstevel@tonic-gate 185*0Sstevel@tonic-gate void 186*0Sstevel@tonic-gate thread_newstate(thread_info_t *ti, thread_state_t newstate) 187*0Sstevel@tonic-gate { 188*0Sstevel@tonic-gate ti->ti_ucred_read = 0; /* invalidate cached ucred */ 189*0Sstevel@tonic-gate if (newstate != ti->ti_state) { 190*0Sstevel@tonic-gate ti->ti_prev_state = ti->ti_state; 191*0Sstevel@tonic-gate ti->ti_state = newstate; 192*0Sstevel@tonic-gate ti->ti_lastchange = gethrtime(); 193*0Sstevel@tonic-gate } 194*0Sstevel@tonic-gate } 195*0Sstevel@tonic-gate 196*0Sstevel@tonic-gate thread_info_t * 197*0Sstevel@tonic-gate thread_self(void) 198*0Sstevel@tonic-gate { 199*0Sstevel@tonic-gate return (pthread_getspecific(thread_info_key)); 200*0Sstevel@tonic-gate } 201*0Sstevel@tonic-gate 202*0Sstevel@tonic-gate ucred_t * 203*0Sstevel@tonic-gate get_ucred(void) 204*0Sstevel@tonic-gate { 205*0Sstevel@tonic-gate thread_info_t *ti = thread_self(); 206*0Sstevel@tonic-gate ucred_t **ret = &ti->ti_ucred; 207*0Sstevel@tonic-gate 208*0Sstevel@tonic-gate if (ti->ti_ucred_read) 209*0Sstevel@tonic-gate return (*ret); /* cached value */ 210*0Sstevel@tonic-gate 211*0Sstevel@tonic-gate if (door_ucred(ret) != 0) 212*0Sstevel@tonic-gate return (NULL); 213*0Sstevel@tonic-gate ti->ti_ucred_read = 1; 214*0Sstevel@tonic-gate 215*0Sstevel@tonic-gate return (*ret); 216*0Sstevel@tonic-gate } 217*0Sstevel@tonic-gate 218*0Sstevel@tonic-gate int 219*0Sstevel@tonic-gate ucred_is_privileged(ucred_t *uc) 220*0Sstevel@tonic-gate { 221*0Sstevel@tonic-gate const priv_set_t *ps; 222*0Sstevel@tonic-gate 223*0Sstevel@tonic-gate if ((ps = ucred_getprivset(uc, PRIV_EFFECTIVE)) != NULL) { 224*0Sstevel@tonic-gate if (priv_isfullset(ps)) 225*0Sstevel@tonic-gate return (1); /* process has all privs */ 226*0Sstevel@tonic-gate 227*0Sstevel@tonic-gate if (privileged_privs != NULL && 228*0Sstevel@tonic-gate priv_issubset(privileged_privs, ps)) 229*0Sstevel@tonic-gate return (1); /* process has zone privs */ 230*0Sstevel@tonic-gate } 231*0Sstevel@tonic-gate 232*0Sstevel@tonic-gate return (0); 233*0Sstevel@tonic-gate } 234*0Sstevel@tonic-gate 235*0Sstevel@tonic-gate static void * 236*0Sstevel@tonic-gate thread_start(void *arg) 237*0Sstevel@tonic-gate { 238*0Sstevel@tonic-gate thread_info_t *ti = arg; 239*0Sstevel@tonic-gate 240*0Sstevel@tonic-gate (void) pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL); 241*0Sstevel@tonic-gate 242*0Sstevel@tonic-gate (void) pthread_mutex_lock(&thread_lock); 243*0Sstevel@tonic-gate num_started++; 244*0Sstevel@tonic-gate (void) uu_list_insert_after(thread_list, uu_list_last(thread_list), 245*0Sstevel@tonic-gate ti); 246*0Sstevel@tonic-gate (void) pthread_mutex_unlock(&thread_lock); 247*0Sstevel@tonic-gate (void) pthread_setspecific(thread_info_key, ti); 248*0Sstevel@tonic-gate 249*0Sstevel@tonic-gate thread_newstate(ti, TI_DOOR_RETURN); 250*0Sstevel@tonic-gate 251*0Sstevel@tonic-gate /* 252*0Sstevel@tonic-gate * Start handling door calls 253*0Sstevel@tonic-gate */ 254*0Sstevel@tonic-gate (void) door_return(NULL, 0, NULL, 0); 255*0Sstevel@tonic-gate return (arg); 256*0Sstevel@tonic-gate } 257*0Sstevel@tonic-gate 258*0Sstevel@tonic-gate static void 259*0Sstevel@tonic-gate new_thread_needed(door_info_t *dip) 260*0Sstevel@tonic-gate { 261*0Sstevel@tonic-gate thread_info_t *ti; 262*0Sstevel@tonic-gate 263*0Sstevel@tonic-gate sigset_t new, old; 264*0Sstevel@tonic-gate 265*0Sstevel@tonic-gate assert(dip == NULL); 266*0Sstevel@tonic-gate 267*0Sstevel@tonic-gate if (!reserve_new_thread()) 268*0Sstevel@tonic-gate return; 269*0Sstevel@tonic-gate 270*0Sstevel@tonic-gate if ((ti = uu_zalloc(sizeof (*ti))) == NULL) 271*0Sstevel@tonic-gate goto fail; 272*0Sstevel@tonic-gate 273*0Sstevel@tonic-gate uu_list_node_init(ti, &ti->ti_node, thread_pool); 274*0Sstevel@tonic-gate ti->ti_state = TI_CREATED; 275*0Sstevel@tonic-gate ti->ti_prev_state = TI_CREATED; 276*0Sstevel@tonic-gate 277*0Sstevel@tonic-gate if ((ti->ti_ucred = uu_zalloc(ucred_size())) == NULL) 278*0Sstevel@tonic-gate goto fail; 279*0Sstevel@tonic-gate 280*0Sstevel@tonic-gate (void) sigfillset(&new); 281*0Sstevel@tonic-gate (void) pthread_sigmask(SIG_SETMASK, &new, &old); 282*0Sstevel@tonic-gate if ((errno = pthread_create(&ti->ti_thread, &thread_attr, thread_start, 283*0Sstevel@tonic-gate ti)) != 0) { 284*0Sstevel@tonic-gate (void) pthread_sigmask(SIG_SETMASK, &old, NULL); 285*0Sstevel@tonic-gate goto fail; 286*0Sstevel@tonic-gate } 287*0Sstevel@tonic-gate 288*0Sstevel@tonic-gate (void) pthread_sigmask(SIG_SETMASK, &old, NULL); 289*0Sstevel@tonic-gate return; 290*0Sstevel@tonic-gate 291*0Sstevel@tonic-gate fail: 292*0Sstevel@tonic-gate /* 293*0Sstevel@tonic-gate * Since the thread_info structure was never linked onto the 294*0Sstevel@tonic-gate * thread list, thread_exiting() can't handle the cleanup. 295*0Sstevel@tonic-gate */ 296*0Sstevel@tonic-gate thread_exiting(NULL); 297*0Sstevel@tonic-gate if (ti != NULL) 298*0Sstevel@tonic-gate thread_info_free(ti); 299*0Sstevel@tonic-gate } 300*0Sstevel@tonic-gate 301*0Sstevel@tonic-gate int 302*0Sstevel@tonic-gate create_connection(ucred_t *uc, repository_door_request_t *rp, 303*0Sstevel@tonic-gate size_t rp_size, int *out_fd) 304*0Sstevel@tonic-gate { 305*0Sstevel@tonic-gate int flags; 306*0Sstevel@tonic-gate int privileged = 0; 307*0Sstevel@tonic-gate uint32_t debugflags = 0; 308*0Sstevel@tonic-gate psinfo_t info; 309*0Sstevel@tonic-gate 310*0Sstevel@tonic-gate if (privileged_pid != 0) { 311*0Sstevel@tonic-gate /* 312*0Sstevel@tonic-gate * in privileged pid mode, we only allow connections from 313*0Sstevel@tonic-gate * our original parent -- the psinfo read verifies that 314*0Sstevel@tonic-gate * it is the same process which we started with. 315*0Sstevel@tonic-gate */ 316*0Sstevel@tonic-gate if (ucred_getpid(uc) != privileged_pid || 317*0Sstevel@tonic-gate read(privileged_psinfo_fd, &info, sizeof (info)) != 318*0Sstevel@tonic-gate sizeof (info)) 319*0Sstevel@tonic-gate return (REPOSITORY_DOOR_FAIL_PERMISSION_DENIED); 320*0Sstevel@tonic-gate 321*0Sstevel@tonic-gate privileged = 1; /* he gets full privileges */ 322*0Sstevel@tonic-gate } else if (privileged_user != 0) { 323*0Sstevel@tonic-gate /* 324*0Sstevel@tonic-gate * in privileged user mode, only one particular user is 325*0Sstevel@tonic-gate * allowed to connect to us, and he can do anything. 326*0Sstevel@tonic-gate */ 327*0Sstevel@tonic-gate if (ucred_geteuid(uc) != privileged_user) 328*0Sstevel@tonic-gate return (REPOSITORY_DOOR_FAIL_PERMISSION_DENIED); 329*0Sstevel@tonic-gate 330*0Sstevel@tonic-gate privileged = 1; 331*0Sstevel@tonic-gate } 332*0Sstevel@tonic-gate 333*0Sstevel@tonic-gate /* 334*0Sstevel@tonic-gate * Check that rp, of size rp_size, is large enough to 335*0Sstevel@tonic-gate * contain field 'f'. If so, write the value into *out, and return 1. 336*0Sstevel@tonic-gate * Otherwise, return 0. 337*0Sstevel@tonic-gate */ 338*0Sstevel@tonic-gate #define GET_ARG(rp, rp_size, f, out) \ 339*0Sstevel@tonic-gate (((rp_size) >= offsetofend(repository_door_request_t, f)) ? \ 340*0Sstevel@tonic-gate ((*(out) = (rp)->f), 1) : 0) 341*0Sstevel@tonic-gate 342*0Sstevel@tonic-gate if (!GET_ARG(rp, rp_size, rdr_flags, &flags)) 343*0Sstevel@tonic-gate return (REPOSITORY_DOOR_FAIL_BAD_REQUEST); 344*0Sstevel@tonic-gate 345*0Sstevel@tonic-gate #if (REPOSITORY_DOOR_FLAG_ALL != REPOSITORY_DOOR_FLAG_DEBUG) 346*0Sstevel@tonic-gate #error Need to update flag checks 347*0Sstevel@tonic-gate #endif 348*0Sstevel@tonic-gate 349*0Sstevel@tonic-gate if (flags & ~REPOSITORY_DOOR_FLAG_ALL) 350*0Sstevel@tonic-gate return (REPOSITORY_DOOR_FAIL_BAD_FLAG); 351*0Sstevel@tonic-gate 352*0Sstevel@tonic-gate if (flags & REPOSITORY_DOOR_FLAG_DEBUG) 353*0Sstevel@tonic-gate if (!GET_ARG(rp, rp_size, rdr_debug, &debugflags)) 354*0Sstevel@tonic-gate return (REPOSITORY_DOOR_FAIL_BAD_REQUEST); 355*0Sstevel@tonic-gate #undef GET_ARG 356*0Sstevel@tonic-gate 357*0Sstevel@tonic-gate return (create_client(ucred_getpid(uc), debugflags, privileged, 358*0Sstevel@tonic-gate out_fd)); 359*0Sstevel@tonic-gate } 360*0Sstevel@tonic-gate 361*0Sstevel@tonic-gate void 362*0Sstevel@tonic-gate configd_vcritical(const char *message, va_list args) 363*0Sstevel@tonic-gate { 364*0Sstevel@tonic-gate if (log_to_syslog) 365*0Sstevel@tonic-gate vsyslog(LOG_CRIT, message, args); 366*0Sstevel@tonic-gate else { 367*0Sstevel@tonic-gate flockfile(stderr); 368*0Sstevel@tonic-gate (void) fprintf(stderr, "svc.configd: Fatal error: "); 369*0Sstevel@tonic-gate (void) vfprintf(stderr, message, args); 370*0Sstevel@tonic-gate if (message[0] == 0 || message[strlen(message) - 1] != '\n') 371*0Sstevel@tonic-gate (void) fprintf(stderr, "\n"); 372*0Sstevel@tonic-gate funlockfile(stderr); 373*0Sstevel@tonic-gate } 374*0Sstevel@tonic-gate } 375*0Sstevel@tonic-gate 376*0Sstevel@tonic-gate void 377*0Sstevel@tonic-gate configd_critical(const char *message, ...) 378*0Sstevel@tonic-gate { 379*0Sstevel@tonic-gate va_list args; 380*0Sstevel@tonic-gate va_start(args, message); 381*0Sstevel@tonic-gate configd_vcritical(message, args); 382*0Sstevel@tonic-gate va_end(args); 383*0Sstevel@tonic-gate } 384*0Sstevel@tonic-gate 385*0Sstevel@tonic-gate static void 386*0Sstevel@tonic-gate usage(const char *prog, int ret) 387*0Sstevel@tonic-gate { 388*0Sstevel@tonic-gate (void) fprintf(stderr, 389*0Sstevel@tonic-gate "usage: %s [-np] [-d door_path] [-r repository_path]\n" 390*0Sstevel@tonic-gate " [-t nonpersist_repository]\n", prog); 391*0Sstevel@tonic-gate exit(ret); 392*0Sstevel@tonic-gate } 393*0Sstevel@tonic-gate 394*0Sstevel@tonic-gate /*ARGSUSED*/ 395*0Sstevel@tonic-gate static void 396*0Sstevel@tonic-gate handler(int sig, siginfo_t *info, void *data) 397*0Sstevel@tonic-gate { 398*0Sstevel@tonic-gate finished = 1; 399*0Sstevel@tonic-gate } 400*0Sstevel@tonic-gate 401*0Sstevel@tonic-gate static int pipe_fd = -1; 402*0Sstevel@tonic-gate 403*0Sstevel@tonic-gate static int 404*0Sstevel@tonic-gate daemonize_start(void) 405*0Sstevel@tonic-gate { 406*0Sstevel@tonic-gate char data; 407*0Sstevel@tonic-gate int status; 408*0Sstevel@tonic-gate 409*0Sstevel@tonic-gate int filedes[2]; 410*0Sstevel@tonic-gate pid_t pid; 411*0Sstevel@tonic-gate 412*0Sstevel@tonic-gate (void) close(0); 413*0Sstevel@tonic-gate (void) dup2(2, 1); /* stderr only */ 414*0Sstevel@tonic-gate 415*0Sstevel@tonic-gate if (pipe(filedes) < 0) 416*0Sstevel@tonic-gate return (-1); 417*0Sstevel@tonic-gate 418*0Sstevel@tonic-gate if ((pid = fork1()) < 0) 419*0Sstevel@tonic-gate return (-1); 420*0Sstevel@tonic-gate 421*0Sstevel@tonic-gate if (pid != 0) { 422*0Sstevel@tonic-gate /* 423*0Sstevel@tonic-gate * parent 424*0Sstevel@tonic-gate */ 425*0Sstevel@tonic-gate struct sigaction act; 426*0Sstevel@tonic-gate 427*0Sstevel@tonic-gate act.sa_sigaction = SIG_DFL; 428*0Sstevel@tonic-gate (void) sigemptyset(&act.sa_mask); 429*0Sstevel@tonic-gate act.sa_flags = 0; 430*0Sstevel@tonic-gate 431*0Sstevel@tonic-gate (void) sigaction(SIGPIPE, &act, NULL); /* ignore SIGPIPE */ 432*0Sstevel@tonic-gate 433*0Sstevel@tonic-gate (void) close(filedes[1]); 434*0Sstevel@tonic-gate if (read(filedes[0], &data, 1) == 1) { 435*0Sstevel@tonic-gate /* presume success */ 436*0Sstevel@tonic-gate _exit(CONFIGD_EXIT_OKAY); 437*0Sstevel@tonic-gate } 438*0Sstevel@tonic-gate 439*0Sstevel@tonic-gate status = -1; 440*0Sstevel@tonic-gate (void) wait4(pid, &status, 0, NULL); 441*0Sstevel@tonic-gate if (WIFEXITED(status)) 442*0Sstevel@tonic-gate _exit(WEXITSTATUS(status)); 443*0Sstevel@tonic-gate else 444*0Sstevel@tonic-gate _exit(-1); 445*0Sstevel@tonic-gate } 446*0Sstevel@tonic-gate 447*0Sstevel@tonic-gate /* 448*0Sstevel@tonic-gate * child 449*0Sstevel@tonic-gate */ 450*0Sstevel@tonic-gate pipe_fd = filedes[1]; 451*0Sstevel@tonic-gate (void) close(filedes[0]); 452*0Sstevel@tonic-gate 453*0Sstevel@tonic-gate /* 454*0Sstevel@tonic-gate * generic Unix setup 455*0Sstevel@tonic-gate */ 456*0Sstevel@tonic-gate (void) setsid(); 457*0Sstevel@tonic-gate (void) umask(0077); 458*0Sstevel@tonic-gate 459*0Sstevel@tonic-gate return (0); 460*0Sstevel@tonic-gate } 461*0Sstevel@tonic-gate 462*0Sstevel@tonic-gate static void 463*0Sstevel@tonic-gate daemonize_ready(void) 464*0Sstevel@tonic-gate { 465*0Sstevel@tonic-gate char data = '\0'; 466*0Sstevel@tonic-gate 467*0Sstevel@tonic-gate /* 468*0Sstevel@tonic-gate * wake the parent 469*0Sstevel@tonic-gate */ 470*0Sstevel@tonic-gate (void) write(pipe_fd, &data, 1); 471*0Sstevel@tonic-gate (void) close(pipe_fd); 472*0Sstevel@tonic-gate } 473*0Sstevel@tonic-gate 474*0Sstevel@tonic-gate int 475*0Sstevel@tonic-gate main(int argc, char *argv[]) 476*0Sstevel@tonic-gate { 477*0Sstevel@tonic-gate thread_info_t *ti = &main_thread_info; 478*0Sstevel@tonic-gate 479*0Sstevel@tonic-gate char pidpath[sizeof ("/proc/" "/psinfo") + 10]; 480*0Sstevel@tonic-gate 481*0Sstevel@tonic-gate struct rlimit fd_new; 482*0Sstevel@tonic-gate 483*0Sstevel@tonic-gate const char *endptr; 484*0Sstevel@tonic-gate sigset_t myset; 485*0Sstevel@tonic-gate int c; 486*0Sstevel@tonic-gate int ret; 487*0Sstevel@tonic-gate int fd; 488*0Sstevel@tonic-gate const char *dbpath = NULL; 489*0Sstevel@tonic-gate const char *npdbpath = NULL; 490*0Sstevel@tonic-gate const char *doorpath = REPOSITORY_DOOR_NAME; 491*0Sstevel@tonic-gate struct sigaction act; 492*0Sstevel@tonic-gate 493*0Sstevel@tonic-gate int daemonize = 1; /* default to daemonizing */ 494*0Sstevel@tonic-gate int have_npdb = 1; 495*0Sstevel@tonic-gate 496*0Sstevel@tonic-gate closefrom(3); /* get rid of extraneous fds */ 497*0Sstevel@tonic-gate 498*0Sstevel@tonic-gate while ((c = getopt(argc, argv, "Dnpd:r:t:")) != -1) { 499*0Sstevel@tonic-gate switch (c) { 500*0Sstevel@tonic-gate case 'n': 501*0Sstevel@tonic-gate daemonize = 0; 502*0Sstevel@tonic-gate break; 503*0Sstevel@tonic-gate case 'd': 504*0Sstevel@tonic-gate doorpath = optarg; 505*0Sstevel@tonic-gate is_main_repository = 0; 506*0Sstevel@tonic-gate have_npdb = 0; /* default to no non-persist */ 507*0Sstevel@tonic-gate break; 508*0Sstevel@tonic-gate case 'p': 509*0Sstevel@tonic-gate log_to_syslog = 0; /* don't use syslog */ 510*0Sstevel@tonic-gate is_main_repository = 0; 511*0Sstevel@tonic-gate 512*0Sstevel@tonic-gate /* 513*0Sstevel@tonic-gate * If our parent exits while we're opening its /proc 514*0Sstevel@tonic-gate * psinfo, we're vulnerable to a pid wrapping. To 515*0Sstevel@tonic-gate * protect against that, re-check our ppid after 516*0Sstevel@tonic-gate * opening it. 517*0Sstevel@tonic-gate */ 518*0Sstevel@tonic-gate privileged_pid = getppid(); 519*0Sstevel@tonic-gate (void) snprintf(pidpath, sizeof (pidpath), 520*0Sstevel@tonic-gate "/proc/%d/psinfo", privileged_pid); 521*0Sstevel@tonic-gate if ((fd = open(pidpath, O_RDONLY)) < 0 || 522*0Sstevel@tonic-gate getppid() != privileged_pid) { 523*0Sstevel@tonic-gate (void) fprintf(stderr, 524*0Sstevel@tonic-gate "%s: unable to get parent info\n", argv[0]); 525*0Sstevel@tonic-gate exit(CONFIGD_EXIT_BAD_ARGS); 526*0Sstevel@tonic-gate } 527*0Sstevel@tonic-gate privileged_psinfo_fd = fd; 528*0Sstevel@tonic-gate break; 529*0Sstevel@tonic-gate case 'r': 530*0Sstevel@tonic-gate dbpath = optarg; 531*0Sstevel@tonic-gate is_main_repository = 0; 532*0Sstevel@tonic-gate break; 533*0Sstevel@tonic-gate case 't': 534*0Sstevel@tonic-gate npdbpath = optarg; 535*0Sstevel@tonic-gate is_main_repository = 0; 536*0Sstevel@tonic-gate break; 537*0Sstevel@tonic-gate default: 538*0Sstevel@tonic-gate usage(argv[0], CONFIGD_EXIT_BAD_ARGS); 539*0Sstevel@tonic-gate break; 540*0Sstevel@tonic-gate } 541*0Sstevel@tonic-gate } 542*0Sstevel@tonic-gate 543*0Sstevel@tonic-gate /* 544*0Sstevel@tonic-gate * If we're not running as root, allow our euid full access, and 545*0Sstevel@tonic-gate * everyone else no access. 546*0Sstevel@tonic-gate */ 547*0Sstevel@tonic-gate if (privileged_pid == 0 && geteuid() != 0) { 548*0Sstevel@tonic-gate privileged_user = geteuid(); 549*0Sstevel@tonic-gate } 550*0Sstevel@tonic-gate 551*0Sstevel@tonic-gate privileged_privs = priv_str_to_set("zone", "", &endptr); 552*0Sstevel@tonic-gate if (endptr != NULL && privileged_privs != NULL) { 553*0Sstevel@tonic-gate priv_freeset(privileged_privs); 554*0Sstevel@tonic-gate privileged_privs = NULL; 555*0Sstevel@tonic-gate } 556*0Sstevel@tonic-gate 557*0Sstevel@tonic-gate openlog("svc.configd", LOG_PID | LOG_CONS, LOG_DAEMON); 558*0Sstevel@tonic-gate (void) setlogmask(LOG_UPTO(LOG_NOTICE)); 559*0Sstevel@tonic-gate 560*0Sstevel@tonic-gate /* 561*0Sstevel@tonic-gate * if a non-persist db is specified, always enable it 562*0Sstevel@tonic-gate */ 563*0Sstevel@tonic-gate if (npdbpath) 564*0Sstevel@tonic-gate have_npdb = 1; 565*0Sstevel@tonic-gate 566*0Sstevel@tonic-gate if (optind != argc) 567*0Sstevel@tonic-gate usage(argv[0], CONFIGD_EXIT_BAD_ARGS); 568*0Sstevel@tonic-gate 569*0Sstevel@tonic-gate if (daemonize) { 570*0Sstevel@tonic-gate if (getuid() == 0) 571*0Sstevel@tonic-gate (void) chdir("/"); 572*0Sstevel@tonic-gate if (daemonize_start() < 0) { 573*0Sstevel@tonic-gate (void) perror("unable to daemonize"); 574*0Sstevel@tonic-gate exit(CONFIGD_EXIT_INIT_FAILED); 575*0Sstevel@tonic-gate } 576*0Sstevel@tonic-gate } 577*0Sstevel@tonic-gate if (getuid() == 0) 578*0Sstevel@tonic-gate (void) core_set_process_path(CONFIGD_CORE, 579*0Sstevel@tonic-gate strlen(CONFIGD_CORE) + 1, getpid()); 580*0Sstevel@tonic-gate 581*0Sstevel@tonic-gate /* 582*0Sstevel@tonic-gate * this should be enabled once we can drop privileges and still get 583*0Sstevel@tonic-gate * a core dump. 584*0Sstevel@tonic-gate */ 585*0Sstevel@tonic-gate #if 0 586*0Sstevel@tonic-gate /* turn off basic privileges we do not need */ 587*0Sstevel@tonic-gate (void) priv_set(PRIV_OFF, PRIV_PERMITTED, PRIV_FILE_LINK_ANY, 588*0Sstevel@tonic-gate PRIV_PROC_EXEC, PRIV_PROC_FORK, PRIV_PROC_SESSION, NULL); 589*0Sstevel@tonic-gate #endif 590*0Sstevel@tonic-gate 591*0Sstevel@tonic-gate /* not that we can exec, but to be safe, shut them all off... */ 592*0Sstevel@tonic-gate (void) priv_set(PRIV_SET, PRIV_INHERITABLE, NULL); 593*0Sstevel@tonic-gate 594*0Sstevel@tonic-gate (void) sigfillset(&act.sa_mask); 595*0Sstevel@tonic-gate 596*0Sstevel@tonic-gate /* signals to ignore */ 597*0Sstevel@tonic-gate act.sa_sigaction = SIG_IGN; 598*0Sstevel@tonic-gate act.sa_flags = 0; 599*0Sstevel@tonic-gate (void) sigaction(SIGPIPE, &act, NULL); 600*0Sstevel@tonic-gate (void) sigaction(SIGALRM, &act, NULL); 601*0Sstevel@tonic-gate (void) sigaction(SIGUSR1, &act, NULL); 602*0Sstevel@tonic-gate (void) sigaction(SIGUSR2, &act, NULL); 603*0Sstevel@tonic-gate (void) sigaction(SIGPOLL, &act, NULL); 604*0Sstevel@tonic-gate 605*0Sstevel@tonic-gate /* signals to abort on */ 606*0Sstevel@tonic-gate act.sa_sigaction = (void (*)(int, siginfo_t *, void *))&abort_handler; 607*0Sstevel@tonic-gate act.sa_flags = SA_SIGINFO; 608*0Sstevel@tonic-gate 609*0Sstevel@tonic-gate (void) sigaction(SIGABRT, &act, NULL); 610*0Sstevel@tonic-gate 611*0Sstevel@tonic-gate /* signals to handle */ 612*0Sstevel@tonic-gate act.sa_sigaction = &handler; 613*0Sstevel@tonic-gate act.sa_flags = SA_SIGINFO; 614*0Sstevel@tonic-gate 615*0Sstevel@tonic-gate (void) sigaction(SIGHUP, &act, NULL); 616*0Sstevel@tonic-gate (void) sigaction(SIGINT, &act, NULL); 617*0Sstevel@tonic-gate (void) sigaction(SIGTERM, &act, NULL); 618*0Sstevel@tonic-gate 619*0Sstevel@tonic-gate (void) sigemptyset(&myset); 620*0Sstevel@tonic-gate (void) sigaddset(&myset, SIGHUP); 621*0Sstevel@tonic-gate (void) sigaddset(&myset, SIGINT); 622*0Sstevel@tonic-gate (void) sigaddset(&myset, SIGTERM); 623*0Sstevel@tonic-gate 624*0Sstevel@tonic-gate if ((errno = pthread_attr_init(&thread_attr)) != 0) { 625*0Sstevel@tonic-gate (void) perror("initializing"); 626*0Sstevel@tonic-gate exit(CONFIGD_EXIT_INIT_FAILED); 627*0Sstevel@tonic-gate } 628*0Sstevel@tonic-gate 629*0Sstevel@tonic-gate /* 630*0Sstevel@tonic-gate * Set the hard and soft limits to CONFIGD_MAX_FDS. 631*0Sstevel@tonic-gate */ 632*0Sstevel@tonic-gate fd_new.rlim_max = fd_new.rlim_cur = CONFIGD_MAX_FDS; 633*0Sstevel@tonic-gate (void) setrlimit(RLIMIT_NOFILE, &fd_new); 634*0Sstevel@tonic-gate 635*0Sstevel@tonic-gate if ((ret = backend_init(dbpath, npdbpath, have_npdb)) != 636*0Sstevel@tonic-gate CONFIGD_EXIT_OKAY) 637*0Sstevel@tonic-gate exit(ret); 638*0Sstevel@tonic-gate 639*0Sstevel@tonic-gate if (!client_init()) 640*0Sstevel@tonic-gate exit(CONFIGD_EXIT_INIT_FAILED); 641*0Sstevel@tonic-gate 642*0Sstevel@tonic-gate if (!rc_node_init()) 643*0Sstevel@tonic-gate exit(CONFIGD_EXIT_INIT_FAILED); 644*0Sstevel@tonic-gate 645*0Sstevel@tonic-gate (void) pthread_attr_setdetachstate(&thread_attr, 646*0Sstevel@tonic-gate PTHREAD_CREATE_DETACHED); 647*0Sstevel@tonic-gate (void) pthread_attr_setscope(&thread_attr, PTHREAD_SCOPE_SYSTEM); 648*0Sstevel@tonic-gate 649*0Sstevel@tonic-gate if ((errno = pthread_key_create(&thread_info_key, 650*0Sstevel@tonic-gate thread_exiting)) != 0) { 651*0Sstevel@tonic-gate perror("pthread_key_create"); 652*0Sstevel@tonic-gate exit(CONFIGD_EXIT_INIT_FAILED); 653*0Sstevel@tonic-gate } 654*0Sstevel@tonic-gate 655*0Sstevel@tonic-gate if ((thread_pool = uu_list_pool_create("thread_pool", 656*0Sstevel@tonic-gate sizeof (thread_info_t), offsetof(thread_info_t, ti_node), 657*0Sstevel@tonic-gate NULL, UU_LIST_POOL_DEBUG)) == NULL) { 658*0Sstevel@tonic-gate configd_critical("uu_list_pool_create: %s\n", 659*0Sstevel@tonic-gate uu_strerror(uu_error())); 660*0Sstevel@tonic-gate exit(CONFIGD_EXIT_INIT_FAILED); 661*0Sstevel@tonic-gate } 662*0Sstevel@tonic-gate 663*0Sstevel@tonic-gate if ((thread_list = uu_list_create(thread_pool, NULL, 0)) == NULL) { 664*0Sstevel@tonic-gate configd_critical("uu_list_create: %s\n", 665*0Sstevel@tonic-gate uu_strerror(uu_error())); 666*0Sstevel@tonic-gate exit(CONFIGD_EXIT_INIT_FAILED); 667*0Sstevel@tonic-gate } 668*0Sstevel@tonic-gate 669*0Sstevel@tonic-gate (void) memset(ti, '\0', sizeof (*ti)); 670*0Sstevel@tonic-gate uu_list_node_init(ti, &ti->ti_node, thread_pool); 671*0Sstevel@tonic-gate (void) uu_list_insert_before(thread_list, uu_list_first(thread_list), 672*0Sstevel@tonic-gate ti); 673*0Sstevel@tonic-gate 674*0Sstevel@tonic-gate ti->ti_thread = pthread_self(); 675*0Sstevel@tonic-gate ti->ti_state = TI_SIGNAL_WAIT; 676*0Sstevel@tonic-gate ti->ti_prev_state = TI_SIGNAL_WAIT; 677*0Sstevel@tonic-gate 678*0Sstevel@tonic-gate (void) pthread_setspecific(thread_info_key, ti); 679*0Sstevel@tonic-gate 680*0Sstevel@tonic-gate (void) door_server_create(new_thread_needed); 681*0Sstevel@tonic-gate 682*0Sstevel@tonic-gate if (!setup_main_door(doorpath)) { 683*0Sstevel@tonic-gate configd_critical("Setting up main door failed.\n"); 684*0Sstevel@tonic-gate exit(CONFIGD_EXIT_DOOR_INIT_FAILED); 685*0Sstevel@tonic-gate } 686*0Sstevel@tonic-gate 687*0Sstevel@tonic-gate if (daemonize) 688*0Sstevel@tonic-gate daemonize_ready(); 689*0Sstevel@tonic-gate 690*0Sstevel@tonic-gate (void) pthread_sigmask(SIG_BLOCK, &myset, NULL); 691*0Sstevel@tonic-gate while (!finished) { 692*0Sstevel@tonic-gate int sig = sigwait(&myset); 693*0Sstevel@tonic-gate if (sig > 0) { 694*0Sstevel@tonic-gate break; 695*0Sstevel@tonic-gate } 696*0Sstevel@tonic-gate } 697*0Sstevel@tonic-gate 698*0Sstevel@tonic-gate backend_fini(); 699*0Sstevel@tonic-gate 700*0Sstevel@tonic-gate return (CONFIGD_EXIT_OKAY); 701*0Sstevel@tonic-gate } 702