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