1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate /*
30*0Sstevel@tonic-gate  * PICL daemon
31*0Sstevel@tonic-gate  */
32*0Sstevel@tonic-gate 
33*0Sstevel@tonic-gate #include <stdio.h>
34*0Sstevel@tonic-gate #include <stdlib.h>
35*0Sstevel@tonic-gate #include <stdarg.h>
36*0Sstevel@tonic-gate #include <string.h>
37*0Sstevel@tonic-gate #include <libintl.h>
38*0Sstevel@tonic-gate #include <locale.h>
39*0Sstevel@tonic-gate #include <alloca.h>
40*0Sstevel@tonic-gate #include <errno.h>
41*0Sstevel@tonic-gate #include <assert.h>
42*0Sstevel@tonic-gate #include <stropts.h>
43*0Sstevel@tonic-gate #include <unistd.h>
44*0Sstevel@tonic-gate #include <signal.h>
45*0Sstevel@tonic-gate #include <pthread.h>
46*0Sstevel@tonic-gate #include <synch.h>
47*0Sstevel@tonic-gate #include <door.h>
48*0Sstevel@tonic-gate #include <sys/door.h>
49*0Sstevel@tonic-gate #include <fcntl.h>
50*0Sstevel@tonic-gate #include <dlfcn.h>
51*0Sstevel@tonic-gate #include <time.h>
52*0Sstevel@tonic-gate #include <sys/utsname.h>
53*0Sstevel@tonic-gate #include <sys/systeminfo.h>
54*0Sstevel@tonic-gate #include <sys/stat.h>
55*0Sstevel@tonic-gate #include <sys/wait.h>
56*0Sstevel@tonic-gate #include <dirent.h>
57*0Sstevel@tonic-gate #include <syslog.h>
58*0Sstevel@tonic-gate #include <poll.h>
59*0Sstevel@tonic-gate #include <limits.h>
60*0Sstevel@tonic-gate #include <picl.h>
61*0Sstevel@tonic-gate #include "picl2door.h"
62*0Sstevel@tonic-gate #include <picltree.h>
63*0Sstevel@tonic-gate #include "ptree_impl.h"
64*0Sstevel@tonic-gate 
65*0Sstevel@tonic-gate /*
66*0Sstevel@tonic-gate  * Log text messages
67*0Sstevel@tonic-gate  */
68*0Sstevel@tonic-gate #define	MUST_BE_ROOT	gettext("this program must be run as root\n")
69*0Sstevel@tonic-gate #define	CD_ROOT_FAILED	gettext("chdir to root failed\n")
70*0Sstevel@tonic-gate #define	INIT_FAILED	gettext("ptree initialization failed\n")
71*0Sstevel@tonic-gate #define	DAEMON_RUNNING	gettext("PICL daemon already running\n")
72*0Sstevel@tonic-gate #define	DOOR_FAILED	gettext("Failed creating picld door\n")
73*0Sstevel@tonic-gate #define	SIGACT_FAILED	\
74*0Sstevel@tonic-gate 		gettext("Failed to install signal handler for %s: %s\n")
75*0Sstevel@tonic-gate 
76*0Sstevel@tonic-gate /*
77*0Sstevel@tonic-gate  * Constants
78*0Sstevel@tonic-gate  */
79*0Sstevel@tonic-gate #define	PICLD				"picld"
80*0Sstevel@tonic-gate #define	DOS_PICL_REQUESTS_LIMIT		10000
81*0Sstevel@tonic-gate #define	SLIDING_INTERVAL_MILLISECONDS	1000
82*0Sstevel@tonic-gate #define	PICLD_MAJOR_REV			0x1
83*0Sstevel@tonic-gate #define	PICLD_MINOR_REV			0x0
84*0Sstevel@tonic-gate #define	DOS_SLEEPTIME_MS		1000
85*0Sstevel@tonic-gate 
86*0Sstevel@tonic-gate /*
87*0Sstevel@tonic-gate  * Macros
88*0Sstevel@tonic-gate  */
89*0Sstevel@tonic-gate #define	PICLD_VERSION(x, y)	((x << 8) | y)
90*0Sstevel@tonic-gate #define	PICL_CLIENT_REV(x)	(x & 0xff)
91*0Sstevel@tonic-gate #define	MILLI_TO_NANO(x)	(x * 1000000)
92*0Sstevel@tonic-gate 
93*0Sstevel@tonic-gate extern	char	**environ;
94*0Sstevel@tonic-gate 
95*0Sstevel@tonic-gate /*
96*0Sstevel@tonic-gate  * Module Variables
97*0Sstevel@tonic-gate  */
98*0Sstevel@tonic-gate static	int		logflag = 1;
99*0Sstevel@tonic-gate static	int		doreinit = 0;
100*0Sstevel@tonic-gate static	int		door_id = -1;
101*0Sstevel@tonic-gate static  int 		service_requests = 0;
102*0Sstevel@tonic-gate static	hrtime_t	orig_time;
103*0Sstevel@tonic-gate static	hrtime_t	sliding_interval_ms;
104*0Sstevel@tonic-gate static	uint32_t	dos_req_limit;
105*0Sstevel@tonic-gate static	uint32_t	dos_ms;
106*0Sstevel@tonic-gate static	pthread_mutex_t	dos_mutex = PTHREAD_MUTEX_INITIALIZER;
107*0Sstevel@tonic-gate static	rwlock_t	init_lk;
108*0Sstevel@tonic-gate 
109*0Sstevel@tonic-gate /*
110*0Sstevel@tonic-gate  * This returns an error message to libpicl
111*0Sstevel@tonic-gate  */
112*0Sstevel@tonic-gate static void
113*0Sstevel@tonic-gate picld_return_error(picl_callnumber_t cnum, picl_errno_t err)
114*0Sstevel@tonic-gate {
115*0Sstevel@tonic-gate 	picl_reterror_t	ret_error;
116*0Sstevel@tonic-gate 
117*0Sstevel@tonic-gate 	ret_error.cnum = PICL_CNUM_ERROR;
118*0Sstevel@tonic-gate 	ret_error.in_cnum = cnum;
119*0Sstevel@tonic-gate 	ret_error.errnum = err;
120*0Sstevel@tonic-gate 	(void) rw_unlock(&init_lk);
121*0Sstevel@tonic-gate 	(void) door_return((char *)&ret_error, sizeof (picl_reterror_t), NULL,
122*0Sstevel@tonic-gate 	    0);
123*0Sstevel@tonic-gate }
124*0Sstevel@tonic-gate 
125*0Sstevel@tonic-gate /*
126*0Sstevel@tonic-gate  * picld_init is called when a picl_initialize request is received
127*0Sstevel@tonic-gate  */
128*0Sstevel@tonic-gate static void
129*0Sstevel@tonic-gate picld_init(picl_service_t *req)
130*0Sstevel@tonic-gate {
131*0Sstevel@tonic-gate 	picl_retinit_t	ret_init;
132*0Sstevel@tonic-gate 	int	clmajrev;
133*0Sstevel@tonic-gate 
134*0Sstevel@tonic-gate 	clmajrev = PICL_CLIENT_REV(req->req_init.clrev);
135*0Sstevel@tonic-gate 
136*0Sstevel@tonic-gate 	if (clmajrev < PICL_VERSION_1)
137*0Sstevel@tonic-gate 		picld_return_error(req->req_init.cnum, PICL_NOTSUPPORTED);
138*0Sstevel@tonic-gate 
139*0Sstevel@tonic-gate 	ret_init.cnum = req->req_init.cnum;
140*0Sstevel@tonic-gate 	ret_init.rev = PICLD_VERSION(PICLD_MAJOR_REV, PICLD_MINOR_REV);
141*0Sstevel@tonic-gate 
142*0Sstevel@tonic-gate 	(void) rw_unlock(&init_lk);
143*0Sstevel@tonic-gate 	(void) door_return((char *)&ret_init, sizeof (picl_retinit_t), NULL, 0);
144*0Sstevel@tonic-gate }
145*0Sstevel@tonic-gate 
146*0Sstevel@tonic-gate /*
147*0Sstevel@tonic-gate  * picld_fini is called when a picl_shutdown request is received
148*0Sstevel@tonic-gate  */
149*0Sstevel@tonic-gate static void
150*0Sstevel@tonic-gate picld_fini(picl_service_t *in)
151*0Sstevel@tonic-gate {
152*0Sstevel@tonic-gate 	picl_retfini_t	ret;
153*0Sstevel@tonic-gate 
154*0Sstevel@tonic-gate 	ret.cnum = in->req_fini.cnum;
155*0Sstevel@tonic-gate 
156*0Sstevel@tonic-gate 	(void) rw_unlock(&init_lk);
157*0Sstevel@tonic-gate 	(void) door_return((char *)&ret, sizeof (picl_retfini_t), NULL, 0);
158*0Sstevel@tonic-gate }
159*0Sstevel@tonic-gate 
160*0Sstevel@tonic-gate static void
161*0Sstevel@tonic-gate picld_ping(picl_service_t *in)
162*0Sstevel@tonic-gate {
163*0Sstevel@tonic-gate 	picl_retping_t	ret;
164*0Sstevel@tonic-gate 
165*0Sstevel@tonic-gate 	ret.cnum = in->req_ping.cnum;
166*0Sstevel@tonic-gate 
167*0Sstevel@tonic-gate 	(void) rw_unlock(&init_lk);
168*0Sstevel@tonic-gate 	(void) door_return((char *)&ret, sizeof (picl_retping_t), NULL, 0);
169*0Sstevel@tonic-gate }
170*0Sstevel@tonic-gate 
171*0Sstevel@tonic-gate /*
172*0Sstevel@tonic-gate  * picld_wait is called when a picl_wait request is received
173*0Sstevel@tonic-gate  */
174*0Sstevel@tonic-gate static void
175*0Sstevel@tonic-gate picld_wait(picl_service_t *in)
176*0Sstevel@tonic-gate {
177*0Sstevel@tonic-gate 	picl_retwait_t	ret;
178*0Sstevel@tonic-gate 	int		err;
179*0Sstevel@tonic-gate 
180*0Sstevel@tonic-gate 	ret.cnum = in->req_wait.cnum;
181*0Sstevel@tonic-gate 
182*0Sstevel@tonic-gate 	err = xptree_refresh_notify(in->req_wait.secs);
183*0Sstevel@tonic-gate 	ret.retcode = err;
184*0Sstevel@tonic-gate 
185*0Sstevel@tonic-gate 	(void) rw_unlock(&init_lk);
186*0Sstevel@tonic-gate 	(void) door_return((char *)&ret, sizeof (picl_retwait_t), NULL, 0);
187*0Sstevel@tonic-gate }
188*0Sstevel@tonic-gate 
189*0Sstevel@tonic-gate /*
190*0Sstevel@tonic-gate  * This function returns the handle of the root node of the PICL tree
191*0Sstevel@tonic-gate  */
192*0Sstevel@tonic-gate static void
193*0Sstevel@tonic-gate picld_getroot(picl_service_t *in)
194*0Sstevel@tonic-gate {
195*0Sstevel@tonic-gate 	picl_retroot_t	ret;
196*0Sstevel@tonic-gate 	int		err;
197*0Sstevel@tonic-gate 
198*0Sstevel@tonic-gate 	ret.cnum = PICL_CNUM_GETROOT;
199*0Sstevel@tonic-gate 	err = ptree_get_root(&ret.rnode);
200*0Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
201*0Sstevel@tonic-gate 		picld_return_error(in->in.cnum, err);
202*0Sstevel@tonic-gate 	cvt_ptree2picl(&ret.rnode);
203*0Sstevel@tonic-gate 	(void) rw_unlock(&init_lk);
204*0Sstevel@tonic-gate 	(void) door_return((char *)&ret, sizeof (picl_retroot_t), NULL, 0);
205*0Sstevel@tonic-gate }
206*0Sstevel@tonic-gate 
207*0Sstevel@tonic-gate /*
208*0Sstevel@tonic-gate  * This function returns the value of the PICL property
209*0Sstevel@tonic-gate  */
210*0Sstevel@tonic-gate static void
211*0Sstevel@tonic-gate picld_get_attrval(picl_service_t *in)
212*0Sstevel@tonic-gate {
213*0Sstevel@tonic-gate 	picl_retattrval_t	*ret;
214*0Sstevel@tonic-gate 	int			err;
215*0Sstevel@tonic-gate 	size_t			vbufsize;
216*0Sstevel@tonic-gate 	size_t			len;
217*0Sstevel@tonic-gate 	door_cred_t		cred;
218*0Sstevel@tonic-gate 	picl_prophdl_t		ptreeh;
219*0Sstevel@tonic-gate 	ptree_propinfo_t	pinfo;
220*0Sstevel@tonic-gate 
221*0Sstevel@tonic-gate 	if (door_cred(&cred) < 0)
222*0Sstevel@tonic-gate 		picld_return_error(in->in.cnum, PICL_FAILURE);
223*0Sstevel@tonic-gate 
224*0Sstevel@tonic-gate 	err = cvt_picl2ptree(in->req_attrval.attr, &ptreeh);
225*0Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
226*0Sstevel@tonic-gate 		picld_return_error(in->in.cnum, err);
227*0Sstevel@tonic-gate 
228*0Sstevel@tonic-gate 	err = ptree_get_propinfo(ptreeh, &pinfo);
229*0Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
230*0Sstevel@tonic-gate 		picld_return_error(in->in.cnum, err);
231*0Sstevel@tonic-gate 
232*0Sstevel@tonic-gate 	if (!(pinfo.piclinfo.accessmode & PICL_READ))
233*0Sstevel@tonic-gate 		picld_return_error(in->in.cnum, PICL_NOTREADABLE);
234*0Sstevel@tonic-gate 
235*0Sstevel@tonic-gate 	vbufsize = pinfo.piclinfo.size;
236*0Sstevel@tonic-gate 	vbufsize = MIN((size_t)in->req_attrval.bufsize, vbufsize);
237*0Sstevel@tonic-gate 
238*0Sstevel@tonic-gate 	len = sizeof (picl_retattrval_t) + vbufsize;
239*0Sstevel@tonic-gate 	ret = alloca(len);
240*0Sstevel@tonic-gate 	if (ret == NULL)
241*0Sstevel@tonic-gate 		picld_return_error(in->in.cnum, PICL_FAILURE);
242*0Sstevel@tonic-gate 	ret->cnum = PICL_CNUM_GETATTRVAL;
243*0Sstevel@tonic-gate 	ret->attr = in->req_attrval.attr;
244*0Sstevel@tonic-gate 	ret->nbytes = (uint32_t)vbufsize;
245*0Sstevel@tonic-gate 	err = xptree_get_propval_with_cred(ptreeh, ret->ret_buf, vbufsize,
246*0Sstevel@tonic-gate 	    cred);
247*0Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
248*0Sstevel@tonic-gate 		picld_return_error(in->in.cnum, err);
249*0Sstevel@tonic-gate 
250*0Sstevel@tonic-gate 	/*
251*0Sstevel@tonic-gate 	 * adjust returned bytes for charstrings
252*0Sstevel@tonic-gate 	 */
253*0Sstevel@tonic-gate 	if (pinfo.piclinfo.type == PICL_PTYPE_CHARSTRING)
254*0Sstevel@tonic-gate 		ret->nbytes = (uint32_t)strlen(ret->ret_buf) + 1;
255*0Sstevel@tonic-gate 
256*0Sstevel@tonic-gate 	/*
257*0Sstevel@tonic-gate 	 * convert handle values to picl handles
258*0Sstevel@tonic-gate 	 */
259*0Sstevel@tonic-gate 	if ((pinfo.piclinfo.type == PICL_PTYPE_TABLE) ||
260*0Sstevel@tonic-gate 	    (pinfo.piclinfo.type == PICL_PTYPE_REFERENCE))
261*0Sstevel@tonic-gate 		cvt_ptree2picl(&ret->ret_nodeh);
262*0Sstevel@tonic-gate 	(void) rw_unlock(&init_lk);
263*0Sstevel@tonic-gate 	(void) door_return((char *)ret, sizeof (picl_retattrval_t) +
264*0Sstevel@tonic-gate 	    (size_t)ret->nbytes, NULL, 0);
265*0Sstevel@tonic-gate }
266*0Sstevel@tonic-gate 
267*0Sstevel@tonic-gate /*
268*0Sstevel@tonic-gate  * This function returns the value of the PICL property specified by
269*0Sstevel@tonic-gate  * its name.
270*0Sstevel@tonic-gate  */
271*0Sstevel@tonic-gate static void
272*0Sstevel@tonic-gate picld_get_attrval_by_name(picl_service_t *in)
273*0Sstevel@tonic-gate {
274*0Sstevel@tonic-gate 	picl_retattrvalbyname_t	*ret;
275*0Sstevel@tonic-gate 	int			err;
276*0Sstevel@tonic-gate 	size_t			vbufsize;
277*0Sstevel@tonic-gate 	size_t			len;
278*0Sstevel@tonic-gate 	door_cred_t		cred;
279*0Sstevel@tonic-gate 	picl_nodehdl_t		ptreeh;
280*0Sstevel@tonic-gate 	ptree_propinfo_t	pinfo;
281*0Sstevel@tonic-gate 
282*0Sstevel@tonic-gate 	if (door_cred(&cred) < 0)
283*0Sstevel@tonic-gate 		picld_return_error(in->in.cnum, PICL_FAILURE);
284*0Sstevel@tonic-gate 
285*0Sstevel@tonic-gate 	err = cvt_picl2ptree(in->req_attrvalbyname.nodeh, &ptreeh);
286*0Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
287*0Sstevel@tonic-gate 		picld_return_error(in->in.cnum, err);
288*0Sstevel@tonic-gate 
289*0Sstevel@tonic-gate 	err = xptree_get_propinfo_by_name(ptreeh,
290*0Sstevel@tonic-gate 	    in->req_attrvalbyname.propname, &pinfo);
291*0Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
292*0Sstevel@tonic-gate 		picld_return_error(in->in.cnum, err);
293*0Sstevel@tonic-gate 
294*0Sstevel@tonic-gate 	if (!(pinfo.piclinfo.accessmode & PICL_READ))
295*0Sstevel@tonic-gate 		picld_return_error(in->in.cnum, PICL_NOTREADABLE);
296*0Sstevel@tonic-gate 
297*0Sstevel@tonic-gate 	/*
298*0Sstevel@tonic-gate 	 * allocate the minimum of piclinfo.size and input bufsize
299*0Sstevel@tonic-gate 	 */
300*0Sstevel@tonic-gate 	vbufsize = pinfo.piclinfo.size;
301*0Sstevel@tonic-gate 	vbufsize = MIN((size_t)in->req_attrvalbyname.bufsize, vbufsize);
302*0Sstevel@tonic-gate 	len = sizeof (picl_retattrvalbyname_t) + vbufsize;
303*0Sstevel@tonic-gate 	ret = alloca(len);
304*0Sstevel@tonic-gate 	if (ret == NULL)
305*0Sstevel@tonic-gate 		picld_return_error(in->in.cnum, PICL_FAILURE);
306*0Sstevel@tonic-gate 	ret->cnum = PICL_CNUM_GETATTRVALBYNAME;
307*0Sstevel@tonic-gate 	ret->nodeh = in->req_attrvalbyname.nodeh;
308*0Sstevel@tonic-gate 	(void) strcpy(ret->propname, in->req_attrvalbyname.propname);
309*0Sstevel@tonic-gate 	ret->nbytes = (uint32_t)vbufsize;
310*0Sstevel@tonic-gate 
311*0Sstevel@tonic-gate 	err = xptree_get_propval_by_name_with_cred(ptreeh,
312*0Sstevel@tonic-gate 	    in->req_attrvalbyname.propname, ret->ret_buf, vbufsize,
313*0Sstevel@tonic-gate 	    cred);
314*0Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
315*0Sstevel@tonic-gate 		picld_return_error(in->in.cnum, err);
316*0Sstevel@tonic-gate 	/*
317*0Sstevel@tonic-gate 	 * adjust returned value size for charstrings
318*0Sstevel@tonic-gate 	 */
319*0Sstevel@tonic-gate 	if (pinfo.piclinfo.type == PICL_PTYPE_CHARSTRING)
320*0Sstevel@tonic-gate 		ret->nbytes = (uint32_t)strlen(ret->ret_buf) + 1;
321*0Sstevel@tonic-gate 
322*0Sstevel@tonic-gate 	if ((pinfo.piclinfo.type == PICL_PTYPE_TABLE) ||
323*0Sstevel@tonic-gate 	    (pinfo.piclinfo.type == PICL_PTYPE_REFERENCE))
324*0Sstevel@tonic-gate 		cvt_ptree2picl(&ret->ret_nodeh);
325*0Sstevel@tonic-gate 
326*0Sstevel@tonic-gate 	(void) rw_unlock(&init_lk);
327*0Sstevel@tonic-gate 	(void) door_return((char *)ret, sizeof (picl_retattrvalbyname_t) +
328*0Sstevel@tonic-gate 	    (size_t)ret->nbytes, NULL, 0);
329*0Sstevel@tonic-gate }
330*0Sstevel@tonic-gate 
331*0Sstevel@tonic-gate /*
332*0Sstevel@tonic-gate  * This function sets a property value
333*0Sstevel@tonic-gate  */
334*0Sstevel@tonic-gate static void
335*0Sstevel@tonic-gate picld_set_attrval(picl_service_t *in)
336*0Sstevel@tonic-gate {
337*0Sstevel@tonic-gate 	picl_retsetattrval_t	ret;
338*0Sstevel@tonic-gate 	int			err;
339*0Sstevel@tonic-gate 	door_cred_t		cred;
340*0Sstevel@tonic-gate 	picl_prophdl_t		ptreeh;
341*0Sstevel@tonic-gate 	ptree_propinfo_t	pinfo;
342*0Sstevel@tonic-gate 
343*0Sstevel@tonic-gate 	if (door_cred(&cred) < 0)
344*0Sstevel@tonic-gate 		picld_return_error(in->in.cnum, PICL_FAILURE);
345*0Sstevel@tonic-gate 
346*0Sstevel@tonic-gate 	err = cvt_picl2ptree(in->req_setattrval.attr, &ptreeh);
347*0Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
348*0Sstevel@tonic-gate 		picld_return_error(in->in.cnum, err);
349*0Sstevel@tonic-gate 
350*0Sstevel@tonic-gate 	err = ptree_get_propinfo(ptreeh, &pinfo);
351*0Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
352*0Sstevel@tonic-gate 		picld_return_error(in->in.cnum, err);
353*0Sstevel@tonic-gate 
354*0Sstevel@tonic-gate 	if (!(pinfo.piclinfo.accessmode & PICL_WRITE))
355*0Sstevel@tonic-gate 		picld_return_error(in->in.cnum, PICL_NOTWRITABLE);
356*0Sstevel@tonic-gate 	/*
357*0Sstevel@tonic-gate 	 * For non-volatile prop, only super user can set its value.
358*0Sstevel@tonic-gate 	 */
359*0Sstevel@tonic-gate 	if (!(pinfo.piclinfo.accessmode & PICL_VOLATILE) &&
360*0Sstevel@tonic-gate 	    (cred.dc_euid != SUPER_USER))
361*0Sstevel@tonic-gate 		picld_return_error(in->in.cnum, PICL_PERMDENIED);
362*0Sstevel@tonic-gate 
363*0Sstevel@tonic-gate 	ret.cnum = PICL_CNUM_SETATTRVAL;
364*0Sstevel@tonic-gate 	ret.attr = in->req_setattrval.attr;
365*0Sstevel@tonic-gate 
366*0Sstevel@tonic-gate 	err = xptree_update_propval_with_cred(ptreeh, in->req_setattrval.valbuf,
367*0Sstevel@tonic-gate 	    (size_t)in->req_setattrval.bufsize, cred);
368*0Sstevel@tonic-gate 
369*0Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
370*0Sstevel@tonic-gate 		picld_return_error(in->in.cnum, err);
371*0Sstevel@tonic-gate 
372*0Sstevel@tonic-gate 	(void) rw_unlock(&init_lk);
373*0Sstevel@tonic-gate 	(void) door_return((char *)&ret, sizeof (picl_retsetattrval_t), NULL,
374*0Sstevel@tonic-gate 	    0);
375*0Sstevel@tonic-gate }
376*0Sstevel@tonic-gate 
377*0Sstevel@tonic-gate /*
378*0Sstevel@tonic-gate  * This function sets the value of a property specified by its name.
379*0Sstevel@tonic-gate  */
380*0Sstevel@tonic-gate static void
381*0Sstevel@tonic-gate picld_set_attrval_by_name(picl_service_t *in)
382*0Sstevel@tonic-gate {
383*0Sstevel@tonic-gate 	picl_retsetattrvalbyname_t	ret;
384*0Sstevel@tonic-gate 	int				err;
385*0Sstevel@tonic-gate 	door_cred_t			cred;
386*0Sstevel@tonic-gate 	picl_prophdl_t			ptreeh;
387*0Sstevel@tonic-gate 	ptree_propinfo_t		pinfo;
388*0Sstevel@tonic-gate 
389*0Sstevel@tonic-gate 	if (door_cred(&cred) < 0)
390*0Sstevel@tonic-gate 		picld_return_error(in->in.cnum, PICL_FAILURE);
391*0Sstevel@tonic-gate 
392*0Sstevel@tonic-gate 	err = cvt_picl2ptree(in->req_setattrvalbyname.nodeh, &ptreeh);
393*0Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
394*0Sstevel@tonic-gate 		picld_return_error(in->in.cnum, err);
395*0Sstevel@tonic-gate 
396*0Sstevel@tonic-gate 	err = xptree_get_propinfo_by_name(ptreeh,
397*0Sstevel@tonic-gate 	    in->req_setattrvalbyname.propname, &pinfo);
398*0Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
399*0Sstevel@tonic-gate 		picld_return_error(in->in.cnum, err);
400*0Sstevel@tonic-gate 
401*0Sstevel@tonic-gate 	if (!(pinfo.piclinfo.accessmode & PICL_WRITE))
402*0Sstevel@tonic-gate 		picld_return_error(in->in.cnum, PICL_NOTWRITABLE);
403*0Sstevel@tonic-gate 
404*0Sstevel@tonic-gate 	/*
405*0Sstevel@tonic-gate 	 * For non-volatile prop, only super user can set its value.
406*0Sstevel@tonic-gate 	 */
407*0Sstevel@tonic-gate 	if (!(pinfo.piclinfo.accessmode & PICL_VOLATILE) &&
408*0Sstevel@tonic-gate 	    (cred.dc_euid != SUPER_USER))
409*0Sstevel@tonic-gate 		picld_return_error(in->in.cnum, PICL_PERMDENIED);
410*0Sstevel@tonic-gate 
411*0Sstevel@tonic-gate 	ret.cnum = PICL_CNUM_SETATTRVALBYNAME;
412*0Sstevel@tonic-gate 	ret.nodeh = in->req_setattrvalbyname.nodeh;
413*0Sstevel@tonic-gate 	(void) strcpy(ret.propname, in->req_setattrvalbyname.propname);
414*0Sstevel@tonic-gate 
415*0Sstevel@tonic-gate 	err = xptree_update_propval_by_name_with_cred(ptreeh,
416*0Sstevel@tonic-gate 		in->req_setattrvalbyname.propname,
417*0Sstevel@tonic-gate 		in->req_setattrvalbyname.valbuf,
418*0Sstevel@tonic-gate 		(size_t)in->req_setattrvalbyname.bufsize,
419*0Sstevel@tonic-gate 		cred);
420*0Sstevel@tonic-gate 
421*0Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
422*0Sstevel@tonic-gate 		picld_return_error(in->in.cnum, err);
423*0Sstevel@tonic-gate 
424*0Sstevel@tonic-gate 	(void) rw_unlock(&init_lk);
425*0Sstevel@tonic-gate 	(void) door_return((char *)&ret, sizeof (picl_retsetattrvalbyname_t),
426*0Sstevel@tonic-gate 	    NULL, 0);
427*0Sstevel@tonic-gate }
428*0Sstevel@tonic-gate 
429*0Sstevel@tonic-gate /*
430*0Sstevel@tonic-gate  * This function returns the property information
431*0Sstevel@tonic-gate  */
432*0Sstevel@tonic-gate static void
433*0Sstevel@tonic-gate picld_get_attrinfo(picl_service_t *in)
434*0Sstevel@tonic-gate {
435*0Sstevel@tonic-gate 	picl_retattrinfo_t	ret;
436*0Sstevel@tonic-gate 	int			err;
437*0Sstevel@tonic-gate 	ptree_propinfo_t	pinfo;
438*0Sstevel@tonic-gate 	picl_prophdl_t		ptreeh;
439*0Sstevel@tonic-gate 
440*0Sstevel@tonic-gate 	err = cvt_picl2ptree(in->req_attrinfo.attr, &ptreeh);
441*0Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
442*0Sstevel@tonic-gate 		picld_return_error(in->in.cnum, err);
443*0Sstevel@tonic-gate 
444*0Sstevel@tonic-gate 	ret.cnum = PICL_CNUM_GETATTRINFO;
445*0Sstevel@tonic-gate 	ret.attr = in->req_attrinfo.attr;
446*0Sstevel@tonic-gate 
447*0Sstevel@tonic-gate 	err = ptree_get_propinfo(ptreeh, &pinfo);
448*0Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
449*0Sstevel@tonic-gate 		picld_return_error(in->in.cnum, err);
450*0Sstevel@tonic-gate 
451*0Sstevel@tonic-gate 	ret.type = pinfo.piclinfo.type;
452*0Sstevel@tonic-gate 	ret.accessmode = pinfo.piclinfo.accessmode;
453*0Sstevel@tonic-gate 	ret.size = (uint32_t)pinfo.piclinfo.size;
454*0Sstevel@tonic-gate 	(void) strcpy(ret.name, pinfo.piclinfo.name);
455*0Sstevel@tonic-gate 	(void) rw_unlock(&init_lk);
456*0Sstevel@tonic-gate 	(void) door_return((char *)&ret, sizeof (picl_retattrinfo_t), NULL, 0);
457*0Sstevel@tonic-gate }
458*0Sstevel@tonic-gate 
459*0Sstevel@tonic-gate /*
460*0Sstevel@tonic-gate  * This function returns the node's first property handle
461*0Sstevel@tonic-gate  */
462*0Sstevel@tonic-gate static void
463*0Sstevel@tonic-gate picld_get_first_attr(picl_service_t *in)
464*0Sstevel@tonic-gate {
465*0Sstevel@tonic-gate 	picl_retfirstattr_t	ret;
466*0Sstevel@tonic-gate 	int			err;
467*0Sstevel@tonic-gate 	picl_prophdl_t		ptreeh;
468*0Sstevel@tonic-gate 
469*0Sstevel@tonic-gate 	err = cvt_picl2ptree(in->req_firstattr.nodeh, &ptreeh);
470*0Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
471*0Sstevel@tonic-gate 		picld_return_error(in->in.cnum, err);
472*0Sstevel@tonic-gate 
473*0Sstevel@tonic-gate 	ret.cnum = PICL_CNUM_GETFIRSTATTR;
474*0Sstevel@tonic-gate 	ret.nodeh = in->req_firstattr.nodeh;
475*0Sstevel@tonic-gate 
476*0Sstevel@tonic-gate 	err = ptree_get_first_prop(ptreeh, &ret.attr);
477*0Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
478*0Sstevel@tonic-gate 		picld_return_error(in->in.cnum, err);
479*0Sstevel@tonic-gate 	cvt_ptree2picl(&ret.attr);
480*0Sstevel@tonic-gate 	(void) rw_unlock(&init_lk);
481*0Sstevel@tonic-gate 	(void) door_return((char *)&ret, sizeof (picl_retfirstattr_t), NULL, 0);
482*0Sstevel@tonic-gate }
483*0Sstevel@tonic-gate 
484*0Sstevel@tonic-gate /*
485*0Sstevel@tonic-gate  * This function returns the next property handle in list
486*0Sstevel@tonic-gate  */
487*0Sstevel@tonic-gate static void
488*0Sstevel@tonic-gate picld_get_next_attr(picl_service_t *in)
489*0Sstevel@tonic-gate {
490*0Sstevel@tonic-gate 	picl_retnextattr_t	ret;
491*0Sstevel@tonic-gate 	int			err;
492*0Sstevel@tonic-gate 	picl_prophdl_t		ptreeh;
493*0Sstevel@tonic-gate 
494*0Sstevel@tonic-gate 	err = cvt_picl2ptree(in->req_nextattr.attr, &ptreeh);
495*0Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
496*0Sstevel@tonic-gate 		picld_return_error(in->in.cnum, err);
497*0Sstevel@tonic-gate 
498*0Sstevel@tonic-gate 	ret.cnum = PICL_CNUM_GETNEXTATTR;
499*0Sstevel@tonic-gate 	ret.attr = in->req_nextattr.attr;
500*0Sstevel@tonic-gate 
501*0Sstevel@tonic-gate 	err = ptree_get_next_prop(ptreeh, &ret.nextattr);
502*0Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
503*0Sstevel@tonic-gate 		picld_return_error(in->in.cnum, err);
504*0Sstevel@tonic-gate 
505*0Sstevel@tonic-gate 	cvt_ptree2picl(&ret.nextattr);
506*0Sstevel@tonic-gate 
507*0Sstevel@tonic-gate 	(void) rw_unlock(&init_lk);
508*0Sstevel@tonic-gate 	(void) door_return((char *)&ret, sizeof (picl_retnextattr_t), NULL, 0);
509*0Sstevel@tonic-gate }
510*0Sstevel@tonic-gate 
511*0Sstevel@tonic-gate /*
512*0Sstevel@tonic-gate  * This function returns the handle of a property specified by its name
513*0Sstevel@tonic-gate  */
514*0Sstevel@tonic-gate static void
515*0Sstevel@tonic-gate picld_get_attr_by_name(picl_service_t *in)
516*0Sstevel@tonic-gate {
517*0Sstevel@tonic-gate 	picl_retattrbyname_t	ret;
518*0Sstevel@tonic-gate 	int			err;
519*0Sstevel@tonic-gate 	picl_prophdl_t		ptreeh;
520*0Sstevel@tonic-gate 
521*0Sstevel@tonic-gate 	err = cvt_picl2ptree(in->req_attrbyname.nodeh, &ptreeh);
522*0Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
523*0Sstevel@tonic-gate 		picld_return_error(in->in.cnum, err);
524*0Sstevel@tonic-gate 
525*0Sstevel@tonic-gate 	ret.cnum = PICL_CNUM_GETATTRBYNAME;
526*0Sstevel@tonic-gate 	ret.nodeh = in->req_attrbyname.nodeh;
527*0Sstevel@tonic-gate 	(void) strcpy(ret.propname, in->req_attrbyname.propname);
528*0Sstevel@tonic-gate 
529*0Sstevel@tonic-gate 	err = ptree_get_prop_by_name(ptreeh, ret.propname, &ret.attr);
530*0Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
531*0Sstevel@tonic-gate 		picld_return_error(in->in.cnum, err);
532*0Sstevel@tonic-gate 
533*0Sstevel@tonic-gate 	cvt_ptree2picl(&ret.attr);
534*0Sstevel@tonic-gate 	(void) rw_unlock(&init_lk);
535*0Sstevel@tonic-gate 	(void) door_return((char *)&ret, sizeof (picl_retattrbyname_t), NULL,
536*0Sstevel@tonic-gate 	    0);
537*0Sstevel@tonic-gate }
538*0Sstevel@tonic-gate 
539*0Sstevel@tonic-gate /*
540*0Sstevel@tonic-gate  * This function gets the next property on the same row in the table
541*0Sstevel@tonic-gate  */
542*0Sstevel@tonic-gate static void
543*0Sstevel@tonic-gate picld_get_attr_by_row(picl_service_t *in)
544*0Sstevel@tonic-gate {
545*0Sstevel@tonic-gate 	picl_retattrbyrow_t	ret;
546*0Sstevel@tonic-gate 	int			err;
547*0Sstevel@tonic-gate 	picl_prophdl_t		ptreeh;
548*0Sstevel@tonic-gate 
549*0Sstevel@tonic-gate 	err = cvt_picl2ptree(in->req_attrbyrow.attr, &ptreeh);
550*0Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
551*0Sstevel@tonic-gate 		picld_return_error(in->in.cnum, err);
552*0Sstevel@tonic-gate 
553*0Sstevel@tonic-gate 	ret.cnum = PICL_CNUM_GETATTRBYROW;
554*0Sstevel@tonic-gate 	ret.attr = in->req_attrbyrow.attr;
555*0Sstevel@tonic-gate 
556*0Sstevel@tonic-gate 	err = ptree_get_next_by_row(ptreeh, &ret.rowattr);
557*0Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
558*0Sstevel@tonic-gate 		picld_return_error(in->in.cnum, err);
559*0Sstevel@tonic-gate 	cvt_ptree2picl(&ret.rowattr);
560*0Sstevel@tonic-gate 
561*0Sstevel@tonic-gate 	(void) rw_unlock(&init_lk);
562*0Sstevel@tonic-gate 	(void) door_return((char *)&ret, sizeof (picl_retattrbyrow_t), NULL, 0);
563*0Sstevel@tonic-gate }
564*0Sstevel@tonic-gate 
565*0Sstevel@tonic-gate /*
566*0Sstevel@tonic-gate  * This function returns the handle of the next property in the same column
567*0Sstevel@tonic-gate  * of the table.
568*0Sstevel@tonic-gate  */
569*0Sstevel@tonic-gate static void
570*0Sstevel@tonic-gate picld_get_attr_by_col(picl_service_t *in)
571*0Sstevel@tonic-gate {
572*0Sstevel@tonic-gate 	picl_retattrbycol_t	ret;
573*0Sstevel@tonic-gate 	int			err;
574*0Sstevel@tonic-gate 	picl_prophdl_t		ptreeh;
575*0Sstevel@tonic-gate 
576*0Sstevel@tonic-gate 	err = cvt_picl2ptree(in->req_attrbycol.attr, &ptreeh);
577*0Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
578*0Sstevel@tonic-gate 		picld_return_error(in->in.cnum, err);
579*0Sstevel@tonic-gate 
580*0Sstevel@tonic-gate 	ret.cnum = PICL_CNUM_GETATTRBYCOL;
581*0Sstevel@tonic-gate 	ret.attr = in->req_attrbycol.attr;
582*0Sstevel@tonic-gate 
583*0Sstevel@tonic-gate 	err = ptree_get_next_by_col(ptreeh, &ret.colattr);
584*0Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
585*0Sstevel@tonic-gate 		picld_return_error(in->in.cnum, err);
586*0Sstevel@tonic-gate 
587*0Sstevel@tonic-gate 	cvt_ptree2picl(&ret.colattr);
588*0Sstevel@tonic-gate 
589*0Sstevel@tonic-gate 	(void) rw_unlock(&init_lk);
590*0Sstevel@tonic-gate 	(void) door_return((char *)&ret, sizeof (picl_retattrbycol_t), NULL, 0);
591*0Sstevel@tonic-gate }
592*0Sstevel@tonic-gate 
593*0Sstevel@tonic-gate /*
594*0Sstevel@tonic-gate  * This function finds the node in the PICLTREE that matches the given
595*0Sstevel@tonic-gate  * criteria and returns its handle.
596*0Sstevel@tonic-gate  */
597*0Sstevel@tonic-gate static void
598*0Sstevel@tonic-gate picld_find_node(picl_service_t *in)
599*0Sstevel@tonic-gate {
600*0Sstevel@tonic-gate 	picl_retfindnode_t	ret;
601*0Sstevel@tonic-gate 	int			err;
602*0Sstevel@tonic-gate 	picl_nodehdl_t		ptreeh;
603*0Sstevel@tonic-gate 
604*0Sstevel@tonic-gate 	err = cvt_picl2ptree(in->req_findnode.nodeh, &ptreeh);
605*0Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
606*0Sstevel@tonic-gate 		picld_return_error(in->in.cnum, err);
607*0Sstevel@tonic-gate 
608*0Sstevel@tonic-gate 	ret.cnum = PICL_CNUM_FINDNODE;
609*0Sstevel@tonic-gate 
610*0Sstevel@tonic-gate 	err = ptree_find_node(ptreeh, in->req_findnode.propname,
611*0Sstevel@tonic-gate 	    in->req_findnode.ptype, in->req_findnode.valbuf,
612*0Sstevel@tonic-gate 	    in->req_findnode.valsize, &ret.rnodeh);
613*0Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
614*0Sstevel@tonic-gate 		picld_return_error(in->in.cnum, err);
615*0Sstevel@tonic-gate 
616*0Sstevel@tonic-gate 	cvt_ptree2picl(&ret.rnodeh);
617*0Sstevel@tonic-gate 
618*0Sstevel@tonic-gate 	(void) rw_unlock(&init_lk);
619*0Sstevel@tonic-gate 	(void) door_return((char *)&ret, sizeof (ret), NULL, 0);
620*0Sstevel@tonic-gate }
621*0Sstevel@tonic-gate 
622*0Sstevel@tonic-gate /*
623*0Sstevel@tonic-gate  * This function finds the property/node that corresponds to the given path
624*0Sstevel@tonic-gate  * and returns its handle
625*0Sstevel@tonic-gate  */
626*0Sstevel@tonic-gate static void
627*0Sstevel@tonic-gate picld_get_node_by_path(picl_service_t *in)
628*0Sstevel@tonic-gate {
629*0Sstevel@tonic-gate 	picl_retnodebypath_t	ret;
630*0Sstevel@tonic-gate 	int			err;
631*0Sstevel@tonic-gate 
632*0Sstevel@tonic-gate 	ret.cnum = PICL_CNUM_NODEBYPATH;
633*0Sstevel@tonic-gate 	err = ptree_get_node_by_path(in->req_nodebypath.pathbuf, &ret.nodeh);
634*0Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
635*0Sstevel@tonic-gate 		picld_return_error(in->in.cnum, err);
636*0Sstevel@tonic-gate 	cvt_ptree2picl(&ret.nodeh);
637*0Sstevel@tonic-gate 	(void) rw_unlock(&init_lk);
638*0Sstevel@tonic-gate 	(void) door_return((char *)&ret, sizeof (ret), NULL, 0);
639*0Sstevel@tonic-gate }
640*0Sstevel@tonic-gate 
641*0Sstevel@tonic-gate /*
642*0Sstevel@tonic-gate  * This function returns finds the frutree parent node for a given node
643*0Sstevel@tonic-gate  * and returns its handle
644*0Sstevel@tonic-gate  */
645*0Sstevel@tonic-gate static void
646*0Sstevel@tonic-gate picld_get_frutree_parent(picl_service_t *in)
647*0Sstevel@tonic-gate {
648*0Sstevel@tonic-gate 	picl_retfruparent_t	ret;
649*0Sstevel@tonic-gate 	int			err;
650*0Sstevel@tonic-gate 	picl_nodehdl_t		ptreeh;
651*0Sstevel@tonic-gate 
652*0Sstevel@tonic-gate 	err = cvt_picl2ptree(in->req_fruparent.devh, &ptreeh);
653*0Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
654*0Sstevel@tonic-gate 		picld_return_error(in->in.cnum, err);
655*0Sstevel@tonic-gate 
656*0Sstevel@tonic-gate 	ret.cnum = PICL_CNUM_FRUTREEPARENT;
657*0Sstevel@tonic-gate 
658*0Sstevel@tonic-gate 	err = ptree_get_frutree_parent(ptreeh, &ret.fruh);
659*0Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
660*0Sstevel@tonic-gate 		picld_return_error(in->in.cnum, err);
661*0Sstevel@tonic-gate 	cvt_ptree2picl(&ret.fruh);
662*0Sstevel@tonic-gate 
663*0Sstevel@tonic-gate 	(void) rw_unlock(&init_lk);
664*0Sstevel@tonic-gate 	(void) door_return((char *)&ret, sizeof (ret), NULL, 0);
665*0Sstevel@tonic-gate }
666*0Sstevel@tonic-gate 
667*0Sstevel@tonic-gate /*
668*0Sstevel@tonic-gate  * This function is called when an unknown client request is received.
669*0Sstevel@tonic-gate  */
670*0Sstevel@tonic-gate static void
671*0Sstevel@tonic-gate picld_unknown_service(picl_service_t *in)
672*0Sstevel@tonic-gate {
673*0Sstevel@tonic-gate 	picld_return_error(in->in.cnum, PICL_UNKNOWNSERVICE);
674*0Sstevel@tonic-gate }
675*0Sstevel@tonic-gate 
676*0Sstevel@tonic-gate static void
677*0Sstevel@tonic-gate check_denial_of_service(int cnum)
678*0Sstevel@tonic-gate {
679*0Sstevel@tonic-gate 	hrtime_t	window;
680*0Sstevel@tonic-gate 	hrtime_t	current;
681*0Sstevel@tonic-gate 	int		dos_flag;
682*0Sstevel@tonic-gate 
683*0Sstevel@tonic-gate 	current = gethrtime();
684*0Sstevel@tonic-gate 	dos_flag = 0;
685*0Sstevel@tonic-gate 
686*0Sstevel@tonic-gate 	if (pthread_mutex_lock(&dos_mutex) != 0)
687*0Sstevel@tonic-gate 		picld_return_error(cnum, PICL_FAILURE);
688*0Sstevel@tonic-gate 
689*0Sstevel@tonic-gate 	++service_requests;
690*0Sstevel@tonic-gate 	window = current - orig_time;
691*0Sstevel@tonic-gate 	if (window > MILLI_TO_NANO(sliding_interval_ms)) {
692*0Sstevel@tonic-gate 		orig_time = current;
693*0Sstevel@tonic-gate 		service_requests = 1;
694*0Sstevel@tonic-gate 	}
695*0Sstevel@tonic-gate 
696*0Sstevel@tonic-gate 	if (service_requests > dos_req_limit)
697*0Sstevel@tonic-gate 		dos_flag = 1;
698*0Sstevel@tonic-gate 
699*0Sstevel@tonic-gate 	if (pthread_mutex_unlock(&dos_mutex) != 0)
700*0Sstevel@tonic-gate 		picld_return_error(cnum, PICL_FAILURE);
701*0Sstevel@tonic-gate 
702*0Sstevel@tonic-gate 	if (dos_flag)
703*0Sstevel@tonic-gate 		(void) poll(NULL, 0, dos_ms);
704*0Sstevel@tonic-gate }
705*0Sstevel@tonic-gate 
706*0Sstevel@tonic-gate 
707*0Sstevel@tonic-gate /* ARGSUSED */
708*0Sstevel@tonic-gate static void
709*0Sstevel@tonic-gate picld_door_handler(void *cookie, char *argp, size_t asize,
710*0Sstevel@tonic-gate     door_desc_t *dp, uint_t n_desc)
711*0Sstevel@tonic-gate {
712*0Sstevel@tonic-gate 	picl_service_t  *req;
713*0Sstevel@tonic-gate 
714*0Sstevel@tonic-gate 	/*LINTED*/
715*0Sstevel@tonic-gate 	req = (picl_service_t *)argp;
716*0Sstevel@tonic-gate 
717*0Sstevel@tonic-gate 	if (req == NULL)
718*0Sstevel@tonic-gate 		(void) door_return((char *)req, 0, NULL, 0);
719*0Sstevel@tonic-gate 
720*0Sstevel@tonic-gate 	check_denial_of_service(req->in.cnum);
721*0Sstevel@tonic-gate 
722*0Sstevel@tonic-gate 	(void) rw_rdlock(&init_lk);
723*0Sstevel@tonic-gate 	switch (req->in.cnum) {	/* client call number */
724*0Sstevel@tonic-gate 	case PICL_CNUM_INIT:
725*0Sstevel@tonic-gate 		/*LINTED*/
726*0Sstevel@tonic-gate 		picld_init((picl_service_t *)argp);
727*0Sstevel@tonic-gate 		break;
728*0Sstevel@tonic-gate 	case PICL_CNUM_FINI:
729*0Sstevel@tonic-gate 		/*LINTED*/
730*0Sstevel@tonic-gate 		picld_fini((picl_service_t *)argp);
731*0Sstevel@tonic-gate 		break;
732*0Sstevel@tonic-gate 	case PICL_CNUM_GETROOT:
733*0Sstevel@tonic-gate 		/*LINTED*/
734*0Sstevel@tonic-gate 		picld_getroot((picl_service_t *)argp);
735*0Sstevel@tonic-gate 		break;
736*0Sstevel@tonic-gate 	case PICL_CNUM_GETATTRVAL:
737*0Sstevel@tonic-gate 		/*LINTED*/
738*0Sstevel@tonic-gate 		picld_get_attrval((picl_service_t *)argp);
739*0Sstevel@tonic-gate 		break;
740*0Sstevel@tonic-gate 	case PICL_CNUM_GETATTRVALBYNAME:
741*0Sstevel@tonic-gate 		/*LINTED*/
742*0Sstevel@tonic-gate 		picld_get_attrval_by_name((picl_service_t *)argp);
743*0Sstevel@tonic-gate 		break;
744*0Sstevel@tonic-gate 	case PICL_CNUM_GETATTRINFO:
745*0Sstevel@tonic-gate 		/*LINTED*/
746*0Sstevel@tonic-gate 		picld_get_attrinfo((picl_service_t *)argp);
747*0Sstevel@tonic-gate 		break;
748*0Sstevel@tonic-gate 	case PICL_CNUM_GETFIRSTATTR:
749*0Sstevel@tonic-gate 		/*LINTED*/
750*0Sstevel@tonic-gate 		picld_get_first_attr((picl_service_t *)argp);
751*0Sstevel@tonic-gate 		break;
752*0Sstevel@tonic-gate 	case PICL_CNUM_GETNEXTATTR:
753*0Sstevel@tonic-gate 		/*LINTED*/
754*0Sstevel@tonic-gate 		picld_get_next_attr((picl_service_t *)argp);
755*0Sstevel@tonic-gate 		break;
756*0Sstevel@tonic-gate 	case PICL_CNUM_GETATTRBYNAME:
757*0Sstevel@tonic-gate 		/*LINTED*/
758*0Sstevel@tonic-gate 		picld_get_attr_by_name((picl_service_t *)argp);
759*0Sstevel@tonic-gate 		break;
760*0Sstevel@tonic-gate 	case PICL_CNUM_GETATTRBYROW:
761*0Sstevel@tonic-gate 		/*LINTED*/
762*0Sstevel@tonic-gate 		picld_get_attr_by_row((picl_service_t *)argp);
763*0Sstevel@tonic-gate 		break;
764*0Sstevel@tonic-gate 	case PICL_CNUM_GETATTRBYCOL:
765*0Sstevel@tonic-gate 		/*LINTED*/
766*0Sstevel@tonic-gate 		picld_get_attr_by_col((picl_service_t *)argp);
767*0Sstevel@tonic-gate 		break;
768*0Sstevel@tonic-gate 	case PICL_CNUM_SETATTRVAL:
769*0Sstevel@tonic-gate 		/*LINTED*/
770*0Sstevel@tonic-gate 		picld_set_attrval((picl_service_t *)argp);
771*0Sstevel@tonic-gate 		break;
772*0Sstevel@tonic-gate 	case PICL_CNUM_SETATTRVALBYNAME:
773*0Sstevel@tonic-gate 		/*LINTED*/
774*0Sstevel@tonic-gate 		picld_set_attrval_by_name((picl_service_t *)argp);
775*0Sstevel@tonic-gate 		break;
776*0Sstevel@tonic-gate 	case PICL_CNUM_PING:
777*0Sstevel@tonic-gate 		/*LINTED*/
778*0Sstevel@tonic-gate 		picld_ping((picl_service_t *)argp);
779*0Sstevel@tonic-gate 		break;
780*0Sstevel@tonic-gate 	case PICL_CNUM_WAIT:
781*0Sstevel@tonic-gate 		/*LINTED*/
782*0Sstevel@tonic-gate 		picld_wait((picl_service_t *)argp);
783*0Sstevel@tonic-gate 		break;
784*0Sstevel@tonic-gate 	case PICL_CNUM_FINDNODE:
785*0Sstevel@tonic-gate 		/*LINTED*/
786*0Sstevel@tonic-gate 		picld_find_node((picl_service_t *)argp);
787*0Sstevel@tonic-gate 		break;
788*0Sstevel@tonic-gate 	case PICL_CNUM_NODEBYPATH:
789*0Sstevel@tonic-gate 		/*LINTED*/
790*0Sstevel@tonic-gate 		picld_get_node_by_path((picl_service_t *)argp);
791*0Sstevel@tonic-gate 		break;
792*0Sstevel@tonic-gate 	case PICL_CNUM_FRUTREEPARENT:
793*0Sstevel@tonic-gate 		/*LINTED*/
794*0Sstevel@tonic-gate 		picld_get_frutree_parent((picl_service_t *)argp);
795*0Sstevel@tonic-gate 		break;
796*0Sstevel@tonic-gate 	default:
797*0Sstevel@tonic-gate 		/*LINTED*/
798*0Sstevel@tonic-gate 		picld_unknown_service((picl_service_t *)argp);
799*0Sstevel@tonic-gate 		break;
800*0Sstevel@tonic-gate 	};
801*0Sstevel@tonic-gate 	/*NOTREACHED*/
802*0Sstevel@tonic-gate }
803*0Sstevel@tonic-gate 
804*0Sstevel@tonic-gate /* ARGSUSED */
805*0Sstevel@tonic-gate static void
806*0Sstevel@tonic-gate hup_handler(int sig, siginfo_t *siginfo, void *sigctx)
807*0Sstevel@tonic-gate {
808*0Sstevel@tonic-gate 	doreinit = 1;
809*0Sstevel@tonic-gate }
810*0Sstevel@tonic-gate 
811*0Sstevel@tonic-gate /*
812*0Sstevel@tonic-gate  * "ping" to see if a daemon is already running
813*0Sstevel@tonic-gate  */
814*0Sstevel@tonic-gate static int
815*0Sstevel@tonic-gate daemon_exists(void)
816*0Sstevel@tonic-gate {
817*0Sstevel@tonic-gate 	door_arg_t	darg;
818*0Sstevel@tonic-gate 	picl_reqping_t	req_ping;
819*0Sstevel@tonic-gate 	picl_retping_t	ret_ping;
820*0Sstevel@tonic-gate 	int		doorh;
821*0Sstevel@tonic-gate 	door_info_t	dinfo;
822*0Sstevel@tonic-gate 
823*0Sstevel@tonic-gate 	doorh = open(PICLD_DOOR, O_RDONLY);
824*0Sstevel@tonic-gate 	if (doorh < 0)
825*0Sstevel@tonic-gate 		return (0);
826*0Sstevel@tonic-gate 
827*0Sstevel@tonic-gate 	if (door_info(doorh, &dinfo) < 0) {
828*0Sstevel@tonic-gate 		(void) close(doorh);
829*0Sstevel@tonic-gate 		return (0);
830*0Sstevel@tonic-gate 	}
831*0Sstevel@tonic-gate 
832*0Sstevel@tonic-gate 	if ((dinfo.di_attributes & DOOR_REVOKED) ||
833*0Sstevel@tonic-gate 	    (dinfo.di_data != (door_ptr_t)PICLD_DOOR_COOKIE)) {
834*0Sstevel@tonic-gate 		(void) close(doorh);
835*0Sstevel@tonic-gate 		return (0);
836*0Sstevel@tonic-gate 	}
837*0Sstevel@tonic-gate 
838*0Sstevel@tonic-gate 	if (dinfo.di_target != getpid()) {
839*0Sstevel@tonic-gate 		(void) close(doorh);
840*0Sstevel@tonic-gate 		return (1);
841*0Sstevel@tonic-gate 	}
842*0Sstevel@tonic-gate 
843*0Sstevel@tonic-gate 	req_ping.cnum = PICL_CNUM_PING;
844*0Sstevel@tonic-gate 
845*0Sstevel@tonic-gate 	darg.data_ptr = (char *)&req_ping;
846*0Sstevel@tonic-gate 	darg.data_size = sizeof (picl_reqping_t);
847*0Sstevel@tonic-gate 	darg.desc_ptr = NULL;
848*0Sstevel@tonic-gate 	darg.desc_num = 0;
849*0Sstevel@tonic-gate 	darg.rbuf = (char *)&ret_ping;
850*0Sstevel@tonic-gate 	darg.rsize = sizeof (picl_retping_t);
851*0Sstevel@tonic-gate 
852*0Sstevel@tonic-gate 	if (door_call(doorh, &darg) < 0) {
853*0Sstevel@tonic-gate 		(void) close(doorh);
854*0Sstevel@tonic-gate 		return (0);
855*0Sstevel@tonic-gate 	}
856*0Sstevel@tonic-gate 
857*0Sstevel@tonic-gate 	(void) close(doorh);
858*0Sstevel@tonic-gate 	return (1);
859*0Sstevel@tonic-gate }
860*0Sstevel@tonic-gate 
861*0Sstevel@tonic-gate /*
862*0Sstevel@tonic-gate  * Create the picld door
863*0Sstevel@tonic-gate  */
864*0Sstevel@tonic-gate static int
865*0Sstevel@tonic-gate setup_door(void)
866*0Sstevel@tonic-gate {
867*0Sstevel@tonic-gate 	struct stat	stbuf;
868*0Sstevel@tonic-gate 
869*0Sstevel@tonic-gate 	/*
870*0Sstevel@tonic-gate 	 * Create the door
871*0Sstevel@tonic-gate 	 */
872*0Sstevel@tonic-gate 	door_id = door_create(picld_door_handler, PICLD_DOOR_COOKIE,
873*0Sstevel@tonic-gate 	    DOOR_UNREF | DOOR_REFUSE_DESC | DOOR_NO_CANCEL);
874*0Sstevel@tonic-gate 
875*0Sstevel@tonic-gate 	if (door_id < 0) {
876*0Sstevel@tonic-gate 		return (-1);
877*0Sstevel@tonic-gate 	}
878*0Sstevel@tonic-gate 
879*0Sstevel@tonic-gate 	if (stat(PICLD_DOOR, &stbuf) < 0) {
880*0Sstevel@tonic-gate 		int newfd;
881*0Sstevel@tonic-gate 		if ((newfd = creat(PICLD_DOOR, 0444)) < 0)
882*0Sstevel@tonic-gate 			return (-1);
883*0Sstevel@tonic-gate 		(void) close(newfd);
884*0Sstevel@tonic-gate 	}
885*0Sstevel@tonic-gate 
886*0Sstevel@tonic-gate 	if (fattach(door_id, PICLD_DOOR) < 0) {
887*0Sstevel@tonic-gate 		if ((errno != EBUSY) ||
888*0Sstevel@tonic-gate 		    (fdetach(PICLD_DOOR) < 0) ||
889*0Sstevel@tonic-gate 		    (fattach(door_id, PICLD_DOOR) < 0))
890*0Sstevel@tonic-gate 			return (-1);
891*0Sstevel@tonic-gate 	}
892*0Sstevel@tonic-gate 
893*0Sstevel@tonic-gate 	return (0);
894*0Sstevel@tonic-gate }
895*0Sstevel@tonic-gate 
896*0Sstevel@tonic-gate /*
897*0Sstevel@tonic-gate  * Main function of picl daemon
898*0Sstevel@tonic-gate  */
899*0Sstevel@tonic-gate int
900*0Sstevel@tonic-gate main(int argc, char **argv)
901*0Sstevel@tonic-gate {
902*0Sstevel@tonic-gate 	struct	sigaction	act;
903*0Sstevel@tonic-gate 	int			c;
904*0Sstevel@tonic-gate 	sigset_t		ublk;
905*0Sstevel@tonic-gate 
906*0Sstevel@tonic-gate 
907*0Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
908*0Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
909*0Sstevel@tonic-gate 
910*0Sstevel@tonic-gate 	if (getuid() != 0) {
911*0Sstevel@tonic-gate 		syslog(LOG_CRIT, MUST_BE_ROOT);
912*0Sstevel@tonic-gate 		return (0);
913*0Sstevel@tonic-gate 	}
914*0Sstevel@tonic-gate 
915*0Sstevel@tonic-gate 	(void) rwlock_init(&init_lk, USYNC_THREAD, NULL);
916*0Sstevel@tonic-gate 	doreinit = 0;
917*0Sstevel@tonic-gate 	logflag = 1;
918*0Sstevel@tonic-gate 	dos_req_limit = DOS_PICL_REQUESTS_LIMIT;
919*0Sstevel@tonic-gate 	sliding_interval_ms = SLIDING_INTERVAL_MILLISECONDS;
920*0Sstevel@tonic-gate 	dos_ms = DOS_SLEEPTIME_MS;
921*0Sstevel@tonic-gate 	verbose_level = 0;
922*0Sstevel@tonic-gate 
923*0Sstevel@tonic-gate 	/*
924*0Sstevel@tonic-gate 	 * parse arguments
925*0Sstevel@tonic-gate 	 */
926*0Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "is:t:l:r:v:d:")) != EOF) {
927*0Sstevel@tonic-gate 		switch (c) {
928*0Sstevel@tonic-gate 		case 'd':
929*0Sstevel@tonic-gate 			dos_ms = strtol(optarg, (char **)NULL, 0);
930*0Sstevel@tonic-gate 			break;
931*0Sstevel@tonic-gate 		case 'i':
932*0Sstevel@tonic-gate 			logflag = 0;
933*0Sstevel@tonic-gate 			break;
934*0Sstevel@tonic-gate 		case 's':
935*0Sstevel@tonic-gate 			sliding_interval_ms = strtoll(optarg, (char **)NULL, 0);
936*0Sstevel@tonic-gate 			break;
937*0Sstevel@tonic-gate 		case 't':
938*0Sstevel@tonic-gate 			dos_req_limit = strtol(optarg, (char **)NULL, 0);
939*0Sstevel@tonic-gate 			break;
940*0Sstevel@tonic-gate 		case 'v':
941*0Sstevel@tonic-gate 			verbose_level = strtol(optarg, (char **)NULL, 0);
942*0Sstevel@tonic-gate 			logflag = 0;
943*0Sstevel@tonic-gate 			break;
944*0Sstevel@tonic-gate 		default:
945*0Sstevel@tonic-gate 			break;
946*0Sstevel@tonic-gate 		}
947*0Sstevel@tonic-gate 	}
948*0Sstevel@tonic-gate 
949*0Sstevel@tonic-gate 	orig_time = gethrtime();
950*0Sstevel@tonic-gate 
951*0Sstevel@tonic-gate 	/*
952*0Sstevel@tonic-gate 	 * is there a daemon already running?
953*0Sstevel@tonic-gate 	 */
954*0Sstevel@tonic-gate 
955*0Sstevel@tonic-gate 	if (daemon_exists()) {
956*0Sstevel@tonic-gate 		syslog(LOG_CRIT, DAEMON_RUNNING);
957*0Sstevel@tonic-gate 		exit(1);
958*0Sstevel@tonic-gate 	}
959*0Sstevel@tonic-gate 
960*0Sstevel@tonic-gate 	/*
961*0Sstevel@tonic-gate 	 * Mask off/block SIGALRM signal so that the environmental plug-in
962*0Sstevel@tonic-gate 	 * (piclenvd) can use it to simulate sleep() without being affected
963*0Sstevel@tonic-gate 	 * by time being set back. No other PICL plug-in should use SIGALRM
964*0Sstevel@tonic-gate 	 * or alarm() for now.
965*0Sstevel@tonic-gate 	 */
966*0Sstevel@tonic-gate 	(void) sigemptyset(&ublk);
967*0Sstevel@tonic-gate 	(void) sigaddset(&ublk, SIGALRM);
968*0Sstevel@tonic-gate 	(void) sigprocmask(SIG_BLOCK, &ublk, NULL);
969*0Sstevel@tonic-gate 
970*0Sstevel@tonic-gate 	/*
971*0Sstevel@tonic-gate 	 * Ignore SIGHUP until all the initialization is done.
972*0Sstevel@tonic-gate 	 */
973*0Sstevel@tonic-gate 	act.sa_handler = SIG_IGN;
974*0Sstevel@tonic-gate 	(void) sigemptyset(&act.sa_mask);
975*0Sstevel@tonic-gate 	act.sa_flags = 0;
976*0Sstevel@tonic-gate 	if (sigaction(SIGHUP, &act, NULL) == -1)
977*0Sstevel@tonic-gate 		syslog(LOG_ERR, SIGACT_FAILED, strsignal(SIGHUP),
978*0Sstevel@tonic-gate 		    strerror(errno));
979*0Sstevel@tonic-gate 
980*0Sstevel@tonic-gate 	if (logflag != 0) {	/* daemonize */
981*0Sstevel@tonic-gate 		pid_t pid;
982*0Sstevel@tonic-gate 
983*0Sstevel@tonic-gate 		pid = fork();
984*0Sstevel@tonic-gate 		if (pid < 0)
985*0Sstevel@tonic-gate 			exit(1);
986*0Sstevel@tonic-gate 		if (pid > 0)
987*0Sstevel@tonic-gate 			/* parent */
988*0Sstevel@tonic-gate 			exit(0);
989*0Sstevel@tonic-gate 
990*0Sstevel@tonic-gate 		/* child */
991*0Sstevel@tonic-gate 		if (chdir("/") == -1) {
992*0Sstevel@tonic-gate 			syslog(LOG_CRIT, CD_ROOT_FAILED);
993*0Sstevel@tonic-gate 			exit(1);
994*0Sstevel@tonic-gate 		}
995*0Sstevel@tonic-gate 
996*0Sstevel@tonic-gate 		(void) setsid();
997*0Sstevel@tonic-gate 		(void) close(STDIN_FILENO);
998*0Sstevel@tonic-gate 		(void) close(STDOUT_FILENO);
999*0Sstevel@tonic-gate 		(void) close(STDERR_FILENO);
1000*0Sstevel@tonic-gate 		(void) open("/dev/null", O_RDWR, 0);
1001*0Sstevel@tonic-gate 		(void) dup2(STDIN_FILENO, STDOUT_FILENO);
1002*0Sstevel@tonic-gate 		(void) dup2(STDIN_FILENO, STDERR_FILENO);
1003*0Sstevel@tonic-gate 		openlog(PICLD, LOG_PID, LOG_DAEMON);
1004*0Sstevel@tonic-gate 	}
1005*0Sstevel@tonic-gate 
1006*0Sstevel@tonic-gate 	/*
1007*0Sstevel@tonic-gate 	 * Initialize the PICL Tree
1008*0Sstevel@tonic-gate 	 */
1009*0Sstevel@tonic-gate 	if (xptree_initialize(NULL) != PICL_SUCCESS) {
1010*0Sstevel@tonic-gate 		syslog(LOG_CRIT, INIT_FAILED);
1011*0Sstevel@tonic-gate 		exit(1);
1012*0Sstevel@tonic-gate 	}
1013*0Sstevel@tonic-gate 
1014*0Sstevel@tonic-gate 	if (setup_door()) {
1015*0Sstevel@tonic-gate 		syslog(LOG_CRIT, DOOR_FAILED);
1016*0Sstevel@tonic-gate 		exit(1);
1017*0Sstevel@tonic-gate 	}
1018*0Sstevel@tonic-gate 
1019*0Sstevel@tonic-gate 	/*
1020*0Sstevel@tonic-gate 	 * setup signal handlers for post-init
1021*0Sstevel@tonic-gate 	 */
1022*0Sstevel@tonic-gate 	act.sa_sigaction = hup_handler;
1023*0Sstevel@tonic-gate 	(void) sigemptyset(&act.sa_mask);
1024*0Sstevel@tonic-gate 	act.sa_flags = SA_SIGINFO;
1025*0Sstevel@tonic-gate 	if (sigaction(SIGHUP, &act, NULL) == -1)
1026*0Sstevel@tonic-gate 		syslog(LOG_ERR, SIGACT_FAILED, strsignal(SIGHUP),
1027*0Sstevel@tonic-gate 		    strerror(errno));
1028*0Sstevel@tonic-gate 
1029*0Sstevel@tonic-gate 	/*
1030*0Sstevel@tonic-gate 	 * wait for requests
1031*0Sstevel@tonic-gate 	 */
1032*0Sstevel@tonic-gate 	for (;;) {
1033*0Sstevel@tonic-gate 		(void) pause();
1034*0Sstevel@tonic-gate 		if (doreinit) {
1035*0Sstevel@tonic-gate 			/*
1036*0Sstevel@tonic-gate 			 * Block SIGHUP during reinitialization.
1037*0Sstevel@tonic-gate 			 * Also mask off/block SIGALRM signal so that the
1038*0Sstevel@tonic-gate 			 * environmental plug-in (piclenvd) can use it to
1039*0Sstevel@tonic-gate 			 * simulate sleep() without being affected by time
1040*0Sstevel@tonic-gate 			 * being set back. No ohter PICL plug-in should use
1041*0Sstevel@tonic-gate 			 * SIGALRM or alarm() for now.
1042*0Sstevel@tonic-gate 			 */
1043*0Sstevel@tonic-gate 			(void) sigemptyset(&ublk);
1044*0Sstevel@tonic-gate 			(void) sigaddset(&ublk, SIGHUP);
1045*0Sstevel@tonic-gate 			(void) sigaddset(&ublk, SIGALRM);
1046*0Sstevel@tonic-gate 			(void) sigprocmask(SIG_BLOCK, &ublk, NULL);
1047*0Sstevel@tonic-gate 			(void) sigdelset(&ublk, SIGALRM);
1048*0Sstevel@tonic-gate 			doreinit = 0;
1049*0Sstevel@tonic-gate 			(void) rw_wrlock(&init_lk);
1050*0Sstevel@tonic-gate 			xptree_destroy();
1051*0Sstevel@tonic-gate 			(void) xptree_reinitialize();
1052*0Sstevel@tonic-gate 			(void) rw_unlock(&init_lk);
1053*0Sstevel@tonic-gate 			(void) sigprocmask(SIG_UNBLOCK, &ublk, NULL);
1054*0Sstevel@tonic-gate 		}
1055*0Sstevel@tonic-gate 	}
1056*0Sstevel@tonic-gate }
1057