xref: /f-stack/lib/ff_event.h (revision 77ab9738)
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 /* EVFILT_NETDEV        (-8)          no longer supported */
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_SYSCOUNT    11
51 
52 #define EV_SET(kevp_, a, b, c, d, e, f) do {    \
53     struct kevent *kevp = (kevp_);        \
54     (kevp)->ident = (a);            \
55     (kevp)->filter = (b);            \
56     (kevp)->flags = (c);            \
57     (kevp)->fflags = (d);            \
58     (kevp)->data = (e);            \
59     (kevp)->udata = (f);            \
60 } while(0)
61 
62 struct kevent {
63     uintptr_t ident;     /* identifier for this event */
64     short filter;        /* filter for event */
65     u_short flags;
66     u_int fflags;
67     intptr_t data;
68     void *udata;         /* opaque user data identifier */
69 };
70 
71 /* actions */
72 #define EV_ADD        0x0001        /* add event to kq (implies enable) */
73 #define EV_DELETE     0x0002        /* delete event from kq */
74 #define EV_ENABLE     0x0004        /* enable event */
75 #define EV_DISABLE    0x0008        /* disable event (not reported) */
76 
77 /* flags */
78 #define EV_ONESHOT    0x0010        /* only report one occurrence */
79 #define EV_CLEAR      0x0020        /* clear event state after reporting */
80 #define EV_RECEIPT    0x0040        /* force EV_ERROR on success, data=0 */
81 #define EV_DISPATCH   0x0080        /* disable event after reporting */
82 
83 #define EV_SYSFLAGS   0xF000        /* reserved by system */
84 #define EV_DROP       0x1000        /* note should be dropped */
85 #define EV_FLAG1      0x2000        /* filter-specific flag */
86 
87 /* returned values */
88 #define EV_EOF        0x8000        /* EOF detected */
89 #define EV_ERROR      0x4000        /* error, data contains errno */
90 
91  /*
92   * data/hint flags/masks for EVFILT_USER, shared with userspace
93   *
94   * On input, the top two bits of fflags specifies how the lower twenty four
95   * bits should be applied to the stored value of fflags.
96   *
97   * On output, the top two bits will always be set to NOTE_FFNOP and the
98   * remaining twenty four bits will contain the stored fflags value.
99   */
100 #define NOTE_FFNOP         0x00000000        /* ignore input fflags */
101 #define NOTE_FFAND         0x40000000        /* AND fflags */
102 #define NOTE_FFOR          0x80000000        /* OR fflags */
103 #define NOTE_FFCOPY        0xc0000000        /* copy fflags */
104 #define NOTE_FFCTRLMASK    0xc0000000        /* masks for operations */
105 #define NOTE_FFLAGSMASK    0x00ffffff
106 
107 #define NOTE_TRIGGER       0x01000000        /* Cause the event to be
108                            triggered for output. */
109 
110 /*
111  * data/hint flags for EVFILT_{READ|WRITE}, shared with userspace
112  */
113 #define NOTE_LOWAT        0x0001            /* low water mark */
114 
115 /*
116  * data/hint flags for EVFILT_VNODE, shared with userspace
117  */
118 #define NOTE_DELETE    0x0001            /* vnode was removed */
119 #define NOTE_WRITE     0x0002            /* data contents changed */
120 #define NOTE_EXTEND    0x0004            /* size increased */
121 #define NOTE_ATTRIB    0x0008            /* attributes changed */
122 #define NOTE_LINK      0x0010            /* link count changed */
123 #define NOTE_RENAME    0x0020            /* vnode was renamed */
124 #define NOTE_REVOKE    0x0040            /* vnode access was revoked */
125 
126 /*
127  * data/hint flags for EVFILT_PROC, shared with userspace
128  */
129 #define NOTE_EXIT         0x80000000        /* process exited */
130 #define NOTE_FORK         0x40000000        /* process forked */
131 #define NOTE_EXEC         0x20000000        /* process exec'd */
132 #define NOTE_PCTRLMASK    0xf0000000        /* mask for hint bits */
133 #define NOTE_PDATAMASK    0x000fffff        /* mask for pid */
134 
135 /* additional flags for EVFILT_PROC */
136 #define NOTE_TRACK        0x00000001        /* follow across forks */
137 #define NOTE_TRACKERR     0x00000002        /* could not track child */
138 #define NOTE_CHILD        0x00000004        /* am a child process */
139 
140 struct knote;
141 SLIST_HEAD(klist, knote);
142 struct kqueue;
143 TAILQ_HEAD(kqlist, kqueue);
144 struct knlist {
145     struct klist kl_list;
146     void (*kl_lock)(void *);    /* lock function */
147     void (*kl_unlock)(void *);
148     void (*kl_assert_locked)(void *);
149     void (*kl_assert_unlocked)(void *);
150     void *kl_lockarg;           /* argument passed to kl_lockf() */
151 };
152 
153 #endif
154 
155 #ifdef __cplusplus
156 }
157 #endif
158 #endif
159 
160