xref: /onnv-gate/usr/src/lib/libpicl/picl.c (revision 0:68f95e015346)
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 2004 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  * This module implements the PICL Interface used by PICL clients
31*0Sstevel@tonic-gate  * to access services of the PICL daemon
32*0Sstevel@tonic-gate  *
33*0Sstevel@tonic-gate  * Locking Strategy
34*0Sstevel@tonic-gate  * A single reader/writer lock (icl_lock) protects the access to the interface
35*0Sstevel@tonic-gate  * to the picl daemon, and the reference count, refcnt, variable.
36*0Sstevel@tonic-gate  * A reader lock is obtained to send a request to the daemon.
37*0Sstevel@tonic-gate  * A writer lock is obtained to initialize, reinitialize, or shutdown
38*0Sstevel@tonic-gate  * the interface.
39*0Sstevel@tonic-gate  */
40*0Sstevel@tonic-gate 
41*0Sstevel@tonic-gate #include <stdio.h>
42*0Sstevel@tonic-gate #include <stdlib.h>
43*0Sstevel@tonic-gate #include <string.h>
44*0Sstevel@tonic-gate #include <unistd.h>
45*0Sstevel@tonic-gate #include <alloca.h>
46*0Sstevel@tonic-gate #include <fcntl.h>
47*0Sstevel@tonic-gate #include <libintl.h>
48*0Sstevel@tonic-gate #include <errno.h>
49*0Sstevel@tonic-gate #include <sys/mman.h>
50*0Sstevel@tonic-gate #include <door.h>
51*0Sstevel@tonic-gate #include <sys/door.h>
52*0Sstevel@tonic-gate #include <sys/time.h>
53*0Sstevel@tonic-gate #include <assert.h>
54*0Sstevel@tonic-gate #include <synch.h>
55*0Sstevel@tonic-gate #include <limits.h>
56*0Sstevel@tonic-gate #include <picl.h>
57*0Sstevel@tonic-gate #include "picl2door.h"
58*0Sstevel@tonic-gate 
59*0Sstevel@tonic-gate /*
60*0Sstevel@tonic-gate  * Module variables
61*0Sstevel@tonic-gate  */
62*0Sstevel@tonic-gate static	int		door_handle = -1;
63*0Sstevel@tonic-gate static	uint32_t	refcnt = 0;
64*0Sstevel@tonic-gate static	rwlock_t	picl_lock = DEFAULTRWLOCK;
65*0Sstevel@tonic-gate 
66*0Sstevel@tonic-gate static	char 		*picl_errmsg[] = {
67*0Sstevel@tonic-gate 	"No error",
68*0Sstevel@tonic-gate 	"General system failure",
69*0Sstevel@tonic-gate 	"Daemon not responding",
70*0Sstevel@tonic-gate 	"Unknown PICL service",
71*0Sstevel@tonic-gate 	"Session not initialized",
72*0Sstevel@tonic-gate 	"Invalid arguments",
73*0Sstevel@tonic-gate 	"Argument too big",
74*0Sstevel@tonic-gate 	"Property not found",
75*0Sstevel@tonic-gate 	"Not a table property handle",
76*0Sstevel@tonic-gate 	"Not a node handle",
77*0Sstevel@tonic-gate 	"Not a property handle",
78*0Sstevel@tonic-gate 	"End of property list",
79*0Sstevel@tonic-gate 	"Property already exists",
80*0Sstevel@tonic-gate 	"Property not writable",
81*0Sstevel@tonic-gate 	"Insufficient permissions",
82*0Sstevel@tonic-gate 	"Invalid handle",
83*0Sstevel@tonic-gate 	"Stale handle",
84*0Sstevel@tonic-gate 	"Unsupported version",
85*0Sstevel@tonic-gate 	"Wait timed out",
86*0Sstevel@tonic-gate 	"Attempting to destroy before delete",
87*0Sstevel@tonic-gate 	"PICL Tree is busy",
88*0Sstevel@tonic-gate 	"Already has a parent",
89*0Sstevel@tonic-gate 	"Property name is reserved",
90*0Sstevel@tonic-gate 	"Invalid reference value",
91*0Sstevel@tonic-gate 	"Continue tree walk",
92*0Sstevel@tonic-gate 	"Terminate tree walk",
93*0Sstevel@tonic-gate 	"Node not found",
94*0Sstevel@tonic-gate 	"Not enough space available",
95*0Sstevel@tonic-gate 	"Property not readable",
96*0Sstevel@tonic-gate 	"Property value unavailable"
97*0Sstevel@tonic-gate };
98*0Sstevel@tonic-gate 
99*0Sstevel@tonic-gate #define	N_ERRORS 		(sizeof (picl_errmsg)/sizeof (picl_errmsg[0]))
100*0Sstevel@tonic-gate #define	SEND_REQ_TRYCOUNT	1
101*0Sstevel@tonic-gate 
102*0Sstevel@tonic-gate /*
103*0Sstevel@tonic-gate  * This function sends the client request to the daemon using a door call.
104*0Sstevel@tonic-gate  * If door_handle is -1, it returns PICL_NOTINITIALIZED.
105*0Sstevel@tonic-gate  * If the door_call fails, it returns PICL_NORESPONSE. Otherwise, it
106*0Sstevel@tonic-gate  * checks the response from the daemon for error. If an error is returned
107*0Sstevel@tonic-gate  * this function returns the error code returned and unmaps any
108*0Sstevel@tonic-gate  * memory mapped by the door call. For successful results, the caller is
109*0Sstevel@tonic-gate  * responsible to unmap the mapped memory after retrieving the results.
110*0Sstevel@tonic-gate  *
111*0Sstevel@tonic-gate  * This function does not attempt to reinitialize the interface if the
112*0Sstevel@tonic-gate  * initial door_call fails. It is called from handshake() , shutdown()
113*0Sstevel@tonic-gate  * and trysend_req() routines.
114*0Sstevel@tonic-gate  */
115*0Sstevel@tonic-gate static int
post_req(door_arg_t * dargp,void * data_ptr,size_t data_size,door_desc_t * desc_ptr,uint_t desc_num,void * rbuf,size_t rsize)116*0Sstevel@tonic-gate post_req(door_arg_t *dargp, void *data_ptr, size_t data_size,
117*0Sstevel@tonic-gate     door_desc_t *desc_ptr, uint_t desc_num, void *rbuf, size_t rsize)
118*0Sstevel@tonic-gate {
119*0Sstevel@tonic-gate 	int		err;
120*0Sstevel@tonic-gate 	picl_service_t	*ret;
121*0Sstevel@tonic-gate 	int		req_cnum;
122*0Sstevel@tonic-gate 
123*0Sstevel@tonic-gate 	req_cnum = ((picl_service_t *)data_ptr)->in.cnum;
124*0Sstevel@tonic-gate 	dargp->data_ptr = data_ptr;
125*0Sstevel@tonic-gate 	dargp->data_size = data_size;
126*0Sstevel@tonic-gate 	dargp->desc_ptr = desc_ptr;
127*0Sstevel@tonic-gate 	dargp->desc_num = desc_num;
128*0Sstevel@tonic-gate 	dargp->rbuf = rbuf;
129*0Sstevel@tonic-gate 	dargp->rsize = rsize;
130*0Sstevel@tonic-gate 
131*0Sstevel@tonic-gate 	if (door_call(door_handle, dargp) < 0)
132*0Sstevel@tonic-gate 		return (PICL_NORESPONSE);
133*0Sstevel@tonic-gate 
134*0Sstevel@tonic-gate 	/*LINTED*/
135*0Sstevel@tonic-gate 	ret = (picl_service_t *)dargp->rbuf;
136*0Sstevel@tonic-gate 	if (ret->in.cnum == req_cnum)
137*0Sstevel@tonic-gate 		return (PICL_SUCCESS);
138*0Sstevel@tonic-gate 	else if ((ret->in.cnum == PICL_CNUM_ERROR) &&
139*0Sstevel@tonic-gate 	    (ret->ret_error.in_cnum == req_cnum))
140*0Sstevel@tonic-gate 		err = ret->ret_error.errnum;
141*0Sstevel@tonic-gate 	else
142*0Sstevel@tonic-gate 	    err = PICL_UNKNOWNSERVICE;
143*0Sstevel@tonic-gate 	if (dargp->rbuf != rbuf)
144*0Sstevel@tonic-gate 		(void) munmap(dargp->rbuf, dargp->rsize);
145*0Sstevel@tonic-gate 	return (err);
146*0Sstevel@tonic-gate }
147*0Sstevel@tonic-gate 
148*0Sstevel@tonic-gate /*
149*0Sstevel@tonic-gate  * This function posts an INIT message to the daemon to
150*0Sstevel@tonic-gate  * verify communication channel.
151*0Sstevel@tonic-gate  */
152*0Sstevel@tonic-gate static int
handshake(void)153*0Sstevel@tonic-gate handshake(void)
154*0Sstevel@tonic-gate {
155*0Sstevel@tonic-gate 	int		err;
156*0Sstevel@tonic-gate 	door_arg_t	darg;
157*0Sstevel@tonic-gate 	picl_reqinit_t	req;
158*0Sstevel@tonic-gate 	picl_retinit_t	outargs;
159*0Sstevel@tonic-gate 
160*0Sstevel@tonic-gate 	req.cnum = PICL_CNUM_INIT;
161*0Sstevel@tonic-gate 	req.clrev = PICL_VERSION_1;
162*0Sstevel@tonic-gate 
163*0Sstevel@tonic-gate 	if ((err = post_req(&darg, &req, sizeof (picl_reqinit_t), NULL,
164*0Sstevel@tonic-gate 	    0, &outargs, sizeof (picl_retinit_t))) != PICL_SUCCESS)
165*0Sstevel@tonic-gate 		return (err);
166*0Sstevel@tonic-gate 
167*0Sstevel@tonic-gate 	if (darg.rbuf != (char *)&outargs)
168*0Sstevel@tonic-gate 		(void) munmap(darg.rbuf, darg.rsize);
169*0Sstevel@tonic-gate 	return (PICL_SUCCESS);
170*0Sstevel@tonic-gate }
171*0Sstevel@tonic-gate 
172*0Sstevel@tonic-gate /*
173*0Sstevel@tonic-gate  * This function calls post_req() to make door_call and reinitializes
174*0Sstevel@tonic-gate  * the interface is post_req() fails.
175*0Sstevel@tonic-gate  */
176*0Sstevel@tonic-gate static int
trysend_req(door_arg_t * dargp,void * data_ptr,size_t data_size,door_desc_t * desc_ptr,uint_t desc_num,void * rbuf,size_t rsize,unsigned int trycount)177*0Sstevel@tonic-gate trysend_req(door_arg_t *dargp, void *data_ptr, size_t data_size,
178*0Sstevel@tonic-gate     door_desc_t *desc_ptr, uint_t desc_num, void *rbuf, size_t rsize,
179*0Sstevel@tonic-gate 	unsigned int trycount)
180*0Sstevel@tonic-gate {
181*0Sstevel@tonic-gate 	int	err;
182*0Sstevel@tonic-gate 	int	write_locked;
183*0Sstevel@tonic-gate 
184*0Sstevel@tonic-gate 	write_locked = 0;
185*0Sstevel@tonic-gate 	(void) rw_rdlock(&picl_lock);
186*0Sstevel@tonic-gate 	if (refcnt == 0) {
187*0Sstevel@tonic-gate 		(void) rw_unlock(&picl_lock);	/* read unlock */
188*0Sstevel@tonic-gate 		return (PICL_NOTINITIALIZED);
189*0Sstevel@tonic-gate 	}
190*0Sstevel@tonic-gate 
191*0Sstevel@tonic-gate 	while ((err = post_req(dargp, data_ptr, data_size, desc_ptr, desc_num,
192*0Sstevel@tonic-gate 	    rbuf, rsize)) == PICL_NORESPONSE) {
193*0Sstevel@tonic-gate 		if (trycount == 0)	/* no more retry */
194*0Sstevel@tonic-gate 			break;
195*0Sstevel@tonic-gate 
196*0Sstevel@tonic-gate 		if (write_locked == 1) {	/* close and open door */
197*0Sstevel@tonic-gate 			(void) close(door_handle);
198*0Sstevel@tonic-gate 			if ((door_handle = open(PICLD_DOOR, O_RDONLY)) < 0) {
199*0Sstevel@tonic-gate 				err = PICL_NORESPONSE;
200*0Sstevel@tonic-gate 				break;
201*0Sstevel@tonic-gate 			}
202*0Sstevel@tonic-gate 			--trycount;
203*0Sstevel@tonic-gate 			continue;
204*0Sstevel@tonic-gate 		}
205*0Sstevel@tonic-gate 		/*
206*0Sstevel@tonic-gate 		 * Upgrade read to a write lock
207*0Sstevel@tonic-gate 		 */
208*0Sstevel@tonic-gate 		(void) rw_unlock(&picl_lock);
209*0Sstevel@tonic-gate 		(void) rw_wrlock(&picl_lock);
210*0Sstevel@tonic-gate 
211*0Sstevel@tonic-gate 		/*
212*0Sstevel@tonic-gate 		 * if picl_shutdown happens during lock upgrade
213*0Sstevel@tonic-gate 		 */
214*0Sstevel@tonic-gate 		if (refcnt == 0) {
215*0Sstevel@tonic-gate 			err =  PICL_NOTINITIALIZED;
216*0Sstevel@tonic-gate 			break;
217*0Sstevel@tonic-gate 		}
218*0Sstevel@tonic-gate 		write_locked = 1;
219*0Sstevel@tonic-gate 		continue;
220*0Sstevel@tonic-gate 	}
221*0Sstevel@tonic-gate 	(void) rw_unlock(&picl_lock);	/* read or write unlock */
222*0Sstevel@tonic-gate 	return (err);
223*0Sstevel@tonic-gate }
224*0Sstevel@tonic-gate 
225*0Sstevel@tonic-gate /*
226*0Sstevel@tonic-gate  * Initialize the PICL interface
227*0Sstevel@tonic-gate  * Increment the reference count.
228*0Sstevel@tonic-gate  */
229*0Sstevel@tonic-gate int
picl_initialize(void)230*0Sstevel@tonic-gate picl_initialize(void)
231*0Sstevel@tonic-gate {
232*0Sstevel@tonic-gate 	int	err;
233*0Sstevel@tonic-gate 
234*0Sstevel@tonic-gate 	(void) rw_wrlock(&picl_lock);
235*0Sstevel@tonic-gate 	if (refcnt > 0) {		/* previously initialized */
236*0Sstevel@tonic-gate 		err = handshake();
237*0Sstevel@tonic-gate 		if (err == PICL_SUCCESS) {
238*0Sstevel@tonic-gate 			++refcnt;
239*0Sstevel@tonic-gate 			(void) rw_unlock(&picl_lock);	/* write unlock */
240*0Sstevel@tonic-gate 			return (err);
241*0Sstevel@tonic-gate 		}
242*0Sstevel@tonic-gate 		if (err != PICL_NORESPONSE) {
243*0Sstevel@tonic-gate 			(void) rw_unlock(&picl_lock);	/* write unlock */
244*0Sstevel@tonic-gate 			return (err);
245*0Sstevel@tonic-gate 		}
246*0Sstevel@tonic-gate 		(void) close(door_handle);	/* close bad door */
247*0Sstevel@tonic-gate 	}
248*0Sstevel@tonic-gate 
249*0Sstevel@tonic-gate 	/*
250*0Sstevel@tonic-gate 	 * Open picld door and initialize door_handle
251*0Sstevel@tonic-gate 	 */
252*0Sstevel@tonic-gate 	if ((door_handle = open(PICLD_DOOR, O_RDONLY)) < 0) {
253*0Sstevel@tonic-gate 		(void) rw_unlock(&picl_lock); /* write unlock */
254*0Sstevel@tonic-gate 		return (PICL_NORESPONSE);
255*0Sstevel@tonic-gate 	}
256*0Sstevel@tonic-gate 
257*0Sstevel@tonic-gate 	err = handshake();
258*0Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
259*0Sstevel@tonic-gate 		(void) close(door_handle);
260*0Sstevel@tonic-gate 	else
261*0Sstevel@tonic-gate 		++refcnt;
262*0Sstevel@tonic-gate 	(void) rw_unlock(&picl_lock);	/* write unlock */
263*0Sstevel@tonic-gate 	return (err);
264*0Sstevel@tonic-gate }
265*0Sstevel@tonic-gate 
266*0Sstevel@tonic-gate /*
267*0Sstevel@tonic-gate  * Shutdown the PICL interface
268*0Sstevel@tonic-gate  * Decrement the reference count and close the door_handle if refcnt is zero
269*0Sstevel@tonic-gate  */
270*0Sstevel@tonic-gate int
picl_shutdown(void)271*0Sstevel@tonic-gate picl_shutdown(void)
272*0Sstevel@tonic-gate {
273*0Sstevel@tonic-gate 	int		err;
274*0Sstevel@tonic-gate 	door_arg_t	darg;
275*0Sstevel@tonic-gate 	picl_reqfini_t	req_fini;
276*0Sstevel@tonic-gate 	picl_retfini_t	outargs;
277*0Sstevel@tonic-gate 
278*0Sstevel@tonic-gate 	(void) rw_wrlock(&picl_lock);	/* write lock */
279*0Sstevel@tonic-gate 	if (refcnt == 0) {
280*0Sstevel@tonic-gate 		(void) rw_unlock(&picl_lock);	/* write unlock */
281*0Sstevel@tonic-gate 		return (PICL_NOTINITIALIZED);
282*0Sstevel@tonic-gate 	}
283*0Sstevel@tonic-gate 	req_fini.cnum = PICL_CNUM_FINI;
284*0Sstevel@tonic-gate 	err = post_req(&darg, &req_fini, sizeof (picl_reqfini_t),
285*0Sstevel@tonic-gate 	    NULL, 0, &outargs, sizeof (picl_retfini_t));
286*0Sstevel@tonic-gate 	--refcnt;
287*0Sstevel@tonic-gate 	if (refcnt == 0)
288*0Sstevel@tonic-gate 		(void) close(door_handle);
289*0Sstevel@tonic-gate 	(void) rw_unlock(&picl_lock);	/* write unlock */
290*0Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
291*0Sstevel@tonic-gate 		return (err);
292*0Sstevel@tonic-gate 	if (darg.rbuf != (char *)&outargs)
293*0Sstevel@tonic-gate 		(void) munmap(darg.rbuf, darg.rsize);
294*0Sstevel@tonic-gate 	return (PICL_SUCCESS);
295*0Sstevel@tonic-gate }
296*0Sstevel@tonic-gate 
297*0Sstevel@tonic-gate /*
298*0Sstevel@tonic-gate  * This function waits for the specified number of seconds for a PICL
299*0Sstevel@tonic-gate  * tree refresh.
300*0Sstevel@tonic-gate  */
301*0Sstevel@tonic-gate int
picl_wait(unsigned int secs)302*0Sstevel@tonic-gate picl_wait(unsigned int secs)
303*0Sstevel@tonic-gate {
304*0Sstevel@tonic-gate 	door_arg_t	darg;
305*0Sstevel@tonic-gate 	picl_reqwait_t	req_wait;
306*0Sstevel@tonic-gate 	picl_retwait_t	outargs;
307*0Sstevel@tonic-gate 	picl_service_t	*ret;
308*0Sstevel@tonic-gate 	int		err;
309*0Sstevel@tonic-gate 
310*0Sstevel@tonic-gate 	req_wait.cnum = PICL_CNUM_WAIT;
311*0Sstevel@tonic-gate 	req_wait.secs = secs;
312*0Sstevel@tonic-gate 	err = trysend_req(&darg, &req_wait, sizeof (picl_reqwait_t),
313*0Sstevel@tonic-gate 	    NULL, 0, &outargs, sizeof (picl_retwait_t), SEND_REQ_TRYCOUNT);
314*0Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
315*0Sstevel@tonic-gate 		return (err);
316*0Sstevel@tonic-gate 
317*0Sstevel@tonic-gate 	/*LINTED*/
318*0Sstevel@tonic-gate 	ret = (picl_service_t *)darg.rbuf;
319*0Sstevel@tonic-gate 	err = ret->ret_wait.retcode;
320*0Sstevel@tonic-gate 	if (darg.rbuf != (char *)&outargs)
321*0Sstevel@tonic-gate 		(void) munmap(darg.rbuf, darg.rsize);
322*0Sstevel@tonic-gate 	return (err);
323*0Sstevel@tonic-gate }
324*0Sstevel@tonic-gate 
325*0Sstevel@tonic-gate /*
326*0Sstevel@tonic-gate  * This function copies the handle of the root node of the PICL tree into
327*0Sstevel@tonic-gate  * the buffer <rooth>
328*0Sstevel@tonic-gate  */
329*0Sstevel@tonic-gate int
picl_get_root(picl_nodehdl_t * rooth)330*0Sstevel@tonic-gate picl_get_root(picl_nodehdl_t *rooth)
331*0Sstevel@tonic-gate {
332*0Sstevel@tonic-gate 	door_arg_t	darg;
333*0Sstevel@tonic-gate 	picl_reqroot_t	req_root;
334*0Sstevel@tonic-gate 	picl_retroot_t	outargs;
335*0Sstevel@tonic-gate 	picl_service_t	*ret;
336*0Sstevel@tonic-gate 	int	err;
337*0Sstevel@tonic-gate 
338*0Sstevel@tonic-gate 	req_root.cnum = PICL_CNUM_GETROOT;
339*0Sstevel@tonic-gate 	err = trysend_req(&darg, &req_root, sizeof (picl_reqroot_t), NULL,
340*0Sstevel@tonic-gate 	    0, &outargs, sizeof (picl_retroot_t), SEND_REQ_TRYCOUNT);
341*0Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
342*0Sstevel@tonic-gate 		return (err);
343*0Sstevel@tonic-gate 	/*LINTED*/
344*0Sstevel@tonic-gate 	ret = (picl_service_t *)darg.rbuf;
345*0Sstevel@tonic-gate 	*rooth = ret->ret_root.rnode;
346*0Sstevel@tonic-gate 	if (darg.rbuf != (char *)&outargs)
347*0Sstevel@tonic-gate 		(void) munmap(darg.rbuf, darg.rsize);
348*0Sstevel@tonic-gate 	return (PICL_SUCCESS);
349*0Sstevel@tonic-gate }
350*0Sstevel@tonic-gate 
351*0Sstevel@tonic-gate /*
352*0Sstevel@tonic-gate  * This function copies the value of the property specified by its handle
353*0Sstevel@tonic-gate  * into the buffer <valbuf>.
354*0Sstevel@tonic-gate  */
355*0Sstevel@tonic-gate int
picl_get_propval(picl_prophdl_t proph,void * valbuf,size_t nbytes)356*0Sstevel@tonic-gate picl_get_propval(picl_prophdl_t proph, void *valbuf, size_t nbytes)
357*0Sstevel@tonic-gate {
358*0Sstevel@tonic-gate 	door_arg_t		darg;
359*0Sstevel@tonic-gate 	picl_reqattrval_t	req_attrval;
360*0Sstevel@tonic-gate 	picl_service_t		*ret;
361*0Sstevel@tonic-gate 	picl_retattrval_t	*outargs;
362*0Sstevel@tonic-gate 	int			err;
363*0Sstevel@tonic-gate 
364*0Sstevel@tonic-gate 	req_attrval.cnum = PICL_CNUM_GETATTRVAL;
365*0Sstevel@tonic-gate 	req_attrval.attr = proph;
366*0Sstevel@tonic-gate 	req_attrval.bufsize = (uint32_t)nbytes;
367*0Sstevel@tonic-gate 	if ((size_t)req_attrval.bufsize != nbytes)
368*0Sstevel@tonic-gate 		return (PICL_VALUETOOBIG);
369*0Sstevel@tonic-gate 	outargs = alloca(sizeof (picl_retattrval_t) + nbytes);
370*0Sstevel@tonic-gate 
371*0Sstevel@tonic-gate 	err = trysend_req(&darg, &req_attrval, sizeof (picl_reqattrval_t),
372*0Sstevel@tonic-gate 	    NULL, 0, outargs, sizeof (picl_retattrval_t) + nbytes,
373*0Sstevel@tonic-gate 	    SEND_REQ_TRYCOUNT);
374*0Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
375*0Sstevel@tonic-gate 		return (err);
376*0Sstevel@tonic-gate 
377*0Sstevel@tonic-gate 	/*LINTED*/
378*0Sstevel@tonic-gate 	ret = (picl_service_t *)darg.rbuf;
379*0Sstevel@tonic-gate 	if (ret->ret_attrval.nbytes > (uint32_t)nbytes)
380*0Sstevel@tonic-gate 		err = PICL_VALUETOOBIG;
381*0Sstevel@tonic-gate 	else
382*0Sstevel@tonic-gate 		(void) memcpy(valbuf, ret->ret_attrval.ret_buf,
383*0Sstevel@tonic-gate 		    (size_t)ret->ret_attrval.nbytes);
384*0Sstevel@tonic-gate 	if (darg.rbuf != (char *)outargs)
385*0Sstevel@tonic-gate 		(void) munmap(darg.rbuf, darg.rsize);
386*0Sstevel@tonic-gate 	return (err);
387*0Sstevel@tonic-gate }
388*0Sstevel@tonic-gate 
389*0Sstevel@tonic-gate /*
390*0Sstevel@tonic-gate  * This function copies the value of the property specified by its
391*0Sstevel@tonic-gate  * name into the buffer <valbuf>
392*0Sstevel@tonic-gate  */
393*0Sstevel@tonic-gate int
picl_get_propval_by_name(picl_nodehdl_t nodeh,const char * propname,void * valbuf,size_t nbytes)394*0Sstevel@tonic-gate picl_get_propval_by_name(picl_nodehdl_t nodeh, const char *propname,
395*0Sstevel@tonic-gate     void *valbuf, size_t nbytes)
396*0Sstevel@tonic-gate {
397*0Sstevel@tonic-gate 	door_arg_t		darg;
398*0Sstevel@tonic-gate 	picl_reqattrvalbyname_t	req_attrvalbyname;
399*0Sstevel@tonic-gate 	picl_service_t		*ret;
400*0Sstevel@tonic-gate 	picl_retattrvalbyname_t	*outargs;
401*0Sstevel@tonic-gate 	int			err;
402*0Sstevel@tonic-gate 
403*0Sstevel@tonic-gate 	req_attrvalbyname.cnum = PICL_CNUM_GETATTRVALBYNAME;
404*0Sstevel@tonic-gate 	req_attrvalbyname.nodeh = nodeh;
405*0Sstevel@tonic-gate 	(void) strcpy(req_attrvalbyname.propname, propname);
406*0Sstevel@tonic-gate 	req_attrvalbyname.bufsize = (uint32_t)nbytes;
407*0Sstevel@tonic-gate 	if ((size_t)req_attrvalbyname.bufsize != nbytes)
408*0Sstevel@tonic-gate 		return (PICL_VALUETOOBIG);
409*0Sstevel@tonic-gate 	outargs = alloca(sizeof (picl_retattrvalbyname_t) + nbytes);
410*0Sstevel@tonic-gate 
411*0Sstevel@tonic-gate 	err = trysend_req(&darg, &req_attrvalbyname,
412*0Sstevel@tonic-gate 	    sizeof (picl_reqattrvalbyname_t), NULL, 0, outargs,
413*0Sstevel@tonic-gate 	    sizeof (picl_retattrvalbyname_t) + nbytes, SEND_REQ_TRYCOUNT);
414*0Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
415*0Sstevel@tonic-gate 		return (err);
416*0Sstevel@tonic-gate 
417*0Sstevel@tonic-gate 	/*LINTED*/
418*0Sstevel@tonic-gate 	ret = (picl_service_t *)darg.rbuf;
419*0Sstevel@tonic-gate 	if (ret->ret_attrvalbyname.nbytes > (uint32_t)nbytes)
420*0Sstevel@tonic-gate 		err = PICL_VALUETOOBIG;
421*0Sstevel@tonic-gate 	else
422*0Sstevel@tonic-gate 		(void) memcpy(valbuf, ret->ret_attrvalbyname.ret_buf,
423*0Sstevel@tonic-gate 		    (size_t)ret->ret_attrvalbyname.nbytes);
424*0Sstevel@tonic-gate 	if (darg.rbuf != (char *)outargs)
425*0Sstevel@tonic-gate 		(void) munmap(darg.rbuf, darg.rsize);
426*0Sstevel@tonic-gate 	return (err);
427*0Sstevel@tonic-gate }
428*0Sstevel@tonic-gate 
429*0Sstevel@tonic-gate /*
430*0Sstevel@tonic-gate  * This function sets the value of the property specified by its
431*0Sstevel@tonic-gate  * handle with the value specified in <valbuf>.
432*0Sstevel@tonic-gate  */
433*0Sstevel@tonic-gate int
picl_set_propval(picl_prophdl_t proph,void * valbuf,size_t nbytes)434*0Sstevel@tonic-gate picl_set_propval(picl_prophdl_t proph, void *valbuf, size_t nbytes)
435*0Sstevel@tonic-gate {
436*0Sstevel@tonic-gate 	door_arg_t		darg;
437*0Sstevel@tonic-gate 	picl_reqsetattrval_t	ret_setattrval;
438*0Sstevel@tonic-gate 	picl_reqsetattrval_t	*inargs;
439*0Sstevel@tonic-gate 	int			err;
440*0Sstevel@tonic-gate 
441*0Sstevel@tonic-gate 	if (nbytes >= (size_t)PICL_PROPSIZE_MAX)
442*0Sstevel@tonic-gate 		return (PICL_VALUETOOBIG);
443*0Sstevel@tonic-gate 
444*0Sstevel@tonic-gate 	inargs = alloca(sizeof (picl_reqsetattrval_t) + nbytes);
445*0Sstevel@tonic-gate 	inargs->cnum = PICL_CNUM_SETATTRVAL;
446*0Sstevel@tonic-gate 	inargs->attr = proph;
447*0Sstevel@tonic-gate 	inargs->bufsize = (uint32_t)nbytes;
448*0Sstevel@tonic-gate 	if ((size_t)inargs->bufsize != nbytes)
449*0Sstevel@tonic-gate 		return (PICL_VALUETOOBIG);
450*0Sstevel@tonic-gate 	(void) memcpy(inargs->valbuf, valbuf, nbytes);
451*0Sstevel@tonic-gate 
452*0Sstevel@tonic-gate 	err = trysend_req(&darg, inargs, sizeof (picl_reqsetattrval_t) +
453*0Sstevel@tonic-gate 	    nbytes, NULL, 0, &ret_setattrval,
454*0Sstevel@tonic-gate 	    sizeof (picl_retsetattrval_t), SEND_REQ_TRYCOUNT);
455*0Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
456*0Sstevel@tonic-gate 		return (err);
457*0Sstevel@tonic-gate 
458*0Sstevel@tonic-gate 	if (darg.rbuf != (char *)&ret_setattrval)
459*0Sstevel@tonic-gate 		(void) munmap(darg.rbuf, darg.rsize);
460*0Sstevel@tonic-gate 	return (PICL_SUCCESS);
461*0Sstevel@tonic-gate }
462*0Sstevel@tonic-gate 
463*0Sstevel@tonic-gate /*
464*0Sstevel@tonic-gate  * This function sets the value of the property specified by its
465*0Sstevel@tonic-gate  * name with the value given in <valbuf>
466*0Sstevel@tonic-gate  */
467*0Sstevel@tonic-gate int
picl_set_propval_by_name(picl_nodehdl_t nodeh,const char * propname,void * valbuf,size_t nbytes)468*0Sstevel@tonic-gate picl_set_propval_by_name(picl_nodehdl_t nodeh, const char *propname,
469*0Sstevel@tonic-gate     void *valbuf, size_t nbytes)
470*0Sstevel@tonic-gate {
471*0Sstevel@tonic-gate 	door_arg_t			darg;
472*0Sstevel@tonic-gate 	picl_retsetattrvalbyname_t	ret_setattrvalbyname;
473*0Sstevel@tonic-gate 	picl_reqsetattrvalbyname_t	*inargs;
474*0Sstevel@tonic-gate 	int				err;
475*0Sstevel@tonic-gate 
476*0Sstevel@tonic-gate 	if (nbytes >= (size_t)PICL_PROPSIZE_MAX)
477*0Sstevel@tonic-gate 		return (PICL_VALUETOOBIG);
478*0Sstevel@tonic-gate 
479*0Sstevel@tonic-gate 	inargs = alloca(sizeof (picl_reqsetattrvalbyname_t) + nbytes);
480*0Sstevel@tonic-gate 	inargs->cnum = PICL_CNUM_SETATTRVALBYNAME;
481*0Sstevel@tonic-gate 	inargs->nodeh = nodeh;
482*0Sstevel@tonic-gate 	(void) strcpy(inargs->propname, propname);
483*0Sstevel@tonic-gate 	inargs->bufsize = (uint32_t)nbytes;
484*0Sstevel@tonic-gate 	if ((size_t)inargs->bufsize != nbytes)
485*0Sstevel@tonic-gate 		return (PICL_VALUETOOBIG);
486*0Sstevel@tonic-gate 	(void) memcpy(inargs->valbuf, valbuf, nbytes);
487*0Sstevel@tonic-gate 
488*0Sstevel@tonic-gate 	err = trysend_req(&darg, inargs,
489*0Sstevel@tonic-gate 	    sizeof (picl_reqsetattrvalbyname_t) + nbytes, NULL, 0,
490*0Sstevel@tonic-gate 	    &ret_setattrvalbyname, sizeof (picl_retsetattrvalbyname_t),
491*0Sstevel@tonic-gate 	    SEND_REQ_TRYCOUNT);
492*0Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
493*0Sstevel@tonic-gate 		return (err);
494*0Sstevel@tonic-gate 
495*0Sstevel@tonic-gate 	if (darg.rbuf != (char *)&ret_setattrvalbyname)
496*0Sstevel@tonic-gate 		(void) munmap(darg.rbuf, darg.rsize);
497*0Sstevel@tonic-gate 	return (PICL_SUCCESS);
498*0Sstevel@tonic-gate }
499*0Sstevel@tonic-gate 
500*0Sstevel@tonic-gate /*
501*0Sstevel@tonic-gate  * This function copies the information of the specified property
502*0Sstevel@tonic-gate  * into <pinfo>
503*0Sstevel@tonic-gate  */
504*0Sstevel@tonic-gate int
picl_get_propinfo(picl_prophdl_t proph,picl_propinfo_t * pinfo)505*0Sstevel@tonic-gate picl_get_propinfo(picl_prophdl_t proph, picl_propinfo_t *pinfo)
506*0Sstevel@tonic-gate {
507*0Sstevel@tonic-gate 	door_arg_t		darg;
508*0Sstevel@tonic-gate 	picl_reqattrinfo_t	req_attrinfo;
509*0Sstevel@tonic-gate 	picl_service_t		*ret;
510*0Sstevel@tonic-gate 	picl_retattrinfo_t	outargs;
511*0Sstevel@tonic-gate 	int			err;
512*0Sstevel@tonic-gate 
513*0Sstevel@tonic-gate 	req_attrinfo.cnum = PICL_CNUM_GETATTRINFO;
514*0Sstevel@tonic-gate 	req_attrinfo.attr = proph;
515*0Sstevel@tonic-gate 
516*0Sstevel@tonic-gate 	err = trysend_req(&darg, &req_attrinfo,
517*0Sstevel@tonic-gate 	    sizeof (picl_reqattrinfo_t), NULL, 0, &outargs,
518*0Sstevel@tonic-gate 	    sizeof (picl_retattrinfo_t), SEND_REQ_TRYCOUNT);
519*0Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
520*0Sstevel@tonic-gate 		return (err);
521*0Sstevel@tonic-gate 
522*0Sstevel@tonic-gate 	/*LINTED*/
523*0Sstevel@tonic-gate 	ret = (picl_service_t *)darg.rbuf;
524*0Sstevel@tonic-gate 	pinfo->type = ret->ret_attrinfo.type;
525*0Sstevel@tonic-gate 	pinfo->accessmode = ret->ret_attrinfo.accessmode;
526*0Sstevel@tonic-gate 	pinfo->size = (size_t)ret->ret_attrinfo.size;
527*0Sstevel@tonic-gate 	(void) strcpy(pinfo->name, ret->ret_attrinfo.name);
528*0Sstevel@tonic-gate 	if (darg.rbuf != (char *)&outargs)
529*0Sstevel@tonic-gate 		(void) munmap(darg.rbuf, darg.rsize);
530*0Sstevel@tonic-gate 	return (PICL_SUCCESS);
531*0Sstevel@tonic-gate }
532*0Sstevel@tonic-gate 
533*0Sstevel@tonic-gate /*
534*0Sstevel@tonic-gate  * This function copies the handle of the first property of a node into
535*0Sstevel@tonic-gate  * <proph>
536*0Sstevel@tonic-gate  */
537*0Sstevel@tonic-gate int
picl_get_first_prop(picl_nodehdl_t nodeh,picl_prophdl_t * proph)538*0Sstevel@tonic-gate picl_get_first_prop(picl_nodehdl_t nodeh, picl_prophdl_t *proph)
539*0Sstevel@tonic-gate {
540*0Sstevel@tonic-gate 	door_arg_t		darg;
541*0Sstevel@tonic-gate 	picl_reqfirstattr_t	req_firstattr;
542*0Sstevel@tonic-gate 	picl_service_t		*ret;
543*0Sstevel@tonic-gate 	picl_retfirstattr_t	outargs;
544*0Sstevel@tonic-gate 	int			err;
545*0Sstevel@tonic-gate 
546*0Sstevel@tonic-gate 	req_firstattr.cnum = PICL_CNUM_GETFIRSTATTR;
547*0Sstevel@tonic-gate 	req_firstattr.nodeh = nodeh;
548*0Sstevel@tonic-gate 
549*0Sstevel@tonic-gate 	err = trysend_req(&darg, &req_firstattr,
550*0Sstevel@tonic-gate 	    sizeof (picl_reqfirstattr_t), NULL, 0, &outargs,
551*0Sstevel@tonic-gate 	    sizeof (picl_retfirstattr_t), SEND_REQ_TRYCOUNT);
552*0Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
553*0Sstevel@tonic-gate 		return (err);
554*0Sstevel@tonic-gate 
555*0Sstevel@tonic-gate 	/*LINTED*/
556*0Sstevel@tonic-gate 	ret = (picl_service_t *)darg.rbuf;
557*0Sstevel@tonic-gate 	*proph = ret->ret_firstattr.attr;
558*0Sstevel@tonic-gate 	if (darg.rbuf != (char *)&outargs)
559*0Sstevel@tonic-gate 		(void) munmap(darg.rbuf, darg.rsize);
560*0Sstevel@tonic-gate 	return (PICL_SUCCESS);
561*0Sstevel@tonic-gate }
562*0Sstevel@tonic-gate 
563*0Sstevel@tonic-gate /*
564*0Sstevel@tonic-gate  * This function copies the handle of the next property in list
565*0Sstevel@tonic-gate  * into <nextprop>.
566*0Sstevel@tonic-gate  */
567*0Sstevel@tonic-gate int
picl_get_next_prop(picl_prophdl_t proph,picl_prophdl_t * nextprop)568*0Sstevel@tonic-gate picl_get_next_prop(picl_prophdl_t proph, picl_prophdl_t *nextprop)
569*0Sstevel@tonic-gate {
570*0Sstevel@tonic-gate 	door_arg_t		darg;
571*0Sstevel@tonic-gate 	picl_reqnextattr_t	req_nextattr;
572*0Sstevel@tonic-gate 	picl_service_t		*ret;
573*0Sstevel@tonic-gate 	picl_retnextattr_t	outargs;
574*0Sstevel@tonic-gate 	int			err;
575*0Sstevel@tonic-gate 
576*0Sstevel@tonic-gate 
577*0Sstevel@tonic-gate 	req_nextattr.cnum = PICL_CNUM_GETNEXTATTR;
578*0Sstevel@tonic-gate 	req_nextattr.attr = proph;
579*0Sstevel@tonic-gate 
580*0Sstevel@tonic-gate 	err = trysend_req(&darg, &req_nextattr,
581*0Sstevel@tonic-gate 	    sizeof (picl_reqnextattr_t), NULL, 0,  &outargs,
582*0Sstevel@tonic-gate 	    sizeof (picl_retnextattr_t), SEND_REQ_TRYCOUNT);
583*0Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
584*0Sstevel@tonic-gate 		return (err);
585*0Sstevel@tonic-gate 
586*0Sstevel@tonic-gate 	/*LINTED*/
587*0Sstevel@tonic-gate 	ret = (picl_service_t *)darg.rbuf;
588*0Sstevel@tonic-gate 	*nextprop = ret->ret_nextattr.nextattr;
589*0Sstevel@tonic-gate 	if (darg.rbuf != (char *)&outargs)
590*0Sstevel@tonic-gate 		(void) munmap(darg.rbuf, darg.rsize);
591*0Sstevel@tonic-gate 	return (PICL_SUCCESS);
592*0Sstevel@tonic-gate }
593*0Sstevel@tonic-gate 
594*0Sstevel@tonic-gate /*
595*0Sstevel@tonic-gate  * This function copies the handle of the property specified by its
596*0Sstevel@tonic-gate  * name into <proph>.
597*0Sstevel@tonic-gate  */
598*0Sstevel@tonic-gate int
picl_get_prop_by_name(picl_nodehdl_t nodeh,const char * name,picl_prophdl_t * proph)599*0Sstevel@tonic-gate picl_get_prop_by_name(picl_nodehdl_t nodeh, const char *name,
600*0Sstevel@tonic-gate     picl_prophdl_t *proph)
601*0Sstevel@tonic-gate {
602*0Sstevel@tonic-gate 	door_arg_t		darg;
603*0Sstevel@tonic-gate 	picl_reqattrbyname_t	req_attrbyname;
604*0Sstevel@tonic-gate 	picl_service_t		*ret;
605*0Sstevel@tonic-gate 	picl_retattrbyname_t	outargs;
606*0Sstevel@tonic-gate 	int			err;
607*0Sstevel@tonic-gate 
608*0Sstevel@tonic-gate 	req_attrbyname.cnum = PICL_CNUM_GETATTRBYNAME;
609*0Sstevel@tonic-gate 	req_attrbyname.nodeh = nodeh;
610*0Sstevel@tonic-gate 	(void) strcpy(req_attrbyname.propname, name);
611*0Sstevel@tonic-gate 
612*0Sstevel@tonic-gate 	err = trysend_req(&darg, &req_attrbyname,
613*0Sstevel@tonic-gate 	    sizeof (picl_reqattrbyname_t), NULL, 0, &outargs,
614*0Sstevel@tonic-gate 	    sizeof (picl_retattrbyname_t), SEND_REQ_TRYCOUNT);
615*0Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
616*0Sstevel@tonic-gate 		return (err);
617*0Sstevel@tonic-gate 
618*0Sstevel@tonic-gate 	/*LINTED*/
619*0Sstevel@tonic-gate 	ret = (picl_service_t *)darg.rbuf;
620*0Sstevel@tonic-gate 	*proph = ret->ret_attrbyname.attr;
621*0Sstevel@tonic-gate 	if (darg.rbuf != (char *)&outargs)
622*0Sstevel@tonic-gate 		(void) munmap(darg.rbuf, darg.rsize);
623*0Sstevel@tonic-gate 	return (PICL_SUCCESS);
624*0Sstevel@tonic-gate }
625*0Sstevel@tonic-gate 
626*0Sstevel@tonic-gate /*
627*0Sstevel@tonic-gate  * This function copies the handle of the next property on the same
628*0Sstevel@tonic-gate  * row of the table into <rowproph>.
629*0Sstevel@tonic-gate  * When proph is the table handle, the handle of the property that is
630*0Sstevel@tonic-gate  * in first row and first column is copied.
631*0Sstevel@tonic-gate  */
632*0Sstevel@tonic-gate int
picl_get_next_by_row(picl_prophdl_t proph,picl_prophdl_t * rowproph)633*0Sstevel@tonic-gate picl_get_next_by_row(picl_prophdl_t proph, picl_prophdl_t *rowproph)
634*0Sstevel@tonic-gate {
635*0Sstevel@tonic-gate 	door_arg_t		darg;
636*0Sstevel@tonic-gate 	picl_reqattrbyrow_t	req_attrbyrow;
637*0Sstevel@tonic-gate 	picl_service_t		*ret;
638*0Sstevel@tonic-gate 	picl_retattrbyrow_t	outargs;
639*0Sstevel@tonic-gate 	int			err;
640*0Sstevel@tonic-gate 
641*0Sstevel@tonic-gate 	req_attrbyrow.cnum = PICL_CNUM_GETATTRBYROW;
642*0Sstevel@tonic-gate 	req_attrbyrow.attr = proph;
643*0Sstevel@tonic-gate 
644*0Sstevel@tonic-gate 	err = trysend_req(&darg, &req_attrbyrow,
645*0Sstevel@tonic-gate 	    sizeof (picl_reqattrbyrow_t), NULL, 0, &outargs,
646*0Sstevel@tonic-gate 	    sizeof (picl_retattrbyrow_t), SEND_REQ_TRYCOUNT);
647*0Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
648*0Sstevel@tonic-gate 		return (err);
649*0Sstevel@tonic-gate 
650*0Sstevel@tonic-gate 	/*LINTED*/
651*0Sstevel@tonic-gate 	ret = (picl_service_t *)darg.rbuf;
652*0Sstevel@tonic-gate 	*rowproph = ret->ret_attrbyrow.rowattr;
653*0Sstevel@tonic-gate 	if (darg.rbuf != (char *)&outargs)
654*0Sstevel@tonic-gate 		(void) munmap(darg.rbuf, darg.rsize);
655*0Sstevel@tonic-gate 	return (PICL_SUCCESS);
656*0Sstevel@tonic-gate }
657*0Sstevel@tonic-gate 
658*0Sstevel@tonic-gate /*
659*0Sstevel@tonic-gate  * This function copies the handle of the next property on the same
660*0Sstevel@tonic-gate  * column of the table into <colproph>.
661*0Sstevel@tonic-gate  * When proph is the table handle, the handle of the property that is
662*0Sstevel@tonic-gate  * in the first row and first column is copied.
663*0Sstevel@tonic-gate  */
664*0Sstevel@tonic-gate int
picl_get_next_by_col(picl_prophdl_t proph,picl_prophdl_t * colproph)665*0Sstevel@tonic-gate picl_get_next_by_col(picl_prophdl_t proph, picl_prophdl_t *colproph)
666*0Sstevel@tonic-gate {
667*0Sstevel@tonic-gate 	door_arg_t		darg;
668*0Sstevel@tonic-gate 	picl_reqattrbycol_t	req_attrbycol;
669*0Sstevel@tonic-gate 	picl_service_t		*ret;
670*0Sstevel@tonic-gate 	picl_retattrbycol_t	outargs;
671*0Sstevel@tonic-gate 	int			err;
672*0Sstevel@tonic-gate 
673*0Sstevel@tonic-gate 	req_attrbycol.cnum = PICL_CNUM_GETATTRBYCOL;
674*0Sstevel@tonic-gate 	req_attrbycol.attr = proph;
675*0Sstevel@tonic-gate 
676*0Sstevel@tonic-gate 	err = trysend_req(&darg, (char *)&req_attrbycol,
677*0Sstevel@tonic-gate 	    sizeof (picl_reqattrbycol_t), NULL, 0, (char *)&outargs,
678*0Sstevel@tonic-gate 	    sizeof (picl_retattrbycol_t), SEND_REQ_TRYCOUNT);
679*0Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
680*0Sstevel@tonic-gate 		return (err);
681*0Sstevel@tonic-gate 
682*0Sstevel@tonic-gate 	/*LINTED*/
683*0Sstevel@tonic-gate 	ret = (picl_service_t *)darg.rbuf;
684*0Sstevel@tonic-gate 	*colproph = ret->ret_attrbycol.colattr;
685*0Sstevel@tonic-gate 	if (darg.rbuf != (char *)&outargs)
686*0Sstevel@tonic-gate 		(void) munmap(darg.rbuf, darg.rsize);
687*0Sstevel@tonic-gate 	return (PICL_SUCCESS);
688*0Sstevel@tonic-gate }
689*0Sstevel@tonic-gate 
690*0Sstevel@tonic-gate /*
691*0Sstevel@tonic-gate  * This function returns the picl error messages corresponding to the
692*0Sstevel@tonic-gate  * error number.
693*0Sstevel@tonic-gate  */
694*0Sstevel@tonic-gate char *
picl_strerror(int err)695*0Sstevel@tonic-gate picl_strerror(int err)
696*0Sstevel@tonic-gate {
697*0Sstevel@tonic-gate 	if ((err < N_ERRORS) && (err >= 0)) {
698*0Sstevel@tonic-gate 		return (gettext(picl_errmsg[err]));
699*0Sstevel@tonic-gate 	}
700*0Sstevel@tonic-gate 	return ((char *)NULL);
701*0Sstevel@tonic-gate }
702*0Sstevel@tonic-gate 
703*0Sstevel@tonic-gate /*
704*0Sstevel@tonic-gate  * recursively visit all nodes
705*0Sstevel@tonic-gate  */
706*0Sstevel@tonic-gate static int
do_walk(picl_nodehdl_t rooth,const char * classname,void * c_args,int (* callback_fn)(picl_nodehdl_t hdl,void * args))707*0Sstevel@tonic-gate do_walk(picl_nodehdl_t rooth, const char *classname,
708*0Sstevel@tonic-gate     void *c_args, int (*callback_fn)(picl_nodehdl_t hdl, void *args))
709*0Sstevel@tonic-gate {
710*0Sstevel@tonic-gate 	int		err;
711*0Sstevel@tonic-gate 	picl_nodehdl_t	chdh;
712*0Sstevel@tonic-gate 	char		classval[PICL_CLASSNAMELEN_MAX];
713*0Sstevel@tonic-gate 
714*0Sstevel@tonic-gate 	err = picl_get_propval_by_name(rooth, PICL_PROP_CHILD, &chdh,
715*0Sstevel@tonic-gate 	    sizeof (chdh));
716*0Sstevel@tonic-gate 	while (err == PICL_SUCCESS) {
717*0Sstevel@tonic-gate 		err = picl_get_propval_by_name(chdh, PICL_PROP_CLASSNAME,
718*0Sstevel@tonic-gate 		    classval, sizeof (classval));
719*0Sstevel@tonic-gate 		if (err != PICL_SUCCESS)
720*0Sstevel@tonic-gate 			return (err);
721*0Sstevel@tonic-gate 
722*0Sstevel@tonic-gate 		if ((classname == NULL) || (strcmp(classname, classval) == 0)) {
723*0Sstevel@tonic-gate 			err = callback_fn(chdh, c_args);
724*0Sstevel@tonic-gate 			if (err != PICL_WALK_CONTINUE)
725*0Sstevel@tonic-gate 				return (err);
726*0Sstevel@tonic-gate 		}
727*0Sstevel@tonic-gate 
728*0Sstevel@tonic-gate 		if ((err = do_walk(chdh, classname, c_args, callback_fn)) !=
729*0Sstevel@tonic-gate 		    PICL_WALK_CONTINUE)
730*0Sstevel@tonic-gate 			return (err);
731*0Sstevel@tonic-gate 
732*0Sstevel@tonic-gate 		err = picl_get_propval_by_name(chdh, PICL_PROP_PEER, &chdh,
733*0Sstevel@tonic-gate 		    sizeof (chdh));
734*0Sstevel@tonic-gate 	}
735*0Sstevel@tonic-gate 	if (err == PICL_PROPNOTFOUND)	/* end of a branch */
736*0Sstevel@tonic-gate 		return (PICL_WALK_CONTINUE);
737*0Sstevel@tonic-gate 	return (err);
738*0Sstevel@tonic-gate 
739*0Sstevel@tonic-gate }
740*0Sstevel@tonic-gate 
741*0Sstevel@tonic-gate /*
742*0Sstevel@tonic-gate  * This function walks the tree by class and invokes the callback
743*0Sstevel@tonic-gate  * function on class name matches.
744*0Sstevel@tonic-gate  */
745*0Sstevel@tonic-gate int
picl_walk_tree_by_class(picl_nodehdl_t rooth,const char * classname,void * c_args,int (* callback_fn)(picl_nodehdl_t hdl,void * args))746*0Sstevel@tonic-gate picl_walk_tree_by_class(picl_nodehdl_t rooth, const char *classname,
747*0Sstevel@tonic-gate     void *c_args, int (*callback_fn)(picl_nodehdl_t hdl, void *args))
748*0Sstevel@tonic-gate {
749*0Sstevel@tonic-gate 	int		err;
750*0Sstevel@tonic-gate 
751*0Sstevel@tonic-gate 	if (callback_fn == NULL)
752*0Sstevel@tonic-gate 		return (PICL_INVALIDARG);
753*0Sstevel@tonic-gate 	err = do_walk(rooth, classname, c_args, callback_fn);
754*0Sstevel@tonic-gate 	if ((err == PICL_WALK_CONTINUE) || (err == PICL_WALK_TERMINATE))
755*0Sstevel@tonic-gate 		return (PICL_SUCCESS);
756*0Sstevel@tonic-gate 	return (err);
757*0Sstevel@tonic-gate }
758*0Sstevel@tonic-gate 
759*0Sstevel@tonic-gate /*
760*0Sstevel@tonic-gate  * This function gets propinfo and prop handle of the named property
761*0Sstevel@tonic-gate  */
762*0Sstevel@tonic-gate int
picl_get_propinfo_by_name(picl_nodehdl_t nodeh,const char * prop_name,picl_propinfo_t * pinfo,picl_prophdl_t * proph)763*0Sstevel@tonic-gate picl_get_propinfo_by_name(picl_nodehdl_t nodeh, const char *prop_name,
764*0Sstevel@tonic-gate     picl_propinfo_t *pinfo, picl_prophdl_t *proph)
765*0Sstevel@tonic-gate {
766*0Sstevel@tonic-gate 	int		err;
767*0Sstevel@tonic-gate 	picl_prophdl_t	tmpproph;
768*0Sstevel@tonic-gate 	picl_propinfo_t	tmppinfo;
769*0Sstevel@tonic-gate 
770*0Sstevel@tonic-gate 	err = picl_get_prop_by_name(nodeh, prop_name, &tmpproph);
771*0Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
772*0Sstevel@tonic-gate 		return (err);
773*0Sstevel@tonic-gate 
774*0Sstevel@tonic-gate 	err = picl_get_propinfo(tmpproph, &tmppinfo);
775*0Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
776*0Sstevel@tonic-gate 		return (err);
777*0Sstevel@tonic-gate 
778*0Sstevel@tonic-gate 	*proph = tmpproph;
779*0Sstevel@tonic-gate 	*pinfo = tmppinfo;
780*0Sstevel@tonic-gate 	return (PICL_SUCCESS);
781*0Sstevel@tonic-gate }
782*0Sstevel@tonic-gate 
783*0Sstevel@tonic-gate int
picl_get_node_by_path(const char * piclpath,picl_nodehdl_t * nodeh)784*0Sstevel@tonic-gate picl_get_node_by_path(const char *piclpath, picl_nodehdl_t *nodeh)
785*0Sstevel@tonic-gate {
786*0Sstevel@tonic-gate 	door_arg_t		darg;
787*0Sstevel@tonic-gate 	picl_reqnodebypath_t	req;
788*0Sstevel@tonic-gate 	picl_retnodebypath_t	out;
789*0Sstevel@tonic-gate 	picl_service_t		*ret;
790*0Sstevel@tonic-gate 	int	err;
791*0Sstevel@tonic-gate 
792*0Sstevel@tonic-gate 	req.cnum = PICL_CNUM_NODEBYPATH;
793*0Sstevel@tonic-gate 	req.psize = PATH_MAX;
794*0Sstevel@tonic-gate 	if (strlen(piclpath) >= PATH_MAX)
795*0Sstevel@tonic-gate 		return (PICL_VALUETOOBIG);
796*0Sstevel@tonic-gate 	(void) strncpy(req.pathbuf, piclpath, PATH_MAX);
797*0Sstevel@tonic-gate 
798*0Sstevel@tonic-gate 	err = trysend_req(&darg, &req, sizeof (req), NULL, 0, &out,
799*0Sstevel@tonic-gate 	    sizeof (out), SEND_REQ_TRYCOUNT);
800*0Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
801*0Sstevel@tonic-gate 		return (err);
802*0Sstevel@tonic-gate 
803*0Sstevel@tonic-gate 	/*LINTED*/
804*0Sstevel@tonic-gate 	ret = (picl_service_t *)darg.rbuf;
805*0Sstevel@tonic-gate 	*nodeh = ret->ret_nodebypath.nodeh;
806*0Sstevel@tonic-gate 	if (darg.rbuf != (char *)&out)
807*0Sstevel@tonic-gate 		(void) munmap(darg.rbuf, darg.rsize);
808*0Sstevel@tonic-gate 	return (err);
809*0Sstevel@tonic-gate }
810*0Sstevel@tonic-gate 
811*0Sstevel@tonic-gate int
picl_find_node(picl_nodehdl_t rooth,char * pname,picl_prop_type_t ptype,void * pval,size_t valsize,picl_nodehdl_t * retnodeh)812*0Sstevel@tonic-gate picl_find_node(picl_nodehdl_t rooth, char *pname, picl_prop_type_t ptype,
813*0Sstevel@tonic-gate     void *pval, size_t valsize, picl_nodehdl_t *retnodeh)
814*0Sstevel@tonic-gate {
815*0Sstevel@tonic-gate 	door_arg_t		darg;
816*0Sstevel@tonic-gate 	picl_reqfindnode_t	*req;
817*0Sstevel@tonic-gate 	picl_service_t		*ret;
818*0Sstevel@tonic-gate 	picl_retfindnode_t	out;
819*0Sstevel@tonic-gate 	int			err;
820*0Sstevel@tonic-gate 
821*0Sstevel@tonic-gate 	req = alloca(sizeof (picl_reqfindnode_t) + valsize);
822*0Sstevel@tonic-gate 	req->cnum = PICL_CNUM_FINDNODE;
823*0Sstevel@tonic-gate 	req->nodeh = rooth;
824*0Sstevel@tonic-gate 	if (strlen(pname) >= PICL_PROPNAMELEN_MAX)
825*0Sstevel@tonic-gate 		return (PICL_VALUETOOBIG);
826*0Sstevel@tonic-gate 	(void) strncpy(req->propname, pname, PICL_PROPNAMELEN_MAX);
827*0Sstevel@tonic-gate 	req->ptype = ptype;
828*0Sstevel@tonic-gate 	req->valsize = (uint32_t)valsize;
829*0Sstevel@tonic-gate 	if ((size_t)req->valsize != valsize)
830*0Sstevel@tonic-gate 		return (PICL_VALUETOOBIG);
831*0Sstevel@tonic-gate 	(void) memcpy(req->valbuf, pval, valsize);
832*0Sstevel@tonic-gate 
833*0Sstevel@tonic-gate 	err = trysend_req(&darg, req, sizeof (picl_reqfindnode_t) + valsize,
834*0Sstevel@tonic-gate 	    NULL, 0, &out, sizeof (out), SEND_REQ_TRYCOUNT);
835*0Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
836*0Sstevel@tonic-gate 		return (err);
837*0Sstevel@tonic-gate 
838*0Sstevel@tonic-gate 	/*LINTED*/
839*0Sstevel@tonic-gate 	ret = (picl_service_t *)darg.rbuf;
840*0Sstevel@tonic-gate 	*retnodeh = ret->ret_findnode.rnodeh;
841*0Sstevel@tonic-gate 	if (darg.rbuf != (char *)&out)
842*0Sstevel@tonic-gate 		(void) munmap(darg.rbuf, darg.rsize);
843*0Sstevel@tonic-gate 	return (err);
844*0Sstevel@tonic-gate }
845*0Sstevel@tonic-gate 
846*0Sstevel@tonic-gate int
picl_get_frutree_parent(picl_nodehdl_t devh,picl_nodehdl_t * fruh)847*0Sstevel@tonic-gate picl_get_frutree_parent(picl_nodehdl_t devh, picl_nodehdl_t *fruh)
848*0Sstevel@tonic-gate {
849*0Sstevel@tonic-gate 	door_arg_t		darg;
850*0Sstevel@tonic-gate 	picl_reqfruparent_t	req;
851*0Sstevel@tonic-gate 	picl_retfruparent_t	out;
852*0Sstevel@tonic-gate 	picl_service_t		*ret;
853*0Sstevel@tonic-gate 	int			err;
854*0Sstevel@tonic-gate 
855*0Sstevel@tonic-gate 	req.cnum = PICL_CNUM_FRUTREEPARENT;
856*0Sstevel@tonic-gate 	req.devh = devh;
857*0Sstevel@tonic-gate 
858*0Sstevel@tonic-gate 	err = trysend_req(&darg, &req, sizeof (req), NULL, 0, &out,
859*0Sstevel@tonic-gate 	    sizeof (out), SEND_REQ_TRYCOUNT);
860*0Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
861*0Sstevel@tonic-gate 		return (err);
862*0Sstevel@tonic-gate 
863*0Sstevel@tonic-gate 	/*LINTED*/
864*0Sstevel@tonic-gate 	ret = (picl_service_t *)darg.rbuf;
865*0Sstevel@tonic-gate 	*fruh = ret->ret_fruparent.fruh;
866*0Sstevel@tonic-gate 	if (darg.rbuf != (char *)&out)
867*0Sstevel@tonic-gate 		(void) munmap(darg.rbuf, darg.rsize);
868*0Sstevel@tonic-gate 	return (err);
869*0Sstevel@tonic-gate }
870