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 524b3ac2eSjfrank * Common Development and Distribution License (the "License"). 624b3ac2eSjfrank * 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 */ 210ec57554Sraf 227c478bd9Sstevel@tonic-gate /* 2324b3ac2eSjfrank * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 247c478bd9Sstevel@tonic-gate * Use is subject to license terms. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 287c478bd9Sstevel@tonic-gate 297c478bd9Sstevel@tonic-gate /* 307c478bd9Sstevel@tonic-gate * PICL daemon 317c478bd9Sstevel@tonic-gate */ 327c478bd9Sstevel@tonic-gate 337c478bd9Sstevel@tonic-gate #include <stdio.h> 347c478bd9Sstevel@tonic-gate #include <stdlib.h> 357c478bd9Sstevel@tonic-gate #include <stdarg.h> 367c478bd9Sstevel@tonic-gate #include <string.h> 377c478bd9Sstevel@tonic-gate #include <libintl.h> 387c478bd9Sstevel@tonic-gate #include <locale.h> 397c478bd9Sstevel@tonic-gate #include <alloca.h> 407c478bd9Sstevel@tonic-gate #include <errno.h> 417c478bd9Sstevel@tonic-gate #include <assert.h> 427c478bd9Sstevel@tonic-gate #include <stropts.h> 437c478bd9Sstevel@tonic-gate #include <unistd.h> 447c478bd9Sstevel@tonic-gate #include <signal.h> 457c478bd9Sstevel@tonic-gate #include <pthread.h> 467c478bd9Sstevel@tonic-gate #include <synch.h> 477c478bd9Sstevel@tonic-gate #include <door.h> 487c478bd9Sstevel@tonic-gate #include <sys/door.h> 497c478bd9Sstevel@tonic-gate #include <fcntl.h> 507c478bd9Sstevel@tonic-gate #include <dlfcn.h> 517c478bd9Sstevel@tonic-gate #include <time.h> 527c478bd9Sstevel@tonic-gate #include <sys/utsname.h> 537c478bd9Sstevel@tonic-gate #include <sys/systeminfo.h> 547c478bd9Sstevel@tonic-gate #include <sys/stat.h> 557c478bd9Sstevel@tonic-gate #include <sys/wait.h> 567c478bd9Sstevel@tonic-gate #include <dirent.h> 577c478bd9Sstevel@tonic-gate #include <syslog.h> 587c478bd9Sstevel@tonic-gate #include <poll.h> 597c478bd9Sstevel@tonic-gate #include <limits.h> 607c478bd9Sstevel@tonic-gate #include <picl.h> 617c478bd9Sstevel@tonic-gate #include "picl2door.h" 627c478bd9Sstevel@tonic-gate #include <picltree.h> 637c478bd9Sstevel@tonic-gate #include "ptree_impl.h" 647c478bd9Sstevel@tonic-gate 657c478bd9Sstevel@tonic-gate /* 667c478bd9Sstevel@tonic-gate * Log text messages 677c478bd9Sstevel@tonic-gate */ 687c478bd9Sstevel@tonic-gate #define MUST_BE_ROOT gettext("this program must be run as root\n") 697c478bd9Sstevel@tonic-gate #define CD_ROOT_FAILED gettext("chdir to root failed\n") 707c478bd9Sstevel@tonic-gate #define INIT_FAILED gettext("ptree initialization failed\n") 717c478bd9Sstevel@tonic-gate #define DAEMON_RUNNING gettext("PICL daemon already running\n") 727c478bd9Sstevel@tonic-gate #define DOOR_FAILED gettext("Failed creating picld door\n") 737c478bd9Sstevel@tonic-gate #define SIGACT_FAILED \ 747c478bd9Sstevel@tonic-gate gettext("Failed to install signal handler for %s: %s\n") 757c478bd9Sstevel@tonic-gate 767c478bd9Sstevel@tonic-gate /* 777c478bd9Sstevel@tonic-gate * Constants 787c478bd9Sstevel@tonic-gate */ 797c478bd9Sstevel@tonic-gate #define PICLD "picld" 807c478bd9Sstevel@tonic-gate #define DOS_PICL_REQUESTS_LIMIT 10000 817c478bd9Sstevel@tonic-gate #define SLIDING_INTERVAL_MILLISECONDS 1000 827c478bd9Sstevel@tonic-gate #define PICLD_MAJOR_REV 0x1 837c478bd9Sstevel@tonic-gate #define PICLD_MINOR_REV 0x0 847c478bd9Sstevel@tonic-gate #define DOS_SLEEPTIME_MS 1000 85*980a6e61Sjfrank #define MAX_POOL_SIZE _POSIX_THREAD_THREADS_MAX 86*980a6e61Sjfrank #define MAX_CONCURRENT_WAITS (_POSIX_THREAD_THREADS_MAX - 2) 87*980a6e61Sjfrank #define MAX_USER_WAITS 4 887c478bd9Sstevel@tonic-gate 897c478bd9Sstevel@tonic-gate /* 907c478bd9Sstevel@tonic-gate * Macros 917c478bd9Sstevel@tonic-gate */ 927c478bd9Sstevel@tonic-gate #define PICLD_VERSION(x, y) ((x << 8) | y) 937c478bd9Sstevel@tonic-gate #define PICL_CLIENT_REV(x) (x & 0xff) 947c478bd9Sstevel@tonic-gate #define MILLI_TO_NANO(x) (x * 1000000) 957c478bd9Sstevel@tonic-gate 967c478bd9Sstevel@tonic-gate extern char **environ; 977c478bd9Sstevel@tonic-gate 987c478bd9Sstevel@tonic-gate /* 997c478bd9Sstevel@tonic-gate * Module Variables 1007c478bd9Sstevel@tonic-gate */ 1017c478bd9Sstevel@tonic-gate static int logflag = 1; 1027c478bd9Sstevel@tonic-gate static int doreinit = 0; 1037c478bd9Sstevel@tonic-gate static int door_id = -1; 104*980a6e61Sjfrank static pthread_mutex_t door_mutex = PTHREAD_MUTEX_INITIALIZER; 105*980a6e61Sjfrank static pthread_cond_t door_cv = PTHREAD_COND_INITIALIZER; 1067c478bd9Sstevel@tonic-gate static int service_requests = 0; 1077c478bd9Sstevel@tonic-gate static hrtime_t orig_time; 1087c478bd9Sstevel@tonic-gate static hrtime_t sliding_interval_ms; 1097c478bd9Sstevel@tonic-gate static uint32_t dos_req_limit; 1107c478bd9Sstevel@tonic-gate static uint32_t dos_ms; 1117c478bd9Sstevel@tonic-gate static pthread_mutex_t dos_mutex = PTHREAD_MUTEX_INITIALIZER; 1127c478bd9Sstevel@tonic-gate static rwlock_t init_lk; 113*980a6e61Sjfrank static int pool_count = 0; 114*980a6e61Sjfrank static pthread_mutex_t pool_mutex = PTHREAD_MUTEX_INITIALIZER; 115*980a6e61Sjfrank static pthread_mutex_t wait_req_mutex = PTHREAD_MUTEX_INITIALIZER; 116*980a6e61Sjfrank static int wait_count = 0; 117*980a6e61Sjfrank static struct { 118*980a6e61Sjfrank uid_t uid; 119*980a6e61Sjfrank int count; 120*980a6e61Sjfrank } user_count[MAX_CONCURRENT_WAITS]; 1217c478bd9Sstevel@tonic-gate 1227c478bd9Sstevel@tonic-gate /* 1237c478bd9Sstevel@tonic-gate * This returns an error message to libpicl 1247c478bd9Sstevel@tonic-gate */ 1257c478bd9Sstevel@tonic-gate static void 1267c478bd9Sstevel@tonic-gate picld_return_error(picl_callnumber_t cnum, picl_errno_t err) 1277c478bd9Sstevel@tonic-gate { 1287c478bd9Sstevel@tonic-gate picl_reterror_t ret_error; 1297c478bd9Sstevel@tonic-gate 1307c478bd9Sstevel@tonic-gate ret_error.cnum = PICL_CNUM_ERROR; 1317c478bd9Sstevel@tonic-gate ret_error.in_cnum = cnum; 1327c478bd9Sstevel@tonic-gate ret_error.errnum = err; 1337c478bd9Sstevel@tonic-gate (void) rw_unlock(&init_lk); 1347c478bd9Sstevel@tonic-gate (void) door_return((char *)&ret_error, sizeof (picl_reterror_t), NULL, 1357c478bd9Sstevel@tonic-gate 0); 1367c478bd9Sstevel@tonic-gate } 1377c478bd9Sstevel@tonic-gate 1387c478bd9Sstevel@tonic-gate /* 1397c478bd9Sstevel@tonic-gate * picld_init is called when a picl_initialize request is received 1407c478bd9Sstevel@tonic-gate */ 1417c478bd9Sstevel@tonic-gate static void 1427c478bd9Sstevel@tonic-gate picld_init(picl_service_t *req) 1437c478bd9Sstevel@tonic-gate { 1447c478bd9Sstevel@tonic-gate picl_retinit_t ret_init; 1457c478bd9Sstevel@tonic-gate int clmajrev; 1467c478bd9Sstevel@tonic-gate 1477c478bd9Sstevel@tonic-gate clmajrev = PICL_CLIENT_REV(req->req_init.clrev); 1487c478bd9Sstevel@tonic-gate 1497c478bd9Sstevel@tonic-gate if (clmajrev < PICL_VERSION_1) 1507c478bd9Sstevel@tonic-gate picld_return_error(req->req_init.cnum, PICL_NOTSUPPORTED); 1517c478bd9Sstevel@tonic-gate 1527c478bd9Sstevel@tonic-gate ret_init.cnum = req->req_init.cnum; 1537c478bd9Sstevel@tonic-gate ret_init.rev = PICLD_VERSION(PICLD_MAJOR_REV, PICLD_MINOR_REV); 1547c478bd9Sstevel@tonic-gate 1557c478bd9Sstevel@tonic-gate (void) rw_unlock(&init_lk); 1567c478bd9Sstevel@tonic-gate (void) door_return((char *)&ret_init, sizeof (picl_retinit_t), NULL, 0); 1577c478bd9Sstevel@tonic-gate } 1587c478bd9Sstevel@tonic-gate 1597c478bd9Sstevel@tonic-gate /* 1607c478bd9Sstevel@tonic-gate * picld_fini is called when a picl_shutdown request is received 1617c478bd9Sstevel@tonic-gate */ 1627c478bd9Sstevel@tonic-gate static void 1637c478bd9Sstevel@tonic-gate picld_fini(picl_service_t *in) 1647c478bd9Sstevel@tonic-gate { 1657c478bd9Sstevel@tonic-gate picl_retfini_t ret; 1667c478bd9Sstevel@tonic-gate 1677c478bd9Sstevel@tonic-gate ret.cnum = in->req_fini.cnum; 1687c478bd9Sstevel@tonic-gate 1697c478bd9Sstevel@tonic-gate (void) rw_unlock(&init_lk); 1707c478bd9Sstevel@tonic-gate (void) door_return((char *)&ret, sizeof (picl_retfini_t), NULL, 0); 1717c478bd9Sstevel@tonic-gate } 1727c478bd9Sstevel@tonic-gate 1737c478bd9Sstevel@tonic-gate static void 1747c478bd9Sstevel@tonic-gate picld_ping(picl_service_t *in) 1757c478bd9Sstevel@tonic-gate { 1767c478bd9Sstevel@tonic-gate picl_retping_t ret; 1777c478bd9Sstevel@tonic-gate 1787c478bd9Sstevel@tonic-gate ret.cnum = in->req_ping.cnum; 1797c478bd9Sstevel@tonic-gate 1807c478bd9Sstevel@tonic-gate (void) rw_unlock(&init_lk); 1817c478bd9Sstevel@tonic-gate (void) door_return((char *)&ret, sizeof (picl_retping_t), NULL, 0); 1827c478bd9Sstevel@tonic-gate } 1837c478bd9Sstevel@tonic-gate 184*980a6e61Sjfrank static int 185*980a6e61Sjfrank check_user(uid_t uid) 186*980a6e61Sjfrank { 187*980a6e61Sjfrank int i; 188*980a6e61Sjfrank uid_t tmp_uid; 189*980a6e61Sjfrank int free_idx = -1; 190*980a6e61Sjfrank 191*980a6e61Sjfrank if (uid == 0) 192*980a6e61Sjfrank return (PICL_SUCCESS); 193*980a6e61Sjfrank for (i = 0; i < MAX_CONCURRENT_WAITS; i++) { 194*980a6e61Sjfrank if ((tmp_uid = user_count[i].uid) == uid) { 195*980a6e61Sjfrank if (user_count[i].count == MAX_USER_WAITS) 196*980a6e61Sjfrank return (PICL_FAILURE); 197*980a6e61Sjfrank user_count[i].count++; 198*980a6e61Sjfrank return (PICL_SUCCESS); 199*980a6e61Sjfrank } 200*980a6e61Sjfrank if ((free_idx == -1) && (tmp_uid == 0)) 201*980a6e61Sjfrank free_idx = i; 202*980a6e61Sjfrank } 203*980a6e61Sjfrank if (free_idx != -1) { 204*980a6e61Sjfrank user_count[free_idx].uid = uid; 205*980a6e61Sjfrank user_count[free_idx].count = 1; 206*980a6e61Sjfrank return (PICL_SUCCESS); 207*980a6e61Sjfrank } 208*980a6e61Sjfrank return (PICL_FAILURE); 209*980a6e61Sjfrank } 210*980a6e61Sjfrank 211*980a6e61Sjfrank static void 212*980a6e61Sjfrank done_user(uid_t uid) 213*980a6e61Sjfrank { 214*980a6e61Sjfrank int i; 215*980a6e61Sjfrank 216*980a6e61Sjfrank if (uid == 0) 217*980a6e61Sjfrank return; 218*980a6e61Sjfrank for (i = 0; i < MAX_CONCURRENT_WAITS; i++) { 219*980a6e61Sjfrank if (user_count[i].uid == uid) { 220*980a6e61Sjfrank if (--user_count[i].count == 0) 221*980a6e61Sjfrank user_count[i].uid = 0; 222*980a6e61Sjfrank return; 223*980a6e61Sjfrank } 224*980a6e61Sjfrank } 225*980a6e61Sjfrank } 226*980a6e61Sjfrank 227*980a6e61Sjfrank static int 228*980a6e61Sjfrank enter_picld_wait(uid_t uid) 229*980a6e61Sjfrank { 230*980a6e61Sjfrank int rv; 231*980a6e61Sjfrank 232*980a6e61Sjfrank if (pthread_mutex_lock(&wait_req_mutex) != 0) 233*980a6e61Sjfrank return (PICL_FAILURE); 234*980a6e61Sjfrank if ((wait_count < MAX_CONCURRENT_WAITS) && 235*980a6e61Sjfrank (check_user(uid) == PICL_SUCCESS)) { 236*980a6e61Sjfrank rv = PICL_SUCCESS; 237*980a6e61Sjfrank wait_count++; 238*980a6e61Sjfrank } else { 239*980a6e61Sjfrank rv = PICL_FAILURE; 240*980a6e61Sjfrank } 241*980a6e61Sjfrank (void) pthread_mutex_unlock(&wait_req_mutex); 242*980a6e61Sjfrank return (rv); 243*980a6e61Sjfrank } 244*980a6e61Sjfrank 245*980a6e61Sjfrank static void 246*980a6e61Sjfrank exit_picld_wait(uid_t uid) 247*980a6e61Sjfrank { 248*980a6e61Sjfrank (void) pthread_mutex_lock(&wait_req_mutex); 249*980a6e61Sjfrank done_user(uid); 250*980a6e61Sjfrank wait_count--; 251*980a6e61Sjfrank (void) pthread_mutex_unlock(&wait_req_mutex); 252*980a6e61Sjfrank } 253*980a6e61Sjfrank 2547c478bd9Sstevel@tonic-gate /* 2557c478bd9Sstevel@tonic-gate * picld_wait is called when a picl_wait request is received 2567c478bd9Sstevel@tonic-gate */ 2577c478bd9Sstevel@tonic-gate static void 2587c478bd9Sstevel@tonic-gate picld_wait(picl_service_t *in) 2597c478bd9Sstevel@tonic-gate { 2607c478bd9Sstevel@tonic-gate picl_retwait_t ret; 2617c478bd9Sstevel@tonic-gate int err; 262*980a6e61Sjfrank ucred_t *puc = NULL; 263*980a6e61Sjfrank uid_t uid; 2647c478bd9Sstevel@tonic-gate 2657c478bd9Sstevel@tonic-gate ret.cnum = in->req_wait.cnum; 266*980a6e61Sjfrank if (door_ucred(&puc) != 0) 267*980a6e61Sjfrank ret.retcode = PICL_FAILURE; 268*980a6e61Sjfrank else { 269*980a6e61Sjfrank uid = ucred_geteuid(puc); 270*980a6e61Sjfrank if (enter_picld_wait(uid) == PICL_FAILURE) 271*980a6e61Sjfrank ret.retcode = PICL_FAILURE; 272*980a6e61Sjfrank else { 2737c478bd9Sstevel@tonic-gate err = xptree_refresh_notify(in->req_wait.secs); 2747c478bd9Sstevel@tonic-gate ret.retcode = err; 275*980a6e61Sjfrank exit_picld_wait(uid); 276*980a6e61Sjfrank } 277*980a6e61Sjfrank ucred_free(puc); 278*980a6e61Sjfrank } 2797c478bd9Sstevel@tonic-gate (void) rw_unlock(&init_lk); 2807c478bd9Sstevel@tonic-gate (void) door_return((char *)&ret, sizeof (picl_retwait_t), NULL, 0); 2817c478bd9Sstevel@tonic-gate } 2827c478bd9Sstevel@tonic-gate 2837c478bd9Sstevel@tonic-gate /* 2847c478bd9Sstevel@tonic-gate * This function returns the handle of the root node of the PICL tree 2857c478bd9Sstevel@tonic-gate */ 2867c478bd9Sstevel@tonic-gate static void 2877c478bd9Sstevel@tonic-gate picld_getroot(picl_service_t *in) 2887c478bd9Sstevel@tonic-gate { 2897c478bd9Sstevel@tonic-gate picl_retroot_t ret; 2907c478bd9Sstevel@tonic-gate int err; 2917c478bd9Sstevel@tonic-gate 2927c478bd9Sstevel@tonic-gate ret.cnum = PICL_CNUM_GETROOT; 2937c478bd9Sstevel@tonic-gate err = ptree_get_root(&ret.rnode); 2947c478bd9Sstevel@tonic-gate if (err != PICL_SUCCESS) 2957c478bd9Sstevel@tonic-gate picld_return_error(in->in.cnum, err); 2967c478bd9Sstevel@tonic-gate cvt_ptree2picl(&ret.rnode); 2977c478bd9Sstevel@tonic-gate (void) rw_unlock(&init_lk); 2987c478bd9Sstevel@tonic-gate (void) door_return((char *)&ret, sizeof (picl_retroot_t), NULL, 0); 2997c478bd9Sstevel@tonic-gate } 3007c478bd9Sstevel@tonic-gate 3017c478bd9Sstevel@tonic-gate /* 3027c478bd9Sstevel@tonic-gate * This function returns the value of the PICL property 3037c478bd9Sstevel@tonic-gate */ 3047c478bd9Sstevel@tonic-gate static void 3057c478bd9Sstevel@tonic-gate picld_get_attrval(picl_service_t *in) 3067c478bd9Sstevel@tonic-gate { 3077c478bd9Sstevel@tonic-gate picl_retattrval_t *ret; 3087c478bd9Sstevel@tonic-gate int err; 3097c478bd9Sstevel@tonic-gate size_t vbufsize; 3107c478bd9Sstevel@tonic-gate size_t len; 3117c478bd9Sstevel@tonic-gate door_cred_t cred; 3127c478bd9Sstevel@tonic-gate picl_prophdl_t ptreeh; 3137c478bd9Sstevel@tonic-gate ptree_propinfo_t pinfo; 3147c478bd9Sstevel@tonic-gate 3157c478bd9Sstevel@tonic-gate if (door_cred(&cred) < 0) 3167c478bd9Sstevel@tonic-gate picld_return_error(in->in.cnum, PICL_FAILURE); 3177c478bd9Sstevel@tonic-gate 3187c478bd9Sstevel@tonic-gate err = cvt_picl2ptree(in->req_attrval.attr, &ptreeh); 3197c478bd9Sstevel@tonic-gate if (err != PICL_SUCCESS) 3207c478bd9Sstevel@tonic-gate picld_return_error(in->in.cnum, err); 3217c478bd9Sstevel@tonic-gate 3227c478bd9Sstevel@tonic-gate err = ptree_get_propinfo(ptreeh, &pinfo); 3237c478bd9Sstevel@tonic-gate if (err != PICL_SUCCESS) 3247c478bd9Sstevel@tonic-gate picld_return_error(in->in.cnum, err); 3257c478bd9Sstevel@tonic-gate 3267c478bd9Sstevel@tonic-gate if (!(pinfo.piclinfo.accessmode & PICL_READ)) 3277c478bd9Sstevel@tonic-gate picld_return_error(in->in.cnum, PICL_NOTREADABLE); 3287c478bd9Sstevel@tonic-gate 3297c478bd9Sstevel@tonic-gate vbufsize = pinfo.piclinfo.size; 3307c478bd9Sstevel@tonic-gate vbufsize = MIN((size_t)in->req_attrval.bufsize, vbufsize); 3317c478bd9Sstevel@tonic-gate 3327c478bd9Sstevel@tonic-gate len = sizeof (picl_retattrval_t) + vbufsize; 3337c478bd9Sstevel@tonic-gate ret = alloca(len); 3347c478bd9Sstevel@tonic-gate if (ret == NULL) 3357c478bd9Sstevel@tonic-gate picld_return_error(in->in.cnum, PICL_FAILURE); 3367c478bd9Sstevel@tonic-gate ret->cnum = PICL_CNUM_GETATTRVAL; 3377c478bd9Sstevel@tonic-gate ret->attr = in->req_attrval.attr; 3387c478bd9Sstevel@tonic-gate ret->nbytes = (uint32_t)vbufsize; 3397c478bd9Sstevel@tonic-gate err = xptree_get_propval_with_cred(ptreeh, ret->ret_buf, vbufsize, 3407c478bd9Sstevel@tonic-gate cred); 3417c478bd9Sstevel@tonic-gate if (err != PICL_SUCCESS) 3427c478bd9Sstevel@tonic-gate picld_return_error(in->in.cnum, err); 3437c478bd9Sstevel@tonic-gate 3447c478bd9Sstevel@tonic-gate /* 3457c478bd9Sstevel@tonic-gate * adjust returned bytes for charstrings 3467c478bd9Sstevel@tonic-gate */ 3477c478bd9Sstevel@tonic-gate if (pinfo.piclinfo.type == PICL_PTYPE_CHARSTRING) 3487c478bd9Sstevel@tonic-gate ret->nbytes = (uint32_t)strlen(ret->ret_buf) + 1; 3497c478bd9Sstevel@tonic-gate 3507c478bd9Sstevel@tonic-gate /* 3517c478bd9Sstevel@tonic-gate * convert handle values to picl handles 3527c478bd9Sstevel@tonic-gate */ 3537c478bd9Sstevel@tonic-gate if ((pinfo.piclinfo.type == PICL_PTYPE_TABLE) || 3547c478bd9Sstevel@tonic-gate (pinfo.piclinfo.type == PICL_PTYPE_REFERENCE)) 3557c478bd9Sstevel@tonic-gate cvt_ptree2picl(&ret->ret_nodeh); 3567c478bd9Sstevel@tonic-gate (void) rw_unlock(&init_lk); 3577c478bd9Sstevel@tonic-gate (void) door_return((char *)ret, sizeof (picl_retattrval_t) + 3587c478bd9Sstevel@tonic-gate (size_t)ret->nbytes, NULL, 0); 3597c478bd9Sstevel@tonic-gate } 3607c478bd9Sstevel@tonic-gate 3617c478bd9Sstevel@tonic-gate /* 3627c478bd9Sstevel@tonic-gate * This function returns the value of the PICL property specified by 3637c478bd9Sstevel@tonic-gate * its name. 3647c478bd9Sstevel@tonic-gate */ 3657c478bd9Sstevel@tonic-gate static void 3667c478bd9Sstevel@tonic-gate picld_get_attrval_by_name(picl_service_t *in) 3677c478bd9Sstevel@tonic-gate { 3687c478bd9Sstevel@tonic-gate picl_retattrvalbyname_t *ret; 3697c478bd9Sstevel@tonic-gate int err; 3707c478bd9Sstevel@tonic-gate size_t vbufsize; 3717c478bd9Sstevel@tonic-gate size_t len; 3727c478bd9Sstevel@tonic-gate door_cred_t cred; 3737c478bd9Sstevel@tonic-gate picl_nodehdl_t ptreeh; 3747c478bd9Sstevel@tonic-gate ptree_propinfo_t pinfo; 3757c478bd9Sstevel@tonic-gate 3767c478bd9Sstevel@tonic-gate if (door_cred(&cred) < 0) 3777c478bd9Sstevel@tonic-gate picld_return_error(in->in.cnum, PICL_FAILURE); 3787c478bd9Sstevel@tonic-gate 3797c478bd9Sstevel@tonic-gate err = cvt_picl2ptree(in->req_attrvalbyname.nodeh, &ptreeh); 3807c478bd9Sstevel@tonic-gate if (err != PICL_SUCCESS) 3817c478bd9Sstevel@tonic-gate picld_return_error(in->in.cnum, err); 3827c478bd9Sstevel@tonic-gate 3837c478bd9Sstevel@tonic-gate err = xptree_get_propinfo_by_name(ptreeh, 3847c478bd9Sstevel@tonic-gate in->req_attrvalbyname.propname, &pinfo); 3857c478bd9Sstevel@tonic-gate if (err != PICL_SUCCESS) 3867c478bd9Sstevel@tonic-gate picld_return_error(in->in.cnum, err); 3877c478bd9Sstevel@tonic-gate 3887c478bd9Sstevel@tonic-gate if (!(pinfo.piclinfo.accessmode & PICL_READ)) 3897c478bd9Sstevel@tonic-gate picld_return_error(in->in.cnum, PICL_NOTREADABLE); 3907c478bd9Sstevel@tonic-gate 3917c478bd9Sstevel@tonic-gate /* 3927c478bd9Sstevel@tonic-gate * allocate the minimum of piclinfo.size and input bufsize 3937c478bd9Sstevel@tonic-gate */ 3947c478bd9Sstevel@tonic-gate vbufsize = pinfo.piclinfo.size; 3957c478bd9Sstevel@tonic-gate vbufsize = MIN((size_t)in->req_attrvalbyname.bufsize, vbufsize); 3967c478bd9Sstevel@tonic-gate len = sizeof (picl_retattrvalbyname_t) + vbufsize; 3977c478bd9Sstevel@tonic-gate ret = alloca(len); 3987c478bd9Sstevel@tonic-gate if (ret == NULL) 3997c478bd9Sstevel@tonic-gate picld_return_error(in->in.cnum, PICL_FAILURE); 4007c478bd9Sstevel@tonic-gate ret->cnum = PICL_CNUM_GETATTRVALBYNAME; 4017c478bd9Sstevel@tonic-gate ret->nodeh = in->req_attrvalbyname.nodeh; 4027c478bd9Sstevel@tonic-gate (void) strcpy(ret->propname, in->req_attrvalbyname.propname); 4037c478bd9Sstevel@tonic-gate ret->nbytes = (uint32_t)vbufsize; 4047c478bd9Sstevel@tonic-gate 4057c478bd9Sstevel@tonic-gate err = xptree_get_propval_by_name_with_cred(ptreeh, 4067c478bd9Sstevel@tonic-gate in->req_attrvalbyname.propname, ret->ret_buf, vbufsize, 4077c478bd9Sstevel@tonic-gate cred); 4087c478bd9Sstevel@tonic-gate if (err != PICL_SUCCESS) 4097c478bd9Sstevel@tonic-gate picld_return_error(in->in.cnum, err); 4107c478bd9Sstevel@tonic-gate /* 4117c478bd9Sstevel@tonic-gate * adjust returned value size for charstrings 4127c478bd9Sstevel@tonic-gate */ 4137c478bd9Sstevel@tonic-gate if (pinfo.piclinfo.type == PICL_PTYPE_CHARSTRING) 4147c478bd9Sstevel@tonic-gate ret->nbytes = (uint32_t)strlen(ret->ret_buf) + 1; 4157c478bd9Sstevel@tonic-gate 4167c478bd9Sstevel@tonic-gate if ((pinfo.piclinfo.type == PICL_PTYPE_TABLE) || 4177c478bd9Sstevel@tonic-gate (pinfo.piclinfo.type == PICL_PTYPE_REFERENCE)) 4187c478bd9Sstevel@tonic-gate cvt_ptree2picl(&ret->ret_nodeh); 4197c478bd9Sstevel@tonic-gate 4207c478bd9Sstevel@tonic-gate (void) rw_unlock(&init_lk); 4217c478bd9Sstevel@tonic-gate (void) door_return((char *)ret, sizeof (picl_retattrvalbyname_t) + 4227c478bd9Sstevel@tonic-gate (size_t)ret->nbytes, NULL, 0); 4237c478bd9Sstevel@tonic-gate } 4247c478bd9Sstevel@tonic-gate 4257c478bd9Sstevel@tonic-gate /* 4267c478bd9Sstevel@tonic-gate * This function sets a property value 4277c478bd9Sstevel@tonic-gate */ 4287c478bd9Sstevel@tonic-gate static void 4297c478bd9Sstevel@tonic-gate picld_set_attrval(picl_service_t *in) 4307c478bd9Sstevel@tonic-gate { 4317c478bd9Sstevel@tonic-gate picl_retsetattrval_t ret; 4327c478bd9Sstevel@tonic-gate int err; 4337c478bd9Sstevel@tonic-gate door_cred_t cred; 4347c478bd9Sstevel@tonic-gate picl_prophdl_t ptreeh; 4357c478bd9Sstevel@tonic-gate ptree_propinfo_t pinfo; 4367c478bd9Sstevel@tonic-gate 4377c478bd9Sstevel@tonic-gate if (door_cred(&cred) < 0) 4387c478bd9Sstevel@tonic-gate picld_return_error(in->in.cnum, PICL_FAILURE); 4397c478bd9Sstevel@tonic-gate 4407c478bd9Sstevel@tonic-gate err = cvt_picl2ptree(in->req_setattrval.attr, &ptreeh); 4417c478bd9Sstevel@tonic-gate if (err != PICL_SUCCESS) 4427c478bd9Sstevel@tonic-gate picld_return_error(in->in.cnum, err); 4437c478bd9Sstevel@tonic-gate 4447c478bd9Sstevel@tonic-gate err = ptree_get_propinfo(ptreeh, &pinfo); 4457c478bd9Sstevel@tonic-gate if (err != PICL_SUCCESS) 4467c478bd9Sstevel@tonic-gate picld_return_error(in->in.cnum, err); 4477c478bd9Sstevel@tonic-gate 4487c478bd9Sstevel@tonic-gate if (!(pinfo.piclinfo.accessmode & PICL_WRITE)) 4497c478bd9Sstevel@tonic-gate picld_return_error(in->in.cnum, PICL_NOTWRITABLE); 4507c478bd9Sstevel@tonic-gate /* 4517c478bd9Sstevel@tonic-gate * For non-volatile prop, only super user can set its value. 4527c478bd9Sstevel@tonic-gate */ 4537c478bd9Sstevel@tonic-gate if (!(pinfo.piclinfo.accessmode & PICL_VOLATILE) && 4547c478bd9Sstevel@tonic-gate (cred.dc_euid != SUPER_USER)) 4557c478bd9Sstevel@tonic-gate picld_return_error(in->in.cnum, PICL_PERMDENIED); 4567c478bd9Sstevel@tonic-gate 4577c478bd9Sstevel@tonic-gate ret.cnum = PICL_CNUM_SETATTRVAL; 4587c478bd9Sstevel@tonic-gate ret.attr = in->req_setattrval.attr; 4597c478bd9Sstevel@tonic-gate 4607c478bd9Sstevel@tonic-gate err = xptree_update_propval_with_cred(ptreeh, in->req_setattrval.valbuf, 4617c478bd9Sstevel@tonic-gate (size_t)in->req_setattrval.bufsize, cred); 4627c478bd9Sstevel@tonic-gate 4637c478bd9Sstevel@tonic-gate if (err != PICL_SUCCESS) 4647c478bd9Sstevel@tonic-gate picld_return_error(in->in.cnum, err); 4657c478bd9Sstevel@tonic-gate 4667c478bd9Sstevel@tonic-gate (void) rw_unlock(&init_lk); 4677c478bd9Sstevel@tonic-gate (void) door_return((char *)&ret, sizeof (picl_retsetattrval_t), NULL, 4687c478bd9Sstevel@tonic-gate 0); 4697c478bd9Sstevel@tonic-gate } 4707c478bd9Sstevel@tonic-gate 4717c478bd9Sstevel@tonic-gate /* 4727c478bd9Sstevel@tonic-gate * This function sets the value of a property specified by its name. 4737c478bd9Sstevel@tonic-gate */ 4747c478bd9Sstevel@tonic-gate static void 4757c478bd9Sstevel@tonic-gate picld_set_attrval_by_name(picl_service_t *in) 4767c478bd9Sstevel@tonic-gate { 4777c478bd9Sstevel@tonic-gate picl_retsetattrvalbyname_t ret; 4787c478bd9Sstevel@tonic-gate int err; 4797c478bd9Sstevel@tonic-gate door_cred_t cred; 4807c478bd9Sstevel@tonic-gate picl_prophdl_t ptreeh; 4817c478bd9Sstevel@tonic-gate ptree_propinfo_t pinfo; 4827c478bd9Sstevel@tonic-gate 4837c478bd9Sstevel@tonic-gate if (door_cred(&cred) < 0) 4847c478bd9Sstevel@tonic-gate picld_return_error(in->in.cnum, PICL_FAILURE); 4857c478bd9Sstevel@tonic-gate 4867c478bd9Sstevel@tonic-gate err = cvt_picl2ptree(in->req_setattrvalbyname.nodeh, &ptreeh); 4877c478bd9Sstevel@tonic-gate if (err != PICL_SUCCESS) 4887c478bd9Sstevel@tonic-gate picld_return_error(in->in.cnum, err); 4897c478bd9Sstevel@tonic-gate 4907c478bd9Sstevel@tonic-gate err = xptree_get_propinfo_by_name(ptreeh, 4917c478bd9Sstevel@tonic-gate in->req_setattrvalbyname.propname, &pinfo); 4927c478bd9Sstevel@tonic-gate if (err != PICL_SUCCESS) 4937c478bd9Sstevel@tonic-gate picld_return_error(in->in.cnum, err); 4947c478bd9Sstevel@tonic-gate 4957c478bd9Sstevel@tonic-gate if (!(pinfo.piclinfo.accessmode & PICL_WRITE)) 4967c478bd9Sstevel@tonic-gate picld_return_error(in->in.cnum, PICL_NOTWRITABLE); 4977c478bd9Sstevel@tonic-gate 4987c478bd9Sstevel@tonic-gate /* 4997c478bd9Sstevel@tonic-gate * For non-volatile prop, only super user can set its value. 5007c478bd9Sstevel@tonic-gate */ 5017c478bd9Sstevel@tonic-gate if (!(pinfo.piclinfo.accessmode & PICL_VOLATILE) && 5027c478bd9Sstevel@tonic-gate (cred.dc_euid != SUPER_USER)) 5037c478bd9Sstevel@tonic-gate picld_return_error(in->in.cnum, PICL_PERMDENIED); 5047c478bd9Sstevel@tonic-gate 5057c478bd9Sstevel@tonic-gate ret.cnum = PICL_CNUM_SETATTRVALBYNAME; 5067c478bd9Sstevel@tonic-gate ret.nodeh = in->req_setattrvalbyname.nodeh; 5077c478bd9Sstevel@tonic-gate (void) strcpy(ret.propname, in->req_setattrvalbyname.propname); 5087c478bd9Sstevel@tonic-gate 5097c478bd9Sstevel@tonic-gate err = xptree_update_propval_by_name_with_cred(ptreeh, 5107c478bd9Sstevel@tonic-gate in->req_setattrvalbyname.propname, 5117c478bd9Sstevel@tonic-gate in->req_setattrvalbyname.valbuf, 5127c478bd9Sstevel@tonic-gate (size_t)in->req_setattrvalbyname.bufsize, 5137c478bd9Sstevel@tonic-gate cred); 5147c478bd9Sstevel@tonic-gate 5157c478bd9Sstevel@tonic-gate if (err != PICL_SUCCESS) 5167c478bd9Sstevel@tonic-gate picld_return_error(in->in.cnum, err); 5177c478bd9Sstevel@tonic-gate 5187c478bd9Sstevel@tonic-gate (void) rw_unlock(&init_lk); 5197c478bd9Sstevel@tonic-gate (void) door_return((char *)&ret, sizeof (picl_retsetattrvalbyname_t), 5207c478bd9Sstevel@tonic-gate NULL, 0); 5217c478bd9Sstevel@tonic-gate } 5227c478bd9Sstevel@tonic-gate 5237c478bd9Sstevel@tonic-gate /* 5247c478bd9Sstevel@tonic-gate * This function returns the property information 5257c478bd9Sstevel@tonic-gate */ 5267c478bd9Sstevel@tonic-gate static void 5277c478bd9Sstevel@tonic-gate picld_get_attrinfo(picl_service_t *in) 5287c478bd9Sstevel@tonic-gate { 5297c478bd9Sstevel@tonic-gate picl_retattrinfo_t ret; 5307c478bd9Sstevel@tonic-gate int err; 5317c478bd9Sstevel@tonic-gate ptree_propinfo_t pinfo; 5327c478bd9Sstevel@tonic-gate picl_prophdl_t ptreeh; 5337c478bd9Sstevel@tonic-gate 5347c478bd9Sstevel@tonic-gate err = cvt_picl2ptree(in->req_attrinfo.attr, &ptreeh); 5357c478bd9Sstevel@tonic-gate if (err != PICL_SUCCESS) 5367c478bd9Sstevel@tonic-gate picld_return_error(in->in.cnum, err); 5377c478bd9Sstevel@tonic-gate 5387c478bd9Sstevel@tonic-gate ret.cnum = PICL_CNUM_GETATTRINFO; 5397c478bd9Sstevel@tonic-gate ret.attr = in->req_attrinfo.attr; 5407c478bd9Sstevel@tonic-gate 5417c478bd9Sstevel@tonic-gate err = ptree_get_propinfo(ptreeh, &pinfo); 5427c478bd9Sstevel@tonic-gate if (err != PICL_SUCCESS) 5437c478bd9Sstevel@tonic-gate picld_return_error(in->in.cnum, err); 5447c478bd9Sstevel@tonic-gate 5457c478bd9Sstevel@tonic-gate ret.type = pinfo.piclinfo.type; 5467c478bd9Sstevel@tonic-gate ret.accessmode = pinfo.piclinfo.accessmode; 5477c478bd9Sstevel@tonic-gate ret.size = (uint32_t)pinfo.piclinfo.size; 5487c478bd9Sstevel@tonic-gate (void) strcpy(ret.name, pinfo.piclinfo.name); 5497c478bd9Sstevel@tonic-gate (void) rw_unlock(&init_lk); 5507c478bd9Sstevel@tonic-gate (void) door_return((char *)&ret, sizeof (picl_retattrinfo_t), NULL, 0); 5517c478bd9Sstevel@tonic-gate } 5527c478bd9Sstevel@tonic-gate 5537c478bd9Sstevel@tonic-gate /* 5547c478bd9Sstevel@tonic-gate * This function returns the node's first property handle 5557c478bd9Sstevel@tonic-gate */ 5567c478bd9Sstevel@tonic-gate static void 5577c478bd9Sstevel@tonic-gate picld_get_first_attr(picl_service_t *in) 5587c478bd9Sstevel@tonic-gate { 5597c478bd9Sstevel@tonic-gate picl_retfirstattr_t ret; 5607c478bd9Sstevel@tonic-gate int err; 5617c478bd9Sstevel@tonic-gate picl_prophdl_t ptreeh; 5627c478bd9Sstevel@tonic-gate 5637c478bd9Sstevel@tonic-gate err = cvt_picl2ptree(in->req_firstattr.nodeh, &ptreeh); 5647c478bd9Sstevel@tonic-gate if (err != PICL_SUCCESS) 5657c478bd9Sstevel@tonic-gate picld_return_error(in->in.cnum, err); 5667c478bd9Sstevel@tonic-gate 5677c478bd9Sstevel@tonic-gate ret.cnum = PICL_CNUM_GETFIRSTATTR; 5687c478bd9Sstevel@tonic-gate ret.nodeh = in->req_firstattr.nodeh; 5697c478bd9Sstevel@tonic-gate 5707c478bd9Sstevel@tonic-gate err = ptree_get_first_prop(ptreeh, &ret.attr); 5717c478bd9Sstevel@tonic-gate if (err != PICL_SUCCESS) 5727c478bd9Sstevel@tonic-gate picld_return_error(in->in.cnum, err); 5737c478bd9Sstevel@tonic-gate cvt_ptree2picl(&ret.attr); 5747c478bd9Sstevel@tonic-gate (void) rw_unlock(&init_lk); 5757c478bd9Sstevel@tonic-gate (void) door_return((char *)&ret, sizeof (picl_retfirstattr_t), NULL, 0); 5767c478bd9Sstevel@tonic-gate } 5777c478bd9Sstevel@tonic-gate 5787c478bd9Sstevel@tonic-gate /* 5797c478bd9Sstevel@tonic-gate * This function returns the next property handle in list 5807c478bd9Sstevel@tonic-gate */ 5817c478bd9Sstevel@tonic-gate static void 5827c478bd9Sstevel@tonic-gate picld_get_next_attr(picl_service_t *in) 5837c478bd9Sstevel@tonic-gate { 5847c478bd9Sstevel@tonic-gate picl_retnextattr_t ret; 5857c478bd9Sstevel@tonic-gate int err; 5867c478bd9Sstevel@tonic-gate picl_prophdl_t ptreeh; 5877c478bd9Sstevel@tonic-gate 5887c478bd9Sstevel@tonic-gate err = cvt_picl2ptree(in->req_nextattr.attr, &ptreeh); 5897c478bd9Sstevel@tonic-gate if (err != PICL_SUCCESS) 5907c478bd9Sstevel@tonic-gate picld_return_error(in->in.cnum, err); 5917c478bd9Sstevel@tonic-gate 5927c478bd9Sstevel@tonic-gate ret.cnum = PICL_CNUM_GETNEXTATTR; 5937c478bd9Sstevel@tonic-gate ret.attr = in->req_nextattr.attr; 5947c478bd9Sstevel@tonic-gate 5957c478bd9Sstevel@tonic-gate err = ptree_get_next_prop(ptreeh, &ret.nextattr); 5967c478bd9Sstevel@tonic-gate if (err != PICL_SUCCESS) 5977c478bd9Sstevel@tonic-gate picld_return_error(in->in.cnum, err); 5987c478bd9Sstevel@tonic-gate 5997c478bd9Sstevel@tonic-gate cvt_ptree2picl(&ret.nextattr); 6007c478bd9Sstevel@tonic-gate 6017c478bd9Sstevel@tonic-gate (void) rw_unlock(&init_lk); 6027c478bd9Sstevel@tonic-gate (void) door_return((char *)&ret, sizeof (picl_retnextattr_t), NULL, 0); 6037c478bd9Sstevel@tonic-gate } 6047c478bd9Sstevel@tonic-gate 6057c478bd9Sstevel@tonic-gate /* 6067c478bd9Sstevel@tonic-gate * This function returns the handle of a property specified by its name 6077c478bd9Sstevel@tonic-gate */ 6087c478bd9Sstevel@tonic-gate static void 6097c478bd9Sstevel@tonic-gate picld_get_attr_by_name(picl_service_t *in) 6107c478bd9Sstevel@tonic-gate { 6117c478bd9Sstevel@tonic-gate picl_retattrbyname_t ret; 6127c478bd9Sstevel@tonic-gate int err; 6137c478bd9Sstevel@tonic-gate picl_prophdl_t ptreeh; 6147c478bd9Sstevel@tonic-gate 6157c478bd9Sstevel@tonic-gate err = cvt_picl2ptree(in->req_attrbyname.nodeh, &ptreeh); 6167c478bd9Sstevel@tonic-gate if (err != PICL_SUCCESS) 6177c478bd9Sstevel@tonic-gate picld_return_error(in->in.cnum, err); 6187c478bd9Sstevel@tonic-gate 6197c478bd9Sstevel@tonic-gate ret.cnum = PICL_CNUM_GETATTRBYNAME; 6207c478bd9Sstevel@tonic-gate ret.nodeh = in->req_attrbyname.nodeh; 6217c478bd9Sstevel@tonic-gate (void) strcpy(ret.propname, in->req_attrbyname.propname); 6227c478bd9Sstevel@tonic-gate 6237c478bd9Sstevel@tonic-gate err = ptree_get_prop_by_name(ptreeh, ret.propname, &ret.attr); 6247c478bd9Sstevel@tonic-gate if (err != PICL_SUCCESS) 6257c478bd9Sstevel@tonic-gate picld_return_error(in->in.cnum, err); 6267c478bd9Sstevel@tonic-gate 6277c478bd9Sstevel@tonic-gate cvt_ptree2picl(&ret.attr); 6287c478bd9Sstevel@tonic-gate (void) rw_unlock(&init_lk); 6297c478bd9Sstevel@tonic-gate (void) door_return((char *)&ret, sizeof (picl_retattrbyname_t), NULL, 6307c478bd9Sstevel@tonic-gate 0); 6317c478bd9Sstevel@tonic-gate } 6327c478bd9Sstevel@tonic-gate 6337c478bd9Sstevel@tonic-gate /* 6347c478bd9Sstevel@tonic-gate * This function gets the next property on the same row in the table 6357c478bd9Sstevel@tonic-gate */ 6367c478bd9Sstevel@tonic-gate static void 6377c478bd9Sstevel@tonic-gate picld_get_attr_by_row(picl_service_t *in) 6387c478bd9Sstevel@tonic-gate { 6397c478bd9Sstevel@tonic-gate picl_retattrbyrow_t ret; 6407c478bd9Sstevel@tonic-gate int err; 6417c478bd9Sstevel@tonic-gate picl_prophdl_t ptreeh; 6427c478bd9Sstevel@tonic-gate 6437c478bd9Sstevel@tonic-gate err = cvt_picl2ptree(in->req_attrbyrow.attr, &ptreeh); 6447c478bd9Sstevel@tonic-gate if (err != PICL_SUCCESS) 6457c478bd9Sstevel@tonic-gate picld_return_error(in->in.cnum, err); 6467c478bd9Sstevel@tonic-gate 6477c478bd9Sstevel@tonic-gate ret.cnum = PICL_CNUM_GETATTRBYROW; 6487c478bd9Sstevel@tonic-gate ret.attr = in->req_attrbyrow.attr; 6497c478bd9Sstevel@tonic-gate 6507c478bd9Sstevel@tonic-gate err = ptree_get_next_by_row(ptreeh, &ret.rowattr); 6517c478bd9Sstevel@tonic-gate if (err != PICL_SUCCESS) 6527c478bd9Sstevel@tonic-gate picld_return_error(in->in.cnum, err); 6537c478bd9Sstevel@tonic-gate cvt_ptree2picl(&ret.rowattr); 6547c478bd9Sstevel@tonic-gate 6557c478bd9Sstevel@tonic-gate (void) rw_unlock(&init_lk); 6567c478bd9Sstevel@tonic-gate (void) door_return((char *)&ret, sizeof (picl_retattrbyrow_t), NULL, 0); 6577c478bd9Sstevel@tonic-gate } 6587c478bd9Sstevel@tonic-gate 6597c478bd9Sstevel@tonic-gate /* 6607c478bd9Sstevel@tonic-gate * This function returns the handle of the next property in the same column 6617c478bd9Sstevel@tonic-gate * of the table. 6627c478bd9Sstevel@tonic-gate */ 6637c478bd9Sstevel@tonic-gate static void 6647c478bd9Sstevel@tonic-gate picld_get_attr_by_col(picl_service_t *in) 6657c478bd9Sstevel@tonic-gate { 6667c478bd9Sstevel@tonic-gate picl_retattrbycol_t ret; 6677c478bd9Sstevel@tonic-gate int err; 6687c478bd9Sstevel@tonic-gate picl_prophdl_t ptreeh; 6697c478bd9Sstevel@tonic-gate 6707c478bd9Sstevel@tonic-gate err = cvt_picl2ptree(in->req_attrbycol.attr, &ptreeh); 6717c478bd9Sstevel@tonic-gate if (err != PICL_SUCCESS) 6727c478bd9Sstevel@tonic-gate picld_return_error(in->in.cnum, err); 6737c478bd9Sstevel@tonic-gate 6747c478bd9Sstevel@tonic-gate ret.cnum = PICL_CNUM_GETATTRBYCOL; 6757c478bd9Sstevel@tonic-gate ret.attr = in->req_attrbycol.attr; 6767c478bd9Sstevel@tonic-gate 6777c478bd9Sstevel@tonic-gate err = ptree_get_next_by_col(ptreeh, &ret.colattr); 6787c478bd9Sstevel@tonic-gate if (err != PICL_SUCCESS) 6797c478bd9Sstevel@tonic-gate picld_return_error(in->in.cnum, err); 6807c478bd9Sstevel@tonic-gate 6817c478bd9Sstevel@tonic-gate cvt_ptree2picl(&ret.colattr); 6827c478bd9Sstevel@tonic-gate 6837c478bd9Sstevel@tonic-gate (void) rw_unlock(&init_lk); 6847c478bd9Sstevel@tonic-gate (void) door_return((char *)&ret, sizeof (picl_retattrbycol_t), NULL, 0); 6857c478bd9Sstevel@tonic-gate } 6867c478bd9Sstevel@tonic-gate 6877c478bd9Sstevel@tonic-gate /* 6887c478bd9Sstevel@tonic-gate * This function finds the node in the PICLTREE that matches the given 6897c478bd9Sstevel@tonic-gate * criteria and returns its handle. 6907c478bd9Sstevel@tonic-gate */ 6917c478bd9Sstevel@tonic-gate static void 6927c478bd9Sstevel@tonic-gate picld_find_node(picl_service_t *in) 6937c478bd9Sstevel@tonic-gate { 6947c478bd9Sstevel@tonic-gate picl_retfindnode_t ret; 6957c478bd9Sstevel@tonic-gate int err; 6967c478bd9Sstevel@tonic-gate picl_nodehdl_t ptreeh; 6977c478bd9Sstevel@tonic-gate 6987c478bd9Sstevel@tonic-gate err = cvt_picl2ptree(in->req_findnode.nodeh, &ptreeh); 6997c478bd9Sstevel@tonic-gate if (err != PICL_SUCCESS) 7007c478bd9Sstevel@tonic-gate picld_return_error(in->in.cnum, err); 7017c478bd9Sstevel@tonic-gate 7027c478bd9Sstevel@tonic-gate ret.cnum = PICL_CNUM_FINDNODE; 7037c478bd9Sstevel@tonic-gate 7047c478bd9Sstevel@tonic-gate err = ptree_find_node(ptreeh, in->req_findnode.propname, 7057c478bd9Sstevel@tonic-gate in->req_findnode.ptype, in->req_findnode.valbuf, 7067c478bd9Sstevel@tonic-gate in->req_findnode.valsize, &ret.rnodeh); 7077c478bd9Sstevel@tonic-gate if (err != PICL_SUCCESS) 7087c478bd9Sstevel@tonic-gate picld_return_error(in->in.cnum, err); 7097c478bd9Sstevel@tonic-gate 7107c478bd9Sstevel@tonic-gate cvt_ptree2picl(&ret.rnodeh); 7117c478bd9Sstevel@tonic-gate 7127c478bd9Sstevel@tonic-gate (void) rw_unlock(&init_lk); 7137c478bd9Sstevel@tonic-gate (void) door_return((char *)&ret, sizeof (ret), NULL, 0); 7147c478bd9Sstevel@tonic-gate } 7157c478bd9Sstevel@tonic-gate 7167c478bd9Sstevel@tonic-gate /* 7177c478bd9Sstevel@tonic-gate * This function finds the property/node that corresponds to the given path 7187c478bd9Sstevel@tonic-gate * and returns its handle 7197c478bd9Sstevel@tonic-gate */ 7207c478bd9Sstevel@tonic-gate static void 7217c478bd9Sstevel@tonic-gate picld_get_node_by_path(picl_service_t *in) 7227c478bd9Sstevel@tonic-gate { 7237c478bd9Sstevel@tonic-gate picl_retnodebypath_t ret; 7247c478bd9Sstevel@tonic-gate int err; 7257c478bd9Sstevel@tonic-gate 7267c478bd9Sstevel@tonic-gate ret.cnum = PICL_CNUM_NODEBYPATH; 7277c478bd9Sstevel@tonic-gate err = ptree_get_node_by_path(in->req_nodebypath.pathbuf, &ret.nodeh); 7287c478bd9Sstevel@tonic-gate if (err != PICL_SUCCESS) 7297c478bd9Sstevel@tonic-gate picld_return_error(in->in.cnum, err); 7307c478bd9Sstevel@tonic-gate cvt_ptree2picl(&ret.nodeh); 7317c478bd9Sstevel@tonic-gate (void) rw_unlock(&init_lk); 7327c478bd9Sstevel@tonic-gate (void) door_return((char *)&ret, sizeof (ret), NULL, 0); 7337c478bd9Sstevel@tonic-gate } 7347c478bd9Sstevel@tonic-gate 7357c478bd9Sstevel@tonic-gate /* 7367c478bd9Sstevel@tonic-gate * This function returns finds the frutree parent node for a given node 7377c478bd9Sstevel@tonic-gate * and returns its handle 7387c478bd9Sstevel@tonic-gate */ 7397c478bd9Sstevel@tonic-gate static void 7407c478bd9Sstevel@tonic-gate picld_get_frutree_parent(picl_service_t *in) 7417c478bd9Sstevel@tonic-gate { 7427c478bd9Sstevel@tonic-gate picl_retfruparent_t ret; 7437c478bd9Sstevel@tonic-gate int err; 7447c478bd9Sstevel@tonic-gate picl_nodehdl_t ptreeh; 7457c478bd9Sstevel@tonic-gate 7467c478bd9Sstevel@tonic-gate err = cvt_picl2ptree(in->req_fruparent.devh, &ptreeh); 7477c478bd9Sstevel@tonic-gate if (err != PICL_SUCCESS) 7487c478bd9Sstevel@tonic-gate picld_return_error(in->in.cnum, err); 7497c478bd9Sstevel@tonic-gate 7507c478bd9Sstevel@tonic-gate ret.cnum = PICL_CNUM_FRUTREEPARENT; 7517c478bd9Sstevel@tonic-gate 7527c478bd9Sstevel@tonic-gate err = ptree_get_frutree_parent(ptreeh, &ret.fruh); 7537c478bd9Sstevel@tonic-gate if (err != PICL_SUCCESS) 7547c478bd9Sstevel@tonic-gate picld_return_error(in->in.cnum, err); 7557c478bd9Sstevel@tonic-gate cvt_ptree2picl(&ret.fruh); 7567c478bd9Sstevel@tonic-gate 7577c478bd9Sstevel@tonic-gate (void) rw_unlock(&init_lk); 7587c478bd9Sstevel@tonic-gate (void) door_return((char *)&ret, sizeof (ret), NULL, 0); 7597c478bd9Sstevel@tonic-gate } 7607c478bd9Sstevel@tonic-gate 7617c478bd9Sstevel@tonic-gate /* 7627c478bd9Sstevel@tonic-gate * This function is called when an unknown client request is received. 7637c478bd9Sstevel@tonic-gate */ 7647c478bd9Sstevel@tonic-gate static void 7657c478bd9Sstevel@tonic-gate picld_unknown_service(picl_service_t *in) 7667c478bd9Sstevel@tonic-gate { 7677c478bd9Sstevel@tonic-gate picld_return_error(in->in.cnum, PICL_UNKNOWNSERVICE); 7687c478bd9Sstevel@tonic-gate } 7697c478bd9Sstevel@tonic-gate 7707c478bd9Sstevel@tonic-gate static void 7717c478bd9Sstevel@tonic-gate check_denial_of_service(int cnum) 7727c478bd9Sstevel@tonic-gate { 7737c478bd9Sstevel@tonic-gate hrtime_t window; 7747c478bd9Sstevel@tonic-gate hrtime_t current; 7757c478bd9Sstevel@tonic-gate int dos_flag; 7767c478bd9Sstevel@tonic-gate 7777c478bd9Sstevel@tonic-gate current = gethrtime(); 7787c478bd9Sstevel@tonic-gate dos_flag = 0; 7797c478bd9Sstevel@tonic-gate 7807c478bd9Sstevel@tonic-gate if (pthread_mutex_lock(&dos_mutex) != 0) 7817c478bd9Sstevel@tonic-gate picld_return_error(cnum, PICL_FAILURE); 7827c478bd9Sstevel@tonic-gate 7837c478bd9Sstevel@tonic-gate ++service_requests; 7847c478bd9Sstevel@tonic-gate window = current - orig_time; 7857c478bd9Sstevel@tonic-gate if (window > MILLI_TO_NANO(sliding_interval_ms)) { 7867c478bd9Sstevel@tonic-gate orig_time = current; 7877c478bd9Sstevel@tonic-gate service_requests = 1; 7887c478bd9Sstevel@tonic-gate } 7897c478bd9Sstevel@tonic-gate 7907c478bd9Sstevel@tonic-gate if (service_requests > dos_req_limit) 7917c478bd9Sstevel@tonic-gate dos_flag = 1; 7927c478bd9Sstevel@tonic-gate 7937c478bd9Sstevel@tonic-gate if (pthread_mutex_unlock(&dos_mutex) != 0) 7947c478bd9Sstevel@tonic-gate picld_return_error(cnum, PICL_FAILURE); 7957c478bd9Sstevel@tonic-gate 7967c478bd9Sstevel@tonic-gate if (dos_flag) 7977c478bd9Sstevel@tonic-gate (void) poll(NULL, 0, dos_ms); 7987c478bd9Sstevel@tonic-gate } 7997c478bd9Sstevel@tonic-gate 8007c478bd9Sstevel@tonic-gate /* ARGSUSED */ 8017c478bd9Sstevel@tonic-gate static void 8027c478bd9Sstevel@tonic-gate picld_door_handler(void *cookie, char *argp, size_t asize, 8037c478bd9Sstevel@tonic-gate door_desc_t *dp, uint_t n_desc) 8047c478bd9Sstevel@tonic-gate { 8057c478bd9Sstevel@tonic-gate picl_service_t *req; 8067c478bd9Sstevel@tonic-gate 8077c478bd9Sstevel@tonic-gate /*LINTED*/ 8087c478bd9Sstevel@tonic-gate req = (picl_service_t *)argp; 8097c478bd9Sstevel@tonic-gate 8107c478bd9Sstevel@tonic-gate if (req == NULL) 8117c478bd9Sstevel@tonic-gate (void) door_return((char *)req, 0, NULL, 0); 8127c478bd9Sstevel@tonic-gate 8137c478bd9Sstevel@tonic-gate check_denial_of_service(req->in.cnum); 8147c478bd9Sstevel@tonic-gate 8157c478bd9Sstevel@tonic-gate (void) rw_rdlock(&init_lk); 8167c478bd9Sstevel@tonic-gate switch (req->in.cnum) { /* client call number */ 8177c478bd9Sstevel@tonic-gate case PICL_CNUM_INIT: 8187c478bd9Sstevel@tonic-gate /*LINTED*/ 8197c478bd9Sstevel@tonic-gate picld_init((picl_service_t *)argp); 8207c478bd9Sstevel@tonic-gate break; 8217c478bd9Sstevel@tonic-gate case PICL_CNUM_FINI: 8227c478bd9Sstevel@tonic-gate /*LINTED*/ 8237c478bd9Sstevel@tonic-gate picld_fini((picl_service_t *)argp); 8247c478bd9Sstevel@tonic-gate break; 8257c478bd9Sstevel@tonic-gate case PICL_CNUM_GETROOT: 8267c478bd9Sstevel@tonic-gate /*LINTED*/ 8277c478bd9Sstevel@tonic-gate picld_getroot((picl_service_t *)argp); 8287c478bd9Sstevel@tonic-gate break; 8297c478bd9Sstevel@tonic-gate case PICL_CNUM_GETATTRVAL: 8307c478bd9Sstevel@tonic-gate /*LINTED*/ 8317c478bd9Sstevel@tonic-gate picld_get_attrval((picl_service_t *)argp); 8327c478bd9Sstevel@tonic-gate break; 8337c478bd9Sstevel@tonic-gate case PICL_CNUM_GETATTRVALBYNAME: 8347c478bd9Sstevel@tonic-gate /*LINTED*/ 8357c478bd9Sstevel@tonic-gate picld_get_attrval_by_name((picl_service_t *)argp); 8367c478bd9Sstevel@tonic-gate break; 8377c478bd9Sstevel@tonic-gate case PICL_CNUM_GETATTRINFO: 8387c478bd9Sstevel@tonic-gate /*LINTED*/ 8397c478bd9Sstevel@tonic-gate picld_get_attrinfo((picl_service_t *)argp); 8407c478bd9Sstevel@tonic-gate break; 8417c478bd9Sstevel@tonic-gate case PICL_CNUM_GETFIRSTATTR: 8427c478bd9Sstevel@tonic-gate /*LINTED*/ 8437c478bd9Sstevel@tonic-gate picld_get_first_attr((picl_service_t *)argp); 8447c478bd9Sstevel@tonic-gate break; 8457c478bd9Sstevel@tonic-gate case PICL_CNUM_GETNEXTATTR: 8467c478bd9Sstevel@tonic-gate /*LINTED*/ 8477c478bd9Sstevel@tonic-gate picld_get_next_attr((picl_service_t *)argp); 8487c478bd9Sstevel@tonic-gate break; 8497c478bd9Sstevel@tonic-gate case PICL_CNUM_GETATTRBYNAME: 8507c478bd9Sstevel@tonic-gate /*LINTED*/ 8517c478bd9Sstevel@tonic-gate picld_get_attr_by_name((picl_service_t *)argp); 8527c478bd9Sstevel@tonic-gate break; 8537c478bd9Sstevel@tonic-gate case PICL_CNUM_GETATTRBYROW: 8547c478bd9Sstevel@tonic-gate /*LINTED*/ 8557c478bd9Sstevel@tonic-gate picld_get_attr_by_row((picl_service_t *)argp); 8567c478bd9Sstevel@tonic-gate break; 8577c478bd9Sstevel@tonic-gate case PICL_CNUM_GETATTRBYCOL: 8587c478bd9Sstevel@tonic-gate /*LINTED*/ 8597c478bd9Sstevel@tonic-gate picld_get_attr_by_col((picl_service_t *)argp); 8607c478bd9Sstevel@tonic-gate break; 8617c478bd9Sstevel@tonic-gate case PICL_CNUM_SETATTRVAL: 8627c478bd9Sstevel@tonic-gate /*LINTED*/ 8637c478bd9Sstevel@tonic-gate picld_set_attrval((picl_service_t *)argp); 8647c478bd9Sstevel@tonic-gate break; 8657c478bd9Sstevel@tonic-gate case PICL_CNUM_SETATTRVALBYNAME: 8667c478bd9Sstevel@tonic-gate /*LINTED*/ 8677c478bd9Sstevel@tonic-gate picld_set_attrval_by_name((picl_service_t *)argp); 8687c478bd9Sstevel@tonic-gate break; 8697c478bd9Sstevel@tonic-gate case PICL_CNUM_PING: 8707c478bd9Sstevel@tonic-gate /*LINTED*/ 8717c478bd9Sstevel@tonic-gate picld_ping((picl_service_t *)argp); 8727c478bd9Sstevel@tonic-gate break; 8737c478bd9Sstevel@tonic-gate case PICL_CNUM_WAIT: 8747c478bd9Sstevel@tonic-gate /*LINTED*/ 8757c478bd9Sstevel@tonic-gate picld_wait((picl_service_t *)argp); 8767c478bd9Sstevel@tonic-gate break; 8777c478bd9Sstevel@tonic-gate case PICL_CNUM_FINDNODE: 8787c478bd9Sstevel@tonic-gate /*LINTED*/ 8797c478bd9Sstevel@tonic-gate picld_find_node((picl_service_t *)argp); 8807c478bd9Sstevel@tonic-gate break; 8817c478bd9Sstevel@tonic-gate case PICL_CNUM_NODEBYPATH: 8827c478bd9Sstevel@tonic-gate /*LINTED*/ 8837c478bd9Sstevel@tonic-gate picld_get_node_by_path((picl_service_t *)argp); 8847c478bd9Sstevel@tonic-gate break; 8857c478bd9Sstevel@tonic-gate case PICL_CNUM_FRUTREEPARENT: 8867c478bd9Sstevel@tonic-gate /*LINTED*/ 8877c478bd9Sstevel@tonic-gate picld_get_frutree_parent((picl_service_t *)argp); 8887c478bd9Sstevel@tonic-gate break; 8897c478bd9Sstevel@tonic-gate default: 8907c478bd9Sstevel@tonic-gate /*LINTED*/ 8917c478bd9Sstevel@tonic-gate picld_unknown_service((picl_service_t *)argp); 8927c478bd9Sstevel@tonic-gate break; 8937c478bd9Sstevel@tonic-gate }; 8947c478bd9Sstevel@tonic-gate /*NOTREACHED*/ 8957c478bd9Sstevel@tonic-gate } 8967c478bd9Sstevel@tonic-gate 8977c478bd9Sstevel@tonic-gate /* ARGSUSED */ 8987c478bd9Sstevel@tonic-gate static void 8997c478bd9Sstevel@tonic-gate hup_handler(int sig, siginfo_t *siginfo, void *sigctx) 9007c478bd9Sstevel@tonic-gate { 9017c478bd9Sstevel@tonic-gate doreinit = 1; 9027c478bd9Sstevel@tonic-gate } 9037c478bd9Sstevel@tonic-gate 9047c478bd9Sstevel@tonic-gate /* 9057c478bd9Sstevel@tonic-gate * "ping" to see if a daemon is already running 9067c478bd9Sstevel@tonic-gate */ 9077c478bd9Sstevel@tonic-gate static int 9087c478bd9Sstevel@tonic-gate daemon_exists(void) 9097c478bd9Sstevel@tonic-gate { 9107c478bd9Sstevel@tonic-gate door_arg_t darg; 9117c478bd9Sstevel@tonic-gate picl_reqping_t req_ping; 9127c478bd9Sstevel@tonic-gate picl_retping_t ret_ping; 9137c478bd9Sstevel@tonic-gate int doorh; 9147c478bd9Sstevel@tonic-gate door_info_t dinfo; 9157c478bd9Sstevel@tonic-gate 9167c478bd9Sstevel@tonic-gate doorh = open(PICLD_DOOR, O_RDONLY); 9177c478bd9Sstevel@tonic-gate if (doorh < 0) 9187c478bd9Sstevel@tonic-gate return (0); 9197c478bd9Sstevel@tonic-gate 9207c478bd9Sstevel@tonic-gate if (door_info(doorh, &dinfo) < 0) { 9217c478bd9Sstevel@tonic-gate (void) close(doorh); 9227c478bd9Sstevel@tonic-gate return (0); 9237c478bd9Sstevel@tonic-gate } 9247c478bd9Sstevel@tonic-gate 9257c478bd9Sstevel@tonic-gate if ((dinfo.di_attributes & DOOR_REVOKED) || 9260ec57554Sraf (dinfo.di_data != (uintptr_t)PICLD_DOOR_COOKIE)) { 9277c478bd9Sstevel@tonic-gate (void) close(doorh); 9287c478bd9Sstevel@tonic-gate return (0); 9297c478bd9Sstevel@tonic-gate } 9307c478bd9Sstevel@tonic-gate 9317c478bd9Sstevel@tonic-gate if (dinfo.di_target != getpid()) { 9327c478bd9Sstevel@tonic-gate (void) close(doorh); 9337c478bd9Sstevel@tonic-gate return (1); 9347c478bd9Sstevel@tonic-gate } 9357c478bd9Sstevel@tonic-gate 9367c478bd9Sstevel@tonic-gate req_ping.cnum = PICL_CNUM_PING; 9377c478bd9Sstevel@tonic-gate 9387c478bd9Sstevel@tonic-gate darg.data_ptr = (char *)&req_ping; 9397c478bd9Sstevel@tonic-gate darg.data_size = sizeof (picl_reqping_t); 9407c478bd9Sstevel@tonic-gate darg.desc_ptr = NULL; 9417c478bd9Sstevel@tonic-gate darg.desc_num = 0; 9427c478bd9Sstevel@tonic-gate darg.rbuf = (char *)&ret_ping; 9437c478bd9Sstevel@tonic-gate darg.rsize = sizeof (picl_retping_t); 9447c478bd9Sstevel@tonic-gate 9457c478bd9Sstevel@tonic-gate if (door_call(doorh, &darg) < 0) { 9467c478bd9Sstevel@tonic-gate (void) close(doorh); 9477c478bd9Sstevel@tonic-gate return (0); 9487c478bd9Sstevel@tonic-gate } 9497c478bd9Sstevel@tonic-gate 9507c478bd9Sstevel@tonic-gate (void) close(doorh); 9517c478bd9Sstevel@tonic-gate return (1); 9527c478bd9Sstevel@tonic-gate } 9537c478bd9Sstevel@tonic-gate 9547c478bd9Sstevel@tonic-gate /* 955*980a6e61Sjfrank * picld_create_server_thread - binds the running thread to the private 956*980a6e61Sjfrank * door pool, and sets the required cancellation state. 957*980a6e61Sjfrank */ 958*980a6e61Sjfrank /* ARGSUSED */ 959*980a6e61Sjfrank static void * 960*980a6e61Sjfrank picld_create_server_thread(void *arg) 961*980a6e61Sjfrank { 962*980a6e61Sjfrank /* 963*980a6e61Sjfrank * wait for door descriptor to be initialized 964*980a6e61Sjfrank */ 965*980a6e61Sjfrank (void) pthread_mutex_lock(&door_mutex); 966*980a6e61Sjfrank while (door_id == -1) { 967*980a6e61Sjfrank (void) pthread_cond_wait(&door_cv, &door_mutex); 968*980a6e61Sjfrank } 969*980a6e61Sjfrank (void) pthread_mutex_unlock(&door_mutex); 970*980a6e61Sjfrank 971*980a6e61Sjfrank /* 972*980a6e61Sjfrank * Bind this thread to the door's private thread pool 973*980a6e61Sjfrank */ 974*980a6e61Sjfrank if (door_bind(door_id) < 0) { 975*980a6e61Sjfrank perror("door_bind"); 976*980a6e61Sjfrank } 977*980a6e61Sjfrank 978*980a6e61Sjfrank /* 979*980a6e61Sjfrank * Disable thread cancellation mechanism 980*980a6e61Sjfrank */ 981*980a6e61Sjfrank (void) pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL); 982*980a6e61Sjfrank (void) door_return(NULL, 0, NULL, 0); /* wait for door invocation */ 983*980a6e61Sjfrank return (NULL); 984*980a6e61Sjfrank } 985*980a6e61Sjfrank 986*980a6e61Sjfrank /* 987*980a6e61Sjfrank * picld_server_create_fn - creates threads for the private door pool 988*980a6e61Sjfrank * 989*980a6e61Sjfrank */ 990*980a6e61Sjfrank /* ARGSUSED */ 991*980a6e61Sjfrank static void 992*980a6e61Sjfrank picld_server_create_fn(door_info_t *dip) 993*980a6e61Sjfrank { 994*980a6e61Sjfrank pthread_attr_t attr; 995*980a6e61Sjfrank 996*980a6e61Sjfrank (void) pthread_mutex_lock(&pool_mutex); 997*980a6e61Sjfrank if (pool_count < MAX_POOL_SIZE) { 998*980a6e61Sjfrank (void) pthread_attr_init(&attr); 999*980a6e61Sjfrank (void) pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM); 1000*980a6e61Sjfrank (void) pthread_attr_setdetachstate(&attr, 1001*980a6e61Sjfrank PTHREAD_CREATE_DETACHED); 1002*980a6e61Sjfrank if (pthread_create(NULL, &attr, picld_create_server_thread, 1003*980a6e61Sjfrank NULL)) { 1004*980a6e61Sjfrank perror("pthread_create"); 1005*980a6e61Sjfrank } else { 1006*980a6e61Sjfrank pool_count++; 1007*980a6e61Sjfrank } 1008*980a6e61Sjfrank } 1009*980a6e61Sjfrank (void) pthread_mutex_unlock(&pool_mutex); 1010*980a6e61Sjfrank } 1011*980a6e61Sjfrank 1012*980a6e61Sjfrank /* 10137c478bd9Sstevel@tonic-gate * Create the picld door 10147c478bd9Sstevel@tonic-gate */ 10157c478bd9Sstevel@tonic-gate static int 10167c478bd9Sstevel@tonic-gate setup_door(void) 10177c478bd9Sstevel@tonic-gate { 10187c478bd9Sstevel@tonic-gate struct stat stbuf; 10197c478bd9Sstevel@tonic-gate 1020*980a6e61Sjfrank (void) door_server_create(picld_server_create_fn); 1021*980a6e61Sjfrank (void) pthread_mutex_lock(&door_mutex); 10227c478bd9Sstevel@tonic-gate /* 10237c478bd9Sstevel@tonic-gate * Create the door 10247c478bd9Sstevel@tonic-gate */ 10257c478bd9Sstevel@tonic-gate door_id = door_create(picld_door_handler, PICLD_DOOR_COOKIE, 1026*980a6e61Sjfrank DOOR_REFUSE_DESC | DOOR_NO_CANCEL | DOOR_PRIVATE); 10277c478bd9Sstevel@tonic-gate 10287c478bd9Sstevel@tonic-gate if (door_id < 0) { 1029*980a6e61Sjfrank (void) pthread_mutex_unlock(&door_mutex); 10307c478bd9Sstevel@tonic-gate return (-1); 1031*980a6e61Sjfrank } else { 1032*980a6e61Sjfrank (void) pthread_cond_signal(&door_cv); 1033*980a6e61Sjfrank (void) pthread_mutex_unlock(&door_mutex); 10347c478bd9Sstevel@tonic-gate } 10357c478bd9Sstevel@tonic-gate 10367c478bd9Sstevel@tonic-gate if (stat(PICLD_DOOR, &stbuf) < 0) { 10377c478bd9Sstevel@tonic-gate int newfd; 103824b3ac2eSjfrank mode_t old_mask; 103924b3ac2eSjfrank /* ensure that the door file is world-readable */ 104024b3ac2eSjfrank old_mask = umask(0); 104124b3ac2eSjfrank newfd = creat(PICLD_DOOR, 0444); 104224b3ac2eSjfrank /* restore the file mode creation mask */ 104324b3ac2eSjfrank (void) umask(old_mask); 104424b3ac2eSjfrank if (newfd < 0) 10457c478bd9Sstevel@tonic-gate return (-1); 10467c478bd9Sstevel@tonic-gate (void) close(newfd); 10477c478bd9Sstevel@tonic-gate } 10487c478bd9Sstevel@tonic-gate 10497c478bd9Sstevel@tonic-gate if (fattach(door_id, PICLD_DOOR) < 0) { 10507c478bd9Sstevel@tonic-gate if ((errno != EBUSY) || 10517c478bd9Sstevel@tonic-gate (fdetach(PICLD_DOOR) < 0) || 10527c478bd9Sstevel@tonic-gate (fattach(door_id, PICLD_DOOR) < 0)) 10537c478bd9Sstevel@tonic-gate return (-1); 10547c478bd9Sstevel@tonic-gate } 10557c478bd9Sstevel@tonic-gate return (0); 10567c478bd9Sstevel@tonic-gate } 10577c478bd9Sstevel@tonic-gate 10587c478bd9Sstevel@tonic-gate /* 10597c478bd9Sstevel@tonic-gate * Main function of picl daemon 10607c478bd9Sstevel@tonic-gate */ 10617c478bd9Sstevel@tonic-gate int 10627c478bd9Sstevel@tonic-gate main(int argc, char **argv) 10637c478bd9Sstevel@tonic-gate { 10647c478bd9Sstevel@tonic-gate struct sigaction act; 10657c478bd9Sstevel@tonic-gate int c; 10667c478bd9Sstevel@tonic-gate sigset_t ublk; 10677c478bd9Sstevel@tonic-gate 10687c478bd9Sstevel@tonic-gate 10697c478bd9Sstevel@tonic-gate (void) setlocale(LC_ALL, ""); 10707c478bd9Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN); 10717c478bd9Sstevel@tonic-gate 10727c478bd9Sstevel@tonic-gate if (getuid() != 0) { 10737c478bd9Sstevel@tonic-gate syslog(LOG_CRIT, MUST_BE_ROOT); 10747c478bd9Sstevel@tonic-gate return (0); 10757c478bd9Sstevel@tonic-gate } 10767c478bd9Sstevel@tonic-gate 10777c478bd9Sstevel@tonic-gate (void) rwlock_init(&init_lk, USYNC_THREAD, NULL); 10787c478bd9Sstevel@tonic-gate doreinit = 0; 10797c478bd9Sstevel@tonic-gate logflag = 1; 10807c478bd9Sstevel@tonic-gate dos_req_limit = DOS_PICL_REQUESTS_LIMIT; 10817c478bd9Sstevel@tonic-gate sliding_interval_ms = SLIDING_INTERVAL_MILLISECONDS; 10827c478bd9Sstevel@tonic-gate dos_ms = DOS_SLEEPTIME_MS; 10837c478bd9Sstevel@tonic-gate verbose_level = 0; 10847c478bd9Sstevel@tonic-gate 10857c478bd9Sstevel@tonic-gate /* 10867c478bd9Sstevel@tonic-gate * parse arguments 10877c478bd9Sstevel@tonic-gate */ 10887c478bd9Sstevel@tonic-gate while ((c = getopt(argc, argv, "is:t:l:r:v:d:")) != EOF) { 10897c478bd9Sstevel@tonic-gate switch (c) { 10907c478bd9Sstevel@tonic-gate case 'd': 10917c478bd9Sstevel@tonic-gate dos_ms = strtol(optarg, (char **)NULL, 0); 10927c478bd9Sstevel@tonic-gate break; 10937c478bd9Sstevel@tonic-gate case 'i': 10947c478bd9Sstevel@tonic-gate logflag = 0; 10957c478bd9Sstevel@tonic-gate break; 10967c478bd9Sstevel@tonic-gate case 's': 10977c478bd9Sstevel@tonic-gate sliding_interval_ms = strtoll(optarg, (char **)NULL, 0); 10987c478bd9Sstevel@tonic-gate break; 10997c478bd9Sstevel@tonic-gate case 't': 11007c478bd9Sstevel@tonic-gate dos_req_limit = strtol(optarg, (char **)NULL, 0); 11017c478bd9Sstevel@tonic-gate break; 11027c478bd9Sstevel@tonic-gate case 'v': 11037c478bd9Sstevel@tonic-gate verbose_level = strtol(optarg, (char **)NULL, 0); 11047c478bd9Sstevel@tonic-gate logflag = 0; 11057c478bd9Sstevel@tonic-gate break; 11067c478bd9Sstevel@tonic-gate default: 11077c478bd9Sstevel@tonic-gate break; 11087c478bd9Sstevel@tonic-gate } 11097c478bd9Sstevel@tonic-gate } 11107c478bd9Sstevel@tonic-gate 11117c478bd9Sstevel@tonic-gate orig_time = gethrtime(); 11127c478bd9Sstevel@tonic-gate 11137c478bd9Sstevel@tonic-gate /* 11147c478bd9Sstevel@tonic-gate * is there a daemon already running? 11157c478bd9Sstevel@tonic-gate */ 11167c478bd9Sstevel@tonic-gate 11177c478bd9Sstevel@tonic-gate if (daemon_exists()) { 11187c478bd9Sstevel@tonic-gate syslog(LOG_CRIT, DAEMON_RUNNING); 11197c478bd9Sstevel@tonic-gate exit(1); 11207c478bd9Sstevel@tonic-gate } 11217c478bd9Sstevel@tonic-gate 11227c478bd9Sstevel@tonic-gate /* 11237c478bd9Sstevel@tonic-gate * Mask off/block SIGALRM signal so that the environmental plug-in 11247c478bd9Sstevel@tonic-gate * (piclenvd) can use it to simulate sleep() without being affected 11257c478bd9Sstevel@tonic-gate * by time being set back. No other PICL plug-in should use SIGALRM 11267c478bd9Sstevel@tonic-gate * or alarm() for now. 11277c478bd9Sstevel@tonic-gate */ 11287c478bd9Sstevel@tonic-gate (void) sigemptyset(&ublk); 11297c478bd9Sstevel@tonic-gate (void) sigaddset(&ublk, SIGALRM); 11307c478bd9Sstevel@tonic-gate (void) sigprocmask(SIG_BLOCK, &ublk, NULL); 11317c478bd9Sstevel@tonic-gate 11327c478bd9Sstevel@tonic-gate /* 11337c478bd9Sstevel@tonic-gate * Ignore SIGHUP until all the initialization is done. 11347c478bd9Sstevel@tonic-gate */ 11357c478bd9Sstevel@tonic-gate act.sa_handler = SIG_IGN; 11367c478bd9Sstevel@tonic-gate (void) sigemptyset(&act.sa_mask); 11377c478bd9Sstevel@tonic-gate act.sa_flags = 0; 11387c478bd9Sstevel@tonic-gate if (sigaction(SIGHUP, &act, NULL) == -1) 11397c478bd9Sstevel@tonic-gate syslog(LOG_ERR, SIGACT_FAILED, strsignal(SIGHUP), 11407c478bd9Sstevel@tonic-gate strerror(errno)); 11417c478bd9Sstevel@tonic-gate 11427c478bd9Sstevel@tonic-gate if (logflag != 0) { /* daemonize */ 11437c478bd9Sstevel@tonic-gate pid_t pid; 11447c478bd9Sstevel@tonic-gate 11457c478bd9Sstevel@tonic-gate pid = fork(); 11467c478bd9Sstevel@tonic-gate if (pid < 0) 11477c478bd9Sstevel@tonic-gate exit(1); 11487c478bd9Sstevel@tonic-gate if (pid > 0) 11497c478bd9Sstevel@tonic-gate /* parent */ 11507c478bd9Sstevel@tonic-gate exit(0); 11517c478bd9Sstevel@tonic-gate 11527c478bd9Sstevel@tonic-gate /* child */ 11537c478bd9Sstevel@tonic-gate if (chdir("/") == -1) { 11547c478bd9Sstevel@tonic-gate syslog(LOG_CRIT, CD_ROOT_FAILED); 11557c478bd9Sstevel@tonic-gate exit(1); 11567c478bd9Sstevel@tonic-gate } 11577c478bd9Sstevel@tonic-gate 11587c478bd9Sstevel@tonic-gate (void) setsid(); 11597c478bd9Sstevel@tonic-gate (void) close(STDIN_FILENO); 11607c478bd9Sstevel@tonic-gate (void) close(STDOUT_FILENO); 11617c478bd9Sstevel@tonic-gate (void) close(STDERR_FILENO); 11627c478bd9Sstevel@tonic-gate (void) open("/dev/null", O_RDWR, 0); 11637c478bd9Sstevel@tonic-gate (void) dup2(STDIN_FILENO, STDOUT_FILENO); 11647c478bd9Sstevel@tonic-gate (void) dup2(STDIN_FILENO, STDERR_FILENO); 11657c478bd9Sstevel@tonic-gate openlog(PICLD, LOG_PID, LOG_DAEMON); 11667c478bd9Sstevel@tonic-gate } 11677c478bd9Sstevel@tonic-gate 11687c478bd9Sstevel@tonic-gate /* 11697c478bd9Sstevel@tonic-gate * Initialize the PICL Tree 11707c478bd9Sstevel@tonic-gate */ 11717c478bd9Sstevel@tonic-gate if (xptree_initialize(NULL) != PICL_SUCCESS) { 11727c478bd9Sstevel@tonic-gate syslog(LOG_CRIT, INIT_FAILED); 11737c478bd9Sstevel@tonic-gate exit(1); 11747c478bd9Sstevel@tonic-gate } 11757c478bd9Sstevel@tonic-gate 11767c478bd9Sstevel@tonic-gate if (setup_door()) { 11777c478bd9Sstevel@tonic-gate syslog(LOG_CRIT, DOOR_FAILED); 11787c478bd9Sstevel@tonic-gate exit(1); 11797c478bd9Sstevel@tonic-gate } 11807c478bd9Sstevel@tonic-gate 11817c478bd9Sstevel@tonic-gate /* 11827c478bd9Sstevel@tonic-gate * setup signal handlers for post-init 11837c478bd9Sstevel@tonic-gate */ 11847c478bd9Sstevel@tonic-gate act.sa_sigaction = hup_handler; 11857c478bd9Sstevel@tonic-gate (void) sigemptyset(&act.sa_mask); 11867c478bd9Sstevel@tonic-gate act.sa_flags = SA_SIGINFO; 11877c478bd9Sstevel@tonic-gate if (sigaction(SIGHUP, &act, NULL) == -1) 11887c478bd9Sstevel@tonic-gate syslog(LOG_ERR, SIGACT_FAILED, strsignal(SIGHUP), 11897c478bd9Sstevel@tonic-gate strerror(errno)); 11907c478bd9Sstevel@tonic-gate 11917c478bd9Sstevel@tonic-gate /* 11927c478bd9Sstevel@tonic-gate * wait for requests 11937c478bd9Sstevel@tonic-gate */ 11947c478bd9Sstevel@tonic-gate for (;;) { 11957c478bd9Sstevel@tonic-gate (void) pause(); 11967c478bd9Sstevel@tonic-gate if (doreinit) { 11977c478bd9Sstevel@tonic-gate /* 11987c478bd9Sstevel@tonic-gate * Block SIGHUP during reinitialization. 11997c478bd9Sstevel@tonic-gate * Also mask off/block SIGALRM signal so that the 12007c478bd9Sstevel@tonic-gate * environmental plug-in (piclenvd) can use it to 12017c478bd9Sstevel@tonic-gate * simulate sleep() without being affected by time 12027c478bd9Sstevel@tonic-gate * being set back. No ohter PICL plug-in should use 12037c478bd9Sstevel@tonic-gate * SIGALRM or alarm() for now. 12047c478bd9Sstevel@tonic-gate */ 12057c478bd9Sstevel@tonic-gate (void) sigemptyset(&ublk); 12067c478bd9Sstevel@tonic-gate (void) sigaddset(&ublk, SIGHUP); 12077c478bd9Sstevel@tonic-gate (void) sigaddset(&ublk, SIGALRM); 12087c478bd9Sstevel@tonic-gate (void) sigprocmask(SIG_BLOCK, &ublk, NULL); 12097c478bd9Sstevel@tonic-gate (void) sigdelset(&ublk, SIGALRM); 12107c478bd9Sstevel@tonic-gate doreinit = 0; 12117c478bd9Sstevel@tonic-gate (void) rw_wrlock(&init_lk); 12127c478bd9Sstevel@tonic-gate xptree_destroy(); 12137c478bd9Sstevel@tonic-gate (void) xptree_reinitialize(); 12147c478bd9Sstevel@tonic-gate (void) rw_unlock(&init_lk); 12157c478bd9Sstevel@tonic-gate (void) sigprocmask(SIG_UNBLOCK, &ublk, NULL); 12167c478bd9Sstevel@tonic-gate } 12177c478bd9Sstevel@tonic-gate } 12187c478bd9Sstevel@tonic-gate } 1219