1/*
2 * Copyright (c) 2003-2012 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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.
14 *
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
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
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
59#include <machine/types.h>
60#include <sys/cdefs.h>
61#include <stdint.h>
62
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 */
70#define EVFILT_MACHPORT         (-8)	/* Mach portsets */
71#define EVFILT_FS		(-9)	/* Filesystem events */
72#define EVFILT_USER             (-10)   /* User events */
73					/* (-11) unused */
74#define EVFILT_VM		(-12)	/* Virtual memory events */
75
76#ifdef PRIVATE
77#define EVFILT_SOCK		(-13)	/* Socket events */
78#define EVFILT_MEMORYSTATUS	(-14)	/* Memorystatus events */
79#endif /* PRIVATE */
80
81#define EVFILT_SYSCOUNT		14
82#define EVFILT_THREADMARKER	EVFILT_SYSCOUNT /* Internal use only */
83
84#pragma pack(4)
85
86struct kevent {
87	uintptr_t	ident;		/* identifier for this event */
88	int16_t		filter;		/* filter for event */
89	uint16_t	flags;		/* general flags */
90	uint32_t	fflags;		/* filter-specific flags */
91	intptr_t	data;		/* filter-specific data */
92	void		*udata;		/* opaque user data identifier */
93};
94
95#ifdef KERNEL_PRIVATE
96
97struct user64_kevent {
98	uint64_t	ident;		/* identifier for this event */
99	int16_t		filter;		/* filter for event */
100	uint16_t	flags;		/* general flags */
101	uint32_t	fflags;		/* filter-specific flags */
102	int64_t		data;		/* filter-specific data */
103	user_addr_t	udata;		/* opaque user data identifier */
104};
105
106struct user32_kevent {
107	uint32_t	ident;		/* identifier for this event */
108	int16_t		filter;		/* filter for event */
109	uint16_t	flags;		/* general flags */
110	uint32_t	fflags;		/* filter-specific flags */
111	int32_t		data;		/* filter-specific data */
112	user32_addr_t	udata;		/* opaque user data identifier */
113};
114
115#endif
116
117#pragma pack()
118
119struct kevent64_s {
120	uint64_t	ident;		/* identifier for this event */
121	int16_t		filter;		/* filter for event */
122	uint16_t	flags;		/* general flags */
123	uint32_t	fflags;		/* filter-specific flags */
124	int64_t		data;		/* filter-specific data */
125	uint64_t	udata;		/* opaque user data identifier */
126	uint64_t	ext[2];		/* filter-specific extensions */
127};
128
129#define EV_SET(kevp, a, b, c, d, e, f) do {	\
130	struct kevent *__kevp__ = (kevp);	\
131	__kevp__->ident = (a);			\
132	__kevp__->filter = (b);			\
133	__kevp__->flags = (c);			\
134	__kevp__->fflags = (d);			\
135	__kevp__->data = (e);			\
136	__kevp__->udata = (f);			\
137} while(0)
138
139#define EV_SET64(kevp, a, b, c, d, e, f, g, h) do {	\
140	struct kevent64_s *__kevp__ = (kevp);		\
141	__kevp__->ident = (a);				\
142	__kevp__->filter = (b);				\
143	__kevp__->flags = (c);				\
144	__kevp__->fflags = (d);				\
145	__kevp__->data = (e);				\
146	__kevp__->udata = (f);				\
147	__kevp__->ext[0] = (g);				\
148	__kevp__->ext[1] = (h);				\
149} while(0)
150
151/* actions */
152#define EV_ADD		0x0001		/* add event to kq (implies enable) */
153#define EV_DELETE	0x0002		/* delete event from kq */
154#define EV_ENABLE	0x0004		/* enable event */
155#define EV_DISABLE	0x0008		/* disable event (not reported) */
156#define EV_RECEIPT	0x0040		/* force EV_ERROR on success, data == 0 */
157
158/* flags */
159#define EV_ONESHOT	0x0010		/* only report one occurrence */
160#define EV_CLEAR	0x0020		/* clear event state after reporting */
161#define EV_DISPATCH     0x0080          /* disable event after reporting */
162
163#define EV_SYSFLAGS	0xF000		/* reserved by system */
164#define EV_FLAG0	0x1000		/* filter-specific flag */
165#define EV_FLAG1	0x2000		/* filter-specific flag */
166
167/* returned values */
168#define EV_EOF		0x8000		/* EOF detected */
169#define EV_ERROR	0x4000		/* error, data contains errno */
170
171/*
172 * Filter specific flags for EVFILT_READ
173 *
174 * The default behavior for EVFILT_READ is to make the "read" determination
175 * relative to the current file descriptor read pointer. The EV_POLL
176 * flag indicates the determination should be made via poll(2) semantics
177 * (which always returns true for regular files - regardless of the amount
178 * of unread data in the file).
179 *
180 * On input, EV_OOBAND specifies that only OOB data should be looked for.
181 * The returned data count is the number of bytes beyond the current OOB marker.
182 *
183 * On output, EV_OOBAND indicates that OOB data is present
184 * If it was not specified as an input parameter, then the data count is the
185 * number of bytes before the current OOB marker. If at the marker, the
186 * data count indicates the number of bytes available after it.  In either
187 * case, it's the amount of data one could expect to receive next.
188 */
189#define EV_POLL	EV_FLAG0
190#define EV_OOBAND	EV_FLAG1
191
192/*
193 * data/hint fflags for EVFILT_USER, shared with userspace
194 */
195
196/*
197 * On input, NOTE_TRIGGER causes the event to be triggered for output.
198 */
199#define NOTE_TRIGGER	0x01000000
200
201/*
202 * On input, the top two bits of fflags specifies how the lower twenty four
203 * bits should be applied to the stored value of fflags.
204 *
205 * On output, the top two bits will always be set to NOTE_FFNOP and the
206 * remaining twenty four bits will contain the stored fflags value.
207 */
208#define NOTE_FFNOP      0x00000000              /* ignore input fflags */
209#define NOTE_FFAND      0x40000000              /* and fflags */
210#define NOTE_FFOR       0x80000000              /* or fflags */
211#define NOTE_FFCOPY     0xc0000000              /* copy fflags */
212#define NOTE_FFCTRLMASK 0xc0000000              /* mask for operations */
213#define NOTE_FFLAGSMASK	0x00ffffff
214
215/*
216 * data/hint fflags for EVFILT_{READ|WRITE}, shared with userspace
217 *
218 * The default behavior for EVFILT_READ is to make the determination
219 * realtive to the current file descriptor read pointer.
220 */
221#define NOTE_LOWAT	0x00000001		/* low water mark */
222/*
223 * data/hint fflags for EVFILT_VNODE, shared with userspace
224 */
225#define	NOTE_DELETE	0x00000001		/* vnode was removed */
226#define	NOTE_WRITE	0x00000002		/* data contents changed */
227#define	NOTE_EXTEND	0x00000004		/* size increased */
228#define	NOTE_ATTRIB	0x00000008		/* attributes changed */
229#define	NOTE_LINK	0x00000010		/* link count changed */
230#define	NOTE_RENAME	0x00000020		/* vnode was renamed */
231#define	NOTE_REVOKE	0x00000040		/* vnode access was revoked */
232#define NOTE_NONE	0x00000080		/* No specific vnode event: to test for EVFILT_READ activation*/
233
234/*
235 * data/hint fflags for EVFILT_PROC, shared with userspace
236 *
237 * Please note that EVFILT_PROC and EVFILT_SIGNAL share the same knote list
238 * that hangs off the proc structure. They also both play games with the hint
239 * passed to KNOTE(). If NOTE_SIGNAL is passed as a hint, then the lower bits
240 * of the hint contain the signal. IF NOTE_FORK is passed, then the lower bits
241 * contain the PID of the child (but the pid does not get passed through in
242 * the actual kevent).
243 */
244enum {
245	eNoteReapDeprecated __deprecated_enum_msg("This kqueue(2) EVFILT_PROC flag is deprecated") = 0x10000000
246};
247
248#define	NOTE_EXIT		0x80000000	/* process exited */
249#define	NOTE_FORK		0x40000000	/* process forked */
250#define	NOTE_EXEC		0x20000000	/* process exec'd */
251#define	NOTE_REAP		((unsigned int)eNoteReapDeprecated /* 0x10000000 */)	/* process reaped */
252#define	NOTE_SIGNAL		0x08000000	/* shared with EVFILT_SIGNAL */
253#define	NOTE_EXITSTATUS		0x04000000	/* exit status to be returned, valid for child process only */
254#define	NOTE_EXIT_DETAIL	0x02000000	/* provide details on reasons for exit */
255
256
257#define	NOTE_PDATAMASK	0x000fffff		/* mask for signal & exit status */
258#define	NOTE_PCTRLMASK	(~NOTE_PDATAMASK)
259
260/*
261 * If NOTE_EXITSTATUS is present, provide additional info about exiting process.
262 */
263enum {
264	eNoteExitReparentedDeprecated __deprecated_enum_msg("This kqueue(2) EVFILT_PROC flag is no longer sent") = 0x00080000
265};
266#define NOTE_EXIT_REPARENTED	((unsigned int)eNoteExitReparentedDeprecated)	/* exited while reparented */
267
268/*
269 * If NOTE_EXIT_DETAIL is present, these bits indicate specific reasons for exiting.
270 */
271#define NOTE_EXIT_DETAIL_MASK		0x00070000
272#define	NOTE_EXIT_DECRYPTFAIL		0x00010000
273#define	NOTE_EXIT_MEMORY		0x00020000
274#define NOTE_EXIT_CSERROR		0x00040000
275
276#ifdef PRIVATE
277
278/*
279 * If NOTE_EXIT_MEMORY is present, these bits indicate specific jetsam condition.
280 */
281#define NOTE_EXIT_MEMORY_DETAIL_MASK	0xfc000000
282#define NOTE_EXIT_MEMORY_VMPAGESHORTAGE	0x80000000	/* jetsam condition: lowest jetsam priority proc killed due to vm page shortage */
283#define NOTE_EXIT_MEMORY_VMTHRASHING	0x40000000	/* jetsam condition: lowest jetsam priority proc killed due to vm thrashing */
284#define NOTE_EXIT_MEMORY_HIWAT		0x20000000	/* jetsam condition: process reached its high water mark */
285#define NOTE_EXIT_MEMORY_PID		0x10000000	/* jetsam condition: special pid kill requested */
286#define NOTE_EXIT_MEMORY_IDLE		0x08000000	/* jetsam condition: idle process cleaned up */
287#define NOTE_EXIT_MEMORY_VNODE		0X04000000	/* jetsam condition: virtual node kill */
288
289#endif
290
291/*
292 * data/hint fflags for EVFILT_VM, shared with userspace.
293 */
294#define NOTE_VM_PRESSURE			0x80000000              /* will react on memory pressure */
295#define NOTE_VM_PRESSURE_TERMINATE		0x40000000              /* will quit on memory pressure, possibly after cleaning up dirty state */
296#define NOTE_VM_PRESSURE_SUDDEN_TERMINATE	0x20000000		/* will quit immediately on memory pressure */
297#define NOTE_VM_ERROR				0x10000000              /* there was an error */
298
299#ifdef PRIVATE
300
301/*
302 * data/hint fflags for EVFILT_MEMORYSTATUS, shared with userspace.
303 */
304#define NOTE_MEMORYSTATUS_PRESSURE_NORMAL	0x00000001	/* system memory pressure has returned to normal */
305#define NOTE_MEMORYSTATUS_PRESSURE_WARN		0x00000002	/* system memory pressure has changed to the warning state */
306#define NOTE_MEMORYSTATUS_PRESSURE_CRITICAL	0x00000004	/* system memory pressure has changed to the critical state */
307
308typedef enum vm_pressure_level {
309        kVMPressureNormal   = 0,
310        kVMPressureWarning  = 1,
311        kVMPressureUrgent   = 2,
312        kVMPressureCritical = 3,
313} vm_pressure_level_t;
314
315#endif
316
317/*
318 * data/hint fflags for EVFILT_TIMER, shared with userspace.
319 * The default is a (repeating) interval timer with the data
320 * specifying the timeout interval in milliseconds.
321 *
322 * All timeouts are implicitly EV_CLEAR events.
323 */
324#define NOTE_SECONDS	0x00000001		/* data is seconds         */
325#define NOTE_USECONDS	0x00000002		/* data is microseconds    */
326#define NOTE_NSECONDS	0x00000004		/* data is nanoseconds     */
327#define NOTE_ABSOLUTE	0x00000008		/* absolute timeout        */
328						/* ... implicit EV_ONESHOT */
329#define NOTE_LEEWAY	0x00000010		/* ext[1] holds leeway for power aware timers */
330#define NOTE_CRITICAL	0x00000020		/* system does minimal timer coalescing */
331#define NOTE_BACKGROUND	0x00000040		/* system does maximum timer coalescing */
332#ifdef PRIVATE
333/*
334 * data/hint fflags for EVFILT_SOCK, shared with userspace.
335 *
336 */
337#define	NOTE_CONNRESET		0x00000001 /* Received RST */
338#define	NOTE_READCLOSED		0x00000002 /* Read side is shutdown */
339#define	NOTE_WRITECLOSED	0x00000004 /* Write side is shutdown */
340#define	NOTE_TIMEOUT		0x00000008 /* timeout: rexmt, keep-alive or persist */
341#define	NOTE_NOSRCADDR		0x00000010 /* source address not available */
342#define	NOTE_IFDENIED		0x00000020 /* interface denied connection */
343#define	NOTE_SUSPEND		0x00000040 /* output queue suspended */
344#define	NOTE_RESUME		0x00000080 /* output queue resumed */
345#define NOTE_KEEPALIVE		0x00000100 /* TCP Keepalive received */
346#define NOTE_ADAPTIVE_WTIMO	0x00000200 /* TCP adaptive write timeout */
347#define NOTE_ADAPTIVE_RTIMO	0x00000400 /* TCP adaptive read timeout */
348#define	NOTE_CONNECTED		0x00000800 /* socket is connected */
349#define	NOTE_DISCONNECTED	0x00001000 /* socket is disconnected */
350#define	NOTE_CONNINFO_UPDATED	0x00002000 /* connection info was updated */
351
352#endif /* PRIVATE */
353
354/*
355 * data/hint fflags for EVFILT_MACHPORT, shared with userspace.
356 *
357 * Only portsets are supported at this time.
358 *
359 * The fflags field can optionally contain the MACH_RCV_MSG, MACH_RCV_LARGE,
360 * and related trailer receive options as defined in <mach/message.h>.
361 * The presence of these flags directs the kevent64() call to attempt to receive
362 * the message during kevent delivery, rather than just indicate that a message exists.
363 * On setup, The ext[0] field contains the receive buffer pointer and ext[1] contains
364 * the receive buffer length.  Upon event delivery, the actual received message size
365 * is returned in ext[1].  As with mach_msg(), the buffer must be large enough to
366 * receive the message and the requested (or default) message trailers.  In addition,
367 * the fflags field contains the return code normally returned by mach_msg().
368 *
369 * If no message receipt options were provided in the fflags field on setup, no
370 * message is received by this call. Instead, on output, the data field simply
371 * contains the name of the actual port detected with a message waiting.
372 */
373
374/*
375 * DEPRECATED!!!!!!!!!
376 * NOTE_TRACK, NOTE_TRACKERR, and NOTE_CHILD are no longer supported as of 10.5
377 */
378/* additional flags for EVFILT_PROC */
379#define	NOTE_TRACK	0x00000001		/* follow across forks */
380#define	NOTE_TRACKERR	0x00000002		/* could not track child */
381#define	NOTE_CHILD	0x00000004		/* am a child process */
382
383
384
385#ifndef KERNEL
386/* Temporay solution for BootX to use inode.h till kqueue moves to vfs layer */
387#include <sys/queue.h>
388struct knote;
389SLIST_HEAD(klist, knote);
390#endif
391
392#ifdef KERNEL
393
394#ifdef KERNEL_PRIVATE
395#include <sys/queue.h>
396#include <kern/kern_types.h>
397
398#ifdef MALLOC_DECLARE
399MALLOC_DECLARE(M_KQUEUE);
400#endif
401
402TAILQ_HEAD(kqtailq, knote);	/* a list of "queued" events */
403
404struct knote {
405	int		kn_inuse;	/* inuse count */
406	struct kqtailq	*kn_tq;		/* pointer to tail queue */
407	TAILQ_ENTRY(knote)	kn_tqe;		/* linkage for tail queue */
408	struct kqueue	*kn_kq;	/* which kqueue we are on */
409	SLIST_ENTRY(knote)	kn_link;	/* linkage for search list */
410	SLIST_ENTRY(knote)	kn_selnext;	/* klist element chain */
411	union {
412		struct		fileproc *p_fp;	/* file data pointer */
413		struct		proc *p_proc;	/* proc pointer */
414		struct          ipc_pset *p_pset;       /* pset pointer */
415	} kn_ptr;
416	struct			filterops *kn_fop;
417	int			kn_status;	/* status bits */
418	int			kn_sfflags;	/* saved filter flags */
419	struct 			kevent64_s kn_kevent;
420	void			*kn_hook;
421	int			kn_hookid;
422	int64_t			kn_sdata;	/* saved data field */
423
424#define KN_ACTIVE	0x01			/* event has been triggered */
425#define KN_QUEUED	0x02			/* event is on queue */
426#define KN_DISABLED	0x04			/* event is disabled */
427#define KN_DROPPING	0x08			/* knote is being dropped */
428#define KN_USEWAIT	0x10			/* wait for knote use */
429#define KN_ATTACHING	0x20			/* event is pending attach */
430#define KN_STAYQUEUED	0x40			/* force event to stay on queue */
431
432#define kn_id		kn_kevent.ident
433#define kn_filter	kn_kevent.filter
434#define kn_flags	kn_kevent.flags
435#define kn_fflags	kn_kevent.fflags
436#define kn_data		kn_kevent.data
437#define kn_udata	kn_kevent.udata
438#define kn_ext		kn_kevent.ext
439#define kn_fp		kn_ptr.p_fp
440};
441
442/* Hint values for f_touch filter operation */
443#define EVENT_REGISTER  1
444#define EVENT_PROCESS   2
445
446struct filterops {
447	int	f_isfd;		/* true if ident == filedescriptor */
448	int	(*f_attach)(struct knote *kn);
449	void	(*f_detach)(struct knote *kn);
450	int	(*f_event)(struct knote *kn, long hint);
451	/* Optional f_touch operation, called only if !f_isfd && non-NULL */
452	void    (*f_touch)(struct knote *kn, struct kevent64_s *kev, long type);
453	/* Optional f_peek operation, called only if KN_STAYQUEUED is set */
454	unsigned (*f_peek)(struct knote *kn);
455};
456
457struct proc;
458struct wait_queue;
459
460SLIST_HEAD(klist, knote);
461extern void	knote_init(void);
462extern void	klist_init(struct klist *list);
463
464#define KNOTE(list, hint)	knote(list, hint)
465#define KNOTE_ATTACH(list, kn)	knote_attach(list, kn)
466#define KNOTE_DETACH(list, kn)	knote_detach(list, kn)
467
468
469extern void	knote(struct klist *list, long hint);
470extern int	knote_attach(struct klist *list, struct knote *kn);
471extern int	knote_detach(struct klist *list, struct knote *kn);
472extern int	knote_link_wait_queue(struct knote *kn, struct wait_queue *wq, wait_queue_link_t wql);
473extern int	knote_unlink_wait_queue(struct knote *kn, struct wait_queue *wq, wait_queue_link_t *wqlp);
474extern void	knote_fdclose(struct proc *p, int fd);
475extern void	knote_markstayqueued(struct knote *kn);
476
477#endif /* !KERNEL_PRIVATE */
478
479#else 	/* KERNEL */
480
481
482struct timespec;
483
484__BEGIN_DECLS
485int     kqueue(void);
486int     kevent(int kq, const struct kevent *changelist, int nchanges,
487		    struct kevent *eventlist, int nevents,
488		    const struct timespec *timeout);
489int     kevent64(int kq, const struct kevent64_s *changelist,
490		    int nchanges, struct kevent64_s *eventlist,
491		    int nevents, unsigned int flags,
492		    const struct timespec *timeout);
493__END_DECLS
494
495
496#endif /* KERNEL */
497
498
499#endif /* !_SYS_EVENT_H_ */
500