xref: /titanic_53/usr/src/lib/libcontract/common/libcontract.c (revision 7b209c2cc5ea45251aba06dcc6181d3f23da807a)
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 /*
22*7b209c2cSacruz  * 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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <sys/ctfs.h>
297c478bd9Sstevel@tonic-gate #include <sys/contract.h>
30*7b209c2cSacruz #include <string.h>
317c478bd9Sstevel@tonic-gate #include <libnvpair.h>
327c478bd9Sstevel@tonic-gate #include <assert.h>
337c478bd9Sstevel@tonic-gate #include <unistd.h>
347c478bd9Sstevel@tonic-gate #include <errno.h>
357c478bd9Sstevel@tonic-gate #include <libcontract.h>
367c478bd9Sstevel@tonic-gate #include "libcontract_impl.h"
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate /*
397c478bd9Sstevel@tonic-gate  * Common template routines
407c478bd9Sstevel@tonic-gate  */
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate int
437c478bd9Sstevel@tonic-gate ct_tmpl_activate(int fd)
447c478bd9Sstevel@tonic-gate {
457c478bd9Sstevel@tonic-gate 	if (ioctl(fd, CT_TACTIVATE) == -1)
467c478bd9Sstevel@tonic-gate 		return (errno);
477c478bd9Sstevel@tonic-gate 	return (0);
487c478bd9Sstevel@tonic-gate }
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate int
517c478bd9Sstevel@tonic-gate ct_tmpl_clear(int fd)
527c478bd9Sstevel@tonic-gate {
537c478bd9Sstevel@tonic-gate 	if (ioctl(fd, CT_TCLEAR) == -1)
547c478bd9Sstevel@tonic-gate 		return (errno);
557c478bd9Sstevel@tonic-gate 	return (0);
567c478bd9Sstevel@tonic-gate }
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate int
597c478bd9Sstevel@tonic-gate ct_tmpl_create(int fd, ctid_t *ctidp)
607c478bd9Sstevel@tonic-gate {
617c478bd9Sstevel@tonic-gate 	ctid_t ctid = ioctl(fd, CT_TCREATE);
627c478bd9Sstevel@tonic-gate 	if (ctid == -1)
637c478bd9Sstevel@tonic-gate 		return (errno);
647c478bd9Sstevel@tonic-gate 	*ctidp = ctid;
657c478bd9Sstevel@tonic-gate 	return (0);
667c478bd9Sstevel@tonic-gate }
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate int
6925e8c5aaSvikram ct_tmpl_set_internal(int fd, uint_t id, uintptr_t value)
707c478bd9Sstevel@tonic-gate {
717c478bd9Sstevel@tonic-gate 	ct_param_t param;
72*7b209c2cSacruz 	uint64_t param_value = value;
73*7b209c2cSacruz 
747c478bd9Sstevel@tonic-gate 	param.ctpm_id = id;
75*7b209c2cSacruz 	param.ctpm_size = sizeof (uint64_t);
76*7b209c2cSacruz 	param.ctpm_value = &param_value;
777c478bd9Sstevel@tonic-gate 	if (ioctl(fd, CT_TSET, &param) == -1)
787c478bd9Sstevel@tonic-gate 		return (errno);
79*7b209c2cSacruz 
80*7b209c2cSacruz 	return (0);
81*7b209c2cSacruz }
82*7b209c2cSacruz 
83*7b209c2cSacruz int
84*7b209c2cSacruz ct_tmpl_set_internal_string(int fd, uint_t id, const char *value)
85*7b209c2cSacruz {
86*7b209c2cSacruz 	ct_param_t param;
87*7b209c2cSacruz 
88*7b209c2cSacruz 	if (value == NULL)
89*7b209c2cSacruz 		return (EINVAL);
90*7b209c2cSacruz 	param.ctpm_id = id;
91*7b209c2cSacruz 	param.ctpm_size = strlen(value) + 1;
92*7b209c2cSacruz 	param.ctpm_value = (void *)value;
93*7b209c2cSacruz 	if (ioctl(fd, CT_TSET, &param) == -1)
94*7b209c2cSacruz 		return (errno);
95*7b209c2cSacruz 
967c478bd9Sstevel@tonic-gate 	return (0);
977c478bd9Sstevel@tonic-gate }
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate int
1007c478bd9Sstevel@tonic-gate ct_tmpl_set_critical(int fd, uint_t events)
1017c478bd9Sstevel@tonic-gate {
1027c478bd9Sstevel@tonic-gate 	return (ct_tmpl_set_internal(fd, CTP_EV_CRITICAL, events));
1037c478bd9Sstevel@tonic-gate }
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate int
1067c478bd9Sstevel@tonic-gate ct_tmpl_set_informative(int fd, uint_t events)
1077c478bd9Sstevel@tonic-gate {
1087c478bd9Sstevel@tonic-gate 	return (ct_tmpl_set_internal(fd, CTP_EV_INFO, events));
1097c478bd9Sstevel@tonic-gate }
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate int
1127c478bd9Sstevel@tonic-gate ct_tmpl_set_cookie(int fd, uint64_t cookie)
1137c478bd9Sstevel@tonic-gate {
1147c478bd9Sstevel@tonic-gate 	ct_param_t param;
115*7b209c2cSacruz 	uint64_t param_value = cookie;
116*7b209c2cSacruz 
1177c478bd9Sstevel@tonic-gate 	param.ctpm_id = CTP_COOKIE;
118*7b209c2cSacruz 	param.ctpm_size = sizeof (uint64_t);
119*7b209c2cSacruz 	param.ctpm_value = &param_value;
1207c478bd9Sstevel@tonic-gate 	if (ioctl(fd, CT_TSET, &param) == -1)
1217c478bd9Sstevel@tonic-gate 		return (errno);
1227c478bd9Sstevel@tonic-gate 	return (0);
1237c478bd9Sstevel@tonic-gate }
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate int
1267c478bd9Sstevel@tonic-gate ct_tmpl_get_internal(int fd, uint_t id, uint_t *value)
1277c478bd9Sstevel@tonic-gate {
1287c478bd9Sstevel@tonic-gate 	ct_param_t param;
129*7b209c2cSacruz 	uint64_t param_value;
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate 	param.ctpm_id = id;
132*7b209c2cSacruz 	param.ctpm_size = sizeof (uint64_t);
133*7b209c2cSacruz 	param.ctpm_value = &param_value;
1347c478bd9Sstevel@tonic-gate 	if (ioctl(fd, CT_TGET, &param) == -1)
1357c478bd9Sstevel@tonic-gate 		return (errno);
136*7b209c2cSacruz 	*value = param_value;
1377c478bd9Sstevel@tonic-gate 	return (0);
1387c478bd9Sstevel@tonic-gate }
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate int
141*7b209c2cSacruz ct_tmpl_get_internal_string(int fd, uint32_t id, char *buf, size_t size)
14225e8c5aaSvikram {
14325e8c5aaSvikram 	ct_param_t param;
14425e8c5aaSvikram 
14525e8c5aaSvikram 	param.ctpm_id = id;
146*7b209c2cSacruz 	param.ctpm_size = size;
147*7b209c2cSacruz 	param.ctpm_value = buf;
14825e8c5aaSvikram 	if (ioctl(fd, CT_TGET, &param) == -1)
149*7b209c2cSacruz 		return (-1);
150*7b209c2cSacruz 	return (param.ctpm_size);
15125e8c5aaSvikram }
15225e8c5aaSvikram 
15325e8c5aaSvikram int
1547c478bd9Sstevel@tonic-gate ct_tmpl_get_critical(int fd, uint_t *events)
1557c478bd9Sstevel@tonic-gate {
1567c478bd9Sstevel@tonic-gate 	return (ct_tmpl_get_internal(fd, CTP_EV_CRITICAL, events));
1577c478bd9Sstevel@tonic-gate }
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate int
1607c478bd9Sstevel@tonic-gate ct_tmpl_get_informative(int fd, uint_t *events)
1617c478bd9Sstevel@tonic-gate {
1627c478bd9Sstevel@tonic-gate 	return (ct_tmpl_get_internal(fd, CTP_EV_INFO, events));
1637c478bd9Sstevel@tonic-gate }
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate int
1667c478bd9Sstevel@tonic-gate ct_tmpl_get_cookie(int fd, uint64_t *cookie)
1677c478bd9Sstevel@tonic-gate {
1687c478bd9Sstevel@tonic-gate 	ct_param_t param;
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate 	param.ctpm_id = CTP_COOKIE;
171*7b209c2cSacruz 	param.ctpm_size = sizeof (uint64_t);
172*7b209c2cSacruz 	param.ctpm_value = cookie;
1737c478bd9Sstevel@tonic-gate 	if (ioctl(fd, CT_TGET, &param) == -1)
1747c478bd9Sstevel@tonic-gate 		return (errno);
1757c478bd9Sstevel@tonic-gate 	return (0);
1767c478bd9Sstevel@tonic-gate }
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate /*
1797c478bd9Sstevel@tonic-gate  * Common ctl routines
1807c478bd9Sstevel@tonic-gate  */
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate int
1837c478bd9Sstevel@tonic-gate ct_ctl_adopt(int fd)
1847c478bd9Sstevel@tonic-gate {
1857c478bd9Sstevel@tonic-gate 	if (ioctl(fd, CT_CADOPT) == -1)
1867c478bd9Sstevel@tonic-gate 		return (errno);
1877c478bd9Sstevel@tonic-gate 	return (0);
1887c478bd9Sstevel@tonic-gate }
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate int
1917c478bd9Sstevel@tonic-gate ct_ctl_abandon(int fd)
1927c478bd9Sstevel@tonic-gate {
1937c478bd9Sstevel@tonic-gate 	if (ioctl(fd, CT_CABANDON) == -1)
1947c478bd9Sstevel@tonic-gate 		return (errno);
1957c478bd9Sstevel@tonic-gate 	return (0);
1967c478bd9Sstevel@tonic-gate }
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1997c478bd9Sstevel@tonic-gate int
2007c478bd9Sstevel@tonic-gate ct_ctl_newct(int cfd, ctevid_t evid, int tfd)
2017c478bd9Sstevel@tonic-gate {
2027c478bd9Sstevel@tonic-gate 	if (ioctl(cfd, CT_CNEWCT, tfd) == -1)
2037c478bd9Sstevel@tonic-gate 		return (errno);
2047c478bd9Sstevel@tonic-gate 	return (0);
2057c478bd9Sstevel@tonic-gate }
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate int
2087c478bd9Sstevel@tonic-gate ct_ctl_ack(int fd, ctevid_t event)
2097c478bd9Sstevel@tonic-gate {
2107c478bd9Sstevel@tonic-gate 	if (ioctl(fd, CT_CACK, &event) == -1)
2117c478bd9Sstevel@tonic-gate 		return (errno);
2127c478bd9Sstevel@tonic-gate 	return (0);
2137c478bd9Sstevel@tonic-gate }
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate int
21625e8c5aaSvikram ct_ctl_nack(int fd, ctevid_t event)
21725e8c5aaSvikram {
21825e8c5aaSvikram 	if (ioctl(fd, CT_CNACK, &event) == -1)
21925e8c5aaSvikram 		return (errno);
22025e8c5aaSvikram 	return (0);
22125e8c5aaSvikram }
22225e8c5aaSvikram 
22325e8c5aaSvikram int
2247c478bd9Sstevel@tonic-gate ct_ctl_qack(int fd, ctevid_t event)
2257c478bd9Sstevel@tonic-gate {
2267c478bd9Sstevel@tonic-gate 	if (ioctl(fd, CT_CQREQ, &event) == -1)
2277c478bd9Sstevel@tonic-gate 		return (errno);
2287c478bd9Sstevel@tonic-gate 	return (0);
2297c478bd9Sstevel@tonic-gate }
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate /*
2327c478bd9Sstevel@tonic-gate  * Common status routines
2337c478bd9Sstevel@tonic-gate  */
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate int
2367c478bd9Sstevel@tonic-gate ct_status_read(int fd, int detail, ct_stathdl_t *stathdl)
2377c478bd9Sstevel@tonic-gate {
2387c478bd9Sstevel@tonic-gate 	char *status_buffer = NULL;
2397c478bd9Sstevel@tonic-gate 	int status_nbytes = 0;
2407c478bd9Sstevel@tonic-gate 	struct ctlib_status_info *info;
2417c478bd9Sstevel@tonic-gate 	int error;
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate 	info = malloc(sizeof (struct ctlib_status_info));
2447c478bd9Sstevel@tonic-gate 	if (info == NULL)
2457c478bd9Sstevel@tonic-gate 		return (errno);
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate 	info->status.ctst_detail = detail;
2487c478bd9Sstevel@tonic-gate 	if (detail != CTD_COMMON) {
2497c478bd9Sstevel@tonic-gate 		for (;;) {
2507c478bd9Sstevel@tonic-gate 			info->status.ctst_nbytes = status_nbytes;
2517c478bd9Sstevel@tonic-gate 			info->status.ctst_buffer = status_buffer;
2527c478bd9Sstevel@tonic-gate 			do
2537c478bd9Sstevel@tonic-gate 				error = ioctl(fd, CT_SSTATUS, &info->status);
2547c478bd9Sstevel@tonic-gate 			while (error == -1 && errno == EINTR);
2557c478bd9Sstevel@tonic-gate 			if (error == -1)
2567c478bd9Sstevel@tonic-gate 				goto errout;
2577c478bd9Sstevel@tonic-gate 			if (info->status.ctst_nbytes <= status_nbytes)
2587c478bd9Sstevel@tonic-gate 				break;
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate 			if (status_buffer)
2617c478bd9Sstevel@tonic-gate 				free(status_buffer);
2627c478bd9Sstevel@tonic-gate 			status_nbytes = info->status.ctst_nbytes;
2637c478bd9Sstevel@tonic-gate 			status_buffer = malloc(status_nbytes);
2647c478bd9Sstevel@tonic-gate 			if (status_buffer == NULL)
2657c478bd9Sstevel@tonic-gate 				goto errout;
2667c478bd9Sstevel@tonic-gate 		}
2677c478bd9Sstevel@tonic-gate 		if ((errno = nvlist_unpack(info->status.ctst_buffer,
2687c478bd9Sstevel@tonic-gate 		    info->status.ctst_nbytes, &info->nvl, 0)) != 0)
2697c478bd9Sstevel@tonic-gate 			goto errout;
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate 		free(status_buffer);
2727c478bd9Sstevel@tonic-gate 		status_buffer = NULL;
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate 	} else {
2757c478bd9Sstevel@tonic-gate 		info->status.ctst_nbytes = 0;
2767c478bd9Sstevel@tonic-gate 		info->nvl = NULL;
2777c478bd9Sstevel@tonic-gate 		if (ioctl(fd, CT_SSTATUS, &info->status) == -1)
2787c478bd9Sstevel@tonic-gate 			goto errout;
2797c478bd9Sstevel@tonic-gate 	}
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate 	*stathdl = info;
2827c478bd9Sstevel@tonic-gate 	return (0);
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate errout:
2857c478bd9Sstevel@tonic-gate 	error = errno;
2867c478bd9Sstevel@tonic-gate 	if (status_buffer)
2877c478bd9Sstevel@tonic-gate 		free(status_buffer);
2887c478bd9Sstevel@tonic-gate 	if (info)
2897c478bd9Sstevel@tonic-gate 		free(info);
2907c478bd9Sstevel@tonic-gate 	return (error);
2917c478bd9Sstevel@tonic-gate }
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate void
2947c478bd9Sstevel@tonic-gate ct_status_free(ct_stathdl_t stathdl)
2957c478bd9Sstevel@tonic-gate {
2967c478bd9Sstevel@tonic-gate 	struct ctlib_status_info *info = stathdl;
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate 	if (info->nvl) {
2997c478bd9Sstevel@tonic-gate 		assert(info->status.ctst_detail != CTD_COMMON);
3007c478bd9Sstevel@tonic-gate 		nvlist_free(info->nvl);
3017c478bd9Sstevel@tonic-gate 	}
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate 	free(info);
3047c478bd9Sstevel@tonic-gate }
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate ctid_t
3077c478bd9Sstevel@tonic-gate ct_status_get_id(ct_stathdl_t stathdl)
3087c478bd9Sstevel@tonic-gate {
3097c478bd9Sstevel@tonic-gate 	struct ctlib_status_info *info = stathdl;
3107c478bd9Sstevel@tonic-gate 	return (info->status.ctst_id);
3117c478bd9Sstevel@tonic-gate }
3127c478bd9Sstevel@tonic-gate 
3137c478bd9Sstevel@tonic-gate zoneid_t
3147c478bd9Sstevel@tonic-gate ct_status_get_zoneid(ct_stathdl_t stathdl)
3157c478bd9Sstevel@tonic-gate {
3167c478bd9Sstevel@tonic-gate 	struct ctlib_status_info *info = stathdl;
3177c478bd9Sstevel@tonic-gate 	return (info->status.ctst_zoneid);
3187c478bd9Sstevel@tonic-gate }
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate const char *
3217c478bd9Sstevel@tonic-gate ct_status_get_type(ct_stathdl_t stathdl)
3227c478bd9Sstevel@tonic-gate {
3237c478bd9Sstevel@tonic-gate 	struct ctlib_status_info *info = stathdl;
3247c478bd9Sstevel@tonic-gate 	return (types[info->status.ctst_type].type_name);
3257c478bd9Sstevel@tonic-gate }
3267c478bd9Sstevel@tonic-gate 
3277c478bd9Sstevel@tonic-gate id_t
3287c478bd9Sstevel@tonic-gate ct_status_get_holder(ct_stathdl_t stathdl)
3297c478bd9Sstevel@tonic-gate {
3307c478bd9Sstevel@tonic-gate 	struct ctlib_status_info *info = stathdl;
3317c478bd9Sstevel@tonic-gate 	return (info->status.ctst_holder);
3327c478bd9Sstevel@tonic-gate }
3337c478bd9Sstevel@tonic-gate 
3347c478bd9Sstevel@tonic-gate ctstate_t
3357c478bd9Sstevel@tonic-gate ct_status_get_state(ct_stathdl_t stathdl)
3367c478bd9Sstevel@tonic-gate {
3377c478bd9Sstevel@tonic-gate 	struct ctlib_status_info *info = stathdl;
3387c478bd9Sstevel@tonic-gate 	return (info->status.ctst_state);
3397c478bd9Sstevel@tonic-gate }
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate int
3427c478bd9Sstevel@tonic-gate ct_status_get_nevents(ct_stathdl_t stathdl)
3437c478bd9Sstevel@tonic-gate {
3447c478bd9Sstevel@tonic-gate 	struct ctlib_status_info *info = stathdl;
3457c478bd9Sstevel@tonic-gate 	return (info->status.ctst_nevents);
3467c478bd9Sstevel@tonic-gate }
3477c478bd9Sstevel@tonic-gate 
3487c478bd9Sstevel@tonic-gate int
3497c478bd9Sstevel@tonic-gate ct_status_get_ntime(ct_stathdl_t stathdl)
3507c478bd9Sstevel@tonic-gate {
3517c478bd9Sstevel@tonic-gate 	struct ctlib_status_info *info = stathdl;
3527c478bd9Sstevel@tonic-gate 	return (info->status.ctst_ntime);
3537c478bd9Sstevel@tonic-gate }
3547c478bd9Sstevel@tonic-gate 
3557c478bd9Sstevel@tonic-gate int
3567c478bd9Sstevel@tonic-gate ct_status_get_qtime(ct_stathdl_t stathdl)
3577c478bd9Sstevel@tonic-gate {
3587c478bd9Sstevel@tonic-gate 	struct ctlib_status_info *info = stathdl;
3597c478bd9Sstevel@tonic-gate 	return (info->status.ctst_qtime);
3607c478bd9Sstevel@tonic-gate }
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate ctevid_t
3637c478bd9Sstevel@tonic-gate ct_status_get_nevid(ct_stathdl_t stathdl)
3647c478bd9Sstevel@tonic-gate {
3657c478bd9Sstevel@tonic-gate 	struct ctlib_status_info *info = stathdl;
3667c478bd9Sstevel@tonic-gate 	return (info->status.ctst_nevid);
3677c478bd9Sstevel@tonic-gate }
3687c478bd9Sstevel@tonic-gate 
3697c478bd9Sstevel@tonic-gate uint_t
3707c478bd9Sstevel@tonic-gate ct_status_get_informative(ct_stathdl_t stathdl)
3717c478bd9Sstevel@tonic-gate {
3727c478bd9Sstevel@tonic-gate 	struct ctlib_status_info *info = stathdl;
3737c478bd9Sstevel@tonic-gate 	return (info->status.ctst_informative);
3747c478bd9Sstevel@tonic-gate }
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate uint_t
3777c478bd9Sstevel@tonic-gate ct_status_get_critical(ct_stathdl_t stathdl)
3787c478bd9Sstevel@tonic-gate {
3797c478bd9Sstevel@tonic-gate 	struct ctlib_status_info *info = stathdl;
3807c478bd9Sstevel@tonic-gate 	return (info->status.ctst_critical);
3817c478bd9Sstevel@tonic-gate }
3827c478bd9Sstevel@tonic-gate 
3837c478bd9Sstevel@tonic-gate uint64_t
3847c478bd9Sstevel@tonic-gate ct_status_get_cookie(ct_stathdl_t stathdl)
3857c478bd9Sstevel@tonic-gate {
3867c478bd9Sstevel@tonic-gate 	struct ctlib_status_info *info = stathdl;
3877c478bd9Sstevel@tonic-gate 	return (info->status.ctst_cookie);
3887c478bd9Sstevel@tonic-gate }
3897c478bd9Sstevel@tonic-gate 
3907c478bd9Sstevel@tonic-gate /*
3917c478bd9Sstevel@tonic-gate  * Common event routines
3927c478bd9Sstevel@tonic-gate  */
3937c478bd9Sstevel@tonic-gate 
3947c478bd9Sstevel@tonic-gate static int
3957c478bd9Sstevel@tonic-gate unpack_and_merge(nvlist_t **nvl, char *buffer, size_t len)
3967c478bd9Sstevel@tonic-gate {
3977c478bd9Sstevel@tonic-gate 	nvlist_t *tmpnvl;
3987c478bd9Sstevel@tonic-gate 	int error;
3997c478bd9Sstevel@tonic-gate 
4007c478bd9Sstevel@tonic-gate 	if ((error = nvlist_unpack(buffer, len, &tmpnvl, 0)) != 0)
4017c478bd9Sstevel@tonic-gate 		return (error);
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate 	if (*nvl == NULL) {
4047c478bd9Sstevel@tonic-gate 		*nvl = tmpnvl;
4057c478bd9Sstevel@tonic-gate 		return (0);
4067c478bd9Sstevel@tonic-gate 	}
4077c478bd9Sstevel@tonic-gate 
4087c478bd9Sstevel@tonic-gate 	error = nvlist_merge(*nvl, tmpnvl, 0);
4097c478bd9Sstevel@tonic-gate 	nvlist_free(tmpnvl);
4107c478bd9Sstevel@tonic-gate 	return (error);
4117c478bd9Sstevel@tonic-gate }
4127c478bd9Sstevel@tonic-gate 
4137c478bd9Sstevel@tonic-gate static int
4147c478bd9Sstevel@tonic-gate ct_event_read_internal(int fd, int cmd, ct_evthdl_t *evt)
4157c478bd9Sstevel@tonic-gate {
4167c478bd9Sstevel@tonic-gate 	char *event_buffer = NULL;
4177c478bd9Sstevel@tonic-gate 	int event_nbytes = 0;
4187c478bd9Sstevel@tonic-gate 	struct ctlib_event_info *info;
4197c478bd9Sstevel@tonic-gate 	ct_event_t *event;
4207c478bd9Sstevel@tonic-gate 	int error;
4217c478bd9Sstevel@tonic-gate 
4227c478bd9Sstevel@tonic-gate 	info = malloc(sizeof (struct ctlib_event_info));
4237c478bd9Sstevel@tonic-gate 	if (info == NULL)
4247c478bd9Sstevel@tonic-gate 		return (errno);
4257c478bd9Sstevel@tonic-gate 	info->nvl = NULL;
4267c478bd9Sstevel@tonic-gate 	event = &info->event;
4277c478bd9Sstevel@tonic-gate 
4287c478bd9Sstevel@tonic-gate 	for (;;) {
4297c478bd9Sstevel@tonic-gate 		event->ctev_nbytes = event_nbytes;
4307c478bd9Sstevel@tonic-gate 		event->ctev_buffer = event_buffer;
4317c478bd9Sstevel@tonic-gate 		do
4327c478bd9Sstevel@tonic-gate 			error = ioctl(fd, cmd, event);
4337c478bd9Sstevel@tonic-gate 		while (error == -1 && errno == EINTR);
4347c478bd9Sstevel@tonic-gate 		if (error == -1) {
4357c478bd9Sstevel@tonic-gate 			error = errno;
4367c478bd9Sstevel@tonic-gate 			goto errout;
4377c478bd9Sstevel@tonic-gate 		}
4387c478bd9Sstevel@tonic-gate 		if (event->ctev_nbytes <= event_nbytes)
4397c478bd9Sstevel@tonic-gate 			break;
4407c478bd9Sstevel@tonic-gate 
4417c478bd9Sstevel@tonic-gate 		if (event_buffer)
4427c478bd9Sstevel@tonic-gate 			free(event_buffer);
4437c478bd9Sstevel@tonic-gate 		event_nbytes = event->ctev_nbytes;
4447c478bd9Sstevel@tonic-gate 		event_buffer = malloc(event_nbytes);
4457c478bd9Sstevel@tonic-gate 		if (event_buffer == NULL) {
4467c478bd9Sstevel@tonic-gate 			error = errno;
4477c478bd9Sstevel@tonic-gate 			goto errout;
4487c478bd9Sstevel@tonic-gate 		}
4497c478bd9Sstevel@tonic-gate 	}
4507c478bd9Sstevel@tonic-gate 
4517c478bd9Sstevel@tonic-gate 	if (event->ctev_goffset > 0 && (error = unpack_and_merge(&info->nvl,
4527c478bd9Sstevel@tonic-gate 	    event->ctev_buffer, event->ctev_goffset)) != 0)
4537c478bd9Sstevel@tonic-gate 		goto errout;
4547c478bd9Sstevel@tonic-gate 
4557c478bd9Sstevel@tonic-gate 	if (event->ctev_goffset < event->ctev_nbytes &&
4567c478bd9Sstevel@tonic-gate 	    (error = unpack_and_merge(&info->nvl,
4577c478bd9Sstevel@tonic-gate 	    event->ctev_buffer + event->ctev_goffset,
4587c478bd9Sstevel@tonic-gate 	    event->ctev_nbytes - event->ctev_goffset)) != 0)
4597c478bd9Sstevel@tonic-gate 		goto errout;
4607c478bd9Sstevel@tonic-gate 
4617c478bd9Sstevel@tonic-gate 	free(event_buffer);
4627c478bd9Sstevel@tonic-gate 
4637c478bd9Sstevel@tonic-gate 	*evt = info;
4647c478bd9Sstevel@tonic-gate 	return (0);
4657c478bd9Sstevel@tonic-gate 
4667c478bd9Sstevel@tonic-gate errout:
4677c478bd9Sstevel@tonic-gate 	if (event_buffer)
4687c478bd9Sstevel@tonic-gate 		free(event_buffer);
4697c478bd9Sstevel@tonic-gate 	if (info) {
4707c478bd9Sstevel@tonic-gate 		if (info->nvl)
4717c478bd9Sstevel@tonic-gate 			nvlist_free(info->nvl);
4727c478bd9Sstevel@tonic-gate 		free(info);
4737c478bd9Sstevel@tonic-gate 	}
4747c478bd9Sstevel@tonic-gate 	return (error);
4757c478bd9Sstevel@tonic-gate }
4767c478bd9Sstevel@tonic-gate 
4777c478bd9Sstevel@tonic-gate int
4787c478bd9Sstevel@tonic-gate ct_event_read(int fd, ct_evthdl_t *evthdl)
4797c478bd9Sstevel@tonic-gate {
4807c478bd9Sstevel@tonic-gate 	return (ct_event_read_internal(fd, CT_ERECV, evthdl));
4817c478bd9Sstevel@tonic-gate }
4827c478bd9Sstevel@tonic-gate 
4837c478bd9Sstevel@tonic-gate int
4847c478bd9Sstevel@tonic-gate ct_event_read_critical(int fd, ct_evthdl_t *evthdl)
4857c478bd9Sstevel@tonic-gate {
4867c478bd9Sstevel@tonic-gate 	return (ct_event_read_internal(fd, CT_ECRECV, evthdl));
4877c478bd9Sstevel@tonic-gate }
4887c478bd9Sstevel@tonic-gate 
4897c478bd9Sstevel@tonic-gate int
4907c478bd9Sstevel@tonic-gate ct_event_reset(int fd)
4917c478bd9Sstevel@tonic-gate {
4927c478bd9Sstevel@tonic-gate 	if (ioctl(fd, CT_ERESET) == -1)
4937c478bd9Sstevel@tonic-gate 		return (errno);
4947c478bd9Sstevel@tonic-gate 	return (0);
4957c478bd9Sstevel@tonic-gate }
4967c478bd9Sstevel@tonic-gate 
4977c478bd9Sstevel@tonic-gate int
4987c478bd9Sstevel@tonic-gate ct_event_reliable(int fd)
4997c478bd9Sstevel@tonic-gate {
5007c478bd9Sstevel@tonic-gate 	if (ioctl(fd, CT_ERELIABLE) == -1)
5017c478bd9Sstevel@tonic-gate 		return (errno);
5027c478bd9Sstevel@tonic-gate 	return (0);
5037c478bd9Sstevel@tonic-gate }
5047c478bd9Sstevel@tonic-gate 
5057c478bd9Sstevel@tonic-gate void
5067c478bd9Sstevel@tonic-gate ct_event_free(ct_evthdl_t evthdl)
5077c478bd9Sstevel@tonic-gate {
5087c478bd9Sstevel@tonic-gate 	struct ctlib_event_info *info = evthdl;
5097c478bd9Sstevel@tonic-gate 
5107c478bd9Sstevel@tonic-gate 	if (info->nvl)
5117c478bd9Sstevel@tonic-gate 		nvlist_free(info->nvl);
5127c478bd9Sstevel@tonic-gate 	free(info);
5137c478bd9Sstevel@tonic-gate }
5147c478bd9Sstevel@tonic-gate 
5157c478bd9Sstevel@tonic-gate 
5167c478bd9Sstevel@tonic-gate uint_t
5177c478bd9Sstevel@tonic-gate ct_event_get_flags(ct_evthdl_t evthdl)
5187c478bd9Sstevel@tonic-gate {
5197c478bd9Sstevel@tonic-gate 	struct ctlib_event_info *info = evthdl;
5207c478bd9Sstevel@tonic-gate 	return (info->event.ctev_flags);
5217c478bd9Sstevel@tonic-gate }
5227c478bd9Sstevel@tonic-gate 
5237c478bd9Sstevel@tonic-gate ctid_t
5247c478bd9Sstevel@tonic-gate ct_event_get_ctid(ct_evthdl_t evthdl)
5257c478bd9Sstevel@tonic-gate {
5267c478bd9Sstevel@tonic-gate 	struct ctlib_event_info *info = evthdl;
5277c478bd9Sstevel@tonic-gate 	return (info->event.ctev_id);
5287c478bd9Sstevel@tonic-gate }
5297c478bd9Sstevel@tonic-gate 
5307c478bd9Sstevel@tonic-gate ctevid_t
5317c478bd9Sstevel@tonic-gate ct_event_get_evid(ct_evthdl_t evthdl)
5327c478bd9Sstevel@tonic-gate {
5337c478bd9Sstevel@tonic-gate 	struct ctlib_event_info *info = evthdl;
5347c478bd9Sstevel@tonic-gate 	return (info->event.ctev_evid);
5357c478bd9Sstevel@tonic-gate }
5367c478bd9Sstevel@tonic-gate 
5377c478bd9Sstevel@tonic-gate uint_t
5387c478bd9Sstevel@tonic-gate ct_event_get_type(ct_evthdl_t evthdl)
5397c478bd9Sstevel@tonic-gate {
5407c478bd9Sstevel@tonic-gate 	struct ctlib_event_info *info = evthdl;
5417c478bd9Sstevel@tonic-gate 	return (info->event.ctev_type);
5427c478bd9Sstevel@tonic-gate }
5437c478bd9Sstevel@tonic-gate 
5447c478bd9Sstevel@tonic-gate int
5457c478bd9Sstevel@tonic-gate ct_event_get_nevid(ct_evthdl_t evthdl, ctevid_t *evidp)
5467c478bd9Sstevel@tonic-gate {
5477c478bd9Sstevel@tonic-gate 	struct ctlib_event_info *info = evthdl;
5487c478bd9Sstevel@tonic-gate 	if (info->nvl == NULL ||
5497c478bd9Sstevel@tonic-gate 	    nvlist_lookup_uint64(info->nvl, CTS_NEVID, evidp))
5507c478bd9Sstevel@tonic-gate 		return (EINVAL);
5517c478bd9Sstevel@tonic-gate 	return (0);
5527c478bd9Sstevel@tonic-gate }
5537c478bd9Sstevel@tonic-gate 
5547c478bd9Sstevel@tonic-gate int
5557c478bd9Sstevel@tonic-gate ct_event_get_newct(ct_evthdl_t evthdl, ctid_t *ctidp)
5567c478bd9Sstevel@tonic-gate {
5577c478bd9Sstevel@tonic-gate 	struct ctlib_event_info *info = evthdl;
5587c478bd9Sstevel@tonic-gate 	if (info->nvl == NULL ||
5597c478bd9Sstevel@tonic-gate 	    nvlist_lookup_int32(info->nvl, CTS_NEWCT, (int *)ctidp))
5607c478bd9Sstevel@tonic-gate 		return (EINVAL);
5617c478bd9Sstevel@tonic-gate 	return (0);
5627c478bd9Sstevel@tonic-gate }
563