]> git.saurik.com Git - apple/xnu.git/blame - bsd/sys/event.h
xnu-2050.7.9.tar.gz
[apple/xnu.git] / bsd / sys / event.h
CommitLineData
55e303ae 1/*
316670eb 2 * Copyright (c) 2003-2012 Apple Inc. All rights reserved.
55e303ae 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
55e303ae 5 *
2d21ac55
A
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
8f6c56a5 14 *
2d21ac55
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
8f6c56a5 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
55e303ae
A
27 */
28/*-
29 * Copyright (c) 1999,2000,2001 Jonathan Lemon <jlemon@FreeBSD.org>
30 * All rights reserved.
31 *
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
34 * are met:
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 *
41 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * $FreeBSD: src/sys/sys/event.h,v 1.5.2.5 2001/12/14 19:21:22 jlemon Exp $
54 */
55
56#ifndef _SYS_EVENT_H_
57#define _SYS_EVENT_H_
58
91447636
A
59#include <machine/types.h>
60#include <sys/cdefs.h>
61#include <stdint.h>
62
55e303ae
A
63#define EVFILT_READ (-1)
64#define EVFILT_WRITE (-2)
65#define EVFILT_AIO (-3) /* attached to aio requests */
66#define EVFILT_VNODE (-4) /* attached to vnodes */
67#define EVFILT_PROC (-5) /* attached to struct proc */
68#define EVFILT_SIGNAL (-6) /* attached to struct proc */
69#define EVFILT_TIMER (-7) /* timers */
b0d623f7 70#define EVFILT_MACHPORT (-8) /* Mach portsets */
55e303ae 71#define EVFILT_FS (-9) /* Filesystem events */
b0d623f7 72#define EVFILT_USER (-10) /* User events */
6d2010ae
A
73 /* (-11) unused */
74#define EVFILT_VM (-12) /* Virtual memory events */
55e303ae 75
316670eb
A
76#ifdef PRIVATE
77#define EVFILT_SOCK (-13) /* Socket events */
78#endif /* PRIVATE */
79
80#define EVFILT_SYSCOUNT 13
91447636
A
81#define EVFILT_THREADMARKER EVFILT_SYSCOUNT /* Internal use only */
82
0c530ab8 83#pragma pack(4)
55e303ae
A
84
85struct kevent {
86 uintptr_t ident; /* identifier for this event */
b0d623f7
A
87 int16_t filter; /* filter for event */
88 uint16_t flags; /* general flags */
89 uint32_t fflags; /* filter-specific flags */
91447636 90 intptr_t data; /* filter-specific data */
55e303ae 91 void *udata; /* opaque user data identifier */
91447636
A
92};
93
94#ifdef KERNEL_PRIVATE
95
b0d623f7 96struct user64_kevent {
91447636 97 uint64_t ident; /* identifier for this event */
b0d623f7
A
98 int16_t filter; /* filter for event */
99 uint16_t flags; /* general flags */
100 uint32_t fflags; /* filter-specific flags */
91447636
A
101 int64_t data; /* filter-specific data */
102 user_addr_t udata; /* opaque user data identifier */
55e303ae
A
103};
104
b0d623f7
A
105struct user32_kevent {
106 uint32_t ident; /* identifier for this event */
107 int16_t filter; /* filter for event */
108 uint16_t flags; /* general flags */
109 uint32_t fflags; /* filter-specific flags */
110 int32_t data; /* filter-specific data */
111 user32_addr_t udata; /* opaque user data identifier */
112};
113
91447636
A
114#endif
115
0c530ab8 116#pragma pack()
91447636 117
b0d623f7
A
118struct kevent64_s {
119 uint64_t ident; /* identifier for this event */
120 int16_t filter; /* filter for event */
121 uint16_t flags; /* general flags */
122 uint32_t fflags; /* filter-specific flags */
123 int64_t data; /* filter-specific data */
124 uint64_t udata; /* opaque user data identifier */
125 uint64_t ext[2]; /* filter-specific extensions */
126};
127
55e303ae
A
128#define EV_SET(kevp, a, b, c, d, e, f) do { \
129 struct kevent *__kevp__ = (kevp); \
130 __kevp__->ident = (a); \
131 __kevp__->filter = (b); \
132 __kevp__->flags = (c); \
133 __kevp__->fflags = (d); \
134 __kevp__->data = (e); \
135 __kevp__->udata = (f); \
136} while(0)
137
b0d623f7
A
138#define EV_SET64(kevp, a, b, c, d, e, f, g, h) do { \
139 struct kevent64_s *__kevp__ = (kevp); \
140 __kevp__->ident = (a); \
141 __kevp__->filter = (b); \
142 __kevp__->flags = (c); \
143 __kevp__->fflags = (d); \
144 __kevp__->data = (e); \
145 __kevp__->udata = (f); \
146 __kevp__->ext[0] = (g); \
147 __kevp__->ext[1] = (h); \
148} while(0)
149
55e303ae
A
150/* actions */
151#define EV_ADD 0x0001 /* add event to kq (implies enable) */
152#define EV_DELETE 0x0002 /* delete event from kq */
153#define EV_ENABLE 0x0004 /* enable event */
154#define EV_DISABLE 0x0008 /* disable event (not reported) */
2d21ac55 155#define EV_RECEIPT 0x0040 /* force EV_ERROR on success, data == 0 */
55e303ae
A
156
157/* flags */
158#define EV_ONESHOT 0x0010 /* only report one occurrence */
159#define EV_CLEAR 0x0020 /* clear event state after reporting */
b0d623f7 160#define EV_DISPATCH 0x0080 /* disable event after reporting */
55e303ae
A
161
162#define EV_SYSFLAGS 0xF000 /* reserved by system */
91447636 163#define EV_FLAG0 0x1000 /* filter-specific flag */
55e303ae
A
164#define EV_FLAG1 0x2000 /* filter-specific flag */
165
166/* returned values */
167#define EV_EOF 0x8000 /* EOF detected */
168#define EV_ERROR 0x4000 /* error, data contains errno */
169
170/*
91447636
A
171 * Filter specific flags for EVFILT_READ
172 *
173 * The default behavior for EVFILT_READ is to make the "read" determination
174 * relative to the current file descriptor read pointer. The EV_POLL
175 * flag indicates the determination should be made via poll(2) semantics
176 * (which always returns true for regular files - regardless of the amount
177 * of unread data in the file).
178 *
179 * On input, EV_OOBAND specifies that only OOB data should be looked for.
180 * The returned data count is the number of bytes beyond the current OOB marker.
181 *
182 * On output, EV_OOBAND indicates that OOB data is present
183 * If it was not specified as an input parameter, then the data count is the
184 * number of bytes before the current OOB marker. If at the marker, the
185 * data count indicates the number of bytes available after it. In either
186 * case, it's the amount of data one could expect to receive next.
55e303ae 187 */
91447636
A
188#define EV_POLL EV_FLAG0
189#define EV_OOBAND EV_FLAG1
55e303ae 190
b0d623f7
A
191/*
192 * data/hint fflags for EVFILT_USER, shared with userspace
193 */
194
195/*
196 * On input, NOTE_TRIGGER causes the event to be triggered for output.
197 */
198#define NOTE_TRIGGER 0x01000000
b0d623f7
A
199
200/*
201 * On input, the top two bits of fflags specifies how the lower twenty four
202 * bits should be applied to the stored value of fflags.
203 *
204 * On output, the top two bits will always be set to NOTE_FFNOP and the
205 * remaining twenty four bits will contain the stored fflags value.
206 */
207#define NOTE_FFNOP 0x00000000 /* ignore input fflags */
208#define NOTE_FFAND 0x40000000 /* and fflags */
209#define NOTE_FFOR 0x80000000 /* or fflags */
210#define NOTE_FFCOPY 0xc0000000 /* copy fflags */
211#define NOTE_FFCTRLMASK 0xc0000000 /* mask for operations */
212#define NOTE_FFLAGSMASK 0x00ffffff
213
55e303ae 214/*
91447636
A
215 * data/hint fflags for EVFILT_{READ|WRITE}, shared with userspace
216 *
217 * The default behavior for EVFILT_READ is to make the determination
218 * realtive to the current file descriptor read pointer.
219 */
220#define NOTE_LOWAT 0x00000001 /* low water mark */
221/*
222 * data/hint fflags for EVFILT_VNODE, shared with userspace
55e303ae 223 */
91447636
A
224#define NOTE_DELETE 0x00000001 /* vnode was removed */
225#define NOTE_WRITE 0x00000002 /* data contents changed */
226#define NOTE_EXTEND 0x00000004 /* size increased */
227#define NOTE_ATTRIB 0x00000008 /* attributes changed */
228#define NOTE_LINK 0x00000010 /* link count changed */
229#define NOTE_RENAME 0x00000020 /* vnode was renamed */
230#define NOTE_REVOKE 0x00000040 /* vnode access was revoked */
b0d623f7 231#define NOTE_NONE 0x00000080 /* No specific vnode event: to test for EVFILT_READ activation*/
55e303ae
A
232
233/*
91447636 234 * data/hint fflags for EVFILT_PROC, shared with userspace
2d21ac55
A
235 *
236 * Please note that EVFILT_PROC and EVFILT_SIGNAL share the same knote list
237 * that hangs off the proc structure. They also both play games with the hint
238 * passed to KNOTE(). If NOTE_SIGNAL is passed as a hint, then the lower bits
239 * of the hint contain the signal. IF NOTE_FORK is passed, then the lower bits
6d2010ae 240 * contain the PID of the child.
55e303ae
A
241 */
242#define NOTE_EXIT 0x80000000 /* process exited */
243#define NOTE_FORK 0x40000000 /* process forked */
244#define NOTE_EXEC 0x20000000 /* process exec'd */
2d21ac55
A
245#define NOTE_REAP 0x10000000 /* process reaped */
246#define NOTE_SIGNAL 0x08000000 /* shared with EVFILT_SIGNAL */
6d2010ae
A
247#define NOTE_EXITSTATUS 0x04000000 /* exit status to be returned, valid for child process only */
248#define NOTE_RESOURCEEND 0x02000000 /* resource limit reached, resource type returned */
316670eb
A
249
250#if CONFIG_EMBEDDED
251/* 0x01000000 is reserved for future use */
252
253/* App states notification */
254#define NOTE_APPACTIVE 0x00800000 /* app went to active state */
255#define NOTE_APPBACKGROUND 0x00400000 /* app went to background */
256#define NOTE_APPNONUI 0x00200000 /* app went to active with no UI */
257#define NOTE_APPINACTIVE 0x00100000 /* app went to inactive state */
258#define NOTE_APPALLSTATES 0x00f00000
259#endif /* CONFIG_EMBEDDED */
260
2d21ac55
A
261#define NOTE_PDATAMASK 0x000fffff /* mask for pid/signal */
262#define NOTE_PCTRLMASK (~NOTE_PDATAMASK)
55e303ae 263
6d2010ae
A
264/*
265 * data/hint fflags for EVFILT_VM, shared with userspace.
266 */
267#define NOTE_VM_PRESSURE 0x80000000 /* will react on memory pressure */
268#define NOTE_VM_PRESSURE_TERMINATE 0x40000000 /* will quit on memory pressure, possibly after cleaning up dirty state */
269#define NOTE_VM_PRESSURE_SUDDEN_TERMINATE 0x20000000 /* will quit immediately on memory pressure */
270#define NOTE_VM_ERROR 0x10000000 /* there was an error */
271
91447636
A
272/*
273 * data/hint fflags for EVFILT_TIMER, shared with userspace.
274 * The default is a (repeating) interval timer with the data
275 * specifying the timeout interval in milliseconds.
276 *
277 * All timeouts are implicitly EV_CLEAR events.
278 */
279#define NOTE_SECONDS 0x00000001 /* data is seconds */
280#define NOTE_USECONDS 0x00000002 /* data is microseconds */
281#define NOTE_NSECONDS 0x00000004 /* data is nanoseconds */
282#define NOTE_ABSOLUTE 0x00000008 /* absolute timeout */
283 /* ... implicit EV_ONESHOT */
316670eb
A
284#ifdef PRIVATE
285/*
286 * data/hint fflags for EVFILT_SOCK, shared with userspace.
287 *
288 */
289#define NOTE_CONNRESET 0x00000001 /* Received RST */
290#define NOTE_READCLOSED 0x00000002 /* Read side is shutdown */
291#define NOTE_WRITECLOSED 0x00000004 /* Write side is shutdown */
292#define NOTE_TIMEOUT 0x00000008 /* timeout: rexmt, keep-alive or persist */
293#define NOTE_NOSRCADDR 0x00000010 /* source address not available */
294#define NOTE_IFDENIED 0x00000020 /* interface denied connection */
295#define NOTE_SUSPEND 0x00000040 /* output queue suspended */
296#define NOTE_RESUME 0x00000080 /* output queue resumed */
297#define NOTE_KEEPALIVE 0x00000100 /* TCP Keepalive received */
298
299#endif /* PRIVATE */
300
b0d623f7
A
301/*
302 * data/hint fflags for EVFILT_MACHPORT, shared with userspace.
303 *
6d2010ae 304 * Only portsets are supported at this time.
b0d623f7
A
305 *
306 * The fflags field can optionally contain the MACH_RCV_MSG, MACH_RCV_LARGE,
307 * and related trailer receive options as defined in <mach/message.h>.
308 * The presence of these flags directs the kevent64() call to attempt to receive
309 * the message during kevent delivery, rather than just indicate that a message exists.
310 * On setup, The ext[0] field contains the receive buffer pointer and ext[1] contains
311 * the receive buffer length. Upon event delivery, the actual received message size
312 * is returned in ext[1]. As with mach_msg(), the buffer must be large enough to
313 * receive the message and the requested (or default) message trailers. In addition,
314 * the fflags field contains the return code normally returned by mach_msg().
315 *
316 * If no message receipt options were provided in the fflags field on setup, no
317 * message is received by this call. Instead, on output, the data field simply
318 * contains the name of the actual port detected with a message waiting.
319 */
320
2d21ac55
A
321/*
322 * DEPRECATED!!!!!!!!!
323 * NOTE_TRACK, NOTE_TRACKERR, and NOTE_CHILD are no longer supported as of 10.5
324 */
55e303ae
A
325/* additional flags for EVFILT_PROC */
326#define NOTE_TRACK 0x00000001 /* follow across forks */
327#define NOTE_TRACKERR 0x00000002 /* could not track child */
328#define NOTE_CHILD 0x00000004 /* am a child process */
329
330
55e303ae 331
91447636
A
332#ifndef KERNEL
333/* Temporay solution for BootX to use inode.h till kqueue moves to vfs layer */
334#include <sys/queue.h>
335struct knote;
336SLIST_HEAD(klist, knote);
337#endif
338
339#ifdef KERNEL
340
341#ifdef KERNEL_PRIVATE
55e303ae
A
342#include <sys/queue.h>
343
344#ifdef MALLOC_DECLARE
345MALLOC_DECLARE(M_KQUEUE);
346#endif
347
91447636
A
348TAILQ_HEAD(kqtailq, knote); /* a list of "queued" events */
349
55e303ae 350struct knote {
91447636
A
351 int kn_inuse; /* inuse count */
352 struct kqtailq *kn_tq; /* pointer to tail queue */
353 TAILQ_ENTRY(knote) kn_tqe; /* linkage for tail queue */
354 struct kqueue *kn_kq; /* which kqueue we are on */
355 SLIST_ENTRY(knote) kn_link; /* linkage for search list */
55e303ae 356 SLIST_ENTRY(knote) kn_selnext; /* klist element chain */
55e303ae 357 union {
91447636 358 struct fileproc *p_fp; /* file data pointer */
55e303ae 359 struct proc *p_proc; /* proc pointer */
b0d623f7 360 struct ipc_pset *p_pset; /* pset pointer */
55e303ae
A
361 } kn_ptr;
362 struct filterops *kn_fop;
91447636 363 int kn_status; /* status bits */
55e303ae 364 int kn_sfflags; /* saved filter flags */
b0d623f7
A
365 struct kevent64_s kn_kevent;
366 void *kn_hook;
91447636
A
367 int kn_hookid;
368 int64_t kn_sdata; /* saved data field */
369
55e303ae
A
370#define KN_ACTIVE 0x01 /* event has been triggered */
371#define KN_QUEUED 0x02 /* event is on queue */
372#define KN_DISABLED 0x04 /* event is disabled */
91447636
A
373#define KN_DROPPING 0x08 /* knote is being dropped */
374#define KN_USEWAIT 0x10 /* wait for knote use */
b0d623f7
A
375#define KN_ATTACHING 0x20 /* event is pending attach */
376#define KN_STAYQUEUED 0x40 /* force event to stay on queue */
55e303ae
A
377
378#define kn_id kn_kevent.ident
379#define kn_filter kn_kevent.filter
380#define kn_flags kn_kevent.flags
381#define kn_fflags kn_kevent.fflags
382#define kn_data kn_kevent.data
b0d623f7
A
383#define kn_udata kn_kevent.udata
384#define kn_ext kn_kevent.ext
55e303ae
A
385#define kn_fp kn_ptr.p_fp
386};
387
b0d623f7
A
388/* Hint values for f_touch filter operation */
389#define EVENT_REGISTER 1
390#define EVENT_PROCESS 2
391
55e303ae
A
392struct filterops {
393 int f_isfd; /* true if ident == filedescriptor */
91447636
A
394 int (*f_attach)(struct knote *kn);
395 void (*f_detach)(struct knote *kn);
396 int (*f_event)(struct knote *kn, long hint);
b0d623f7
A
397 /* Optional f_touch operation, called only if !f_isfd && non-NULL */
398 void (*f_touch)(struct knote *kn, struct kevent64_s *kev, long type);
399 /* Optional f_peek operation, called only if KN_STAYQUEUED is set */
6d2010ae 400 unsigned (*f_peek)(struct knote *kn);
55e303ae
A
401};
402
403struct proc;
b0d623f7 404struct wait_queue;
55e303ae
A
405
406SLIST_HEAD(klist, knote);
2d21ac55 407extern void knote_init(void) __attribute__((section("__TEXT, initcode")));
55e303ae
A
408extern void klist_init(struct klist *list);
409
410#define KNOTE(list, hint) knote(list, hint)
411#define KNOTE_ATTACH(list, kn) knote_attach(list, kn)
412#define KNOTE_DETACH(list, kn) knote_detach(list, kn)
413
414
415extern void knote(struct klist *list, long hint);
416extern int knote_attach(struct klist *list, struct knote *kn);
417extern int knote_detach(struct klist *list, struct knote *kn);
316670eb
A
418extern int knote_link_wait_queue(struct knote *kn, struct wait_queue *wq, wait_queue_link_t wql);
419extern int knote_unlink_wait_queue(struct knote *kn, struct wait_queue *wq, wait_queue_link_t *wqlp);
55e303ae 420extern void knote_fdclose(struct proc *p, int fd);
6d2010ae 421extern void knote_markstayqueued(struct knote *kn);
55e303ae 422
91447636
A
423#endif /* !KERNEL_PRIVATE */
424
425#else /* KERNEL */
55e303ae 426
55e303ae 427
55e303ae
A
428struct timespec;
429
430__BEGIN_DECLS
91447636
A
431int kqueue(void);
432int kevent(int kq, const struct kevent *changelist, int nchanges,
55e303ae 433 struct kevent *eventlist, int nevents,
91447636 434 const struct timespec *timeout);
b0d623f7
A
435int kevent64(int kq, const struct kevent64_s *changelist,
436 int nchanges, struct kevent64_s *eventlist,
437 int nevents, unsigned int flags,
438 const struct timespec *timeout);
55e303ae
A
439__END_DECLS
440
55e303ae 441
91447636 442#endif /* KERNEL */
55e303ae 443
55e303ae
A
444
445#endif /* !_SYS_EVENT_H_ */