13b005d3pjd/*
23b005d3pjd * CDDL HEADER START
33b005d3pjd *
43b005d3pjd * The contents of this file are subject to the terms of the
51b03c5bpjd * Common Development and Distribution License (the "License").
61b03c5bpjd * You may not use this file except in compliance with the License.
73b005d3pjd *
83b005d3pjd * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
93b005d3pjd * or http://www.opensolaris.org/os/licensing.
103b005d3pjd * See the License for the specific language governing permissions
113b005d3pjd * and limitations under the License.
123b005d3pjd *
133b005d3pjd * When distributing Covered Code, include this CDDL HEADER in each
143b005d3pjd * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
153b005d3pjd * If applicable, add the following below this CDDL HEADER, with the
163b005d3pjd * fields enclosed by brackets "[]" replaced with your own identifying
173b005d3pjd * information: Portions Copyright [yyyy] [name of copyright owner]
183b005d3pjd *
193b005d3pjd * CDDL HEADER END
203b005d3pjd */
211b03c5bpjd
223b005d3pjd/*
231b03c5bpjd * Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
243b005d3pjd */
253b005d3pjd
263b005d3pjd#ifndef	_SYS_SYSEVENT_H
273b005d3pjd#define	_SYS_SYSEVENT_H
283b005d3pjd
293b005d3pjd#include <sys/nvpair.h>
303b005d3pjd
313b005d3pjd#ifdef	__cplusplus
323b005d3pjdextern "C" {
333b005d3pjd#endif
343b005d3pjd
353b005d3pjd#ifndef	NULL
363b005d3pjd#if defined(_LP64) && !defined(__cplusplus)
373b005d3pjd#define	NULL    0L
383b005d3pjd#else
393b005d3pjd#define	NULL	0
403b005d3pjd#endif
413b005d3pjd#endif
423b005d3pjd
433b005d3pjd/* Internal registration class and subclass */
443b005d3pjd#define	EC_ALL		"register_all_classes"
453b005d3pjd#define	EC_SUB_ALL	"register_all_subclasses"
463b005d3pjd
473b005d3pjd/*
483b005d3pjd * Event allocation/enqueuing sleep/nosleep flags
493b005d3pjd */
503b005d3pjd#define	SE_SLEEP		0
513b005d3pjd#define	SE_NOSLEEP		1
523b005d3pjd
533b005d3pjd/* Framework error codes */
543b005d3pjd#define	SE_EINVAL		1	/* Invalid argument */
553b005d3pjd#define	SE_ENOMEM		2	/* Unable to allocate memory */
563b005d3pjd#define	SE_EQSIZE		3	/* Maximum event q size exceeded */
573b005d3pjd#define	SE_EFAULT		4	/* Copy fault */
583b005d3pjd#define	SE_NOTFOUND		5	/* Attribute not found */
593b005d3pjd#define	SE_NO_TRANSPORT		6	/* sysevent transport down */
603b005d3pjd
613b005d3pjd/* Internal data types */
623b005d3pjd
633b005d3pjd#define	SE_DATA_TYPE_BYTE	DATA_TYPE_BYTE
643b005d3pjd#define	SE_DATA_TYPE_INT16	DATA_TYPE_INT16
653b005d3pjd#define	SE_DATA_TYPE_UINT16	DATA_TYPE_UINT16
663b005d3pjd#define	SE_DATA_TYPE_INT32	DATA_TYPE_INT32
673b005d3pjd#define	SE_DATA_TYPE_UINT32	DATA_TYPE_UINT32
683b005d3pjd#define	SE_DATA_TYPE_INT64	DATA_TYPE_INT64
693b005d3pjd#define	SE_DATA_TYPE_UINT64	DATA_TYPE_UINT64
703b005d3pjd#define	SE_DATA_TYPE_STRING	DATA_TYPE_STRING
713b005d3pjd#define	SE_DATA_TYPE_BYTES	DATA_TYPE_BYTE_ARRAY
723b005d3pjd#define	SE_DATA_TYPE_TIME	DATA_TYPE_HRTIME
733b005d3pjd
743b005d3pjd#define	SE_KERN_PID	0
753b005d3pjd
763b005d3pjd#define	SUNW_VENDOR	"SUNW"
773b005d3pjd#define	SE_USR_PUB	"usr:"
783b005d3pjd#define	SE_KERN_PUB	"kern:"
79996c429bdrewery#define	SUNW_KERN_PUB	SUNW_VENDOR ":" SE_KERN_PUB
80996c429bdrewery#define	SUNW_USR_PUB	SUNW_VENDOR ":" SE_USR_PUB
813b005d3pjd
823b005d3pjd/*
833b005d3pjd * Event header and attribute value limits
843b005d3pjd */
853b005d3pjd#define	MAX_ATTR_NAME	1024
863b005d3pjd#define	MAX_STRING_SZ	1024
873b005d3pjd#define	MAX_BYTE_ARRAY	1024
883b005d3pjd
893b005d3pjd#define	MAX_CLASS_LEN		64
903b005d3pjd#define	MAX_SUBCLASS_LEN	64
913b005d3pjd#define	MAX_PUB_LEN		128
923b005d3pjd#define	MAX_CHNAME_LEN		128
933b005d3pjd#define	MAX_SUBID_LEN		16
943b005d3pjd
953b005d3pjd/*
963b005d3pjd * Limit for the event payload size
973b005d3pjd */
983b005d3pjd#define	MAX_EV_SIZE_LEN		(SHRT_MAX/4)
993b005d3pjd
1003b005d3pjd/* Opaque sysevent_t data type */
1013b005d3pjdtypedef void *sysevent_t;
1023b005d3pjd
1033b005d3pjd/* Opaque channel bind data type */
1043b005d3pjdtypedef void evchan_t;
1053b005d3pjd
1063b005d3pjd/* sysevent attribute list */
1073b005d3pjdtypedef nvlist_t sysevent_attr_list_t;
1083b005d3pjd
1093b005d3pjd/* sysevent attribute name-value pair */
1103b005d3pjdtypedef nvpair_t sysevent_attr_t;
1113b005d3pjd
1123b005d3pjd/* Unique event identifier */
1133b005d3pjdtypedef struct sysevent_id {
1143b005d3pjd	uint64_t eid_seq;
1153b005d3pjd	hrtime_t eid_ts;
1163b005d3pjd} sysevent_id_t;
1173b005d3pjd
1183b005d3pjd/* Event attribute value structures */
1193b005d3pjdtypedef struct sysevent_bytes {
1203b005d3pjd	int32_t	size;
1213b005d3pjd	uchar_t	*data;
1223b005d3pjd} sysevent_bytes_t;
1233b005d3pjd
1243b005d3pjdtypedef struct sysevent_value {
1253b005d3pjd	int32_t		value_type;		/* data type */
1263b005d3pjd	union {
1273b005d3pjd		uchar_t		sv_byte;
1283b005d3pjd		int16_t		sv_int16;
1293b005d3pjd		uint16_t	sv_uint16;
1303b005d3pjd		int32_t		sv_int32;
1313b005d3pjd		uint32_t	sv_uint32;
1323b005d3pjd		int64_t		sv_int64;
1333b005d3pjd		uint64_t	sv_uint64;
1343b005d3pjd		hrtime_t	sv_time;
1353b005d3pjd		char		*sv_string;
1363b005d3pjd		sysevent_bytes_t	sv_bytes;
1373b005d3pjd	} value;
1383b005d3pjd} sysevent_value_t;
1393b005d3pjd
1403b005d3pjd/*
1413b005d3pjd * The following flags determine the memory allocation semantics to use for
1423b005d3pjd * kernel event buffer allocation by userland and kernel versions of
1433b005d3pjd * sysevent_evc_publish().
1443b005d3pjd *
1453b005d3pjd * EVCH_SLEEP and EVCH_NOSLEEP respectively map to KM_SLEEP and KM_NOSLEEP.
1463b005d3pjd * EVCH_TRYHARD is a kernel-only publish flag that allow event allocation
1473b005d3pjd * routines to use use alternate kmem caches in situations where free memory
1483b005d3pjd * may be low.  Kernel callers of sysevent_evc_publish() must set flags to
1493b005d3pjd * one of EVCH_SLEEP, EVCH_NOSLEEP or EVCH_TRYHARD.  Userland callers of
1503b005d3pjd * sysevent_evc_publish() must set flags to one of EVCH_SLEEP or EVCH_NOSLEEP.
1513b005d3pjd *
1523b005d3pjd * EVCH_QWAIT determines whether or not we should wait for slots in the event
1533b005d3pjd * queue at publication time.  EVCH_QWAIT may be used by kernel and userland
1543b005d3pjd * publishers and must be used in conjunction with any of one of EVCH_SLEEP,
1553b005d3pjd * EVCH_NOSLEEP or EVCH_TRYHARD (kernel-only).
1563b005d3pjd */
1573b005d3pjd
1583b005d3pjd#define	EVCH_NOSLEEP	0x0001	/* No sleep on kmem_alloc() */
1593b005d3pjd#define	EVCH_SLEEP	0x0002	/* Sleep on kmem_alloc() */
1603b005d3pjd#define	EVCH_TRYHARD	0x0004	/* May use alternate kmem cache for alloc */
1613b005d3pjd#define	EVCH_QWAIT	0x0008	/* Wait for slot in event queue */
1623b005d3pjd
1633b005d3pjd/*
1641b03c5bpjd * Meaning of flags for subscribe. Bits 8 to 15 are dedicated to
1651b03c5bpjd * the consolidation private interface, so flags defined here are restricted
1661b03c5bpjd * to the LSB.
1671b03c5bpjd *
1681b03c5bpjd * EVCH_SUB_KEEP indicates that this subscription should persist even if
1691b03c5bpjd * this subscriber id should die unexpectedly; matching events will be
1701b03c5bpjd * queued (up to a limit) and will be delivered if/when we restart again
1711b03c5bpjd * with the same subscriber id.
1721b03c5bpjd */
1731b03c5bpjd#define	EVCH_SUB_KEEP		0x01
1741b03c5bpjd
1751b03c5bpjd/*
1761b03c5bpjd * Subscriptions may be wildcarded, but we limit the number of
1771b03c5bpjd * wildcards permitted.
1781b03c5bpjd */
1791b03c5bpjd#define	EVCH_WILDCARD_MAX	10
1801b03c5bpjd
1811b03c5bpjd/*
1821b03c5bpjd * Used in unsubscribe to indicate all subscriber ids for a channel.
1833b005d3pjd */
1843b005d3pjd#define	EVCH_ALLSUB		"all_subs"
1853b005d3pjd
1863b005d3pjd/*
1873b005d3pjd * Meaning of flags parameter of channel bind function
1881b03c5bpjd *
1891b03c5bpjd * EVCH_CREAT indicates to create a channel if not already present.
1901b03c5bpjd *
1911b03c5bpjd * EVCH_HOLD_PEND indicates that events should be published to this
1921b03c5bpjd * channel even if there are no matching subscribers present; when
1931b03c5bpjd * a subscriber belatedly binds to the channel and registers their
1941b03c5bpjd * subscriptions they will receive events that predate their bind.
1951b03c5bpjd * If the channel is closed, however, with no remaining bindings then
1961b03c5bpjd * the channel is destroyed.
1971b03c5bpjd *
1981b03c5bpjd * EVCH_HOLD_PEND_INDEF is a stronger version of EVCH_HOLD_PEND -
1991b03c5bpjd * even if the channel has no remaining bindings it will not be
2001b03c5bpjd * destroyed so long as events remain unconsumed.  This is suitable for
2011b03c5bpjd * use with short-lived event producers that may bind to (create) the
2021b03c5bpjd * channel and exit before the intended consumer has started.
2033b005d3pjd */
2041b03c5bpjd#define	EVCH_CREAT		0x0001
2053b005d3pjd#define	EVCH_HOLD_PEND		0x0002
2061b03c5bpjd#define	EVCH_HOLD_PEND_INDEF	0x0004
2071b03c5bpjd#define	EVCH_B_FLAGS		0x0007	/* All valid bits */
2083b005d3pjd
2093b005d3pjd/*
2103b005d3pjd * Meaning of commands of evc_control function
2113b005d3pjd */
2123b005d3pjd#define	EVCH_GET_CHAN_LEN_MAX	 1	/* Get event queue length limit */
2133b005d3pjd#define	EVCH_GET_CHAN_LEN	 2	/* Get event queue length */
2143b005d3pjd#define	EVCH_SET_CHAN_LEN	 3	/* Set event queue length */
2153b005d3pjd#define	EVCH_CMD_LAST		 EVCH_SET_CHAN_LEN	/* Last command */
2163b005d3pjd
21755c26d8smh#ifdef illumos
2183b005d3pjd/*
2191b03c5bpjd * Shared user/kernel event channel interface definitions
2203b005d3pjd */
2211b03c5bpjdextern int sysevent_evc_bind(const char *, evchan_t **, uint32_t);
2221b03c5bpjdextern int sysevent_evc_unbind(evchan_t *);
2231b03c5bpjdextern int sysevent_evc_subscribe(evchan_t *, const char *, const char *,
2243b005d3pjd    int (*)(sysevent_t *, void *), void *, uint32_t);
2251b03c5bpjdextern int sysevent_evc_unsubscribe(evchan_t *, const char *);
2261b03c5bpjdextern int sysevent_evc_publish(evchan_t *, const char *, const char *,
2273b005d3pjd    const char *, const char *, nvlist_t *, uint32_t);
2281b03c5bpjdextern int sysevent_evc_control(evchan_t *, int, ...);
2291b03c5bpjdextern int sysevent_evc_setpropnvl(evchan_t *, nvlist_t *);
2301b03c5bpjdextern int sysevent_evc_getpropnvl(evchan_t *, nvlist_t **);
23155c26d8smh#endif	/* illumos */
2323b005d3pjd
2331b03c5bpjd#ifndef	_KERNEL
2341b03c5bpjd
23555c26d8smh#ifdef illumos
2361b03c5bpjd/*
2371b03c5bpjd * Userland-only event channel interfaces
2381b03c5bpjd */
2391b03c5bpjd
2401b03c5bpjd#include <door.h>
2411b03c5bpjd
2421b03c5bpjdtypedef struct sysevent_subattr sysevent_subattr_t;
2431b03c5bpjd
2441b03c5bpjdextern sysevent_subattr_t *sysevent_subattr_alloc(void);
2451b03c5bpjdextern void sysevent_subattr_free(sysevent_subattr_t *);
2461b03c5bpjd
2471b03c5bpjdextern void sysevent_subattr_thrattr(sysevent_subattr_t *, pthread_attr_t *);
2481b03c5bpjdextern void sysevent_subattr_sigmask(sysevent_subattr_t *, sigset_t *);
2491b03c5bpjd
2501b03c5bpjdextern void sysevent_subattr_thrcreate(sysevent_subattr_t *,
2511b03c5bpjd    door_xcreate_server_func_t *, void *);
2521b03c5bpjdextern void sysevent_subattr_thrsetup(sysevent_subattr_t *,
2531b03c5bpjd    door_xcreate_thrsetup_func_t *, void *);
2541b03c5bpjd
2551b03c5bpjdextern int sysevent_evc_xsubscribe(evchan_t *, const char *, const char *,
2561b03c5bpjd    int (*)(sysevent_t *, void *), void *, uint32_t, sysevent_subattr_t *);
25755c26d8smh#endif	/* illumos */
2581b03c5bpjd
2591b03c5bpjd#else
2603b005d3pjd
2613b005d3pjd/*
2623b005d3pjd * Kernel log_event interfaces.
2633b005d3pjd */
2641b03c5bpjdextern int log_sysevent(sysevent_t *, int, sysevent_id_t *);
2651b03c5bpjd
2661b03c5bpjdextern sysevent_t *sysevent_alloc(char *, char *, char *, int);
2671b03c5bpjdextern void sysevent_free(sysevent_t *);
2681b03c5bpjdextern int sysevent_add_attr(sysevent_attr_list_t **, char *,
2691b03c5bpjd    sysevent_value_t *, int);
2701b03c5bpjdextern void sysevent_free_attr(sysevent_attr_list_t *);
2711b03c5bpjdextern int sysevent_attach_attributes(sysevent_t *, sysevent_attr_list_t *);
2721b03c5bpjdextern void sysevent_detach_attributes(sysevent_t *);
27355c26d8smh#ifdef illumos
2741b03c5bpjdextern char *sysevent_get_class_name(sysevent_t *);
2751b03c5bpjdextern char *sysevent_get_subclass_name(sysevent_t *);
2761b03c5bpjdextern uint64_t sysevent_get_seq(sysevent_t *);
2771b03c5bpjdextern void sysevent_get_time(sysevent_t *, hrtime_t *);
2781b03c5bpjdextern size_t sysevent_get_size(sysevent_t *);
2791b03c5bpjdextern char *sysevent_get_pub(sysevent_t *);
2801b03c5bpjdextern int sysevent_get_attr_list(sysevent_t *, nvlist_t **);
28155c26d8smh#endif	/* illumos */
2823b005d3pjd
2833b005d3pjd#endif	/* _KERNEL */
2843b005d3pjd
2853b005d3pjd#ifdef	__cplusplus
2863b005d3pjd}
2873b005d3pjd#endif
2883b005d3pjd
2893b005d3pjd#endif	/* _SYS_SYSEVENT_H */
290