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