10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*4845Svikram  * Common Development and Distribution License (the "License").
6*4845Svikram  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*4845Svikram  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include <sys/ctfs.h>
290Sstevel@tonic-gate #include <sys/contract.h>
300Sstevel@tonic-gate #include <libnvpair.h>
310Sstevel@tonic-gate #include <assert.h>
320Sstevel@tonic-gate #include <unistd.h>
330Sstevel@tonic-gate #include <errno.h>
340Sstevel@tonic-gate #include <libcontract.h>
350Sstevel@tonic-gate #include "libcontract_impl.h"
360Sstevel@tonic-gate 
370Sstevel@tonic-gate /*
380Sstevel@tonic-gate  * Common template routines
390Sstevel@tonic-gate  */
400Sstevel@tonic-gate 
410Sstevel@tonic-gate int
420Sstevel@tonic-gate ct_tmpl_activate(int fd)
430Sstevel@tonic-gate {
440Sstevel@tonic-gate 	if (ioctl(fd, CT_TACTIVATE) == -1)
450Sstevel@tonic-gate 		return (errno);
460Sstevel@tonic-gate 	return (0);
470Sstevel@tonic-gate }
480Sstevel@tonic-gate 
490Sstevel@tonic-gate int
500Sstevel@tonic-gate ct_tmpl_clear(int fd)
510Sstevel@tonic-gate {
520Sstevel@tonic-gate 	if (ioctl(fd, CT_TCLEAR) == -1)
530Sstevel@tonic-gate 		return (errno);
540Sstevel@tonic-gate 	return (0);
550Sstevel@tonic-gate }
560Sstevel@tonic-gate 
570Sstevel@tonic-gate int
580Sstevel@tonic-gate ct_tmpl_create(int fd, ctid_t *ctidp)
590Sstevel@tonic-gate {
600Sstevel@tonic-gate 	ctid_t ctid = ioctl(fd, CT_TCREATE);
610Sstevel@tonic-gate 	if (ctid == -1)
620Sstevel@tonic-gate 		return (errno);
630Sstevel@tonic-gate 	*ctidp = ctid;
640Sstevel@tonic-gate 	return (0);
650Sstevel@tonic-gate }
660Sstevel@tonic-gate 
670Sstevel@tonic-gate int
68*4845Svikram ct_tmpl_set_internal(int fd, uint_t id, uintptr_t value)
690Sstevel@tonic-gate {
700Sstevel@tonic-gate 	ct_param_t param;
710Sstevel@tonic-gate 	param.ctpm_id = id;
72*4845Svikram 	param.ctpm_value = (uint64_t)value;
730Sstevel@tonic-gate 	if (ioctl(fd, CT_TSET, &param) == -1)
740Sstevel@tonic-gate 		return (errno);
750Sstevel@tonic-gate 	return (0);
760Sstevel@tonic-gate }
770Sstevel@tonic-gate 
780Sstevel@tonic-gate int
790Sstevel@tonic-gate ct_tmpl_set_critical(int fd, uint_t events)
800Sstevel@tonic-gate {
810Sstevel@tonic-gate 	return (ct_tmpl_set_internal(fd, CTP_EV_CRITICAL, events));
820Sstevel@tonic-gate }
830Sstevel@tonic-gate 
840Sstevel@tonic-gate int
850Sstevel@tonic-gate ct_tmpl_set_informative(int fd, uint_t events)
860Sstevel@tonic-gate {
870Sstevel@tonic-gate 	return (ct_tmpl_set_internal(fd, CTP_EV_INFO, events));
880Sstevel@tonic-gate }
890Sstevel@tonic-gate 
900Sstevel@tonic-gate int
910Sstevel@tonic-gate ct_tmpl_set_cookie(int fd, uint64_t cookie)
920Sstevel@tonic-gate {
930Sstevel@tonic-gate 	ct_param_t param;
940Sstevel@tonic-gate 	param.ctpm_id = CTP_COOKIE;
950Sstevel@tonic-gate 	param.ctpm_value = cookie;
960Sstevel@tonic-gate 	if (ioctl(fd, CT_TSET, &param) == -1)
970Sstevel@tonic-gate 		return (errno);
980Sstevel@tonic-gate 	return (0);
990Sstevel@tonic-gate }
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate int
1020Sstevel@tonic-gate ct_tmpl_get_internal(int fd, uint_t id, uint_t *value)
1030Sstevel@tonic-gate {
1040Sstevel@tonic-gate 	ct_param_t param;
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate 	param.ctpm_id = id;
1070Sstevel@tonic-gate 	if (ioctl(fd, CT_TGET, &param) == -1)
1080Sstevel@tonic-gate 		return (errno);
1090Sstevel@tonic-gate 	*value = param.ctpm_value;
1100Sstevel@tonic-gate 	return (0);
1110Sstevel@tonic-gate }
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate int
114*4845Svikram ct_tmpl_get_internal_string(int fd, uint_t id, char *value)
115*4845Svikram {
116*4845Svikram 	ct_param_t param;
117*4845Svikram 
118*4845Svikram 	param.ctpm_id = id;
119*4845Svikram 	param.ctpm_value = (uint64_t)(uintptr_t)value;
120*4845Svikram 	if (ioctl(fd, CT_TGET, &param) == -1)
121*4845Svikram 		return (errno);
122*4845Svikram 	return (0);
123*4845Svikram }
124*4845Svikram 
125*4845Svikram int
1260Sstevel@tonic-gate ct_tmpl_get_critical(int fd, uint_t *events)
1270Sstevel@tonic-gate {
1280Sstevel@tonic-gate 	return (ct_tmpl_get_internal(fd, CTP_EV_CRITICAL, events));
1290Sstevel@tonic-gate }
1300Sstevel@tonic-gate 
1310Sstevel@tonic-gate int
1320Sstevel@tonic-gate ct_tmpl_get_informative(int fd, uint_t *events)
1330Sstevel@tonic-gate {
1340Sstevel@tonic-gate 	return (ct_tmpl_get_internal(fd, CTP_EV_INFO, events));
1350Sstevel@tonic-gate }
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate int
1380Sstevel@tonic-gate ct_tmpl_get_cookie(int fd, uint64_t *cookie)
1390Sstevel@tonic-gate {
1400Sstevel@tonic-gate 	ct_param_t param;
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate 	param.ctpm_id = CTP_COOKIE;
1430Sstevel@tonic-gate 	if (ioctl(fd, CT_TGET, &param) == -1)
1440Sstevel@tonic-gate 		return (errno);
1450Sstevel@tonic-gate 	*cookie = param.ctpm_value;
1460Sstevel@tonic-gate 	return (0);
1470Sstevel@tonic-gate }
1480Sstevel@tonic-gate 
1490Sstevel@tonic-gate /*
1500Sstevel@tonic-gate  * Common ctl routines
1510Sstevel@tonic-gate  */
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate int
1540Sstevel@tonic-gate ct_ctl_adopt(int fd)
1550Sstevel@tonic-gate {
1560Sstevel@tonic-gate 	if (ioctl(fd, CT_CADOPT) == -1)
1570Sstevel@tonic-gate 		return (errno);
1580Sstevel@tonic-gate 	return (0);
1590Sstevel@tonic-gate }
1600Sstevel@tonic-gate 
1610Sstevel@tonic-gate int
1620Sstevel@tonic-gate ct_ctl_abandon(int fd)
1630Sstevel@tonic-gate {
1640Sstevel@tonic-gate 	if (ioctl(fd, CT_CABANDON) == -1)
1650Sstevel@tonic-gate 		return (errno);
1660Sstevel@tonic-gate 	return (0);
1670Sstevel@tonic-gate }
1680Sstevel@tonic-gate 
1690Sstevel@tonic-gate /*ARGSUSED*/
1700Sstevel@tonic-gate int
1710Sstevel@tonic-gate ct_ctl_newct(int cfd, ctevid_t evid, int tfd)
1720Sstevel@tonic-gate {
1730Sstevel@tonic-gate 	if (ioctl(cfd, CT_CNEWCT, tfd) == -1)
1740Sstevel@tonic-gate 		return (errno);
1750Sstevel@tonic-gate 	return (0);
1760Sstevel@tonic-gate }
1770Sstevel@tonic-gate 
1780Sstevel@tonic-gate int
1790Sstevel@tonic-gate ct_ctl_ack(int fd, ctevid_t event)
1800Sstevel@tonic-gate {
1810Sstevel@tonic-gate 	if (ioctl(fd, CT_CACK, &event) == -1)
1820Sstevel@tonic-gate 		return (errno);
1830Sstevel@tonic-gate 	return (0);
1840Sstevel@tonic-gate }
1850Sstevel@tonic-gate 
1860Sstevel@tonic-gate int
187*4845Svikram ct_ctl_nack(int fd, ctevid_t event)
188*4845Svikram {
189*4845Svikram 	if (ioctl(fd, CT_CNACK, &event) == -1)
190*4845Svikram 		return (errno);
191*4845Svikram 	return (0);
192*4845Svikram }
193*4845Svikram 
194*4845Svikram int
1950Sstevel@tonic-gate ct_ctl_qack(int fd, ctevid_t event)
1960Sstevel@tonic-gate {
1970Sstevel@tonic-gate 	if (ioctl(fd, CT_CQREQ, &event) == -1)
1980Sstevel@tonic-gate 		return (errno);
1990Sstevel@tonic-gate 	return (0);
2000Sstevel@tonic-gate }
2010Sstevel@tonic-gate 
2020Sstevel@tonic-gate /*
2030Sstevel@tonic-gate  * Common status routines
2040Sstevel@tonic-gate  */
2050Sstevel@tonic-gate 
2060Sstevel@tonic-gate int
2070Sstevel@tonic-gate ct_status_read(int fd, int detail, ct_stathdl_t *stathdl)
2080Sstevel@tonic-gate {
2090Sstevel@tonic-gate 	char *status_buffer = NULL;
2100Sstevel@tonic-gate 	int status_nbytes = 0;
2110Sstevel@tonic-gate 	struct ctlib_status_info *info;
2120Sstevel@tonic-gate 	int error;
2130Sstevel@tonic-gate 
2140Sstevel@tonic-gate 	info = malloc(sizeof (struct ctlib_status_info));
2150Sstevel@tonic-gate 	if (info == NULL)
2160Sstevel@tonic-gate 		return (errno);
2170Sstevel@tonic-gate 
2180Sstevel@tonic-gate 	info->status.ctst_detail = detail;
2190Sstevel@tonic-gate 	if (detail != CTD_COMMON) {
2200Sstevel@tonic-gate 		for (;;) {
2210Sstevel@tonic-gate 			info->status.ctst_nbytes = status_nbytes;
2220Sstevel@tonic-gate 			info->status.ctst_buffer = status_buffer;
2230Sstevel@tonic-gate 			do
2240Sstevel@tonic-gate 				error = ioctl(fd, CT_SSTATUS, &info->status);
2250Sstevel@tonic-gate 			while (error == -1 && errno == EINTR);
2260Sstevel@tonic-gate 			if (error == -1)
2270Sstevel@tonic-gate 				goto errout;
2280Sstevel@tonic-gate 			if (info->status.ctst_nbytes <= status_nbytes)
2290Sstevel@tonic-gate 				break;
2300Sstevel@tonic-gate 
2310Sstevel@tonic-gate 			if (status_buffer)
2320Sstevel@tonic-gate 				free(status_buffer);
2330Sstevel@tonic-gate 			status_nbytes = info->status.ctst_nbytes;
2340Sstevel@tonic-gate 			status_buffer = malloc(status_nbytes);
2350Sstevel@tonic-gate 			if (status_buffer == NULL)
2360Sstevel@tonic-gate 				goto errout;
2370Sstevel@tonic-gate 		}
2380Sstevel@tonic-gate 		if ((errno = nvlist_unpack(info->status.ctst_buffer,
2390Sstevel@tonic-gate 		    info->status.ctst_nbytes, &info->nvl, 0)) != 0)
2400Sstevel@tonic-gate 			goto errout;
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate 		free(status_buffer);
2430Sstevel@tonic-gate 		status_buffer = NULL;
2440Sstevel@tonic-gate 
2450Sstevel@tonic-gate 	} else {
2460Sstevel@tonic-gate 		info->status.ctst_nbytes = 0;
2470Sstevel@tonic-gate 		info->nvl = NULL;
2480Sstevel@tonic-gate 		if (ioctl(fd, CT_SSTATUS, &info->status) == -1)
2490Sstevel@tonic-gate 			goto errout;
2500Sstevel@tonic-gate 	}
2510Sstevel@tonic-gate 
2520Sstevel@tonic-gate 	*stathdl = info;
2530Sstevel@tonic-gate 	return (0);
2540Sstevel@tonic-gate 
2550Sstevel@tonic-gate errout:
2560Sstevel@tonic-gate 	error = errno;
2570Sstevel@tonic-gate 	if (status_buffer)
2580Sstevel@tonic-gate 		free(status_buffer);
2590Sstevel@tonic-gate 	if (info)
2600Sstevel@tonic-gate 		free(info);
2610Sstevel@tonic-gate 	return (error);
2620Sstevel@tonic-gate }
2630Sstevel@tonic-gate 
2640Sstevel@tonic-gate void
2650Sstevel@tonic-gate ct_status_free(ct_stathdl_t stathdl)
2660Sstevel@tonic-gate {
2670Sstevel@tonic-gate 	struct ctlib_status_info *info = stathdl;
2680Sstevel@tonic-gate 
2690Sstevel@tonic-gate 	if (info->nvl) {
2700Sstevel@tonic-gate 		assert(info->status.ctst_detail != CTD_COMMON);
2710Sstevel@tonic-gate 		nvlist_free(info->nvl);
2720Sstevel@tonic-gate 	}
2730Sstevel@tonic-gate 
2740Sstevel@tonic-gate 	free(info);
2750Sstevel@tonic-gate }
2760Sstevel@tonic-gate 
2770Sstevel@tonic-gate ctid_t
2780Sstevel@tonic-gate ct_status_get_id(ct_stathdl_t stathdl)
2790Sstevel@tonic-gate {
2800Sstevel@tonic-gate 	struct ctlib_status_info *info = stathdl;
2810Sstevel@tonic-gate 	return (info->status.ctst_id);
2820Sstevel@tonic-gate }
2830Sstevel@tonic-gate 
2840Sstevel@tonic-gate zoneid_t
2850Sstevel@tonic-gate ct_status_get_zoneid(ct_stathdl_t stathdl)
2860Sstevel@tonic-gate {
2870Sstevel@tonic-gate 	struct ctlib_status_info *info = stathdl;
2880Sstevel@tonic-gate 	return (info->status.ctst_zoneid);
2890Sstevel@tonic-gate }
2900Sstevel@tonic-gate 
2910Sstevel@tonic-gate const char *
2920Sstevel@tonic-gate ct_status_get_type(ct_stathdl_t stathdl)
2930Sstevel@tonic-gate {
2940Sstevel@tonic-gate 	struct ctlib_status_info *info = stathdl;
2950Sstevel@tonic-gate 	return (types[info->status.ctst_type].type_name);
2960Sstevel@tonic-gate }
2970Sstevel@tonic-gate 
2980Sstevel@tonic-gate id_t
2990Sstevel@tonic-gate ct_status_get_holder(ct_stathdl_t stathdl)
3000Sstevel@tonic-gate {
3010Sstevel@tonic-gate 	struct ctlib_status_info *info = stathdl;
3020Sstevel@tonic-gate 	return (info->status.ctst_holder);
3030Sstevel@tonic-gate }
3040Sstevel@tonic-gate 
3050Sstevel@tonic-gate ctstate_t
3060Sstevel@tonic-gate ct_status_get_state(ct_stathdl_t stathdl)
3070Sstevel@tonic-gate {
3080Sstevel@tonic-gate 	struct ctlib_status_info *info = stathdl;
3090Sstevel@tonic-gate 	return (info->status.ctst_state);
3100Sstevel@tonic-gate }
3110Sstevel@tonic-gate 
3120Sstevel@tonic-gate int
3130Sstevel@tonic-gate ct_status_get_nevents(ct_stathdl_t stathdl)
3140Sstevel@tonic-gate {
3150Sstevel@tonic-gate 	struct ctlib_status_info *info = stathdl;
3160Sstevel@tonic-gate 	return (info->status.ctst_nevents);
3170Sstevel@tonic-gate }
3180Sstevel@tonic-gate 
3190Sstevel@tonic-gate int
3200Sstevel@tonic-gate ct_status_get_ntime(ct_stathdl_t stathdl)
3210Sstevel@tonic-gate {
3220Sstevel@tonic-gate 	struct ctlib_status_info *info = stathdl;
3230Sstevel@tonic-gate 	return (info->status.ctst_ntime);
3240Sstevel@tonic-gate }
3250Sstevel@tonic-gate 
3260Sstevel@tonic-gate int
3270Sstevel@tonic-gate ct_status_get_qtime(ct_stathdl_t stathdl)
3280Sstevel@tonic-gate {
3290Sstevel@tonic-gate 	struct ctlib_status_info *info = stathdl;
3300Sstevel@tonic-gate 	return (info->status.ctst_qtime);
3310Sstevel@tonic-gate }
3320Sstevel@tonic-gate 
3330Sstevel@tonic-gate ctevid_t
3340Sstevel@tonic-gate ct_status_get_nevid(ct_stathdl_t stathdl)
3350Sstevel@tonic-gate {
3360Sstevel@tonic-gate 	struct ctlib_status_info *info = stathdl;
3370Sstevel@tonic-gate 	return (info->status.ctst_nevid);
3380Sstevel@tonic-gate }
3390Sstevel@tonic-gate 
3400Sstevel@tonic-gate uint_t
3410Sstevel@tonic-gate ct_status_get_informative(ct_stathdl_t stathdl)
3420Sstevel@tonic-gate {
3430Sstevel@tonic-gate 	struct ctlib_status_info *info = stathdl;
3440Sstevel@tonic-gate 	return (info->status.ctst_informative);
3450Sstevel@tonic-gate }
3460Sstevel@tonic-gate 
3470Sstevel@tonic-gate uint_t
3480Sstevel@tonic-gate ct_status_get_critical(ct_stathdl_t stathdl)
3490Sstevel@tonic-gate {
3500Sstevel@tonic-gate 	struct ctlib_status_info *info = stathdl;
3510Sstevel@tonic-gate 	return (info->status.ctst_critical);
3520Sstevel@tonic-gate }
3530Sstevel@tonic-gate 
3540Sstevel@tonic-gate uint64_t
3550Sstevel@tonic-gate ct_status_get_cookie(ct_stathdl_t stathdl)
3560Sstevel@tonic-gate {
3570Sstevel@tonic-gate 	struct ctlib_status_info *info = stathdl;
3580Sstevel@tonic-gate 	return (info->status.ctst_cookie);
3590Sstevel@tonic-gate }
3600Sstevel@tonic-gate 
3610Sstevel@tonic-gate /*
3620Sstevel@tonic-gate  * Common event routines
3630Sstevel@tonic-gate  */
3640Sstevel@tonic-gate 
3650Sstevel@tonic-gate static int
3660Sstevel@tonic-gate unpack_and_merge(nvlist_t **nvl, char *buffer, size_t len)
3670Sstevel@tonic-gate {
3680Sstevel@tonic-gate 	nvlist_t *tmpnvl;
3690Sstevel@tonic-gate 	int error;
3700Sstevel@tonic-gate 
3710Sstevel@tonic-gate 	if ((error = nvlist_unpack(buffer, len, &tmpnvl, 0)) != 0)
3720Sstevel@tonic-gate 		return (error);
3730Sstevel@tonic-gate 
3740Sstevel@tonic-gate 	if (*nvl == NULL) {
3750Sstevel@tonic-gate 		*nvl = tmpnvl;
3760Sstevel@tonic-gate 		return (0);
3770Sstevel@tonic-gate 	}
3780Sstevel@tonic-gate 
3790Sstevel@tonic-gate 	error = nvlist_merge(*nvl, tmpnvl, 0);
3800Sstevel@tonic-gate 	nvlist_free(tmpnvl);
3810Sstevel@tonic-gate 	return (error);
3820Sstevel@tonic-gate }
3830Sstevel@tonic-gate 
3840Sstevel@tonic-gate static int
3850Sstevel@tonic-gate ct_event_read_internal(int fd, int cmd, ct_evthdl_t *evt)
3860Sstevel@tonic-gate {
3870Sstevel@tonic-gate 	char *event_buffer = NULL;
3880Sstevel@tonic-gate 	int event_nbytes = 0;
3890Sstevel@tonic-gate 	struct ctlib_event_info *info;
3900Sstevel@tonic-gate 	ct_event_t *event;
3910Sstevel@tonic-gate 	int error;
3920Sstevel@tonic-gate 
3930Sstevel@tonic-gate 	info = malloc(sizeof (struct ctlib_event_info));
3940Sstevel@tonic-gate 	if (info == NULL)
3950Sstevel@tonic-gate 		return (errno);
3960Sstevel@tonic-gate 	info->nvl = NULL;
3970Sstevel@tonic-gate 	event = &info->event;
3980Sstevel@tonic-gate 
3990Sstevel@tonic-gate 	for (;;) {
4000Sstevel@tonic-gate 		event->ctev_nbytes = event_nbytes;
4010Sstevel@tonic-gate 		event->ctev_buffer = event_buffer;
4020Sstevel@tonic-gate 		do
4030Sstevel@tonic-gate 			error = ioctl(fd, cmd, event);
4040Sstevel@tonic-gate 		while (error == -1 && errno == EINTR);
4050Sstevel@tonic-gate 		if (error == -1) {
4060Sstevel@tonic-gate 			error = errno;
4070Sstevel@tonic-gate 			goto errout;
4080Sstevel@tonic-gate 		}
4090Sstevel@tonic-gate 		if (event->ctev_nbytes <= event_nbytes)
4100Sstevel@tonic-gate 			break;
4110Sstevel@tonic-gate 
4120Sstevel@tonic-gate 		if (event_buffer)
4130Sstevel@tonic-gate 			free(event_buffer);
4140Sstevel@tonic-gate 		event_nbytes = event->ctev_nbytes;
4150Sstevel@tonic-gate 		event_buffer = malloc(event_nbytes);
4160Sstevel@tonic-gate 		if (event_buffer == NULL) {
4170Sstevel@tonic-gate 			error = errno;
4180Sstevel@tonic-gate 			goto errout;
4190Sstevel@tonic-gate 		}
4200Sstevel@tonic-gate 	}
4210Sstevel@tonic-gate 
4220Sstevel@tonic-gate 	if (event->ctev_goffset > 0 && (error = unpack_and_merge(&info->nvl,
4230Sstevel@tonic-gate 	    event->ctev_buffer, event->ctev_goffset)) != 0)
4240Sstevel@tonic-gate 		goto errout;
4250Sstevel@tonic-gate 
4260Sstevel@tonic-gate 	if (event->ctev_goffset < event->ctev_nbytes &&
4270Sstevel@tonic-gate 	    (error = unpack_and_merge(&info->nvl,
4280Sstevel@tonic-gate 	    event->ctev_buffer + event->ctev_goffset,
4290Sstevel@tonic-gate 	    event->ctev_nbytes - event->ctev_goffset)) != 0)
4300Sstevel@tonic-gate 		goto errout;
4310Sstevel@tonic-gate 
4320Sstevel@tonic-gate 	free(event_buffer);
4330Sstevel@tonic-gate 
4340Sstevel@tonic-gate 	*evt = info;
4350Sstevel@tonic-gate 	return (0);
4360Sstevel@tonic-gate 
4370Sstevel@tonic-gate errout:
4380Sstevel@tonic-gate 	if (event_buffer)
4390Sstevel@tonic-gate 		free(event_buffer);
4400Sstevel@tonic-gate 	if (info) {
4410Sstevel@tonic-gate 		if (info->nvl)
4420Sstevel@tonic-gate 			nvlist_free(info->nvl);
4430Sstevel@tonic-gate 		free(info);
4440Sstevel@tonic-gate 	}
4450Sstevel@tonic-gate 	return (error);
4460Sstevel@tonic-gate }
4470Sstevel@tonic-gate 
4480Sstevel@tonic-gate int
4490Sstevel@tonic-gate ct_event_read(int fd, ct_evthdl_t *evthdl)
4500Sstevel@tonic-gate {
4510Sstevel@tonic-gate 	return (ct_event_read_internal(fd, CT_ERECV, evthdl));
4520Sstevel@tonic-gate }
4530Sstevel@tonic-gate 
4540Sstevel@tonic-gate int
4550Sstevel@tonic-gate ct_event_read_critical(int fd, ct_evthdl_t *evthdl)
4560Sstevel@tonic-gate {
4570Sstevel@tonic-gate 	return (ct_event_read_internal(fd, CT_ECRECV, evthdl));
4580Sstevel@tonic-gate }
4590Sstevel@tonic-gate 
4600Sstevel@tonic-gate int
4610Sstevel@tonic-gate ct_event_reset(int fd)
4620Sstevel@tonic-gate {
4630Sstevel@tonic-gate 	if (ioctl(fd, CT_ERESET) == -1)
4640Sstevel@tonic-gate 		return (errno);
4650Sstevel@tonic-gate 	return (0);
4660Sstevel@tonic-gate }
4670Sstevel@tonic-gate 
4680Sstevel@tonic-gate int
4690Sstevel@tonic-gate ct_event_reliable(int fd)
4700Sstevel@tonic-gate {
4710Sstevel@tonic-gate 	if (ioctl(fd, CT_ERELIABLE) == -1)
4720Sstevel@tonic-gate 		return (errno);
4730Sstevel@tonic-gate 	return (0);
4740Sstevel@tonic-gate }
4750Sstevel@tonic-gate 
4760Sstevel@tonic-gate void
4770Sstevel@tonic-gate ct_event_free(ct_evthdl_t evthdl)
4780Sstevel@tonic-gate {
4790Sstevel@tonic-gate 	struct ctlib_event_info *info = evthdl;
4800Sstevel@tonic-gate 
4810Sstevel@tonic-gate 	if (info->nvl)
4820Sstevel@tonic-gate 		nvlist_free(info->nvl);
4830Sstevel@tonic-gate 	free(info);
4840Sstevel@tonic-gate }
4850Sstevel@tonic-gate 
4860Sstevel@tonic-gate 
4870Sstevel@tonic-gate uint_t
4880Sstevel@tonic-gate ct_event_get_flags(ct_evthdl_t evthdl)
4890Sstevel@tonic-gate {
4900Sstevel@tonic-gate 	struct ctlib_event_info *info = evthdl;
4910Sstevel@tonic-gate 	return (info->event.ctev_flags);
4920Sstevel@tonic-gate }
4930Sstevel@tonic-gate 
4940Sstevel@tonic-gate ctid_t
4950Sstevel@tonic-gate ct_event_get_ctid(ct_evthdl_t evthdl)
4960Sstevel@tonic-gate {
4970Sstevel@tonic-gate 	struct ctlib_event_info *info = evthdl;
4980Sstevel@tonic-gate 	return (info->event.ctev_id);
4990Sstevel@tonic-gate }
5000Sstevel@tonic-gate 
5010Sstevel@tonic-gate ctevid_t
5020Sstevel@tonic-gate ct_event_get_evid(ct_evthdl_t evthdl)
5030Sstevel@tonic-gate {
5040Sstevel@tonic-gate 	struct ctlib_event_info *info = evthdl;
5050Sstevel@tonic-gate 	return (info->event.ctev_evid);
5060Sstevel@tonic-gate }
5070Sstevel@tonic-gate 
5080Sstevel@tonic-gate uint_t
5090Sstevel@tonic-gate ct_event_get_type(ct_evthdl_t evthdl)
5100Sstevel@tonic-gate {
5110Sstevel@tonic-gate 	struct ctlib_event_info *info = evthdl;
5120Sstevel@tonic-gate 	return (info->event.ctev_type);
5130Sstevel@tonic-gate }
5140Sstevel@tonic-gate 
5150Sstevel@tonic-gate int
5160Sstevel@tonic-gate ct_event_get_nevid(ct_evthdl_t evthdl, ctevid_t *evidp)
5170Sstevel@tonic-gate {
5180Sstevel@tonic-gate 	struct ctlib_event_info *info = evthdl;
5190Sstevel@tonic-gate 	if (info->nvl == NULL ||
5200Sstevel@tonic-gate 	    nvlist_lookup_uint64(info->nvl, CTS_NEVID, evidp))
5210Sstevel@tonic-gate 		return (EINVAL);
5220Sstevel@tonic-gate 	return (0);
5230Sstevel@tonic-gate }
5240Sstevel@tonic-gate 
5250Sstevel@tonic-gate int
5260Sstevel@tonic-gate ct_event_get_newct(ct_evthdl_t evthdl, ctid_t *ctidp)
5270Sstevel@tonic-gate {
5280Sstevel@tonic-gate 	struct ctlib_event_info *info = evthdl;
5290Sstevel@tonic-gate 	if (info->nvl == NULL ||
5300Sstevel@tonic-gate 	    nvlist_lookup_int32(info->nvl, CTS_NEWCT, (int *)ctidp))
5310Sstevel@tonic-gate 		return (EINVAL);
5320Sstevel@tonic-gate 	return (0);
5330Sstevel@tonic-gate }
534