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