FreeBSD/Linux Kernel Cross Reference
sys/sys/event.h
1 /* $OpenBSD: event.h,v 1.67 2022/03/31 01:41:22 millert 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.11 2001/02/24 01:41:31 jlemon Exp $
29 */
30
31 #ifndef _SYS_EVENT_H_
32 #define _SYS_EVENT_H_
33
34 #define EVFILT_READ (-1)
35 #define EVFILT_WRITE (-2)
36 #define EVFILT_AIO (-3) /* attached to aio requests */
37 #define EVFILT_VNODE (-4) /* attached to vnodes */
38 #define EVFILT_PROC (-5) /* attached to struct process */
39 #define EVFILT_SIGNAL (-6) /* attached to struct process */
40 #define EVFILT_TIMER (-7) /* timers */
41 #define EVFILT_DEVICE (-8) /* devices */
42 #define EVFILT_EXCEPT (-9) /* exceptional conditions */
43
44 #define EVFILT_SYSCOUNT 9
45
46 #define EV_SET(kevp, a, b, c, d, e, f) do { \
47 struct kevent *__kevp = (kevp); \
48 (__kevp)->ident = (a); \
49 (__kevp)->filter = (b); \
50 (__kevp)->flags = (c); \
51 (__kevp)->fflags = (d); \
52 (__kevp)->data = (e); \
53 (__kevp)->udata = (f); \
54 } while(0)
55
56 struct kevent {
57 __uintptr_t ident; /* identifier for this event */
58 short filter; /* filter for event */
59 unsigned short flags; /* action flags for kqueue */
60 unsigned int fflags; /* filter flag value */
61 __int64_t data; /* filter data value */
62 void *udata; /* opaque user data identifier */
63 };
64
65 /* actions */
66 #define EV_ADD 0x0001 /* add event to kq (implies enable) */
67 #define EV_DELETE 0x0002 /* delete event from kq */
68 #define EV_ENABLE 0x0004 /* enable event */
69 #define EV_DISABLE 0x0008 /* disable event (not reported) */
70
71 /* flags */
72 #define EV_ONESHOT 0x0010 /* only report one occurrence */
73 #define EV_CLEAR 0x0020 /* clear event state after reporting */
74 #define EV_RECEIPT 0x0040 /* force EV_ERROR on success, data=0 */
75 #define EV_DISPATCH 0x0080 /* disable event after reporting */
76
77 #define EV_SYSFLAGS 0xf800 /* reserved by system */
78 #define EV_FLAG1 0x2000 /* filter-specific flag */
79
80 /* returned values */
81 #define EV_EOF 0x8000 /* EOF detected */
82 #define EV_ERROR 0x4000 /* error, data contains errno */
83
84 /*
85 * data/hint flags for EVFILT_{READ|WRITE}, shared with userspace
86 */
87 #define NOTE_LOWAT 0x0001 /* low water mark */
88 #define NOTE_EOF 0x0002 /* return on EOF */
89
90 /*
91 * data/hint flags for EVFILT_EXCEPT, shared with userspace and with
92 * EVFILT_{READ|WRITE}
93 */
94 #define NOTE_OOB 0x0004 /* OOB data on a socket */
95
96 /*
97 * data/hint flags for EVFILT_VNODE, shared with userspace
98 */
99 #define NOTE_DELETE 0x0001 /* vnode was removed */
100 #define NOTE_WRITE 0x0002 /* data contents changed */
101 #define NOTE_EXTEND 0x0004 /* size increased */
102 #define NOTE_ATTRIB 0x0008 /* attributes changed */
103 #define NOTE_LINK 0x0010 /* link count changed */
104 #define NOTE_RENAME 0x0020 /* vnode was renamed */
105 #define NOTE_REVOKE 0x0040 /* vnode access was revoked */
106 #define NOTE_TRUNCATE 0x0080 /* vnode was truncated */
107
108 /*
109 * data/hint flags for EVFILT_PROC, shared with userspace
110 */
111 #define NOTE_EXIT 0x80000000 /* process exited */
112 #define NOTE_FORK 0x40000000 /* process forked */
113 #define NOTE_EXEC 0x20000000 /* process exec'd */
114 #define NOTE_PCTRLMASK 0xf0000000 /* mask for hint bits */
115 #define NOTE_PDATAMASK 0x000fffff /* mask for pid */
116
117 /* additional flags for EVFILT_PROC */
118 #define NOTE_TRACK 0x00000001 /* follow across forks */
119 #define NOTE_TRACKERR 0x00000002 /* could not track child */
120 #define NOTE_CHILD 0x00000004 /* am a child process */
121
122 /* data/hint flags for EVFILT_DEVICE, shared with userspace */
123 #define NOTE_CHANGE 0x00000001 /* device change event */
124
125 /*
126 * This is currently visible to userland to work around broken
127 * programs which pull in <sys/proc.h> or <sys/selinfo.h>.
128 */
129 #include <sys/queue.h>
130
131 struct klistops;
132 struct knote;
133 SLIST_HEAD(knlist, knote);
134
135 struct klist {
136 struct knlist kl_list;
137 const struct klistops *kl_ops;
138 void *kl_arg;
139 };
140
141 #ifdef _KERNEL
142
143 /* kernel-only flags */
144 #define __EV_SELECT 0x0800 /* match behavior of select */
145 #define __EV_POLL 0x1000 /* match behavior of poll */
146 #define __EV_HUP EV_FLAG1 /* device or socket disconnected */
147
148 #define EVFILT_MARKER 0xf /* placemarker for tailq */
149
150 /*
151 * hint flag for in-kernel use - must not equal any existing note
152 */
153 #define NOTE_SUBMIT 0x01000000 /* initial knote submission */
154
155 #define KNOTE(list, hint) do { \
156 struct klist *__list = (list); \
157 if (!klist_empty(__list)) \
158 knote(__list, hint); \
159 } while (0)
160
161 #define KN_HASHSIZE 64 /* XXX should be tunable */
162
163 /*
164 * Flag indicating hint is a signal. Used by EVFILT_SIGNAL, and also
165 * shared by EVFILT_PROC (all knotes attached to p->p_klist)
166 */
167 #define NOTE_SIGNAL 0x08000000
168
169 /*
170 * = Event filter interface
171 *
172 * == .f_flags
173 *
174 * Defines properties of the event filter:
175 *
176 * - FILTEROP_ISFD Each knote of this filter is associated
177 * with a file descriptor.
178 *
179 * - FILTEROP_MPSAFE The kqueue subsystem can invoke .f_attach(),
180 * .f_detach(), .f_modify() and .f_process() without
181 * the kernel lock.
182 *
183 * == .f_attach()
184 *
185 * Attaches the knote to the object.
186 *
187 * == .f_detach()
188 *
189 * Detaches the knote from the object. The object must not use this knote
190 * for delivering events after this callback has returned.
191 *
192 * == .f_event()
193 *
194 * Notifies the filter about an event. Called through knote().
195 *
196 * == .f_modify()
197 *
198 * Modifies the knote with new state from the user.
199 *
200 * Returns non-zero if the knote has become active.
201 *
202 * == .f_process()
203 *
204 * Checks if the event is active and returns non-zero if the event should be
205 * returned to the user.
206 *
207 * If kev is non-NULL and the event is active, the callback should store
208 * the event's state in kev for delivery to the user.
209 *
210 * == Concurrency control
211 *
212 * The kqueue subsystem serializes calls of .f_attach(), .f_detach(),
213 * .f_modify() and .f_process().
214 */
215
216 #define FILTEROP_ISFD 0x00000001 /* ident == filedescriptor */
217 #define FILTEROP_MPSAFE 0x00000002 /* safe without kernel lock */
218
219 struct filterops {
220 int f_flags;
221 int (*f_attach)(struct knote *kn);
222 void (*f_detach)(struct knote *kn);
223 int (*f_event)(struct knote *kn, long hint);
224 int (*f_modify)(struct kevent *kev, struct knote *kn);
225 int (*f_process)(struct knote *kn, struct kevent *kev);
226 };
227
228 /*
229 * Locking:
230 * I immutable after creation
231 * o object lock
232 * q kn_kq->kq_lock
233 */
234 struct knote {
235 SLIST_ENTRY(knote) kn_link; /* for fd */
236 SLIST_ENTRY(knote) kn_selnext; /* for struct selinfo */
237 TAILQ_ENTRY(knote) kn_tqe;
238 struct kqueue *kn_kq; /* [I] which queue we are on */
239 struct kevent kn_kevent;
240 int kn_status; /* [q] */
241 int kn_sfflags; /* [o] saved filter flags */
242 __int64_t kn_sdata; /* [o] saved data field */
243 union {
244 struct file *p_fp; /* file data pointer */
245 struct process *p_process; /* process pointer */
246 } kn_ptr;
247 const struct filterops *kn_fop;
248 void *kn_hook; /* [o] */
249 unsigned int kn_pollid; /* [I] */
250
251 #define KN_ACTIVE 0x0001 /* event has been triggered */
252 #define KN_QUEUED 0x0002 /* event is on queue */
253 #define KN_DISABLED 0x0004 /* event is disabled */
254 #define KN_DETACHED 0x0008 /* knote is detached */
255 #define KN_PROCESSING 0x0010 /* knote is being processed */
256 #define KN_WAITING 0x0020 /* waiting on processing */
257
258 #define kn_id kn_kevent.ident /* [I] */
259 #define kn_filter kn_kevent.filter /* [I] */
260 #define kn_flags kn_kevent.flags /* [o] */
261 #define kn_fflags kn_kevent.fflags /* [o] */
262 #define kn_data kn_kevent.data /* [o] */
263 #define kn_udata kn_kevent.udata /* [o] */
264 #define kn_fp kn_ptr.p_fp /* [o] */
265 };
266
267 struct klistops {
268 void (*klo_assertlk)(void *);
269 int (*klo_lock)(void *);
270 void (*klo_unlock)(void *, int);
271 };
272
273 struct kqueue_scan_state {
274 struct kqueue *kqs_kq; /* kqueue of this scan */
275 struct knote kqs_start; /* start marker */
276 struct knote kqs_end; /* end marker */
277 int kqs_nevent; /* number of events collected */
278 int kqs_queued; /* if set, end marker is
279 * in queue */
280 };
281
282 struct mutex;
283 struct proc;
284 struct rwlock;
285 struct timespec;
286
287 extern const struct filterops sig_filtops;
288 extern const struct filterops dead_filtops;
289 extern const struct klistops socket_klistops;
290
291 extern void kqpoll_init(unsigned int);
292 extern void kqpoll_done(unsigned int);
293 extern void kqpoll_exit(void);
294 extern void knote(struct klist *list, long hint);
295 extern void knote_fdclose(struct proc *p, int fd);
296 extern void knote_processexit(struct process *);
297 extern void knote_assign(const struct kevent *, struct knote *);
298 extern void knote_submit(struct knote *, struct kevent *);
299 extern void kqueue_init(void);
300 extern void kqueue_init_percpu(void);
301 extern int kqueue_register(struct kqueue *kq, struct kevent *kev,
302 unsigned int pollid, struct proc *p);
303 extern int kqueue_scan(struct kqueue_scan_state *, int, struct kevent *,
304 struct timespec *, struct proc *, int *);
305 extern void kqueue_scan_setup(struct kqueue_scan_state *, struct kqueue *);
306 extern void kqueue_scan_finish(struct kqueue_scan_state *);
307 extern int filt_seltrue(struct knote *kn, long hint);
308 extern int seltrue_kqfilter(dev_t, struct knote *);
309 extern void klist_init(struct klist *, const struct klistops *, void *);
310 extern void klist_init_mutex(struct klist *, struct mutex *);
311 extern void klist_init_rwlock(struct klist *, struct rwlock *);
312 extern void klist_free(struct klist *);
313 extern void klist_insert(struct klist *, struct knote *);
314 extern void klist_insert_locked(struct klist *, struct knote *);
315 extern void klist_remove(struct klist *, struct knote *);
316 extern void klist_remove_locked(struct klist *, struct knote *);
317 extern void klist_invalidate(struct klist *);
318
319 static inline int
320 knote_modify_fn(const struct kevent *kev, struct knote *kn,
321 int (*f_event)(struct knote *, long))
322 {
323 knote_assign(kev, kn);
324 return ((*f_event)(kn, 0));
325 }
326
327 static inline int
328 knote_modify(const struct kevent *kev, struct knote *kn)
329 {
330 return (knote_modify_fn(kev, kn, kn->kn_fop->f_event));
331 }
332
333 static inline int
334 knote_process_fn(struct knote *kn, struct kevent *kev,
335 int (*f_event)(struct knote *, long))
336 {
337 int active;
338
339 /*
340 * If called from kqueue_scan(), skip f_event
341 * when EV_ONESHOT is set, to preserve old behaviour.
342 */
343 if (kev != NULL && (kn->kn_flags & EV_ONESHOT))
344 active = 1;
345 else
346 active = (*f_event)(kn, 0);
347 if (active)
348 knote_submit(kn, kev);
349 return (active);
350 }
351
352 static inline int
353 knote_process(struct knote *kn, struct kevent *kev)
354 {
355 return (knote_process_fn(kn, kev, kn->kn_fop->f_event));
356 }
357
358 static inline int
359 klist_empty(struct klist *klist)
360 {
361 return (SLIST_EMPTY(&klist->kl_list));
362 }
363
364 #else /* !_KERNEL */
365
366 #include <sys/cdefs.h>
367 struct timespec;
368
369 __BEGIN_DECLS
370 int kqueue(void);
371 int kevent(int kq, const struct kevent *changelist, int nchanges,
372 struct kevent *eventlist, int nevents,
373 const struct timespec *timeout);
374 __END_DECLS
375
376 #endif /* !_KERNEL */
377
378 #endif /* !_SYS_EVENT_H_ */
Cache object: 339759a9730c6f164a40d2da7df3ccc3
|