xref: /illumos-gate/usr/src/uts/common/sys/sysevent.h (revision fdf1a871)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Copyright 2015 Joyent, Inc.
25  */
26 
27 #ifndef	_SYS_SYSEVENT_H
28 #define	_SYS_SYSEVENT_H
29 
30 #include <sys/nvpair.h>
31 #include <sys/null.h>
32 
33 #ifdef	__cplusplus
34 extern "C" {
35 #endif
36 
37 /* Internal registration class and subclass */
38 #define	EC_ALL		"register_all_classes"
39 #define	EC_SUB_ALL	"register_all_subclasses"
40 
41 /*
42  * Event allocation/enqueuing sleep/nosleep flags
43  */
44 #define	SE_SLEEP		0
45 #define	SE_NOSLEEP		1
46 
47 /* Framework error codes */
48 #define	SE_EINVAL		1	/* Invalid argument */
49 #define	SE_ENOMEM		2	/* Unable to allocate memory */
50 #define	SE_EQSIZE		3	/* Maximum event q size exceeded */
51 #define	SE_EFAULT		4	/* Copy fault */
52 #define	SE_NOTFOUND		5	/* Attribute not found */
53 #define	SE_NO_TRANSPORT		6	/* sysevent transport down */
54 
55 /* Internal data types */
56 
57 #define	SE_DATA_TYPE_BYTE	DATA_TYPE_BYTE
58 #define	SE_DATA_TYPE_INT16	DATA_TYPE_INT16
59 #define	SE_DATA_TYPE_UINT16	DATA_TYPE_UINT16
60 #define	SE_DATA_TYPE_INT32	DATA_TYPE_INT32
61 #define	SE_DATA_TYPE_UINT32	DATA_TYPE_UINT32
62 #define	SE_DATA_TYPE_INT64	DATA_TYPE_INT64
63 #define	SE_DATA_TYPE_UINT64	DATA_TYPE_UINT64
64 #define	SE_DATA_TYPE_STRING	DATA_TYPE_STRING
65 #define	SE_DATA_TYPE_BYTES	DATA_TYPE_BYTE_ARRAY
66 #define	SE_DATA_TYPE_TIME	DATA_TYPE_HRTIME
67 
68 #define	SE_KERN_PID	0
69 
70 #define	SUNW_VENDOR	"SUNW"
71 #define	ILLUMOS_VENDOR	"ILLUMOS"
72 #define	SE_USR_PUB	"usr:"
73 #define	SE_KERN_PUB	"kern:"
74 #define	SUNW_KERN_PUB	SUNW_VENDOR ":" SE_KERN_PUB
75 #define	SUNW_USR_PUB	SUNW_VENDOR ":" SE_USR_PUB
76 #define	ILLUMOS_KERN_PUB	ILLUMOS_VENDOR ":" SE_KERN_PUB
77 
78 /*
79  * Event header and attribute value limits
80  */
81 #define	MAX_ATTR_NAME	1024
82 #define	MAX_STRING_SZ	1024
83 #define	MAX_BYTE_ARRAY	1024
84 
85 #define	MAX_CLASS_LEN		64
86 #define	MAX_SUBCLASS_LEN	64
87 #define	MAX_PUB_LEN		128
88 #define	MAX_CHNAME_LEN		128
89 #define	MAX_SUBID_LEN		16
90 
91 /*
92  * Limit for the event payload size
93  */
94 #define	MAX_EV_SIZE_LEN		(SHRT_MAX/4)
95 
96 /* Opaque sysevent_t data type */
97 typedef void *sysevent_t;
98 
99 /* Opaque channel bind data type */
100 typedef void evchan_t;
101 
102 /* sysevent attribute list */
103 typedef nvlist_t sysevent_attr_list_t;
104 
105 /* sysevent attribute name-value pair */
106 typedef nvpair_t sysevent_attr_t;
107 
108 /* Unique event identifier */
109 typedef struct sysevent_id {
110 	uint64_t eid_seq;
111 	hrtime_t eid_ts;
112 } sysevent_id_t;
113 
114 /* Event attribute value structures */
115 typedef struct sysevent_bytes {
116 	int32_t	size;
117 	uchar_t	*data;
118 } sysevent_bytes_t;
119 
120 typedef struct sysevent_value {
121 	int32_t		value_type;		/* data type */
122 	union {
123 		uchar_t		sv_byte;
124 		int16_t		sv_int16;
125 		uint16_t	sv_uint16;
126 		int32_t		sv_int32;
127 		uint32_t	sv_uint32;
128 		int64_t		sv_int64;
129 		uint64_t	sv_uint64;
130 		hrtime_t	sv_time;
131 		char		*sv_string;
132 		sysevent_bytes_t	sv_bytes;
133 	} value;
134 } sysevent_value_t;
135 
136 /*
137  * The following flags determine the memory allocation semantics to use for
138  * kernel event buffer allocation by userland and kernel versions of
139  * sysevent_evc_publish().
140  *
141  * EVCH_SLEEP and EVCH_NOSLEEP respectively map to KM_SLEEP and KM_NOSLEEP.
142  * EVCH_TRYHARD is a kernel-only publish flag that allow event allocation
143  * routines to use use alternate kmem caches in situations where free memory
144  * may be low.  Kernel callers of sysevent_evc_publish() must set flags to
145  * one of EVCH_SLEEP, EVCH_NOSLEEP or EVCH_TRYHARD.  Userland callers of
146  * sysevent_evc_publish() must set flags to one of EVCH_SLEEP or EVCH_NOSLEEP.
147  *
148  * EVCH_QWAIT determines whether or not we should wait for slots in the event
149  * queue at publication time.  EVCH_QWAIT may be used by kernel and userland
150  * publishers and must be used in conjunction with any of one of EVCH_SLEEP,
151  * EVCH_NOSLEEP or EVCH_TRYHARD (kernel-only).
152  */
153 
154 #define	EVCH_NOSLEEP	0x0001	/* No sleep on kmem_alloc() */
155 #define	EVCH_SLEEP	0x0002	/* Sleep on kmem_alloc() */
156 #define	EVCH_TRYHARD	0x0004	/* May use alternate kmem cache for alloc */
157 #define	EVCH_QWAIT	0x0008	/* Wait for slot in event queue */
158 
159 /*
160  * Meaning of flags for subscribe. Bits 8 to 15 are dedicated to
161  * the consolidation private interface, so flags defined here are restricted
162  * to the LSB.
163  *
164  * EVCH_SUB_KEEP indicates that this subscription should persist even if
165  * this subscriber id should die unexpectedly; matching events will be
166  * queued (up to a limit) and will be delivered if/when we restart again
167  * with the same subscriber id.
168  */
169 #define	EVCH_SUB_KEEP		0x01
170 
171 /*
172  * Subscriptions may be wildcarded, but we limit the number of
173  * wildcards permitted.
174  */
175 #define	EVCH_WILDCARD_MAX	10
176 
177 /*
178  * Used in unsubscribe to indicate all subscriber ids for a channel.
179  */
180 #define	EVCH_ALLSUB		"all_subs"
181 
182 /*
183  * Meaning of flags parameter of channel bind function
184  *
185  * EVCH_CREAT indicates to create a channel if not already present.
186  *
187  * EVCH_HOLD_PEND indicates that events should be published to this
188  * channel even if there are no matching subscribers present; when
189  * a subscriber belatedly binds to the channel and registers their
190  * subscriptions they will receive events that predate their bind.
191  * If the channel is closed, however, with no remaining bindings then
192  * the channel is destroyed.
193  *
194  * EVCH_HOLD_PEND_INDEF is a stronger version of EVCH_HOLD_PEND -
195  * even if the channel has no remaining bindings it will not be
196  * destroyed so long as events remain unconsumed.  This is suitable for
197  * use with short-lived event producers that may bind to (create) the
198  * channel and exit before the intended consumer has started.
199  */
200 #define	EVCH_CREAT		0x0001
201 #define	EVCH_HOLD_PEND		0x0002
202 #define	EVCH_HOLD_PEND_INDEF	0x0004
203 #define	EVCH_B_FLAGS		0x0007	/* All valid bits */
204 
205 /*
206  * Meaning of commands of evc_control function
207  */
208 #define	EVCH_GET_CHAN_LEN_MAX	 1	/* Get event queue length limit */
209 #define	EVCH_GET_CHAN_LEN	 2	/* Get event queue length */
210 #define	EVCH_SET_CHAN_LEN	 3	/* Set event queue length */
211 #define	EVCH_CMD_LAST		 EVCH_SET_CHAN_LEN	/* Last command */
212 
213 /*
214  * Shared user/kernel event channel interface definitions
215  */
216 extern int sysevent_evc_bind(const char *, evchan_t **, uint32_t);
217 extern int sysevent_evc_unbind(evchan_t *);
218 extern int sysevent_evc_subscribe(evchan_t *, const char *, const char *,
219     int (*)(sysevent_t *, void *), void *, uint32_t);
220 extern int sysevent_evc_unsubscribe(evchan_t *, const char *);
221 extern int sysevent_evc_publish(evchan_t *, const char *, const char *,
222     const char *, const char *, nvlist_t *, uint32_t);
223 extern int sysevent_evc_control(evchan_t *, int, ...);
224 extern int sysevent_evc_setpropnvl(evchan_t *, nvlist_t *);
225 extern int sysevent_evc_getpropnvl(evchan_t *, nvlist_t **);
226 
227 #ifndef	_KERNEL
228 
229 /*
230  * Userland-only event channel interfaces
231  */
232 
233 #include <door.h>
234 
235 typedef struct sysevent_subattr sysevent_subattr_t;
236 
237 extern sysevent_subattr_t *sysevent_subattr_alloc(void);
238 extern void sysevent_subattr_free(sysevent_subattr_t *);
239 
240 extern void sysevent_subattr_thrattr(sysevent_subattr_t *, pthread_attr_t *);
241 extern void sysevent_subattr_sigmask(sysevent_subattr_t *, sigset_t *);
242 
243 extern void sysevent_subattr_thrcreate(sysevent_subattr_t *,
244     door_xcreate_server_func_t *, void *);
245 extern void sysevent_subattr_thrsetup(sysevent_subattr_t *,
246     door_xcreate_thrsetup_func_t *, void *);
247 
248 extern int sysevent_evc_xsubscribe(evchan_t *, const char *, const char *,
249     int (*)(sysevent_t *, void *), void *, uint32_t, sysevent_subattr_t *);
250 
251 #else
252 
253 /*
254  * Kernel log_event interfaces.
255  */
256 extern int log_sysevent(sysevent_t *, int, sysevent_id_t *);
257 
258 extern sysevent_t *sysevent_alloc(char *, char *, char *, int);
259 extern void sysevent_free(sysevent_t *);
260 extern int sysevent_add_attr(sysevent_attr_list_t **, char *,
261     sysevent_value_t *, int);
262 extern void sysevent_free_attr(sysevent_attr_list_t *);
263 extern int sysevent_attach_attributes(sysevent_t *, sysevent_attr_list_t *);
264 extern void sysevent_detach_attributes(sysevent_t *);
265 extern char *sysevent_get_class_name(sysevent_t *);
266 extern char *sysevent_get_subclass_name(sysevent_t *);
267 extern uint64_t sysevent_get_seq(sysevent_t *);
268 extern void sysevent_get_time(sysevent_t *, hrtime_t *);
269 extern size_t sysevent_get_size(sysevent_t *);
270 extern char *sysevent_get_pub(sysevent_t *);
271 extern int sysevent_get_attr_list(sysevent_t *, nvlist_t **);
272 
273 #endif	/* _KERNEL */
274 
275 #ifdef	__cplusplus
276 }
277 #endif
278 
279 #endif	/* _SYS_SYSEVENT_H */
280