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