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 580ab886dSwesolows * Common Development and Distribution License (the "License"). 680ab886dSwesolows * 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 */ 2180ab886dSwesolows 227c478bd9Sstevel@tonic-gate /* 23*49b225e1SGavin Maltby * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 247c478bd9Sstevel@tonic-gate * Use is subject to license terms. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate #include <stdio.h> 287c478bd9Sstevel@tonic-gate #include <fcntl.h> 297c478bd9Sstevel@tonic-gate #include <errno.h> 307c478bd9Sstevel@tonic-gate #include <door.h> 317c478bd9Sstevel@tonic-gate #include <unistd.h> 327c478bd9Sstevel@tonic-gate #include <stddef.h> 337c478bd9Sstevel@tonic-gate #include <stdlib.h> 347c478bd9Sstevel@tonic-gate #include <string.h> 357c478bd9Sstevel@tonic-gate #include <strings.h> 367c478bd9Sstevel@tonic-gate #include <synch.h> 377c478bd9Sstevel@tonic-gate #include <pthread.h> 387c478bd9Sstevel@tonic-gate #include <thread.h> 397c478bd9Sstevel@tonic-gate #include <libnvpair.h> 407c478bd9Sstevel@tonic-gate #include <assert.h> 417c478bd9Sstevel@tonic-gate #include <sys/stat.h> 427c478bd9Sstevel@tonic-gate #include <sys/types.h> 437c478bd9Sstevel@tonic-gate #include <sys/modctl.h> 447c478bd9Sstevel@tonic-gate #include <sys/mnttab.h> 457c478bd9Sstevel@tonic-gate #include <sys/sysevent.h> 467c478bd9Sstevel@tonic-gate #include <sys/sysevent_impl.h> 477c478bd9Sstevel@tonic-gate 487c478bd9Sstevel@tonic-gate #include "libsysevent.h" 497c478bd9Sstevel@tonic-gate #include "libsysevent_impl.h" 507c478bd9Sstevel@tonic-gate 517c478bd9Sstevel@tonic-gate /* 527c478bd9Sstevel@tonic-gate * libsysevent - The system event framework library 537c478bd9Sstevel@tonic-gate * 547c478bd9Sstevel@tonic-gate * This library provides routines to help with marshalling 557c478bd9Sstevel@tonic-gate * and unmarshalling of data contained in a sysevent event 567c478bd9Sstevel@tonic-gate * buffer. 577c478bd9Sstevel@tonic-gate */ 587c478bd9Sstevel@tonic-gate 597c478bd9Sstevel@tonic-gate #define SE_ENCODE_METHOD NV_ENCODE_NATIVE 607c478bd9Sstevel@tonic-gate 617c478bd9Sstevel@tonic-gate #define dprint if (libsysevent_debug) (void) printf 627c478bd9Sstevel@tonic-gate static int libsysevent_debug = 0; 637c478bd9Sstevel@tonic-gate 647c478bd9Sstevel@tonic-gate static sysevent_t *se_unpack(sysevent_t *); 657c478bd9Sstevel@tonic-gate static int cleanup_id(sysevent_handle_t *shp, uint32_t id, int type); 667c478bd9Sstevel@tonic-gate 677c478bd9Sstevel@tonic-gate /* 687c478bd9Sstevel@tonic-gate * The following routines allow system event publication to the sysevent 697c478bd9Sstevel@tonic-gate * framework. 707c478bd9Sstevel@tonic-gate */ 717c478bd9Sstevel@tonic-gate 727c478bd9Sstevel@tonic-gate /* 737c478bd9Sstevel@tonic-gate * sysevent_alloc - allocate a sysevent buffer 747c478bd9Sstevel@tonic-gate */ 757c478bd9Sstevel@tonic-gate static sysevent_t * 767c478bd9Sstevel@tonic-gate sysevent_alloc(char *class, int class_sz, char *subclass, int subclass_sz, 777c478bd9Sstevel@tonic-gate char *pub, int pub_sz, nvlist_t *attr_list) 787c478bd9Sstevel@tonic-gate { 797c478bd9Sstevel@tonic-gate int payload_sz; 807c478bd9Sstevel@tonic-gate int aligned_class_sz, aligned_subclass_sz, aligned_pub_sz; 817c478bd9Sstevel@tonic-gate size_t nvlist_sz = 0; 827c478bd9Sstevel@tonic-gate char *attr; 837c478bd9Sstevel@tonic-gate uint64_t attr_offset; 847c478bd9Sstevel@tonic-gate sysevent_t *ev; 857c478bd9Sstevel@tonic-gate 867c478bd9Sstevel@tonic-gate if (attr_list != NULL) { 877c478bd9Sstevel@tonic-gate if (nvlist_size(attr_list, &nvlist_sz, SE_ENCODE_METHOD) 887c478bd9Sstevel@tonic-gate != 0) { 897c478bd9Sstevel@tonic-gate return (NULL); 907c478bd9Sstevel@tonic-gate } 917c478bd9Sstevel@tonic-gate } 927c478bd9Sstevel@tonic-gate 937c478bd9Sstevel@tonic-gate /* 947c478bd9Sstevel@tonic-gate * Calculate and reserve space for the class, subclass and 957c478bd9Sstevel@tonic-gate * publisher strings in the event buffer 967c478bd9Sstevel@tonic-gate */ 977c478bd9Sstevel@tonic-gate 987c478bd9Sstevel@tonic-gate /* String sizes must be 64-bit aligned in the event buffer */ 997c478bd9Sstevel@tonic-gate aligned_class_sz = SE_ALIGN(class_sz); 1007c478bd9Sstevel@tonic-gate aligned_subclass_sz = SE_ALIGN(subclass_sz); 1017c478bd9Sstevel@tonic-gate aligned_pub_sz = SE_ALIGN(pub_sz); 1027c478bd9Sstevel@tonic-gate 1037c478bd9Sstevel@tonic-gate payload_sz = (aligned_class_sz - sizeof (uint64_t)) + 1047c478bd9Sstevel@tonic-gate (aligned_subclass_sz - sizeof (uint64_t)) + 1057c478bd9Sstevel@tonic-gate (aligned_pub_sz - sizeof (uint64_t)) - sizeof (uint64_t) + 1067c478bd9Sstevel@tonic-gate nvlist_sz; 1077c478bd9Sstevel@tonic-gate 1087c478bd9Sstevel@tonic-gate /* 1097c478bd9Sstevel@tonic-gate * Allocate event buffer plus additional payload overhead. 1107c478bd9Sstevel@tonic-gate */ 1117c478bd9Sstevel@tonic-gate ev = calloc(1, sizeof (sysevent_impl_t) + payload_sz); 1127c478bd9Sstevel@tonic-gate if (ev == NULL) { 1137c478bd9Sstevel@tonic-gate return (NULL); 1147c478bd9Sstevel@tonic-gate } 1157c478bd9Sstevel@tonic-gate 1167c478bd9Sstevel@tonic-gate /* Initialize the event buffer data */ 1177c478bd9Sstevel@tonic-gate SE_VERSION(ev) = SYS_EVENT_VERSION; 1187c478bd9Sstevel@tonic-gate (void) bcopy(class, SE_CLASS_NAME(ev), class_sz); 1197c478bd9Sstevel@tonic-gate 1207c478bd9Sstevel@tonic-gate SE_SUBCLASS_OFF(ev) = SE_ALIGN(offsetof(sysevent_impl_t, se_class_name)) 1217c478bd9Sstevel@tonic-gate + aligned_class_sz; 1227c478bd9Sstevel@tonic-gate (void) bcopy(subclass, SE_SUBCLASS_NAME(ev), subclass_sz); 1237c478bd9Sstevel@tonic-gate 1247c478bd9Sstevel@tonic-gate SE_PUB_OFF(ev) = SE_SUBCLASS_OFF(ev) + aligned_subclass_sz; 1257c478bd9Sstevel@tonic-gate (void) bcopy(pub, SE_PUB_NAME(ev), pub_sz); 1267c478bd9Sstevel@tonic-gate 1277c478bd9Sstevel@tonic-gate SE_PAYLOAD_SZ(ev) = payload_sz; 1287c478bd9Sstevel@tonic-gate SE_ATTR_PTR(ev) = (uint64_t)0; 1297c478bd9Sstevel@tonic-gate 1307c478bd9Sstevel@tonic-gate /* Check for attribute list */ 1317c478bd9Sstevel@tonic-gate if (attr_list == NULL) { 1327c478bd9Sstevel@tonic-gate return (ev); 1337c478bd9Sstevel@tonic-gate } 1347c478bd9Sstevel@tonic-gate 1357c478bd9Sstevel@tonic-gate /* Copy attribute data to contiguous memory */ 1367c478bd9Sstevel@tonic-gate SE_FLAG(ev) = SE_PACKED_BUF; 1377c478bd9Sstevel@tonic-gate attr_offset = SE_ATTR_OFF(ev); 1387c478bd9Sstevel@tonic-gate attr = (char *)((caddr_t)ev + attr_offset); 1397c478bd9Sstevel@tonic-gate if (nvlist_pack(attr_list, &attr, &nvlist_sz, SE_ENCODE_METHOD, 1407c478bd9Sstevel@tonic-gate 0) != 0) { 1417c478bd9Sstevel@tonic-gate free(ev); 1427c478bd9Sstevel@tonic-gate return (NULL); 1437c478bd9Sstevel@tonic-gate } 1447c478bd9Sstevel@tonic-gate 1457c478bd9Sstevel@tonic-gate return (ev); 1467c478bd9Sstevel@tonic-gate } 1477c478bd9Sstevel@tonic-gate 1487c478bd9Sstevel@tonic-gate /* 1497c478bd9Sstevel@tonic-gate * sysevent_post_event - generate a system event via the sysevent framework 1507c478bd9Sstevel@tonic-gate */ 1517c478bd9Sstevel@tonic-gate int 1527c478bd9Sstevel@tonic-gate sysevent_post_event(char *class, char *subclass, char *vendor, char *pub_name, 1537c478bd9Sstevel@tonic-gate nvlist_t *attr_list, sysevent_id_t *eid) 1547c478bd9Sstevel@tonic-gate { 1557c478bd9Sstevel@tonic-gate int error; 1567c478bd9Sstevel@tonic-gate sysevent_t *ev; 1577c478bd9Sstevel@tonic-gate 1587c478bd9Sstevel@tonic-gate ev = sysevent_alloc_event(class, subclass, vendor, pub_name, attr_list); 1597c478bd9Sstevel@tonic-gate if (ev == NULL) { 1607c478bd9Sstevel@tonic-gate return (-1); 1617c478bd9Sstevel@tonic-gate } 1627c478bd9Sstevel@tonic-gate 1637c478bd9Sstevel@tonic-gate error = modctl(MODEVENTS, (uintptr_t)MODEVENTS_POST_EVENT, 164*49b225e1SGavin Maltby (uintptr_t)ev, (uintptr_t)SE_SIZE(ev), (uintptr_t)eid, 0); 1657c478bd9Sstevel@tonic-gate 1667c478bd9Sstevel@tonic-gate sysevent_free(ev); 1677c478bd9Sstevel@tonic-gate 1687c478bd9Sstevel@tonic-gate if (error) { 1697c478bd9Sstevel@tonic-gate errno = EIO; 1707c478bd9Sstevel@tonic-gate return (-1); 1717c478bd9Sstevel@tonic-gate } 1727c478bd9Sstevel@tonic-gate 1737c478bd9Sstevel@tonic-gate return (0); 1747c478bd9Sstevel@tonic-gate } 1757c478bd9Sstevel@tonic-gate 1767c478bd9Sstevel@tonic-gate /* 1777c478bd9Sstevel@tonic-gate * The following routines are used to free or duplicate a 1787c478bd9Sstevel@tonic-gate * sysevent event buffer. 1797c478bd9Sstevel@tonic-gate */ 1807c478bd9Sstevel@tonic-gate 1817c478bd9Sstevel@tonic-gate /* 1827c478bd9Sstevel@tonic-gate * sysevent_dup - Allocate and copy an event buffer 1837c478bd9Sstevel@tonic-gate * Copies both packed and unpacked to unpacked sysevent. 1847c478bd9Sstevel@tonic-gate */ 1857c478bd9Sstevel@tonic-gate sysevent_t * 1867c478bd9Sstevel@tonic-gate sysevent_dup(sysevent_t *ev) 1877c478bd9Sstevel@tonic-gate { 1887c478bd9Sstevel@tonic-gate nvlist_t *nvl, *cnvl = NULL; 1897c478bd9Sstevel@tonic-gate uint64_t attr_offset; 1907c478bd9Sstevel@tonic-gate sysevent_t *copy; 1917c478bd9Sstevel@tonic-gate 1927c478bd9Sstevel@tonic-gate if (SE_FLAG(ev) == SE_PACKED_BUF) 1937c478bd9Sstevel@tonic-gate return (se_unpack(ev)); 1947c478bd9Sstevel@tonic-gate 1957c478bd9Sstevel@tonic-gate /* Copy event header information */ 1967c478bd9Sstevel@tonic-gate attr_offset = SE_ATTR_OFF(ev); 1977c478bd9Sstevel@tonic-gate copy = calloc(1, attr_offset); 1987c478bd9Sstevel@tonic-gate if (copy == NULL) 1997c478bd9Sstevel@tonic-gate return (NULL); 2007c478bd9Sstevel@tonic-gate bcopy(ev, copy, attr_offset); 2017c478bd9Sstevel@tonic-gate 20280ab886dSwesolows nvl = (nvlist_t *)(uintptr_t)SE_ATTR_PTR(ev); 2037c478bd9Sstevel@tonic-gate if (nvl && nvlist_dup(nvl, &cnvl, 0) != 0) { 2047c478bd9Sstevel@tonic-gate free(copy); 2057c478bd9Sstevel@tonic-gate return (NULL); 2067c478bd9Sstevel@tonic-gate } 2077c478bd9Sstevel@tonic-gate 2087c478bd9Sstevel@tonic-gate SE_ATTR_PTR(copy) = (uintptr_t)cnvl; 2097c478bd9Sstevel@tonic-gate SE_FLAG(copy) = 0; /* unpacked */ 2107c478bd9Sstevel@tonic-gate return (copy); 2117c478bd9Sstevel@tonic-gate } 2127c478bd9Sstevel@tonic-gate 2137c478bd9Sstevel@tonic-gate /* 2147c478bd9Sstevel@tonic-gate * sysevent_free - Free memory allocated for an event buffer 2157c478bd9Sstevel@tonic-gate */ 2167c478bd9Sstevel@tonic-gate void 2177c478bd9Sstevel@tonic-gate sysevent_free(sysevent_t *ev) 2187c478bd9Sstevel@tonic-gate { 21980ab886dSwesolows nvlist_t *attr_list = (nvlist_t *)(uintptr_t)SE_ATTR_PTR(ev); 2207c478bd9Sstevel@tonic-gate 2217c478bd9Sstevel@tonic-gate if (attr_list) 2227c478bd9Sstevel@tonic-gate nvlist_free(attr_list); 2237c478bd9Sstevel@tonic-gate free(ev); 2247c478bd9Sstevel@tonic-gate } 2257c478bd9Sstevel@tonic-gate 2267c478bd9Sstevel@tonic-gate /* 2277c478bd9Sstevel@tonic-gate * The following routines are used to extract attribute data from a sysevent 2287c478bd9Sstevel@tonic-gate * handle. 2297c478bd9Sstevel@tonic-gate */ 2307c478bd9Sstevel@tonic-gate 2317c478bd9Sstevel@tonic-gate /* 2327c478bd9Sstevel@tonic-gate * sysevent_get_attr_list - allocate and return an attribute associated with 2337c478bd9Sstevel@tonic-gate * the given sysevent buffer. 2347c478bd9Sstevel@tonic-gate */ 2357c478bd9Sstevel@tonic-gate int 2367c478bd9Sstevel@tonic-gate sysevent_get_attr_list(sysevent_t *ev, nvlist_t **nvlist) 2377c478bd9Sstevel@tonic-gate { 2387c478bd9Sstevel@tonic-gate int error; 2397c478bd9Sstevel@tonic-gate caddr_t attr; 2407c478bd9Sstevel@tonic-gate size_t attr_len; 2417c478bd9Sstevel@tonic-gate uint64_t attr_offset; 2427c478bd9Sstevel@tonic-gate nvlist_t *nvl; 2437c478bd9Sstevel@tonic-gate 2447c478bd9Sstevel@tonic-gate *nvlist = NULL; 2457c478bd9Sstevel@tonic-gate 2467c478bd9Sstevel@tonic-gate /* Duplicate attribute for an unpacked sysevent buffer */ 2477c478bd9Sstevel@tonic-gate if (SE_FLAG(ev) != SE_PACKED_BUF) { 24880ab886dSwesolows nvl = (nvlist_t *)(uintptr_t)SE_ATTR_PTR(ev); 2497c478bd9Sstevel@tonic-gate if (nvl == NULL) { 2507c478bd9Sstevel@tonic-gate return (0); 2517c478bd9Sstevel@tonic-gate } 2527c478bd9Sstevel@tonic-gate if ((error = nvlist_dup(nvl, nvlist, 0)) != 0) { 2537c478bd9Sstevel@tonic-gate if (error == ENOMEM) { 2547c478bd9Sstevel@tonic-gate errno = error; 2557c478bd9Sstevel@tonic-gate } else { 2567c478bd9Sstevel@tonic-gate errno = EINVAL; 2577c478bd9Sstevel@tonic-gate } 2587c478bd9Sstevel@tonic-gate return (-1); 2597c478bd9Sstevel@tonic-gate } 2607c478bd9Sstevel@tonic-gate return (0); 2617c478bd9Sstevel@tonic-gate } 2627c478bd9Sstevel@tonic-gate 2637c478bd9Sstevel@tonic-gate attr_offset = SE_ATTR_OFF(ev); 2647c478bd9Sstevel@tonic-gate if (SE_SIZE(ev) == attr_offset) { 2657c478bd9Sstevel@tonic-gate return (0); 2667c478bd9Sstevel@tonic-gate } 2677c478bd9Sstevel@tonic-gate 2687c478bd9Sstevel@tonic-gate /* unpack nvlist */ 2697c478bd9Sstevel@tonic-gate attr = (caddr_t)ev + attr_offset; 2707c478bd9Sstevel@tonic-gate attr_len = SE_SIZE(ev) - attr_offset; 2717c478bd9Sstevel@tonic-gate if ((error = nvlist_unpack(attr, attr_len, nvlist, 0)) != 0) { 2727c478bd9Sstevel@tonic-gate if (error == ENOMEM) { 2737c478bd9Sstevel@tonic-gate errno = error; 2747c478bd9Sstevel@tonic-gate } else { 2757c478bd9Sstevel@tonic-gate errno = EINVAL; 2767c478bd9Sstevel@tonic-gate } 2777c478bd9Sstevel@tonic-gate return (-1); 2787c478bd9Sstevel@tonic-gate } 2797c478bd9Sstevel@tonic-gate 2807c478bd9Sstevel@tonic-gate return (0); 2817c478bd9Sstevel@tonic-gate } 2827c478bd9Sstevel@tonic-gate 2837c478bd9Sstevel@tonic-gate /* 2847c478bd9Sstevel@tonic-gate * sysevent_attr_name - Get name of attribute 2857c478bd9Sstevel@tonic-gate */ 2867c478bd9Sstevel@tonic-gate char * 2877c478bd9Sstevel@tonic-gate sysevent_attr_name(sysevent_attr_t *attr) 2887c478bd9Sstevel@tonic-gate { 2897c478bd9Sstevel@tonic-gate if (attr == NULL) { 2907c478bd9Sstevel@tonic-gate errno = EINVAL; 2917c478bd9Sstevel@tonic-gate return (NULL); 2927c478bd9Sstevel@tonic-gate } 2937c478bd9Sstevel@tonic-gate return (nvpair_name((nvpair_t *)attr)); 2947c478bd9Sstevel@tonic-gate } 2957c478bd9Sstevel@tonic-gate 2967c478bd9Sstevel@tonic-gate /* 2977c478bd9Sstevel@tonic-gate * sysevent_attr_value - Get attribute value data and type 2987c478bd9Sstevel@tonic-gate */ 2997c478bd9Sstevel@tonic-gate int 3007c478bd9Sstevel@tonic-gate sysevent_attr_value(sysevent_attr_t *attr, sysevent_value_t *se_value) 3017c478bd9Sstevel@tonic-gate { 3027c478bd9Sstevel@tonic-gate nvpair_t *nvp = attr; 3037c478bd9Sstevel@tonic-gate 3047c478bd9Sstevel@tonic-gate if (nvp == NULL) 3057c478bd9Sstevel@tonic-gate return (EINVAL); 3067c478bd9Sstevel@tonic-gate 3077c478bd9Sstevel@tonic-gate /* Convert DATA_TYPE_* to SE_DATA_TYPE_* */ 3087c478bd9Sstevel@tonic-gate switch (nvpair_type(nvp)) { 3097c478bd9Sstevel@tonic-gate case DATA_TYPE_BYTE: 3107c478bd9Sstevel@tonic-gate se_value->value_type = SE_DATA_TYPE_BYTE; 3117c478bd9Sstevel@tonic-gate (void) nvpair_value_byte(nvp, &se_value->value.sv_byte); 3127c478bd9Sstevel@tonic-gate break; 3137c478bd9Sstevel@tonic-gate case DATA_TYPE_INT16: 3147c478bd9Sstevel@tonic-gate se_value->value_type = SE_DATA_TYPE_INT16; 3157c478bd9Sstevel@tonic-gate (void) nvpair_value_int16(nvp, &se_value->value.sv_int16); 3167c478bd9Sstevel@tonic-gate break; 3177c478bd9Sstevel@tonic-gate case DATA_TYPE_UINT16: 3187c478bd9Sstevel@tonic-gate se_value->value_type = SE_DATA_TYPE_UINT16; 3197c478bd9Sstevel@tonic-gate (void) nvpair_value_uint16(nvp, &se_value->value.sv_uint16); 3207c478bd9Sstevel@tonic-gate break; 3217c478bd9Sstevel@tonic-gate case DATA_TYPE_INT32: 3227c478bd9Sstevel@tonic-gate se_value->value_type = SE_DATA_TYPE_INT32; 3237c478bd9Sstevel@tonic-gate (void) nvpair_value_int32(nvp, &se_value->value.sv_int32); 3247c478bd9Sstevel@tonic-gate break; 3257c478bd9Sstevel@tonic-gate case DATA_TYPE_UINT32: 3267c478bd9Sstevel@tonic-gate se_value->value_type = SE_DATA_TYPE_UINT32; 3277c478bd9Sstevel@tonic-gate (void) nvpair_value_uint32(nvp, &se_value->value.sv_uint32); 3287c478bd9Sstevel@tonic-gate break; 3297c478bd9Sstevel@tonic-gate case DATA_TYPE_INT64: 3307c478bd9Sstevel@tonic-gate se_value->value_type = SE_DATA_TYPE_INT64; 3317c478bd9Sstevel@tonic-gate (void) nvpair_value_int64(nvp, &se_value->value.sv_int64); 3327c478bd9Sstevel@tonic-gate break; 3337c478bd9Sstevel@tonic-gate case DATA_TYPE_UINT64: 3347c478bd9Sstevel@tonic-gate se_value->value_type = SE_DATA_TYPE_UINT64; 3357c478bd9Sstevel@tonic-gate (void) nvpair_value_uint64(nvp, &se_value->value.sv_uint64); 3367c478bd9Sstevel@tonic-gate break; 3377c478bd9Sstevel@tonic-gate case DATA_TYPE_STRING: 3387c478bd9Sstevel@tonic-gate se_value->value_type = SE_DATA_TYPE_STRING; 3397c478bd9Sstevel@tonic-gate (void) nvpair_value_string(nvp, &se_value->value.sv_string); 3407c478bd9Sstevel@tonic-gate break; 3417c478bd9Sstevel@tonic-gate case DATA_TYPE_BYTE_ARRAY: 3427c478bd9Sstevel@tonic-gate se_value->value_type = SE_DATA_TYPE_BYTES; 3437c478bd9Sstevel@tonic-gate (void) nvpair_value_byte_array(nvp, 3447c478bd9Sstevel@tonic-gate &se_value->value.sv_bytes.data, 3457c478bd9Sstevel@tonic-gate (uint_t *)&se_value->value.sv_bytes.size); 3467c478bd9Sstevel@tonic-gate break; 3477c478bd9Sstevel@tonic-gate case DATA_TYPE_HRTIME: 3487c478bd9Sstevel@tonic-gate se_value->value_type = SE_DATA_TYPE_TIME; 3497c478bd9Sstevel@tonic-gate (void) nvpair_value_hrtime(nvp, &se_value->value.sv_time); 3507c478bd9Sstevel@tonic-gate break; 3517c478bd9Sstevel@tonic-gate default: 3527c478bd9Sstevel@tonic-gate return (ENOTSUP); 3537c478bd9Sstevel@tonic-gate } 3547c478bd9Sstevel@tonic-gate return (0); 3557c478bd9Sstevel@tonic-gate } 3567c478bd9Sstevel@tonic-gate 3577c478bd9Sstevel@tonic-gate /* 3587c478bd9Sstevel@tonic-gate * sysevent_attr_next - Get next attribute in event attribute list 3597c478bd9Sstevel@tonic-gate */ 3607c478bd9Sstevel@tonic-gate sysevent_attr_t * 3617c478bd9Sstevel@tonic-gate sysevent_attr_next(sysevent_t *ev, sysevent_attr_t *attr) 3627c478bd9Sstevel@tonic-gate { 3637c478bd9Sstevel@tonic-gate nvlist_t *nvl; 3647c478bd9Sstevel@tonic-gate nvpair_t *nvp = attr; 3657c478bd9Sstevel@tonic-gate 3667c478bd9Sstevel@tonic-gate /* all user visible sysevent_t's are unpacked */ 3677c478bd9Sstevel@tonic-gate assert(SE_FLAG(ev) != SE_PACKED_BUF); 3687c478bd9Sstevel@tonic-gate 3697c478bd9Sstevel@tonic-gate if (SE_ATTR_PTR(ev) == (uint64_t)0) { 3707c478bd9Sstevel@tonic-gate return (NULL); 3717c478bd9Sstevel@tonic-gate } 3727c478bd9Sstevel@tonic-gate 37380ab886dSwesolows nvl = (nvlist_t *)(uintptr_t)SE_ATTR_PTR(ev); 3747c478bd9Sstevel@tonic-gate return (nvlist_next_nvpair(nvl, nvp)); 3757c478bd9Sstevel@tonic-gate } 3767c478bd9Sstevel@tonic-gate 3777c478bd9Sstevel@tonic-gate /* 3787c478bd9Sstevel@tonic-gate * sysevent_lookup_attr - Lookup attribute by name and datatype. 3797c478bd9Sstevel@tonic-gate */ 3807c478bd9Sstevel@tonic-gate int 3817c478bd9Sstevel@tonic-gate sysevent_lookup_attr(sysevent_t *ev, char *name, int datatype, 3827c478bd9Sstevel@tonic-gate sysevent_value_t *se_value) 3837c478bd9Sstevel@tonic-gate { 3847c478bd9Sstevel@tonic-gate nvpair_t *nvp; 3857c478bd9Sstevel@tonic-gate nvlist_t *nvl; 3867c478bd9Sstevel@tonic-gate 3877c478bd9Sstevel@tonic-gate assert(SE_FLAG(ev) != SE_PACKED_BUF); 3887c478bd9Sstevel@tonic-gate 3897c478bd9Sstevel@tonic-gate if (SE_ATTR_PTR(ev) == (uint64_t)0) { 3907c478bd9Sstevel@tonic-gate return (ENOENT); 3917c478bd9Sstevel@tonic-gate } 3927c478bd9Sstevel@tonic-gate 3937c478bd9Sstevel@tonic-gate /* 3947c478bd9Sstevel@tonic-gate * sysevent matches on both name and datatype 3957c478bd9Sstevel@tonic-gate * nvlist_look mataches name only. So we walk 3967c478bd9Sstevel@tonic-gate * nvlist manually here. 3977c478bd9Sstevel@tonic-gate */ 39880ab886dSwesolows nvl = (nvlist_t *)(uintptr_t)SE_ATTR_PTR(ev); 3997c478bd9Sstevel@tonic-gate nvp = nvlist_next_nvpair(nvl, NULL); 4007c478bd9Sstevel@tonic-gate while (nvp) { 4017c478bd9Sstevel@tonic-gate if ((strcmp(name, nvpair_name(nvp)) == 0) && 4027c478bd9Sstevel@tonic-gate (sysevent_attr_value(nvp, se_value) == 0) && 4037c478bd9Sstevel@tonic-gate (se_value->value_type == datatype)) 4047c478bd9Sstevel@tonic-gate return (0); 4057c478bd9Sstevel@tonic-gate nvp = nvlist_next_nvpair(nvl, nvp); 4067c478bd9Sstevel@tonic-gate } 4077c478bd9Sstevel@tonic-gate return (ENOENT); 4087c478bd9Sstevel@tonic-gate } 4097c478bd9Sstevel@tonic-gate 4107c478bd9Sstevel@tonic-gate /* Routines to extract event header information */ 4117c478bd9Sstevel@tonic-gate 4127c478bd9Sstevel@tonic-gate /* 4137c478bd9Sstevel@tonic-gate * sysevent_get_class - Get class id 4147c478bd9Sstevel@tonic-gate */ 4157c478bd9Sstevel@tonic-gate int 4167c478bd9Sstevel@tonic-gate sysevent_get_class(sysevent_t *ev) 4177c478bd9Sstevel@tonic-gate { 4187c478bd9Sstevel@tonic-gate return (SE_CLASS(ev)); 4197c478bd9Sstevel@tonic-gate } 4207c478bd9Sstevel@tonic-gate 4217c478bd9Sstevel@tonic-gate /* 4227c478bd9Sstevel@tonic-gate * sysevent_get_subclass - Get subclass id 4237c478bd9Sstevel@tonic-gate */ 4247c478bd9Sstevel@tonic-gate int 4257c478bd9Sstevel@tonic-gate sysevent_get_subclass(sysevent_t *ev) 4267c478bd9Sstevel@tonic-gate { 4277c478bd9Sstevel@tonic-gate return (SE_SUBCLASS(ev)); 4287c478bd9Sstevel@tonic-gate } 4297c478bd9Sstevel@tonic-gate 4307c478bd9Sstevel@tonic-gate /* 4317c478bd9Sstevel@tonic-gate * sysevent_get_class_name - Get class name string 4327c478bd9Sstevel@tonic-gate */ 4337c478bd9Sstevel@tonic-gate char * 4347c478bd9Sstevel@tonic-gate sysevent_get_class_name(sysevent_t *ev) 4357c478bd9Sstevel@tonic-gate { 4367c478bd9Sstevel@tonic-gate return (SE_CLASS_NAME(ev)); 4377c478bd9Sstevel@tonic-gate } 4387c478bd9Sstevel@tonic-gate 4397c478bd9Sstevel@tonic-gate typedef enum { 4407c478bd9Sstevel@tonic-gate PUB_VEND, 4417c478bd9Sstevel@tonic-gate PUB_KEYWD, 4427c478bd9Sstevel@tonic-gate PUB_NAME, 4437c478bd9Sstevel@tonic-gate PUB_PID 4447c478bd9Sstevel@tonic-gate } se_pub_id_t; 4457c478bd9Sstevel@tonic-gate 4467c478bd9Sstevel@tonic-gate /* 4477c478bd9Sstevel@tonic-gate * sysevent_get_pub - Get publisher name string 4487c478bd9Sstevel@tonic-gate */ 4497c478bd9Sstevel@tonic-gate char * 4507c478bd9Sstevel@tonic-gate sysevent_get_pub(sysevent_t *ev) 4517c478bd9Sstevel@tonic-gate { 4527c478bd9Sstevel@tonic-gate return (SE_PUB_NAME(ev)); 4537c478bd9Sstevel@tonic-gate } 4547c478bd9Sstevel@tonic-gate 4557c478bd9Sstevel@tonic-gate /* 4567c478bd9Sstevel@tonic-gate * Get the requested string pointed by the token. 4577c478bd9Sstevel@tonic-gate * 4587c478bd9Sstevel@tonic-gate * Return NULL if not found or for insufficient memory. 4597c478bd9Sstevel@tonic-gate */ 4607c478bd9Sstevel@tonic-gate static char * 4617c478bd9Sstevel@tonic-gate parse_pub_id(sysevent_t *ev, se_pub_id_t token) 4627c478bd9Sstevel@tonic-gate { 4637c478bd9Sstevel@tonic-gate int i; 4647c478bd9Sstevel@tonic-gate char *pub_id, *pub_element, *str, *next; 4657c478bd9Sstevel@tonic-gate 4667c478bd9Sstevel@tonic-gate next = pub_id = strdup(sysevent_get_pub(ev)); 4677c478bd9Sstevel@tonic-gate for (i = 0; i <= token; ++i) { 4687c478bd9Sstevel@tonic-gate str = strtok_r(next, ":", &next); 4697c478bd9Sstevel@tonic-gate if (str == NULL) { 4707c478bd9Sstevel@tonic-gate free(pub_id); 4717c478bd9Sstevel@tonic-gate return (NULL); 4727c478bd9Sstevel@tonic-gate } 4737c478bd9Sstevel@tonic-gate } 4747c478bd9Sstevel@tonic-gate 4757c478bd9Sstevel@tonic-gate pub_element = strdup(str); 4767c478bd9Sstevel@tonic-gate free(pub_id); 4777c478bd9Sstevel@tonic-gate return (pub_element); 4787c478bd9Sstevel@tonic-gate } 4797c478bd9Sstevel@tonic-gate 4807c478bd9Sstevel@tonic-gate /* 4817c478bd9Sstevel@tonic-gate * Return a pointer to the string following the token 4827c478bd9Sstevel@tonic-gate * 4837c478bd9Sstevel@tonic-gate * Note: This is a dedicated function for parsing 4847c478bd9Sstevel@tonic-gate * publisher strings and not for general purpose. 4857c478bd9Sstevel@tonic-gate */ 4867c478bd9Sstevel@tonic-gate static const char * 4877c478bd9Sstevel@tonic-gate pub_idx(const char *pstr, int token) 4887c478bd9Sstevel@tonic-gate { 4897c478bd9Sstevel@tonic-gate int i; 4907c478bd9Sstevel@tonic-gate 4917c478bd9Sstevel@tonic-gate for (i = 1; i <= token; i++) { 4927c478bd9Sstevel@tonic-gate if ((pstr = index(pstr, ':')) == NULL) 4937c478bd9Sstevel@tonic-gate return (NULL); 4947c478bd9Sstevel@tonic-gate pstr++; 4957c478bd9Sstevel@tonic-gate } 4967c478bd9Sstevel@tonic-gate 4977c478bd9Sstevel@tonic-gate /* String might be empty */ 4987c478bd9Sstevel@tonic-gate if (pstr) { 4997c478bd9Sstevel@tonic-gate if (*pstr == '\0' || *pstr == ':') 5007c478bd9Sstevel@tonic-gate return (NULL); 5017c478bd9Sstevel@tonic-gate } 5027c478bd9Sstevel@tonic-gate return (pstr); 5037c478bd9Sstevel@tonic-gate } 5047c478bd9Sstevel@tonic-gate 5057c478bd9Sstevel@tonic-gate char * 5067c478bd9Sstevel@tonic-gate sysevent_get_vendor_name(sysevent_t *ev) 5077c478bd9Sstevel@tonic-gate { 5087c478bd9Sstevel@tonic-gate return (parse_pub_id(ev, PUB_VEND)); 5097c478bd9Sstevel@tonic-gate } 5107c478bd9Sstevel@tonic-gate 5117c478bd9Sstevel@tonic-gate char * 5127c478bd9Sstevel@tonic-gate sysevent_get_pub_name(sysevent_t *ev) 5137c478bd9Sstevel@tonic-gate { 5147c478bd9Sstevel@tonic-gate return (parse_pub_id(ev, PUB_NAME)); 5157c478bd9Sstevel@tonic-gate } 5167c478bd9Sstevel@tonic-gate 5177c478bd9Sstevel@tonic-gate /* 5187c478bd9Sstevel@tonic-gate * Provide the pid encoded in the publisher string 5197c478bd9Sstevel@tonic-gate * w/o allocating any resouces. 5207c478bd9Sstevel@tonic-gate */ 5217c478bd9Sstevel@tonic-gate void 5227c478bd9Sstevel@tonic-gate sysevent_get_pid(sysevent_t *ev, pid_t *pid) 5237c478bd9Sstevel@tonic-gate { 5247c478bd9Sstevel@tonic-gate const char *part_str; 5257c478bd9Sstevel@tonic-gate const char *pub_str = sysevent_get_pub(ev); 5267c478bd9Sstevel@tonic-gate 5277c478bd9Sstevel@tonic-gate *pid = (pid_t)SE_KERN_PID; 5287c478bd9Sstevel@tonic-gate 5297c478bd9Sstevel@tonic-gate part_str = pub_idx(pub_str, PUB_KEYWD); 5307c478bd9Sstevel@tonic-gate if (part_str != NULL && strstr(part_str, SE_KERN_PUB) != NULL) 5317c478bd9Sstevel@tonic-gate return; 5327c478bd9Sstevel@tonic-gate 5337c478bd9Sstevel@tonic-gate if ((part_str = pub_idx(pub_str, PUB_PID)) == NULL) 5347c478bd9Sstevel@tonic-gate return; 5357c478bd9Sstevel@tonic-gate 5367c478bd9Sstevel@tonic-gate *pid = (pid_t)atoi(part_str); 5377c478bd9Sstevel@tonic-gate } 5387c478bd9Sstevel@tonic-gate 5397c478bd9Sstevel@tonic-gate /* 5407c478bd9Sstevel@tonic-gate * sysevent_get_subclass_name - Get subclass name string 5417c478bd9Sstevel@tonic-gate */ 5427c478bd9Sstevel@tonic-gate char * 5437c478bd9Sstevel@tonic-gate sysevent_get_subclass_name(sysevent_t *ev) 5447c478bd9Sstevel@tonic-gate { 5457c478bd9Sstevel@tonic-gate return (SE_SUBCLASS_NAME(ev)); 5467c478bd9Sstevel@tonic-gate } 5477c478bd9Sstevel@tonic-gate 5487c478bd9Sstevel@tonic-gate /* 5497c478bd9Sstevel@tonic-gate * sysevent_get_seq - Get event sequence id 5507c478bd9Sstevel@tonic-gate */ 5517c478bd9Sstevel@tonic-gate uint64_t 5527c478bd9Sstevel@tonic-gate sysevent_get_seq(sysevent_t *ev) 5537c478bd9Sstevel@tonic-gate { 5547c478bd9Sstevel@tonic-gate return (SE_SEQ(ev)); 5557c478bd9Sstevel@tonic-gate } 5567c478bd9Sstevel@tonic-gate 5577c478bd9Sstevel@tonic-gate /* 5587c478bd9Sstevel@tonic-gate * sysevent_get_time - Get event timestamp 5597c478bd9Sstevel@tonic-gate */ 5607c478bd9Sstevel@tonic-gate void 5617c478bd9Sstevel@tonic-gate sysevent_get_time(sysevent_t *ev, hrtime_t *etime) 5627c478bd9Sstevel@tonic-gate { 5637c478bd9Sstevel@tonic-gate *etime = SE_TIME(ev); 5647c478bd9Sstevel@tonic-gate } 5657c478bd9Sstevel@tonic-gate 5667c478bd9Sstevel@tonic-gate /* 5677c478bd9Sstevel@tonic-gate * sysevent_get_size - Get event buffer size 5687c478bd9Sstevel@tonic-gate */ 5697c478bd9Sstevel@tonic-gate size_t 5707c478bd9Sstevel@tonic-gate sysevent_get_size(sysevent_t *ev) 5717c478bd9Sstevel@tonic-gate { 5727c478bd9Sstevel@tonic-gate return ((size_t)SE_SIZE(ev)); 5737c478bd9Sstevel@tonic-gate } 5747c478bd9Sstevel@tonic-gate 5757c478bd9Sstevel@tonic-gate /* 5767c478bd9Sstevel@tonic-gate * The following routines are used by devfsadm_mod.c to propagate event 5777c478bd9Sstevel@tonic-gate * buffers to devfsadmd. These routines will serve as the basis for 5787c478bd9Sstevel@tonic-gate * event channel publication and subscription. 5797c478bd9Sstevel@tonic-gate */ 5807c478bd9Sstevel@tonic-gate 5817c478bd9Sstevel@tonic-gate /* 5827c478bd9Sstevel@tonic-gate * sysevent_alloc_event - 5837c478bd9Sstevel@tonic-gate * allocate a sysevent buffer for sending through an established event 5847c478bd9Sstevel@tonic-gate * channel. 5857c478bd9Sstevel@tonic-gate */ 5867c478bd9Sstevel@tonic-gate sysevent_t * 5877c478bd9Sstevel@tonic-gate sysevent_alloc_event(char *class, char *subclass, char *vendor, char *pub_name, 5887c478bd9Sstevel@tonic-gate nvlist_t *attr_list) 5897c478bd9Sstevel@tonic-gate { 5907c478bd9Sstevel@tonic-gate int class_sz, subclass_sz, pub_sz; 5917c478bd9Sstevel@tonic-gate char *pub_id; 5927c478bd9Sstevel@tonic-gate sysevent_t *ev; 5937c478bd9Sstevel@tonic-gate 5947c478bd9Sstevel@tonic-gate if ((class == NULL) || (subclass == NULL) || (vendor == NULL) || 5957c478bd9Sstevel@tonic-gate (pub_name == NULL)) { 5967c478bd9Sstevel@tonic-gate errno = EINVAL; 5977c478bd9Sstevel@tonic-gate return (NULL); 5987c478bd9Sstevel@tonic-gate } 5997c478bd9Sstevel@tonic-gate 6007c478bd9Sstevel@tonic-gate class_sz = strlen(class) + 1; 6017c478bd9Sstevel@tonic-gate subclass_sz = strlen(subclass) + 1; 6027c478bd9Sstevel@tonic-gate if ((class_sz > MAX_CLASS_LEN) || 6037c478bd9Sstevel@tonic-gate (subclass_sz > MAX_SUBCLASS_LEN)) { 6047c478bd9Sstevel@tonic-gate errno = EINVAL; 6057c478bd9Sstevel@tonic-gate return (NULL); 6067c478bd9Sstevel@tonic-gate } 6077c478bd9Sstevel@tonic-gate 6087c478bd9Sstevel@tonic-gate /* 6097c478bd9Sstevel@tonic-gate * Calculate the publisher size plus string seperators and maximum 6107c478bd9Sstevel@tonic-gate * pid characters 6117c478bd9Sstevel@tonic-gate */ 6127c478bd9Sstevel@tonic-gate pub_sz = strlen(vendor) + sizeof (SE_USR_PUB) + strlen(pub_name) + 14; 6137c478bd9Sstevel@tonic-gate if (pub_sz > MAX_PUB_LEN) { 6147c478bd9Sstevel@tonic-gate errno = EINVAL; 6157c478bd9Sstevel@tonic-gate return (NULL); 6167c478bd9Sstevel@tonic-gate } 6177c478bd9Sstevel@tonic-gate pub_id = malloc(pub_sz); 6187c478bd9Sstevel@tonic-gate if (pub_id == NULL) { 6197c478bd9Sstevel@tonic-gate errno = ENOMEM; 6207c478bd9Sstevel@tonic-gate return (NULL); 6217c478bd9Sstevel@tonic-gate } 6227c478bd9Sstevel@tonic-gate if (snprintf(pub_id, pub_sz, "%s:%s%s:%d", vendor, SE_USR_PUB, 6237c478bd9Sstevel@tonic-gate pub_name, (int)getpid()) >= pub_sz) { 6247c478bd9Sstevel@tonic-gate free(pub_id); 6257c478bd9Sstevel@tonic-gate errno = EINVAL; 6267c478bd9Sstevel@tonic-gate return (NULL); 6277c478bd9Sstevel@tonic-gate } 6287c478bd9Sstevel@tonic-gate pub_sz = strlen(pub_id) + 1; 6297c478bd9Sstevel@tonic-gate 6307c478bd9Sstevel@tonic-gate ev = sysevent_alloc(class, class_sz, subclass, subclass_sz, 6317c478bd9Sstevel@tonic-gate pub_id, pub_sz, attr_list); 6327c478bd9Sstevel@tonic-gate free(pub_id); 6337c478bd9Sstevel@tonic-gate if (ev == NULL) { 6347c478bd9Sstevel@tonic-gate errno = ENOMEM; 6357c478bd9Sstevel@tonic-gate return (NULL); 6367c478bd9Sstevel@tonic-gate } 6377c478bd9Sstevel@tonic-gate 6387c478bd9Sstevel@tonic-gate return (ev); 6397c478bd9Sstevel@tonic-gate } 6407c478bd9Sstevel@tonic-gate 6417c478bd9Sstevel@tonic-gate /* 6427c478bd9Sstevel@tonic-gate * se_unpack - unpack nvlist to a searchable list. 6437c478bd9Sstevel@tonic-gate * If already unpacked, will do a dup. 6447c478bd9Sstevel@tonic-gate */ 6457c478bd9Sstevel@tonic-gate static sysevent_t * 6467c478bd9Sstevel@tonic-gate se_unpack(sysevent_t *ev) 6477c478bd9Sstevel@tonic-gate { 6487c478bd9Sstevel@tonic-gate caddr_t attr; 6497c478bd9Sstevel@tonic-gate size_t attr_len; 6507c478bd9Sstevel@tonic-gate nvlist_t *attrp = NULL; 6517c478bd9Sstevel@tonic-gate uint64_t attr_offset; 6527c478bd9Sstevel@tonic-gate sysevent_t *copy; 6537c478bd9Sstevel@tonic-gate 6547c478bd9Sstevel@tonic-gate assert(SE_FLAG(ev) == SE_PACKED_BUF); 6557c478bd9Sstevel@tonic-gate 6567c478bd9Sstevel@tonic-gate /* Copy event header information */ 6577c478bd9Sstevel@tonic-gate attr_offset = SE_ATTR_OFF(ev); 6587c478bd9Sstevel@tonic-gate copy = calloc(1, attr_offset); 6597c478bd9Sstevel@tonic-gate if (copy == NULL) 6607c478bd9Sstevel@tonic-gate return (NULL); 6617c478bd9Sstevel@tonic-gate bcopy(ev, copy, attr_offset); 6627c478bd9Sstevel@tonic-gate SE_FLAG(copy) = 0; /* unpacked */ 6637c478bd9Sstevel@tonic-gate 6647c478bd9Sstevel@tonic-gate /* unpack nvlist */ 6657c478bd9Sstevel@tonic-gate attr = (caddr_t)ev + attr_offset; 6667c478bd9Sstevel@tonic-gate attr_len = SE_SIZE(ev) - attr_offset; 6677c478bd9Sstevel@tonic-gate if (attr_len == 0) { 6687c478bd9Sstevel@tonic-gate return (copy); 6697c478bd9Sstevel@tonic-gate } 6707c478bd9Sstevel@tonic-gate if (nvlist_unpack(attr, attr_len, &attrp, 0) != 0) { 6717c478bd9Sstevel@tonic-gate free(copy); 6727c478bd9Sstevel@tonic-gate return (NULL); 6737c478bd9Sstevel@tonic-gate } 6747c478bd9Sstevel@tonic-gate 6757c478bd9Sstevel@tonic-gate SE_ATTR_PTR(copy) = (uintptr_t)attrp; 6767c478bd9Sstevel@tonic-gate return (copy); 6777c478bd9Sstevel@tonic-gate } 6787c478bd9Sstevel@tonic-gate 6797c478bd9Sstevel@tonic-gate /* 6807c478bd9Sstevel@tonic-gate * se_print - Prints elements in an event buffer 6817c478bd9Sstevel@tonic-gate */ 6827c478bd9Sstevel@tonic-gate void 6837c478bd9Sstevel@tonic-gate se_print(FILE *fp, sysevent_t *ev) 6847c478bd9Sstevel@tonic-gate { 6857c478bd9Sstevel@tonic-gate char *vendor, *pub; 6867c478bd9Sstevel@tonic-gate pid_t pid; 6877c478bd9Sstevel@tonic-gate hrtime_t hrt; 6887c478bd9Sstevel@tonic-gate nvlist_t *attr_list = NULL; 6897c478bd9Sstevel@tonic-gate 6907c478bd9Sstevel@tonic-gate (void) sysevent_get_time(ev, &hrt); 6917c478bd9Sstevel@tonic-gate (void) fprintf(fp, "received sysevent id = 0X%llx:%llx\n", 6927c478bd9Sstevel@tonic-gate hrt, (longlong_t)sysevent_get_seq(ev)); 6937c478bd9Sstevel@tonic-gate (void) fprintf(fp, "\tclass = %s\n", sysevent_get_class_name(ev)); 6947c478bd9Sstevel@tonic-gate (void) fprintf(fp, "\tsubclass = %s\n", sysevent_get_subclass_name(ev)); 6957c478bd9Sstevel@tonic-gate if ((vendor = sysevent_get_vendor_name(ev)) != NULL) { 6967c478bd9Sstevel@tonic-gate (void) fprintf(fp, "\tvendor = %s\n", vendor); 6977c478bd9Sstevel@tonic-gate free(vendor); 6987c478bd9Sstevel@tonic-gate } 6997c478bd9Sstevel@tonic-gate if ((pub = sysevent_get_pub_name(ev)) != NULL) { 7007c478bd9Sstevel@tonic-gate sysevent_get_pid(ev, &pid); 7017c478bd9Sstevel@tonic-gate (void) fprintf(fp, "\tpublisher = %s:%d\n", pub, (int)pid); 7027c478bd9Sstevel@tonic-gate free(pub); 7037c478bd9Sstevel@tonic-gate } 7047c478bd9Sstevel@tonic-gate 7057c478bd9Sstevel@tonic-gate if (sysevent_get_attr_list(ev, &attr_list) == 0 && attr_list != NULL) { 7067c478bd9Sstevel@tonic-gate nvlist_print(fp, attr_list); 7077c478bd9Sstevel@tonic-gate nvlist_free(attr_list); 7087c478bd9Sstevel@tonic-gate } 7097c478bd9Sstevel@tonic-gate } 7107c478bd9Sstevel@tonic-gate 7117c478bd9Sstevel@tonic-gate /* 7127c478bd9Sstevel@tonic-gate * The following routines are provided to support establishment and use 7137c478bd9Sstevel@tonic-gate * of sysevent channels. A sysevent channel is established between 7147c478bd9Sstevel@tonic-gate * publishers and subscribers of sysevents for an agreed upon channel name. 7157c478bd9Sstevel@tonic-gate * These routines currently support sysevent channels between user-level 7167c478bd9Sstevel@tonic-gate * applications running on the same system. 7177c478bd9Sstevel@tonic-gate * 7187c478bd9Sstevel@tonic-gate * Sysevent channels may be created by a single publisher or subscriber process. 7197c478bd9Sstevel@tonic-gate * Once established, up to MAX_SUBSRCIBERS subscribers may subscribe interest in 7207c478bd9Sstevel@tonic-gate * receiving sysevent notifications on the named channel. At present, only 7217c478bd9Sstevel@tonic-gate * one publisher is allowed per sysevent channel. 7227c478bd9Sstevel@tonic-gate * 7237c478bd9Sstevel@tonic-gate * The registration information for each channel is kept in the kernel. A 7247c478bd9Sstevel@tonic-gate * kernel-based registration was chosen for persistence and reliability reasons. 7257c478bd9Sstevel@tonic-gate * If either a publisher or a subscriber exits for any reason, the channel 7267c478bd9Sstevel@tonic-gate * properties are maintained until all publishers and subscribers have exited. 7277c478bd9Sstevel@tonic-gate * Additionally, an in-kernel registration allows the API to be extended to 7287c478bd9Sstevel@tonic-gate * include kernel subscribers as well as userland subscribers in the future. 7297c478bd9Sstevel@tonic-gate * 7307c478bd9Sstevel@tonic-gate * To insure fast lookup of subscriptions, a cached copy of the registration 7317c478bd9Sstevel@tonic-gate * is kept and maintained for the publisher process. Updates are made 7327c478bd9Sstevel@tonic-gate * everytime a change is made in the kernel. Changes to the registration are 7337c478bd9Sstevel@tonic-gate * expected to be infrequent. 7347c478bd9Sstevel@tonic-gate * 7357c478bd9Sstevel@tonic-gate * Channel communication between publisher and subscriber processes is 7367c478bd9Sstevel@tonic-gate * implemented primarily via doors. Each publisher creates a door for 7377c478bd9Sstevel@tonic-gate * registration notifications and each subscriber creates a door for event 7387c478bd9Sstevel@tonic-gate * delivery. 7397c478bd9Sstevel@tonic-gate * 7407c478bd9Sstevel@tonic-gate * Most of these routines are used by syseventd(1M), the sysevent publisher 7417c478bd9Sstevel@tonic-gate * for the syseventd channel. Processes wishing to receive sysevent 7427c478bd9Sstevel@tonic-gate * notifications from syseventd may use a set of public 7437c478bd9Sstevel@tonic-gate * APIs designed to subscribe to syseventd sysevents. The subscription 7447c478bd9Sstevel@tonic-gate * APIs are implemented in accordance with PSARC/2001/076. 7457c478bd9Sstevel@tonic-gate * 7467c478bd9Sstevel@tonic-gate */ 7477c478bd9Sstevel@tonic-gate 7487c478bd9Sstevel@tonic-gate /* 7497c478bd9Sstevel@tonic-gate * Door handlers for the channel subscribers 7507c478bd9Sstevel@tonic-gate */ 7517c478bd9Sstevel@tonic-gate 7527c478bd9Sstevel@tonic-gate /* 7537c478bd9Sstevel@tonic-gate * subscriber_event_handler - generic event handling wrapper for subscribers 7547c478bd9Sstevel@tonic-gate * This handler is used to process incoming sysevent 7557c478bd9Sstevel@tonic-gate * notifications from channel publishers. 7567c478bd9Sstevel@tonic-gate * It is created as a seperate thread in each subscriber 7577c478bd9Sstevel@tonic-gate * process per subscription. 7587c478bd9Sstevel@tonic-gate */ 7597c478bd9Sstevel@tonic-gate static void 7607c478bd9Sstevel@tonic-gate subscriber_event_handler(sysevent_handle_t *shp) 7617c478bd9Sstevel@tonic-gate { 7627c478bd9Sstevel@tonic-gate subscriber_priv_t *sub_info; 7637c478bd9Sstevel@tonic-gate sysevent_queue_t *evqp; 7647c478bd9Sstevel@tonic-gate 7657c478bd9Sstevel@tonic-gate sub_info = (subscriber_priv_t *)SH_PRIV_DATA(shp); 7667c478bd9Sstevel@tonic-gate 7677c478bd9Sstevel@tonic-gate (void) mutex_lock(&sub_info->sp_qlock); 7687c478bd9Sstevel@tonic-gate for (;;) { 7697c478bd9Sstevel@tonic-gate while (sub_info->sp_evq_head == NULL && SH_BOUND(shp)) { 7707c478bd9Sstevel@tonic-gate (void) cond_wait(&sub_info->sp_cv, &sub_info->sp_qlock); 7717c478bd9Sstevel@tonic-gate } 7727c478bd9Sstevel@tonic-gate evqp = sub_info->sp_evq_head; 7737c478bd9Sstevel@tonic-gate while (evqp) { 7747c478bd9Sstevel@tonic-gate (void) mutex_unlock(&sub_info->sp_qlock); 7757c478bd9Sstevel@tonic-gate (void) sub_info->sp_func(evqp->sq_ev); 7767c478bd9Sstevel@tonic-gate (void) mutex_lock(&sub_info->sp_qlock); 7777c478bd9Sstevel@tonic-gate sub_info->sp_evq_head = sub_info->sp_evq_head->sq_next; 7787c478bd9Sstevel@tonic-gate free(evqp->sq_ev); 7797c478bd9Sstevel@tonic-gate free(evqp); 7807c478bd9Sstevel@tonic-gate evqp = sub_info->sp_evq_head; 7817c478bd9Sstevel@tonic-gate } 7827c478bd9Sstevel@tonic-gate if (!SH_BOUND(shp)) { 7837c478bd9Sstevel@tonic-gate (void) mutex_unlock(&sub_info->sp_qlock); 7847c478bd9Sstevel@tonic-gate return; 7857c478bd9Sstevel@tonic-gate } 7867c478bd9Sstevel@tonic-gate } 7877c478bd9Sstevel@tonic-gate 7887c478bd9Sstevel@tonic-gate /* NOTREACHED */ 7897c478bd9Sstevel@tonic-gate } 7907c478bd9Sstevel@tonic-gate 7917c478bd9Sstevel@tonic-gate /* 7927c478bd9Sstevel@tonic-gate * Data structure used to communicate event subscription cache updates 7937c478bd9Sstevel@tonic-gate * to publishers via a registration door 7947c478bd9Sstevel@tonic-gate */ 7957c478bd9Sstevel@tonic-gate struct reg_args { 7967c478bd9Sstevel@tonic-gate uint32_t ra_sub_id; 7977c478bd9Sstevel@tonic-gate uint32_t ra_op; 7987c478bd9Sstevel@tonic-gate uint64_t ra_buf_ptr; 7997c478bd9Sstevel@tonic-gate }; 8007c478bd9Sstevel@tonic-gate 8017c478bd9Sstevel@tonic-gate 8027c478bd9Sstevel@tonic-gate /* 8037c478bd9Sstevel@tonic-gate * event_deliver_service - generic event delivery service routine. This routine 8047c478bd9Sstevel@tonic-gate * is called in response to a door call to post an event. 8057c478bd9Sstevel@tonic-gate * 8067c478bd9Sstevel@tonic-gate */ 807*49b225e1SGavin Maltby /*ARGSUSED*/ 8087c478bd9Sstevel@tonic-gate static void 8097c478bd9Sstevel@tonic-gate event_deliver_service(void *cookie, char *args, size_t alen, 8107c478bd9Sstevel@tonic-gate door_desc_t *ddp, uint_t ndid) 8117c478bd9Sstevel@tonic-gate { 8127c478bd9Sstevel@tonic-gate int ret = 0; 8137c478bd9Sstevel@tonic-gate subscriber_priv_t *sub_info; 8147c478bd9Sstevel@tonic-gate sysevent_handle_t *shp; 8157c478bd9Sstevel@tonic-gate sysevent_queue_t *new_eq; 8167c478bd9Sstevel@tonic-gate 8177c478bd9Sstevel@tonic-gate if (args == NULL || alen < sizeof (uint32_t)) { 8187c478bd9Sstevel@tonic-gate ret = EINVAL; 8197c478bd9Sstevel@tonic-gate goto return_from_door; 8207c478bd9Sstevel@tonic-gate } 8217c478bd9Sstevel@tonic-gate 8227c478bd9Sstevel@tonic-gate /* Publisher checking on subscriber */ 8237c478bd9Sstevel@tonic-gate if (alen == sizeof (uint32_t)) { 8247c478bd9Sstevel@tonic-gate ret = 0; 8257c478bd9Sstevel@tonic-gate goto return_from_door; 8267c478bd9Sstevel@tonic-gate } 8277c478bd9Sstevel@tonic-gate 8287c478bd9Sstevel@tonic-gate shp = (sysevent_handle_t *)cookie; 8297c478bd9Sstevel@tonic-gate if (shp == NULL) { 8307c478bd9Sstevel@tonic-gate ret = EBADF; 8317c478bd9Sstevel@tonic-gate goto return_from_door; 8327c478bd9Sstevel@tonic-gate } 8337c478bd9Sstevel@tonic-gate 8347c478bd9Sstevel@tonic-gate /* 8357c478bd9Sstevel@tonic-gate * Mustn't block if we are trying to update the registration with 8367c478bd9Sstevel@tonic-gate * the publisher 8377c478bd9Sstevel@tonic-gate */ 8387c478bd9Sstevel@tonic-gate if (mutex_trylock(SH_LOCK(shp)) != 0) { 8397c478bd9Sstevel@tonic-gate ret = EAGAIN; 8407c478bd9Sstevel@tonic-gate goto return_from_door; 8417c478bd9Sstevel@tonic-gate } 8427c478bd9Sstevel@tonic-gate 8437c478bd9Sstevel@tonic-gate if (!SH_BOUND(shp)) { 8447c478bd9Sstevel@tonic-gate ret = EBADF; 8457c478bd9Sstevel@tonic-gate (void) mutex_unlock(SH_LOCK(shp)); 8467c478bd9Sstevel@tonic-gate goto return_from_door; 8477c478bd9Sstevel@tonic-gate } 8487c478bd9Sstevel@tonic-gate 8497c478bd9Sstevel@tonic-gate sub_info = (subscriber_priv_t *)SH_PRIV_DATA(shp); 8507c478bd9Sstevel@tonic-gate if (sub_info == NULL) { 8517c478bd9Sstevel@tonic-gate ret = EBADF; 8527c478bd9Sstevel@tonic-gate (void) mutex_unlock(SH_LOCK(shp)); 8537c478bd9Sstevel@tonic-gate goto return_from_door; 8547c478bd9Sstevel@tonic-gate } 8557c478bd9Sstevel@tonic-gate 8567c478bd9Sstevel@tonic-gate new_eq = (sysevent_queue_t *)calloc(1, 8577c478bd9Sstevel@tonic-gate sizeof (sysevent_queue_t)); 8587c478bd9Sstevel@tonic-gate if (new_eq == NULL) { 8597c478bd9Sstevel@tonic-gate ret = EAGAIN; 8607c478bd9Sstevel@tonic-gate (void) mutex_unlock(SH_LOCK(shp)); 8617c478bd9Sstevel@tonic-gate goto return_from_door; 8627c478bd9Sstevel@tonic-gate } 8637c478bd9Sstevel@tonic-gate 8647c478bd9Sstevel@tonic-gate /* 8657c478bd9Sstevel@tonic-gate * Allocate and copy the event buffer into the subscriber's 8667c478bd9Sstevel@tonic-gate * address space 8677c478bd9Sstevel@tonic-gate */ 8687c478bd9Sstevel@tonic-gate new_eq->sq_ev = calloc(1, alen); 8697c478bd9Sstevel@tonic-gate if (new_eq->sq_ev == NULL) { 8707c478bd9Sstevel@tonic-gate free(new_eq); 8717c478bd9Sstevel@tonic-gate ret = EAGAIN; 8727c478bd9Sstevel@tonic-gate (void) mutex_unlock(SH_LOCK(shp)); 8737c478bd9Sstevel@tonic-gate goto return_from_door; 8747c478bd9Sstevel@tonic-gate } 8757c478bd9Sstevel@tonic-gate (void) bcopy(args, new_eq->sq_ev, alen); 8767c478bd9Sstevel@tonic-gate 8777c478bd9Sstevel@tonic-gate (void) mutex_lock(&sub_info->sp_qlock); 8787c478bd9Sstevel@tonic-gate if (sub_info->sp_evq_head == NULL) { 8797c478bd9Sstevel@tonic-gate sub_info->sp_evq_head = new_eq; 8807c478bd9Sstevel@tonic-gate } else { 8817c478bd9Sstevel@tonic-gate sub_info->sp_evq_tail->sq_next = new_eq; 8827c478bd9Sstevel@tonic-gate } 8837c478bd9Sstevel@tonic-gate sub_info->sp_evq_tail = new_eq; 8847c478bd9Sstevel@tonic-gate 8857c478bd9Sstevel@tonic-gate (void) cond_signal(&sub_info->sp_cv); 8867c478bd9Sstevel@tonic-gate (void) mutex_unlock(&sub_info->sp_qlock); 8877c478bd9Sstevel@tonic-gate (void) mutex_unlock(SH_LOCK(shp)); 8887c478bd9Sstevel@tonic-gate 8897c478bd9Sstevel@tonic-gate return_from_door: 8907c478bd9Sstevel@tonic-gate (void) door_return((void *)&ret, sizeof (ret), NULL, 0); 8917c478bd9Sstevel@tonic-gate (void) door_return(NULL, 0, NULL, 0); 8927c478bd9Sstevel@tonic-gate } 8937c478bd9Sstevel@tonic-gate 8947c478bd9Sstevel@tonic-gate /* 8957c478bd9Sstevel@tonic-gate * Sysevent subscription information is maintained in the kernel. Updates 8967c478bd9Sstevel@tonic-gate * to the in-kernel registration database is expected to be infrequent and 8977c478bd9Sstevel@tonic-gate * offers consistency for publishers and subscribers that may come and go 8987c478bd9Sstevel@tonic-gate * for a given channel. 8997c478bd9Sstevel@tonic-gate * 9007c478bd9Sstevel@tonic-gate * To expedite registration lookups by publishers, a cached copy of the 9017c478bd9Sstevel@tonic-gate * kernel registration database is kept per-channel. Caches are invalidated 9027c478bd9Sstevel@tonic-gate * and refreshed upon state changes to the in-kernel registration database. 9037c478bd9Sstevel@tonic-gate * 9047c478bd9Sstevel@tonic-gate * To prevent stale subscriber data, publishers may remove subsriber 9057c478bd9Sstevel@tonic-gate * registrations from the in-kernel registration database in the event 9067c478bd9Sstevel@tonic-gate * that a particular subscribing process is unresponsive. 9077c478bd9Sstevel@tonic-gate * 9087c478bd9Sstevel@tonic-gate * The following routines provide a mechanism to update publisher and subscriber 9097c478bd9Sstevel@tonic-gate * information for a specified channel. 9107c478bd9Sstevel@tonic-gate */ 9117c478bd9Sstevel@tonic-gate 9127c478bd9Sstevel@tonic-gate /* 9137c478bd9Sstevel@tonic-gate * clnt_deliver_event - Deliver an event through the consumer's event 9147c478bd9Sstevel@tonic-gate * delivery door 9157c478bd9Sstevel@tonic-gate * 9167c478bd9Sstevel@tonic-gate * Returns -1 if message not delivered. With errno set to cause of error. 9177c478bd9Sstevel@tonic-gate * Returns 0 for success with the results returned in posting buffer. 9187c478bd9Sstevel@tonic-gate */ 9197c478bd9Sstevel@tonic-gate static int 9207c478bd9Sstevel@tonic-gate clnt_deliver_event(int service_door, void *data, size_t datalen, 9217c478bd9Sstevel@tonic-gate void *result, size_t rlen) 9227c478bd9Sstevel@tonic-gate { 9237c478bd9Sstevel@tonic-gate int error = 0; 9247c478bd9Sstevel@tonic-gate door_arg_t door_arg; 9257c478bd9Sstevel@tonic-gate 9267c478bd9Sstevel@tonic-gate door_arg.rbuf = result; 9277c478bd9Sstevel@tonic-gate door_arg.rsize = rlen; 9287c478bd9Sstevel@tonic-gate door_arg.data_ptr = data; 9297c478bd9Sstevel@tonic-gate door_arg.data_size = datalen; 9307c478bd9Sstevel@tonic-gate door_arg.desc_ptr = NULL; 9317c478bd9Sstevel@tonic-gate door_arg.desc_num = 0; 9327c478bd9Sstevel@tonic-gate 9337c478bd9Sstevel@tonic-gate /* 9347c478bd9Sstevel@tonic-gate * Make door call 9357c478bd9Sstevel@tonic-gate */ 9367c478bd9Sstevel@tonic-gate while ((error = door_call(service_door, &door_arg)) != 0) { 9377c478bd9Sstevel@tonic-gate if (errno == EAGAIN || errno == EINTR) { 9387c478bd9Sstevel@tonic-gate continue; 9397c478bd9Sstevel@tonic-gate } else { 9407c478bd9Sstevel@tonic-gate error = errno; 9417c478bd9Sstevel@tonic-gate break; 9427c478bd9Sstevel@tonic-gate } 9437c478bd9Sstevel@tonic-gate } 9447c478bd9Sstevel@tonic-gate 9457c478bd9Sstevel@tonic-gate return (error); 9467c478bd9Sstevel@tonic-gate } 9477c478bd9Sstevel@tonic-gate 9487c478bd9Sstevel@tonic-gate static int 9497c478bd9Sstevel@tonic-gate update_publisher_cache(subscriber_priv_t *sub_info, int update_op, 9507c478bd9Sstevel@tonic-gate uint32_t sub_id, size_t datasz, uchar_t *data) 9517c478bd9Sstevel@tonic-gate { 9527c478bd9Sstevel@tonic-gate int pub_fd; 9537c478bd9Sstevel@tonic-gate uint32_t result = 0; 9547c478bd9Sstevel@tonic-gate struct reg_args *rargs; 9557c478bd9Sstevel@tonic-gate 9567c478bd9Sstevel@tonic-gate rargs = (struct reg_args *)calloc(1, sizeof (struct reg_args) + 9577c478bd9Sstevel@tonic-gate datasz); 9587c478bd9Sstevel@tonic-gate if (rargs == NULL) { 9597c478bd9Sstevel@tonic-gate errno = ENOMEM; 9607c478bd9Sstevel@tonic-gate return (-1); 9617c478bd9Sstevel@tonic-gate } 9627c478bd9Sstevel@tonic-gate 9637c478bd9Sstevel@tonic-gate rargs->ra_sub_id = sub_id; 9647c478bd9Sstevel@tonic-gate rargs->ra_op = update_op; 9657c478bd9Sstevel@tonic-gate bcopy(data, (char *)&rargs->ra_buf_ptr, datasz); 9667c478bd9Sstevel@tonic-gate 9677c478bd9Sstevel@tonic-gate pub_fd = open(sub_info->sp_door_name, O_RDONLY); 9687c478bd9Sstevel@tonic-gate (void) clnt_deliver_event(pub_fd, (void *)rargs, 9697c478bd9Sstevel@tonic-gate sizeof (struct reg_args) + datasz, &result, sizeof (result)); 9707c478bd9Sstevel@tonic-gate (void) close(pub_fd); 9717c478bd9Sstevel@tonic-gate 9727c478bd9Sstevel@tonic-gate free(rargs); 9737c478bd9Sstevel@tonic-gate if (result != 0) { 9747c478bd9Sstevel@tonic-gate errno = result; 9757c478bd9Sstevel@tonic-gate return (-1); 9767c478bd9Sstevel@tonic-gate } 9777c478bd9Sstevel@tonic-gate 9787c478bd9Sstevel@tonic-gate return (0); 9797c478bd9Sstevel@tonic-gate } 9807c478bd9Sstevel@tonic-gate 9817c478bd9Sstevel@tonic-gate 9827c478bd9Sstevel@tonic-gate /* 9837c478bd9Sstevel@tonic-gate * update_kernel_registration - update the in-kernel registration for the 9847c478bd9Sstevel@tonic-gate * given channel. 9857c478bd9Sstevel@tonic-gate */ 9867c478bd9Sstevel@tonic-gate static int 9877c478bd9Sstevel@tonic-gate update_kernel_registration(sysevent_handle_t *shp, int update_type, 9887c478bd9Sstevel@tonic-gate int update_op, uint32_t *sub_id, size_t datasz, uchar_t *data) 9897c478bd9Sstevel@tonic-gate { 9907c478bd9Sstevel@tonic-gate int error; 9917c478bd9Sstevel@tonic-gate char *channel_name = SH_CHANNEL_NAME(shp); 9927c478bd9Sstevel@tonic-gate se_pubsub_t udata; 9937c478bd9Sstevel@tonic-gate 9947c478bd9Sstevel@tonic-gate udata.ps_channel_name_len = strlen(channel_name) + 1; 9957c478bd9Sstevel@tonic-gate udata.ps_op = update_op; 9967c478bd9Sstevel@tonic-gate udata.ps_type = update_type; 9977c478bd9Sstevel@tonic-gate udata.ps_buflen = datasz; 9987c478bd9Sstevel@tonic-gate udata.ps_id = *sub_id; 9997c478bd9Sstevel@tonic-gate 10007c478bd9Sstevel@tonic-gate if ((error = modctl(MODEVENTS, (uintptr_t)MODEVENTS_REGISTER_EVENT, 10017c478bd9Sstevel@tonic-gate (uintptr_t)channel_name, (uintptr_t)data, (uintptr_t)&udata, 0)) 10027c478bd9Sstevel@tonic-gate != 0) { 10037c478bd9Sstevel@tonic-gate return (error); 10047c478bd9Sstevel@tonic-gate } 10057c478bd9Sstevel@tonic-gate 10067c478bd9Sstevel@tonic-gate *sub_id = udata.ps_id; 10077c478bd9Sstevel@tonic-gate 10087c478bd9Sstevel@tonic-gate return (error); 10097c478bd9Sstevel@tonic-gate } 10107c478bd9Sstevel@tonic-gate 10117c478bd9Sstevel@tonic-gate /* 10127c478bd9Sstevel@tonic-gate * get_kernel_registration - get the current subscriber registration for 10137c478bd9Sstevel@tonic-gate * the given channel 10147c478bd9Sstevel@tonic-gate */ 10157c478bd9Sstevel@tonic-gate static nvlist_t * 10167c478bd9Sstevel@tonic-gate get_kernel_registration(char *channel_name, uint32_t class_id) 10177c478bd9Sstevel@tonic-gate { 10187c478bd9Sstevel@tonic-gate char *nvlbuf; 10197c478bd9Sstevel@tonic-gate nvlist_t *nvl; 10207c478bd9Sstevel@tonic-gate se_pubsub_t udata; 10217c478bd9Sstevel@tonic-gate 10227c478bd9Sstevel@tonic-gate nvlbuf = calloc(1, MAX_SUBSCRIPTION_SZ); 10237c478bd9Sstevel@tonic-gate if (nvlbuf == NULL) { 10247c478bd9Sstevel@tonic-gate return (NULL); 10257c478bd9Sstevel@tonic-gate } 10267c478bd9Sstevel@tonic-gate 10277c478bd9Sstevel@tonic-gate udata.ps_buflen = MAX_SUBSCRIPTION_SZ; 10287c478bd9Sstevel@tonic-gate udata.ps_channel_name_len = strlen(channel_name) + 1; 10297c478bd9Sstevel@tonic-gate udata.ps_id = class_id; 10307c478bd9Sstevel@tonic-gate udata.ps_op = SE_GET_REGISTRATION; 10317c478bd9Sstevel@tonic-gate udata.ps_type = PUBLISHER; 10327c478bd9Sstevel@tonic-gate 10337c478bd9Sstevel@tonic-gate if (modctl(MODEVENTS, (uintptr_t)MODEVENTS_REGISTER_EVENT, 10347c478bd9Sstevel@tonic-gate (uintptr_t)channel_name, (uintptr_t)nvlbuf, (uintptr_t)&udata, 0) 10357c478bd9Sstevel@tonic-gate != 0) { 10367c478bd9Sstevel@tonic-gate 10377c478bd9Sstevel@tonic-gate /* Need a bigger buffer to hold channel registration */ 10387c478bd9Sstevel@tonic-gate if (errno == EAGAIN) { 10397c478bd9Sstevel@tonic-gate free(nvlbuf); 10407c478bd9Sstevel@tonic-gate nvlbuf = calloc(1, udata.ps_buflen); 10417c478bd9Sstevel@tonic-gate if (nvlbuf == NULL) 10427c478bd9Sstevel@tonic-gate return (NULL); 10437c478bd9Sstevel@tonic-gate 10447c478bd9Sstevel@tonic-gate /* Try again */ 10457c478bd9Sstevel@tonic-gate if (modctl(MODEVENTS, 10467c478bd9Sstevel@tonic-gate (uintptr_t)MODEVENTS_REGISTER_EVENT, 10477c478bd9Sstevel@tonic-gate (uintptr_t)channel_name, (uintptr_t)nvlbuf, 10487c478bd9Sstevel@tonic-gate (uintptr_t)&udata, 0) != 0) { 10497c478bd9Sstevel@tonic-gate free(nvlbuf); 10507c478bd9Sstevel@tonic-gate return (NULL); 10517c478bd9Sstevel@tonic-gate } 10527c478bd9Sstevel@tonic-gate } else { 10537c478bd9Sstevel@tonic-gate free(nvlbuf); 10547c478bd9Sstevel@tonic-gate return (NULL); 10557c478bd9Sstevel@tonic-gate } 10567c478bd9Sstevel@tonic-gate } 10577c478bd9Sstevel@tonic-gate 10587c478bd9Sstevel@tonic-gate if (nvlist_unpack(nvlbuf, udata.ps_buflen, &nvl, 0) != 0) { 10597c478bd9Sstevel@tonic-gate free(nvlbuf); 10607c478bd9Sstevel@tonic-gate return (NULL); 10617c478bd9Sstevel@tonic-gate } 10627c478bd9Sstevel@tonic-gate free(nvlbuf); 10637c478bd9Sstevel@tonic-gate 10647c478bd9Sstevel@tonic-gate return (nvl); 10657c478bd9Sstevel@tonic-gate } 10667c478bd9Sstevel@tonic-gate 10677c478bd9Sstevel@tonic-gate /* 10687c478bd9Sstevel@tonic-gate * The following routines provide a mechanism for publishers to maintain 10697c478bd9Sstevel@tonic-gate * subscriber information. 10707c478bd9Sstevel@tonic-gate */ 10717c478bd9Sstevel@tonic-gate 10727c478bd9Sstevel@tonic-gate static void 10737c478bd9Sstevel@tonic-gate dealloc_subscribers(sysevent_handle_t *shp) 10747c478bd9Sstevel@tonic-gate { 10757c478bd9Sstevel@tonic-gate int i; 10767c478bd9Sstevel@tonic-gate subscriber_data_t *sub; 10777c478bd9Sstevel@tonic-gate 10787c478bd9Sstevel@tonic-gate for (i = 1; i <= MAX_SUBSCRIBERS; ++i) { 10797c478bd9Sstevel@tonic-gate sub = SH_SUBSCRIBER(shp, i); 10807c478bd9Sstevel@tonic-gate if (sub != NULL) { 10817c478bd9Sstevel@tonic-gate free(sub->sd_door_name); 10827c478bd9Sstevel@tonic-gate free(sub); 10837c478bd9Sstevel@tonic-gate } 10847c478bd9Sstevel@tonic-gate SH_SUBSCRIBER(shp, i) = NULL; 10857c478bd9Sstevel@tonic-gate } 10867c478bd9Sstevel@tonic-gate } 10877c478bd9Sstevel@tonic-gate 1088*49b225e1SGavin Maltby /*ARGSUSED*/ 10897c478bd9Sstevel@tonic-gate static int 10907c478bd9Sstevel@tonic-gate alloc_subscriber(sysevent_handle_t *shp, uint32_t sub_id, int oflag) 10917c478bd9Sstevel@tonic-gate { 10927c478bd9Sstevel@tonic-gate subscriber_data_t *sub; 10937c478bd9Sstevel@tonic-gate char door_name[MAXPATHLEN]; 10947c478bd9Sstevel@tonic-gate 10957c478bd9Sstevel@tonic-gate if (SH_SUBSCRIBER(shp, sub_id) != NULL) { 10967c478bd9Sstevel@tonic-gate return (0); 10977c478bd9Sstevel@tonic-gate } 10987c478bd9Sstevel@tonic-gate 10997c478bd9Sstevel@tonic-gate /* Allocate and initialize the subscriber data */ 11007c478bd9Sstevel@tonic-gate sub = (subscriber_data_t *)calloc(1, 11017c478bd9Sstevel@tonic-gate sizeof (subscriber_data_t)); 11027c478bd9Sstevel@tonic-gate if (sub == NULL) { 11037c478bd9Sstevel@tonic-gate return (-1); 11047c478bd9Sstevel@tonic-gate } 11057c478bd9Sstevel@tonic-gate if (snprintf(door_name, MAXPATHLEN, "%s/%d", 11067c478bd9Sstevel@tonic-gate SH_CHANNEL_PATH(shp), sub_id) >= MAXPATHLEN) { 11077c478bd9Sstevel@tonic-gate free(sub); 11087c478bd9Sstevel@tonic-gate return (-1); 11097c478bd9Sstevel@tonic-gate } 11107c478bd9Sstevel@tonic-gate 11117c478bd9Sstevel@tonic-gate sub->sd_flag = ACTIVE; 11127c478bd9Sstevel@tonic-gate sub->sd_door_name = strdup(door_name); 11137c478bd9Sstevel@tonic-gate if (sub->sd_door_name == NULL) { 11147c478bd9Sstevel@tonic-gate free(sub); 11157c478bd9Sstevel@tonic-gate return (-1); 11167c478bd9Sstevel@tonic-gate } 11177c478bd9Sstevel@tonic-gate 11187c478bd9Sstevel@tonic-gate SH_SUBSCRIBER(shp, sub_id) = sub; 11197c478bd9Sstevel@tonic-gate return (0); 11207c478bd9Sstevel@tonic-gate 11217c478bd9Sstevel@tonic-gate } 11227c478bd9Sstevel@tonic-gate 11237c478bd9Sstevel@tonic-gate /* 11247c478bd9Sstevel@tonic-gate * The following routines are used to update and maintain the registration cache 11257c478bd9Sstevel@tonic-gate * for a particular sysevent channel. 11267c478bd9Sstevel@tonic-gate */ 11277c478bd9Sstevel@tonic-gate 11287c478bd9Sstevel@tonic-gate static uint32_t 11297c478bd9Sstevel@tonic-gate hash_func(const char *s) 11307c478bd9Sstevel@tonic-gate { 11317c478bd9Sstevel@tonic-gate uint32_t result = 0; 11327c478bd9Sstevel@tonic-gate uint_t g; 11337c478bd9Sstevel@tonic-gate 11347c478bd9Sstevel@tonic-gate while (*s != '\0') { 11357c478bd9Sstevel@tonic-gate result <<= 4; 11367c478bd9Sstevel@tonic-gate result += (uint32_t)*s++; 11377c478bd9Sstevel@tonic-gate g = result & 0xf0000000; 11387c478bd9Sstevel@tonic-gate if (g != 0) { 11397c478bd9Sstevel@tonic-gate result ^= g >> 24; 11407c478bd9Sstevel@tonic-gate result ^= g; 11417c478bd9Sstevel@tonic-gate } 11427c478bd9Sstevel@tonic-gate } 11437c478bd9Sstevel@tonic-gate 11447c478bd9Sstevel@tonic-gate return (result); 11457c478bd9Sstevel@tonic-gate } 11467c478bd9Sstevel@tonic-gate 11477c478bd9Sstevel@tonic-gate subclass_lst_t * 11487c478bd9Sstevel@tonic-gate cache_find_subclass(class_lst_t *c_list, char *subclass) 11497c478bd9Sstevel@tonic-gate { 11507c478bd9Sstevel@tonic-gate subclass_lst_t *sc_list; 11517c478bd9Sstevel@tonic-gate 11527c478bd9Sstevel@tonic-gate if (c_list == NULL) 11537c478bd9Sstevel@tonic-gate return (NULL); 11547c478bd9Sstevel@tonic-gate 11557c478bd9Sstevel@tonic-gate sc_list = c_list->cl_subclass_list; 11567c478bd9Sstevel@tonic-gate 11577c478bd9Sstevel@tonic-gate while (sc_list != NULL) { 11587c478bd9Sstevel@tonic-gate if (strcmp(sc_list->sl_name, subclass) == 0) { 11597c478bd9Sstevel@tonic-gate return (sc_list); 11607c478bd9Sstevel@tonic-gate } 11617c478bd9Sstevel@tonic-gate sc_list = sc_list->sl_next; 11627c478bd9Sstevel@tonic-gate } 11637c478bd9Sstevel@tonic-gate 11647c478bd9Sstevel@tonic-gate return (NULL); 11657c478bd9Sstevel@tonic-gate } 11667c478bd9Sstevel@tonic-gate 11677c478bd9Sstevel@tonic-gate 11687c478bd9Sstevel@tonic-gate static class_lst_t * 11697c478bd9Sstevel@tonic-gate cache_find_class(sysevent_handle_t *shp, char *class) 11707c478bd9Sstevel@tonic-gate { 11717c478bd9Sstevel@tonic-gate int index; 11727c478bd9Sstevel@tonic-gate class_lst_t *c_list; 11737c478bd9Sstevel@tonic-gate class_lst_t **class_hash = SH_CLASS_HASH(shp); 11747c478bd9Sstevel@tonic-gate 11757c478bd9Sstevel@tonic-gate if (strcmp(class, EC_ALL) == 0) { 11767c478bd9Sstevel@tonic-gate return (class_hash[0]); 11777c478bd9Sstevel@tonic-gate } 11787c478bd9Sstevel@tonic-gate 11797c478bd9Sstevel@tonic-gate index = CLASS_HASH(class); 11807c478bd9Sstevel@tonic-gate c_list = class_hash[index]; 11817c478bd9Sstevel@tonic-gate while (c_list != NULL) { 11827c478bd9Sstevel@tonic-gate if (strcmp(class, c_list->cl_name) == 0) { 11837c478bd9Sstevel@tonic-gate break; 11847c478bd9Sstevel@tonic-gate } 11857c478bd9Sstevel@tonic-gate c_list = c_list->cl_next; 11867c478bd9Sstevel@tonic-gate } 11877c478bd9Sstevel@tonic-gate 11887c478bd9Sstevel@tonic-gate return (c_list); 11897c478bd9Sstevel@tonic-gate } 11907c478bd9Sstevel@tonic-gate 11917c478bd9Sstevel@tonic-gate static int 11927c478bd9Sstevel@tonic-gate cache_insert_subclass(class_lst_t *c_list, char **subclass_names, 11937c478bd9Sstevel@tonic-gate int subclass_num, uint32_t sub_id) 11947c478bd9Sstevel@tonic-gate { 11957c478bd9Sstevel@tonic-gate int i; 11967c478bd9Sstevel@tonic-gate subclass_lst_t *sc_list; 11977c478bd9Sstevel@tonic-gate 11987c478bd9Sstevel@tonic-gate for (i = 0; i < subclass_num; ++i) { 11997c478bd9Sstevel@tonic-gate if ((sc_list = cache_find_subclass(c_list, subclass_names[i])) 12007c478bd9Sstevel@tonic-gate != NULL) { 12017c478bd9Sstevel@tonic-gate sc_list->sl_num[sub_id] = 1; 12027c478bd9Sstevel@tonic-gate } else { 12037c478bd9Sstevel@tonic-gate sc_list = (subclass_lst_t *)calloc(1, 12047c478bd9Sstevel@tonic-gate sizeof (subclass_lst_t)); 12057c478bd9Sstevel@tonic-gate if (sc_list == NULL) 12067c478bd9Sstevel@tonic-gate return (-1); 12077c478bd9Sstevel@tonic-gate 12087c478bd9Sstevel@tonic-gate sc_list->sl_name = strdup(subclass_names[i]); 12097c478bd9Sstevel@tonic-gate if (sc_list->sl_name == NULL) { 12107c478bd9Sstevel@tonic-gate free(sc_list); 12117c478bd9Sstevel@tonic-gate return (-1); 12127c478bd9Sstevel@tonic-gate } 12137c478bd9Sstevel@tonic-gate 12147c478bd9Sstevel@tonic-gate sc_list->sl_num[sub_id] = 1; 12157c478bd9Sstevel@tonic-gate sc_list->sl_next = c_list->cl_subclass_list; 12167c478bd9Sstevel@tonic-gate c_list->cl_subclass_list = sc_list; 12177c478bd9Sstevel@tonic-gate } 12187c478bd9Sstevel@tonic-gate } 12197c478bd9Sstevel@tonic-gate 12207c478bd9Sstevel@tonic-gate return (0); 12217c478bd9Sstevel@tonic-gate } 12227c478bd9Sstevel@tonic-gate 12237c478bd9Sstevel@tonic-gate static int 12247c478bd9Sstevel@tonic-gate cache_insert_class(sysevent_handle_t *shp, char *class, 12257c478bd9Sstevel@tonic-gate char **subclass_names, int subclass_num, uint32_t sub_id) 12267c478bd9Sstevel@tonic-gate { 12277c478bd9Sstevel@tonic-gate class_lst_t *c_list; 12287c478bd9Sstevel@tonic-gate 12297c478bd9Sstevel@tonic-gate if (strcmp(class, EC_ALL) == 0) { 12307c478bd9Sstevel@tonic-gate char *subclass_all = EC_SUB_ALL; 12317c478bd9Sstevel@tonic-gate 12327c478bd9Sstevel@tonic-gate (void) cache_insert_subclass(SH_CLASS_HASH(shp)[0], 12337c478bd9Sstevel@tonic-gate (char **)&subclass_all, 1, sub_id); 12347c478bd9Sstevel@tonic-gate return (0); 12357c478bd9Sstevel@tonic-gate } 12367c478bd9Sstevel@tonic-gate 12377c478bd9Sstevel@tonic-gate /* New class, add to the registration cache */ 12387c478bd9Sstevel@tonic-gate if ((c_list = cache_find_class(shp, class)) == NULL) { 12397c478bd9Sstevel@tonic-gate 12407c478bd9Sstevel@tonic-gate c_list = (class_lst_t *)calloc(1, sizeof (class_lst_t)); 12417c478bd9Sstevel@tonic-gate if (c_list == NULL) { 12427c478bd9Sstevel@tonic-gate return (1); 12437c478bd9Sstevel@tonic-gate } 12447c478bd9Sstevel@tonic-gate c_list->cl_name = strdup(class); 12457c478bd9Sstevel@tonic-gate if (c_list->cl_name == NULL) { 12467c478bd9Sstevel@tonic-gate free(c_list); 12477c478bd9Sstevel@tonic-gate return (1); 12487c478bd9Sstevel@tonic-gate } 12497c478bd9Sstevel@tonic-gate 12507c478bd9Sstevel@tonic-gate c_list->cl_subclass_list = (subclass_lst_t *) 12517c478bd9Sstevel@tonic-gate calloc(1, sizeof (subclass_lst_t)); 12527c478bd9Sstevel@tonic-gate if (c_list->cl_subclass_list == NULL) { 12537c478bd9Sstevel@tonic-gate free(c_list->cl_name); 12547c478bd9Sstevel@tonic-gate free(c_list); 12557c478bd9Sstevel@tonic-gate return (1); 12567c478bd9Sstevel@tonic-gate } 12577c478bd9Sstevel@tonic-gate c_list->cl_subclass_list->sl_name = strdup(EC_SUB_ALL); 12587c478bd9Sstevel@tonic-gate if (c_list->cl_subclass_list->sl_name == NULL) { 12597c478bd9Sstevel@tonic-gate free(c_list->cl_subclass_list); 12607c478bd9Sstevel@tonic-gate free(c_list->cl_name); 12617c478bd9Sstevel@tonic-gate free(c_list); 12627c478bd9Sstevel@tonic-gate return (1); 12637c478bd9Sstevel@tonic-gate } 12647c478bd9Sstevel@tonic-gate c_list->cl_next = SH_CLASS_HASH(shp)[CLASS_HASH(class)]; 12657c478bd9Sstevel@tonic-gate SH_CLASS_HASH(shp)[CLASS_HASH(class)] = c_list; 12667c478bd9Sstevel@tonic-gate 12677c478bd9Sstevel@tonic-gate } 12687c478bd9Sstevel@tonic-gate 12697c478bd9Sstevel@tonic-gate /* Update the subclass list */ 12707c478bd9Sstevel@tonic-gate if (cache_insert_subclass(c_list, subclass_names, subclass_num, 12717c478bd9Sstevel@tonic-gate sub_id) != 0) 12727c478bd9Sstevel@tonic-gate return (1); 12737c478bd9Sstevel@tonic-gate 12747c478bd9Sstevel@tonic-gate return (0); 12757c478bd9Sstevel@tonic-gate } 12767c478bd9Sstevel@tonic-gate 12777c478bd9Sstevel@tonic-gate static void 12787c478bd9Sstevel@tonic-gate cache_remove_all_class(sysevent_handle_t *shp, uint32_t sub_id) 12797c478bd9Sstevel@tonic-gate { 12807c478bd9Sstevel@tonic-gate int i; 12817c478bd9Sstevel@tonic-gate class_lst_t *c_list; 12827c478bd9Sstevel@tonic-gate subclass_lst_t *sc_list; 12837c478bd9Sstevel@tonic-gate 12847c478bd9Sstevel@tonic-gate for (i = 0; i < CLASS_HASH_SZ + 1; ++i) { 12857c478bd9Sstevel@tonic-gate c_list = SH_CLASS_HASH(shp)[i]; 12867c478bd9Sstevel@tonic-gate while (c_list != NULL) { 12877c478bd9Sstevel@tonic-gate sc_list = c_list->cl_subclass_list; 12887c478bd9Sstevel@tonic-gate while (sc_list != NULL) { 12897c478bd9Sstevel@tonic-gate sc_list->sl_num[sub_id] = 0; 12907c478bd9Sstevel@tonic-gate sc_list = sc_list->sl_next; 12917c478bd9Sstevel@tonic-gate } 12927c478bd9Sstevel@tonic-gate c_list = c_list->cl_next; 12937c478bd9Sstevel@tonic-gate } 12947c478bd9Sstevel@tonic-gate } 12957c478bd9Sstevel@tonic-gate } 12967c478bd9Sstevel@tonic-gate 12977c478bd9Sstevel@tonic-gate static void 12987c478bd9Sstevel@tonic-gate cache_remove_class(sysevent_handle_t *shp, char *class, uint32_t sub_id) 12997c478bd9Sstevel@tonic-gate { 13007c478bd9Sstevel@tonic-gate class_lst_t *c_list; 13017c478bd9Sstevel@tonic-gate subclass_lst_t *sc_list; 13027c478bd9Sstevel@tonic-gate 13037c478bd9Sstevel@tonic-gate if (strcmp(class, EC_ALL) == 0) { 13047c478bd9Sstevel@tonic-gate cache_remove_all_class(shp, sub_id); 13057c478bd9Sstevel@tonic-gate return; 13067c478bd9Sstevel@tonic-gate } 13077c478bd9Sstevel@tonic-gate 13087c478bd9Sstevel@tonic-gate if ((c_list = cache_find_class(shp, class)) == NULL) { 13097c478bd9Sstevel@tonic-gate return; 13107c478bd9Sstevel@tonic-gate } 13117c478bd9Sstevel@tonic-gate 13127c478bd9Sstevel@tonic-gate sc_list = c_list->cl_subclass_list; 13137c478bd9Sstevel@tonic-gate while (sc_list != NULL) { 13147c478bd9Sstevel@tonic-gate sc_list->sl_num[sub_id] = 0; 13157c478bd9Sstevel@tonic-gate sc_list = sc_list->sl_next; 13167c478bd9Sstevel@tonic-gate } 13177c478bd9Sstevel@tonic-gate } 13187c478bd9Sstevel@tonic-gate 13197c478bd9Sstevel@tonic-gate static void 13207c478bd9Sstevel@tonic-gate free_cached_registration(sysevent_handle_t *shp) 13217c478bd9Sstevel@tonic-gate { 13227c478bd9Sstevel@tonic-gate int i; 13237c478bd9Sstevel@tonic-gate class_lst_t *clist, *next_clist; 13247c478bd9Sstevel@tonic-gate subclass_lst_t *sc_list, *next_sc; 13257c478bd9Sstevel@tonic-gate 13267c478bd9Sstevel@tonic-gate for (i = 0; i < CLASS_HASH_SZ + 1; i++) { 13277c478bd9Sstevel@tonic-gate clist = SH_CLASS_HASH(shp)[i]; 13287c478bd9Sstevel@tonic-gate while (clist != NULL) { 13297c478bd9Sstevel@tonic-gate sc_list = clist->cl_subclass_list; 13307c478bd9Sstevel@tonic-gate while (sc_list != NULL) { 13317c478bd9Sstevel@tonic-gate free(sc_list->sl_name); 13327c478bd9Sstevel@tonic-gate next_sc = sc_list->sl_next; 13337c478bd9Sstevel@tonic-gate free(sc_list); 13347c478bd9Sstevel@tonic-gate sc_list = next_sc; 13357c478bd9Sstevel@tonic-gate } 13367c478bd9Sstevel@tonic-gate free(clist->cl_name); 13377c478bd9Sstevel@tonic-gate next_clist = clist->cl_next; 13387c478bd9Sstevel@tonic-gate free(clist); 13397c478bd9Sstevel@tonic-gate clist = next_clist; 13407c478bd9Sstevel@tonic-gate } 13417c478bd9Sstevel@tonic-gate SH_CLASS_HASH(shp)[i] = NULL; 13427c478bd9Sstevel@tonic-gate } 13437c478bd9Sstevel@tonic-gate } 13447c478bd9Sstevel@tonic-gate 13457c478bd9Sstevel@tonic-gate static int 13467c478bd9Sstevel@tonic-gate create_cached_registration(sysevent_handle_t *shp, 13477c478bd9Sstevel@tonic-gate class_lst_t **class_hash) 13487c478bd9Sstevel@tonic-gate { 13497c478bd9Sstevel@tonic-gate int i, j, new_class; 13507c478bd9Sstevel@tonic-gate char *class_name; 13517c478bd9Sstevel@tonic-gate uint_t num_elem; 13527c478bd9Sstevel@tonic-gate uchar_t *subscribers; 13537c478bd9Sstevel@tonic-gate nvlist_t *nvl; 13547c478bd9Sstevel@tonic-gate nvpair_t *nvpair; 13557c478bd9Sstevel@tonic-gate class_lst_t *clist; 13567c478bd9Sstevel@tonic-gate subclass_lst_t *sc_list; 13577c478bd9Sstevel@tonic-gate 13587c478bd9Sstevel@tonic-gate for (i = 0; i < CLASS_HASH_SZ + 1; ++i) { 13597c478bd9Sstevel@tonic-gate 13607c478bd9Sstevel@tonic-gate if ((nvl = get_kernel_registration(SH_CHANNEL_NAME(shp), i)) 13617c478bd9Sstevel@tonic-gate == NULL) { 13627c478bd9Sstevel@tonic-gate if (errno == ENOENT) { 13637c478bd9Sstevel@tonic-gate class_hash[i] = NULL; 13647c478bd9Sstevel@tonic-gate continue; 13657c478bd9Sstevel@tonic-gate } else { 13667c478bd9Sstevel@tonic-gate goto create_failed; 13677c478bd9Sstevel@tonic-gate } 13687c478bd9Sstevel@tonic-gate } 13697c478bd9Sstevel@tonic-gate 13707c478bd9Sstevel@tonic-gate 13717c478bd9Sstevel@tonic-gate nvpair = NULL; 13727c478bd9Sstevel@tonic-gate if ((nvpair = nvlist_next_nvpair(nvl, nvpair)) == NULL) { 13737c478bd9Sstevel@tonic-gate goto create_failed; 13747c478bd9Sstevel@tonic-gate } 13757c478bd9Sstevel@tonic-gate 13767c478bd9Sstevel@tonic-gate new_class = 1; 13777c478bd9Sstevel@tonic-gate while (new_class) { 13787c478bd9Sstevel@tonic-gate /* Extract the class name from the nvpair */ 13797c478bd9Sstevel@tonic-gate if (nvpair_value_string(nvpair, &class_name) != 0) { 13807c478bd9Sstevel@tonic-gate goto create_failed; 13817c478bd9Sstevel@tonic-gate } 13827c478bd9Sstevel@tonic-gate clist = (class_lst_t *) 13837c478bd9Sstevel@tonic-gate calloc(1, sizeof (class_lst_t)); 13847c478bd9Sstevel@tonic-gate if (clist == NULL) { 13857c478bd9Sstevel@tonic-gate goto create_failed; 13867c478bd9Sstevel@tonic-gate } 13877c478bd9Sstevel@tonic-gate 13887c478bd9Sstevel@tonic-gate clist->cl_name = strdup(class_name); 13897c478bd9Sstevel@tonic-gate if (clist->cl_name == NULL) { 13907c478bd9Sstevel@tonic-gate free(clist); 13917c478bd9Sstevel@tonic-gate goto create_failed; 13927c478bd9Sstevel@tonic-gate } 13937c478bd9Sstevel@tonic-gate 13947c478bd9Sstevel@tonic-gate /* 13957c478bd9Sstevel@tonic-gate * Extract the subclass name and registration 13967c478bd9Sstevel@tonic-gate * from the nvpair 13977c478bd9Sstevel@tonic-gate */ 13987c478bd9Sstevel@tonic-gate if ((nvpair = nvlist_next_nvpair(nvl, nvpair)) 13997c478bd9Sstevel@tonic-gate == NULL) { 14007c478bd9Sstevel@tonic-gate free(clist->cl_name); 14017c478bd9Sstevel@tonic-gate free(clist); 14027c478bd9Sstevel@tonic-gate goto create_failed; 14037c478bd9Sstevel@tonic-gate } 14047c478bd9Sstevel@tonic-gate 14057c478bd9Sstevel@tonic-gate clist->cl_next = class_hash[i]; 14067c478bd9Sstevel@tonic-gate class_hash[i] = clist; 14077c478bd9Sstevel@tonic-gate 14087c478bd9Sstevel@tonic-gate for (;;) { 14097c478bd9Sstevel@tonic-gate 14107c478bd9Sstevel@tonic-gate sc_list = (subclass_lst_t *)calloc(1, 14117c478bd9Sstevel@tonic-gate sizeof (subclass_lst_t)); 14127c478bd9Sstevel@tonic-gate if (sc_list == NULL) { 14137c478bd9Sstevel@tonic-gate goto create_failed; 14147c478bd9Sstevel@tonic-gate } 14157c478bd9Sstevel@tonic-gate 14167c478bd9Sstevel@tonic-gate sc_list->sl_next = clist->cl_subclass_list; 14177c478bd9Sstevel@tonic-gate clist->cl_subclass_list = sc_list; 14187c478bd9Sstevel@tonic-gate 14197c478bd9Sstevel@tonic-gate sc_list->sl_name = strdup(nvpair_name(nvpair)); 14207c478bd9Sstevel@tonic-gate if (sc_list->sl_name == NULL) { 14217c478bd9Sstevel@tonic-gate goto create_failed; 14227c478bd9Sstevel@tonic-gate } 14237c478bd9Sstevel@tonic-gate 14247c478bd9Sstevel@tonic-gate if (nvpair_value_byte_array(nvpair, 14257c478bd9Sstevel@tonic-gate &subscribers, &num_elem) != 0) { 14267c478bd9Sstevel@tonic-gate goto create_failed; 14277c478bd9Sstevel@tonic-gate } 14287c478bd9Sstevel@tonic-gate bcopy(subscribers, (uchar_t *)sc_list->sl_num, 14297c478bd9Sstevel@tonic-gate MAX_SUBSCRIBERS + 1); 14307c478bd9Sstevel@tonic-gate 14317c478bd9Sstevel@tonic-gate for (j = 1; j <= MAX_SUBSCRIBERS; ++j) { 14327c478bd9Sstevel@tonic-gate if (sc_list->sl_num[j] == 0) 14337c478bd9Sstevel@tonic-gate continue; 14347c478bd9Sstevel@tonic-gate 14357c478bd9Sstevel@tonic-gate if (alloc_subscriber(shp, j, 1) != 0) { 14367c478bd9Sstevel@tonic-gate goto create_failed; 14377c478bd9Sstevel@tonic-gate } 14387c478bd9Sstevel@tonic-gate } 14397c478bd9Sstevel@tonic-gate 14407c478bd9Sstevel@tonic-gate /* 14417c478bd9Sstevel@tonic-gate * Check next nvpair - either subclass or 14427c478bd9Sstevel@tonic-gate * class 14437c478bd9Sstevel@tonic-gate */ 14447c478bd9Sstevel@tonic-gate if ((nvpair = nvlist_next_nvpair(nvl, nvpair)) 14457c478bd9Sstevel@tonic-gate == NULL) { 14467c478bd9Sstevel@tonic-gate new_class = 0; 14477c478bd9Sstevel@tonic-gate break; 14487c478bd9Sstevel@tonic-gate } else if (strcmp(nvpair_name(nvpair), 14497c478bd9Sstevel@tonic-gate CLASS_NAME) == 0) { 14507c478bd9Sstevel@tonic-gate break; 14517c478bd9Sstevel@tonic-gate } 14527c478bd9Sstevel@tonic-gate } 14537c478bd9Sstevel@tonic-gate } 14547c478bd9Sstevel@tonic-gate nvlist_free(nvl); 14557c478bd9Sstevel@tonic-gate } 14567c478bd9Sstevel@tonic-gate return (0); 14577c478bd9Sstevel@tonic-gate 14587c478bd9Sstevel@tonic-gate create_failed: 14597c478bd9Sstevel@tonic-gate dealloc_subscribers(shp); 14607c478bd9Sstevel@tonic-gate free_cached_registration(shp); 14617c478bd9Sstevel@tonic-gate if (nvl) 14627c478bd9Sstevel@tonic-gate nvlist_free(nvl); 14637c478bd9Sstevel@tonic-gate return (-1); 14647c478bd9Sstevel@tonic-gate 14657c478bd9Sstevel@tonic-gate } 14667c478bd9Sstevel@tonic-gate 14677c478bd9Sstevel@tonic-gate /* 14687c478bd9Sstevel@tonic-gate * cache_update_service - generic event publisher service routine. This routine 14697c478bd9Sstevel@tonic-gate * is called in response to a registration cache update. 14707c478bd9Sstevel@tonic-gate * 14717c478bd9Sstevel@tonic-gate */ 1472*49b225e1SGavin Maltby /*ARGSUSED*/ 14737c478bd9Sstevel@tonic-gate static void 14747c478bd9Sstevel@tonic-gate cache_update_service(void *cookie, char *args, size_t alen, 14757c478bd9Sstevel@tonic-gate door_desc_t *ddp, uint_t ndid) 14767c478bd9Sstevel@tonic-gate { 14777c478bd9Sstevel@tonic-gate int ret = 0; 14787c478bd9Sstevel@tonic-gate uint_t num_elem; 14797c478bd9Sstevel@tonic-gate char *class, **event_list; 14807c478bd9Sstevel@tonic-gate size_t datalen; 14817c478bd9Sstevel@tonic-gate uint32_t sub_id; 14827c478bd9Sstevel@tonic-gate nvlist_t *nvl; 14837c478bd9Sstevel@tonic-gate nvpair_t *nvpair = NULL; 14847c478bd9Sstevel@tonic-gate struct reg_args *rargs; 14857c478bd9Sstevel@tonic-gate sysevent_handle_t *shp; 14867c478bd9Sstevel@tonic-gate subscriber_data_t *sub; 14877c478bd9Sstevel@tonic-gate 14887c478bd9Sstevel@tonic-gate if (alen < sizeof (struct reg_args) || cookie == NULL) { 14897c478bd9Sstevel@tonic-gate ret = EINVAL; 14907c478bd9Sstevel@tonic-gate goto return_from_door; 14917c478bd9Sstevel@tonic-gate } 14927c478bd9Sstevel@tonic-gate 1493*49b225e1SGavin Maltby /* LINTED: E_BAD_PTR_CAST_ALIGN */ 14947c478bd9Sstevel@tonic-gate rargs = (struct reg_args *)args; 14957c478bd9Sstevel@tonic-gate shp = (sysevent_handle_t *)cookie; 14967c478bd9Sstevel@tonic-gate 14977c478bd9Sstevel@tonic-gate datalen = alen - sizeof (struct reg_args); 14987c478bd9Sstevel@tonic-gate sub_id = rargs->ra_sub_id; 14997c478bd9Sstevel@tonic-gate 15007c478bd9Sstevel@tonic-gate (void) mutex_lock(SH_LOCK(shp)); 15017c478bd9Sstevel@tonic-gate 15027c478bd9Sstevel@tonic-gate switch (rargs->ra_op) { 15037c478bd9Sstevel@tonic-gate case SE_UNREGISTER: 15047c478bd9Sstevel@tonic-gate class = (char *)&rargs->ra_buf_ptr; 15057c478bd9Sstevel@tonic-gate cache_remove_class(shp, (char *)class, 15067c478bd9Sstevel@tonic-gate sub_id); 15077c478bd9Sstevel@tonic-gate break; 15087c478bd9Sstevel@tonic-gate case SE_UNBIND_REGISTRATION: 15097c478bd9Sstevel@tonic-gate 15107c478bd9Sstevel@tonic-gate sub = SH_SUBSCRIBER(shp, sub_id); 15117c478bd9Sstevel@tonic-gate if (sub == NULL) 15127c478bd9Sstevel@tonic-gate break; 15137c478bd9Sstevel@tonic-gate 15147c478bd9Sstevel@tonic-gate free(sub->sd_door_name); 15157c478bd9Sstevel@tonic-gate free(sub); 15167c478bd9Sstevel@tonic-gate cache_remove_class(shp, EC_ALL, sub_id); 15177c478bd9Sstevel@tonic-gate SH_SUBSCRIBER(shp, sub_id) = NULL; 15187c478bd9Sstevel@tonic-gate 15197c478bd9Sstevel@tonic-gate break; 15207c478bd9Sstevel@tonic-gate case SE_BIND_REGISTRATION: 15217c478bd9Sstevel@tonic-gate 15227c478bd9Sstevel@tonic-gate /* New subscriber */ 15237c478bd9Sstevel@tonic-gate if (alloc_subscriber(shp, sub_id, 0) != 0) { 15247c478bd9Sstevel@tonic-gate ret = ENOMEM; 15257c478bd9Sstevel@tonic-gate break; 15267c478bd9Sstevel@tonic-gate } 15277c478bd9Sstevel@tonic-gate break; 15287c478bd9Sstevel@tonic-gate case SE_REGISTER: 15297c478bd9Sstevel@tonic-gate 15307c478bd9Sstevel@tonic-gate if (SH_SUBSCRIBER(shp, sub_id) == NULL) { 15317c478bd9Sstevel@tonic-gate ret = EINVAL; 15327c478bd9Sstevel@tonic-gate break; 15337c478bd9Sstevel@tonic-gate } 15347c478bd9Sstevel@tonic-gate /* Get new registration data */ 15357c478bd9Sstevel@tonic-gate if (nvlist_unpack((char *)&rargs->ra_buf_ptr, datalen, 15367c478bd9Sstevel@tonic-gate &nvl, 0) != 0) { 15377c478bd9Sstevel@tonic-gate ret = EFAULT; 15387c478bd9Sstevel@tonic-gate break; 15397c478bd9Sstevel@tonic-gate } 15407c478bd9Sstevel@tonic-gate if ((nvpair = nvlist_next_nvpair(nvl, nvpair)) == NULL) { 15417c478bd9Sstevel@tonic-gate nvlist_free(nvl); 15427c478bd9Sstevel@tonic-gate ret = EFAULT; 15437c478bd9Sstevel@tonic-gate break; 15447c478bd9Sstevel@tonic-gate } 15457c478bd9Sstevel@tonic-gate if (nvpair_value_string_array(nvpair, &event_list, &num_elem) 15467c478bd9Sstevel@tonic-gate != 0) { 15477c478bd9Sstevel@tonic-gate nvlist_free(nvl); 15487c478bd9Sstevel@tonic-gate ret = EFAULT; 15497c478bd9Sstevel@tonic-gate break; 15507c478bd9Sstevel@tonic-gate } 15517c478bd9Sstevel@tonic-gate class = nvpair_name(nvpair); 15527c478bd9Sstevel@tonic-gate 15537c478bd9Sstevel@tonic-gate ret = cache_insert_class(shp, class, 15547c478bd9Sstevel@tonic-gate event_list, num_elem, sub_id); 15557c478bd9Sstevel@tonic-gate if (ret != 0) { 15567c478bd9Sstevel@tonic-gate cache_remove_class(shp, class, sub_id); 15577c478bd9Sstevel@tonic-gate nvlist_free(nvl); 15587c478bd9Sstevel@tonic-gate ret = EFAULT; 15597c478bd9Sstevel@tonic-gate break; 15607c478bd9Sstevel@tonic-gate } 15617c478bd9Sstevel@tonic-gate 15627c478bd9Sstevel@tonic-gate nvlist_free(nvl); 15637c478bd9Sstevel@tonic-gate 15647c478bd9Sstevel@tonic-gate break; 15657c478bd9Sstevel@tonic-gate case SE_CLEANUP: 15667c478bd9Sstevel@tonic-gate /* Cleanup stale subscribers */ 15677c478bd9Sstevel@tonic-gate sysevent_cleanup_subscribers(shp); 15687c478bd9Sstevel@tonic-gate break; 15697c478bd9Sstevel@tonic-gate default: 15707c478bd9Sstevel@tonic-gate ret = EINVAL; 15717c478bd9Sstevel@tonic-gate } 15727c478bd9Sstevel@tonic-gate 15737c478bd9Sstevel@tonic-gate (void) mutex_unlock(SH_LOCK(shp)); 15747c478bd9Sstevel@tonic-gate 15757c478bd9Sstevel@tonic-gate return_from_door: 15767c478bd9Sstevel@tonic-gate (void) door_return((void *)&ret, sizeof (ret), NULL, 0); 15777c478bd9Sstevel@tonic-gate (void) door_return(NULL, 0, NULL, 0); 15787c478bd9Sstevel@tonic-gate } 15797c478bd9Sstevel@tonic-gate 15807c478bd9Sstevel@tonic-gate /* 15817c478bd9Sstevel@tonic-gate * sysevent_send_event - 15827c478bd9Sstevel@tonic-gate * Send an event via the communication channel associated with the sysevent 15837c478bd9Sstevel@tonic-gate * handle. Event notifications are broadcast to all subscribers based upon 15847c478bd9Sstevel@tonic-gate * the event class and subclass. The handle must have been previously 15857c478bd9Sstevel@tonic-gate * allocated and bound by 15867c478bd9Sstevel@tonic-gate * sysevent_open_channel() and sysevent_bind_publisher() 15877c478bd9Sstevel@tonic-gate */ 15887c478bd9Sstevel@tonic-gate int 15897c478bd9Sstevel@tonic-gate sysevent_send_event(sysevent_handle_t *shp, sysevent_t *ev) 15907c478bd9Sstevel@tonic-gate { 15917c478bd9Sstevel@tonic-gate int i, error, sub_fd, result = 0; 15927c478bd9Sstevel@tonic-gate int deliver_error = 0; 15937c478bd9Sstevel@tonic-gate int subscribers_sent = 0; 15947c478bd9Sstevel@tonic-gate int want_resend, resend_cnt = 0; 15957c478bd9Sstevel@tonic-gate char *event_class, *event_subclass; 15967c478bd9Sstevel@tonic-gate uchar_t *all_class_subscribers, *all_subclass_subscribers; 15977c478bd9Sstevel@tonic-gate uchar_t *subclass_subscribers; 15987c478bd9Sstevel@tonic-gate subscriber_data_t *sub; 15997c478bd9Sstevel@tonic-gate subclass_lst_t *sc_lst; 16007c478bd9Sstevel@tonic-gate 16017c478bd9Sstevel@tonic-gate /* Check for proper registration */ 16027c478bd9Sstevel@tonic-gate event_class = sysevent_get_class_name(ev); 16037c478bd9Sstevel@tonic-gate event_subclass = sysevent_get_subclass_name(ev); 16047c478bd9Sstevel@tonic-gate 16057c478bd9Sstevel@tonic-gate (void) mutex_lock(SH_LOCK(shp)); 16067c478bd9Sstevel@tonic-gate 16077c478bd9Sstevel@tonic-gate send_event: 16087c478bd9Sstevel@tonic-gate 16097c478bd9Sstevel@tonic-gate want_resend = 0; 16107c478bd9Sstevel@tonic-gate if (!SH_BOUND(shp)) { 16117c478bd9Sstevel@tonic-gate (void) mutex_unlock(SH_LOCK(shp)); 16127c478bd9Sstevel@tonic-gate errno = EINVAL; 16137c478bd9Sstevel@tonic-gate return (-1); 16147c478bd9Sstevel@tonic-gate } 16157c478bd9Sstevel@tonic-gate 16167c478bd9Sstevel@tonic-gate /* Find all subscribers for this event class/subclass */ 16177c478bd9Sstevel@tonic-gate sc_lst = cache_find_subclass( 16187c478bd9Sstevel@tonic-gate cache_find_class(shp, EC_ALL), EC_SUB_ALL); 16197c478bd9Sstevel@tonic-gate all_class_subscribers = sc_lst->sl_num; 16207c478bd9Sstevel@tonic-gate 16217c478bd9Sstevel@tonic-gate sc_lst = cache_find_subclass( 16227c478bd9Sstevel@tonic-gate cache_find_class(shp, event_class), EC_SUB_ALL); 16237c478bd9Sstevel@tonic-gate if (sc_lst) 16247c478bd9Sstevel@tonic-gate all_subclass_subscribers = sc_lst->sl_num; 16257c478bd9Sstevel@tonic-gate else 16267c478bd9Sstevel@tonic-gate all_subclass_subscribers = NULL; 16277c478bd9Sstevel@tonic-gate 16287c478bd9Sstevel@tonic-gate sc_lst = cache_find_subclass( 16297c478bd9Sstevel@tonic-gate cache_find_class(shp, event_class), event_subclass); 16307c478bd9Sstevel@tonic-gate if (sc_lst) 16317c478bd9Sstevel@tonic-gate subclass_subscribers = sc_lst->sl_num; 16327c478bd9Sstevel@tonic-gate else 16337c478bd9Sstevel@tonic-gate subclass_subscribers = NULL; 16347c478bd9Sstevel@tonic-gate 16357c478bd9Sstevel@tonic-gate /* Send event buffer to all valid subscribers */ 16367c478bd9Sstevel@tonic-gate for (i = 1; i <= MAX_SUBSCRIBERS; ++i) { 16377c478bd9Sstevel@tonic-gate if ((all_class_subscribers[i] | 16387c478bd9Sstevel@tonic-gate (all_subclass_subscribers && all_subclass_subscribers[i]) | 16397c478bd9Sstevel@tonic-gate (subclass_subscribers && subclass_subscribers[i])) == 0) 16407c478bd9Sstevel@tonic-gate continue; 16417c478bd9Sstevel@tonic-gate 16427c478bd9Sstevel@tonic-gate sub = SH_SUBSCRIBER(shp, i); 16437c478bd9Sstevel@tonic-gate assert(sub != NULL); 16447c478bd9Sstevel@tonic-gate 16457c478bd9Sstevel@tonic-gate /* Check for active subscriber */ 16467c478bd9Sstevel@tonic-gate if (!(sub->sd_flag & ACTIVE)) { 16477c478bd9Sstevel@tonic-gate dprint("sysevent_send_event: subscriber %d inactive\n", 16487c478bd9Sstevel@tonic-gate i); 16497c478bd9Sstevel@tonic-gate continue; 16507c478bd9Sstevel@tonic-gate } 16517c478bd9Sstevel@tonic-gate 16527c478bd9Sstevel@tonic-gate /* Process only resend requests */ 16537c478bd9Sstevel@tonic-gate if (resend_cnt > 0 && !(sub->sd_flag & SEND_AGAIN)) { 16547c478bd9Sstevel@tonic-gate continue; 16557c478bd9Sstevel@tonic-gate } 16567c478bd9Sstevel@tonic-gate 16577c478bd9Sstevel@tonic-gate if ((sub_fd = open(sub->sd_door_name, O_RDONLY)) == -1) { 16587c478bd9Sstevel@tonic-gate dprint("sysevent_send_event: Failed to open " 16597c478bd9Sstevel@tonic-gate "%s: %s\n", sub->sd_door_name, strerror(errno)); 16607c478bd9Sstevel@tonic-gate continue; 16617c478bd9Sstevel@tonic-gate } 16627c478bd9Sstevel@tonic-gate result = 0; 16637c478bd9Sstevel@tonic-gate error = clnt_deliver_event(sub_fd, ev, 16647c478bd9Sstevel@tonic-gate sysevent_get_size(ev), &result, sizeof (result)); 16657c478bd9Sstevel@tonic-gate 16667c478bd9Sstevel@tonic-gate (void) close(sub_fd); 16677c478bd9Sstevel@tonic-gate 16687c478bd9Sstevel@tonic-gate /* Successful door call */ 16697c478bd9Sstevel@tonic-gate if (error == 0) { 16707c478bd9Sstevel@tonic-gate switch (result) { 16717c478bd9Sstevel@tonic-gate /* Subscriber requested EAGAIN */ 16727c478bd9Sstevel@tonic-gate case EAGAIN: 16737c478bd9Sstevel@tonic-gate if (resend_cnt > SE_MAX_RETRY_LIMIT) { 16747c478bd9Sstevel@tonic-gate deliver_error = 1; 16757c478bd9Sstevel@tonic-gate } else { 16767c478bd9Sstevel@tonic-gate want_resend = 1; 16777c478bd9Sstevel@tonic-gate dprint("sysevent_send_event: resend " 16787c478bd9Sstevel@tonic-gate "requested for %d\n", i); 16797c478bd9Sstevel@tonic-gate sub->sd_flag |= SEND_AGAIN; 16807c478bd9Sstevel@tonic-gate } 16817c478bd9Sstevel@tonic-gate break; 16827c478bd9Sstevel@tonic-gate /* Bad sysevent handle for subscriber */ 16837c478bd9Sstevel@tonic-gate case EBADF: 16847c478bd9Sstevel@tonic-gate case EINVAL: 16857c478bd9Sstevel@tonic-gate dprint("sysevent_send_event: Bad sysevent " 16867c478bd9Sstevel@tonic-gate "handle for %s", sub->sd_door_name); 16877c478bd9Sstevel@tonic-gate sub->sd_flag = 0; 16887c478bd9Sstevel@tonic-gate deliver_error = 1; 16897c478bd9Sstevel@tonic-gate break; 16907c478bd9Sstevel@tonic-gate /* Successful delivery */ 16917c478bd9Sstevel@tonic-gate default: 16927c478bd9Sstevel@tonic-gate sub->sd_flag &= ~SEND_AGAIN; 16937c478bd9Sstevel@tonic-gate ++subscribers_sent; 16947c478bd9Sstevel@tonic-gate } 16957c478bd9Sstevel@tonic-gate } else { 16967c478bd9Sstevel@tonic-gate dprint("sysevent_send_event: Failed door call " 16977c478bd9Sstevel@tonic-gate "to %s: %s: %d\n", sub->sd_door_name, 16987c478bd9Sstevel@tonic-gate strerror(errno), result); 16997c478bd9Sstevel@tonic-gate sub->sd_flag = 0; 17007c478bd9Sstevel@tonic-gate deliver_error = 1; 17017c478bd9Sstevel@tonic-gate } 17027c478bd9Sstevel@tonic-gate } 17037c478bd9Sstevel@tonic-gate 17047c478bd9Sstevel@tonic-gate if (want_resend) { 17057c478bd9Sstevel@tonic-gate resend_cnt++; 17067c478bd9Sstevel@tonic-gate goto send_event; 17077c478bd9Sstevel@tonic-gate } 17087c478bd9Sstevel@tonic-gate 17097c478bd9Sstevel@tonic-gate if (deliver_error) { 17107c478bd9Sstevel@tonic-gate sysevent_cleanup_subscribers(shp); 17117c478bd9Sstevel@tonic-gate (void) mutex_unlock(SH_LOCK(shp)); 17127c478bd9Sstevel@tonic-gate errno = EFAULT; 17137c478bd9Sstevel@tonic-gate return (-1); 17147c478bd9Sstevel@tonic-gate } 17157c478bd9Sstevel@tonic-gate 17167c478bd9Sstevel@tonic-gate (void) mutex_unlock(SH_LOCK(shp)); 17177c478bd9Sstevel@tonic-gate 17187c478bd9Sstevel@tonic-gate if (subscribers_sent == 0) { 17197c478bd9Sstevel@tonic-gate dprint("sysevent_send_event: No subscribers for %s:%s\n", 17207c478bd9Sstevel@tonic-gate event_class, event_subclass); 17217c478bd9Sstevel@tonic-gate errno = ENOENT; 17227c478bd9Sstevel@tonic-gate return (-1); 17237c478bd9Sstevel@tonic-gate } 17247c478bd9Sstevel@tonic-gate 17257c478bd9Sstevel@tonic-gate return (0); 17267c478bd9Sstevel@tonic-gate } 17277c478bd9Sstevel@tonic-gate 17287c478bd9Sstevel@tonic-gate /* 17297c478bd9Sstevel@tonic-gate * Common routine to establish an event channel through which an event 17307c478bd9Sstevel@tonic-gate * publisher or subscriber may post or receive events. 17317c478bd9Sstevel@tonic-gate */ 17327c478bd9Sstevel@tonic-gate static sysevent_handle_t * 17337c478bd9Sstevel@tonic-gate sysevent_open_channel_common(const char *channel_path) 17347c478bd9Sstevel@tonic-gate { 17357c478bd9Sstevel@tonic-gate uint32_t sub_id = 0; 17367c478bd9Sstevel@tonic-gate char *begin_path; 17377c478bd9Sstevel@tonic-gate struct stat chan_stat; 17387c478bd9Sstevel@tonic-gate sysevent_handle_t *shp; 17397c478bd9Sstevel@tonic-gate 17407c478bd9Sstevel@tonic-gate 17417c478bd9Sstevel@tonic-gate if (channel_path == NULL || strlen(channel_path) + 1 > MAXPATHLEN) { 17427c478bd9Sstevel@tonic-gate errno = EINVAL; 17437c478bd9Sstevel@tonic-gate return (NULL); 17447c478bd9Sstevel@tonic-gate } 17457c478bd9Sstevel@tonic-gate 17467c478bd9Sstevel@tonic-gate if (mkdir(channel_path, S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH) < 0) { 17477c478bd9Sstevel@tonic-gate if (errno != EEXIST) { 17487c478bd9Sstevel@tonic-gate errno = EACCES; 17497c478bd9Sstevel@tonic-gate return (NULL); 17507c478bd9Sstevel@tonic-gate } 17517c478bd9Sstevel@tonic-gate } 17527c478bd9Sstevel@tonic-gate 17537c478bd9Sstevel@tonic-gate /* Check channel file permissions */ 17547c478bd9Sstevel@tonic-gate if (stat(channel_path, &chan_stat) != 0) { 17557c478bd9Sstevel@tonic-gate dprint("sysevent_open_channel: Invalid permissions for channel " 17567c478bd9Sstevel@tonic-gate "%s\n", channel_path); 17577c478bd9Sstevel@tonic-gate errno = EACCES; 17587c478bd9Sstevel@tonic-gate return (NULL); 17597c478bd9Sstevel@tonic-gate } else if (chan_stat.st_uid != getuid() || 17604bc0a2efScasper !S_ISDIR(chan_stat.st_mode)) { 17617c478bd9Sstevel@tonic-gate dprint("sysevent_open_channel: Invalid " 17627c478bd9Sstevel@tonic-gate "permissions for channel %s\n: %d:%d:%d", channel_path, 17637c478bd9Sstevel@tonic-gate (int)chan_stat.st_uid, (int)chan_stat.st_gid, 17647c478bd9Sstevel@tonic-gate (int)chan_stat.st_mode); 17657c478bd9Sstevel@tonic-gate 17667c478bd9Sstevel@tonic-gate errno = EACCES; 17677c478bd9Sstevel@tonic-gate return (NULL); 17687c478bd9Sstevel@tonic-gate } 17697c478bd9Sstevel@tonic-gate 17707c478bd9Sstevel@tonic-gate shp = calloc(1, sizeof (sysevent_impl_hdl_t)); 17717c478bd9Sstevel@tonic-gate if (shp == NULL) { 17727c478bd9Sstevel@tonic-gate errno = ENOMEM; 17737c478bd9Sstevel@tonic-gate return (NULL); 17747c478bd9Sstevel@tonic-gate } 17757c478bd9Sstevel@tonic-gate 17767c478bd9Sstevel@tonic-gate SH_CHANNEL_NAME(shp) = NULL; 17777c478bd9Sstevel@tonic-gate SH_CHANNEL_PATH(shp) = strdup(channel_path); 17787c478bd9Sstevel@tonic-gate if (SH_CHANNEL_PATH(shp) == NULL) { 17797c478bd9Sstevel@tonic-gate free(shp); 17807c478bd9Sstevel@tonic-gate errno = ENOMEM; 17817c478bd9Sstevel@tonic-gate return (NULL); 17827c478bd9Sstevel@tonic-gate } 17837c478bd9Sstevel@tonic-gate 17847c478bd9Sstevel@tonic-gate /* Extract the channel name */ 17857c478bd9Sstevel@tonic-gate begin_path = SH_CHANNEL_PATH(shp); 17867c478bd9Sstevel@tonic-gate while (*begin_path != '\0' && 17877c478bd9Sstevel@tonic-gate (begin_path = strpbrk(begin_path, "/")) != NULL) { 17887c478bd9Sstevel@tonic-gate ++begin_path; 17897c478bd9Sstevel@tonic-gate SH_CHANNEL_NAME(shp) = begin_path; 17907c478bd9Sstevel@tonic-gate } 17917c478bd9Sstevel@tonic-gate 17927c478bd9Sstevel@tonic-gate if (update_kernel_registration(shp, 0, 17937c478bd9Sstevel@tonic-gate SE_OPEN_REGISTRATION, &sub_id, 0, NULL) != 0) { 17947c478bd9Sstevel@tonic-gate dprint("sysevent_open_channel: Failed for channel %s\n", 17957c478bd9Sstevel@tonic-gate SH_CHANNEL_NAME(shp)); 17967c478bd9Sstevel@tonic-gate free(SH_CHANNEL_PATH(shp)); 17977c478bd9Sstevel@tonic-gate free(shp); 17987c478bd9Sstevel@tonic-gate errno = EFAULT; 17997c478bd9Sstevel@tonic-gate return (NULL); 18007c478bd9Sstevel@tonic-gate } 18017c478bd9Sstevel@tonic-gate 18027c478bd9Sstevel@tonic-gate (void) mutex_init(SH_LOCK(shp), USYNC_THREAD, NULL); 18037c478bd9Sstevel@tonic-gate 18047c478bd9Sstevel@tonic-gate return (shp); 18057c478bd9Sstevel@tonic-gate } 18067c478bd9Sstevel@tonic-gate 18077c478bd9Sstevel@tonic-gate /* 18087c478bd9Sstevel@tonic-gate * Establish a sysevent channel for publication and subscription 18097c478bd9Sstevel@tonic-gate */ 18107c478bd9Sstevel@tonic-gate sysevent_handle_t * 18117c478bd9Sstevel@tonic-gate sysevent_open_channel(const char *channel) 18127c478bd9Sstevel@tonic-gate { 18137c478bd9Sstevel@tonic-gate int var_run_mounted = 0; 18147c478bd9Sstevel@tonic-gate char full_channel[MAXPATHLEN + 1]; 18157c478bd9Sstevel@tonic-gate FILE *fp; 18167c478bd9Sstevel@tonic-gate struct stat chan_stat; 18177c478bd9Sstevel@tonic-gate struct extmnttab m; 18187c478bd9Sstevel@tonic-gate 18197c478bd9Sstevel@tonic-gate if (channel == NULL) { 18207c478bd9Sstevel@tonic-gate errno = EINVAL; 18217c478bd9Sstevel@tonic-gate return (NULL); 18227c478bd9Sstevel@tonic-gate } 18237c478bd9Sstevel@tonic-gate 18247c478bd9Sstevel@tonic-gate /* 18257c478bd9Sstevel@tonic-gate * Check that /var/run is mounted as tmpfs before allowing a channel 18267c478bd9Sstevel@tonic-gate * to be opened. 18277c478bd9Sstevel@tonic-gate */ 1828004388ebScasper if ((fp = fopen(MNTTAB, "rF")) == NULL) { 18297c478bd9Sstevel@tonic-gate errno = EACCES; 18307c478bd9Sstevel@tonic-gate return (NULL); 18317c478bd9Sstevel@tonic-gate } 18327c478bd9Sstevel@tonic-gate 18337c478bd9Sstevel@tonic-gate resetmnttab(fp); 18347c478bd9Sstevel@tonic-gate 18357c478bd9Sstevel@tonic-gate while (getextmntent(fp, &m, sizeof (struct extmnttab)) == 0) { 18367c478bd9Sstevel@tonic-gate if (strcmp(m.mnt_mountp, "/var/run") == 0 && 18377c478bd9Sstevel@tonic-gate strcmp(m.mnt_fstype, "tmpfs") == 0) { 18387c478bd9Sstevel@tonic-gate (void) fclose(fp); 18397c478bd9Sstevel@tonic-gate var_run_mounted = 1; 18407c478bd9Sstevel@tonic-gate break; 18417c478bd9Sstevel@tonic-gate } 18427c478bd9Sstevel@tonic-gate } 18437c478bd9Sstevel@tonic-gate (void) fclose(fp); 18447c478bd9Sstevel@tonic-gate 18457c478bd9Sstevel@tonic-gate if (!var_run_mounted) { 18467c478bd9Sstevel@tonic-gate errno = EACCES; 18477c478bd9Sstevel@tonic-gate return (NULL); 18487c478bd9Sstevel@tonic-gate } 18497c478bd9Sstevel@tonic-gate 18507c478bd9Sstevel@tonic-gate if (stat(CHAN_PATH, &chan_stat) < 0) { 18517c478bd9Sstevel@tonic-gate if (mkdir(CHAN_PATH, 18527c478bd9Sstevel@tonic-gate S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH) < 0) { 18537c478bd9Sstevel@tonic-gate dprint("sysevent_open_channel: Unable " 18547c478bd9Sstevel@tonic-gate "to create channel directory %s:%s\n", CHAN_PATH, 18557c478bd9Sstevel@tonic-gate strerror(errno)); 18567c478bd9Sstevel@tonic-gate if (errno != EEXIST) { 18577c478bd9Sstevel@tonic-gate errno = EACCES; 18587c478bd9Sstevel@tonic-gate return (NULL); 18597c478bd9Sstevel@tonic-gate } 18607c478bd9Sstevel@tonic-gate } 18617c478bd9Sstevel@tonic-gate } 18627c478bd9Sstevel@tonic-gate 18637c478bd9Sstevel@tonic-gate if (snprintf(full_channel, MAXPATHLEN, "%s/%s", CHAN_PATH, channel) >= 18647c478bd9Sstevel@tonic-gate MAXPATHLEN) { 18657c478bd9Sstevel@tonic-gate errno = EINVAL; 18667c478bd9Sstevel@tonic-gate return (NULL); 18677c478bd9Sstevel@tonic-gate } 18687c478bd9Sstevel@tonic-gate 18697c478bd9Sstevel@tonic-gate return (sysevent_open_channel_common(full_channel)); 18707c478bd9Sstevel@tonic-gate } 18717c478bd9Sstevel@tonic-gate 18727c478bd9Sstevel@tonic-gate /* 18737c478bd9Sstevel@tonic-gate * Establish a sysevent channel for publication and subscription 18747c478bd9Sstevel@tonic-gate * Full path to the channel determined by the caller 18757c478bd9Sstevel@tonic-gate */ 18767c478bd9Sstevel@tonic-gate sysevent_handle_t * 18777c478bd9Sstevel@tonic-gate sysevent_open_channel_alt(const char *channel_path) 18787c478bd9Sstevel@tonic-gate { 18797c478bd9Sstevel@tonic-gate return (sysevent_open_channel_common(channel_path)); 18807c478bd9Sstevel@tonic-gate } 18817c478bd9Sstevel@tonic-gate 18827c478bd9Sstevel@tonic-gate /* 18837c478bd9Sstevel@tonic-gate * sysevent_close_channel - Clean up resources associated with a previously 18847c478bd9Sstevel@tonic-gate * opened sysevent channel 18857c478bd9Sstevel@tonic-gate */ 18867c478bd9Sstevel@tonic-gate void 18877c478bd9Sstevel@tonic-gate sysevent_close_channel(sysevent_handle_t *shp) 18887c478bd9Sstevel@tonic-gate { 18897c478bd9Sstevel@tonic-gate int error = errno; 18907c478bd9Sstevel@tonic-gate uint32_t sub_id = 0; 18917c478bd9Sstevel@tonic-gate 18927c478bd9Sstevel@tonic-gate if (shp == NULL) { 18937c478bd9Sstevel@tonic-gate return; 18947c478bd9Sstevel@tonic-gate } 18957c478bd9Sstevel@tonic-gate 18967c478bd9Sstevel@tonic-gate (void) mutex_lock(SH_LOCK(shp)); 18977c478bd9Sstevel@tonic-gate if (SH_BOUND(shp)) { 18987c478bd9Sstevel@tonic-gate (void) mutex_unlock(SH_LOCK(shp)); 18997c478bd9Sstevel@tonic-gate if (SH_TYPE(shp) == PUBLISHER) 19007c478bd9Sstevel@tonic-gate sysevent_unbind_publisher(shp); 19017c478bd9Sstevel@tonic-gate else if (SH_TYPE(shp) == SUBSCRIBER) 19027c478bd9Sstevel@tonic-gate sysevent_unbind_subscriber(shp); 19037c478bd9Sstevel@tonic-gate (void) mutex_lock(SH_LOCK(shp)); 19047c478bd9Sstevel@tonic-gate } 19057c478bd9Sstevel@tonic-gate 19067c478bd9Sstevel@tonic-gate (void) update_kernel_registration(shp, 0, 19077c478bd9Sstevel@tonic-gate SE_CLOSE_REGISTRATION, &sub_id, 0, NULL); 19087c478bd9Sstevel@tonic-gate (void) mutex_unlock(SH_LOCK(shp)); 19097c478bd9Sstevel@tonic-gate 19107c478bd9Sstevel@tonic-gate free(SH_CHANNEL_PATH(shp)); 19117c478bd9Sstevel@tonic-gate free(shp); 19127c478bd9Sstevel@tonic-gate errno = error; 19137c478bd9Sstevel@tonic-gate } 19147c478bd9Sstevel@tonic-gate 19157c478bd9Sstevel@tonic-gate /* 19167c478bd9Sstevel@tonic-gate * sysevent_bind_publisher - Bind an event publisher to an event channel 19177c478bd9Sstevel@tonic-gate */ 19187c478bd9Sstevel@tonic-gate int 19197c478bd9Sstevel@tonic-gate sysevent_bind_publisher(sysevent_handle_t *shp) 19207c478bd9Sstevel@tonic-gate { 19217c478bd9Sstevel@tonic-gate int error = 0; 19227c478bd9Sstevel@tonic-gate int fd = -1; 19237c478bd9Sstevel@tonic-gate char door_name[MAXPATHLEN]; 19247c478bd9Sstevel@tonic-gate uint32_t pub_id; 19257c478bd9Sstevel@tonic-gate struct stat reg_stat; 19267c478bd9Sstevel@tonic-gate publisher_priv_t *pub; 19277c478bd9Sstevel@tonic-gate 19287c478bd9Sstevel@tonic-gate if (shp == NULL) { 19297c478bd9Sstevel@tonic-gate errno = EINVAL; 19307c478bd9Sstevel@tonic-gate return (-1); 19317c478bd9Sstevel@tonic-gate } 19327c478bd9Sstevel@tonic-gate 19337c478bd9Sstevel@tonic-gate (void) mutex_lock(SH_LOCK(shp)); 19347c478bd9Sstevel@tonic-gate if (SH_BOUND(shp)) { 19357c478bd9Sstevel@tonic-gate (void) mutex_unlock(SH_LOCK(shp)); 19367c478bd9Sstevel@tonic-gate errno = EINVAL; 19377c478bd9Sstevel@tonic-gate return (-1); 19387c478bd9Sstevel@tonic-gate } 19397c478bd9Sstevel@tonic-gate 19407c478bd9Sstevel@tonic-gate if ((pub = (publisher_priv_t *)calloc(1, sizeof (publisher_priv_t))) == 19417c478bd9Sstevel@tonic-gate NULL) { 19427c478bd9Sstevel@tonic-gate (void) mutex_unlock(SH_LOCK(shp)); 19437c478bd9Sstevel@tonic-gate errno = ENOMEM; 19447c478bd9Sstevel@tonic-gate return (-1); 19457c478bd9Sstevel@tonic-gate } 19467c478bd9Sstevel@tonic-gate SH_PRIV_DATA(shp) = (void *)pub; 19477c478bd9Sstevel@tonic-gate 19487c478bd9Sstevel@tonic-gate if (snprintf(door_name, MAXPATHLEN, "%s/%s", 19497c478bd9Sstevel@tonic-gate SH_CHANNEL_PATH(shp), REG_DOOR) >= MAXPATHLEN) { 19507c478bd9Sstevel@tonic-gate free(pub); 19517c478bd9Sstevel@tonic-gate (void) mutex_unlock(SH_LOCK(shp)); 19527c478bd9Sstevel@tonic-gate errno = ENOMEM; 19537c478bd9Sstevel@tonic-gate return (-1); 19547c478bd9Sstevel@tonic-gate } 19557c478bd9Sstevel@tonic-gate if ((SH_DOOR_NAME(shp) = strdup(door_name)) == NULL) { 19567c478bd9Sstevel@tonic-gate free(pub); 19577c478bd9Sstevel@tonic-gate (void) mutex_unlock(SH_LOCK(shp)); 19587c478bd9Sstevel@tonic-gate errno = ENOMEM; 19597c478bd9Sstevel@tonic-gate return (-1); 19607c478bd9Sstevel@tonic-gate } 19617c478bd9Sstevel@tonic-gate 19627c478bd9Sstevel@tonic-gate /* Only one publisher allowed per channel */ 19637c478bd9Sstevel@tonic-gate if (stat(SH_DOOR_NAME(shp), ®_stat) != 0) { 19647c478bd9Sstevel@tonic-gate if (errno != ENOENT) { 19657c478bd9Sstevel@tonic-gate error = EINVAL; 19667c478bd9Sstevel@tonic-gate goto fail; 19677c478bd9Sstevel@tonic-gate } 19687c478bd9Sstevel@tonic-gate } 19697c478bd9Sstevel@tonic-gate 19707c478bd9Sstevel@tonic-gate /* 19717c478bd9Sstevel@tonic-gate * Remove door file for robustness. 19727c478bd9Sstevel@tonic-gate */ 19737c478bd9Sstevel@tonic-gate if (unlink(SH_DOOR_NAME(shp)) != 0) 19747c478bd9Sstevel@tonic-gate dprint("sysevent_bind_publisher: Unlink of %s failed.\n", 19757c478bd9Sstevel@tonic-gate SH_DOOR_NAME(shp)); 19767c478bd9Sstevel@tonic-gate 19777c478bd9Sstevel@tonic-gate /* Open channel registration door */ 19787c478bd9Sstevel@tonic-gate fd = open(SH_DOOR_NAME(shp), O_CREAT|O_RDWR, 19797c478bd9Sstevel@tonic-gate S_IREAD|S_IWRITE); 19807c478bd9Sstevel@tonic-gate if (fd == -1) { 19817c478bd9Sstevel@tonic-gate error = EINVAL; 19827c478bd9Sstevel@tonic-gate goto fail; 19837c478bd9Sstevel@tonic-gate } 19847c478bd9Sstevel@tonic-gate 19857c478bd9Sstevel@tonic-gate /* 19867c478bd9Sstevel@tonic-gate * Create the registration service for this publisher. 19877c478bd9Sstevel@tonic-gate */ 19887c478bd9Sstevel@tonic-gate if ((SH_DOOR_DESC(shp) = door_create(cache_update_service, 19897c478bd9Sstevel@tonic-gate (void *)shp, DOOR_REFUSE_DESC | DOOR_NO_CANCEL)) == -1) { 19907c478bd9Sstevel@tonic-gate dprint("sysevent_bind_publisher: door create failed: " 19917c478bd9Sstevel@tonic-gate "%s\n", strerror(errno)); 19927c478bd9Sstevel@tonic-gate error = EFAULT; 19937c478bd9Sstevel@tonic-gate goto fail; 19947c478bd9Sstevel@tonic-gate } 19957c478bd9Sstevel@tonic-gate 19967c478bd9Sstevel@tonic-gate (void) fdetach(SH_DOOR_NAME(shp)); 19977c478bd9Sstevel@tonic-gate if (fattach(SH_DOOR_DESC(shp), SH_DOOR_NAME(shp)) != 0) { 19987c478bd9Sstevel@tonic-gate dprint("sysevent_bind_publisher: unable to " 19997c478bd9Sstevel@tonic-gate "bind event channel: fattach: %s\n", 20007c478bd9Sstevel@tonic-gate SH_DOOR_NAME(shp)); 20017c478bd9Sstevel@tonic-gate error = EACCES; 20027c478bd9Sstevel@tonic-gate goto fail; 20037c478bd9Sstevel@tonic-gate } 20047c478bd9Sstevel@tonic-gate 20057c478bd9Sstevel@tonic-gate /* Bind this publisher in the kernel registration database */ 20067c478bd9Sstevel@tonic-gate if (update_kernel_registration(shp, PUBLISHER, 20077c478bd9Sstevel@tonic-gate SE_BIND_REGISTRATION, &pub_id, 0, NULL) != 0) { 20087c478bd9Sstevel@tonic-gate error = errno; 20097c478bd9Sstevel@tonic-gate goto fail; 20107c478bd9Sstevel@tonic-gate } 20117c478bd9Sstevel@tonic-gate 20127c478bd9Sstevel@tonic-gate SH_ID(shp) = pub_id; 20137c478bd9Sstevel@tonic-gate SH_BOUND(shp) = 1; 20147c478bd9Sstevel@tonic-gate SH_TYPE(shp) = PUBLISHER; 20157c478bd9Sstevel@tonic-gate 20167c478bd9Sstevel@tonic-gate 20177c478bd9Sstevel@tonic-gate /* Create the subscription registration cache */ 20187c478bd9Sstevel@tonic-gate if (create_cached_registration(shp, SH_CLASS_HASH(shp)) != 0) { 20197c478bd9Sstevel@tonic-gate (void) update_kernel_registration(shp, 20207c478bd9Sstevel@tonic-gate PUBLISHER, SE_UNBIND_REGISTRATION, &pub_id, 0, NULL); 20217c478bd9Sstevel@tonic-gate error = EFAULT; 20227c478bd9Sstevel@tonic-gate goto fail; 20237c478bd9Sstevel@tonic-gate } 20247c478bd9Sstevel@tonic-gate (void) close(fd); 20257c478bd9Sstevel@tonic-gate 20267c478bd9Sstevel@tonic-gate (void) mutex_unlock(SH_LOCK(shp)); 20277c478bd9Sstevel@tonic-gate 20287c478bd9Sstevel@tonic-gate return (0); 20297c478bd9Sstevel@tonic-gate 20307c478bd9Sstevel@tonic-gate fail: 20317c478bd9Sstevel@tonic-gate SH_BOUND(shp) = 0; 20327c478bd9Sstevel@tonic-gate (void) door_revoke(SH_DOOR_DESC(shp)); 20337c478bd9Sstevel@tonic-gate (void) fdetach(SH_DOOR_NAME(shp)); 20347c478bd9Sstevel@tonic-gate free(SH_DOOR_NAME(shp)); 20357c478bd9Sstevel@tonic-gate free(pub); 20367c478bd9Sstevel@tonic-gate (void) close(fd); 20377c478bd9Sstevel@tonic-gate (void) mutex_unlock(SH_LOCK(shp)); 20387c478bd9Sstevel@tonic-gate errno = error; 20397c478bd9Sstevel@tonic-gate return (-1); 20407c478bd9Sstevel@tonic-gate } 20417c478bd9Sstevel@tonic-gate 20427c478bd9Sstevel@tonic-gate /* 20437c478bd9Sstevel@tonic-gate * sysevent_bind_subscriber - Bind an event receiver to an event channel 20447c478bd9Sstevel@tonic-gate */ 20457c478bd9Sstevel@tonic-gate int 20467c478bd9Sstevel@tonic-gate sysevent_bind_subscriber(sysevent_handle_t *shp, 20477c478bd9Sstevel@tonic-gate void (*event_handler)(sysevent_t *ev)) 20487c478bd9Sstevel@tonic-gate { 20497c478bd9Sstevel@tonic-gate int fd = -1; 20507c478bd9Sstevel@tonic-gate int error = 0; 20517c478bd9Sstevel@tonic-gate uint32_t sub_id = 0; 20527c478bd9Sstevel@tonic-gate char door_name[MAXPATHLEN]; 20537c478bd9Sstevel@tonic-gate subscriber_priv_t *sub_info; 20547c478bd9Sstevel@tonic-gate 20557c478bd9Sstevel@tonic-gate if (shp == NULL || event_handler == NULL) { 20567c478bd9Sstevel@tonic-gate errno = EINVAL; 20577c478bd9Sstevel@tonic-gate return (-1); 20587c478bd9Sstevel@tonic-gate } 20597c478bd9Sstevel@tonic-gate 20607c478bd9Sstevel@tonic-gate (void) mutex_lock(SH_LOCK(shp)); 20617c478bd9Sstevel@tonic-gate if (SH_BOUND(shp)) { 20627c478bd9Sstevel@tonic-gate errno = EINVAL; 20637c478bd9Sstevel@tonic-gate (void) mutex_unlock(SH_LOCK(shp)); 20647c478bd9Sstevel@tonic-gate return (-1); 20657c478bd9Sstevel@tonic-gate } 20667c478bd9Sstevel@tonic-gate 20677c478bd9Sstevel@tonic-gate if ((sub_info = (subscriber_priv_t *)calloc(1, 20687c478bd9Sstevel@tonic-gate sizeof (subscriber_priv_t))) == NULL) { 20697c478bd9Sstevel@tonic-gate errno = ENOMEM; 20707c478bd9Sstevel@tonic-gate (void) mutex_unlock(SH_LOCK(shp)); 20717c478bd9Sstevel@tonic-gate return (-1); 20727c478bd9Sstevel@tonic-gate } 20737c478bd9Sstevel@tonic-gate 20747c478bd9Sstevel@tonic-gate if (snprintf(door_name, MAXPATHLEN, "%s/%s", 20757c478bd9Sstevel@tonic-gate SH_CHANNEL_PATH(shp), REG_DOOR) >= MAXPATHLEN) { 20767c478bd9Sstevel@tonic-gate free(sub_info); 20777c478bd9Sstevel@tonic-gate errno = EINVAL; 20787c478bd9Sstevel@tonic-gate (void) mutex_unlock(SH_LOCK(shp)); 20797c478bd9Sstevel@tonic-gate return (-1); 20807c478bd9Sstevel@tonic-gate } 20817c478bd9Sstevel@tonic-gate 20827c478bd9Sstevel@tonic-gate if ((sub_info->sp_door_name = strdup(door_name)) == NULL) { 20837c478bd9Sstevel@tonic-gate free(sub_info); 20847c478bd9Sstevel@tonic-gate errno = ENOMEM; 20857c478bd9Sstevel@tonic-gate (void) mutex_unlock(SH_LOCK(shp)); 20867c478bd9Sstevel@tonic-gate return (-1); 20877c478bd9Sstevel@tonic-gate } 20887c478bd9Sstevel@tonic-gate (void) cond_init(&sub_info->sp_cv, USYNC_THREAD, NULL); 20897c478bd9Sstevel@tonic-gate (void) mutex_init(&sub_info->sp_qlock, USYNC_THREAD, NULL); 20907c478bd9Sstevel@tonic-gate sub_info->sp_func = event_handler; 20917c478bd9Sstevel@tonic-gate 20927c478bd9Sstevel@tonic-gate /* Update the in-kernel registration */ 20937c478bd9Sstevel@tonic-gate if (update_kernel_registration(shp, SUBSCRIBER, 20947c478bd9Sstevel@tonic-gate SE_BIND_REGISTRATION, &sub_id, 0, NULL) != 0) { 20957c478bd9Sstevel@tonic-gate error = errno; 20967c478bd9Sstevel@tonic-gate goto fail; 20977c478bd9Sstevel@tonic-gate } 20987c478bd9Sstevel@tonic-gate SH_ID(shp) = sub_id; 20997c478bd9Sstevel@tonic-gate 21007c478bd9Sstevel@tonic-gate if (snprintf(door_name, MAXPATHLEN, "%s/%d", 21017c478bd9Sstevel@tonic-gate SH_CHANNEL_PATH(shp), sub_id) >= MAXPATHLEN) { 21027c478bd9Sstevel@tonic-gate error = EINVAL; 21037c478bd9Sstevel@tonic-gate goto fail; 21047c478bd9Sstevel@tonic-gate } 21057c478bd9Sstevel@tonic-gate if ((SH_DOOR_NAME(shp) = strdup(door_name)) == NULL) { 21067c478bd9Sstevel@tonic-gate error = ENOMEM; 21077c478bd9Sstevel@tonic-gate goto fail; 21087c478bd9Sstevel@tonic-gate } 21097c478bd9Sstevel@tonic-gate 21107c478bd9Sstevel@tonic-gate /* 21117c478bd9Sstevel@tonic-gate * Remove door file for robustness. 21127c478bd9Sstevel@tonic-gate */ 21137c478bd9Sstevel@tonic-gate if (unlink(SH_DOOR_NAME(shp)) != 0) 21147c478bd9Sstevel@tonic-gate dprint("sysevent_bind_subscriber: Unlink of %s failed.\n", 21157c478bd9Sstevel@tonic-gate SH_DOOR_NAME(shp)); 21167c478bd9Sstevel@tonic-gate 21177c478bd9Sstevel@tonic-gate fd = open(SH_DOOR_NAME(shp), O_CREAT|O_RDWR, S_IREAD|S_IWRITE); 21187c478bd9Sstevel@tonic-gate if (fd == -1) { 21197c478bd9Sstevel@tonic-gate error = EFAULT; 21207c478bd9Sstevel@tonic-gate goto fail; 21217c478bd9Sstevel@tonic-gate } 21227c478bd9Sstevel@tonic-gate 21237c478bd9Sstevel@tonic-gate /* 21247c478bd9Sstevel@tonic-gate * Create the sysevent door service for this client. 21257c478bd9Sstevel@tonic-gate * syseventd will use this door service to propagate 21267c478bd9Sstevel@tonic-gate * events to the client. 21277c478bd9Sstevel@tonic-gate */ 21287c478bd9Sstevel@tonic-gate if ((SH_DOOR_DESC(shp) = door_create(event_deliver_service, 21297c478bd9Sstevel@tonic-gate (void *)shp, DOOR_REFUSE_DESC | DOOR_NO_CANCEL)) == -1) { 21307c478bd9Sstevel@tonic-gate dprint("sysevent_bind_subscriber: door create failed: " 21317c478bd9Sstevel@tonic-gate "%s\n", strerror(errno)); 21327c478bd9Sstevel@tonic-gate error = EFAULT; 21337c478bd9Sstevel@tonic-gate goto fail; 21347c478bd9Sstevel@tonic-gate } 21357c478bd9Sstevel@tonic-gate 21367c478bd9Sstevel@tonic-gate (void) fdetach(SH_DOOR_NAME(shp)); 21377c478bd9Sstevel@tonic-gate if (fattach(SH_DOOR_DESC(shp), SH_DOOR_NAME(shp)) != 0) { 21387c478bd9Sstevel@tonic-gate error = EFAULT; 21397c478bd9Sstevel@tonic-gate goto fail; 21407c478bd9Sstevel@tonic-gate } 21417c478bd9Sstevel@tonic-gate (void) close(fd); 21427c478bd9Sstevel@tonic-gate 21437c478bd9Sstevel@tonic-gate if (update_publisher_cache(sub_info, SE_BIND_REGISTRATION, 21447c478bd9Sstevel@tonic-gate sub_id, 0, NULL) != 0) { 21457c478bd9Sstevel@tonic-gate error = errno; 21467c478bd9Sstevel@tonic-gate (void) update_kernel_registration(shp, SUBSCRIBER, 21477c478bd9Sstevel@tonic-gate SE_UNBIND_REGISTRATION, &sub_id, 0, NULL); 21487c478bd9Sstevel@tonic-gate goto fail; 21497c478bd9Sstevel@tonic-gate } 21507c478bd9Sstevel@tonic-gate 21517c478bd9Sstevel@tonic-gate SH_BOUND(shp) = 1; 21527c478bd9Sstevel@tonic-gate SH_TYPE(shp) = SUBSCRIBER; 21537c478bd9Sstevel@tonic-gate SH_PRIV_DATA(shp) = (void *)sub_info; 21547c478bd9Sstevel@tonic-gate 21557c478bd9Sstevel@tonic-gate 21567c478bd9Sstevel@tonic-gate /* Create an event handler thread */ 21577c478bd9Sstevel@tonic-gate if (thr_create(NULL, NULL, (void *(*)(void *))subscriber_event_handler, 21587c478bd9Sstevel@tonic-gate shp, THR_BOUND, &sub_info->sp_handler_tid) != 0) { 21597c478bd9Sstevel@tonic-gate error = EFAULT; 21607c478bd9Sstevel@tonic-gate goto fail; 21617c478bd9Sstevel@tonic-gate } 21627c478bd9Sstevel@tonic-gate 21637c478bd9Sstevel@tonic-gate (void) mutex_unlock(SH_LOCK(shp)); 21647c478bd9Sstevel@tonic-gate 21657c478bd9Sstevel@tonic-gate return (0); 21667c478bd9Sstevel@tonic-gate 21677c478bd9Sstevel@tonic-gate fail: 21687c478bd9Sstevel@tonic-gate (void) close(fd); 21697c478bd9Sstevel@tonic-gate (void) door_revoke(SH_DOOR_DESC(shp)); 21707c478bd9Sstevel@tonic-gate (void) fdetach(SH_DOOR_NAME(shp)); 21717c478bd9Sstevel@tonic-gate (void) cond_destroy(&sub_info->sp_cv); 21727c478bd9Sstevel@tonic-gate (void) mutex_destroy(&sub_info->sp_qlock); 21737c478bd9Sstevel@tonic-gate free(sub_info->sp_door_name); 21747c478bd9Sstevel@tonic-gate free(sub_info); 21757c478bd9Sstevel@tonic-gate if (SH_ID(shp)) { 21767c478bd9Sstevel@tonic-gate (void) update_kernel_registration(shp, SUBSCRIBER, 21777c478bd9Sstevel@tonic-gate SE_UNBIND_REGISTRATION, &sub_id, 0, NULL); 21787c478bd9Sstevel@tonic-gate SH_ID(shp) = 0; 21797c478bd9Sstevel@tonic-gate } 21807c478bd9Sstevel@tonic-gate if (SH_BOUND(shp)) { 21817c478bd9Sstevel@tonic-gate (void) update_publisher_cache(sub_info, SE_UNBIND_REGISTRATION, 21827c478bd9Sstevel@tonic-gate sub_id, 0, NULL); 21837c478bd9Sstevel@tonic-gate free(SH_DOOR_NAME(shp)); 21847c478bd9Sstevel@tonic-gate SH_BOUND(shp) = 0; 21857c478bd9Sstevel@tonic-gate } 21867c478bd9Sstevel@tonic-gate (void) mutex_unlock(SH_LOCK(shp)); 21877c478bd9Sstevel@tonic-gate 21887c478bd9Sstevel@tonic-gate errno = error; 21897c478bd9Sstevel@tonic-gate 21907c478bd9Sstevel@tonic-gate return (-1); 21917c478bd9Sstevel@tonic-gate } 21927c478bd9Sstevel@tonic-gate 21937c478bd9Sstevel@tonic-gate /* 21947c478bd9Sstevel@tonic-gate * sysevent_register_event - register an event class and associated subclasses 21957c478bd9Sstevel@tonic-gate * for an event subscriber 21967c478bd9Sstevel@tonic-gate */ 21977c478bd9Sstevel@tonic-gate int 21987c478bd9Sstevel@tonic-gate sysevent_register_event(sysevent_handle_t *shp, 21997c478bd9Sstevel@tonic-gate const char *ev_class, const char **ev_subclass, 22007c478bd9Sstevel@tonic-gate int subclass_num) 22017c478bd9Sstevel@tonic-gate { 22027c478bd9Sstevel@tonic-gate int error; 22037c478bd9Sstevel@tonic-gate char *event_class = (char *)ev_class; 22047c478bd9Sstevel@tonic-gate char **event_subclass_list = (char **)ev_subclass; 22057c478bd9Sstevel@tonic-gate char *nvlbuf = NULL; 22067c478bd9Sstevel@tonic-gate size_t datalen; 22077c478bd9Sstevel@tonic-gate nvlist_t *nvl; 22087c478bd9Sstevel@tonic-gate 22097c478bd9Sstevel@tonic-gate (void) mutex_lock(SH_LOCK(shp)); 22107c478bd9Sstevel@tonic-gate if (event_class == NULL || event_subclass_list == NULL || 22117c478bd9Sstevel@tonic-gate event_subclass_list[0] == NULL || SH_BOUND(shp) != 1 || 22127c478bd9Sstevel@tonic-gate subclass_num <= 0) { 22137c478bd9Sstevel@tonic-gate (void) mutex_unlock(SH_LOCK(shp)); 22147c478bd9Sstevel@tonic-gate errno = EINVAL; 22157c478bd9Sstevel@tonic-gate return (-1); 22167c478bd9Sstevel@tonic-gate } 22177c478bd9Sstevel@tonic-gate 22187c478bd9Sstevel@tonic-gate if (nvlist_alloc(&nvl, NV_UNIQUE_NAME_TYPE, 0) != 0) { 22197c478bd9Sstevel@tonic-gate (void) mutex_unlock(SH_LOCK(shp)); 22207c478bd9Sstevel@tonic-gate return (-1); 22217c478bd9Sstevel@tonic-gate } 22227c478bd9Sstevel@tonic-gate if (nvlist_add_string_array(nvl, event_class, event_subclass_list, 22237c478bd9Sstevel@tonic-gate subclass_num) != 0) { 22247c478bd9Sstevel@tonic-gate nvlist_free(nvl); 22257c478bd9Sstevel@tonic-gate (void) mutex_unlock(SH_LOCK(shp)); 22267c478bd9Sstevel@tonic-gate return (-1); 22277c478bd9Sstevel@tonic-gate } 22287c478bd9Sstevel@tonic-gate if (nvlist_pack(nvl, &nvlbuf, &datalen, NV_ENCODE_NATIVE, 0) != 0) { 22297c478bd9Sstevel@tonic-gate nvlist_free(nvl); 22307c478bd9Sstevel@tonic-gate (void) mutex_unlock(SH_LOCK(shp)); 22317c478bd9Sstevel@tonic-gate return (-1); 22327c478bd9Sstevel@tonic-gate } 22337c478bd9Sstevel@tonic-gate nvlist_free(nvl); 22347c478bd9Sstevel@tonic-gate 22357c478bd9Sstevel@tonic-gate /* Store new subscriber in in-kernel registration */ 22367c478bd9Sstevel@tonic-gate if (update_kernel_registration(shp, SUBSCRIBER, 22377c478bd9Sstevel@tonic-gate SE_REGISTER, &SH_ID(shp), datalen, (uchar_t *)nvlbuf) 22387c478bd9Sstevel@tonic-gate != 0) { 22397c478bd9Sstevel@tonic-gate error = errno; 22407c478bd9Sstevel@tonic-gate free(nvlbuf); 22417c478bd9Sstevel@tonic-gate (void) mutex_unlock(SH_LOCK(shp)); 22427c478bd9Sstevel@tonic-gate errno = error; 22437c478bd9Sstevel@tonic-gate return (-1); 22447c478bd9Sstevel@tonic-gate } 22457c478bd9Sstevel@tonic-gate /* Update the publisher's cached registration */ 22467c478bd9Sstevel@tonic-gate if (update_publisher_cache( 22477c478bd9Sstevel@tonic-gate (subscriber_priv_t *)SH_PRIV_DATA(shp), SE_REGISTER, 22487c478bd9Sstevel@tonic-gate SH_ID(shp), datalen, (uchar_t *)nvlbuf) != 0) { 22497c478bd9Sstevel@tonic-gate error = errno; 22507c478bd9Sstevel@tonic-gate free(nvlbuf); 22517c478bd9Sstevel@tonic-gate (void) mutex_unlock(SH_LOCK(shp)); 22527c478bd9Sstevel@tonic-gate errno = error; 22537c478bd9Sstevel@tonic-gate return (-1); 22547c478bd9Sstevel@tonic-gate } 22557c478bd9Sstevel@tonic-gate 22567c478bd9Sstevel@tonic-gate free(nvlbuf); 22577c478bd9Sstevel@tonic-gate 22587c478bd9Sstevel@tonic-gate (void) mutex_unlock(SH_LOCK(shp)); 22597c478bd9Sstevel@tonic-gate 22607c478bd9Sstevel@tonic-gate return (0); 22617c478bd9Sstevel@tonic-gate } 22627c478bd9Sstevel@tonic-gate 22637c478bd9Sstevel@tonic-gate /* 22647c478bd9Sstevel@tonic-gate * sysevent_unregister_event - Unregister an event class and associated 22657c478bd9Sstevel@tonic-gate * subclasses for an event subscriber 22667c478bd9Sstevel@tonic-gate */ 22677c478bd9Sstevel@tonic-gate void 22687c478bd9Sstevel@tonic-gate sysevent_unregister_event(sysevent_handle_t *shp, const char *class) 22697c478bd9Sstevel@tonic-gate { 22707c478bd9Sstevel@tonic-gate size_t class_sz; 22717c478bd9Sstevel@tonic-gate 22727c478bd9Sstevel@tonic-gate (void) mutex_lock(SH_LOCK(shp)); 22737c478bd9Sstevel@tonic-gate 22747c478bd9Sstevel@tonic-gate if (!SH_BOUND(shp)) { 22757c478bd9Sstevel@tonic-gate (void) mutex_unlock(SH_LOCK(shp)); 22767c478bd9Sstevel@tonic-gate return; 22777c478bd9Sstevel@tonic-gate } 22787c478bd9Sstevel@tonic-gate 22797c478bd9Sstevel@tonic-gate /* Remove subscriber from in-kernel registration */ 22807c478bd9Sstevel@tonic-gate class_sz = strlen(class) + 1; 22817c478bd9Sstevel@tonic-gate (void) update_kernel_registration(shp, SUBSCRIBER, 22827c478bd9Sstevel@tonic-gate SE_UNREGISTER, &SH_ID(shp), class_sz, (uchar_t *)class); 22837c478bd9Sstevel@tonic-gate /* Update the publisher's cached registration */ 22847c478bd9Sstevel@tonic-gate (void) update_publisher_cache( 22857c478bd9Sstevel@tonic-gate (subscriber_priv_t *)SH_PRIV_DATA(shp), SE_UNREGISTER, 22867c478bd9Sstevel@tonic-gate SH_ID(shp), class_sz, (uchar_t *)class); 22877c478bd9Sstevel@tonic-gate 22887c478bd9Sstevel@tonic-gate (void) mutex_unlock(SH_LOCK(shp)); 22897c478bd9Sstevel@tonic-gate } 22907c478bd9Sstevel@tonic-gate 22917c478bd9Sstevel@tonic-gate static int 22927c478bd9Sstevel@tonic-gate cleanup_id(sysevent_handle_t *shp, uint32_t id, int type) 22937c478bd9Sstevel@tonic-gate { 22947c478bd9Sstevel@tonic-gate dprint("cleanup_id: Cleaning up %s/%d\n", SH_CHANNEL_NAME(shp), id); 22957c478bd9Sstevel@tonic-gate 22967c478bd9Sstevel@tonic-gate /* Remove registration from the kernel */ 22977c478bd9Sstevel@tonic-gate if (update_kernel_registration(shp, type, SE_CLEANUP, &id, 22987c478bd9Sstevel@tonic-gate 0, NULL) != 0) { 22997c478bd9Sstevel@tonic-gate dprint("cleanup_id: Unable to clean " 23007c478bd9Sstevel@tonic-gate "up %s/%d\n", SH_CHANNEL_NAME(shp), id); 23017c478bd9Sstevel@tonic-gate return (-1); 23027c478bd9Sstevel@tonic-gate } 23037c478bd9Sstevel@tonic-gate 23047c478bd9Sstevel@tonic-gate return (0); 23057c478bd9Sstevel@tonic-gate } 23067c478bd9Sstevel@tonic-gate 23077c478bd9Sstevel@tonic-gate /* 23087c478bd9Sstevel@tonic-gate * sysevent_cleanup_subscribers: Allows the caller to cleanup resources 23097c478bd9Sstevel@tonic-gate * allocated to unresponsive subscribers. 23107c478bd9Sstevel@tonic-gate */ 23117c478bd9Sstevel@tonic-gate void 23127c478bd9Sstevel@tonic-gate sysevent_cleanup_subscribers(sysevent_handle_t *shp) 23137c478bd9Sstevel@tonic-gate { 23147c478bd9Sstevel@tonic-gate uint32_t ping, result; 23157c478bd9Sstevel@tonic-gate int i, error, sub_fd; 23167c478bd9Sstevel@tonic-gate subscriber_data_t *sub; 23177c478bd9Sstevel@tonic-gate 23187c478bd9Sstevel@tonic-gate if (!SH_BOUND(shp)) { 23197c478bd9Sstevel@tonic-gate return; 23207c478bd9Sstevel@tonic-gate } 23217c478bd9Sstevel@tonic-gate 23227c478bd9Sstevel@tonic-gate for (i = 1; i <= MAX_SUBSCRIBERS; ++i) { 23237c478bd9Sstevel@tonic-gate 23247c478bd9Sstevel@tonic-gate sub = SH_SUBSCRIBER(shp, i); 23257c478bd9Sstevel@tonic-gate if (sub == NULL) { 23267c478bd9Sstevel@tonic-gate continue; 23277c478bd9Sstevel@tonic-gate } 23287c478bd9Sstevel@tonic-gate 23297c478bd9Sstevel@tonic-gate if ((sub_fd = open(sub->sd_door_name, O_RDONLY)) == -1) { 23307c478bd9Sstevel@tonic-gate continue; 23317c478bd9Sstevel@tonic-gate } 23327c478bd9Sstevel@tonic-gate /* Check for valid and responsive subscriber */ 23337c478bd9Sstevel@tonic-gate error = clnt_deliver_event(sub_fd, &ping, 23347c478bd9Sstevel@tonic-gate sizeof (uint32_t), &result, sizeof (result)); 23357c478bd9Sstevel@tonic-gate (void) close(sub_fd); 23367c478bd9Sstevel@tonic-gate 23377c478bd9Sstevel@tonic-gate /* Only cleanup on EBADF (Invalid door descriptor) */ 23387c478bd9Sstevel@tonic-gate if (error != EBADF) 23397c478bd9Sstevel@tonic-gate continue; 23407c478bd9Sstevel@tonic-gate 23417c478bd9Sstevel@tonic-gate if (cleanup_id(shp, i, SUBSCRIBER) != 0) 23427c478bd9Sstevel@tonic-gate continue; 23437c478bd9Sstevel@tonic-gate 23447c478bd9Sstevel@tonic-gate cache_remove_class(shp, EC_ALL, i); 23457c478bd9Sstevel@tonic-gate 23467c478bd9Sstevel@tonic-gate free(sub->sd_door_name); 23477c478bd9Sstevel@tonic-gate free(sub); 23487c478bd9Sstevel@tonic-gate SH_SUBSCRIBER(shp, i) = NULL; 23497c478bd9Sstevel@tonic-gate } 23507c478bd9Sstevel@tonic-gate 23517c478bd9Sstevel@tonic-gate } 23527c478bd9Sstevel@tonic-gate 23537c478bd9Sstevel@tonic-gate /* 23547c478bd9Sstevel@tonic-gate * sysevent_cleanup_publishers: Allows stale publisher handles to be deallocated 23557c478bd9Sstevel@tonic-gate * as needed. 23567c478bd9Sstevel@tonic-gate */ 23577c478bd9Sstevel@tonic-gate void 23587c478bd9Sstevel@tonic-gate sysevent_cleanup_publishers(sysevent_handle_t *shp) 23597c478bd9Sstevel@tonic-gate { 23607c478bd9Sstevel@tonic-gate (void) cleanup_id(shp, 1, PUBLISHER); 23617c478bd9Sstevel@tonic-gate } 23627c478bd9Sstevel@tonic-gate 23637c478bd9Sstevel@tonic-gate /* 23647c478bd9Sstevel@tonic-gate * sysevent_unbind_subscriber: Unbind the subscriber from the sysevent channel. 23657c478bd9Sstevel@tonic-gate */ 23667c478bd9Sstevel@tonic-gate void 23677c478bd9Sstevel@tonic-gate sysevent_unbind_subscriber(sysevent_handle_t *shp) 23687c478bd9Sstevel@tonic-gate { 23697c478bd9Sstevel@tonic-gate subscriber_priv_t *sub_info; 23707c478bd9Sstevel@tonic-gate 23717c478bd9Sstevel@tonic-gate if (shp == NULL) 23727c478bd9Sstevel@tonic-gate return; 23737c478bd9Sstevel@tonic-gate 23747c478bd9Sstevel@tonic-gate (void) mutex_lock(SH_LOCK(shp)); 23757c478bd9Sstevel@tonic-gate if (SH_BOUND(shp) == 0) { 23767c478bd9Sstevel@tonic-gate (void) mutex_unlock(SH_LOCK(shp)); 23777c478bd9Sstevel@tonic-gate return; 23787c478bd9Sstevel@tonic-gate } 23797c478bd9Sstevel@tonic-gate 23807c478bd9Sstevel@tonic-gate /* Update the in-kernel registration */ 23817c478bd9Sstevel@tonic-gate (void) update_kernel_registration(shp, SUBSCRIBER, 23827c478bd9Sstevel@tonic-gate SE_UNBIND_REGISTRATION, &SH_ID(shp), 0, NULL); 23837c478bd9Sstevel@tonic-gate 23847c478bd9Sstevel@tonic-gate /* Update the sysevent channel publisher */ 23857c478bd9Sstevel@tonic-gate sub_info = (subscriber_priv_t *)SH_PRIV_DATA(shp); 23867c478bd9Sstevel@tonic-gate (void) update_publisher_cache(sub_info, SE_UNBIND_REGISTRATION, 23877c478bd9Sstevel@tonic-gate SH_ID(shp), 0, NULL); 23887c478bd9Sstevel@tonic-gate 23897c478bd9Sstevel@tonic-gate /* Close down event delivery facilities */ 23907c478bd9Sstevel@tonic-gate (void) door_revoke(SH_DOOR_DESC(shp)); 23917c478bd9Sstevel@tonic-gate (void) fdetach(SH_DOOR_NAME(shp)); 23927c478bd9Sstevel@tonic-gate 23937c478bd9Sstevel@tonic-gate 23947c478bd9Sstevel@tonic-gate /* 23957c478bd9Sstevel@tonic-gate * Release resources and wait for pending event delivery to 23967c478bd9Sstevel@tonic-gate * complete. 23977c478bd9Sstevel@tonic-gate */ 23987c478bd9Sstevel@tonic-gate (void) mutex_lock(&sub_info->sp_qlock); 23997c478bd9Sstevel@tonic-gate SH_BOUND(shp) = 0; 24007c478bd9Sstevel@tonic-gate /* Signal event handler and drain the subscriber's event queue */ 24017c478bd9Sstevel@tonic-gate (void) cond_signal(&sub_info->sp_cv); 24027c478bd9Sstevel@tonic-gate (void) mutex_unlock(&sub_info->sp_qlock); 24037c478bd9Sstevel@tonic-gate (void) thr_join(sub_info->sp_handler_tid, NULL, NULL); 24047c478bd9Sstevel@tonic-gate 24057c478bd9Sstevel@tonic-gate (void) cond_destroy(&sub_info->sp_cv); 24067c478bd9Sstevel@tonic-gate (void) mutex_destroy(&sub_info->sp_qlock); 24077c478bd9Sstevel@tonic-gate free(sub_info->sp_door_name); 24087c478bd9Sstevel@tonic-gate free(sub_info); 24097c478bd9Sstevel@tonic-gate free(SH_DOOR_NAME(shp)); 24107c478bd9Sstevel@tonic-gate (void) mutex_unlock(SH_LOCK(shp)); 24117c478bd9Sstevel@tonic-gate } 24127c478bd9Sstevel@tonic-gate 24137c478bd9Sstevel@tonic-gate /* 24147c478bd9Sstevel@tonic-gate * sysevent_unbind_publisher: Unbind publisher from the sysevent channel. 24157c478bd9Sstevel@tonic-gate */ 24167c478bd9Sstevel@tonic-gate void 24177c478bd9Sstevel@tonic-gate sysevent_unbind_publisher(sysevent_handle_t *shp) 24187c478bd9Sstevel@tonic-gate { 24197c478bd9Sstevel@tonic-gate if (shp == NULL) 24207c478bd9Sstevel@tonic-gate return; 24217c478bd9Sstevel@tonic-gate 24227c478bd9Sstevel@tonic-gate (void) mutex_lock(SH_LOCK(shp)); 24237c478bd9Sstevel@tonic-gate if (SH_BOUND(shp) == 0) { 24247c478bd9Sstevel@tonic-gate (void) mutex_unlock(SH_LOCK(shp)); 24257c478bd9Sstevel@tonic-gate return; 24267c478bd9Sstevel@tonic-gate } 24277c478bd9Sstevel@tonic-gate 24287c478bd9Sstevel@tonic-gate /* Close down the registration facilities */ 24297c478bd9Sstevel@tonic-gate (void) door_revoke(SH_DOOR_DESC(shp)); 24307c478bd9Sstevel@tonic-gate (void) fdetach(SH_DOOR_NAME(shp)); 24317c478bd9Sstevel@tonic-gate 24327c478bd9Sstevel@tonic-gate /* Update the in-kernel registration */ 24337c478bd9Sstevel@tonic-gate (void) update_kernel_registration(shp, PUBLISHER, 24347c478bd9Sstevel@tonic-gate SE_UNBIND_REGISTRATION, &SH_ID(shp), 0, NULL); 24357c478bd9Sstevel@tonic-gate SH_BOUND(shp) = 0; 24367c478bd9Sstevel@tonic-gate 24377c478bd9Sstevel@tonic-gate /* Free resources associated with bind */ 24387c478bd9Sstevel@tonic-gate free_cached_registration(shp); 24397c478bd9Sstevel@tonic-gate dealloc_subscribers(shp); 24407c478bd9Sstevel@tonic-gate 24417c478bd9Sstevel@tonic-gate free(SH_PRIV_DATA(shp)); 24427c478bd9Sstevel@tonic-gate free(SH_DOOR_NAME(shp)); 24437c478bd9Sstevel@tonic-gate SH_ID(shp) = 0; 24447c478bd9Sstevel@tonic-gate (void) mutex_unlock(SH_LOCK(shp)); 24457c478bd9Sstevel@tonic-gate } 24467c478bd9Sstevel@tonic-gate 24477c478bd9Sstevel@tonic-gate /* 24487c478bd9Sstevel@tonic-gate * Evolving APIs to subscribe to syseventd(1M) system events. 24497c478bd9Sstevel@tonic-gate */ 24507c478bd9Sstevel@tonic-gate 24517c478bd9Sstevel@tonic-gate /* 24527c478bd9Sstevel@tonic-gate * sysevent_bind_handle - Bind application event handler for syseventd 24537c478bd9Sstevel@tonic-gate * subscription. 24547c478bd9Sstevel@tonic-gate */ 24557c478bd9Sstevel@tonic-gate sysevent_handle_t * 24567c478bd9Sstevel@tonic-gate sysevent_bind_handle(void (*event_handler)(sysevent_t *ev)) 24577c478bd9Sstevel@tonic-gate { 24587c478bd9Sstevel@tonic-gate sysevent_handle_t *shp; 24597c478bd9Sstevel@tonic-gate 24607c478bd9Sstevel@tonic-gate if (getuid() != 0) { 24617c478bd9Sstevel@tonic-gate errno = EACCES; 24627c478bd9Sstevel@tonic-gate return (NULL); 24637c478bd9Sstevel@tonic-gate } 24647c478bd9Sstevel@tonic-gate 24657c478bd9Sstevel@tonic-gate if (event_handler == NULL) { 24667c478bd9Sstevel@tonic-gate errno = EINVAL; 24677c478bd9Sstevel@tonic-gate return (NULL); 24687c478bd9Sstevel@tonic-gate } 24697c478bd9Sstevel@tonic-gate 24707c478bd9Sstevel@tonic-gate if ((shp = sysevent_open_channel(SYSEVENTD_CHAN)) == NULL) { 24717c478bd9Sstevel@tonic-gate return (NULL); 24727c478bd9Sstevel@tonic-gate } 24737c478bd9Sstevel@tonic-gate 24747c478bd9Sstevel@tonic-gate if (sysevent_bind_subscriber(shp, event_handler) != 0) { 24757c478bd9Sstevel@tonic-gate 24767c478bd9Sstevel@tonic-gate /* 24777c478bd9Sstevel@tonic-gate * Ask syseventd to clean-up any stale subcribers and try to 24787c478bd9Sstevel@tonic-gate * to bind again 24797c478bd9Sstevel@tonic-gate */ 24807c478bd9Sstevel@tonic-gate if (errno == EBUSY) { 24817c478bd9Sstevel@tonic-gate int pub_fd; 24827c478bd9Sstevel@tonic-gate char door_name[MAXPATHLEN]; 24837c478bd9Sstevel@tonic-gate uint32_t result; 24847c478bd9Sstevel@tonic-gate struct reg_args rargs; 24857c478bd9Sstevel@tonic-gate 24867c478bd9Sstevel@tonic-gate if (snprintf(door_name, MAXPATHLEN, "%s/%s", 24877c478bd9Sstevel@tonic-gate SH_CHANNEL_PATH(shp), REG_DOOR) >= MAXPATHLEN) { 24887c478bd9Sstevel@tonic-gate sysevent_close_channel(shp); 24897c478bd9Sstevel@tonic-gate errno = EINVAL; 24907c478bd9Sstevel@tonic-gate return (NULL); 24917c478bd9Sstevel@tonic-gate } 24927c478bd9Sstevel@tonic-gate 24937c478bd9Sstevel@tonic-gate rargs.ra_op = SE_CLEANUP; 24947c478bd9Sstevel@tonic-gate pub_fd = open(door_name, O_RDONLY); 24957c478bd9Sstevel@tonic-gate (void) clnt_deliver_event(pub_fd, (void *)&rargs, 24967c478bd9Sstevel@tonic-gate sizeof (struct reg_args), &result, sizeof (result)); 24977c478bd9Sstevel@tonic-gate (void) close(pub_fd); 24987c478bd9Sstevel@tonic-gate 24997c478bd9Sstevel@tonic-gate /* Try to bind again */ 25007c478bd9Sstevel@tonic-gate if (sysevent_bind_subscriber(shp, event_handler) != 0) { 25017c478bd9Sstevel@tonic-gate sysevent_close_channel(shp); 25027c478bd9Sstevel@tonic-gate return (NULL); 25037c478bd9Sstevel@tonic-gate } 25047c478bd9Sstevel@tonic-gate } else { 25057c478bd9Sstevel@tonic-gate sysevent_close_channel(shp); 25067c478bd9Sstevel@tonic-gate return (NULL); 25077c478bd9Sstevel@tonic-gate } 25087c478bd9Sstevel@tonic-gate } 25097c478bd9Sstevel@tonic-gate 25107c478bd9Sstevel@tonic-gate return (shp); 25117c478bd9Sstevel@tonic-gate } 25127c478bd9Sstevel@tonic-gate 25137c478bd9Sstevel@tonic-gate /* 25147c478bd9Sstevel@tonic-gate * sysevent_unbind_handle - Unbind caller from syseventd subscriptions 25157c478bd9Sstevel@tonic-gate */ 25167c478bd9Sstevel@tonic-gate void 25177c478bd9Sstevel@tonic-gate sysevent_unbind_handle(sysevent_handle_t *shp) 25187c478bd9Sstevel@tonic-gate { 25197c478bd9Sstevel@tonic-gate sysevent_unbind_subscriber(shp); 25207c478bd9Sstevel@tonic-gate sysevent_close_channel(shp); 25217c478bd9Sstevel@tonic-gate } 25227c478bd9Sstevel@tonic-gate 25237c478bd9Sstevel@tonic-gate /* 25247c478bd9Sstevel@tonic-gate * sysevent_subscribe_event - Subscribe to system event notification from 25257c478bd9Sstevel@tonic-gate * syseventd(1M) for the class and subclasses specified. 25267c478bd9Sstevel@tonic-gate */ 25277c478bd9Sstevel@tonic-gate int 25287c478bd9Sstevel@tonic-gate sysevent_subscribe_event(sysevent_handle_t *shp, const char *event_class, 25297c478bd9Sstevel@tonic-gate const char **event_subclass_list, int num_subclasses) 25307c478bd9Sstevel@tonic-gate { 25317c478bd9Sstevel@tonic-gate return (sysevent_register_event(shp, event_class, 25327c478bd9Sstevel@tonic-gate event_subclass_list, num_subclasses)); 25337c478bd9Sstevel@tonic-gate } 25347c478bd9Sstevel@tonic-gate 25357c478bd9Sstevel@tonic-gate void 25367c478bd9Sstevel@tonic-gate sysevent_unsubscribe_event(sysevent_handle_t *shp, const char *event_class) 25377c478bd9Sstevel@tonic-gate { 25387c478bd9Sstevel@tonic-gate sysevent_unregister_event(shp, event_class); 25397c478bd9Sstevel@tonic-gate } 2540