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 5*25e8c5aaSvikram * Common Development and Distribution License (the "License"). 6*25e8c5aaSvikram * 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*25e8c5aaSvikram * Copyright 2007 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> 307c478bd9Sstevel@tonic-gate #include <libnvpair.h> 317c478bd9Sstevel@tonic-gate #include <assert.h> 327c478bd9Sstevel@tonic-gate #include <unistd.h> 337c478bd9Sstevel@tonic-gate #include <errno.h> 347c478bd9Sstevel@tonic-gate #include <libcontract.h> 357c478bd9Sstevel@tonic-gate #include "libcontract_impl.h" 367c478bd9Sstevel@tonic-gate 377c478bd9Sstevel@tonic-gate /* 387c478bd9Sstevel@tonic-gate * Common template routines 397c478bd9Sstevel@tonic-gate */ 407c478bd9Sstevel@tonic-gate 417c478bd9Sstevel@tonic-gate int 427c478bd9Sstevel@tonic-gate ct_tmpl_activate(int fd) 437c478bd9Sstevel@tonic-gate { 447c478bd9Sstevel@tonic-gate if (ioctl(fd, CT_TACTIVATE) == -1) 457c478bd9Sstevel@tonic-gate return (errno); 467c478bd9Sstevel@tonic-gate return (0); 477c478bd9Sstevel@tonic-gate } 487c478bd9Sstevel@tonic-gate 497c478bd9Sstevel@tonic-gate int 507c478bd9Sstevel@tonic-gate ct_tmpl_clear(int fd) 517c478bd9Sstevel@tonic-gate { 527c478bd9Sstevel@tonic-gate if (ioctl(fd, CT_TCLEAR) == -1) 537c478bd9Sstevel@tonic-gate return (errno); 547c478bd9Sstevel@tonic-gate return (0); 557c478bd9Sstevel@tonic-gate } 567c478bd9Sstevel@tonic-gate 577c478bd9Sstevel@tonic-gate int 587c478bd9Sstevel@tonic-gate ct_tmpl_create(int fd, ctid_t *ctidp) 597c478bd9Sstevel@tonic-gate { 607c478bd9Sstevel@tonic-gate ctid_t ctid = ioctl(fd, CT_TCREATE); 617c478bd9Sstevel@tonic-gate if (ctid == -1) 627c478bd9Sstevel@tonic-gate return (errno); 637c478bd9Sstevel@tonic-gate *ctidp = ctid; 647c478bd9Sstevel@tonic-gate return (0); 657c478bd9Sstevel@tonic-gate } 667c478bd9Sstevel@tonic-gate 677c478bd9Sstevel@tonic-gate int 68*25e8c5aaSvikram ct_tmpl_set_internal(int fd, uint_t id, uintptr_t value) 697c478bd9Sstevel@tonic-gate { 707c478bd9Sstevel@tonic-gate ct_param_t param; 717c478bd9Sstevel@tonic-gate param.ctpm_id = id; 72*25e8c5aaSvikram param.ctpm_value = (uint64_t)value; 737c478bd9Sstevel@tonic-gate if (ioctl(fd, CT_TSET, ¶m) == -1) 747c478bd9Sstevel@tonic-gate return (errno); 757c478bd9Sstevel@tonic-gate return (0); 767c478bd9Sstevel@tonic-gate } 777c478bd9Sstevel@tonic-gate 787c478bd9Sstevel@tonic-gate int 797c478bd9Sstevel@tonic-gate ct_tmpl_set_critical(int fd, uint_t events) 807c478bd9Sstevel@tonic-gate { 817c478bd9Sstevel@tonic-gate return (ct_tmpl_set_internal(fd, CTP_EV_CRITICAL, events)); 827c478bd9Sstevel@tonic-gate } 837c478bd9Sstevel@tonic-gate 847c478bd9Sstevel@tonic-gate int 857c478bd9Sstevel@tonic-gate ct_tmpl_set_informative(int fd, uint_t events) 867c478bd9Sstevel@tonic-gate { 877c478bd9Sstevel@tonic-gate return (ct_tmpl_set_internal(fd, CTP_EV_INFO, events)); 887c478bd9Sstevel@tonic-gate } 897c478bd9Sstevel@tonic-gate 907c478bd9Sstevel@tonic-gate int 917c478bd9Sstevel@tonic-gate ct_tmpl_set_cookie(int fd, uint64_t cookie) 927c478bd9Sstevel@tonic-gate { 937c478bd9Sstevel@tonic-gate ct_param_t param; 947c478bd9Sstevel@tonic-gate param.ctpm_id = CTP_COOKIE; 957c478bd9Sstevel@tonic-gate param.ctpm_value = cookie; 967c478bd9Sstevel@tonic-gate if (ioctl(fd, CT_TSET, ¶m) == -1) 977c478bd9Sstevel@tonic-gate return (errno); 987c478bd9Sstevel@tonic-gate return (0); 997c478bd9Sstevel@tonic-gate } 1007c478bd9Sstevel@tonic-gate 1017c478bd9Sstevel@tonic-gate int 1027c478bd9Sstevel@tonic-gate ct_tmpl_get_internal(int fd, uint_t id, uint_t *value) 1037c478bd9Sstevel@tonic-gate { 1047c478bd9Sstevel@tonic-gate ct_param_t param; 1057c478bd9Sstevel@tonic-gate 1067c478bd9Sstevel@tonic-gate param.ctpm_id = id; 1077c478bd9Sstevel@tonic-gate if (ioctl(fd, CT_TGET, ¶m) == -1) 1087c478bd9Sstevel@tonic-gate return (errno); 1097c478bd9Sstevel@tonic-gate *value = param.ctpm_value; 1107c478bd9Sstevel@tonic-gate return (0); 1117c478bd9Sstevel@tonic-gate } 1127c478bd9Sstevel@tonic-gate 1137c478bd9Sstevel@tonic-gate int 114*25e8c5aaSvikram ct_tmpl_get_internal_string(int fd, uint_t id, char *value) 115*25e8c5aaSvikram { 116*25e8c5aaSvikram ct_param_t param; 117*25e8c5aaSvikram 118*25e8c5aaSvikram param.ctpm_id = id; 119*25e8c5aaSvikram param.ctpm_value = (uint64_t)(uintptr_t)value; 120*25e8c5aaSvikram if (ioctl(fd, CT_TGET, ¶m) == -1) 121*25e8c5aaSvikram return (errno); 122*25e8c5aaSvikram return (0); 123*25e8c5aaSvikram } 124*25e8c5aaSvikram 125*25e8c5aaSvikram int 1267c478bd9Sstevel@tonic-gate ct_tmpl_get_critical(int fd, uint_t *events) 1277c478bd9Sstevel@tonic-gate { 1287c478bd9Sstevel@tonic-gate return (ct_tmpl_get_internal(fd, CTP_EV_CRITICAL, events)); 1297c478bd9Sstevel@tonic-gate } 1307c478bd9Sstevel@tonic-gate 1317c478bd9Sstevel@tonic-gate int 1327c478bd9Sstevel@tonic-gate ct_tmpl_get_informative(int fd, uint_t *events) 1337c478bd9Sstevel@tonic-gate { 1347c478bd9Sstevel@tonic-gate return (ct_tmpl_get_internal(fd, CTP_EV_INFO, events)); 1357c478bd9Sstevel@tonic-gate } 1367c478bd9Sstevel@tonic-gate 1377c478bd9Sstevel@tonic-gate int 1387c478bd9Sstevel@tonic-gate ct_tmpl_get_cookie(int fd, uint64_t *cookie) 1397c478bd9Sstevel@tonic-gate { 1407c478bd9Sstevel@tonic-gate ct_param_t param; 1417c478bd9Sstevel@tonic-gate 1427c478bd9Sstevel@tonic-gate param.ctpm_id = CTP_COOKIE; 1437c478bd9Sstevel@tonic-gate if (ioctl(fd, CT_TGET, ¶m) == -1) 1447c478bd9Sstevel@tonic-gate return (errno); 1457c478bd9Sstevel@tonic-gate *cookie = param.ctpm_value; 1467c478bd9Sstevel@tonic-gate return (0); 1477c478bd9Sstevel@tonic-gate } 1487c478bd9Sstevel@tonic-gate 1497c478bd9Sstevel@tonic-gate /* 1507c478bd9Sstevel@tonic-gate * Common ctl routines 1517c478bd9Sstevel@tonic-gate */ 1527c478bd9Sstevel@tonic-gate 1537c478bd9Sstevel@tonic-gate int 1547c478bd9Sstevel@tonic-gate ct_ctl_adopt(int fd) 1557c478bd9Sstevel@tonic-gate { 1567c478bd9Sstevel@tonic-gate if (ioctl(fd, CT_CADOPT) == -1) 1577c478bd9Sstevel@tonic-gate return (errno); 1587c478bd9Sstevel@tonic-gate return (0); 1597c478bd9Sstevel@tonic-gate } 1607c478bd9Sstevel@tonic-gate 1617c478bd9Sstevel@tonic-gate int 1627c478bd9Sstevel@tonic-gate ct_ctl_abandon(int fd) 1637c478bd9Sstevel@tonic-gate { 1647c478bd9Sstevel@tonic-gate if (ioctl(fd, CT_CABANDON) == -1) 1657c478bd9Sstevel@tonic-gate return (errno); 1667c478bd9Sstevel@tonic-gate return (0); 1677c478bd9Sstevel@tonic-gate } 1687c478bd9Sstevel@tonic-gate 1697c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 1707c478bd9Sstevel@tonic-gate int 1717c478bd9Sstevel@tonic-gate ct_ctl_newct(int cfd, ctevid_t evid, int tfd) 1727c478bd9Sstevel@tonic-gate { 1737c478bd9Sstevel@tonic-gate if (ioctl(cfd, CT_CNEWCT, tfd) == -1) 1747c478bd9Sstevel@tonic-gate return (errno); 1757c478bd9Sstevel@tonic-gate return (0); 1767c478bd9Sstevel@tonic-gate } 1777c478bd9Sstevel@tonic-gate 1787c478bd9Sstevel@tonic-gate int 1797c478bd9Sstevel@tonic-gate ct_ctl_ack(int fd, ctevid_t event) 1807c478bd9Sstevel@tonic-gate { 1817c478bd9Sstevel@tonic-gate if (ioctl(fd, CT_CACK, &event) == -1) 1827c478bd9Sstevel@tonic-gate return (errno); 1837c478bd9Sstevel@tonic-gate return (0); 1847c478bd9Sstevel@tonic-gate } 1857c478bd9Sstevel@tonic-gate 1867c478bd9Sstevel@tonic-gate int 187*25e8c5aaSvikram ct_ctl_nack(int fd, ctevid_t event) 188*25e8c5aaSvikram { 189*25e8c5aaSvikram if (ioctl(fd, CT_CNACK, &event) == -1) 190*25e8c5aaSvikram return (errno); 191*25e8c5aaSvikram return (0); 192*25e8c5aaSvikram } 193*25e8c5aaSvikram 194*25e8c5aaSvikram int 1957c478bd9Sstevel@tonic-gate ct_ctl_qack(int fd, ctevid_t event) 1967c478bd9Sstevel@tonic-gate { 1977c478bd9Sstevel@tonic-gate if (ioctl(fd, CT_CQREQ, &event) == -1) 1987c478bd9Sstevel@tonic-gate return (errno); 1997c478bd9Sstevel@tonic-gate return (0); 2007c478bd9Sstevel@tonic-gate } 2017c478bd9Sstevel@tonic-gate 2027c478bd9Sstevel@tonic-gate /* 2037c478bd9Sstevel@tonic-gate * Common status routines 2047c478bd9Sstevel@tonic-gate */ 2057c478bd9Sstevel@tonic-gate 2067c478bd9Sstevel@tonic-gate int 2077c478bd9Sstevel@tonic-gate ct_status_read(int fd, int detail, ct_stathdl_t *stathdl) 2087c478bd9Sstevel@tonic-gate { 2097c478bd9Sstevel@tonic-gate char *status_buffer = NULL; 2107c478bd9Sstevel@tonic-gate int status_nbytes = 0; 2117c478bd9Sstevel@tonic-gate struct ctlib_status_info *info; 2127c478bd9Sstevel@tonic-gate int error; 2137c478bd9Sstevel@tonic-gate 2147c478bd9Sstevel@tonic-gate info = malloc(sizeof (struct ctlib_status_info)); 2157c478bd9Sstevel@tonic-gate if (info == NULL) 2167c478bd9Sstevel@tonic-gate return (errno); 2177c478bd9Sstevel@tonic-gate 2187c478bd9Sstevel@tonic-gate info->status.ctst_detail = detail; 2197c478bd9Sstevel@tonic-gate if (detail != CTD_COMMON) { 2207c478bd9Sstevel@tonic-gate for (;;) { 2217c478bd9Sstevel@tonic-gate info->status.ctst_nbytes = status_nbytes; 2227c478bd9Sstevel@tonic-gate info->status.ctst_buffer = status_buffer; 2237c478bd9Sstevel@tonic-gate do 2247c478bd9Sstevel@tonic-gate error = ioctl(fd, CT_SSTATUS, &info->status); 2257c478bd9Sstevel@tonic-gate while (error == -1 && errno == EINTR); 2267c478bd9Sstevel@tonic-gate if (error == -1) 2277c478bd9Sstevel@tonic-gate goto errout; 2287c478bd9Sstevel@tonic-gate if (info->status.ctst_nbytes <= status_nbytes) 2297c478bd9Sstevel@tonic-gate break; 2307c478bd9Sstevel@tonic-gate 2317c478bd9Sstevel@tonic-gate if (status_buffer) 2327c478bd9Sstevel@tonic-gate free(status_buffer); 2337c478bd9Sstevel@tonic-gate status_nbytes = info->status.ctst_nbytes; 2347c478bd9Sstevel@tonic-gate status_buffer = malloc(status_nbytes); 2357c478bd9Sstevel@tonic-gate if (status_buffer == NULL) 2367c478bd9Sstevel@tonic-gate goto errout; 2377c478bd9Sstevel@tonic-gate } 2387c478bd9Sstevel@tonic-gate if ((errno = nvlist_unpack(info->status.ctst_buffer, 2397c478bd9Sstevel@tonic-gate info->status.ctst_nbytes, &info->nvl, 0)) != 0) 2407c478bd9Sstevel@tonic-gate goto errout; 2417c478bd9Sstevel@tonic-gate 2427c478bd9Sstevel@tonic-gate free(status_buffer); 2437c478bd9Sstevel@tonic-gate status_buffer = NULL; 2447c478bd9Sstevel@tonic-gate 2457c478bd9Sstevel@tonic-gate } else { 2467c478bd9Sstevel@tonic-gate info->status.ctst_nbytes = 0; 2477c478bd9Sstevel@tonic-gate info->nvl = NULL; 2487c478bd9Sstevel@tonic-gate if (ioctl(fd, CT_SSTATUS, &info->status) == -1) 2497c478bd9Sstevel@tonic-gate goto errout; 2507c478bd9Sstevel@tonic-gate } 2517c478bd9Sstevel@tonic-gate 2527c478bd9Sstevel@tonic-gate *stathdl = info; 2537c478bd9Sstevel@tonic-gate return (0); 2547c478bd9Sstevel@tonic-gate 2557c478bd9Sstevel@tonic-gate errout: 2567c478bd9Sstevel@tonic-gate error = errno; 2577c478bd9Sstevel@tonic-gate if (status_buffer) 2587c478bd9Sstevel@tonic-gate free(status_buffer); 2597c478bd9Sstevel@tonic-gate if (info) 2607c478bd9Sstevel@tonic-gate free(info); 2617c478bd9Sstevel@tonic-gate return (error); 2627c478bd9Sstevel@tonic-gate } 2637c478bd9Sstevel@tonic-gate 2647c478bd9Sstevel@tonic-gate void 2657c478bd9Sstevel@tonic-gate ct_status_free(ct_stathdl_t stathdl) 2667c478bd9Sstevel@tonic-gate { 2677c478bd9Sstevel@tonic-gate struct ctlib_status_info *info = stathdl; 2687c478bd9Sstevel@tonic-gate 2697c478bd9Sstevel@tonic-gate if (info->nvl) { 2707c478bd9Sstevel@tonic-gate assert(info->status.ctst_detail != CTD_COMMON); 2717c478bd9Sstevel@tonic-gate nvlist_free(info->nvl); 2727c478bd9Sstevel@tonic-gate } 2737c478bd9Sstevel@tonic-gate 2747c478bd9Sstevel@tonic-gate free(info); 2757c478bd9Sstevel@tonic-gate } 2767c478bd9Sstevel@tonic-gate 2777c478bd9Sstevel@tonic-gate ctid_t 2787c478bd9Sstevel@tonic-gate ct_status_get_id(ct_stathdl_t stathdl) 2797c478bd9Sstevel@tonic-gate { 2807c478bd9Sstevel@tonic-gate struct ctlib_status_info *info = stathdl; 2817c478bd9Sstevel@tonic-gate return (info->status.ctst_id); 2827c478bd9Sstevel@tonic-gate } 2837c478bd9Sstevel@tonic-gate 2847c478bd9Sstevel@tonic-gate zoneid_t 2857c478bd9Sstevel@tonic-gate ct_status_get_zoneid(ct_stathdl_t stathdl) 2867c478bd9Sstevel@tonic-gate { 2877c478bd9Sstevel@tonic-gate struct ctlib_status_info *info = stathdl; 2887c478bd9Sstevel@tonic-gate return (info->status.ctst_zoneid); 2897c478bd9Sstevel@tonic-gate } 2907c478bd9Sstevel@tonic-gate 2917c478bd9Sstevel@tonic-gate const char * 2927c478bd9Sstevel@tonic-gate ct_status_get_type(ct_stathdl_t stathdl) 2937c478bd9Sstevel@tonic-gate { 2947c478bd9Sstevel@tonic-gate struct ctlib_status_info *info = stathdl; 2957c478bd9Sstevel@tonic-gate return (types[info->status.ctst_type].type_name); 2967c478bd9Sstevel@tonic-gate } 2977c478bd9Sstevel@tonic-gate 2987c478bd9Sstevel@tonic-gate id_t 2997c478bd9Sstevel@tonic-gate ct_status_get_holder(ct_stathdl_t stathdl) 3007c478bd9Sstevel@tonic-gate { 3017c478bd9Sstevel@tonic-gate struct ctlib_status_info *info = stathdl; 3027c478bd9Sstevel@tonic-gate return (info->status.ctst_holder); 3037c478bd9Sstevel@tonic-gate } 3047c478bd9Sstevel@tonic-gate 3057c478bd9Sstevel@tonic-gate ctstate_t 3067c478bd9Sstevel@tonic-gate ct_status_get_state(ct_stathdl_t stathdl) 3077c478bd9Sstevel@tonic-gate { 3087c478bd9Sstevel@tonic-gate struct ctlib_status_info *info = stathdl; 3097c478bd9Sstevel@tonic-gate return (info->status.ctst_state); 3107c478bd9Sstevel@tonic-gate } 3117c478bd9Sstevel@tonic-gate 3127c478bd9Sstevel@tonic-gate int 3137c478bd9Sstevel@tonic-gate ct_status_get_nevents(ct_stathdl_t stathdl) 3147c478bd9Sstevel@tonic-gate { 3157c478bd9Sstevel@tonic-gate struct ctlib_status_info *info = stathdl; 3167c478bd9Sstevel@tonic-gate return (info->status.ctst_nevents); 3177c478bd9Sstevel@tonic-gate } 3187c478bd9Sstevel@tonic-gate 3197c478bd9Sstevel@tonic-gate int 3207c478bd9Sstevel@tonic-gate ct_status_get_ntime(ct_stathdl_t stathdl) 3217c478bd9Sstevel@tonic-gate { 3227c478bd9Sstevel@tonic-gate struct ctlib_status_info *info = stathdl; 3237c478bd9Sstevel@tonic-gate return (info->status.ctst_ntime); 3247c478bd9Sstevel@tonic-gate } 3257c478bd9Sstevel@tonic-gate 3267c478bd9Sstevel@tonic-gate int 3277c478bd9Sstevel@tonic-gate ct_status_get_qtime(ct_stathdl_t stathdl) 3287c478bd9Sstevel@tonic-gate { 3297c478bd9Sstevel@tonic-gate struct ctlib_status_info *info = stathdl; 3307c478bd9Sstevel@tonic-gate return (info->status.ctst_qtime); 3317c478bd9Sstevel@tonic-gate } 3327c478bd9Sstevel@tonic-gate 3337c478bd9Sstevel@tonic-gate ctevid_t 3347c478bd9Sstevel@tonic-gate ct_status_get_nevid(ct_stathdl_t stathdl) 3357c478bd9Sstevel@tonic-gate { 3367c478bd9Sstevel@tonic-gate struct ctlib_status_info *info = stathdl; 3377c478bd9Sstevel@tonic-gate return (info->status.ctst_nevid); 3387c478bd9Sstevel@tonic-gate } 3397c478bd9Sstevel@tonic-gate 3407c478bd9Sstevel@tonic-gate uint_t 3417c478bd9Sstevel@tonic-gate ct_status_get_informative(ct_stathdl_t stathdl) 3427c478bd9Sstevel@tonic-gate { 3437c478bd9Sstevel@tonic-gate struct ctlib_status_info *info = stathdl; 3447c478bd9Sstevel@tonic-gate return (info->status.ctst_informative); 3457c478bd9Sstevel@tonic-gate } 3467c478bd9Sstevel@tonic-gate 3477c478bd9Sstevel@tonic-gate uint_t 3487c478bd9Sstevel@tonic-gate ct_status_get_critical(ct_stathdl_t stathdl) 3497c478bd9Sstevel@tonic-gate { 3507c478bd9Sstevel@tonic-gate struct ctlib_status_info *info = stathdl; 3517c478bd9Sstevel@tonic-gate return (info->status.ctst_critical); 3527c478bd9Sstevel@tonic-gate } 3537c478bd9Sstevel@tonic-gate 3547c478bd9Sstevel@tonic-gate uint64_t 3557c478bd9Sstevel@tonic-gate ct_status_get_cookie(ct_stathdl_t stathdl) 3567c478bd9Sstevel@tonic-gate { 3577c478bd9Sstevel@tonic-gate struct ctlib_status_info *info = stathdl; 3587c478bd9Sstevel@tonic-gate return (info->status.ctst_cookie); 3597c478bd9Sstevel@tonic-gate } 3607c478bd9Sstevel@tonic-gate 3617c478bd9Sstevel@tonic-gate /* 3627c478bd9Sstevel@tonic-gate * Common event routines 3637c478bd9Sstevel@tonic-gate */ 3647c478bd9Sstevel@tonic-gate 3657c478bd9Sstevel@tonic-gate static int 3667c478bd9Sstevel@tonic-gate unpack_and_merge(nvlist_t **nvl, char *buffer, size_t len) 3677c478bd9Sstevel@tonic-gate { 3687c478bd9Sstevel@tonic-gate nvlist_t *tmpnvl; 3697c478bd9Sstevel@tonic-gate int error; 3707c478bd9Sstevel@tonic-gate 3717c478bd9Sstevel@tonic-gate if ((error = nvlist_unpack(buffer, len, &tmpnvl, 0)) != 0) 3727c478bd9Sstevel@tonic-gate return (error); 3737c478bd9Sstevel@tonic-gate 3747c478bd9Sstevel@tonic-gate if (*nvl == NULL) { 3757c478bd9Sstevel@tonic-gate *nvl = tmpnvl; 3767c478bd9Sstevel@tonic-gate return (0); 3777c478bd9Sstevel@tonic-gate } 3787c478bd9Sstevel@tonic-gate 3797c478bd9Sstevel@tonic-gate error = nvlist_merge(*nvl, tmpnvl, 0); 3807c478bd9Sstevel@tonic-gate nvlist_free(tmpnvl); 3817c478bd9Sstevel@tonic-gate return (error); 3827c478bd9Sstevel@tonic-gate } 3837c478bd9Sstevel@tonic-gate 3847c478bd9Sstevel@tonic-gate static int 3857c478bd9Sstevel@tonic-gate ct_event_read_internal(int fd, int cmd, ct_evthdl_t *evt) 3867c478bd9Sstevel@tonic-gate { 3877c478bd9Sstevel@tonic-gate char *event_buffer = NULL; 3887c478bd9Sstevel@tonic-gate int event_nbytes = 0; 3897c478bd9Sstevel@tonic-gate struct ctlib_event_info *info; 3907c478bd9Sstevel@tonic-gate ct_event_t *event; 3917c478bd9Sstevel@tonic-gate int error; 3927c478bd9Sstevel@tonic-gate 3937c478bd9Sstevel@tonic-gate info = malloc(sizeof (struct ctlib_event_info)); 3947c478bd9Sstevel@tonic-gate if (info == NULL) 3957c478bd9Sstevel@tonic-gate return (errno); 3967c478bd9Sstevel@tonic-gate info->nvl = NULL; 3977c478bd9Sstevel@tonic-gate event = &info->event; 3987c478bd9Sstevel@tonic-gate 3997c478bd9Sstevel@tonic-gate for (;;) { 4007c478bd9Sstevel@tonic-gate event->ctev_nbytes = event_nbytes; 4017c478bd9Sstevel@tonic-gate event->ctev_buffer = event_buffer; 4027c478bd9Sstevel@tonic-gate do 4037c478bd9Sstevel@tonic-gate error = ioctl(fd, cmd, event); 4047c478bd9Sstevel@tonic-gate while (error == -1 && errno == EINTR); 4057c478bd9Sstevel@tonic-gate if (error == -1) { 4067c478bd9Sstevel@tonic-gate error = errno; 4077c478bd9Sstevel@tonic-gate goto errout; 4087c478bd9Sstevel@tonic-gate } 4097c478bd9Sstevel@tonic-gate if (event->ctev_nbytes <= event_nbytes) 4107c478bd9Sstevel@tonic-gate break; 4117c478bd9Sstevel@tonic-gate 4127c478bd9Sstevel@tonic-gate if (event_buffer) 4137c478bd9Sstevel@tonic-gate free(event_buffer); 4147c478bd9Sstevel@tonic-gate event_nbytes = event->ctev_nbytes; 4157c478bd9Sstevel@tonic-gate event_buffer = malloc(event_nbytes); 4167c478bd9Sstevel@tonic-gate if (event_buffer == NULL) { 4177c478bd9Sstevel@tonic-gate error = errno; 4187c478bd9Sstevel@tonic-gate goto errout; 4197c478bd9Sstevel@tonic-gate } 4207c478bd9Sstevel@tonic-gate } 4217c478bd9Sstevel@tonic-gate 4227c478bd9Sstevel@tonic-gate if (event->ctev_goffset > 0 && (error = unpack_and_merge(&info->nvl, 4237c478bd9Sstevel@tonic-gate event->ctev_buffer, event->ctev_goffset)) != 0) 4247c478bd9Sstevel@tonic-gate goto errout; 4257c478bd9Sstevel@tonic-gate 4267c478bd9Sstevel@tonic-gate if (event->ctev_goffset < event->ctev_nbytes && 4277c478bd9Sstevel@tonic-gate (error = unpack_and_merge(&info->nvl, 4287c478bd9Sstevel@tonic-gate event->ctev_buffer + event->ctev_goffset, 4297c478bd9Sstevel@tonic-gate event->ctev_nbytes - event->ctev_goffset)) != 0) 4307c478bd9Sstevel@tonic-gate goto errout; 4317c478bd9Sstevel@tonic-gate 4327c478bd9Sstevel@tonic-gate free(event_buffer); 4337c478bd9Sstevel@tonic-gate 4347c478bd9Sstevel@tonic-gate *evt = info; 4357c478bd9Sstevel@tonic-gate return (0); 4367c478bd9Sstevel@tonic-gate 4377c478bd9Sstevel@tonic-gate errout: 4387c478bd9Sstevel@tonic-gate if (event_buffer) 4397c478bd9Sstevel@tonic-gate free(event_buffer); 4407c478bd9Sstevel@tonic-gate if (info) { 4417c478bd9Sstevel@tonic-gate if (info->nvl) 4427c478bd9Sstevel@tonic-gate nvlist_free(info->nvl); 4437c478bd9Sstevel@tonic-gate free(info); 4447c478bd9Sstevel@tonic-gate } 4457c478bd9Sstevel@tonic-gate return (error); 4467c478bd9Sstevel@tonic-gate } 4477c478bd9Sstevel@tonic-gate 4487c478bd9Sstevel@tonic-gate int 4497c478bd9Sstevel@tonic-gate ct_event_read(int fd, ct_evthdl_t *evthdl) 4507c478bd9Sstevel@tonic-gate { 4517c478bd9Sstevel@tonic-gate return (ct_event_read_internal(fd, CT_ERECV, evthdl)); 4527c478bd9Sstevel@tonic-gate } 4537c478bd9Sstevel@tonic-gate 4547c478bd9Sstevel@tonic-gate int 4557c478bd9Sstevel@tonic-gate ct_event_read_critical(int fd, ct_evthdl_t *evthdl) 4567c478bd9Sstevel@tonic-gate { 4577c478bd9Sstevel@tonic-gate return (ct_event_read_internal(fd, CT_ECRECV, evthdl)); 4587c478bd9Sstevel@tonic-gate } 4597c478bd9Sstevel@tonic-gate 4607c478bd9Sstevel@tonic-gate int 4617c478bd9Sstevel@tonic-gate ct_event_reset(int fd) 4627c478bd9Sstevel@tonic-gate { 4637c478bd9Sstevel@tonic-gate if (ioctl(fd, CT_ERESET) == -1) 4647c478bd9Sstevel@tonic-gate return (errno); 4657c478bd9Sstevel@tonic-gate return (0); 4667c478bd9Sstevel@tonic-gate } 4677c478bd9Sstevel@tonic-gate 4687c478bd9Sstevel@tonic-gate int 4697c478bd9Sstevel@tonic-gate ct_event_reliable(int fd) 4707c478bd9Sstevel@tonic-gate { 4717c478bd9Sstevel@tonic-gate if (ioctl(fd, CT_ERELIABLE) == -1) 4727c478bd9Sstevel@tonic-gate return (errno); 4737c478bd9Sstevel@tonic-gate return (0); 4747c478bd9Sstevel@tonic-gate } 4757c478bd9Sstevel@tonic-gate 4767c478bd9Sstevel@tonic-gate void 4777c478bd9Sstevel@tonic-gate ct_event_free(ct_evthdl_t evthdl) 4787c478bd9Sstevel@tonic-gate { 4797c478bd9Sstevel@tonic-gate struct ctlib_event_info *info = evthdl; 4807c478bd9Sstevel@tonic-gate 4817c478bd9Sstevel@tonic-gate if (info->nvl) 4827c478bd9Sstevel@tonic-gate nvlist_free(info->nvl); 4837c478bd9Sstevel@tonic-gate free(info); 4847c478bd9Sstevel@tonic-gate } 4857c478bd9Sstevel@tonic-gate 4867c478bd9Sstevel@tonic-gate 4877c478bd9Sstevel@tonic-gate uint_t 4887c478bd9Sstevel@tonic-gate ct_event_get_flags(ct_evthdl_t evthdl) 4897c478bd9Sstevel@tonic-gate { 4907c478bd9Sstevel@tonic-gate struct ctlib_event_info *info = evthdl; 4917c478bd9Sstevel@tonic-gate return (info->event.ctev_flags); 4927c478bd9Sstevel@tonic-gate } 4937c478bd9Sstevel@tonic-gate 4947c478bd9Sstevel@tonic-gate ctid_t 4957c478bd9Sstevel@tonic-gate ct_event_get_ctid(ct_evthdl_t evthdl) 4967c478bd9Sstevel@tonic-gate { 4977c478bd9Sstevel@tonic-gate struct ctlib_event_info *info = evthdl; 4987c478bd9Sstevel@tonic-gate return (info->event.ctev_id); 4997c478bd9Sstevel@tonic-gate } 5007c478bd9Sstevel@tonic-gate 5017c478bd9Sstevel@tonic-gate ctevid_t 5027c478bd9Sstevel@tonic-gate ct_event_get_evid(ct_evthdl_t evthdl) 5037c478bd9Sstevel@tonic-gate { 5047c478bd9Sstevel@tonic-gate struct ctlib_event_info *info = evthdl; 5057c478bd9Sstevel@tonic-gate return (info->event.ctev_evid); 5067c478bd9Sstevel@tonic-gate } 5077c478bd9Sstevel@tonic-gate 5087c478bd9Sstevel@tonic-gate uint_t 5097c478bd9Sstevel@tonic-gate ct_event_get_type(ct_evthdl_t evthdl) 5107c478bd9Sstevel@tonic-gate { 5117c478bd9Sstevel@tonic-gate struct ctlib_event_info *info = evthdl; 5127c478bd9Sstevel@tonic-gate return (info->event.ctev_type); 5137c478bd9Sstevel@tonic-gate } 5147c478bd9Sstevel@tonic-gate 5157c478bd9Sstevel@tonic-gate int 5167c478bd9Sstevel@tonic-gate ct_event_get_nevid(ct_evthdl_t evthdl, ctevid_t *evidp) 5177c478bd9Sstevel@tonic-gate { 5187c478bd9Sstevel@tonic-gate struct ctlib_event_info *info = evthdl; 5197c478bd9Sstevel@tonic-gate if (info->nvl == NULL || 5207c478bd9Sstevel@tonic-gate nvlist_lookup_uint64(info->nvl, CTS_NEVID, evidp)) 5217c478bd9Sstevel@tonic-gate return (EINVAL); 5227c478bd9Sstevel@tonic-gate return (0); 5237c478bd9Sstevel@tonic-gate } 5247c478bd9Sstevel@tonic-gate 5257c478bd9Sstevel@tonic-gate int 5267c478bd9Sstevel@tonic-gate ct_event_get_newct(ct_evthdl_t evthdl, ctid_t *ctidp) 5277c478bd9Sstevel@tonic-gate { 5287c478bd9Sstevel@tonic-gate struct ctlib_event_info *info = evthdl; 5297c478bd9Sstevel@tonic-gate if (info->nvl == NULL || 5307c478bd9Sstevel@tonic-gate nvlist_lookup_int32(info->nvl, CTS_NEWCT, (int *)ctidp)) 5317c478bd9Sstevel@tonic-gate return (EINVAL); 5327c478bd9Sstevel@tonic-gate return (0); 5337c478bd9Sstevel@tonic-gate } 534