master
1/* $NetBSD: event.h,v 1.54 2022/07/19 00:46:00 thorpej Exp $ */
2
3/*-
4 * Copyright (c) 1999,2000,2001 Jonathan Lemon <jlemon@FreeBSD.org>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 *
28 * $FreeBSD: src/sys/sys/event.h,v 1.12 2001/02/24 01:44:03 jlemon Exp $
29 */
30
31#ifndef _SYS_EVENT_H_
32#define _SYS_EVENT_H_
33
34#include <sys/featuretest.h>
35#include <sys/types.h> /* for size_t */
36#include <sys/inttypes.h> /* for uintptr_t */
37#include <sys/null.h> /* for NULL */
38
39#define EVFILT_READ 0U
40#define EVFILT_WRITE 1U
41#define EVFILT_AIO 2U /* attached to aio requests */
42#define EVFILT_VNODE 3U /* attached to vnodes */
43#define EVFILT_PROC 4U /* attached to struct proc */
44#define EVFILT_SIGNAL 5U /* attached to struct proc */
45#define EVFILT_TIMER 6U /* arbitrary timer (in ms) */
46#define EVFILT_FS 7U /* filesystem events */
47#define EVFILT_USER 8U /* user events */
48#define EVFILT_EMPTY 9U
49#define EVFILT_SYSCOUNT 10U /* number of filters */
50
51#ifdef EVFILT_NAMES
52static const char *evfiltnames[] = {
53 "EVFILT_READ",
54 "EVFILT_WRITE",
55 "EVFILT_AIO",
56 "EVFILT_VNODE",
57 "EVFILT_PROC",
58 "EVFILT_SIGNAL",
59 "EVFILT_TIMER",
60 "EVFILT_FS",
61 "EVFILT_USER",
62 "EVFILT_EMPTY",
63};
64#endif
65
66struct kevent {
67 uintptr_t ident; /* identifier for this event */
68 uint32_t filter; /* filter for event */
69 uint32_t flags; /* action flags for kqueue */
70 uint32_t fflags; /* filter flag value */
71 int64_t data; /* filter data value */
72 void *udata; /* opaque user data identifier */
73};
74
75static __inline void
76_EV_SET(struct kevent *_kevp, uintptr_t _ident, uint32_t _filter,
77 uint32_t _flags, uint32_t _fflags, int64_t _data, void *_udata)
78{
79 _kevp->ident = _ident;
80 _kevp->filter = _filter;
81 _kevp->flags = _flags;
82 _kevp->fflags = _fflags;
83 _kevp->data = _data;
84 _kevp->udata = _udata;
85}
86
87#define EV_SET(kevp, ident, filter, flags, fflags, data, udata) \
88 _EV_SET((kevp), __CAST(uintptr_t, (ident)), (filter), (flags), \
89 (fflags), (data), __CAST(void *, (udata)))
90
91/* actions */
92#define EV_ADD 0x0001U /* add event to kq (implies ENABLE) */
93#define EV_DELETE 0x0002U /* delete event from kq */
94#define EV_ENABLE 0x0004U /* enable event */
95#define EV_DISABLE 0x0008U /* disable event (not reported) */
96
97/* flags */
98#define EV_ONESHOT 0x0010U /* only report one occurrence */
99#define EV_CLEAR 0x0020U /* clear event state after reporting */
100#define EV_RECEIPT 0x0040U /* force EV_ERROR on success, data=0 */
101#define EV_DISPATCH 0x0080U /* disable event after reporting */
102
103#define EV_SYSFLAGS 0xF000U /* reserved by system */
104#define EV_FLAG1 0x2000U /* filter-specific flag */
105
106/* returned values */
107#define EV_EOF 0x8000U /* EOF detected */
108#define EV_ERROR 0x4000U /* error, data contains errno */
109
110/*
111 * data/hint flags/masks for EVFILT_USER, shared with userspace
112 *
113 * On input, the top two bits of fflags specifies how the lower twenty four
114 * bits should be applied to the stored value of fflags.
115 *
116 * On output, the top two bits will always be set to NOTE_FFNOP and the
117 * remaining twenty four bits will contain the stored fflags value.
118 */
119#define NOTE_FFNOP 0x00000000U /* ignore input fflags */
120#define NOTE_FFAND 0x40000000U /* AND fflags */
121#define NOTE_FFOR 0x80000000U /* OR fflags */
122#define NOTE_FFCOPY 0xc0000000U /* copy fflags */
123
124#define NOTE_FFCTRLMASK 0xc0000000U /* masks for operations */
125#define NOTE_FFLAGSMASK 0x00ffffffU
126
127#define NOTE_TRIGGER 0x01000000U /* Cause the event to be
128 triggered for output. */
129/*
130 * hint flag for in-kernel use - must not equal any existing note
131 */
132#ifdef _KERNEL
133#define NOTE_SUBMIT 0x01000000U /* initial knote submission */
134#endif
135/*
136 * data/hint flags for EVFILT_{READ|WRITE}, shared with userspace
137 */
138#define NOTE_LOWAT 0x0001U /* low water mark */
139
140/*
141 * data/hint flags for EVFILT_VNODE, shared with userspace
142 */
143#define NOTE_DELETE 0x0001U /* vnode was removed */
144#define NOTE_WRITE 0x0002U /* data contents changed */
145#define NOTE_EXTEND 0x0004U /* size increased */
146#define NOTE_ATTRIB 0x0008U /* attributes changed */
147#define NOTE_LINK 0x0010U /* link count changed */
148#define NOTE_RENAME 0x0020U /* vnode was renamed */
149#define NOTE_REVOKE 0x0040U /* vnode access was revoked */
150#define NOTE_OPEN 0x0080U /* vnode was opened */
151#define NOTE_CLOSE 0x0100U /* file closed (no FWRITE) */
152#define NOTE_CLOSE_WRITE 0x0200U /* file closed (FWRITE) */
153#define NOTE_READ 0x0400U /* file was read */
154
155/*
156 * data/hint flags for EVFILT_PROC, shared with userspace
157 */
158#define NOTE_EXIT 0x80000000U /* process exited */
159#define NOTE_FORK 0x40000000U /* process forked */
160#define NOTE_EXEC 0x20000000U /* process exec'd */
161#define NOTE_PCTRLMASK 0xf0000000U /* mask for hint bits */
162#define NOTE_PDATAMASK 0x000fffffU /* mask for pid */
163
164/* additional flags for EVFILT_PROC */
165#define NOTE_TRACK 0x00000001U /* follow across forks */
166#define NOTE_TRACKERR 0x00000002U /* could not track child */
167#define NOTE_CHILD 0x00000004U /* am a child process */
168
169/* additional flags for EVFILT_TIMER */
170#define NOTE_MSECONDS 0x00000000U /* data is milliseconds */
171#define NOTE_SECONDS 0x00000001U /* data is seconds */
172#define NOTE_USECONDS 0x00000002U /* data is microseconds */
173#define NOTE_NSECONDS 0x00000003U /* data is nanoseconds */
174#define NOTE_ABSTIME 0x00000010U /* timeout is absolute */
175#ifdef _KERNEL
176#define NOTE_TIMER_UNITMASK 0x0003U
177#endif /* _KERNEL */
178
179/*
180 * This is currently visible to userland to work around broken
181 * programs which pull in <sys/proc.h> or <sys/select.h>.
182 */
183#include <sys/queue.h>
184struct knote;
185SLIST_HEAD(klist, knote);
186
187
188/*
189 * ioctl(2)s supported on kqueue descriptors.
190 */
191#include <sys/ioctl.h>
192
193struct kfilter_mapping {
194 char *name; /* name to lookup or return */
195 size_t len; /* length of name */
196 uint32_t filter; /* filter to lookup or return */
197};
198
199/* map filter to name (max size len) */
200#define KFILTER_BYFILTER _IOWR('k', 0, struct kfilter_mapping)
201/* map name to filter (len ignored) */
202#define KFILTER_BYNAME _IOWR('k', 1, struct kfilter_mapping)
203
204#ifdef _KERNEL
205
206#define KNOTE(list, hint) if (!SLIST_EMPTY(list)) knote(list, hint)
207
208/*
209 * Flag indicating hint is a signal. Used by EVFILT_SIGNAL, and also
210 * shared by EVFILT_PROC (all knotes attached to p->p_klist)
211 */
212#define NOTE_SIGNAL 0x08000000U
213
214/*
215 * Hint values for the optional f_touch event filter. If f_touch is not set
216 * to NULL and f_isfd is zero the f_touch filter will be called with the type
217 * argument set to EVENT_REGISTER during a kevent() system call. It is also
218 * called under the same conditions with the type argument set to EVENT_PROCESS
219 * when the event has been triggered.
220 */
221#define EVENT_REGISTER 1
222#define EVENT_PROCESS 2
223
224/*
225 * Callback methods for each filter type.
226 */
227struct filterops {
228 int f_flags; /* flags; see below */
229 int (*f_attach) (struct knote *);
230 /* called when knote is ADDed */
231 void (*f_detach) (struct knote *);
232 /* called when knote is DELETEd */
233 int (*f_event) (struct knote *, long);
234 /* called when event is triggered */
235 int (*f_touch) (struct knote *, struct kevent *, long);
236};
237
238/* filterops flags */
239#define FILTEROP_ISFD __BIT(0) /* ident == file descriptor */
240#define FILTEROP_MPSAFE __BIT(1) /* does not require KERNEL_LOCK */
241
242/*
243 * Field locking:
244 *
245 * f kn_kq->kq_fdp->fd_lock
246 * q kn_kq->kq_lock
247 * o object mutex (e.g. device driver or vnode interlock)
248 */
249struct kfilter;
250
251struct knote {
252 SLIST_ENTRY(knote) kn_link; /* f: for fd */
253 SLIST_ENTRY(knote) kn_selnext; /* o: for struct selinfo */
254 TAILQ_ENTRY(knote) kn_tqe; /* q: for struct kqueue */
255 struct kqueue *kn_kq; /* q: which queue we are on */
256 struct kevent kn_kevent; /* (see below for locking) */
257 uint32_t kn_status; /* q: flags below */
258 uint32_t kn_sfflags; /* saved filter flags */
259 uintptr_t kn_sdata; /* saved data field */
260 void *kn_obj; /* monitored obj */
261 const struct filterops *kn_fop;
262 struct kfilter *kn_kfilter;
263 void *kn_hook;
264 int kn_hookid;
265
266#define KN_ACTIVE 0x01U /* event has been triggered */
267#define KN_QUEUED 0x02U /* event is on queue */
268#define KN_DISABLED 0x04U /* event is disabled */
269#define KN_DETACHED 0x08U /* knote is detached */
270#define KN_MARKER 0x10U /* is a marker */
271#define KN_BUSY 0x20U /* is being scanned */
272#define KN_WILLDETACH 0x40U /* being detached imminently */
273/* Toggling KN_BUSY also requires kn_kq->kq_fdp->fd_lock. */
274#define __KN_FLAG_BITS \
275 "\20" \
276 "\1ACTIVE" \
277 "\2QUEUED" \
278 "\3DISABLED" \
279 "\4DETACHED" \
280 "\5MARKER" \
281 "\6BUSY" \
282 "\7WILLDETACH"
283
284
285/*
286 * The only time knote::kn_flags can be modified without synchronization
287 * is during filter attach, because the knote has not yet been published.
288 * This is usually to set EV_CLEAR or EV_ONESHOT as mandatory flags for
289 * that filter.
290 */
291#define kn_id kn_kevent.ident
292#define kn_filter kn_kevent.filter
293#define kn_flags kn_kevent.flags /* q */
294#define kn_fflags kn_kevent.fflags /* o */
295#define kn_data kn_kevent.data /* o */
296};
297
298#include <sys/systm.h> /* for copyin_t */
299
300struct lwp;
301struct timespec;
302
303void kqueue_init(void);
304void knote(struct klist *, long);
305void knote_fdclose(int);
306void knote_set_eof(struct knote *, uint32_t);
307void knote_clear_eof(struct knote *);
308
309typedef int (*kevent_fetch_changes_t)(void *, const struct kevent *,
310 struct kevent *, size_t, int);
311typedef int (*kevent_put_events_t)(void *, struct kevent *, struct kevent *,
312 size_t, int);
313
314struct kevent_ops {
315 void *keo_private;
316 copyin_t keo_fetch_timeout;
317 kevent_fetch_changes_t keo_fetch_changes;
318 kevent_put_events_t keo_put_events;
319};
320
321
322int kevent_fetch_changes(void *, const struct kevent *, struct kevent *,
323 size_t, int);
324int kevent_put_events(void *, struct kevent *, struct kevent *, size_t,
325 int);
326int kevent1(register_t *, int, const struct kevent *,
327 size_t, struct kevent *, size_t, const struct timespec *,
328 const struct kevent_ops *);
329
330int kfilter_register(const char *, const struct filterops *, int *);
331int kfilter_unregister(const char *);
332
333int filt_seltrue(struct knote *, long);
334extern const struct filterops seltrue_filtops;
335
336void klist_init(struct klist *);
337void klist_fini(struct klist *);
338void klist_insert(struct klist *, struct knote *);
339bool klist_remove(struct klist *, struct knote *);
340
341#else /* !_KERNEL */
342
343#include <sys/cdefs.h>
344struct timespec;
345
346__BEGIN_DECLS
347#if defined(_NETBSD_SOURCE)
348int kqueue(void);
349int kqueue1(int);
350#ifndef __LIBC12_SOURCE__
351int kevent(int, const struct kevent *, size_t, struct kevent *, size_t,
352 const struct timespec *) __RENAME(__kevent50);
353#endif
354#endif /* !_POSIX_C_SOURCE */
355__END_DECLS
356
357#endif /* !_KERNEL */
358
359#endif /* !_SYS_EVENT_H_ */