17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
525e8c5aaSvikram  * Common Development and Distribution License (the "License").
625e8c5aaSvikram  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
227b209c2cSacruz  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #include <sys/ctfs.h>
277c478bd9Sstevel@tonic-gate #include <sys/contract.h>
287b209c2cSacruz #include <string.h>
297c478bd9Sstevel@tonic-gate #include <libnvpair.h>
307c478bd9Sstevel@tonic-gate #include <assert.h>
317c478bd9Sstevel@tonic-gate #include <unistd.h>
327c478bd9Sstevel@tonic-gate #include <errno.h>
337c478bd9Sstevel@tonic-gate #include <libcontract.h>
347c478bd9Sstevel@tonic-gate #include "libcontract_impl.h"
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate /*
377c478bd9Sstevel@tonic-gate  * Common template routines
387c478bd9Sstevel@tonic-gate  */
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate int
ct_tmpl_activate(int fd)417c478bd9Sstevel@tonic-gate ct_tmpl_activate(int fd)
427c478bd9Sstevel@tonic-gate {
437c478bd9Sstevel@tonic-gate 	if (ioctl(fd, CT_TACTIVATE) == -1)
447c478bd9Sstevel@tonic-gate 		return (errno);
457c478bd9Sstevel@tonic-gate 	return (0);
467c478bd9Sstevel@tonic-gate }
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate int
ct_tmpl_clear(int fd)497c478bd9Sstevel@tonic-gate ct_tmpl_clear(int fd)
507c478bd9Sstevel@tonic-gate {
517c478bd9Sstevel@tonic-gate 	if (ioctl(fd, CT_TCLEAR) == -1)
527c478bd9Sstevel@tonic-gate 		return (errno);
537c478bd9Sstevel@tonic-gate 	return (0);
547c478bd9Sstevel@tonic-gate }
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate int
ct_tmpl_create(int fd,ctid_t * ctidp)577c478bd9Sstevel@tonic-gate ct_tmpl_create(int fd, ctid_t *ctidp)
587c478bd9Sstevel@tonic-gate {
597c478bd9Sstevel@tonic-gate 	ctid_t ctid = ioctl(fd, CT_TCREATE);
607c478bd9Sstevel@tonic-gate 	if (ctid == -1)
617c478bd9Sstevel@tonic-gate 		return (errno);
627c478bd9Sstevel@tonic-gate 	*ctidp = ctid;
637c478bd9Sstevel@tonic-gate 	return (0);
647c478bd9Sstevel@tonic-gate }
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate int
ct_tmpl_set_internal(int fd,uint_t id,uintptr_t value)6725e8c5aaSvikram ct_tmpl_set_internal(int fd, uint_t id, uintptr_t value)
687c478bd9Sstevel@tonic-gate {
697c478bd9Sstevel@tonic-gate 	ct_param_t param;
707b209c2cSacruz 	uint64_t param_value = value;
717b209c2cSacruz 
727b209c2cSacruz 	param.ctpm_id = id;
737b209c2cSacruz 	param.ctpm_size = sizeof (uint64_t);
747b209c2cSacruz 	param.ctpm_value = &param_value;
757b209c2cSacruz 	if (ioctl(fd, CT_TSET, &param) == -1)
767b209c2cSacruz 		return (errno);
777b209c2cSacruz 
787b209c2cSacruz 	return (0);
797b209c2cSacruz }
807b209c2cSacruz 
817b209c2cSacruz int
ct_tmpl_set_internal_string(int fd,uint_t id,const char * value)827b209c2cSacruz ct_tmpl_set_internal_string(int fd, uint_t id, const char *value)
837b209c2cSacruz {
847b209c2cSacruz 	ct_param_t param;
857b209c2cSacruz 
867b209c2cSacruz 	if (value == NULL)
877b209c2cSacruz 		return (EINVAL);
887c478bd9Sstevel@tonic-gate 	param.ctpm_id = id;
897b209c2cSacruz 	param.ctpm_size = strlen(value) + 1;
907b209c2cSacruz 	param.ctpm_value = (void *)value;
917c478bd9Sstevel@tonic-gate 	if (ioctl(fd, CT_TSET, &param) == -1)
927c478bd9Sstevel@tonic-gate 		return (errno);
937b209c2cSacruz 
947c478bd9Sstevel@tonic-gate 	return (0);
957c478bd9Sstevel@tonic-gate }
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate int
ct_tmpl_set_critical(int fd,uint_t events)987c478bd9Sstevel@tonic-gate ct_tmpl_set_critical(int fd, uint_t events)
997c478bd9Sstevel@tonic-gate {
1007c478bd9Sstevel@tonic-gate 	return (ct_tmpl_set_internal(fd, CTP_EV_CRITICAL, events));
1017c478bd9Sstevel@tonic-gate }
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate int
ct_tmpl_set_informative(int fd,uint_t events)1047c478bd9Sstevel@tonic-gate ct_tmpl_set_informative(int fd, uint_t events)
1057c478bd9Sstevel@tonic-gate {
1067c478bd9Sstevel@tonic-gate 	return (ct_tmpl_set_internal(fd, CTP_EV_INFO, events));
1077c478bd9Sstevel@tonic-gate }
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate int
ct_tmpl_set_cookie(int fd,uint64_t cookie)1107c478bd9Sstevel@tonic-gate ct_tmpl_set_cookie(int fd, uint64_t cookie)
1117c478bd9Sstevel@tonic-gate {
1127c478bd9Sstevel@tonic-gate 	ct_param_t param;
1137b209c2cSacruz 	uint64_t param_value = cookie;
1147b209c2cSacruz 
1157c478bd9Sstevel@tonic-gate 	param.ctpm_id = CTP_COOKIE;
1167b209c2cSacruz 	param.ctpm_size = sizeof (uint64_t);
1177b209c2cSacruz 	param.ctpm_value = &param_value;
1187c478bd9Sstevel@tonic-gate 	if (ioctl(fd, CT_TSET, &param) == -1)
1197c478bd9Sstevel@tonic-gate 		return (errno);
1207c478bd9Sstevel@tonic-gate 	return (0);
1217c478bd9Sstevel@tonic-gate }
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate int
ct_tmpl_get_internal(int fd,uint_t id,uint_t * value)1247c478bd9Sstevel@tonic-gate ct_tmpl_get_internal(int fd, uint_t id, uint_t *value)
1257c478bd9Sstevel@tonic-gate {
1267c478bd9Sstevel@tonic-gate 	ct_param_t param;
1277b209c2cSacruz 	uint64_t param_value;
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate 	param.ctpm_id = id;
1307b209c2cSacruz 	param.ctpm_size = sizeof (uint64_t);
1317b209c2cSacruz 	param.ctpm_value = &param_value;
1327c478bd9Sstevel@tonic-gate 	if (ioctl(fd, CT_TGET, &param) == -1)
1337c478bd9Sstevel@tonic-gate 		return (errno);
1347b209c2cSacruz 	*value = param_value;
1357c478bd9Sstevel@tonic-gate 	return (0);
1367c478bd9Sstevel@tonic-gate }
1377c478bd9Sstevel@tonic-gate 
13825e8c5aaSvikram int
ct_tmpl_get_internal_string(int fd,uint32_t id,char * buf,size_t size)1397b209c2cSacruz ct_tmpl_get_internal_string(int fd, uint32_t id, char *buf, size_t size)
14025e8c5aaSvikram {
14125e8c5aaSvikram 	ct_param_t param;
14225e8c5aaSvikram 
14325e8c5aaSvikram 	param.ctpm_id = id;
1447b209c2cSacruz 	param.ctpm_size = size;
1457b209c2cSacruz 	param.ctpm_value = buf;
14625e8c5aaSvikram 	if (ioctl(fd, CT_TGET, &param) == -1)
1477b209c2cSacruz 		return (-1);
1487b209c2cSacruz 	return (param.ctpm_size);
14925e8c5aaSvikram }
15025e8c5aaSvikram 
1517c478bd9Sstevel@tonic-gate int
ct_tmpl_get_critical(int fd,uint_t * events)1527c478bd9Sstevel@tonic-gate ct_tmpl_get_critical(int fd, uint_t *events)
1537c478bd9Sstevel@tonic-gate {
1547c478bd9Sstevel@tonic-gate 	return (ct_tmpl_get_internal(fd, CTP_EV_CRITICAL, events));
1557c478bd9Sstevel@tonic-gate }
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate int
ct_tmpl_get_informative(int fd,uint_t * events)1587c478bd9Sstevel@tonic-gate ct_tmpl_get_informative(int fd, uint_t *events)
1597c478bd9Sstevel@tonic-gate {
1607c478bd9Sstevel@tonic-gate 	return (ct_tmpl_get_internal(fd, CTP_EV_INFO, events));
1617c478bd9Sstevel@tonic-gate }
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate int
ct_tmpl_get_cookie(int fd,uint64_t * cookie)1647c478bd9Sstevel@tonic-gate ct_tmpl_get_cookie(int fd, uint64_t *cookie)
1657c478bd9Sstevel@tonic-gate {
1667c478bd9Sstevel@tonic-gate 	ct_param_t param;
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate 	param.ctpm_id = CTP_COOKIE;
1697b209c2cSacruz 	param.ctpm_size = sizeof (uint64_t);
1707b209c2cSacruz 	param.ctpm_value = cookie;
1717c478bd9Sstevel@tonic-gate 	if (ioctl(fd, CT_TGET, &param) == -1)
1727c478bd9Sstevel@tonic-gate 		return (errno);
1737c478bd9Sstevel@tonic-gate 	return (0);
1747c478bd9Sstevel@tonic-gate }
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate /*
1777c478bd9Sstevel@tonic-gate  * Common ctl routines
1787c478bd9Sstevel@tonic-gate  */
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate int
ct_ctl_adopt(int fd)1817c478bd9Sstevel@tonic-gate ct_ctl_adopt(int fd)
1827c478bd9Sstevel@tonic-gate {
1837c478bd9Sstevel@tonic-gate 	if (ioctl(fd, CT_CADOPT) == -1)
1847c478bd9Sstevel@tonic-gate 		return (errno);
1857c478bd9Sstevel@tonic-gate 	return (0);
1867c478bd9Sstevel@tonic-gate }
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate int
ct_ctl_abandon(int fd)1897c478bd9Sstevel@tonic-gate ct_ctl_abandon(int fd)
1907c478bd9Sstevel@tonic-gate {
1917c478bd9Sstevel@tonic-gate 	if (ioctl(fd, CT_CABANDON) == -1)
1927c478bd9Sstevel@tonic-gate 		return (errno);
1937c478bd9Sstevel@tonic-gate 	return (0);
1947c478bd9Sstevel@tonic-gate }
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1977c478bd9Sstevel@tonic-gate int
ct_ctl_newct(int cfd,ctevid_t evid,int tfd)1987c478bd9Sstevel@tonic-gate ct_ctl_newct(int cfd, ctevid_t evid, int tfd)
1997c478bd9Sstevel@tonic-gate {
2007c478bd9Sstevel@tonic-gate 	if (ioctl(cfd, CT_CNEWCT, tfd) == -1)
2017c478bd9Sstevel@tonic-gate 		return (errno);
2027c478bd9Sstevel@tonic-gate 	return (0);
2037c478bd9Sstevel@tonic-gate }
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate int
ct_ctl_ack(int fd,ctevid_t event)2067c478bd9Sstevel@tonic-gate ct_ctl_ack(int fd, ctevid_t event)
2077c478bd9Sstevel@tonic-gate {
2087c478bd9Sstevel@tonic-gate 	if (ioctl(fd, CT_CACK, &event) == -1)
2097c478bd9Sstevel@tonic-gate 		return (errno);
2107c478bd9Sstevel@tonic-gate 	return (0);
2117c478bd9Sstevel@tonic-gate }
2127c478bd9Sstevel@tonic-gate 
21325e8c5aaSvikram int
ct_ctl_nack(int fd,ctevid_t event)21425e8c5aaSvikram ct_ctl_nack(int fd, ctevid_t event)
21525e8c5aaSvikram {
21625e8c5aaSvikram 	if (ioctl(fd, CT_CNACK, &event) == -1)
21725e8c5aaSvikram 		return (errno);
21825e8c5aaSvikram 	return (0);
21925e8c5aaSvikram }
22025e8c5aaSvikram 
2217c478bd9Sstevel@tonic-gate int
ct_ctl_qack(int fd,ctevid_t event)2227c478bd9Sstevel@tonic-gate ct_ctl_qack(int fd, ctevid_t event)
2237c478bd9Sstevel@tonic-gate {
2247c478bd9Sstevel@tonic-gate 	if (ioctl(fd, CT_CQREQ, &event) == -1)
2257c478bd9Sstevel@tonic-gate 		return (errno);
2267c478bd9Sstevel@tonic-gate 	return (0);
2277c478bd9Sstevel@tonic-gate }
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate /*
2307c478bd9Sstevel@tonic-gate  * Common status routines
2317c478bd9Sstevel@tonic-gate  */
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate int
ct_status_read(int fd,int detail,ct_stathdl_t * stathdl)2347c478bd9Sstevel@tonic-gate ct_status_read(int fd, int detail, ct_stathdl_t *stathdl)
2357c478bd9Sstevel@tonic-gate {
2367c478bd9Sstevel@tonic-gate 	char *status_buffer = NULL;
2377c478bd9Sstevel@tonic-gate 	int status_nbytes = 0;
2387c478bd9Sstevel@tonic-gate 	struct ctlib_status_info *info;
2397c478bd9Sstevel@tonic-gate 	int error;
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate 	info = malloc(sizeof (struct ctlib_status_info));
2427c478bd9Sstevel@tonic-gate 	if (info == NULL)
2437c478bd9Sstevel@tonic-gate 		return (errno);
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate 	info->status.ctst_detail = detail;
2467c478bd9Sstevel@tonic-gate 	if (detail != CTD_COMMON) {
2477c478bd9Sstevel@tonic-gate 		for (;;) {
2487c478bd9Sstevel@tonic-gate 			info->status.ctst_nbytes = status_nbytes;
2497c478bd9Sstevel@tonic-gate 			info->status.ctst_buffer = status_buffer;
2507c478bd9Sstevel@tonic-gate 			do
2517c478bd9Sstevel@tonic-gate 				error = ioctl(fd, CT_SSTATUS, &info->status);
2527c478bd9Sstevel@tonic-gate 			while (error == -1 && errno == EINTR);
2537c478bd9Sstevel@tonic-gate 			if (error == -1)
2547c478bd9Sstevel@tonic-gate 				goto errout;
2557c478bd9Sstevel@tonic-gate 			if (info->status.ctst_nbytes <= status_nbytes)
2567c478bd9Sstevel@tonic-gate 				break;
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate 			if (status_buffer)
2597c478bd9Sstevel@tonic-gate 				free(status_buffer);
2607c478bd9Sstevel@tonic-gate 			status_nbytes = info->status.ctst_nbytes;
2617c478bd9Sstevel@tonic-gate 			status_buffer = malloc(status_nbytes);
2627c478bd9Sstevel@tonic-gate 			if (status_buffer == NULL)
2637c478bd9Sstevel@tonic-gate 				goto errout;
2647c478bd9Sstevel@tonic-gate 		}
2657c478bd9Sstevel@tonic-gate 		if ((errno = nvlist_unpack(info->status.ctst_buffer,
2667c478bd9Sstevel@tonic-gate 		    info->status.ctst_nbytes, &info->nvl, 0)) != 0)
2677c478bd9Sstevel@tonic-gate 			goto errout;
2687c478bd9Sstevel@tonic-gate 
2697c478bd9Sstevel@tonic-gate 		free(status_buffer);
2707c478bd9Sstevel@tonic-gate 		status_buffer = NULL;
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate 	} else {
2737c478bd9Sstevel@tonic-gate 		info->status.ctst_nbytes = 0;
2747c478bd9Sstevel@tonic-gate 		info->nvl = NULL;
2757c478bd9Sstevel@tonic-gate 		if (ioctl(fd, CT_SSTATUS, &info->status) == -1)
2767c478bd9Sstevel@tonic-gate 			goto errout;
2777c478bd9Sstevel@tonic-gate 	}
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate 	*stathdl = info;
2807c478bd9Sstevel@tonic-gate 	return (0);
2817c478bd9Sstevel@tonic-gate 
2827c478bd9Sstevel@tonic-gate errout:
2837c478bd9Sstevel@tonic-gate 	error = errno;
2847c478bd9Sstevel@tonic-gate 	if (status_buffer)
2857c478bd9Sstevel@tonic-gate 		free(status_buffer);
2867c478bd9Sstevel@tonic-gate 	if (info)
2877c478bd9Sstevel@tonic-gate 		free(info);
2887c478bd9Sstevel@tonic-gate 	return (error);
2897c478bd9Sstevel@tonic-gate }
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate void
ct_status_free(ct_stathdl_t stathdl)2927c478bd9Sstevel@tonic-gate ct_status_free(ct_stathdl_t stathdl)
2937c478bd9Sstevel@tonic-gate {
2947c478bd9Sstevel@tonic-gate 	struct ctlib_status_info *info = stathdl;
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate 	if (info->nvl) {
2977c478bd9Sstevel@tonic-gate 		assert(info->status.ctst_detail != CTD_COMMON);
2987c478bd9Sstevel@tonic-gate 		nvlist_free(info->nvl);
2997c478bd9Sstevel@tonic-gate 	}
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate 	free(info);
3027c478bd9Sstevel@tonic-gate }
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate ctid_t
ct_status_get_id(ct_stathdl_t stathdl)3057c478bd9Sstevel@tonic-gate ct_status_get_id(ct_stathdl_t stathdl)
3067c478bd9Sstevel@tonic-gate {
3077c478bd9Sstevel@tonic-gate 	struct ctlib_status_info *info = stathdl;
3087c478bd9Sstevel@tonic-gate 	return (info->status.ctst_id);
3097c478bd9Sstevel@tonic-gate }
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate zoneid_t
ct_status_get_zoneid(ct_stathdl_t stathdl)3127c478bd9Sstevel@tonic-gate ct_status_get_zoneid(ct_stathdl_t stathdl)
3137c478bd9Sstevel@tonic-gate {
3147c478bd9Sstevel@tonic-gate 	struct ctlib_status_info *info = stathdl;
3157c478bd9Sstevel@tonic-gate 	return (info->status.ctst_zoneid);
3167c478bd9Sstevel@tonic-gate }
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate const char *
ct_status_get_type(ct_stathdl_t stathdl)3197c478bd9Sstevel@tonic-gate ct_status_get_type(ct_stathdl_t stathdl)
3207c478bd9Sstevel@tonic-gate {
3217c478bd9Sstevel@tonic-gate 	struct ctlib_status_info *info = stathdl;
3227c478bd9Sstevel@tonic-gate 	return (types[info->status.ctst_type].type_name);
3237c478bd9Sstevel@tonic-gate }
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate id_t
ct_status_get_holder(ct_stathdl_t stathdl)3267c478bd9Sstevel@tonic-gate ct_status_get_holder(ct_stathdl_t stathdl)
3277c478bd9Sstevel@tonic-gate {
3287c478bd9Sstevel@tonic-gate 	struct ctlib_status_info *info = stathdl;
3297c478bd9Sstevel@tonic-gate 	return (info->status.ctst_holder);
3307c478bd9Sstevel@tonic-gate }
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate ctstate_t
ct_status_get_state(ct_stathdl_t stathdl)3337c478bd9Sstevel@tonic-gate ct_status_get_state(ct_stathdl_t stathdl)
3347c478bd9Sstevel@tonic-gate {
3357c478bd9Sstevel@tonic-gate 	struct ctlib_status_info *info = stathdl;
3367c478bd9Sstevel@tonic-gate 	return (info->status.ctst_state);
3377c478bd9Sstevel@tonic-gate }
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate int
ct_status_get_nevents(ct_stathdl_t stathdl)3407c478bd9Sstevel@tonic-gate ct_status_get_nevents(ct_stathdl_t stathdl)
3417c478bd9Sstevel@tonic-gate {
3427c478bd9Sstevel@tonic-gate 	struct ctlib_status_info *info = stathdl;
3437c478bd9Sstevel@tonic-gate 	return (info->status.ctst_nevents);
3447c478bd9Sstevel@tonic-gate }
3457c478bd9Sstevel@tonic-gate 
3467c478bd9Sstevel@tonic-gate int
ct_status_get_ntime(ct_stathdl_t stathdl)3477c478bd9Sstevel@tonic-gate ct_status_get_ntime(ct_stathdl_t stathdl)
3487c478bd9Sstevel@tonic-gate {
3497c478bd9Sstevel@tonic-gate 	struct ctlib_status_info *info = stathdl;
3507c478bd9Sstevel@tonic-gate 	return (info->status.ctst_ntime);
3517c478bd9Sstevel@tonic-gate }
3527c478bd9Sstevel@tonic-gate 
3537c478bd9Sstevel@tonic-gate int
ct_status_get_qtime(ct_stathdl_t stathdl)3547c478bd9Sstevel@tonic-gate ct_status_get_qtime(ct_stathdl_t stathdl)
3557c478bd9Sstevel@tonic-gate {
3567c478bd9Sstevel@tonic-gate 	struct ctlib_status_info *info = stathdl;
3577c478bd9Sstevel@tonic-gate 	return (info->status.ctst_qtime);
3587c478bd9Sstevel@tonic-gate }
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate ctevid_t
ct_status_get_nevid(ct_stathdl_t stathdl)3617c478bd9Sstevel@tonic-gate ct_status_get_nevid(ct_stathdl_t stathdl)
3627c478bd9Sstevel@tonic-gate {
3637c478bd9Sstevel@tonic-gate 	struct ctlib_status_info *info = stathdl;
3647c478bd9Sstevel@tonic-gate 	return (info->status.ctst_nevid);
3657c478bd9Sstevel@tonic-gate }
3667c478bd9Sstevel@tonic-gate 
3677c478bd9Sstevel@tonic-gate uint_t
ct_status_get_informative(ct_stathdl_t stathdl)3687c478bd9Sstevel@tonic-gate ct_status_get_informative(ct_stathdl_t stathdl)
3697c478bd9Sstevel@tonic-gate {
3707c478bd9Sstevel@tonic-gate 	struct ctlib_status_info *info = stathdl;
3717c478bd9Sstevel@tonic-gate 	return (info->status.ctst_informative);
3727c478bd9Sstevel@tonic-gate }
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate uint_t
ct_status_get_critical(ct_stathdl_t stathdl)3757c478bd9Sstevel@tonic-gate ct_status_get_critical(ct_stathdl_t stathdl)
3767c478bd9Sstevel@tonic-gate {
3777c478bd9Sstevel@tonic-gate 	struct ctlib_status_info *info = stathdl;
3787c478bd9Sstevel@tonic-gate 	return (info->status.ctst_critical);
3797c478bd9Sstevel@tonic-gate }
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate uint64_t
ct_status_get_cookie(ct_stathdl_t stathdl)3827c478bd9Sstevel@tonic-gate ct_status_get_cookie(ct_stathdl_t stathdl)
3837c478bd9Sstevel@tonic-gate {
3847c478bd9Sstevel@tonic-gate 	struct ctlib_status_info *info = stathdl;
3857c478bd9Sstevel@tonic-gate 	return (info->status.ctst_cookie);
3867c478bd9Sstevel@tonic-gate }
3877c478bd9Sstevel@tonic-gate 
3887c478bd9Sstevel@tonic-gate /*
3897c478bd9Sstevel@tonic-gate  * Common event routines
3907c478bd9Sstevel@tonic-gate  */
3917c478bd9Sstevel@tonic-gate 
3927c478bd9Sstevel@tonic-gate static int
unpack_and_merge(nvlist_t ** nvl,char * buffer,size_t len)3937c478bd9Sstevel@tonic-gate unpack_and_merge(nvlist_t **nvl, char *buffer, size_t len)
3947c478bd9Sstevel@tonic-gate {
3957c478bd9Sstevel@tonic-gate 	nvlist_t *tmpnvl;
3967c478bd9Sstevel@tonic-gate 	int error;
3977c478bd9Sstevel@tonic-gate 
3987c478bd9Sstevel@tonic-gate 	if ((error = nvlist_unpack(buffer, len, &tmpnvl, 0)) != 0)
3997c478bd9Sstevel@tonic-gate 		return (error);
4007c478bd9Sstevel@tonic-gate 
4017c478bd9Sstevel@tonic-gate 	if (*nvl == NULL) {
4027c478bd9Sstevel@tonic-gate 		*nvl = tmpnvl;
4037c478bd9Sstevel@tonic-gate 		return (0);
4047c478bd9Sstevel@tonic-gate 	}
4057c478bd9Sstevel@tonic-gate 
4067c478bd9Sstevel@tonic-gate 	error = nvlist_merge(*nvl, tmpnvl, 0);
4077c478bd9Sstevel@tonic-gate 	nvlist_free(tmpnvl);
4087c478bd9Sstevel@tonic-gate 	return (error);
4097c478bd9Sstevel@tonic-gate }
4107c478bd9Sstevel@tonic-gate 
4117c478bd9Sstevel@tonic-gate static int
ct_event_read_internal(int fd,int cmd,ct_evthdl_t * evt)4127c478bd9Sstevel@tonic-gate ct_event_read_internal(int fd, int cmd, ct_evthdl_t *evt)
4137c478bd9Sstevel@tonic-gate {
4147c478bd9Sstevel@tonic-gate 	char *event_buffer = NULL;
4157c478bd9Sstevel@tonic-gate 	int event_nbytes = 0;
4167c478bd9Sstevel@tonic-gate 	struct ctlib_event_info *info;
4177c478bd9Sstevel@tonic-gate 	ct_event_t *event;
4187c478bd9Sstevel@tonic-gate 	int error;
4197c478bd9Sstevel@tonic-gate 
4207c478bd9Sstevel@tonic-gate 	info = malloc(sizeof (struct ctlib_event_info));
4217c478bd9Sstevel@tonic-gate 	if (info == NULL)
4227c478bd9Sstevel@tonic-gate 		return (errno);
4237c478bd9Sstevel@tonic-gate 	info->nvl = NULL;
4247c478bd9Sstevel@tonic-gate 	event = &info->event;
4257c478bd9Sstevel@tonic-gate 
4267c478bd9Sstevel@tonic-gate 	for (;;) {
4277c478bd9Sstevel@tonic-gate 		event->ctev_nbytes = event_nbytes;
4287c478bd9Sstevel@tonic-gate 		event->ctev_buffer = event_buffer;
4297c478bd9Sstevel@tonic-gate 		do
4307c478bd9Sstevel@tonic-gate 			error = ioctl(fd, cmd, event);
4317c478bd9Sstevel@tonic-gate 		while (error == -1 && errno == EINTR);
4327c478bd9Sstevel@tonic-gate 		if (error == -1) {
4337c478bd9Sstevel@tonic-gate 			error = errno;
4347c478bd9Sstevel@tonic-gate 			goto errout;
4357c478bd9Sstevel@tonic-gate 		}
4367c478bd9Sstevel@tonic-gate 		if (event->ctev_nbytes <= event_nbytes)
4377c478bd9Sstevel@tonic-gate 			break;
4387c478bd9Sstevel@tonic-gate 
4397c478bd9Sstevel@tonic-gate 		if (event_buffer)
4407c478bd9Sstevel@tonic-gate 			free(event_buffer);
4417c478bd9Sstevel@tonic-gate 		event_nbytes = event->ctev_nbytes;
4427c478bd9Sstevel@tonic-gate 		event_buffer = malloc(event_nbytes);
4437c478bd9Sstevel@tonic-gate 		if (event_buffer == NULL) {
4447c478bd9Sstevel@tonic-gate 			error = errno;
4457c478bd9Sstevel@tonic-gate 			goto errout;
4467c478bd9Sstevel@tonic-gate 		}
4477c478bd9Sstevel@tonic-gate 	}
4487c478bd9Sstevel@tonic-gate 
4497c478bd9Sstevel@tonic-gate 	if (event->ctev_goffset > 0 && (error = unpack_and_merge(&info->nvl,
4507c478bd9Sstevel@tonic-gate 	    event->ctev_buffer, event->ctev_goffset)) != 0)
4517c478bd9Sstevel@tonic-gate 		goto errout;
4527c478bd9Sstevel@tonic-gate 
4537c478bd9Sstevel@tonic-gate 	if (event->ctev_goffset < event->ctev_nbytes &&
4547c478bd9Sstevel@tonic-gate 	    (error = unpack_and_merge(&info->nvl,
4557c478bd9Sstevel@tonic-gate 	    event->ctev_buffer + event->ctev_goffset,
4567c478bd9Sstevel@tonic-gate 	    event->ctev_nbytes - event->ctev_goffset)) != 0)
4577c478bd9Sstevel@tonic-gate 		goto errout;
4587c478bd9Sstevel@tonic-gate 
4597c478bd9Sstevel@tonic-gate 	free(event_buffer);
4607c478bd9Sstevel@tonic-gate 
4617c478bd9Sstevel@tonic-gate 	*evt = info;
4627c478bd9Sstevel@tonic-gate 	return (0);
4637c478bd9Sstevel@tonic-gate 
4647c478bd9Sstevel@tonic-gate errout:
4657c478bd9Sstevel@tonic-gate 	if (event_buffer)
4667c478bd9Sstevel@tonic-gate 		free(event_buffer);
4677c478bd9Sstevel@tonic-gate 	if (info) {
468*aab83bb8SJosef 'Jeff' Sipek 		nvlist_free(info->nvl);
4697c478bd9Sstevel@tonic-gate 		free(info);
4707c478bd9Sstevel@tonic-gate 	}
4717c478bd9Sstevel@tonic-gate 	return (error);
4727c478bd9Sstevel@tonic-gate }
4737c478bd9Sstevel@tonic-gate 
4747c478bd9Sstevel@tonic-gate int
ct_event_read(int fd,ct_evthdl_t * evthdl)4757c478bd9Sstevel@tonic-gate ct_event_read(int fd, ct_evthdl_t *evthdl)
4767c478bd9Sstevel@tonic-gate {
4777c478bd9Sstevel@tonic-gate 	return (ct_event_read_internal(fd, CT_ERECV, evthdl));
4787c478bd9Sstevel@tonic-gate }
4797c478bd9Sstevel@tonic-gate 
4807c478bd9Sstevel@tonic-gate int
ct_event_read_critical(int fd,ct_evthdl_t * evthdl)4817c478bd9Sstevel@tonic-gate ct_event_read_critical(int fd, ct_evthdl_t *evthdl)
4827c478bd9Sstevel@tonic-gate {
4837c478bd9Sstevel@tonic-gate 	return (ct_event_read_internal(fd, CT_ECRECV, evthdl));
4847c478bd9Sstevel@tonic-gate }
4857c478bd9Sstevel@tonic-gate 
4867c478bd9Sstevel@tonic-gate int
ct_event_reset(int fd)4877c478bd9Sstevel@tonic-gate ct_event_reset(int fd)
4887c478bd9Sstevel@tonic-gate {
4897c478bd9Sstevel@tonic-gate 	if (ioctl(fd, CT_ERESET) == -1)
4907c478bd9Sstevel@tonic-gate 		return (errno);
4917c478bd9Sstevel@tonic-gate 	return (0);
4927c478bd9Sstevel@tonic-gate }
4937c478bd9Sstevel@tonic-gate 
4947c478bd9Sstevel@tonic-gate int
ct_event_reliable(int fd)4957c478bd9Sstevel@tonic-gate ct_event_reliable(int fd)
4967c478bd9Sstevel@tonic-gate {
4977c478bd9Sstevel@tonic-gate 	if (ioctl(fd, CT_ERELIABLE) == -1)
4987c478bd9Sstevel@tonic-gate 		return (errno);
4997c478bd9Sstevel@tonic-gate 	return (0);
5007c478bd9Sstevel@tonic-gate }
5017c478bd9Sstevel@tonic-gate 
5027c478bd9Sstevel@tonic-gate void
ct_event_free(ct_evthdl_t evthdl)5037c478bd9Sstevel@tonic-gate ct_event_free(ct_evthdl_t evthdl)
5047c478bd9Sstevel@tonic-gate {
5057c478bd9Sstevel@tonic-gate 	struct ctlib_event_info *info = evthdl;
5067c478bd9Sstevel@tonic-gate 
507*aab83bb8SJosef 'Jeff' Sipek 	nvlist_free(info->nvl);
5087c478bd9Sstevel@tonic-gate 	free(info);
5097c478bd9Sstevel@tonic-gate }
5107c478bd9Sstevel@tonic-gate 
5117c478bd9Sstevel@tonic-gate 
5127c478bd9Sstevel@tonic-gate uint_t
ct_event_get_flags(ct_evthdl_t evthdl)5137c478bd9Sstevel@tonic-gate ct_event_get_flags(ct_evthdl_t evthdl)
5147c478bd9Sstevel@tonic-gate {
5157c478bd9Sstevel@tonic-gate 	struct ctlib_event_info *info = evthdl;
5167c478bd9Sstevel@tonic-gate 	return (info->event.ctev_flags);
5177c478bd9Sstevel@tonic-gate }
5187c478bd9Sstevel@tonic-gate 
5197c478bd9Sstevel@tonic-gate ctid_t
ct_event_get_ctid(ct_evthdl_t evthdl)5207c478bd9Sstevel@tonic-gate ct_event_get_ctid(ct_evthdl_t evthdl)
5217c478bd9Sstevel@tonic-gate {
5227c478bd9Sstevel@tonic-gate 	struct ctlib_event_info *info = evthdl;
5237c478bd9Sstevel@tonic-gate 	return (info->event.ctev_id);
5247c478bd9Sstevel@tonic-gate }
5257c478bd9Sstevel@tonic-gate 
5267c478bd9Sstevel@tonic-gate ctevid_t
ct_event_get_evid(ct_evthdl_t evthdl)5277c478bd9Sstevel@tonic-gate ct_event_get_evid(ct_evthdl_t evthdl)
5287c478bd9Sstevel@tonic-gate {
5297c478bd9Sstevel@tonic-gate 	struct ctlib_event_info *info = evthdl;
5307c478bd9Sstevel@tonic-gate 	return (info->event.ctev_evid);
5317c478bd9Sstevel@tonic-gate }
5327c478bd9Sstevel@tonic-gate 
5337c478bd9Sstevel@tonic-gate uint_t
ct_event_get_type(ct_evthdl_t evthdl)5347c478bd9Sstevel@tonic-gate ct_event_get_type(ct_evthdl_t evthdl)
5357c478bd9Sstevel@tonic-gate {
5367c478bd9Sstevel@tonic-gate 	struct ctlib_event_info *info = evthdl;
5377c478bd9Sstevel@tonic-gate 	return (info->event.ctev_type);
5387c478bd9Sstevel@tonic-gate }
5397c478bd9Sstevel@tonic-gate 
5407c478bd9Sstevel@tonic-gate int
ct_event_get_nevid(ct_evthdl_t evthdl,ctevid_t * evidp)5417c478bd9Sstevel@tonic-gate ct_event_get_nevid(ct_evthdl_t evthdl, ctevid_t *evidp)
5427c478bd9Sstevel@tonic-gate {
5437c478bd9Sstevel@tonic-gate 	struct ctlib_event_info *info = evthdl;
5447c478bd9Sstevel@tonic-gate 	if (info->nvl == NULL ||
5457c478bd9Sstevel@tonic-gate 	    nvlist_lookup_uint64(info->nvl, CTS_NEVID, evidp))
5467c478bd9Sstevel@tonic-gate 		return (EINVAL);
5477c478bd9Sstevel@tonic-gate 	return (0);
5487c478bd9Sstevel@tonic-gate }
5497c478bd9Sstevel@tonic-gate 
5507c478bd9Sstevel@tonic-gate int
ct_event_get_newct(ct_evthdl_t evthdl,ctid_t * ctidp)5517c478bd9Sstevel@tonic-gate ct_event_get_newct(ct_evthdl_t evthdl, ctid_t *ctidp)
5527c478bd9Sstevel@tonic-gate {
5537c478bd9Sstevel@tonic-gate 	struct ctlib_event_info *info = evthdl;
5547c478bd9Sstevel@tonic-gate 	if (info->nvl == NULL ||
5557c478bd9Sstevel@tonic-gate 	    nvlist_lookup_int32(info->nvl, CTS_NEWCT, (int *)ctidp))
5567c478bd9Sstevel@tonic-gate 		return (EINVAL);
5577c478bd9Sstevel@tonic-gate 	return (0);
5587c478bd9Sstevel@tonic-gate }
559