xref: /onnv-gate/usr/src/cmd/picl/picld/picld.c (revision 1016)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
22*1016Sraf 
230Sstevel@tonic-gate /*
240Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
250Sstevel@tonic-gate  * Use is subject to license terms.
260Sstevel@tonic-gate  */
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
290Sstevel@tonic-gate 
300Sstevel@tonic-gate /*
310Sstevel@tonic-gate  * PICL daemon
320Sstevel@tonic-gate  */
330Sstevel@tonic-gate 
340Sstevel@tonic-gate #include <stdio.h>
350Sstevel@tonic-gate #include <stdlib.h>
360Sstevel@tonic-gate #include <stdarg.h>
370Sstevel@tonic-gate #include <string.h>
380Sstevel@tonic-gate #include <libintl.h>
390Sstevel@tonic-gate #include <locale.h>
400Sstevel@tonic-gate #include <alloca.h>
410Sstevel@tonic-gate #include <errno.h>
420Sstevel@tonic-gate #include <assert.h>
430Sstevel@tonic-gate #include <stropts.h>
440Sstevel@tonic-gate #include <unistd.h>
450Sstevel@tonic-gate #include <signal.h>
460Sstevel@tonic-gate #include <pthread.h>
470Sstevel@tonic-gate #include <synch.h>
480Sstevel@tonic-gate #include <door.h>
490Sstevel@tonic-gate #include <sys/door.h>
500Sstevel@tonic-gate #include <fcntl.h>
510Sstevel@tonic-gate #include <dlfcn.h>
520Sstevel@tonic-gate #include <time.h>
530Sstevel@tonic-gate #include <sys/utsname.h>
540Sstevel@tonic-gate #include <sys/systeminfo.h>
550Sstevel@tonic-gate #include <sys/stat.h>
560Sstevel@tonic-gate #include <sys/wait.h>
570Sstevel@tonic-gate #include <dirent.h>
580Sstevel@tonic-gate #include <syslog.h>
590Sstevel@tonic-gate #include <poll.h>
600Sstevel@tonic-gate #include <limits.h>
610Sstevel@tonic-gate #include <picl.h>
620Sstevel@tonic-gate #include "picl2door.h"
630Sstevel@tonic-gate #include <picltree.h>
640Sstevel@tonic-gate #include "ptree_impl.h"
650Sstevel@tonic-gate 
660Sstevel@tonic-gate /*
670Sstevel@tonic-gate  * Log text messages
680Sstevel@tonic-gate  */
690Sstevel@tonic-gate #define	MUST_BE_ROOT	gettext("this program must be run as root\n")
700Sstevel@tonic-gate #define	CD_ROOT_FAILED	gettext("chdir to root failed\n")
710Sstevel@tonic-gate #define	INIT_FAILED	gettext("ptree initialization failed\n")
720Sstevel@tonic-gate #define	DAEMON_RUNNING	gettext("PICL daemon already running\n")
730Sstevel@tonic-gate #define	DOOR_FAILED	gettext("Failed creating picld door\n")
740Sstevel@tonic-gate #define	SIGACT_FAILED	\
750Sstevel@tonic-gate 		gettext("Failed to install signal handler for %s: %s\n")
760Sstevel@tonic-gate 
770Sstevel@tonic-gate /*
780Sstevel@tonic-gate  * Constants
790Sstevel@tonic-gate  */
800Sstevel@tonic-gate #define	PICLD				"picld"
810Sstevel@tonic-gate #define	DOS_PICL_REQUESTS_LIMIT		10000
820Sstevel@tonic-gate #define	SLIDING_INTERVAL_MILLISECONDS	1000
830Sstevel@tonic-gate #define	PICLD_MAJOR_REV			0x1
840Sstevel@tonic-gate #define	PICLD_MINOR_REV			0x0
850Sstevel@tonic-gate #define	DOS_SLEEPTIME_MS		1000
860Sstevel@tonic-gate 
870Sstevel@tonic-gate /*
880Sstevel@tonic-gate  * Macros
890Sstevel@tonic-gate  */
900Sstevel@tonic-gate #define	PICLD_VERSION(x, y)	((x << 8) | y)
910Sstevel@tonic-gate #define	PICL_CLIENT_REV(x)	(x & 0xff)
920Sstevel@tonic-gate #define	MILLI_TO_NANO(x)	(x * 1000000)
930Sstevel@tonic-gate 
940Sstevel@tonic-gate extern	char	**environ;
950Sstevel@tonic-gate 
960Sstevel@tonic-gate /*
970Sstevel@tonic-gate  * Module Variables
980Sstevel@tonic-gate  */
990Sstevel@tonic-gate static	int		logflag = 1;
1000Sstevel@tonic-gate static	int		doreinit = 0;
1010Sstevel@tonic-gate static	int		door_id = -1;
1020Sstevel@tonic-gate static  int 		service_requests = 0;
1030Sstevel@tonic-gate static	hrtime_t	orig_time;
1040Sstevel@tonic-gate static	hrtime_t	sliding_interval_ms;
1050Sstevel@tonic-gate static	uint32_t	dos_req_limit;
1060Sstevel@tonic-gate static	uint32_t	dos_ms;
1070Sstevel@tonic-gate static	pthread_mutex_t	dos_mutex = PTHREAD_MUTEX_INITIALIZER;
1080Sstevel@tonic-gate static	rwlock_t	init_lk;
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate /*
1110Sstevel@tonic-gate  * This returns an error message to libpicl
1120Sstevel@tonic-gate  */
1130Sstevel@tonic-gate static void
1140Sstevel@tonic-gate picld_return_error(picl_callnumber_t cnum, picl_errno_t err)
1150Sstevel@tonic-gate {
1160Sstevel@tonic-gate 	picl_reterror_t	ret_error;
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate 	ret_error.cnum = PICL_CNUM_ERROR;
1190Sstevel@tonic-gate 	ret_error.in_cnum = cnum;
1200Sstevel@tonic-gate 	ret_error.errnum = err;
1210Sstevel@tonic-gate 	(void) rw_unlock(&init_lk);
1220Sstevel@tonic-gate 	(void) door_return((char *)&ret_error, sizeof (picl_reterror_t), NULL,
1230Sstevel@tonic-gate 	    0);
1240Sstevel@tonic-gate }
1250Sstevel@tonic-gate 
1260Sstevel@tonic-gate /*
1270Sstevel@tonic-gate  * picld_init is called when a picl_initialize request is received
1280Sstevel@tonic-gate  */
1290Sstevel@tonic-gate static void
1300Sstevel@tonic-gate picld_init(picl_service_t *req)
1310Sstevel@tonic-gate {
1320Sstevel@tonic-gate 	picl_retinit_t	ret_init;
1330Sstevel@tonic-gate 	int	clmajrev;
1340Sstevel@tonic-gate 
1350Sstevel@tonic-gate 	clmajrev = PICL_CLIENT_REV(req->req_init.clrev);
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate 	if (clmajrev < PICL_VERSION_1)
1380Sstevel@tonic-gate 		picld_return_error(req->req_init.cnum, PICL_NOTSUPPORTED);
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate 	ret_init.cnum = req->req_init.cnum;
1410Sstevel@tonic-gate 	ret_init.rev = PICLD_VERSION(PICLD_MAJOR_REV, PICLD_MINOR_REV);
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate 	(void) rw_unlock(&init_lk);
1440Sstevel@tonic-gate 	(void) door_return((char *)&ret_init, sizeof (picl_retinit_t), NULL, 0);
1450Sstevel@tonic-gate }
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate /*
1480Sstevel@tonic-gate  * picld_fini is called when a picl_shutdown request is received
1490Sstevel@tonic-gate  */
1500Sstevel@tonic-gate static void
1510Sstevel@tonic-gate picld_fini(picl_service_t *in)
1520Sstevel@tonic-gate {
1530Sstevel@tonic-gate 	picl_retfini_t	ret;
1540Sstevel@tonic-gate 
1550Sstevel@tonic-gate 	ret.cnum = in->req_fini.cnum;
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate 	(void) rw_unlock(&init_lk);
1580Sstevel@tonic-gate 	(void) door_return((char *)&ret, sizeof (picl_retfini_t), NULL, 0);
1590Sstevel@tonic-gate }
1600Sstevel@tonic-gate 
1610Sstevel@tonic-gate static void
1620Sstevel@tonic-gate picld_ping(picl_service_t *in)
1630Sstevel@tonic-gate {
1640Sstevel@tonic-gate 	picl_retping_t	ret;
1650Sstevel@tonic-gate 
1660Sstevel@tonic-gate 	ret.cnum = in->req_ping.cnum;
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate 	(void) rw_unlock(&init_lk);
1690Sstevel@tonic-gate 	(void) door_return((char *)&ret, sizeof (picl_retping_t), NULL, 0);
1700Sstevel@tonic-gate }
1710Sstevel@tonic-gate 
1720Sstevel@tonic-gate /*
1730Sstevel@tonic-gate  * picld_wait is called when a picl_wait request is received
1740Sstevel@tonic-gate  */
1750Sstevel@tonic-gate static void
1760Sstevel@tonic-gate picld_wait(picl_service_t *in)
1770Sstevel@tonic-gate {
1780Sstevel@tonic-gate 	picl_retwait_t	ret;
1790Sstevel@tonic-gate 	int		err;
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate 	ret.cnum = in->req_wait.cnum;
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate 	err = xptree_refresh_notify(in->req_wait.secs);
1840Sstevel@tonic-gate 	ret.retcode = err;
1850Sstevel@tonic-gate 
1860Sstevel@tonic-gate 	(void) rw_unlock(&init_lk);
1870Sstevel@tonic-gate 	(void) door_return((char *)&ret, sizeof (picl_retwait_t), NULL, 0);
1880Sstevel@tonic-gate }
1890Sstevel@tonic-gate 
1900Sstevel@tonic-gate /*
1910Sstevel@tonic-gate  * This function returns the handle of the root node of the PICL tree
1920Sstevel@tonic-gate  */
1930Sstevel@tonic-gate static void
1940Sstevel@tonic-gate picld_getroot(picl_service_t *in)
1950Sstevel@tonic-gate {
1960Sstevel@tonic-gate 	picl_retroot_t	ret;
1970Sstevel@tonic-gate 	int		err;
1980Sstevel@tonic-gate 
1990Sstevel@tonic-gate 	ret.cnum = PICL_CNUM_GETROOT;
2000Sstevel@tonic-gate 	err = ptree_get_root(&ret.rnode);
2010Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
2020Sstevel@tonic-gate 		picld_return_error(in->in.cnum, err);
2030Sstevel@tonic-gate 	cvt_ptree2picl(&ret.rnode);
2040Sstevel@tonic-gate 	(void) rw_unlock(&init_lk);
2050Sstevel@tonic-gate 	(void) door_return((char *)&ret, sizeof (picl_retroot_t), NULL, 0);
2060Sstevel@tonic-gate }
2070Sstevel@tonic-gate 
2080Sstevel@tonic-gate /*
2090Sstevel@tonic-gate  * This function returns the value of the PICL property
2100Sstevel@tonic-gate  */
2110Sstevel@tonic-gate static void
2120Sstevel@tonic-gate picld_get_attrval(picl_service_t *in)
2130Sstevel@tonic-gate {
2140Sstevel@tonic-gate 	picl_retattrval_t	*ret;
2150Sstevel@tonic-gate 	int			err;
2160Sstevel@tonic-gate 	size_t			vbufsize;
2170Sstevel@tonic-gate 	size_t			len;
2180Sstevel@tonic-gate 	door_cred_t		cred;
2190Sstevel@tonic-gate 	picl_prophdl_t		ptreeh;
2200Sstevel@tonic-gate 	ptree_propinfo_t	pinfo;
2210Sstevel@tonic-gate 
2220Sstevel@tonic-gate 	if (door_cred(&cred) < 0)
2230Sstevel@tonic-gate 		picld_return_error(in->in.cnum, PICL_FAILURE);
2240Sstevel@tonic-gate 
2250Sstevel@tonic-gate 	err = cvt_picl2ptree(in->req_attrval.attr, &ptreeh);
2260Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
2270Sstevel@tonic-gate 		picld_return_error(in->in.cnum, err);
2280Sstevel@tonic-gate 
2290Sstevel@tonic-gate 	err = ptree_get_propinfo(ptreeh, &pinfo);
2300Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
2310Sstevel@tonic-gate 		picld_return_error(in->in.cnum, err);
2320Sstevel@tonic-gate 
2330Sstevel@tonic-gate 	if (!(pinfo.piclinfo.accessmode & PICL_READ))
2340Sstevel@tonic-gate 		picld_return_error(in->in.cnum, PICL_NOTREADABLE);
2350Sstevel@tonic-gate 
2360Sstevel@tonic-gate 	vbufsize = pinfo.piclinfo.size;
2370Sstevel@tonic-gate 	vbufsize = MIN((size_t)in->req_attrval.bufsize, vbufsize);
2380Sstevel@tonic-gate 
2390Sstevel@tonic-gate 	len = sizeof (picl_retattrval_t) + vbufsize;
2400Sstevel@tonic-gate 	ret = alloca(len);
2410Sstevel@tonic-gate 	if (ret == NULL)
2420Sstevel@tonic-gate 		picld_return_error(in->in.cnum, PICL_FAILURE);
2430Sstevel@tonic-gate 	ret->cnum = PICL_CNUM_GETATTRVAL;
2440Sstevel@tonic-gate 	ret->attr = in->req_attrval.attr;
2450Sstevel@tonic-gate 	ret->nbytes = (uint32_t)vbufsize;
2460Sstevel@tonic-gate 	err = xptree_get_propval_with_cred(ptreeh, ret->ret_buf, vbufsize,
2470Sstevel@tonic-gate 	    cred);
2480Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
2490Sstevel@tonic-gate 		picld_return_error(in->in.cnum, err);
2500Sstevel@tonic-gate 
2510Sstevel@tonic-gate 	/*
2520Sstevel@tonic-gate 	 * adjust returned bytes for charstrings
2530Sstevel@tonic-gate 	 */
2540Sstevel@tonic-gate 	if (pinfo.piclinfo.type == PICL_PTYPE_CHARSTRING)
2550Sstevel@tonic-gate 		ret->nbytes = (uint32_t)strlen(ret->ret_buf) + 1;
2560Sstevel@tonic-gate 
2570Sstevel@tonic-gate 	/*
2580Sstevel@tonic-gate 	 * convert handle values to picl handles
2590Sstevel@tonic-gate 	 */
2600Sstevel@tonic-gate 	if ((pinfo.piclinfo.type == PICL_PTYPE_TABLE) ||
2610Sstevel@tonic-gate 	    (pinfo.piclinfo.type == PICL_PTYPE_REFERENCE))
2620Sstevel@tonic-gate 		cvt_ptree2picl(&ret->ret_nodeh);
2630Sstevel@tonic-gate 	(void) rw_unlock(&init_lk);
2640Sstevel@tonic-gate 	(void) door_return((char *)ret, sizeof (picl_retattrval_t) +
2650Sstevel@tonic-gate 	    (size_t)ret->nbytes, NULL, 0);
2660Sstevel@tonic-gate }
2670Sstevel@tonic-gate 
2680Sstevel@tonic-gate /*
2690Sstevel@tonic-gate  * This function returns the value of the PICL property specified by
2700Sstevel@tonic-gate  * its name.
2710Sstevel@tonic-gate  */
2720Sstevel@tonic-gate static void
2730Sstevel@tonic-gate picld_get_attrval_by_name(picl_service_t *in)
2740Sstevel@tonic-gate {
2750Sstevel@tonic-gate 	picl_retattrvalbyname_t	*ret;
2760Sstevel@tonic-gate 	int			err;
2770Sstevel@tonic-gate 	size_t			vbufsize;
2780Sstevel@tonic-gate 	size_t			len;
2790Sstevel@tonic-gate 	door_cred_t		cred;
2800Sstevel@tonic-gate 	picl_nodehdl_t		ptreeh;
2810Sstevel@tonic-gate 	ptree_propinfo_t	pinfo;
2820Sstevel@tonic-gate 
2830Sstevel@tonic-gate 	if (door_cred(&cred) < 0)
2840Sstevel@tonic-gate 		picld_return_error(in->in.cnum, PICL_FAILURE);
2850Sstevel@tonic-gate 
2860Sstevel@tonic-gate 	err = cvt_picl2ptree(in->req_attrvalbyname.nodeh, &ptreeh);
2870Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
2880Sstevel@tonic-gate 		picld_return_error(in->in.cnum, err);
2890Sstevel@tonic-gate 
2900Sstevel@tonic-gate 	err = xptree_get_propinfo_by_name(ptreeh,
2910Sstevel@tonic-gate 	    in->req_attrvalbyname.propname, &pinfo);
2920Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
2930Sstevel@tonic-gate 		picld_return_error(in->in.cnum, err);
2940Sstevel@tonic-gate 
2950Sstevel@tonic-gate 	if (!(pinfo.piclinfo.accessmode & PICL_READ))
2960Sstevel@tonic-gate 		picld_return_error(in->in.cnum, PICL_NOTREADABLE);
2970Sstevel@tonic-gate 
2980Sstevel@tonic-gate 	/*
2990Sstevel@tonic-gate 	 * allocate the minimum of piclinfo.size and input bufsize
3000Sstevel@tonic-gate 	 */
3010Sstevel@tonic-gate 	vbufsize = pinfo.piclinfo.size;
3020Sstevel@tonic-gate 	vbufsize = MIN((size_t)in->req_attrvalbyname.bufsize, vbufsize);
3030Sstevel@tonic-gate 	len = sizeof (picl_retattrvalbyname_t) + vbufsize;
3040Sstevel@tonic-gate 	ret = alloca(len);
3050Sstevel@tonic-gate 	if (ret == NULL)
3060Sstevel@tonic-gate 		picld_return_error(in->in.cnum, PICL_FAILURE);
3070Sstevel@tonic-gate 	ret->cnum = PICL_CNUM_GETATTRVALBYNAME;
3080Sstevel@tonic-gate 	ret->nodeh = in->req_attrvalbyname.nodeh;
3090Sstevel@tonic-gate 	(void) strcpy(ret->propname, in->req_attrvalbyname.propname);
3100Sstevel@tonic-gate 	ret->nbytes = (uint32_t)vbufsize;
3110Sstevel@tonic-gate 
3120Sstevel@tonic-gate 	err = xptree_get_propval_by_name_with_cred(ptreeh,
3130Sstevel@tonic-gate 	    in->req_attrvalbyname.propname, ret->ret_buf, vbufsize,
3140Sstevel@tonic-gate 	    cred);
3150Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
3160Sstevel@tonic-gate 		picld_return_error(in->in.cnum, err);
3170Sstevel@tonic-gate 	/*
3180Sstevel@tonic-gate 	 * adjust returned value size for charstrings
3190Sstevel@tonic-gate 	 */
3200Sstevel@tonic-gate 	if (pinfo.piclinfo.type == PICL_PTYPE_CHARSTRING)
3210Sstevel@tonic-gate 		ret->nbytes = (uint32_t)strlen(ret->ret_buf) + 1;
3220Sstevel@tonic-gate 
3230Sstevel@tonic-gate 	if ((pinfo.piclinfo.type == PICL_PTYPE_TABLE) ||
3240Sstevel@tonic-gate 	    (pinfo.piclinfo.type == PICL_PTYPE_REFERENCE))
3250Sstevel@tonic-gate 		cvt_ptree2picl(&ret->ret_nodeh);
3260Sstevel@tonic-gate 
3270Sstevel@tonic-gate 	(void) rw_unlock(&init_lk);
3280Sstevel@tonic-gate 	(void) door_return((char *)ret, sizeof (picl_retattrvalbyname_t) +
3290Sstevel@tonic-gate 	    (size_t)ret->nbytes, NULL, 0);
3300Sstevel@tonic-gate }
3310Sstevel@tonic-gate 
3320Sstevel@tonic-gate /*
3330Sstevel@tonic-gate  * This function sets a property value
3340Sstevel@tonic-gate  */
3350Sstevel@tonic-gate static void
3360Sstevel@tonic-gate picld_set_attrval(picl_service_t *in)
3370Sstevel@tonic-gate {
3380Sstevel@tonic-gate 	picl_retsetattrval_t	ret;
3390Sstevel@tonic-gate 	int			err;
3400Sstevel@tonic-gate 	door_cred_t		cred;
3410Sstevel@tonic-gate 	picl_prophdl_t		ptreeh;
3420Sstevel@tonic-gate 	ptree_propinfo_t	pinfo;
3430Sstevel@tonic-gate 
3440Sstevel@tonic-gate 	if (door_cred(&cred) < 0)
3450Sstevel@tonic-gate 		picld_return_error(in->in.cnum, PICL_FAILURE);
3460Sstevel@tonic-gate 
3470Sstevel@tonic-gate 	err = cvt_picl2ptree(in->req_setattrval.attr, &ptreeh);
3480Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
3490Sstevel@tonic-gate 		picld_return_error(in->in.cnum, err);
3500Sstevel@tonic-gate 
3510Sstevel@tonic-gate 	err = ptree_get_propinfo(ptreeh, &pinfo);
3520Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
3530Sstevel@tonic-gate 		picld_return_error(in->in.cnum, err);
3540Sstevel@tonic-gate 
3550Sstevel@tonic-gate 	if (!(pinfo.piclinfo.accessmode & PICL_WRITE))
3560Sstevel@tonic-gate 		picld_return_error(in->in.cnum, PICL_NOTWRITABLE);
3570Sstevel@tonic-gate 	/*
3580Sstevel@tonic-gate 	 * For non-volatile prop, only super user can set its value.
3590Sstevel@tonic-gate 	 */
3600Sstevel@tonic-gate 	if (!(pinfo.piclinfo.accessmode & PICL_VOLATILE) &&
3610Sstevel@tonic-gate 	    (cred.dc_euid != SUPER_USER))
3620Sstevel@tonic-gate 		picld_return_error(in->in.cnum, PICL_PERMDENIED);
3630Sstevel@tonic-gate 
3640Sstevel@tonic-gate 	ret.cnum = PICL_CNUM_SETATTRVAL;
3650Sstevel@tonic-gate 	ret.attr = in->req_setattrval.attr;
3660Sstevel@tonic-gate 
3670Sstevel@tonic-gate 	err = xptree_update_propval_with_cred(ptreeh, in->req_setattrval.valbuf,
3680Sstevel@tonic-gate 	    (size_t)in->req_setattrval.bufsize, cred);
3690Sstevel@tonic-gate 
3700Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
3710Sstevel@tonic-gate 		picld_return_error(in->in.cnum, err);
3720Sstevel@tonic-gate 
3730Sstevel@tonic-gate 	(void) rw_unlock(&init_lk);
3740Sstevel@tonic-gate 	(void) door_return((char *)&ret, sizeof (picl_retsetattrval_t), NULL,
3750Sstevel@tonic-gate 	    0);
3760Sstevel@tonic-gate }
3770Sstevel@tonic-gate 
3780Sstevel@tonic-gate /*
3790Sstevel@tonic-gate  * This function sets the value of a property specified by its name.
3800Sstevel@tonic-gate  */
3810Sstevel@tonic-gate static void
3820Sstevel@tonic-gate picld_set_attrval_by_name(picl_service_t *in)
3830Sstevel@tonic-gate {
3840Sstevel@tonic-gate 	picl_retsetattrvalbyname_t	ret;
3850Sstevel@tonic-gate 	int				err;
3860Sstevel@tonic-gate 	door_cred_t			cred;
3870Sstevel@tonic-gate 	picl_prophdl_t			ptreeh;
3880Sstevel@tonic-gate 	ptree_propinfo_t		pinfo;
3890Sstevel@tonic-gate 
3900Sstevel@tonic-gate 	if (door_cred(&cred) < 0)
3910Sstevel@tonic-gate 		picld_return_error(in->in.cnum, PICL_FAILURE);
3920Sstevel@tonic-gate 
3930Sstevel@tonic-gate 	err = cvt_picl2ptree(in->req_setattrvalbyname.nodeh, &ptreeh);
3940Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
3950Sstevel@tonic-gate 		picld_return_error(in->in.cnum, err);
3960Sstevel@tonic-gate 
3970Sstevel@tonic-gate 	err = xptree_get_propinfo_by_name(ptreeh,
3980Sstevel@tonic-gate 	    in->req_setattrvalbyname.propname, &pinfo);
3990Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
4000Sstevel@tonic-gate 		picld_return_error(in->in.cnum, err);
4010Sstevel@tonic-gate 
4020Sstevel@tonic-gate 	if (!(pinfo.piclinfo.accessmode & PICL_WRITE))
4030Sstevel@tonic-gate 		picld_return_error(in->in.cnum, PICL_NOTWRITABLE);
4040Sstevel@tonic-gate 
4050Sstevel@tonic-gate 	/*
4060Sstevel@tonic-gate 	 * For non-volatile prop, only super user can set its value.
4070Sstevel@tonic-gate 	 */
4080Sstevel@tonic-gate 	if (!(pinfo.piclinfo.accessmode & PICL_VOLATILE) &&
4090Sstevel@tonic-gate 	    (cred.dc_euid != SUPER_USER))
4100Sstevel@tonic-gate 		picld_return_error(in->in.cnum, PICL_PERMDENIED);
4110Sstevel@tonic-gate 
4120Sstevel@tonic-gate 	ret.cnum = PICL_CNUM_SETATTRVALBYNAME;
4130Sstevel@tonic-gate 	ret.nodeh = in->req_setattrvalbyname.nodeh;
4140Sstevel@tonic-gate 	(void) strcpy(ret.propname, in->req_setattrvalbyname.propname);
4150Sstevel@tonic-gate 
4160Sstevel@tonic-gate 	err = xptree_update_propval_by_name_with_cred(ptreeh,
4170Sstevel@tonic-gate 		in->req_setattrvalbyname.propname,
4180Sstevel@tonic-gate 		in->req_setattrvalbyname.valbuf,
4190Sstevel@tonic-gate 		(size_t)in->req_setattrvalbyname.bufsize,
4200Sstevel@tonic-gate 		cred);
4210Sstevel@tonic-gate 
4220Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
4230Sstevel@tonic-gate 		picld_return_error(in->in.cnum, err);
4240Sstevel@tonic-gate 
4250Sstevel@tonic-gate 	(void) rw_unlock(&init_lk);
4260Sstevel@tonic-gate 	(void) door_return((char *)&ret, sizeof (picl_retsetattrvalbyname_t),
4270Sstevel@tonic-gate 	    NULL, 0);
4280Sstevel@tonic-gate }
4290Sstevel@tonic-gate 
4300Sstevel@tonic-gate /*
4310Sstevel@tonic-gate  * This function returns the property information
4320Sstevel@tonic-gate  */
4330Sstevel@tonic-gate static void
4340Sstevel@tonic-gate picld_get_attrinfo(picl_service_t *in)
4350Sstevel@tonic-gate {
4360Sstevel@tonic-gate 	picl_retattrinfo_t	ret;
4370Sstevel@tonic-gate 	int			err;
4380Sstevel@tonic-gate 	ptree_propinfo_t	pinfo;
4390Sstevel@tonic-gate 	picl_prophdl_t		ptreeh;
4400Sstevel@tonic-gate 
4410Sstevel@tonic-gate 	err = cvt_picl2ptree(in->req_attrinfo.attr, &ptreeh);
4420Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
4430Sstevel@tonic-gate 		picld_return_error(in->in.cnum, err);
4440Sstevel@tonic-gate 
4450Sstevel@tonic-gate 	ret.cnum = PICL_CNUM_GETATTRINFO;
4460Sstevel@tonic-gate 	ret.attr = in->req_attrinfo.attr;
4470Sstevel@tonic-gate 
4480Sstevel@tonic-gate 	err = ptree_get_propinfo(ptreeh, &pinfo);
4490Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
4500Sstevel@tonic-gate 		picld_return_error(in->in.cnum, err);
4510Sstevel@tonic-gate 
4520Sstevel@tonic-gate 	ret.type = pinfo.piclinfo.type;
4530Sstevel@tonic-gate 	ret.accessmode = pinfo.piclinfo.accessmode;
4540Sstevel@tonic-gate 	ret.size = (uint32_t)pinfo.piclinfo.size;
4550Sstevel@tonic-gate 	(void) strcpy(ret.name, pinfo.piclinfo.name);
4560Sstevel@tonic-gate 	(void) rw_unlock(&init_lk);
4570Sstevel@tonic-gate 	(void) door_return((char *)&ret, sizeof (picl_retattrinfo_t), NULL, 0);
4580Sstevel@tonic-gate }
4590Sstevel@tonic-gate 
4600Sstevel@tonic-gate /*
4610Sstevel@tonic-gate  * This function returns the node's first property handle
4620Sstevel@tonic-gate  */
4630Sstevel@tonic-gate static void
4640Sstevel@tonic-gate picld_get_first_attr(picl_service_t *in)
4650Sstevel@tonic-gate {
4660Sstevel@tonic-gate 	picl_retfirstattr_t	ret;
4670Sstevel@tonic-gate 	int			err;
4680Sstevel@tonic-gate 	picl_prophdl_t		ptreeh;
4690Sstevel@tonic-gate 
4700Sstevel@tonic-gate 	err = cvt_picl2ptree(in->req_firstattr.nodeh, &ptreeh);
4710Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
4720Sstevel@tonic-gate 		picld_return_error(in->in.cnum, err);
4730Sstevel@tonic-gate 
4740Sstevel@tonic-gate 	ret.cnum = PICL_CNUM_GETFIRSTATTR;
4750Sstevel@tonic-gate 	ret.nodeh = in->req_firstattr.nodeh;
4760Sstevel@tonic-gate 
4770Sstevel@tonic-gate 	err = ptree_get_first_prop(ptreeh, &ret.attr);
4780Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
4790Sstevel@tonic-gate 		picld_return_error(in->in.cnum, err);
4800Sstevel@tonic-gate 	cvt_ptree2picl(&ret.attr);
4810Sstevel@tonic-gate 	(void) rw_unlock(&init_lk);
4820Sstevel@tonic-gate 	(void) door_return((char *)&ret, sizeof (picl_retfirstattr_t), NULL, 0);
4830Sstevel@tonic-gate }
4840Sstevel@tonic-gate 
4850Sstevel@tonic-gate /*
4860Sstevel@tonic-gate  * This function returns the next property handle in list
4870Sstevel@tonic-gate  */
4880Sstevel@tonic-gate static void
4890Sstevel@tonic-gate picld_get_next_attr(picl_service_t *in)
4900Sstevel@tonic-gate {
4910Sstevel@tonic-gate 	picl_retnextattr_t	ret;
4920Sstevel@tonic-gate 	int			err;
4930Sstevel@tonic-gate 	picl_prophdl_t		ptreeh;
4940Sstevel@tonic-gate 
4950Sstevel@tonic-gate 	err = cvt_picl2ptree(in->req_nextattr.attr, &ptreeh);
4960Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
4970Sstevel@tonic-gate 		picld_return_error(in->in.cnum, err);
4980Sstevel@tonic-gate 
4990Sstevel@tonic-gate 	ret.cnum = PICL_CNUM_GETNEXTATTR;
5000Sstevel@tonic-gate 	ret.attr = in->req_nextattr.attr;
5010Sstevel@tonic-gate 
5020Sstevel@tonic-gate 	err = ptree_get_next_prop(ptreeh, &ret.nextattr);
5030Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
5040Sstevel@tonic-gate 		picld_return_error(in->in.cnum, err);
5050Sstevel@tonic-gate 
5060Sstevel@tonic-gate 	cvt_ptree2picl(&ret.nextattr);
5070Sstevel@tonic-gate 
5080Sstevel@tonic-gate 	(void) rw_unlock(&init_lk);
5090Sstevel@tonic-gate 	(void) door_return((char *)&ret, sizeof (picl_retnextattr_t), NULL, 0);
5100Sstevel@tonic-gate }
5110Sstevel@tonic-gate 
5120Sstevel@tonic-gate /*
5130Sstevel@tonic-gate  * This function returns the handle of a property specified by its name
5140Sstevel@tonic-gate  */
5150Sstevel@tonic-gate static void
5160Sstevel@tonic-gate picld_get_attr_by_name(picl_service_t *in)
5170Sstevel@tonic-gate {
5180Sstevel@tonic-gate 	picl_retattrbyname_t	ret;
5190Sstevel@tonic-gate 	int			err;
5200Sstevel@tonic-gate 	picl_prophdl_t		ptreeh;
5210Sstevel@tonic-gate 
5220Sstevel@tonic-gate 	err = cvt_picl2ptree(in->req_attrbyname.nodeh, &ptreeh);
5230Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
5240Sstevel@tonic-gate 		picld_return_error(in->in.cnum, err);
5250Sstevel@tonic-gate 
5260Sstevel@tonic-gate 	ret.cnum = PICL_CNUM_GETATTRBYNAME;
5270Sstevel@tonic-gate 	ret.nodeh = in->req_attrbyname.nodeh;
5280Sstevel@tonic-gate 	(void) strcpy(ret.propname, in->req_attrbyname.propname);
5290Sstevel@tonic-gate 
5300Sstevel@tonic-gate 	err = ptree_get_prop_by_name(ptreeh, ret.propname, &ret.attr);
5310Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
5320Sstevel@tonic-gate 		picld_return_error(in->in.cnum, err);
5330Sstevel@tonic-gate 
5340Sstevel@tonic-gate 	cvt_ptree2picl(&ret.attr);
5350Sstevel@tonic-gate 	(void) rw_unlock(&init_lk);
5360Sstevel@tonic-gate 	(void) door_return((char *)&ret, sizeof (picl_retattrbyname_t), NULL,
5370Sstevel@tonic-gate 	    0);
5380Sstevel@tonic-gate }
5390Sstevel@tonic-gate 
5400Sstevel@tonic-gate /*
5410Sstevel@tonic-gate  * This function gets the next property on the same row in the table
5420Sstevel@tonic-gate  */
5430Sstevel@tonic-gate static void
5440Sstevel@tonic-gate picld_get_attr_by_row(picl_service_t *in)
5450Sstevel@tonic-gate {
5460Sstevel@tonic-gate 	picl_retattrbyrow_t	ret;
5470Sstevel@tonic-gate 	int			err;
5480Sstevel@tonic-gate 	picl_prophdl_t		ptreeh;
5490Sstevel@tonic-gate 
5500Sstevel@tonic-gate 	err = cvt_picl2ptree(in->req_attrbyrow.attr, &ptreeh);
5510Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
5520Sstevel@tonic-gate 		picld_return_error(in->in.cnum, err);
5530Sstevel@tonic-gate 
5540Sstevel@tonic-gate 	ret.cnum = PICL_CNUM_GETATTRBYROW;
5550Sstevel@tonic-gate 	ret.attr = in->req_attrbyrow.attr;
5560Sstevel@tonic-gate 
5570Sstevel@tonic-gate 	err = ptree_get_next_by_row(ptreeh, &ret.rowattr);
5580Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
5590Sstevel@tonic-gate 		picld_return_error(in->in.cnum, err);
5600Sstevel@tonic-gate 	cvt_ptree2picl(&ret.rowattr);
5610Sstevel@tonic-gate 
5620Sstevel@tonic-gate 	(void) rw_unlock(&init_lk);
5630Sstevel@tonic-gate 	(void) door_return((char *)&ret, sizeof (picl_retattrbyrow_t), NULL, 0);
5640Sstevel@tonic-gate }
5650Sstevel@tonic-gate 
5660Sstevel@tonic-gate /*
5670Sstevel@tonic-gate  * This function returns the handle of the next property in the same column
5680Sstevel@tonic-gate  * of the table.
5690Sstevel@tonic-gate  */
5700Sstevel@tonic-gate static void
5710Sstevel@tonic-gate picld_get_attr_by_col(picl_service_t *in)
5720Sstevel@tonic-gate {
5730Sstevel@tonic-gate 	picl_retattrbycol_t	ret;
5740Sstevel@tonic-gate 	int			err;
5750Sstevel@tonic-gate 	picl_prophdl_t		ptreeh;
5760Sstevel@tonic-gate 
5770Sstevel@tonic-gate 	err = cvt_picl2ptree(in->req_attrbycol.attr, &ptreeh);
5780Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
5790Sstevel@tonic-gate 		picld_return_error(in->in.cnum, err);
5800Sstevel@tonic-gate 
5810Sstevel@tonic-gate 	ret.cnum = PICL_CNUM_GETATTRBYCOL;
5820Sstevel@tonic-gate 	ret.attr = in->req_attrbycol.attr;
5830Sstevel@tonic-gate 
5840Sstevel@tonic-gate 	err = ptree_get_next_by_col(ptreeh, &ret.colattr);
5850Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
5860Sstevel@tonic-gate 		picld_return_error(in->in.cnum, err);
5870Sstevel@tonic-gate 
5880Sstevel@tonic-gate 	cvt_ptree2picl(&ret.colattr);
5890Sstevel@tonic-gate 
5900Sstevel@tonic-gate 	(void) rw_unlock(&init_lk);
5910Sstevel@tonic-gate 	(void) door_return((char *)&ret, sizeof (picl_retattrbycol_t), NULL, 0);
5920Sstevel@tonic-gate }
5930Sstevel@tonic-gate 
5940Sstevel@tonic-gate /*
5950Sstevel@tonic-gate  * This function finds the node in the PICLTREE that matches the given
5960Sstevel@tonic-gate  * criteria and returns its handle.
5970Sstevel@tonic-gate  */
5980Sstevel@tonic-gate static void
5990Sstevel@tonic-gate picld_find_node(picl_service_t *in)
6000Sstevel@tonic-gate {
6010Sstevel@tonic-gate 	picl_retfindnode_t	ret;
6020Sstevel@tonic-gate 	int			err;
6030Sstevel@tonic-gate 	picl_nodehdl_t		ptreeh;
6040Sstevel@tonic-gate 
6050Sstevel@tonic-gate 	err = cvt_picl2ptree(in->req_findnode.nodeh, &ptreeh);
6060Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
6070Sstevel@tonic-gate 		picld_return_error(in->in.cnum, err);
6080Sstevel@tonic-gate 
6090Sstevel@tonic-gate 	ret.cnum = PICL_CNUM_FINDNODE;
6100Sstevel@tonic-gate 
6110Sstevel@tonic-gate 	err = ptree_find_node(ptreeh, in->req_findnode.propname,
6120Sstevel@tonic-gate 	    in->req_findnode.ptype, in->req_findnode.valbuf,
6130Sstevel@tonic-gate 	    in->req_findnode.valsize, &ret.rnodeh);
6140Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
6150Sstevel@tonic-gate 		picld_return_error(in->in.cnum, err);
6160Sstevel@tonic-gate 
6170Sstevel@tonic-gate 	cvt_ptree2picl(&ret.rnodeh);
6180Sstevel@tonic-gate 
6190Sstevel@tonic-gate 	(void) rw_unlock(&init_lk);
6200Sstevel@tonic-gate 	(void) door_return((char *)&ret, sizeof (ret), NULL, 0);
6210Sstevel@tonic-gate }
6220Sstevel@tonic-gate 
6230Sstevel@tonic-gate /*
6240Sstevel@tonic-gate  * This function finds the property/node that corresponds to the given path
6250Sstevel@tonic-gate  * and returns its handle
6260Sstevel@tonic-gate  */
6270Sstevel@tonic-gate static void
6280Sstevel@tonic-gate picld_get_node_by_path(picl_service_t *in)
6290Sstevel@tonic-gate {
6300Sstevel@tonic-gate 	picl_retnodebypath_t	ret;
6310Sstevel@tonic-gate 	int			err;
6320Sstevel@tonic-gate 
6330Sstevel@tonic-gate 	ret.cnum = PICL_CNUM_NODEBYPATH;
6340Sstevel@tonic-gate 	err = ptree_get_node_by_path(in->req_nodebypath.pathbuf, &ret.nodeh);
6350Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
6360Sstevel@tonic-gate 		picld_return_error(in->in.cnum, err);
6370Sstevel@tonic-gate 	cvt_ptree2picl(&ret.nodeh);
6380Sstevel@tonic-gate 	(void) rw_unlock(&init_lk);
6390Sstevel@tonic-gate 	(void) door_return((char *)&ret, sizeof (ret), NULL, 0);
6400Sstevel@tonic-gate }
6410Sstevel@tonic-gate 
6420Sstevel@tonic-gate /*
6430Sstevel@tonic-gate  * This function returns finds the frutree parent node for a given node
6440Sstevel@tonic-gate  * and returns its handle
6450Sstevel@tonic-gate  */
6460Sstevel@tonic-gate static void
6470Sstevel@tonic-gate picld_get_frutree_parent(picl_service_t *in)
6480Sstevel@tonic-gate {
6490Sstevel@tonic-gate 	picl_retfruparent_t	ret;
6500Sstevel@tonic-gate 	int			err;
6510Sstevel@tonic-gate 	picl_nodehdl_t		ptreeh;
6520Sstevel@tonic-gate 
6530Sstevel@tonic-gate 	err = cvt_picl2ptree(in->req_fruparent.devh, &ptreeh);
6540Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
6550Sstevel@tonic-gate 		picld_return_error(in->in.cnum, err);
6560Sstevel@tonic-gate 
6570Sstevel@tonic-gate 	ret.cnum = PICL_CNUM_FRUTREEPARENT;
6580Sstevel@tonic-gate 
6590Sstevel@tonic-gate 	err = ptree_get_frutree_parent(ptreeh, &ret.fruh);
6600Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
6610Sstevel@tonic-gate 		picld_return_error(in->in.cnum, err);
6620Sstevel@tonic-gate 	cvt_ptree2picl(&ret.fruh);
6630Sstevel@tonic-gate 
6640Sstevel@tonic-gate 	(void) rw_unlock(&init_lk);
6650Sstevel@tonic-gate 	(void) door_return((char *)&ret, sizeof (ret), NULL, 0);
6660Sstevel@tonic-gate }
6670Sstevel@tonic-gate 
6680Sstevel@tonic-gate /*
6690Sstevel@tonic-gate  * This function is called when an unknown client request is received.
6700Sstevel@tonic-gate  */
6710Sstevel@tonic-gate static void
6720Sstevel@tonic-gate picld_unknown_service(picl_service_t *in)
6730Sstevel@tonic-gate {
6740Sstevel@tonic-gate 	picld_return_error(in->in.cnum, PICL_UNKNOWNSERVICE);
6750Sstevel@tonic-gate }
6760Sstevel@tonic-gate 
6770Sstevel@tonic-gate static void
6780Sstevel@tonic-gate check_denial_of_service(int cnum)
6790Sstevel@tonic-gate {
6800Sstevel@tonic-gate 	hrtime_t	window;
6810Sstevel@tonic-gate 	hrtime_t	current;
6820Sstevel@tonic-gate 	int		dos_flag;
6830Sstevel@tonic-gate 
6840Sstevel@tonic-gate 	current = gethrtime();
6850Sstevel@tonic-gate 	dos_flag = 0;
6860Sstevel@tonic-gate 
6870Sstevel@tonic-gate 	if (pthread_mutex_lock(&dos_mutex) != 0)
6880Sstevel@tonic-gate 		picld_return_error(cnum, PICL_FAILURE);
6890Sstevel@tonic-gate 
6900Sstevel@tonic-gate 	++service_requests;
6910Sstevel@tonic-gate 	window = current - orig_time;
6920Sstevel@tonic-gate 	if (window > MILLI_TO_NANO(sliding_interval_ms)) {
6930Sstevel@tonic-gate 		orig_time = current;
6940Sstevel@tonic-gate 		service_requests = 1;
6950Sstevel@tonic-gate 	}
6960Sstevel@tonic-gate 
6970Sstevel@tonic-gate 	if (service_requests > dos_req_limit)
6980Sstevel@tonic-gate 		dos_flag = 1;
6990Sstevel@tonic-gate 
7000Sstevel@tonic-gate 	if (pthread_mutex_unlock(&dos_mutex) != 0)
7010Sstevel@tonic-gate 		picld_return_error(cnum, PICL_FAILURE);
7020Sstevel@tonic-gate 
7030Sstevel@tonic-gate 	if (dos_flag)
7040Sstevel@tonic-gate 		(void) poll(NULL, 0, dos_ms);
7050Sstevel@tonic-gate }
7060Sstevel@tonic-gate 
7070Sstevel@tonic-gate 
7080Sstevel@tonic-gate /* ARGSUSED */
7090Sstevel@tonic-gate static void
7100Sstevel@tonic-gate picld_door_handler(void *cookie, char *argp, size_t asize,
7110Sstevel@tonic-gate     door_desc_t *dp, uint_t n_desc)
7120Sstevel@tonic-gate {
7130Sstevel@tonic-gate 	picl_service_t  *req;
7140Sstevel@tonic-gate 
7150Sstevel@tonic-gate 	/*LINTED*/
7160Sstevel@tonic-gate 	req = (picl_service_t *)argp;
7170Sstevel@tonic-gate 
7180Sstevel@tonic-gate 	if (req == NULL)
7190Sstevel@tonic-gate 		(void) door_return((char *)req, 0, NULL, 0);
7200Sstevel@tonic-gate 
7210Sstevel@tonic-gate 	check_denial_of_service(req->in.cnum);
7220Sstevel@tonic-gate 
7230Sstevel@tonic-gate 	(void) rw_rdlock(&init_lk);
7240Sstevel@tonic-gate 	switch (req->in.cnum) {	/* client call number */
7250Sstevel@tonic-gate 	case PICL_CNUM_INIT:
7260Sstevel@tonic-gate 		/*LINTED*/
7270Sstevel@tonic-gate 		picld_init((picl_service_t *)argp);
7280Sstevel@tonic-gate 		break;
7290Sstevel@tonic-gate 	case PICL_CNUM_FINI:
7300Sstevel@tonic-gate 		/*LINTED*/
7310Sstevel@tonic-gate 		picld_fini((picl_service_t *)argp);
7320Sstevel@tonic-gate 		break;
7330Sstevel@tonic-gate 	case PICL_CNUM_GETROOT:
7340Sstevel@tonic-gate 		/*LINTED*/
7350Sstevel@tonic-gate 		picld_getroot((picl_service_t *)argp);
7360Sstevel@tonic-gate 		break;
7370Sstevel@tonic-gate 	case PICL_CNUM_GETATTRVAL:
7380Sstevel@tonic-gate 		/*LINTED*/
7390Sstevel@tonic-gate 		picld_get_attrval((picl_service_t *)argp);
7400Sstevel@tonic-gate 		break;
7410Sstevel@tonic-gate 	case PICL_CNUM_GETATTRVALBYNAME:
7420Sstevel@tonic-gate 		/*LINTED*/
7430Sstevel@tonic-gate 		picld_get_attrval_by_name((picl_service_t *)argp);
7440Sstevel@tonic-gate 		break;
7450Sstevel@tonic-gate 	case PICL_CNUM_GETATTRINFO:
7460Sstevel@tonic-gate 		/*LINTED*/
7470Sstevel@tonic-gate 		picld_get_attrinfo((picl_service_t *)argp);
7480Sstevel@tonic-gate 		break;
7490Sstevel@tonic-gate 	case PICL_CNUM_GETFIRSTATTR:
7500Sstevel@tonic-gate 		/*LINTED*/
7510Sstevel@tonic-gate 		picld_get_first_attr((picl_service_t *)argp);
7520Sstevel@tonic-gate 		break;
7530Sstevel@tonic-gate 	case PICL_CNUM_GETNEXTATTR:
7540Sstevel@tonic-gate 		/*LINTED*/
7550Sstevel@tonic-gate 		picld_get_next_attr((picl_service_t *)argp);
7560Sstevel@tonic-gate 		break;
7570Sstevel@tonic-gate 	case PICL_CNUM_GETATTRBYNAME:
7580Sstevel@tonic-gate 		/*LINTED*/
7590Sstevel@tonic-gate 		picld_get_attr_by_name((picl_service_t *)argp);
7600Sstevel@tonic-gate 		break;
7610Sstevel@tonic-gate 	case PICL_CNUM_GETATTRBYROW:
7620Sstevel@tonic-gate 		/*LINTED*/
7630Sstevel@tonic-gate 		picld_get_attr_by_row((picl_service_t *)argp);
7640Sstevel@tonic-gate 		break;
7650Sstevel@tonic-gate 	case PICL_CNUM_GETATTRBYCOL:
7660Sstevel@tonic-gate 		/*LINTED*/
7670Sstevel@tonic-gate 		picld_get_attr_by_col((picl_service_t *)argp);
7680Sstevel@tonic-gate 		break;
7690Sstevel@tonic-gate 	case PICL_CNUM_SETATTRVAL:
7700Sstevel@tonic-gate 		/*LINTED*/
7710Sstevel@tonic-gate 		picld_set_attrval((picl_service_t *)argp);
7720Sstevel@tonic-gate 		break;
7730Sstevel@tonic-gate 	case PICL_CNUM_SETATTRVALBYNAME:
7740Sstevel@tonic-gate 		/*LINTED*/
7750Sstevel@tonic-gate 		picld_set_attrval_by_name((picl_service_t *)argp);
7760Sstevel@tonic-gate 		break;
7770Sstevel@tonic-gate 	case PICL_CNUM_PING:
7780Sstevel@tonic-gate 		/*LINTED*/
7790Sstevel@tonic-gate 		picld_ping((picl_service_t *)argp);
7800Sstevel@tonic-gate 		break;
7810Sstevel@tonic-gate 	case PICL_CNUM_WAIT:
7820Sstevel@tonic-gate 		/*LINTED*/
7830Sstevel@tonic-gate 		picld_wait((picl_service_t *)argp);
7840Sstevel@tonic-gate 		break;
7850Sstevel@tonic-gate 	case PICL_CNUM_FINDNODE:
7860Sstevel@tonic-gate 		/*LINTED*/
7870Sstevel@tonic-gate 		picld_find_node((picl_service_t *)argp);
7880Sstevel@tonic-gate 		break;
7890Sstevel@tonic-gate 	case PICL_CNUM_NODEBYPATH:
7900Sstevel@tonic-gate 		/*LINTED*/
7910Sstevel@tonic-gate 		picld_get_node_by_path((picl_service_t *)argp);
7920Sstevel@tonic-gate 		break;
7930Sstevel@tonic-gate 	case PICL_CNUM_FRUTREEPARENT:
7940Sstevel@tonic-gate 		/*LINTED*/
7950Sstevel@tonic-gate 		picld_get_frutree_parent((picl_service_t *)argp);
7960Sstevel@tonic-gate 		break;
7970Sstevel@tonic-gate 	default:
7980Sstevel@tonic-gate 		/*LINTED*/
7990Sstevel@tonic-gate 		picld_unknown_service((picl_service_t *)argp);
8000Sstevel@tonic-gate 		break;
8010Sstevel@tonic-gate 	};
8020Sstevel@tonic-gate 	/*NOTREACHED*/
8030Sstevel@tonic-gate }
8040Sstevel@tonic-gate 
8050Sstevel@tonic-gate /* ARGSUSED */
8060Sstevel@tonic-gate static void
8070Sstevel@tonic-gate hup_handler(int sig, siginfo_t *siginfo, void *sigctx)
8080Sstevel@tonic-gate {
8090Sstevel@tonic-gate 	doreinit = 1;
8100Sstevel@tonic-gate }
8110Sstevel@tonic-gate 
8120Sstevel@tonic-gate /*
8130Sstevel@tonic-gate  * "ping" to see if a daemon is already running
8140Sstevel@tonic-gate  */
8150Sstevel@tonic-gate static int
8160Sstevel@tonic-gate daemon_exists(void)
8170Sstevel@tonic-gate {
8180Sstevel@tonic-gate 	door_arg_t	darg;
8190Sstevel@tonic-gate 	picl_reqping_t	req_ping;
8200Sstevel@tonic-gate 	picl_retping_t	ret_ping;
8210Sstevel@tonic-gate 	int		doorh;
8220Sstevel@tonic-gate 	door_info_t	dinfo;
8230Sstevel@tonic-gate 
8240Sstevel@tonic-gate 	doorh = open(PICLD_DOOR, O_RDONLY);
8250Sstevel@tonic-gate 	if (doorh < 0)
8260Sstevel@tonic-gate 		return (0);
8270Sstevel@tonic-gate 
8280Sstevel@tonic-gate 	if (door_info(doorh, &dinfo) < 0) {
8290Sstevel@tonic-gate 		(void) close(doorh);
8300Sstevel@tonic-gate 		return (0);
8310Sstevel@tonic-gate 	}
8320Sstevel@tonic-gate 
8330Sstevel@tonic-gate 	if ((dinfo.di_attributes & DOOR_REVOKED) ||
834*1016Sraf 	    (dinfo.di_data != (uintptr_t)PICLD_DOOR_COOKIE)) {
8350Sstevel@tonic-gate 		(void) close(doorh);
8360Sstevel@tonic-gate 		return (0);
8370Sstevel@tonic-gate 	}
8380Sstevel@tonic-gate 
8390Sstevel@tonic-gate 	if (dinfo.di_target != getpid()) {
8400Sstevel@tonic-gate 		(void) close(doorh);
8410Sstevel@tonic-gate 		return (1);
8420Sstevel@tonic-gate 	}
8430Sstevel@tonic-gate 
8440Sstevel@tonic-gate 	req_ping.cnum = PICL_CNUM_PING;
8450Sstevel@tonic-gate 
8460Sstevel@tonic-gate 	darg.data_ptr = (char *)&req_ping;
8470Sstevel@tonic-gate 	darg.data_size = sizeof (picl_reqping_t);
8480Sstevel@tonic-gate 	darg.desc_ptr = NULL;
8490Sstevel@tonic-gate 	darg.desc_num = 0;
8500Sstevel@tonic-gate 	darg.rbuf = (char *)&ret_ping;
8510Sstevel@tonic-gate 	darg.rsize = sizeof (picl_retping_t);
8520Sstevel@tonic-gate 
8530Sstevel@tonic-gate 	if (door_call(doorh, &darg) < 0) {
8540Sstevel@tonic-gate 		(void) close(doorh);
8550Sstevel@tonic-gate 		return (0);
8560Sstevel@tonic-gate 	}
8570Sstevel@tonic-gate 
8580Sstevel@tonic-gate 	(void) close(doorh);
8590Sstevel@tonic-gate 	return (1);
8600Sstevel@tonic-gate }
8610Sstevel@tonic-gate 
8620Sstevel@tonic-gate /*
8630Sstevel@tonic-gate  * Create the picld door
8640Sstevel@tonic-gate  */
8650Sstevel@tonic-gate static int
8660Sstevel@tonic-gate setup_door(void)
8670Sstevel@tonic-gate {
8680Sstevel@tonic-gate 	struct stat	stbuf;
8690Sstevel@tonic-gate 
8700Sstevel@tonic-gate 	/*
8710Sstevel@tonic-gate 	 * Create the door
8720Sstevel@tonic-gate 	 */
8730Sstevel@tonic-gate 	door_id = door_create(picld_door_handler, PICLD_DOOR_COOKIE,
8740Sstevel@tonic-gate 	    DOOR_UNREF | DOOR_REFUSE_DESC | DOOR_NO_CANCEL);
8750Sstevel@tonic-gate 
8760Sstevel@tonic-gate 	if (door_id < 0) {
8770Sstevel@tonic-gate 		return (-1);
8780Sstevel@tonic-gate 	}
8790Sstevel@tonic-gate 
8800Sstevel@tonic-gate 	if (stat(PICLD_DOOR, &stbuf) < 0) {
8810Sstevel@tonic-gate 		int newfd;
8820Sstevel@tonic-gate 		if ((newfd = creat(PICLD_DOOR, 0444)) < 0)
8830Sstevel@tonic-gate 			return (-1);
8840Sstevel@tonic-gate 		(void) close(newfd);
8850Sstevel@tonic-gate 	}
8860Sstevel@tonic-gate 
8870Sstevel@tonic-gate 	if (fattach(door_id, PICLD_DOOR) < 0) {
8880Sstevel@tonic-gate 		if ((errno != EBUSY) ||
8890Sstevel@tonic-gate 		    (fdetach(PICLD_DOOR) < 0) ||
8900Sstevel@tonic-gate 		    (fattach(door_id, PICLD_DOOR) < 0))
8910Sstevel@tonic-gate 			return (-1);
8920Sstevel@tonic-gate 	}
8930Sstevel@tonic-gate 
8940Sstevel@tonic-gate 	return (0);
8950Sstevel@tonic-gate }
8960Sstevel@tonic-gate 
8970Sstevel@tonic-gate /*
8980Sstevel@tonic-gate  * Main function of picl daemon
8990Sstevel@tonic-gate  */
9000Sstevel@tonic-gate int
9010Sstevel@tonic-gate main(int argc, char **argv)
9020Sstevel@tonic-gate {
9030Sstevel@tonic-gate 	struct	sigaction	act;
9040Sstevel@tonic-gate 	int			c;
9050Sstevel@tonic-gate 	sigset_t		ublk;
9060Sstevel@tonic-gate 
9070Sstevel@tonic-gate 
9080Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
9090Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
9100Sstevel@tonic-gate 
9110Sstevel@tonic-gate 	if (getuid() != 0) {
9120Sstevel@tonic-gate 		syslog(LOG_CRIT, MUST_BE_ROOT);
9130Sstevel@tonic-gate 		return (0);
9140Sstevel@tonic-gate 	}
9150Sstevel@tonic-gate 
9160Sstevel@tonic-gate 	(void) rwlock_init(&init_lk, USYNC_THREAD, NULL);
9170Sstevel@tonic-gate 	doreinit = 0;
9180Sstevel@tonic-gate 	logflag = 1;
9190Sstevel@tonic-gate 	dos_req_limit = DOS_PICL_REQUESTS_LIMIT;
9200Sstevel@tonic-gate 	sliding_interval_ms = SLIDING_INTERVAL_MILLISECONDS;
9210Sstevel@tonic-gate 	dos_ms = DOS_SLEEPTIME_MS;
9220Sstevel@tonic-gate 	verbose_level = 0;
9230Sstevel@tonic-gate 
9240Sstevel@tonic-gate 	/*
9250Sstevel@tonic-gate 	 * parse arguments
9260Sstevel@tonic-gate 	 */
9270Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "is:t:l:r:v:d:")) != EOF) {
9280Sstevel@tonic-gate 		switch (c) {
9290Sstevel@tonic-gate 		case 'd':
9300Sstevel@tonic-gate 			dos_ms = strtol(optarg, (char **)NULL, 0);
9310Sstevel@tonic-gate 			break;
9320Sstevel@tonic-gate 		case 'i':
9330Sstevel@tonic-gate 			logflag = 0;
9340Sstevel@tonic-gate 			break;
9350Sstevel@tonic-gate 		case 's':
9360Sstevel@tonic-gate 			sliding_interval_ms = strtoll(optarg, (char **)NULL, 0);
9370Sstevel@tonic-gate 			break;
9380Sstevel@tonic-gate 		case 't':
9390Sstevel@tonic-gate 			dos_req_limit = strtol(optarg, (char **)NULL, 0);
9400Sstevel@tonic-gate 			break;
9410Sstevel@tonic-gate 		case 'v':
9420Sstevel@tonic-gate 			verbose_level = strtol(optarg, (char **)NULL, 0);
9430Sstevel@tonic-gate 			logflag = 0;
9440Sstevel@tonic-gate 			break;
9450Sstevel@tonic-gate 		default:
9460Sstevel@tonic-gate 			break;
9470Sstevel@tonic-gate 		}
9480Sstevel@tonic-gate 	}
9490Sstevel@tonic-gate 
9500Sstevel@tonic-gate 	orig_time = gethrtime();
9510Sstevel@tonic-gate 
9520Sstevel@tonic-gate 	/*
9530Sstevel@tonic-gate 	 * is there a daemon already running?
9540Sstevel@tonic-gate 	 */
9550Sstevel@tonic-gate 
9560Sstevel@tonic-gate 	if (daemon_exists()) {
9570Sstevel@tonic-gate 		syslog(LOG_CRIT, DAEMON_RUNNING);
9580Sstevel@tonic-gate 		exit(1);
9590Sstevel@tonic-gate 	}
9600Sstevel@tonic-gate 
9610Sstevel@tonic-gate 	/*
9620Sstevel@tonic-gate 	 * Mask off/block SIGALRM signal so that the environmental plug-in
9630Sstevel@tonic-gate 	 * (piclenvd) can use it to simulate sleep() without being affected
9640Sstevel@tonic-gate 	 * by time being set back. No other PICL plug-in should use SIGALRM
9650Sstevel@tonic-gate 	 * or alarm() for now.
9660Sstevel@tonic-gate 	 */
9670Sstevel@tonic-gate 	(void) sigemptyset(&ublk);
9680Sstevel@tonic-gate 	(void) sigaddset(&ublk, SIGALRM);
9690Sstevel@tonic-gate 	(void) sigprocmask(SIG_BLOCK, &ublk, NULL);
9700Sstevel@tonic-gate 
9710Sstevel@tonic-gate 	/*
9720Sstevel@tonic-gate 	 * Ignore SIGHUP until all the initialization is done.
9730Sstevel@tonic-gate 	 */
9740Sstevel@tonic-gate 	act.sa_handler = SIG_IGN;
9750Sstevel@tonic-gate 	(void) sigemptyset(&act.sa_mask);
9760Sstevel@tonic-gate 	act.sa_flags = 0;
9770Sstevel@tonic-gate 	if (sigaction(SIGHUP, &act, NULL) == -1)
9780Sstevel@tonic-gate 		syslog(LOG_ERR, SIGACT_FAILED, strsignal(SIGHUP),
9790Sstevel@tonic-gate 		    strerror(errno));
9800Sstevel@tonic-gate 
9810Sstevel@tonic-gate 	if (logflag != 0) {	/* daemonize */
9820Sstevel@tonic-gate 		pid_t pid;
9830Sstevel@tonic-gate 
9840Sstevel@tonic-gate 		pid = fork();
9850Sstevel@tonic-gate 		if (pid < 0)
9860Sstevel@tonic-gate 			exit(1);
9870Sstevel@tonic-gate 		if (pid > 0)
9880Sstevel@tonic-gate 			/* parent */
9890Sstevel@tonic-gate 			exit(0);
9900Sstevel@tonic-gate 
9910Sstevel@tonic-gate 		/* child */
9920Sstevel@tonic-gate 		if (chdir("/") == -1) {
9930Sstevel@tonic-gate 			syslog(LOG_CRIT, CD_ROOT_FAILED);
9940Sstevel@tonic-gate 			exit(1);
9950Sstevel@tonic-gate 		}
9960Sstevel@tonic-gate 
9970Sstevel@tonic-gate 		(void) setsid();
9980Sstevel@tonic-gate 		(void) close(STDIN_FILENO);
9990Sstevel@tonic-gate 		(void) close(STDOUT_FILENO);
10000Sstevel@tonic-gate 		(void) close(STDERR_FILENO);
10010Sstevel@tonic-gate 		(void) open("/dev/null", O_RDWR, 0);
10020Sstevel@tonic-gate 		(void) dup2(STDIN_FILENO, STDOUT_FILENO);
10030Sstevel@tonic-gate 		(void) dup2(STDIN_FILENO, STDERR_FILENO);
10040Sstevel@tonic-gate 		openlog(PICLD, LOG_PID, LOG_DAEMON);
10050Sstevel@tonic-gate 	}
10060Sstevel@tonic-gate 
10070Sstevel@tonic-gate 	/*
10080Sstevel@tonic-gate 	 * Initialize the PICL Tree
10090Sstevel@tonic-gate 	 */
10100Sstevel@tonic-gate 	if (xptree_initialize(NULL) != PICL_SUCCESS) {
10110Sstevel@tonic-gate 		syslog(LOG_CRIT, INIT_FAILED);
10120Sstevel@tonic-gate 		exit(1);
10130Sstevel@tonic-gate 	}
10140Sstevel@tonic-gate 
10150Sstevel@tonic-gate 	if (setup_door()) {
10160Sstevel@tonic-gate 		syslog(LOG_CRIT, DOOR_FAILED);
10170Sstevel@tonic-gate 		exit(1);
10180Sstevel@tonic-gate 	}
10190Sstevel@tonic-gate 
10200Sstevel@tonic-gate 	/*
10210Sstevel@tonic-gate 	 * setup signal handlers for post-init
10220Sstevel@tonic-gate 	 */
10230Sstevel@tonic-gate 	act.sa_sigaction = hup_handler;
10240Sstevel@tonic-gate 	(void) sigemptyset(&act.sa_mask);
10250Sstevel@tonic-gate 	act.sa_flags = SA_SIGINFO;
10260Sstevel@tonic-gate 	if (sigaction(SIGHUP, &act, NULL) == -1)
10270Sstevel@tonic-gate 		syslog(LOG_ERR, SIGACT_FAILED, strsignal(SIGHUP),
10280Sstevel@tonic-gate 		    strerror(errno));
10290Sstevel@tonic-gate 
10300Sstevel@tonic-gate 	/*
10310Sstevel@tonic-gate 	 * wait for requests
10320Sstevel@tonic-gate 	 */
10330Sstevel@tonic-gate 	for (;;) {
10340Sstevel@tonic-gate 		(void) pause();
10350Sstevel@tonic-gate 		if (doreinit) {
10360Sstevel@tonic-gate 			/*
10370Sstevel@tonic-gate 			 * Block SIGHUP during reinitialization.
10380Sstevel@tonic-gate 			 * Also mask off/block SIGALRM signal so that the
10390Sstevel@tonic-gate 			 * environmental plug-in (piclenvd) can use it to
10400Sstevel@tonic-gate 			 * simulate sleep() without being affected by time
10410Sstevel@tonic-gate 			 * being set back. No ohter PICL plug-in should use
10420Sstevel@tonic-gate 			 * SIGALRM or alarm() for now.
10430Sstevel@tonic-gate 			 */
10440Sstevel@tonic-gate 			(void) sigemptyset(&ublk);
10450Sstevel@tonic-gate 			(void) sigaddset(&ublk, SIGHUP);
10460Sstevel@tonic-gate 			(void) sigaddset(&ublk, SIGALRM);
10470Sstevel@tonic-gate 			(void) sigprocmask(SIG_BLOCK, &ublk, NULL);
10480Sstevel@tonic-gate 			(void) sigdelset(&ublk, SIGALRM);
10490Sstevel@tonic-gate 			doreinit = 0;
10500Sstevel@tonic-gate 			(void) rw_wrlock(&init_lk);
10510Sstevel@tonic-gate 			xptree_destroy();
10520Sstevel@tonic-gate 			(void) xptree_reinitialize();
10530Sstevel@tonic-gate 			(void) rw_unlock(&init_lk);
10540Sstevel@tonic-gate 			(void) sigprocmask(SIG_UNBLOCK, &ublk, NULL);
10550Sstevel@tonic-gate 		}
10560Sstevel@tonic-gate 	}
10570Sstevel@tonic-gate }
1058