]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - tests/sys/kqueue/libkqueue/main.c
MFC r336761 & r336781:
[FreeBSD/FreeBSD.git] / tests / sys / kqueue / libkqueue / main.c
1 /*
2  * Copyright (c) 2009 Mark Heily <mark@heily.com>
3  *
4  * Permission to use, copy, modify, and distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  *
16  * $FreeBSD$
17  */
18
19 #include <sys/types.h>
20
21 #include "config.h"
22 #include "common.h"
23
24 int testnum = 1;
25 char *cur_test_id = NULL;
26 int kqfd;
27
28 extern void test_evfilt_read();
29 extern void test_evfilt_signal();
30 extern void test_evfilt_vnode();
31 extern void test_evfilt_timer();
32 extern void test_evfilt_proc();
33 #if HAVE_EVFILT_USER
34 extern void test_evfilt_user();
35 #endif
36
37 /* Checks if any events are pending, which is an error. */
38 void 
39 test_no_kevents(void)
40 {
41     int nfds;
42     struct timespec timeo;
43     struct kevent kev;
44
45     puts("confirming that there are no events pending");
46     memset(&timeo, 0, sizeof(timeo));
47     nfds = kevent(kqfd, NULL, 0, &kev, 1, &timeo);
48     if (nfds != 0) {
49         puts("\nUnexpected event:");
50         puts(kevent_to_str(&kev));
51         errx(1, "%d event(s) pending, but none expected:", nfds);
52     }
53 }
54
55 /* Checks if any events are pending, which is an error. Do not print
56  * out anything unless events are found.
57 */
58 void 
59 test_no_kevents_quietly(void)
60 {
61     int nfds;
62     struct timespec timeo;
63     struct kevent kev;
64
65     memset(&timeo, 0, sizeof(timeo));
66     nfds = kevent(kqfd, NULL, 0, &kev, 1, &timeo);
67     if (nfds != 0) {
68         puts("\nUnexpected event:");
69         puts(kevent_to_str(&kev));
70         errx(1, "%d event(s) pending, but none expected:", nfds);
71     }
72 }
73
74 /* Retrieve a single kevent */
75 struct kevent *
76 kevent_get(int kqfd)
77 {
78     int nfds;
79     struct kevent *kev;
80
81     if ((kev = calloc(1, sizeof(*kev))) == NULL)
82         err(1, "out of memory");
83     
84     nfds = kevent(kqfd, NULL, 0, kev, 1, NULL);
85     if (nfds < 1)
86         err(1, "kevent(2)");
87
88     return (kev);
89 }
90
91 /* Retrieve a single kevent, specifying a maximum time to wait for it. */
92 struct kevent *
93 kevent_get_timeout(int kqfd, int seconds)
94 {
95     int nfds;
96     struct kevent *kev;
97     struct timespec timeout = {seconds, 0};
98
99     if ((kev = calloc(1, sizeof(*kev))) == NULL)
100         err(1, "out of memory");
101     
102     nfds = kevent(kqfd, NULL, 0, kev, 1, &timeout);
103     if (nfds < 0) {
104         err(1, "kevent(2)");
105     } else if (nfds == 0) {
106         free(kev);
107         kev = NULL;
108     }
109
110     return (kev);
111 }
112
113 char *
114 kevent_fflags_dump(struct kevent *kev)
115 {
116     char *buf;
117
118 #define KEVFFL_DUMP(attrib) \
119     if (kev->fflags & attrib) \
120         strncat(buf, #attrib" ", 64);
121
122     if ((buf = calloc(1, 1024)) == NULL)
123         abort();
124
125     /* Not every filter has meaningful fflags */
126     if (kev->filter == EVFILT_PROC) {
127         snprintf(buf, 1024, "fflags = %x (", kev->fflags);
128         KEVFFL_DUMP(NOTE_EXIT);
129         KEVFFL_DUMP(NOTE_FORK);
130         KEVFFL_DUMP(NOTE_EXEC);
131         KEVFFL_DUMP(NOTE_CHILD);
132         KEVFFL_DUMP(NOTE_TRACKERR);
133         KEVFFL_DUMP(NOTE_TRACK);
134         buf[strlen(buf) - 1] = ')';
135     } else if (kev->filter == EVFILT_PROCDESC) {
136         snprintf(buf, 1024, "fflags = %x (", kev->fflags);
137         KEVFFL_DUMP(NOTE_EXIT);
138         KEVFFL_DUMP(NOTE_FORK);
139         KEVFFL_DUMP(NOTE_EXEC);
140         buf[strlen(buf) - 1] = ')';
141     } else if (kev->filter == EVFILT_VNODE) {
142         snprintf(buf, 1024, "fflags = %x (", kev->fflags);
143         KEVFFL_DUMP(NOTE_DELETE);
144         KEVFFL_DUMP(NOTE_WRITE);
145         KEVFFL_DUMP(NOTE_EXTEND);
146 #if HAVE_NOTE_TRUNCATE
147         KEVFFL_DUMP(NOTE_TRUNCATE);
148 #endif
149         KEVFFL_DUMP(NOTE_ATTRIB);
150         KEVFFL_DUMP(NOTE_LINK);
151         KEVFFL_DUMP(NOTE_RENAME);
152 #if HAVE_NOTE_REVOKE
153         KEVFFL_DUMP(NOTE_REVOKE);
154 #endif
155         buf[strlen(buf) - 1] = ')';
156     } else {
157         snprintf(buf, 1024, "fflags = %x", kev->fflags);
158     }
159
160     return (buf);
161 }
162
163 char *
164 kevent_flags_dump(struct kevent *kev)
165 {
166     char *buf;
167
168 #define KEVFL_DUMP(attrib) \
169     if (kev->flags & attrib) \
170         strncat(buf, #attrib" ", 64);
171
172     if ((buf = calloc(1, 1024)) == NULL)
173         abort();
174
175     snprintf(buf, 1024, "flags = %d (", kev->flags);
176     KEVFL_DUMP(EV_ADD);
177     KEVFL_DUMP(EV_ENABLE);
178     KEVFL_DUMP(EV_DISABLE);
179     KEVFL_DUMP(EV_DELETE);
180     KEVFL_DUMP(EV_ONESHOT);
181     KEVFL_DUMP(EV_CLEAR);
182     KEVFL_DUMP(EV_EOF);
183     KEVFL_DUMP(EV_ERROR);
184 #if HAVE_EV_DISPATCH
185     KEVFL_DUMP(EV_DISPATCH);
186 #endif
187 #if HAVE_EV_RECEIPT
188     KEVFL_DUMP(EV_RECEIPT);
189 #endif
190     buf[strlen(buf) - 1] = ')';
191
192     return (buf);
193 }
194
195 /* Copied from ../kevent.c kevent_dump() and improved */
196 const char *
197 kevent_to_str(struct kevent *kev)
198 {
199     char buf[512];
200
201     snprintf(&buf[0], sizeof(buf), 
202             "[ident=%d, filter=%d, %s, %s, data=%d, udata=%p]",
203             (u_int) kev->ident,
204             kev->filter,
205             kevent_flags_dump(kev),
206             kevent_fflags_dump(kev),
207             (int) kev->data,
208             kev->udata);
209
210     return (strdup(buf));
211 }
212
213 void
214 kevent_add(int kqfd, struct kevent *kev, 
215         uintptr_t ident,
216         short     filter,
217         u_short   flags,
218         u_int     fflags,
219         intptr_t  data,
220         void      *udata)
221 {
222     EV_SET(kev, ident, filter, flags, fflags, data, NULL);    
223     if (kevent(kqfd, kev, 1, NULL, 0, NULL) < 0) {
224         printf("Unable to add the following kevent:\n%s\n",
225                 kevent_to_str(kev));
226         err(1, "kevent(): %s", strerror(errno));
227     }
228 }
229
230 void
231 kevent_cmp(struct kevent *k1, struct kevent *k2)
232 {
233 /* XXX-
234    Workaround for inconsistent implementation of kevent(2) 
235  */
236 #ifdef __FreeBSD__
237     if (k1->flags & EV_ADD)
238         k2->flags |= EV_ADD;
239 #endif
240     if (memcmp(k1, k2, sizeof(*k1)) != 0) {
241         printf("kevent_cmp: mismatch:\n  %s !=\n  %s\n", 
242               kevent_to_str(k1), kevent_to_str(k2));
243         abort();
244     }
245 }
246
247 void
248 test_begin(const char *func)
249 {
250     if (cur_test_id)
251         free(cur_test_id);
252     cur_test_id = strdup(func);
253     if (!cur_test_id)
254         err(1, "strdup failed");
255
256     printf("\n\nTest %d: %s\n", testnum++, func);
257 }
258
259 void
260 success(void)
261 {
262     printf("%-70s %s\n", cur_test_id, "passed");
263     free(cur_test_id);
264     cur_test_id = NULL;
265 }
266
267 void
268 test_kqueue(void)
269 {
270     test_begin("kqueue()");
271     if ((kqfd = kqueue()) < 0)
272         err(1, "kqueue()");
273     test_no_kevents();
274     success();
275 }
276
277 void
278 test_kqueue_close(void)
279 {
280     test_begin("close(kq)");
281     if (close(kqfd) < 0)
282         err(1, "close()");
283     success();
284 }
285
286 int 
287 main(int argc, char **argv)
288 {
289     int test_proc = 1;
290     int test_socket = 1;
291     int test_signal = 1;
292     int test_vnode = 1;
293     int test_timer = 1;
294 #ifdef __FreeBSD__
295     int test_user = 1;
296 #else
297     /* XXX-FIXME temporary */
298     int test_user = 0;
299 #endif
300
301     while (argc) {
302         if (strcmp(argv[0], "--no-proc") == 0)
303             test_proc = 0;
304         if (strcmp(argv[0], "--no-socket") == 0)
305             test_socket = 0;
306         if (strcmp(argv[0], "--no-timer") == 0)
307             test_timer = 0;
308         if (strcmp(argv[0], "--no-signal") == 0)
309             test_signal = 0;
310         if (strcmp(argv[0], "--no-vnode") == 0)
311             test_vnode = 0;
312         if (strcmp(argv[0], "--no-user") == 0)
313             test_user = 0;
314         argv++;
315         argc--;
316     }
317
318     /*
319      * Some tests fork.  If output is fully buffered,
320      * the children inherit some buffered data and flush
321      * it when they exit, causing some data to be printed twice.
322      * Use line buffering to avoid this problem.
323      */
324     setlinebuf(stdout);
325     setlinebuf(stderr);
326
327     test_kqueue();
328     test_kqueue_close();
329
330     if (test_socket) 
331         test_evfilt_read();
332     if (test_signal) 
333         test_evfilt_signal();
334     if (test_vnode) 
335         test_evfilt_vnode();
336 #if HAVE_EVFILT_USER
337     if (test_user) 
338         test_evfilt_user();
339 #endif
340     if (test_timer) 
341         test_evfilt_timer();
342     if (test_proc) 
343         test_evfilt_proc();
344
345     printf("\n---\n"
346             "+OK All %d tests completed.\n", testnum - 1);
347     return (0);
348 }