xref: /f-stack/lib/ff_event.h (revision e7dcc16e)
1 /*
2  * Copyright (C) 2017-2021 THL A29 Limited, a Tencent company.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice, this
9  *   list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright notice,
11  *   this list of conditions and the following disclaimer in the documentation
12  *   and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
18  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
23  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  *
25  */
26 
27 #ifndef _FSTACK_EVENT_H
28 #define _FSTACK_EVENT_H
29 
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33 
34 #ifndef _KERNEL
35 
36 #include <stdint.h>
37 #include <sys/queue.h>
38 
39 #define EVFILT_READ        (-1)
40 #define EVFILT_WRITE       (-2)
41 #define EVFILT_AIO         (-3)    /* attached to aio requests */
42 #define EVFILT_VNODE       (-4)    /* attached to vnodes */
43 #define EVFILT_PROC        (-5)    /* attached to struct proc */
44 #define EVFILT_SIGNAL      (-6)    /* attached to struct proc */
45 #define EVFILT_TIMER       (-7)    /* timers */
46 #define EVFILT_PROCDESC    (-8)    /* attached to process descriptors */
47 #define EVFILT_FS          (-9)    /* filesystem events */
48 #define EVFILT_LIO         (-10)    /* attached to lio requests */
49 #define EVFILT_USER        (-11)    /* User events */
50 #define EVFILT_SENDFILE    (-12)    /* attached to sendfile requests */
51 #define EVFILT_EMPTY       (-13)    /* empty send socket buf */
52 #define EVFILT_SYSCOUNT    13
53 
54 #if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
55 #define	EV_SET(kevp_, a, b, c, d, e, f) do {	\
56     *(kevp_) = (struct kevent){     \
57         .ident = (a),           \
58         .filter = (b),          \
59         .flags = (c),           \
60         .fflags = (d),          \
61         .data = (e),            \
62         .udata = (f),           \
63         .ext = {0},             \
64     };                  \
65 } while(0)
66 #else /* Pre-C99 or not STDC (e.g., C++) */
67 /* The definition of the local variable kevp could possibly conflict
68  * with a user-defined value passed in parameters a-f.
69  */
70 #define EV_SET(kevp_, a, b, c, d, e, f) do {	\
71     struct kevent *kevp = (kevp_);      \
72     (kevp)->ident = (a);            \
73     (kevp)->filter = (b);           \
74     (kevp)->flags = (c);            \
75     (kevp)->fflags = (d);           \
76     (kevp)->data = (e);         \
77     (kevp)->udata = (f);            \
78     (kevp)->ext[0] = 0;         \
79     (kevp)->ext[1] = 0;         \
80     (kevp)->ext[2] = 0;         \
81     (kevp)->ext[3] = 0;         \
82 } while(0)
83 #endif
84 
85 struct kevent {
86     uintptr_t ident;      /* identifier for this event */
87     short filter;           /* filter for event */
88     unsigned short flags;   /* action flags for kqueue */
89     unsigned int fflags;    /* filter flag value */
90     __int64_t data;         /* filter data value */
91     void *udata;            /* opaque user data identifier */
92     __uint64_t ext[4];      /* extensions */
93 };
94 
95 #if defined(_WANT_FREEBSD11_KEVENT)
96 /* Older structure used in FreeBSD 11.x and older. */
97 struct kevent_freebsd11 {
98     uintptr_t ident;      /* identifier for this event */
99     short filter;           /* filter for event */
100     unsigned short flags;
101     unsigned int fflags;
102     __intptr_t data;
103     void *udata;        /* opaque user data identifier */
104 };
105 #endif
106 
107 #if defined(_WANT_KEVENT32) || (defined(_KERNEL) && defined(__LP64__))
108 struct kevent32 {
109     uint32_t ident;     /* identifier for this event */
110     short filter;       /* filter for event */
111     u_short flags;
112     u_int fflags;
113 #ifndef __amd64__
114     uint32_t pad0;
115 #endif
116     uint32_t data1, data2;
117     uint32_t udata;     /* opaque user data identifier */
118 #ifndef __amd64__
119     uint32_t pad1;
120 #endif
121     uint32_t ext64[8];
122     };
123 
124 #ifdef _WANT_FREEBSD11_KEVENT
125     struct kevent32_freebsd11 {
126     u_int32_t ident;        /* identifier for this event */
127     short filter;           /* filter for event */
128     u_short flags;
129     u_int fflags;
130     int32_t data;
131     u_int32_t udata;        /* opaque user data identifier */
132     };
133 #endif
134 #endif
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 #define EV_FORCEONESHOT 0x0100      /* enable _ONESHOT and force trigger */
142 
143 /* flags */
144 #define EV_ONESHOT    0x0010        /* only report one occurrence */
145 #define EV_CLEAR      0x0020        /* clear event state after reporting */
146 #define EV_RECEIPT    0x0040        /* force EV_ERROR on success, data=0 */
147 #define EV_DISPATCH   0x0080        /* disable event after reporting */
148 
149 #define EV_SYSFLAGS   0xF000        /* reserved by system */
150 #define EV_DROP       0x1000        /* note should be dropped */
151 #define EV_FLAG1      0x2000        /* filter-specific flag */
152 #define EV_FLAG2      0x4000        /* filter-specific flag */
153 
154 /* returned values */
155 #define EV_EOF        0x8000        /* EOF detected */
156 #define EV_ERROR      0x4000        /* error, data contains errno */
157 
158  /*
159   * data/hint flags/masks for EVFILT_USER, shared with userspace
160   *
161   * On input, the top two bits of fflags specifies how the lower twenty four
162   * bits should be applied to the stored value of fflags.
163   *
164   * On output, the top two bits will always be set to NOTE_FFNOP and the
165   * remaining twenty four bits will contain the stored fflags value.
166   */
167 #define NOTE_FFNOP         0x00000000        /* ignore input fflags */
168 #define NOTE_FFAND         0x40000000        /* AND fflags */
169 #define NOTE_FFOR          0x80000000        /* OR fflags */
170 #define NOTE_FFCOPY        0xc0000000        /* copy fflags */
171 #define NOTE_FFCTRLMASK    0xc0000000        /* masks for operations */
172 #define NOTE_FFLAGSMASK    0x00ffffff
173 
174 #define NOTE_TRIGGER       0x01000000        /* Cause the event to be
175                            triggered for output. */
176 
177 /*
178  * data/hint flags for EVFILT_{READ|WRITE}, shared with userspace
179  */
180 #define NOTE_LOWAT        0x0001            /* low water mark */
181 #define NOTE_FILE_POLL    0x0002            /* behave like poll() */
182 
183 /*
184  * data/hint flags for EVFILT_VNODE, shared with userspace
185  */
186 #define NOTE_DELETE    0x0001            /* vnode was removed */
187 #define NOTE_WRITE     0x0002            /* data contents changed */
188 #define NOTE_EXTEND    0x0004            /* size increased */
189 #define NOTE_ATTRIB    0x0008            /* attributes changed */
190 #define NOTE_LINK      0x0010            /* link count changed */
191 #define NOTE_RENAME    0x0020            /* vnode was renamed */
192 #define NOTE_REVOKE    0x0040            /* vnode access was revoked */
193 #define	NOTE_OPEN      0x0080            /* vnode was opened */
194 #define	NOTE_CLOSE     0x0100            /* file closed, fd did not
195                            allowed write */
196 #define	NOTE_CLOSE_WRITE 0x0200          /* file closed, fd did allowed
197                            write */
198 #define	NOTE_READ      0x0400            /* file was read */
199 
200 /*
201  * data/hint flags for EVFILT_PROC and EVFILT_PROCDESC, shared with userspace
202  */
203 #define NOTE_EXIT         0x80000000        /* process exited */
204 #define NOTE_FORK         0x40000000        /* process forked */
205 #define NOTE_EXEC         0x20000000        /* process exec'd */
206 #define NOTE_PCTRLMASK    0xf0000000        /* mask for hint bits */
207 #define NOTE_PDATAMASK    0x000fffff        /* mask for pid */
208 
209 /* additional flags for EVFILT_PROC */
210 #define NOTE_TRACK        0x00000001        /* follow across forks */
211 #define NOTE_TRACKERR     0x00000002        /* could not track child */
212 #define NOTE_CHILD        0x00000004        /* am a child process */
213 
214 /* additional flags for EVFILT_TIMER */
215 #define NOTE_SECONDS		0x00000001	/* data is seconds */
216 #define NOTE_MSECONDS		0x00000002	/* data is milliseconds */
217 #define NOTE_USECONDS		0x00000004	/* data is microseconds */
218 #define NOTE_NSECONDS		0x00000008	/* data is nanoseconds */
219 #define	NOTE_ABSTIME		0x00000010	/* timeout is absolute */
220 
221 struct knote;
222 SLIST_HEAD(klist, knote);
223 struct kqueue;
224 TAILQ_HEAD(kqlist, kqueue);
225 struct knlist {
226     struct klist kl_list;
227     void (*kl_lock)(void *);    /* lock function */
228     void (*kl_unlock)(void *);
229     void (*kl_assert_lock)(void *, int);
230     void *kl_lockarg;           /* argument passed to lock functions */
231     int kl_autodestroy;
232 };
233 
234 #endif
235 
236 #ifdef __cplusplus
237 }
238 #endif
239 #endif
240 
241