1 /*
2  * Copyright (c) 2003-2019 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 /*
64  * Filter types
65  */
66 #define EVFILT_READ             (-1)
67 #define EVFILT_WRITE            (-2)
68 #define EVFILT_AIO              (-3)    /* attached to aio requests */
69 #define EVFILT_VNODE            (-4)    /* attached to vnodes */
70 #define EVFILT_PROC             (-5)    /* attached to struct proc */
71 #define EVFILT_SIGNAL           (-6)    /* attached to struct proc */
72 #define EVFILT_TIMER            (-7)    /* timers */
73 #define EVFILT_MACHPORT         (-8)    /* Mach portsets */
74 #define EVFILT_FS               (-9)    /* Filesystem events */
75 #define EVFILT_USER             (-10)   /* User events */
76 #define EVFILT_VM               (-12)   /* Virtual memory events */
77 #define EVFILT_EXCEPT           (-15)   /* Exception events */
78 
79 #define EVFILT_SYSCOUNT         17
80 #define EVFILT_THREADMARKER     EVFILT_SYSCOUNT /* Internal use only */
81 
82 #pragma pack(4)
83 
84 struct kevent {
85 	uintptr_t       ident;  /* identifier for this event */
86 	int16_t         filter; /* filter for event */
87 	uint16_t        flags;  /* general flags */
88 	uint32_t        fflags; /* filter-specific flags */
89 	intptr_t        data;   /* filter-specific data */
90 	void            *udata; /* opaque user data identifier */
91 };
92 
93 
94 #pragma pack()
95 
96 struct kevent64_s {
97 	uint64_t        ident;          /* identifier for this event */
98 	int16_t         filter;         /* filter for event */
99 	uint16_t        flags;          /* general flags */
100 	uint32_t        fflags;         /* filter-specific flags */
101 	int64_t         data;           /* filter-specific data */
102 	uint64_t        udata;          /* opaque user data identifier */
103 	uint64_t        ext[2];         /* filter-specific extensions */
104 };
105 
106 
107 #define EV_SET(kevp, a, b, c, d, e, f) do {     \
108 	struct kevent *__kevp__ = (kevp);       \
109 	__kevp__->ident = (a);                  \
110 	__kevp__->filter = (b);                 \
111 	__kevp__->flags = (c);                  \
112 	__kevp__->fflags = (d);                 \
113 	__kevp__->data = (e);                   \
114 	__kevp__->udata = (f);                  \
115 } while(0)
116 
117 #define EV_SET64(kevp, a, b, c, d, e, f, g, h) do {     \
118 	struct kevent64_s *__kevp__ = (kevp);           \
119 	__kevp__->ident = (a);                          \
120 	__kevp__->filter = (b);                         \
121 	__kevp__->flags = (c);                          \
122 	__kevp__->fflags = (d);                         \
123 	__kevp__->data = (e);                           \
124 	__kevp__->udata = (f);                          \
125 	__kevp__->ext[0] = (g);                         \
126 	__kevp__->ext[1] = (h);                         \
127 } while(0)
128 
129 
130 /* kevent system call flags */
131 #define KEVENT_FLAG_NONE                         0x000000       /* no flag value */
132 #define KEVENT_FLAG_IMMEDIATE                    0x000001       /* immediate timeout */
133 #define KEVENT_FLAG_ERROR_EVENTS                 0x000002       /* output events only include change errors */
134 
135 
136 /* actions */
137 #define EV_ADD              0x0001      /* add event to kq (implies enable) */
138 #define EV_DELETE           0x0002      /* delete event from kq */
139 #define EV_ENABLE           0x0004      /* enable event */
140 #define EV_DISABLE          0x0008      /* disable event (not reported) */
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 immediate event output */
146                                         /* ... with or without EV_ERROR */
147                                         /* ... use KEVENT_FLAG_ERROR_EVENTS */
148                                         /*     on syscalls supporting flags */
149 
150 #define EV_DISPATCH         0x0080      /* disable event after reporting */
151 #define EV_UDATA_SPECIFIC   0x0100      /* unique kevent per udata value */
152 
153 #define EV_DISPATCH2        (EV_DISPATCH | EV_UDATA_SPECIFIC)
154 /* ... in combination with EV_DELETE */
155 /* will defer delete until udata-specific */
156 /* event enabled. EINPROGRESS will be */
157 /* returned to indicate the deferral */
158 
159 #define EV_VANISHED         0x0200      /* report that source has vanished  */
160                                         /* ... only valid with EV_DISPATCH2 */
161 
162 #define EV_SYSFLAGS         0xF000      /* reserved by system */
163 #define EV_FLAG0            0x1000      /* filter-specific flag */
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 /*
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.
175  *
176  * The EV_POLL flag indicates the determination should be made via poll(2)
177  * semantics. These semantics dictate always returning true for regular files,
178  * regardless of the amount of unread data in the file.
179  *
180  * On input, EV_OOBAND specifies that filter should actively return in the
181  * presence of OOB on the descriptor. It implies that filter will return
182  * if there is OOB data available to read OR when any other condition
183  * for the read are met (for example number of bytes regular data becomes >=
184  * low-watermark).
185  * If EV_OOBAND is not set on input, it implies that the filter should not actively
186  * return for out of band data on the descriptor. The filter will then only return
187  * when some other condition for read is met (ex: when number of regular data bytes
188  * >=low-watermark OR when socket can't receive more data (SS_CANTRCVMORE)).
189  *
190  * On output, EV_OOBAND indicates the presence of OOB data on the descriptor.
191  * If it was not specified as an input parameter, then the data count is the
192  * number of bytes before the current OOB marker, else data count is the number
193  * of bytes beyond OOB marker.
194  */
195 #define EV_POLL         EV_FLAG0
196 #define EV_OOBAND       EV_FLAG1
197 
198 /*
199  * data/hint fflags for EVFILT_USER, shared with userspace
200  */
201 
202 /*
203  * On input, NOTE_TRIGGER causes the event to be triggered for output.
204  */
205 #define NOTE_TRIGGER    0x01000000
206 
207 /*
208  * On input, the top two bits of fflags specifies how the lower twenty four
209  * bits should be applied to the stored value of fflags.
210  *
211  * On output, the top two bits will always be set to NOTE_FFNOP and the
212  * remaining twenty four bits will contain the stored fflags value.
213  */
214 #define NOTE_FFNOP      0x00000000              /* ignore input fflags */
215 #define NOTE_FFAND      0x40000000              /* and fflags */
216 #define NOTE_FFOR       0x80000000              /* or fflags */
217 #define NOTE_FFCOPY     0xc0000000              /* copy fflags */
218 #define NOTE_FFCTRLMASK 0xc0000000              /* mask for operations */
219 #define NOTE_FFLAGSMASK 0x00ffffff
220 
221 
222 /*
223  * data/hint fflags for EVFILT_{READ|WRITE}, shared with userspace
224  *
225  * The default behavior for EVFILT_READ is to make the determination
226  * realtive to the current file descriptor read pointer.
227  */
228 #define NOTE_LOWAT      0x00000001              /* low water mark */
229 
230 /* data/hint flags for EVFILT_EXCEPT, shared with userspace */
231 #define NOTE_OOB        0x00000002              /* OOB data */
232 
233 /*
234  * data/hint fflags for EVFILT_VNODE, shared with userspace
235  */
236 #define NOTE_DELETE     0x00000001              /* vnode was removed */
237 #define NOTE_WRITE      0x00000002              /* data contents changed */
238 #define NOTE_EXTEND     0x00000004              /* size increased */
239 #define NOTE_ATTRIB     0x00000008              /* attributes changed */
240 #define NOTE_LINK       0x00000010              /* link count changed */
241 #define NOTE_RENAME     0x00000020              /* vnode was renamed */
242 #define NOTE_REVOKE     0x00000040              /* vnode access was revoked */
243 #define NOTE_NONE       0x00000080              /* No specific vnode event: to test for EVFILT_READ activation*/
244 #define NOTE_FUNLOCK    0x00000100              /* vnode was unlocked by flock(2) */
245 
246 /*
247  * data/hint fflags for EVFILT_PROC, shared with userspace
248  *
249  * Please note that EVFILT_PROC and EVFILT_SIGNAL share the same knote list
250  * that hangs off the proc structure. They also both play games with the hint
251  * passed to KNOTE(). If NOTE_SIGNAL is passed as a hint, then the lower bits
252  * of the hint contain the signal. IF NOTE_FORK is passed, then the lower bits
253  * contain the PID of the child (but the pid does not get passed through in
254  * the actual kevent).
255  */
256 enum {
257 	eNoteReapDeprecated __deprecated_enum_msg("This kqueue(2) EVFILT_PROC flag is deprecated") = 0x10000000
258 };
259 
260 #define NOTE_EXIT               0x80000000      /* process exited */
261 #define NOTE_FORK               0x40000000      /* process forked */
262 #define NOTE_EXEC               0x20000000      /* process exec'd */
263 #define NOTE_REAP               ((unsigned int)eNoteReapDeprecated /* 0x10000000 */ )   /* process reaped */
264 #define NOTE_SIGNAL             0x08000000      /* shared with EVFILT_SIGNAL */
265 #define NOTE_EXITSTATUS         0x04000000      /* exit status to be returned, valid for child process or when allowed to signal target pid */
266 #define NOTE_EXIT_DETAIL        0x02000000      /* provide details on reasons for exit */
267 
268 #define NOTE_PDATAMASK  0x000fffff              /* mask for signal & exit status */
269 #define NOTE_PCTRLMASK  (~NOTE_PDATAMASK)
270 
271 /*
272  * If NOTE_EXITSTATUS is present, provide additional info about exiting process.
273  */
274 enum {
275 	eNoteExitReparentedDeprecated __deprecated_enum_msg("This kqueue(2) EVFILT_PROC flag is no longer sent") = 0x00080000
276 };
277 #define NOTE_EXIT_REPARENTED    ((unsigned int)eNoteExitReparentedDeprecated)   /* exited while reparented */
278 
279 /*
280  * If NOTE_EXIT_DETAIL is present, these bits indicate specific reasons for exiting.
281  */
282 #define NOTE_EXIT_DETAIL_MASK           0x00070000
283 #define NOTE_EXIT_DECRYPTFAIL           0x00010000
284 #define NOTE_EXIT_MEMORY                0x00020000
285 #define NOTE_EXIT_CSERROR               0x00040000
286 
287 
288 /*
289  * data/hint fflags for EVFILT_VM, shared with userspace.
290  */
291 #define NOTE_VM_PRESSURE                        0x80000000              /* will react on memory pressure */
292 #define NOTE_VM_PRESSURE_TERMINATE              0x40000000              /* will quit on memory pressure, possibly after cleaning up dirty state */
293 #define NOTE_VM_PRESSURE_SUDDEN_TERMINATE       0x20000000              /* will quit immediately on memory pressure */
294 #define NOTE_VM_ERROR                           0x10000000              /* there was an error */
295 
296 
297 /*
298  * data/hint fflags for EVFILT_TIMER, shared with userspace.
299  * The default is a (repeating) interval timer with the data
300  * specifying the timeout interval in milliseconds.
301  *
302  * All timeouts are implicitly EV_CLEAR events.
303  */
304 #define NOTE_SECONDS    0x00000001              /* data is seconds         */
305 #define NOTE_USECONDS   0x00000002              /* data is microseconds    */
306 #define NOTE_NSECONDS   0x00000004              /* data is nanoseconds     */
307 #define NOTE_ABSOLUTE   0x00000008              /* absolute timeout        */
308 /* ... implicit EV_ONESHOT, timeout uses the gettimeofday epoch */
309 #define NOTE_LEEWAY             0x00000010              /* ext[1] holds leeway for power aware timers */
310 #define NOTE_CRITICAL   0x00000020              /* system does minimal timer coalescing */
311 #define NOTE_BACKGROUND 0x00000040              /* system does maximum timer coalescing */
312 #define NOTE_MACH_CONTINUOUS_TIME       0x00000080
313 /*
314  * NOTE_MACH_CONTINUOUS_TIME:
315  * with NOTE_ABSOLUTE: causes the timer to continue to tick across sleep,
316  *      still uses gettimeofday epoch
317  * with NOTE_MACHTIME and NOTE_ABSOLUTE: uses mach continuous time epoch
318  * without NOTE_ABSOLUTE (interval timer mode): continues to tick across sleep
319  */
320 #define NOTE_MACHTIME   0x00000100              /* data is mach absolute time units */
321 /* timeout uses the mach absolute time epoch */
322 
323 
324 /*
325  * data/hint fflags for EVFILT_MACHPORT, shared with userspace.
326  *
327  * Only portsets are supported at this time.
328  *
329  * The fflags field can optionally contain the MACH_RCV_MSG, MACH_RCV_LARGE,
330  * and related trailer receive options as defined in <mach/message.h>.
331  * The presence of these flags directs the kevent64() call to attempt to receive
332  * the message during kevent delivery, rather than just indicate that a message exists.
333  * On setup, The ext[0] field contains the receive buffer pointer and ext[1] contains
334  * the receive buffer length.  Upon event delivery, the actual received message size
335  * is returned in ext[1].  As with mach_msg(), the buffer must be large enough to
336  * receive the message and the requested (or default) message trailers.  In addition,
337  * the fflags field contains the return code normally returned by mach_msg().
338  *
339  * If MACH_RCV_MSG is specified, and the ext[1] field specifies a zero length, the
340  * system call argument specifying an ouput area (kevent_qos) will be consulted. If
341  * the system call specified an output data area, the user-space address
342  * of the received message is carved from that provided output data area (if enough
343  * space remains there). The address and length of each received message is
344  * returned in the ext[0] and ext[1] fields (respectively) of the corresponding kevent.
345  *
346  * IF_MACH_RCV_VOUCHER_CONTENT is specified, the contents of the message voucher is
347  * extracted (as specified in the xflags field) and stored in ext[2] up to ext[3]
348  * length.  If the input length is zero, and the system call provided a data area,
349  * the space for the voucher content is carved from the provided space and its
350  * address and length is returned in ext[2] and ext[3] respectively.
351  *
352  * If no message receipt options were provided in the fflags field on setup, no
353  * message is received by this call. Instead, on output, the data field simply
354  * contains the name of the actual port detected with a message waiting.
355  */
356 
357 /*
358  * DEPRECATED!!!!!!!!!
359  * NOTE_TRACK, NOTE_TRACKERR, and NOTE_CHILD are no longer supported as of 10.5
360  */
361 /* additional flags for EVFILT_PROC */
362 #define NOTE_TRACK      0x00000001              /* follow across forks */
363 #define NOTE_TRACKERR   0x00000002              /* could not track child */
364 #define NOTE_CHILD      0x00000004              /* am a child process */
365 
366 
367 
368 /* Temporay solution for BootX to use inode.h till kqueue moves to vfs layer */
369 #include <sys/queue.h>
370 struct knote;
371 SLIST_HEAD(klist, knote);
372 
373 
374 #include <sys/types.h>
375 
376 struct timespec;
377 
378 __BEGIN_DECLS
379 int     kqueue(void);
380 int     kevent(int kq,
381     const struct kevent *changelist, int nchanges,
382     struct kevent *eventlist, int nevents,
383     const struct timespec *timeout);
384 int     kevent64(int kq,
385     const struct kevent64_s *changelist, int nchanges,
386     struct kevent64_s *eventlist, int nevents,
387     unsigned int flags,
388     const struct timespec *timeout);
389 
390 
391 __END_DECLS
392 
393 
394 
395 
396 #endif /* !_SYS_EVENT_H_ */