17c478bdstevel@tonic-gate/*
27c478bdstevel@tonic-gate * CDDL HEADER START
37c478bdstevel@tonic-gate *
47c478bdstevel@tonic-gate * The contents of this file are subject to the terms of the
549b225eGavin Maltby * Common Development and Distribution License (the "License").
649b225eGavin Maltby * You may not use this file except in compliance with the License.
77c478bdstevel@tonic-gate *
87c478bdstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bdstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bdstevel@tonic-gate * See the License for the specific language governing permissions
117c478bdstevel@tonic-gate * and limitations under the License.
127c478bdstevel@tonic-gate *
137c478bdstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bdstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bdstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bdstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bdstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bdstevel@tonic-gate *
197c478bdstevel@tonic-gate * CDDL HEADER END
207c478bdstevel@tonic-gate */
2149b225eGavin Maltby
227c478bdstevel@tonic-gate/*
23f6e214cGavin Maltby * Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
24a25df66Robert Mustacchi * Copyright 2015 Joyent, Inc.
257c478bdstevel@tonic-gate */
267c478bdstevel@tonic-gate
277c478bdstevel@tonic-gate#ifndef	_SYS_SYSEVENT_H
287c478bdstevel@tonic-gate#define	_SYS_SYSEVENT_H
297c478bdstevel@tonic-gate
307c478bdstevel@tonic-gate#include <sys/nvpair.h>
314870e0aRichard PALO#include <sys/null.h>
327c478bdstevel@tonic-gate
337c478bdstevel@tonic-gate#ifdef	__cplusplus
347c478bdstevel@tonic-gateextern "C" {
357c478bdstevel@tonic-gate#endif
367c478bdstevel@tonic-gate
377c478bdstevel@tonic-gate/* Internal registration class and subclass */
387c478bdstevel@tonic-gate#define	EC_ALL		"register_all_classes"
397c478bdstevel@tonic-gate#define	EC_SUB_ALL	"register_all_subclasses"
407c478bdstevel@tonic-gate
417c478bdstevel@tonic-gate/*
427c478bdstevel@tonic-gate * Event allocation/enqueuing sleep/nosleep flags
437c478bdstevel@tonic-gate */
447c478bdstevel@tonic-gate#define	SE_SLEEP		0
457c478bdstevel@tonic-gate#define	SE_NOSLEEP		1
467c478bdstevel@tonic-gate
477c478bdstevel@tonic-gate/* Framework error codes */
487c478bdstevel@tonic-gate#define	SE_EINVAL		1	/* Invalid argument */
497c478bdstevel@tonic-gate#define	SE_ENOMEM		2	/* Unable to allocate memory */
507c478bdstevel@tonic-gate#define	SE_EQSIZE		3	/* Maximum event q size exceeded */
517c478bdstevel@tonic-gate#define	SE_EFAULT		4	/* Copy fault */
527c478bdstevel@tonic-gate#define	SE_NOTFOUND		5	/* Attribute not found */
537c478bdstevel@tonic-gate#define	SE_NO_TRANSPORT		6	/* sysevent transport down */
547c478bdstevel@tonic-gate
557c478bdstevel@tonic-gate/* Internal data types */
567c478bdstevel@tonic-gate
577c478bdstevel@tonic-gate#define	SE_DATA_TYPE_BYTE	DATA_TYPE_BYTE
587c478bdstevel@tonic-gate#define	SE_DATA_TYPE_INT16	DATA_TYPE_INT16
597c478bdstevel@tonic-gate#define	SE_DATA_TYPE_UINT16	DATA_TYPE_UINT16
607c478bdstevel@tonic-gate#define	SE_DATA_TYPE_INT32	DATA_TYPE_INT32
617c478bdstevel@tonic-gate#define	SE_DATA_TYPE_UINT32	DATA_TYPE_UINT32
627c478bdstevel@tonic-gate#define	SE_DATA_TYPE_INT64	DATA_TYPE_INT64
637c478bdstevel@tonic-gate#define	SE_DATA_TYPE_UINT64	DATA_TYPE_UINT64
647c478bdstevel@tonic-gate#define	SE_DATA_TYPE_STRING	DATA_TYPE_STRING
657c478bdstevel@tonic-gate#define	SE_DATA_TYPE_BYTES	DATA_TYPE_BYTE_ARRAY
667c478bdstevel@tonic-gate#define	SE_DATA_TYPE_TIME	DATA_TYPE_HRTIME
677c478bdstevel@tonic-gate
687c478bdstevel@tonic-gate#define	SE_KERN_PID	0
697c478bdstevel@tonic-gate
707c478bdstevel@tonic-gate#define	SUNW_VENDOR	"SUNW"
71a25df66Robert Mustacchi#define	ILLUMOS_VENDOR	"ILLUMOS"
727c478bdstevel@tonic-gate#define	SE_USR_PUB	"usr:"
737c478bdstevel@tonic-gate#define	SE_KERN_PUB	"kern:"
7499d2926Toomas Soome#define	SUNW_KERN_PUB	SUNW_VENDOR ":" SE_KERN_PUB
7599d2926Toomas Soome#define	SUNW_USR_PUB	SUNW_VENDOR ":" SE_USR_PUB
76a25df66Robert Mustacchi#define	ILLUMOS_KERN_PUB	ILLUMOS_VENDOR":"SE_KERN_PUB
777c478bdstevel@tonic-gate
787c478bdstevel@tonic-gate/*
797c478bdstevel@tonic-gate * Event header and attribute value limits
807c478bdstevel@tonic-gate */
817c478bdstevel@tonic-gate#define	MAX_ATTR_NAME	1024
827c478bdstevel@tonic-gate#define	MAX_STRING_SZ	1024
837c478bdstevel@tonic-gate#define	MAX_BYTE_ARRAY	1024
847c478bdstevel@tonic-gate
857c478bdstevel@tonic-gate#define	MAX_CLASS_LEN		64
867c478bdstevel@tonic-gate#define	MAX_SUBCLASS_LEN	64
877c478bdstevel@tonic-gate#define	MAX_PUB_LEN		128
887c478bdstevel@tonic-gate#define	MAX_CHNAME_LEN		128
897c478bdstevel@tonic-gate#define	MAX_SUBID_LEN		16
907c478bdstevel@tonic-gate
917c478bdstevel@tonic-gate/*
927c478bdstevel@tonic-gate * Limit for the event payload size
937c478bdstevel@tonic-gate */
947c478bdstevel@tonic-gate#define	MAX_EV_SIZE_LEN		(SHRT_MAX/4)
957c478bdstevel@tonic-gate
967c478bdstevel@tonic-gate/* Opaque sysevent_t data type */
977c478bdstevel@tonic-gatetypedef void *sysevent_t;
987c478bdstevel@tonic-gate
997c478bdstevel@tonic-gate/* Opaque channel bind data type */
1007c478bdstevel@tonic-gatetypedef void evchan_t;
1017c478bdstevel@tonic-gate
1027c478bdstevel@tonic-gate/* sysevent attribute list */
1037c478bdstevel@tonic-gatetypedef nvlist_t sysevent_attr_list_t;
1047c478bdstevel@tonic-gate
1057c478bdstevel@tonic-gate/* sysevent attribute name-value pair */
1067c478bdstevel@tonic-gatetypedef nvpair_t sysevent_attr_t;
1077c478bdstevel@tonic-gate
1087c478bdstevel@tonic-gate/* Unique event identifier */
1097c478bdstevel@tonic-gatetypedef struct sysevent_id {
1107c478bdstevel@tonic-gate	uint64_t eid_seq;
1117c478bdstevel@tonic-gate	hrtime_t eid_ts;
1127c478bdstevel@tonic-gate} sysevent_id_t;
1137c478bdstevel@tonic-gate
1147c478bdstevel@tonic-gate/* Event attribute value structures */
1157c478bdstevel@tonic-gatetypedef struct sysevent_bytes {
1167c478bdstevel@tonic-gate	int32_t	size;
1177c478bdstevel@tonic-gate	uchar_t	*data;
1187c478bdstevel@tonic-gate} sysevent_bytes_t;
1197c478bdstevel@tonic-gate
1207c478bdstevel@tonic-gatetypedef struct sysevent_value {
1217c478bdstevel@tonic-gate	int32_t		value_type;		/* data type */
1227c478bdstevel@tonic-gate	union {
1237c478bdstevel@tonic-gate		uchar_t		sv_byte;
1247c478bdstevel@tonic-gate		int16_t		sv_int16;
1257c478bdstevel@tonic-gate		uint16_t	sv_uint16;
1267c478bdstevel@tonic-gate		int32_t		sv_int32;
1277c478bdstevel@tonic-gate		uint32_t	sv_uint32;
1287c478bdstevel@tonic-gate		int64_t		sv_int64;
1297c478bdstevel@tonic-gate		uint64_t	sv_uint64;
1307c478bdstevel@tonic-gate		hrtime_t	sv_time;
1317c478bdstevel@tonic-gate		char		*sv_string;
1327c478bdstevel@tonic-gate		sysevent_bytes_t	sv_bytes;
1337c478bdstevel@tonic-gate	} value;
1347c478bdstevel@tonic-gate} sysevent_value_t;
1357c478bdstevel@tonic-gate
1367c478bdstevel@tonic-gate/*
1377c478bdstevel@tonic-gate * The following flags determine the memory allocation semantics to use for
1387c478bdstevel@tonic-gate * kernel event buffer allocation by userland and kernel versions of
1397c478bdstevel@tonic-gate * sysevent_evc_publish().
1407c478bdstevel@tonic-gate *
1417c478bdstevel@tonic-gate * EVCH_SLEEP and EVCH_NOSLEEP respectively map to KM_SLEEP and KM_NOSLEEP.
1427c478bdstevel@tonic-gate * EVCH_TRYHARD is a kernel-only publish flag that allow event allocation
1437c478bdstevel@tonic-gate * routines to use use alternate kmem caches in situations where free memory
1447c478bdstevel@tonic-gate * may be low.  Kernel callers of sysevent_evc_publish() must set flags to
1457c478bdstevel@tonic-gate * one of EVCH_SLEEP, EVCH_NOSLEEP or EVCH_TRYHARD.  Userland callers of
1467c478bdstevel@tonic-gate * sysevent_evc_publish() must set flags to one of EVCH_SLEEP or EVCH_NOSLEEP.
1477c478bdstevel@tonic-gate *
1487c478bdstevel@tonic-gate * EVCH_QWAIT determines whether or not we should wait for slots in the event
1497c478bdstevel@tonic-gate * queue at publication time.  EVCH_QWAIT may be used by kernel and userland
1507c478bdstevel@tonic-gate * publishers and must be used in conjunction with any of one of EVCH_SLEEP,
1517c478bdstevel@tonic-gate * EVCH_NOSLEEP or EVCH_TRYHARD (kernel-only).
1527c478bdstevel@tonic-gate */
1537c478bdstevel@tonic-gate
1547c478bdstevel@tonic-gate#define	EVCH_NOSLEEP	0x0001	/* No sleep on kmem_alloc() */
1557c478bdstevel@tonic-gate#define	EVCH_SLEEP	0x0002	/* Sleep on kmem_alloc() */
1567c478bdstevel@tonic-gate#define	EVCH_TRYHARD	0x0004	/* May use alternate kmem cache for alloc */
1577c478bdstevel@tonic-gate#define	EVCH_QWAIT	0x0008	/* Wait for slot in event queue */
1587c478bdstevel@tonic-gate
1597c478bdstevel@tonic-gate/*
16049b225eGavin Maltby * Meaning of flags for subscribe. Bits 8 to 15 are dedicated to
16149b225eGavin Maltby * the consolidation private interface, so flags defined here are restricted
16249b225eGavin Maltby * to the LSB.
16349b225eGavin Maltby *
16449b225eGavin Maltby * EVCH_SUB_KEEP indicates that this subscription should persist even if
16549b225eGavin Maltby * this subscriber id should die unexpectedly; matching events will be
16649b225eGavin Maltby * queued (up to a limit) and will be delivered if/when we restart again
16749b225eGavin Maltby * with the same subscriber id.
16849b225eGavin Maltby */
16949b225eGavin Maltby#define	EVCH_SUB_KEEP		0x01
17049b225eGavin Maltby
17149b225eGavin Maltby/*
17249b225eGavin Maltby * Subscriptions may be wildcarded, but we limit the number of
17349b225eGavin Maltby * wildcards permitted.
17449b225eGavin Maltby */
17549b225eGavin Maltby#define	EVCH_WILDCARD_MAX	10
17649b225eGavin Maltby
17749b225eGavin Maltby/*
17849b225eGavin Maltby * Used in unsubscribe to indicate all subscriber ids for a channel.
1797c478bdstevel@tonic-gate */
1807c478bdstevel@tonic-gate#define	EVCH_ALLSUB		"all_subs"
1817c478bdstevel@tonic-gate
1827c478bdstevel@tonic-gate/*
1837c478bdstevel@tonic-gate * Meaning of flags parameter of channel bind function
18449b225eGavin Maltby *
18549b225eGavin Maltby * EVCH_CREAT indicates to create a channel if not already present.
18649b225eGavin Maltby *
18749b225eGavin Maltby * EVCH_HOLD_PEND indicates that events should be published to this
18849b225eGavin Maltby * channel even if there are no matching subscribers present; when
18949b225eGavin Maltby * a subscriber belatedly binds to the channel and registers their
19049b225eGavin Maltby * subscriptions they will receive events that predate their bind.
19149b225eGavin Maltby * If the channel is closed, however, with no remaining bindings then
19249b225eGavin Maltby * the channel is destroyed.
19349b225eGavin Maltby *
19449b225eGavin Maltby * EVCH_HOLD_PEND_INDEF is a stronger version of EVCH_HOLD_PEND -
19549b225eGavin Maltby * even if the channel has no remaining bindings it will not be
19649b225eGavin Maltby * destroyed so long as events remain unconsumed.  This is suitable for
19749b225eGavin Maltby * use with short-lived event producers that may bind to (create) the
19849b225eGavin Maltby * channel and exit before the intended consumer has started.
1997c478bdstevel@tonic-gate */
20049b225eGavin Maltby#define	EVCH_CREAT		0x0001
2017c478bdstevel@tonic-gate#define	EVCH_HOLD_PEND		0x0002
20249b225eGavin Maltby#define	EVCH_HOLD_PEND_INDEF	0x0004
20349b225eGavin Maltby#define	EVCH_B_FLAGS		0x0007	/* All valid bits */
2047c478bdstevel@tonic-gate
2057c478bdstevel@tonic-gate/*
2067c478bdstevel@tonic-gate * Meaning of commands of evc_control function
2077c478bdstevel@tonic-gate */
2087c478bdstevel@tonic-gate#define	EVCH_GET_CHAN_LEN_MAX	 1	/* Get event queue length limit */
2097c478bdstevel@tonic-gate#define	EVCH_GET_CHAN_LEN	 2	/* Get event queue length */
2107c478bdstevel@tonic-gate#define	EVCH_SET_CHAN_LEN	 3	/* Set event queue length */
2117c478bdstevel@tonic-gate#define	EVCH_CMD_LAST		 EVCH_SET_CHAN_LEN	/* Last command */
2127c478bdstevel@tonic-gate
2137c478bdstevel@tonic-gate/*
21449b225eGavin Maltby * Shared user/kernel event channel interface definitions
2157c478bdstevel@tonic-gate */
21649b225eGavin Maltbyextern int sysevent_evc_bind(const char *, evchan_t **, uint32_t);
21749b225eGavin Maltbyextern int sysevent_evc_unbind(evchan_t *);
21849b225eGavin Maltbyextern int sysevent_evc_subscribe(evchan_t *, const char *, const char *,
2197c478bdstevel@tonic-gate    int (*)(sysevent_t *, void *), void *, uint32_t);
22049b225eGavin Maltbyextern int sysevent_evc_unsubscribe(evchan_t *, const char *);
22149b225eGavin Maltbyextern int sysevent_evc_publish(evchan_t *, const char *, const char *,
2227c478bdstevel@tonic-gate    const char *, const char *, nvlist_t *, uint32_t);
22349b225eGavin Maltbyextern int sysevent_evc_control(evchan_t *, int, ...);
224f6e214cGavin Maltbyextern int sysevent_evc_setpropnvl(evchan_t *, nvlist_t *);
225f6e214cGavin Maltbyextern int sysevent_evc_getpropnvl(evchan_t *, nvlist_t **);
2267c478bdstevel@tonic-gate
22749b225eGavin Maltby#ifndef	_KERNEL
22849b225eGavin Maltby
22949b225eGavin Maltby/*
23049b225eGavin Maltby * Userland-only event channel interfaces
23149b225eGavin Maltby */
23249b225eGavin Maltby
23349b225eGavin Maltby#include <door.h>
23449b225eGavin Maltby
23549b225eGavin Maltbytypedef struct sysevent_subattr sysevent_subattr_t;
23649b225eGavin Maltby
23749b225eGavin Maltbyextern sysevent_subattr_t *sysevent_subattr_alloc(void);
23849b225eGavin Maltbyextern void sysevent_subattr_free(sysevent_subattr_t *);
23949b225eGavin Maltby
24049b225eGavin Maltbyextern void sysevent_subattr_thrattr(sysevent_subattr_t *, pthread_attr_t *);
24149b225eGavin Maltbyextern void sysevent_subattr_sigmask(sysevent_subattr_t *, sigset_t *);
24249b225eGavin Maltby
24349b225eGavin Maltbyextern void sysevent_subattr_thrcreate(sysevent_subattr_t *,
24449b225eGavin Maltby    door_xcreate_server_func_t *, void *);
24549b225eGavin Maltbyextern void sysevent_subattr_thrsetup(sysevent_subattr_t *,
24649b225eGavin Maltby    door_xcreate_thrsetup_func_t *, void *);
24749b225eGavin Maltby
24849b225eGavin Maltbyextern int sysevent_evc_xsubscribe(evchan_t *, const char *, const char *,
24949b225eGavin Maltby    int (*)(sysevent_t *, void *), void *, uint32_t, sysevent_subattr_t *);
25049b225eGavin Maltby
25149b225eGavin Maltby#else
2527c478bdstevel@tonic-gate
2537c478bdstevel@tonic-gate/*
2547c478bdstevel@tonic-gate * Kernel log_event interfaces.
2557c478bdstevel@tonic-gate */
25649b225eGavin Maltbyextern int log_sysevent(sysevent_t *, int, sysevent_id_t *);
25749b225eGavin Maltby
25849b225eGavin Maltbyextern sysevent_t *sysevent_alloc(char *, char *, char *, int);
25949b225eGavin Maltbyextern void sysevent_free(sysevent_t *);
26049b225eGavin Maltbyextern int sysevent_add_attr(sysevent_attr_list_t **, char *,
26149b225eGavin Maltby    sysevent_value_t *, int);
26249b225eGavin Maltbyextern void sysevent_free_attr(sysevent_attr_list_t *);
26349b225eGavin Maltbyextern int sysevent_attach_attributes(sysevent_t *, sysevent_attr_list_t *);
26449b225eGavin Maltbyextern void sysevent_detach_attributes(sysevent_t *);
26549b225eGavin Maltbyextern char *sysevent_get_class_name(sysevent_t *);
26649b225eGavin Maltbyextern char *sysevent_get_subclass_name(sysevent_t *);
26749b225eGavin Maltbyextern uint64_t sysevent_get_seq(sysevent_t *);
26849b225eGavin Maltbyextern void sysevent_get_time(sysevent_t *, hrtime_t *);
26949b225eGavin Maltbyextern size_t sysevent_get_size(sysevent_t *);
27049b225eGavin Maltbyextern char *sysevent_get_pub(sysevent_t *);
27149b225eGavin Maltbyextern int sysevent_get_attr_list(sysevent_t *, nvlist_t **);
2727c478bdstevel@tonic-gate
2737c478bdstevel@tonic-gate#endif	/* _KERNEL */
2747c478bdstevel@tonic-gate
2757c478bdstevel@tonic-gate#ifdef	__cplusplus
2767c478bdstevel@tonic-gate}
2777c478bdstevel@tonic-gate#endif
2787c478bdstevel@tonic-gate
2797c478bdstevel@tonic-gate#endif	/* _SYS_SYSEVENT_H */
280