xref: /freebsd/sys/sys/event.h (revision 1bd74d201a534540614663686890ab96a3bbe2c7)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
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 
29 #ifndef _SYS_EVENT_H_
30 #define _SYS_EVENT_H_
31 
32 #include <sys/_types.h>
33 #include <sys/queue.h>
34 
35 #define EVFILT_READ		(-1)
36 #define EVFILT_WRITE		(-2)
37 #define EVFILT_AIO		(-3)	/* attached to aio requests */
38 #define EVFILT_VNODE		(-4)	/* attached to vnodes */
39 #define EVFILT_PROC		(-5)	/* attached to struct proc */
40 #define EVFILT_SIGNAL		(-6)	/* attached to struct proc */
41 #define EVFILT_TIMER		(-7)	/* timers */
42 #define EVFILT_PROCDESC		(-8)	/* attached to process descriptors */
43 #define EVFILT_FS		(-9)	/* filesystem events */
44 #define EVFILT_LIO		(-10)	/* attached to lio requests */
45 #define EVFILT_USER		(-11)	/* User events */
46 #define EVFILT_SENDFILE		(-12)	/* attached to sendfile requests */
47 #define EVFILT_EMPTY		(-13)	/* empty send socket buf */
48 #define EVFILT_JAIL		(-14)	/* attached to struct prison */
49 #define EVFILT_SYSCOUNT		14
50 
51 #if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
52 #define	EV_SET(kevp_, a, b, c, d, e, f) do {	\
53 	*(kevp_) = (struct kevent){		\
54 	    .ident = (a),			\
55 	    .filter = (b),			\
56 	    .flags = (c),			\
57 	    .fflags = (d),			\
58 	    .data = (e),			\
59 	    .udata = (f),			\
60 	    .ext = {0},				\
61 	};					\
62 } while (0)
63 #else /* Pre-C99 or not STDC (e.g., C++) */
64 /*
65  * The definition of the local variable kevp could possibly conflict
66  * with a user-defined value passed in parameters a-f.
67  */
68 #define EV_SET(kevp_, a, b, c, d, e, f) do {	\
69 	struct kevent *kevp = (kevp_);		\
70 	(kevp)->ident = (a);			\
71 	(kevp)->filter = (b);			\
72 	(kevp)->flags = (c);			\
73 	(kevp)->fflags = (d);			\
74 	(kevp)->data = (e);			\
75 	(kevp)->udata = (f);			\
76 	(kevp)->ext[0] = 0;			\
77 	(kevp)->ext[1] = 0;			\
78 	(kevp)->ext[2] = 0;			\
79 	(kevp)->ext[3] = 0;			\
80 } while (0)
81 #endif
82 
83 struct kevent {
84 	__uintptr_t	ident;		/* identifier for this event */
85 	short		filter;		/* filter for event */
86 	unsigned short	flags;		/* action flags for kqueue */
87 	unsigned int	fflags;		/* filter flag value */
88 	__int64_t	data;		/* filter data value */
89 	void		*udata;		/* opaque user data identifier */
90 	__uint64_t	ext[4];		/* extensions */
91 };
92 
93 #if defined(_WANT_FREEBSD11_KEVENT)
94 /* Older structure used in FreeBSD 11.x and older. */
95 struct freebsd11_kevent {
96 	__uintptr_t	ident;		/* identifier for this event */
97 	short		filter;		/* filter for event */
98 	unsigned short	flags;
99 	unsigned int	fflags;
100 	__intptr_t	data;
101 	void		*udata;		/* opaque user data identifier */
102 };
103 #endif
104 
105 #if defined(_WANT_KEVENT32) || defined(_KERNEL)
106 struct kevent32 {
107 	__uint32_t	ident;		/* identifier for this event */
108 	short		filter;		/* filter for event */
109 	unsigned short	flags;
110 	unsigned int	fflags;
111 #ifndef __amd64__
112 	__uint32_t	pad0;
113 #endif
114 	__uint32_t	data1, data2;
115 	__uint32_t	udata;		/* opaque user data identifier */
116 #ifndef __amd64__
117 	__uint32_t	pad1;
118 #endif
119 	__uint32_t	ext64[8];
120 };
121 
122 #ifdef _WANT_FREEBSD11_KEVENT
123 struct freebsd11_kevent32 {
124 	__uint32_t	ident;		/* identifier for this event */
125 	short		filter;		/* filter for event */
126 	unsigned short	flags;
127 	unsigned int	fflags;
128 	__int32_t	data;
129 	__uint32_t	udata;		/* opaque user data identifier */
130 };
131 #endif
132 #endif
133 
134 /* actions */
135 #define EV_ADD		0x0001		/* add event to kq (implies enable) */
136 #define EV_DELETE	0x0002		/* delete event from kq */
137 #define EV_ENABLE	0x0004		/* enable event */
138 #define EV_DISABLE	0x0008		/* disable event (not reported) */
139 #define EV_FORCEONESHOT	0x0100		/* enable _ONESHOT and force trigger */
140 #define EV_KEEPUDATA	0x0200		/* do not update the udata field */
141 
142 /* flags */
143 #define EV_ONESHOT	0x0010		/* only report one occurrence */
144 #define EV_CLEAR	0x0020		/* clear event state after reporting */
145 #define EV_RECEIPT	0x0040		/* force EV_ERROR on success, data=0 */
146 #define EV_DISPATCH	0x0080		/* disable event after reporting */
147 
148 #define EV_SYSFLAGS	0xF000		/* reserved by system */
149 #define	EV_DROP		0x1000		/* note should be dropped */
150 #define EV_FLAG1	0x2000		/* filter-specific flag */
151 #define EV_FLAG2	0x4000		/* filter-specific flag */
152 
153 /* returned values */
154 #define EV_EOF		0x8000		/* EOF detected */
155 #define EV_ERROR	0x4000		/* error, data contains errno */
156 
157  /*
158   * data/hint flags/masks for EVFILT_USER, shared with userspace
159   *
160   * On input, the top two bits of fflags specifies how the lower twenty four
161   * bits should be applied to the stored value of fflags.
162   *
163   * On output, the top two bits will always be set to NOTE_FFNOP and the
164   * remaining twenty four bits will contain the stored fflags value.
165   */
166 #define NOTE_FFNOP	0x00000000		/* ignore input fflags */
167 #define NOTE_FFAND	0x40000000		/* AND fflags */
168 #define NOTE_FFOR	0x80000000		/* OR fflags */
169 #define NOTE_FFCOPY	0xc0000000		/* copy fflags */
170 #define NOTE_FFCTRLMASK	0xc0000000		/* masks for operations */
171 #define NOTE_FFLAGSMASK	0x00ffffff
172 
173 #define NOTE_TRIGGER	0x01000000		/* Cause the event to be
174 						   triggered for output. */
175 
176 /*
177  * data/hint flags for EVFILT_{READ|WRITE}, shared with userspace
178  */
179 #define NOTE_LOWAT	0x0001			/* low water mark */
180 #define NOTE_FILE_POLL	0x0002			/* behave like poll() */
181 
182 /*
183  * data/hint flags for EVFILT_VNODE, shared with userspace
184  */
185 #define	NOTE_DELETE	0x0001			/* vnode was removed */
186 #define	NOTE_WRITE	0x0002			/* data contents changed */
187 #define	NOTE_EXTEND	0x0004			/* size increased */
188 #define	NOTE_ATTRIB	0x0008			/* attributes changed */
189 #define	NOTE_LINK	0x0010			/* link count changed */
190 #define	NOTE_RENAME	0x0020			/* vnode was renamed */
191 #define	NOTE_REVOKE	0x0040			/* vnode access was revoked */
192 #define	NOTE_OPEN	0x0080			/* vnode was opened */
193 #define	NOTE_CLOSE	0x0100			/* file closed, fd did not
194 						   allowed write */
195 #define	NOTE_CLOSE_WRITE 0x0200			/* file closed, fd did allowed
196 						   write */
197 #define	NOTE_READ	0x0400			/* file was read */
198 
199 /*
200  * data/hint flags for EVFILT_PROC and EVFILT_PROCDESC, shared with userspace
201  */
202 #define	NOTE_EXIT	0x80000000		/* process exited */
203 #define	NOTE_FORK	0x40000000		/* process forked */
204 #define	NOTE_EXEC	0x20000000		/* process exec'd */
205 #define	NOTE_PCTRLMASK	0xf0000000		/* mask for hint bits */
206 #define	NOTE_PDATAMASK	0x000fffff		/* mask for pid */
207 
208 /* data/hint flags for EVFILT_JAIL */
209 #define	NOTE_JAIL_SET		0x80000000	/* jail was modified */
210 #define	NOTE_JAIL_CHILD		0x40000000	/* child jail was created */
211 #define	NOTE_JAIL_ATTACH	0x20000000	/* jail was attached to */
212 #define	NOTE_JAIL_REMOVE	0x10000000	/* jail was removed */
213 #define NOTE_JAIL_ATTACH_MULTI	0x08000000	/* multiple procs attached */
214 #define	NOTE_JAIL_CTRLMASK	0xf0000000	/* mask for hint bits */
215 #define	NOTE_JAIL_DATAMASK	0x000fffff	/* mask for pid */
216 
217 /* additional flags for EVFILT_PROC and EVFILT_JAIL */
218 #define	NOTE_TRACK	0x00000001		/* follow across fork/create */
219 #define	NOTE_TRACKERR	0x00000002		/* could not track child */
220 #define	NOTE_CHILD	0x00000004		/* am a child process/jail */
221 
222 /* additional flags for EVFILT_TIMER */
223 #define NOTE_SECONDS		0x00000001	/* data is seconds */
224 #define NOTE_MSECONDS		0x00000002	/* data is milliseconds */
225 #define NOTE_USECONDS		0x00000004	/* data is microseconds */
226 #define NOTE_NSECONDS		0x00000008	/* data is nanoseconds */
227 #define	NOTE_ABSTIME		0x00000010	/* timeout is absolute */
228 
229 /* Flags for kqueuex(2) */
230 #define	KQUEUE_CLOEXEC	0x00000001	/* close on exec */
231 
232 struct knote;
233 SLIST_HEAD(klist, knote);
234 struct kqueue;
235 TAILQ_HEAD(kqlist, kqueue);
236 struct knlist {
237 	struct	klist	kl_list;
238 	void    (*kl_lock)(void *);	/* lock function */
239 	void    (*kl_unlock)(void *);
240 	void	(*kl_assert_lock)(void *, int);
241 	void	*kl_lockarg;		/* argument passed to lock functions */
242 	int	kl_autodestroy;
243 };
244 
245 #ifdef _KERNEL
246 
247 /*
248  * Flags for knote call
249  */
250 #define	KNF_LISTLOCKED	0x0001			/* knlist is locked */
251 #define	KNF_NOKQLOCK	0x0002			/* do not keep KQ_LOCK */
252 
253 #define KNOTE(list, hint, flags)	knote(list, hint, flags)
254 #define KNOTE_LOCKED(list, hint)	knote(list, hint, KNF_LISTLOCKED)
255 #define KNOTE_UNLOCKED(list, hint)	knote(list, hint, 0)
256 
257 #define	KNLIST_EMPTY(list)		SLIST_EMPTY(&(list)->kl_list)
258 
259 /*
260  * Flag indicating hint is a signal.  Used by EVFILT_SIGNAL, and also
261  * shared by EVFILT_PROC  (all knotes attached to p->p_klist)
262  */
263 #define NOTE_SIGNAL	0x08000000
264 
265 /*
266  * Hint values for the optional f_touch event filter.  If f_touch is not set
267  * to NULL and f_isfd is zero the f_touch filter will be called with the type
268  * argument set to EVENT_REGISTER during a kevent() system call.  It is also
269  * called under the same conditions with the type argument set to EVENT_PROCESS
270  * when the event has been triggered.
271  */
272 #define EVENT_REGISTER	1
273 #define EVENT_PROCESS	2
274 
275 struct kinfo_knote;
276 struct proc;
277 
278 struct filterops {
279 	int	f_isfd;		/* true if ident == filedescriptor */
280 	int	(*f_attach)(struct knote *kn);
281 	void	(*f_detach)(struct knote *kn);
282 	int	(*f_event)(struct knote *kn, long hint);
283 	void	(*f_touch)(struct knote *kn, struct kevent *kev, u_long type);
284 	int	(*f_userdump)(struct proc *p, struct knote *kn,
285 		    struct kinfo_knote *kin);
286 };
287 
288 /*
289  * An in-flux knote cannot be dropped from its kq while the kq is
290  * unlocked.  If the KN_SCAN flag is not set, a thread can only set
291  * kn_influx when it is exclusive owner of the knote state, and can
292  * modify kn_status as if it had the KQ lock.  KN_SCAN must not be set
293  * on a knote which is already in flux.
294  *
295  * kn_sfflags, kn_sdata, and kn_kevent are protected by the knlist lock.
296  */
297 struct knote {
298 	SLIST_ENTRY(knote)	kn_link;	/* for kq */
299 	SLIST_ENTRY(knote)	kn_selnext;	/* for struct selinfo */
300 	struct			knlist *kn_knlist;	/* f_attach populated */
301 	TAILQ_ENTRY(knote)	kn_tqe;
302 	struct			kqueue *kn_kq;	/* which queue we are on */
303 	struct 			kevent kn_kevent;
304 	void			*kn_hook;
305 	int			kn_hookid;
306 	int			kn_status;	/* protected by kq lock */
307 #define KN_ACTIVE	0x01			/* event has been triggered */
308 #define KN_QUEUED	0x02			/* event is on queue */
309 #define KN_DISABLED	0x04			/* event is disabled */
310 #define KN_DETACHED	0x08			/* knote is detached */
311 #define KN_MARKER	0x20			/* ignore this knote */
312 #define KN_KQUEUE	0x40			/* this knote belongs to a kq */
313 #define	KN_SCAN		0x100			/* flux set in kqueue_scan() */
314 	int			kn_influx;
315 	int			kn_sfflags;	/* saved filter flags */
316 	int64_t			kn_sdata;	/* saved data field */
317 	union {
318 		struct		file *p_fp;	/* file data pointer */
319 		struct		proc *p_proc;	/* proc pointer */
320 		struct		kaiocb *p_aio;	/* AIO job pointer */
321 		struct		aioliojob *p_lio;	/* LIO job pointer */
322 		struct		prison *p_prison;	/* prison pointer */
323 		void		*p_v;		/* generic other pointer */
324 	} kn_ptr;
325 	const struct		filterops *kn_fop;
326 
327 #define kn_id		kn_kevent.ident
328 #define kn_filter	kn_kevent.filter
329 #define kn_flags	kn_kevent.flags
330 #define kn_fflags	kn_kevent.fflags
331 #define kn_data		kn_kevent.data
332 #define kn_fp		kn_ptr.p_fp
333 };
334 struct kevent_copyops {
335 	void	*arg;
336 	int	(*k_copyout)(void *arg, struct kevent *kevp, int count);
337 	int	(*k_copyin)(void *arg, struct kevent *kevp, int count);
338 	size_t	kevent_size;
339 };
340 
341 struct thread;
342 struct proc;
343 struct knlist;
344 struct mtx;
345 struct rwlock;
346 
347 void	knote(struct knlist *list, long hint, int lockflags);
348 void	knote_fork(struct knlist *list, int pid);
349 struct knlist *knlist_alloc(struct mtx *lock);
350 void	knlist_detach(struct knlist *knl);
351 void	knlist_add(struct knlist *knl, struct knote *kn, int islocked);
352 void	knlist_remove(struct knlist *knl, struct knote *kn, int islocked);
353 int	knlist_empty(struct knlist *knl);
354 void	knlist_init(struct knlist *knl, void *lock, void (*kl_lock)(void *),
355 	    void (*kl_unlock)(void *), void (*kl_assert_lock)(void *, int));
356 void	knlist_init_mtx(struct knlist *knl, struct mtx *lock);
357 void	knlist_destroy(struct knlist *knl);
358 void	knlist_cleardel(struct knlist *knl, struct thread *td,
359 	    int islocked, int killkn);
360 #define knlist_clear(knl, islocked)				\
361 	knlist_cleardel((knl), NULL, (islocked), 0)
362 #define knlist_delete(knl, td, islocked)			\
363 	knlist_cleardel((knl), (td), (islocked), 1)
364 void	knote_fdclose(struct thread *p, int fd);
365 int 	kqfd_register(int fd, struct kevent *kev, struct thread *p,
366 	    int mflag);
367 int	kqueue_add_filteropts(int filt, const struct filterops *filtops);
368 int	kqueue_del_filteropts(int filt);
369 void	kqueue_drain_schedtask(void);
370 
371 #else 	/* !_KERNEL */
372 
373 #include <sys/cdefs.h>
374 struct timespec;
375 
376 __BEGIN_DECLS
377 int     kqueue(void);
378 int     kqueuex(unsigned flags);
379 int     kqueue1(int flags);
380 int     kevent(int kq, const struct kevent *changelist, int nchanges,
381 	    struct kevent *eventlist, int nevents,
382 	    const struct timespec *timeout);
383 __END_DECLS
384 
385 #endif /* !_KERNEL */
386 
387 #endif /* !_SYS_EVENT_H_ */
388