xref: /titanic_53/usr/src/lib/libcontract/common/libcontract.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate #include <sys/ctfs.h>
30*7c478bd9Sstevel@tonic-gate #include <sys/contract.h>
31*7c478bd9Sstevel@tonic-gate #include <libnvpair.h>
32*7c478bd9Sstevel@tonic-gate #include <assert.h>
33*7c478bd9Sstevel@tonic-gate #include <unistd.h>
34*7c478bd9Sstevel@tonic-gate #include <errno.h>
35*7c478bd9Sstevel@tonic-gate #include <libcontract.h>
36*7c478bd9Sstevel@tonic-gate #include "libcontract_impl.h"
37*7c478bd9Sstevel@tonic-gate 
38*7c478bd9Sstevel@tonic-gate /*
39*7c478bd9Sstevel@tonic-gate  * Common template routines
40*7c478bd9Sstevel@tonic-gate  */
41*7c478bd9Sstevel@tonic-gate 
42*7c478bd9Sstevel@tonic-gate int
43*7c478bd9Sstevel@tonic-gate ct_tmpl_activate(int fd)
44*7c478bd9Sstevel@tonic-gate {
45*7c478bd9Sstevel@tonic-gate 	if (ioctl(fd, CT_TACTIVATE) == -1)
46*7c478bd9Sstevel@tonic-gate 		return (errno);
47*7c478bd9Sstevel@tonic-gate 	return (0);
48*7c478bd9Sstevel@tonic-gate }
49*7c478bd9Sstevel@tonic-gate 
50*7c478bd9Sstevel@tonic-gate int
51*7c478bd9Sstevel@tonic-gate ct_tmpl_clear(int fd)
52*7c478bd9Sstevel@tonic-gate {
53*7c478bd9Sstevel@tonic-gate 	if (ioctl(fd, CT_TCLEAR) == -1)
54*7c478bd9Sstevel@tonic-gate 		return (errno);
55*7c478bd9Sstevel@tonic-gate 	return (0);
56*7c478bd9Sstevel@tonic-gate }
57*7c478bd9Sstevel@tonic-gate 
58*7c478bd9Sstevel@tonic-gate int
59*7c478bd9Sstevel@tonic-gate ct_tmpl_create(int fd, ctid_t *ctidp)
60*7c478bd9Sstevel@tonic-gate {
61*7c478bd9Sstevel@tonic-gate 	ctid_t ctid = ioctl(fd, CT_TCREATE);
62*7c478bd9Sstevel@tonic-gate 	if (ctid == -1)
63*7c478bd9Sstevel@tonic-gate 		return (errno);
64*7c478bd9Sstevel@tonic-gate 	*ctidp = ctid;
65*7c478bd9Sstevel@tonic-gate 	return (0);
66*7c478bd9Sstevel@tonic-gate }
67*7c478bd9Sstevel@tonic-gate 
68*7c478bd9Sstevel@tonic-gate int
69*7c478bd9Sstevel@tonic-gate ct_tmpl_set_internal(int fd, uint_t id, uint_t value)
70*7c478bd9Sstevel@tonic-gate {
71*7c478bd9Sstevel@tonic-gate 	ct_param_t param;
72*7c478bd9Sstevel@tonic-gate 	param.ctpm_id = id;
73*7c478bd9Sstevel@tonic-gate 	param.ctpm_value = value;
74*7c478bd9Sstevel@tonic-gate 	if (ioctl(fd, CT_TSET, &param) == -1)
75*7c478bd9Sstevel@tonic-gate 		return (errno);
76*7c478bd9Sstevel@tonic-gate 	return (0);
77*7c478bd9Sstevel@tonic-gate }
78*7c478bd9Sstevel@tonic-gate 
79*7c478bd9Sstevel@tonic-gate int
80*7c478bd9Sstevel@tonic-gate ct_tmpl_set_critical(int fd, uint_t events)
81*7c478bd9Sstevel@tonic-gate {
82*7c478bd9Sstevel@tonic-gate 	return (ct_tmpl_set_internal(fd, CTP_EV_CRITICAL, events));
83*7c478bd9Sstevel@tonic-gate }
84*7c478bd9Sstevel@tonic-gate 
85*7c478bd9Sstevel@tonic-gate int
86*7c478bd9Sstevel@tonic-gate ct_tmpl_set_informative(int fd, uint_t events)
87*7c478bd9Sstevel@tonic-gate {
88*7c478bd9Sstevel@tonic-gate 	return (ct_tmpl_set_internal(fd, CTP_EV_INFO, events));
89*7c478bd9Sstevel@tonic-gate }
90*7c478bd9Sstevel@tonic-gate 
91*7c478bd9Sstevel@tonic-gate int
92*7c478bd9Sstevel@tonic-gate ct_tmpl_set_cookie(int fd, uint64_t cookie)
93*7c478bd9Sstevel@tonic-gate {
94*7c478bd9Sstevel@tonic-gate 	ct_param_t param;
95*7c478bd9Sstevel@tonic-gate 	param.ctpm_id = CTP_COOKIE;
96*7c478bd9Sstevel@tonic-gate 	param.ctpm_value = cookie;
97*7c478bd9Sstevel@tonic-gate 	if (ioctl(fd, CT_TSET, &param) == -1)
98*7c478bd9Sstevel@tonic-gate 		return (errno);
99*7c478bd9Sstevel@tonic-gate 	return (0);
100*7c478bd9Sstevel@tonic-gate }
101*7c478bd9Sstevel@tonic-gate 
102*7c478bd9Sstevel@tonic-gate int
103*7c478bd9Sstevel@tonic-gate ct_tmpl_get_internal(int fd, uint_t id, uint_t *value)
104*7c478bd9Sstevel@tonic-gate {
105*7c478bd9Sstevel@tonic-gate 	ct_param_t param;
106*7c478bd9Sstevel@tonic-gate 
107*7c478bd9Sstevel@tonic-gate 	param.ctpm_id = id;
108*7c478bd9Sstevel@tonic-gate 	if (ioctl(fd, CT_TGET, &param) == -1)
109*7c478bd9Sstevel@tonic-gate 		return (errno);
110*7c478bd9Sstevel@tonic-gate 	*value = param.ctpm_value;
111*7c478bd9Sstevel@tonic-gate 	return (0);
112*7c478bd9Sstevel@tonic-gate }
113*7c478bd9Sstevel@tonic-gate 
114*7c478bd9Sstevel@tonic-gate int
115*7c478bd9Sstevel@tonic-gate ct_tmpl_get_critical(int fd, uint_t *events)
116*7c478bd9Sstevel@tonic-gate {
117*7c478bd9Sstevel@tonic-gate 	return (ct_tmpl_get_internal(fd, CTP_EV_CRITICAL, events));
118*7c478bd9Sstevel@tonic-gate }
119*7c478bd9Sstevel@tonic-gate 
120*7c478bd9Sstevel@tonic-gate int
121*7c478bd9Sstevel@tonic-gate ct_tmpl_get_informative(int fd, uint_t *events)
122*7c478bd9Sstevel@tonic-gate {
123*7c478bd9Sstevel@tonic-gate 	return (ct_tmpl_get_internal(fd, CTP_EV_INFO, events));
124*7c478bd9Sstevel@tonic-gate }
125*7c478bd9Sstevel@tonic-gate 
126*7c478bd9Sstevel@tonic-gate int
127*7c478bd9Sstevel@tonic-gate ct_tmpl_get_cookie(int fd, uint64_t *cookie)
128*7c478bd9Sstevel@tonic-gate {
129*7c478bd9Sstevel@tonic-gate 	ct_param_t param;
130*7c478bd9Sstevel@tonic-gate 
131*7c478bd9Sstevel@tonic-gate 	param.ctpm_id = CTP_COOKIE;
132*7c478bd9Sstevel@tonic-gate 	if (ioctl(fd, CT_TGET, &param) == -1)
133*7c478bd9Sstevel@tonic-gate 		return (errno);
134*7c478bd9Sstevel@tonic-gate 	*cookie = param.ctpm_value;
135*7c478bd9Sstevel@tonic-gate 	return (0);
136*7c478bd9Sstevel@tonic-gate }
137*7c478bd9Sstevel@tonic-gate 
138*7c478bd9Sstevel@tonic-gate /*
139*7c478bd9Sstevel@tonic-gate  * Common ctl routines
140*7c478bd9Sstevel@tonic-gate  */
141*7c478bd9Sstevel@tonic-gate 
142*7c478bd9Sstevel@tonic-gate int
143*7c478bd9Sstevel@tonic-gate ct_ctl_adopt(int fd)
144*7c478bd9Sstevel@tonic-gate {
145*7c478bd9Sstevel@tonic-gate 	if (ioctl(fd, CT_CADOPT) == -1)
146*7c478bd9Sstevel@tonic-gate 		return (errno);
147*7c478bd9Sstevel@tonic-gate 	return (0);
148*7c478bd9Sstevel@tonic-gate }
149*7c478bd9Sstevel@tonic-gate 
150*7c478bd9Sstevel@tonic-gate int
151*7c478bd9Sstevel@tonic-gate ct_ctl_abandon(int fd)
152*7c478bd9Sstevel@tonic-gate {
153*7c478bd9Sstevel@tonic-gate 	if (ioctl(fd, CT_CABANDON) == -1)
154*7c478bd9Sstevel@tonic-gate 		return (errno);
155*7c478bd9Sstevel@tonic-gate 	return (0);
156*7c478bd9Sstevel@tonic-gate }
157*7c478bd9Sstevel@tonic-gate 
158*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
159*7c478bd9Sstevel@tonic-gate int
160*7c478bd9Sstevel@tonic-gate ct_ctl_newct(int cfd, ctevid_t evid, int tfd)
161*7c478bd9Sstevel@tonic-gate {
162*7c478bd9Sstevel@tonic-gate 	if (ioctl(cfd, CT_CNEWCT, tfd) == -1)
163*7c478bd9Sstevel@tonic-gate 		return (errno);
164*7c478bd9Sstevel@tonic-gate 	return (0);
165*7c478bd9Sstevel@tonic-gate }
166*7c478bd9Sstevel@tonic-gate 
167*7c478bd9Sstevel@tonic-gate int
168*7c478bd9Sstevel@tonic-gate ct_ctl_ack(int fd, ctevid_t event)
169*7c478bd9Sstevel@tonic-gate {
170*7c478bd9Sstevel@tonic-gate 	if (ioctl(fd, CT_CACK, &event) == -1)
171*7c478bd9Sstevel@tonic-gate 		return (errno);
172*7c478bd9Sstevel@tonic-gate 	return (0);
173*7c478bd9Sstevel@tonic-gate }
174*7c478bd9Sstevel@tonic-gate 
175*7c478bd9Sstevel@tonic-gate int
176*7c478bd9Sstevel@tonic-gate ct_ctl_qack(int fd, ctevid_t event)
177*7c478bd9Sstevel@tonic-gate {
178*7c478bd9Sstevel@tonic-gate 	if (ioctl(fd, CT_CQREQ, &event) == -1)
179*7c478bd9Sstevel@tonic-gate 		return (errno);
180*7c478bd9Sstevel@tonic-gate 	return (0);
181*7c478bd9Sstevel@tonic-gate }
182*7c478bd9Sstevel@tonic-gate 
183*7c478bd9Sstevel@tonic-gate /*
184*7c478bd9Sstevel@tonic-gate  * Common status routines
185*7c478bd9Sstevel@tonic-gate  */
186*7c478bd9Sstevel@tonic-gate 
187*7c478bd9Sstevel@tonic-gate int
188*7c478bd9Sstevel@tonic-gate ct_status_read(int fd, int detail, ct_stathdl_t *stathdl)
189*7c478bd9Sstevel@tonic-gate {
190*7c478bd9Sstevel@tonic-gate 	char *status_buffer = NULL;
191*7c478bd9Sstevel@tonic-gate 	int status_nbytes = 0;
192*7c478bd9Sstevel@tonic-gate 	struct ctlib_status_info *info;
193*7c478bd9Sstevel@tonic-gate 	int error;
194*7c478bd9Sstevel@tonic-gate 
195*7c478bd9Sstevel@tonic-gate 	info = malloc(sizeof (struct ctlib_status_info));
196*7c478bd9Sstevel@tonic-gate 	if (info == NULL)
197*7c478bd9Sstevel@tonic-gate 		return (errno);
198*7c478bd9Sstevel@tonic-gate 
199*7c478bd9Sstevel@tonic-gate 	info->status.ctst_detail = detail;
200*7c478bd9Sstevel@tonic-gate 	if (detail != CTD_COMMON) {
201*7c478bd9Sstevel@tonic-gate 		for (;;) {
202*7c478bd9Sstevel@tonic-gate 			info->status.ctst_nbytes = status_nbytes;
203*7c478bd9Sstevel@tonic-gate 			info->status.ctst_buffer = status_buffer;
204*7c478bd9Sstevel@tonic-gate 			do
205*7c478bd9Sstevel@tonic-gate 				error = ioctl(fd, CT_SSTATUS, &info->status);
206*7c478bd9Sstevel@tonic-gate 			while (error == -1 && errno == EINTR);
207*7c478bd9Sstevel@tonic-gate 			if (error == -1)
208*7c478bd9Sstevel@tonic-gate 				goto errout;
209*7c478bd9Sstevel@tonic-gate 			if (info->status.ctst_nbytes <= status_nbytes)
210*7c478bd9Sstevel@tonic-gate 				break;
211*7c478bd9Sstevel@tonic-gate 
212*7c478bd9Sstevel@tonic-gate 			if (status_buffer)
213*7c478bd9Sstevel@tonic-gate 				free(status_buffer);
214*7c478bd9Sstevel@tonic-gate 			status_nbytes = info->status.ctst_nbytes;
215*7c478bd9Sstevel@tonic-gate 			status_buffer = malloc(status_nbytes);
216*7c478bd9Sstevel@tonic-gate 			if (status_buffer == NULL)
217*7c478bd9Sstevel@tonic-gate 				goto errout;
218*7c478bd9Sstevel@tonic-gate 		}
219*7c478bd9Sstevel@tonic-gate 		if ((errno = nvlist_unpack(info->status.ctst_buffer,
220*7c478bd9Sstevel@tonic-gate 		    info->status.ctst_nbytes, &info->nvl, 0)) != 0)
221*7c478bd9Sstevel@tonic-gate 			goto errout;
222*7c478bd9Sstevel@tonic-gate 
223*7c478bd9Sstevel@tonic-gate 		free(status_buffer);
224*7c478bd9Sstevel@tonic-gate 		status_buffer = NULL;
225*7c478bd9Sstevel@tonic-gate 
226*7c478bd9Sstevel@tonic-gate 	} else {
227*7c478bd9Sstevel@tonic-gate 		info->status.ctst_nbytes = 0;
228*7c478bd9Sstevel@tonic-gate 		info->nvl = NULL;
229*7c478bd9Sstevel@tonic-gate 		if (ioctl(fd, CT_SSTATUS, &info->status) == -1)
230*7c478bd9Sstevel@tonic-gate 			goto errout;
231*7c478bd9Sstevel@tonic-gate 	}
232*7c478bd9Sstevel@tonic-gate 
233*7c478bd9Sstevel@tonic-gate 	*stathdl = info;
234*7c478bd9Sstevel@tonic-gate 	return (0);
235*7c478bd9Sstevel@tonic-gate 
236*7c478bd9Sstevel@tonic-gate errout:
237*7c478bd9Sstevel@tonic-gate 	error = errno;
238*7c478bd9Sstevel@tonic-gate 	if (status_buffer)
239*7c478bd9Sstevel@tonic-gate 		free(status_buffer);
240*7c478bd9Sstevel@tonic-gate 	if (info)
241*7c478bd9Sstevel@tonic-gate 		free(info);
242*7c478bd9Sstevel@tonic-gate 	return (error);
243*7c478bd9Sstevel@tonic-gate }
244*7c478bd9Sstevel@tonic-gate 
245*7c478bd9Sstevel@tonic-gate void
246*7c478bd9Sstevel@tonic-gate ct_status_free(ct_stathdl_t stathdl)
247*7c478bd9Sstevel@tonic-gate {
248*7c478bd9Sstevel@tonic-gate 	struct ctlib_status_info *info = stathdl;
249*7c478bd9Sstevel@tonic-gate 
250*7c478bd9Sstevel@tonic-gate 	if (info->nvl) {
251*7c478bd9Sstevel@tonic-gate 		assert(info->status.ctst_detail != CTD_COMMON);
252*7c478bd9Sstevel@tonic-gate 		nvlist_free(info->nvl);
253*7c478bd9Sstevel@tonic-gate 	}
254*7c478bd9Sstevel@tonic-gate 
255*7c478bd9Sstevel@tonic-gate 	free(info);
256*7c478bd9Sstevel@tonic-gate }
257*7c478bd9Sstevel@tonic-gate 
258*7c478bd9Sstevel@tonic-gate ctid_t
259*7c478bd9Sstevel@tonic-gate ct_status_get_id(ct_stathdl_t stathdl)
260*7c478bd9Sstevel@tonic-gate {
261*7c478bd9Sstevel@tonic-gate 	struct ctlib_status_info *info = stathdl;
262*7c478bd9Sstevel@tonic-gate 	return (info->status.ctst_id);
263*7c478bd9Sstevel@tonic-gate }
264*7c478bd9Sstevel@tonic-gate 
265*7c478bd9Sstevel@tonic-gate zoneid_t
266*7c478bd9Sstevel@tonic-gate ct_status_get_zoneid(ct_stathdl_t stathdl)
267*7c478bd9Sstevel@tonic-gate {
268*7c478bd9Sstevel@tonic-gate 	struct ctlib_status_info *info = stathdl;
269*7c478bd9Sstevel@tonic-gate 	return (info->status.ctst_zoneid);
270*7c478bd9Sstevel@tonic-gate }
271*7c478bd9Sstevel@tonic-gate 
272*7c478bd9Sstevel@tonic-gate const char *
273*7c478bd9Sstevel@tonic-gate ct_status_get_type(ct_stathdl_t stathdl)
274*7c478bd9Sstevel@tonic-gate {
275*7c478bd9Sstevel@tonic-gate 	struct ctlib_status_info *info = stathdl;
276*7c478bd9Sstevel@tonic-gate 	return (types[info->status.ctst_type].type_name);
277*7c478bd9Sstevel@tonic-gate }
278*7c478bd9Sstevel@tonic-gate 
279*7c478bd9Sstevel@tonic-gate id_t
280*7c478bd9Sstevel@tonic-gate ct_status_get_holder(ct_stathdl_t stathdl)
281*7c478bd9Sstevel@tonic-gate {
282*7c478bd9Sstevel@tonic-gate 	struct ctlib_status_info *info = stathdl;
283*7c478bd9Sstevel@tonic-gate 	return (info->status.ctst_holder);
284*7c478bd9Sstevel@tonic-gate }
285*7c478bd9Sstevel@tonic-gate 
286*7c478bd9Sstevel@tonic-gate ctstate_t
287*7c478bd9Sstevel@tonic-gate ct_status_get_state(ct_stathdl_t stathdl)
288*7c478bd9Sstevel@tonic-gate {
289*7c478bd9Sstevel@tonic-gate 	struct ctlib_status_info *info = stathdl;
290*7c478bd9Sstevel@tonic-gate 	return (info->status.ctst_state);
291*7c478bd9Sstevel@tonic-gate }
292*7c478bd9Sstevel@tonic-gate 
293*7c478bd9Sstevel@tonic-gate int
294*7c478bd9Sstevel@tonic-gate ct_status_get_nevents(ct_stathdl_t stathdl)
295*7c478bd9Sstevel@tonic-gate {
296*7c478bd9Sstevel@tonic-gate 	struct ctlib_status_info *info = stathdl;
297*7c478bd9Sstevel@tonic-gate 	return (info->status.ctst_nevents);
298*7c478bd9Sstevel@tonic-gate }
299*7c478bd9Sstevel@tonic-gate 
300*7c478bd9Sstevel@tonic-gate int
301*7c478bd9Sstevel@tonic-gate ct_status_get_ntime(ct_stathdl_t stathdl)
302*7c478bd9Sstevel@tonic-gate {
303*7c478bd9Sstevel@tonic-gate 	struct ctlib_status_info *info = stathdl;
304*7c478bd9Sstevel@tonic-gate 	return (info->status.ctst_ntime);
305*7c478bd9Sstevel@tonic-gate }
306*7c478bd9Sstevel@tonic-gate 
307*7c478bd9Sstevel@tonic-gate int
308*7c478bd9Sstevel@tonic-gate ct_status_get_qtime(ct_stathdl_t stathdl)
309*7c478bd9Sstevel@tonic-gate {
310*7c478bd9Sstevel@tonic-gate 	struct ctlib_status_info *info = stathdl;
311*7c478bd9Sstevel@tonic-gate 	return (info->status.ctst_qtime);
312*7c478bd9Sstevel@tonic-gate }
313*7c478bd9Sstevel@tonic-gate 
314*7c478bd9Sstevel@tonic-gate ctevid_t
315*7c478bd9Sstevel@tonic-gate ct_status_get_nevid(ct_stathdl_t stathdl)
316*7c478bd9Sstevel@tonic-gate {
317*7c478bd9Sstevel@tonic-gate 	struct ctlib_status_info *info = stathdl;
318*7c478bd9Sstevel@tonic-gate 	return (info->status.ctst_nevid);
319*7c478bd9Sstevel@tonic-gate }
320*7c478bd9Sstevel@tonic-gate 
321*7c478bd9Sstevel@tonic-gate uint_t
322*7c478bd9Sstevel@tonic-gate ct_status_get_informative(ct_stathdl_t stathdl)
323*7c478bd9Sstevel@tonic-gate {
324*7c478bd9Sstevel@tonic-gate 	struct ctlib_status_info *info = stathdl;
325*7c478bd9Sstevel@tonic-gate 	return (info->status.ctst_informative);
326*7c478bd9Sstevel@tonic-gate }
327*7c478bd9Sstevel@tonic-gate 
328*7c478bd9Sstevel@tonic-gate uint_t
329*7c478bd9Sstevel@tonic-gate ct_status_get_critical(ct_stathdl_t stathdl)
330*7c478bd9Sstevel@tonic-gate {
331*7c478bd9Sstevel@tonic-gate 	struct ctlib_status_info *info = stathdl;
332*7c478bd9Sstevel@tonic-gate 	return (info->status.ctst_critical);
333*7c478bd9Sstevel@tonic-gate }
334*7c478bd9Sstevel@tonic-gate 
335*7c478bd9Sstevel@tonic-gate uint64_t
336*7c478bd9Sstevel@tonic-gate ct_status_get_cookie(ct_stathdl_t stathdl)
337*7c478bd9Sstevel@tonic-gate {
338*7c478bd9Sstevel@tonic-gate 	struct ctlib_status_info *info = stathdl;
339*7c478bd9Sstevel@tonic-gate 	return (info->status.ctst_cookie);
340*7c478bd9Sstevel@tonic-gate }
341*7c478bd9Sstevel@tonic-gate 
342*7c478bd9Sstevel@tonic-gate /*
343*7c478bd9Sstevel@tonic-gate  * Common event routines
344*7c478bd9Sstevel@tonic-gate  */
345*7c478bd9Sstevel@tonic-gate 
346*7c478bd9Sstevel@tonic-gate static int
347*7c478bd9Sstevel@tonic-gate unpack_and_merge(nvlist_t **nvl, char *buffer, size_t len)
348*7c478bd9Sstevel@tonic-gate {
349*7c478bd9Sstevel@tonic-gate 	nvlist_t *tmpnvl;
350*7c478bd9Sstevel@tonic-gate 	int error;
351*7c478bd9Sstevel@tonic-gate 
352*7c478bd9Sstevel@tonic-gate 	if ((error = nvlist_unpack(buffer, len, &tmpnvl, 0)) != 0)
353*7c478bd9Sstevel@tonic-gate 		return (error);
354*7c478bd9Sstevel@tonic-gate 
355*7c478bd9Sstevel@tonic-gate 	if (*nvl == NULL) {
356*7c478bd9Sstevel@tonic-gate 		*nvl = tmpnvl;
357*7c478bd9Sstevel@tonic-gate 		return (0);
358*7c478bd9Sstevel@tonic-gate 	}
359*7c478bd9Sstevel@tonic-gate 
360*7c478bd9Sstevel@tonic-gate 	error = nvlist_merge(*nvl, tmpnvl, 0);
361*7c478bd9Sstevel@tonic-gate 	nvlist_free(tmpnvl);
362*7c478bd9Sstevel@tonic-gate 	return (error);
363*7c478bd9Sstevel@tonic-gate }
364*7c478bd9Sstevel@tonic-gate 
365*7c478bd9Sstevel@tonic-gate static int
366*7c478bd9Sstevel@tonic-gate ct_event_read_internal(int fd, int cmd, ct_evthdl_t *evt)
367*7c478bd9Sstevel@tonic-gate {
368*7c478bd9Sstevel@tonic-gate 	char *event_buffer = NULL;
369*7c478bd9Sstevel@tonic-gate 	int event_nbytes = 0;
370*7c478bd9Sstevel@tonic-gate 	struct ctlib_event_info *info;
371*7c478bd9Sstevel@tonic-gate 	ct_event_t *event;
372*7c478bd9Sstevel@tonic-gate 	int error;
373*7c478bd9Sstevel@tonic-gate 
374*7c478bd9Sstevel@tonic-gate 	info = malloc(sizeof (struct ctlib_event_info));
375*7c478bd9Sstevel@tonic-gate 	if (info == NULL)
376*7c478bd9Sstevel@tonic-gate 		return (errno);
377*7c478bd9Sstevel@tonic-gate 	info->nvl = NULL;
378*7c478bd9Sstevel@tonic-gate 	event = &info->event;
379*7c478bd9Sstevel@tonic-gate 
380*7c478bd9Sstevel@tonic-gate 	for (;;) {
381*7c478bd9Sstevel@tonic-gate 		event->ctev_nbytes = event_nbytes;
382*7c478bd9Sstevel@tonic-gate 		event->ctev_buffer = event_buffer;
383*7c478bd9Sstevel@tonic-gate 		do
384*7c478bd9Sstevel@tonic-gate 			error = ioctl(fd, cmd, event);
385*7c478bd9Sstevel@tonic-gate 		while (error == -1 && errno == EINTR);
386*7c478bd9Sstevel@tonic-gate 		if (error == -1) {
387*7c478bd9Sstevel@tonic-gate 			error = errno;
388*7c478bd9Sstevel@tonic-gate 			goto errout;
389*7c478bd9Sstevel@tonic-gate 		}
390*7c478bd9Sstevel@tonic-gate 		if (event->ctev_nbytes <= event_nbytes)
391*7c478bd9Sstevel@tonic-gate 			break;
392*7c478bd9Sstevel@tonic-gate 
393*7c478bd9Sstevel@tonic-gate 		if (event_buffer)
394*7c478bd9Sstevel@tonic-gate 			free(event_buffer);
395*7c478bd9Sstevel@tonic-gate 		event_nbytes = event->ctev_nbytes;
396*7c478bd9Sstevel@tonic-gate 		event_buffer = malloc(event_nbytes);
397*7c478bd9Sstevel@tonic-gate 		if (event_buffer == NULL) {
398*7c478bd9Sstevel@tonic-gate 			error = errno;
399*7c478bd9Sstevel@tonic-gate 			goto errout;
400*7c478bd9Sstevel@tonic-gate 		}
401*7c478bd9Sstevel@tonic-gate 	}
402*7c478bd9Sstevel@tonic-gate 
403*7c478bd9Sstevel@tonic-gate 	if (event->ctev_goffset > 0 && (error = unpack_and_merge(&info->nvl,
404*7c478bd9Sstevel@tonic-gate 	    event->ctev_buffer, event->ctev_goffset)) != 0)
405*7c478bd9Sstevel@tonic-gate 		goto errout;
406*7c478bd9Sstevel@tonic-gate 
407*7c478bd9Sstevel@tonic-gate 	if (event->ctev_goffset < event->ctev_nbytes &&
408*7c478bd9Sstevel@tonic-gate 	    (error = unpack_and_merge(&info->nvl,
409*7c478bd9Sstevel@tonic-gate 	    event->ctev_buffer + event->ctev_goffset,
410*7c478bd9Sstevel@tonic-gate 	    event->ctev_nbytes - event->ctev_goffset)) != 0)
411*7c478bd9Sstevel@tonic-gate 		goto errout;
412*7c478bd9Sstevel@tonic-gate 
413*7c478bd9Sstevel@tonic-gate 	free(event_buffer);
414*7c478bd9Sstevel@tonic-gate 
415*7c478bd9Sstevel@tonic-gate 	*evt = info;
416*7c478bd9Sstevel@tonic-gate 	return (0);
417*7c478bd9Sstevel@tonic-gate 
418*7c478bd9Sstevel@tonic-gate errout:
419*7c478bd9Sstevel@tonic-gate 	if (event_buffer)
420*7c478bd9Sstevel@tonic-gate 		free(event_buffer);
421*7c478bd9Sstevel@tonic-gate 	if (info) {
422*7c478bd9Sstevel@tonic-gate 		if (info->nvl)
423*7c478bd9Sstevel@tonic-gate 			nvlist_free(info->nvl);
424*7c478bd9Sstevel@tonic-gate 		free(info);
425*7c478bd9Sstevel@tonic-gate 	}
426*7c478bd9Sstevel@tonic-gate 	return (error);
427*7c478bd9Sstevel@tonic-gate }
428*7c478bd9Sstevel@tonic-gate 
429*7c478bd9Sstevel@tonic-gate int
430*7c478bd9Sstevel@tonic-gate ct_event_read(int fd, ct_evthdl_t *evthdl)
431*7c478bd9Sstevel@tonic-gate {
432*7c478bd9Sstevel@tonic-gate 	return (ct_event_read_internal(fd, CT_ERECV, evthdl));
433*7c478bd9Sstevel@tonic-gate }
434*7c478bd9Sstevel@tonic-gate 
435*7c478bd9Sstevel@tonic-gate int
436*7c478bd9Sstevel@tonic-gate ct_event_read_critical(int fd, ct_evthdl_t *evthdl)
437*7c478bd9Sstevel@tonic-gate {
438*7c478bd9Sstevel@tonic-gate 	return (ct_event_read_internal(fd, CT_ECRECV, evthdl));
439*7c478bd9Sstevel@tonic-gate }
440*7c478bd9Sstevel@tonic-gate 
441*7c478bd9Sstevel@tonic-gate int
442*7c478bd9Sstevel@tonic-gate ct_event_reset(int fd)
443*7c478bd9Sstevel@tonic-gate {
444*7c478bd9Sstevel@tonic-gate 	if (ioctl(fd, CT_ERESET) == -1)
445*7c478bd9Sstevel@tonic-gate 		return (errno);
446*7c478bd9Sstevel@tonic-gate 	return (0);
447*7c478bd9Sstevel@tonic-gate }
448*7c478bd9Sstevel@tonic-gate 
449*7c478bd9Sstevel@tonic-gate int
450*7c478bd9Sstevel@tonic-gate ct_event_reliable(int fd)
451*7c478bd9Sstevel@tonic-gate {
452*7c478bd9Sstevel@tonic-gate 	if (ioctl(fd, CT_ERELIABLE) == -1)
453*7c478bd9Sstevel@tonic-gate 		return (errno);
454*7c478bd9Sstevel@tonic-gate 	return (0);
455*7c478bd9Sstevel@tonic-gate }
456*7c478bd9Sstevel@tonic-gate 
457*7c478bd9Sstevel@tonic-gate void
458*7c478bd9Sstevel@tonic-gate ct_event_free(ct_evthdl_t evthdl)
459*7c478bd9Sstevel@tonic-gate {
460*7c478bd9Sstevel@tonic-gate 	struct ctlib_event_info *info = evthdl;
461*7c478bd9Sstevel@tonic-gate 
462*7c478bd9Sstevel@tonic-gate 	if (info->nvl)
463*7c478bd9Sstevel@tonic-gate 		nvlist_free(info->nvl);
464*7c478bd9Sstevel@tonic-gate 	free(info);
465*7c478bd9Sstevel@tonic-gate }
466*7c478bd9Sstevel@tonic-gate 
467*7c478bd9Sstevel@tonic-gate 
468*7c478bd9Sstevel@tonic-gate uint_t
469*7c478bd9Sstevel@tonic-gate ct_event_get_flags(ct_evthdl_t evthdl)
470*7c478bd9Sstevel@tonic-gate {
471*7c478bd9Sstevel@tonic-gate 	struct ctlib_event_info *info = evthdl;
472*7c478bd9Sstevel@tonic-gate 	return (info->event.ctev_flags);
473*7c478bd9Sstevel@tonic-gate }
474*7c478bd9Sstevel@tonic-gate 
475*7c478bd9Sstevel@tonic-gate ctid_t
476*7c478bd9Sstevel@tonic-gate ct_event_get_ctid(ct_evthdl_t evthdl)
477*7c478bd9Sstevel@tonic-gate {
478*7c478bd9Sstevel@tonic-gate 	struct ctlib_event_info *info = evthdl;
479*7c478bd9Sstevel@tonic-gate 	return (info->event.ctev_id);
480*7c478bd9Sstevel@tonic-gate }
481*7c478bd9Sstevel@tonic-gate 
482*7c478bd9Sstevel@tonic-gate ctevid_t
483*7c478bd9Sstevel@tonic-gate ct_event_get_evid(ct_evthdl_t evthdl)
484*7c478bd9Sstevel@tonic-gate {
485*7c478bd9Sstevel@tonic-gate 	struct ctlib_event_info *info = evthdl;
486*7c478bd9Sstevel@tonic-gate 	return (info->event.ctev_evid);
487*7c478bd9Sstevel@tonic-gate }
488*7c478bd9Sstevel@tonic-gate 
489*7c478bd9Sstevel@tonic-gate uint_t
490*7c478bd9Sstevel@tonic-gate ct_event_get_type(ct_evthdl_t evthdl)
491*7c478bd9Sstevel@tonic-gate {
492*7c478bd9Sstevel@tonic-gate 	struct ctlib_event_info *info = evthdl;
493*7c478bd9Sstevel@tonic-gate 	return (info->event.ctev_type);
494*7c478bd9Sstevel@tonic-gate }
495*7c478bd9Sstevel@tonic-gate 
496*7c478bd9Sstevel@tonic-gate int
497*7c478bd9Sstevel@tonic-gate ct_event_get_nevid(ct_evthdl_t evthdl, ctevid_t *evidp)
498*7c478bd9Sstevel@tonic-gate {
499*7c478bd9Sstevel@tonic-gate 	struct ctlib_event_info *info = evthdl;
500*7c478bd9Sstevel@tonic-gate 	if (info->nvl == NULL ||
501*7c478bd9Sstevel@tonic-gate 	    nvlist_lookup_uint64(info->nvl, CTS_NEVID, evidp))
502*7c478bd9Sstevel@tonic-gate 		return (EINVAL);
503*7c478bd9Sstevel@tonic-gate 	return (0);
504*7c478bd9Sstevel@tonic-gate }
505*7c478bd9Sstevel@tonic-gate 
506*7c478bd9Sstevel@tonic-gate int
507*7c478bd9Sstevel@tonic-gate ct_event_get_newct(ct_evthdl_t evthdl, ctid_t *ctidp)
508*7c478bd9Sstevel@tonic-gate {
509*7c478bd9Sstevel@tonic-gate 	struct ctlib_event_info *info = evthdl;
510*7c478bd9Sstevel@tonic-gate 	if (info->nvl == NULL ||
511*7c478bd9Sstevel@tonic-gate 	    nvlist_lookup_int32(info->nvl, CTS_NEWCT, (int *)ctidp))
512*7c478bd9Sstevel@tonic-gate 		return (EINVAL);
513*7c478bd9Sstevel@tonic-gate 	return (0);
514*7c478bd9Sstevel@tonic-gate }
515