2 * Copyright (c) 2009 Mark Heily <mark@heily.com>
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.
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.
17 #include <sys/types.h>
23 static char *cur_test_id = NULL;
24 static int testnum = 1;
26 /* Checks if any events are pending, which is an error. */
28 _test_no_kevents(const char *file, int line)
31 struct timespec timeo;
35 puts("confirming that there are no events pending");
36 memset(&timeo, 0, sizeof(timeo));
37 nfds = kevent(kqfd, NULL, 0, &kev, 1, &timeo);
39 printf("\n[%s:%d]: Unexpected event:", file, line);
40 kev_str = kevent_to_str(&kev);
43 errx(1, "%d event(s) pending, but none expected:", nfds);
47 /* Checks if any events are pending, which is an error. Do not print
48 * out anything unless events are found.
51 test_no_kevents_quietly(void)
54 struct timespec timeo;
58 memset(&timeo, 0, sizeof(timeo));
59 nfds = kevent(kqfd, NULL, 0, &kev, 1, &timeo);
61 puts("\nUnexpected event:");
62 kev_str = kevent_to_str(&kev);
65 errx(1, "%d event(s) pending, but none expected:", nfds);
69 /* Retrieve a single kevent */
76 if ((kev = calloc(1, sizeof(*kev))) == NULL)
77 err(1, "out of memory");
79 nfds = kevent(fd, NULL, 0, kev, 1, NULL);
86 /* Retrieve a single kevent, specifying a maximum time to wait for it. */
88 kevent_get_timeout(int fd, int seconds)
92 struct timespec timeout = {seconds, 0};
94 if ((kev = calloc(1, sizeof(*kev))) == NULL)
95 err(1, "out of memory");
97 nfds = kevent(fd, NULL, 0, kev, 1, &timeout);
100 } else if (nfds == 0) {
109 kevent_fflags_dump(struct kevent *kev)
113 #define KEVFFL_DUMP(attrib) \
114 if (kev->fflags & attrib) \
115 strncat(buf, #attrib" ", 64);
117 if ((buf = calloc(1, 1024)) == NULL)
120 /* Not every filter has meaningful fflags */
121 if (kev->filter == EVFILT_PROC) {
122 snprintf(buf, 1024, "fflags = %x (", kev->fflags);
123 KEVFFL_DUMP(NOTE_EXIT);
124 KEVFFL_DUMP(NOTE_FORK);
125 KEVFFL_DUMP(NOTE_EXEC);
126 KEVFFL_DUMP(NOTE_CHILD);
127 KEVFFL_DUMP(NOTE_TRACKERR);
128 KEVFFL_DUMP(NOTE_TRACK);
129 buf[strlen(buf) - 1] = ')';
130 } else if (kev->filter == EVFILT_PROCDESC) {
131 snprintf(buf, 1024, "fflags = %x (", kev->fflags);
132 KEVFFL_DUMP(NOTE_EXIT);
133 KEVFFL_DUMP(NOTE_FORK);
134 KEVFFL_DUMP(NOTE_EXEC);
135 buf[strlen(buf) - 1] = ')';
136 } else if (kev->filter == EVFILT_VNODE) {
137 snprintf(buf, 1024, "fflags = %x (", kev->fflags);
138 KEVFFL_DUMP(NOTE_DELETE);
139 KEVFFL_DUMP(NOTE_WRITE);
140 KEVFFL_DUMP(NOTE_EXTEND);
141 #if HAVE_NOTE_TRUNCATE
142 KEVFFL_DUMP(NOTE_TRUNCATE);
144 KEVFFL_DUMP(NOTE_ATTRIB);
145 KEVFFL_DUMP(NOTE_LINK);
146 KEVFFL_DUMP(NOTE_RENAME);
148 KEVFFL_DUMP(NOTE_REVOKE);
150 buf[strlen(buf) - 1] = ')';
152 snprintf(buf, 1024, "fflags = %x", kev->fflags);
159 kevent_flags_dump(struct kevent *kev)
163 #define KEVFL_DUMP(attrib) \
164 if (kev->flags & attrib) \
165 strncat(buf, #attrib" ", 64);
167 if ((buf = calloc(1, 1024)) == NULL)
170 snprintf(buf, 1024, "flags = %d (", kev->flags);
172 KEVFL_DUMP(EV_ENABLE);
173 KEVFL_DUMP(EV_DISABLE);
174 KEVFL_DUMP(EV_DELETE);
175 KEVFL_DUMP(EV_ONESHOT);
176 KEVFL_DUMP(EV_CLEAR);
178 KEVFL_DUMP(EV_ERROR);
180 KEVFL_DUMP(EV_DISPATCH);
183 KEVFL_DUMP(EV_RECEIPT);
185 buf[strlen(buf) - 1] = ')';
190 /* Copied from ../kevent.c kevent_dump() and improved */
192 kevent_to_str(struct kevent *kev)
195 char *flags_str = kevent_flags_dump(kev);
196 char *fflags_str = kevent_fflags_dump(kev);
198 snprintf(&buf[0], sizeof(buf),
199 "[ident=%ju, filter=%d, %s, %s, data=%jd, udata=%p, "
200 "ext=[%jx %jx %jx %jx]",
201 (uintmax_t) kev->ident,
205 (uintmax_t)kev->data,
207 (uintmax_t)kev->ext[0],
208 (uintmax_t)kev->ext[1],
209 (uintmax_t)kev->ext[2],
210 (uintmax_t)kev->ext[3]);
215 return (strdup(buf));
219 kevent_add(int fd, struct kevent *kev,
229 EV_SET(kev, ident, filter, flags, fflags, data, udata);
230 if (kevent(fd, kev, 1, NULL, 0, NULL) < 0) {
231 kev_str = kevent_to_str(kev);
232 printf("Unable to add the following kevent:\n%s\n",
235 err(1, "kevent(): %s", strerror(errno));
240 _kevent_cmp(struct kevent *k1, struct kevent *k2, const char *file, int line)
246 Workaround for inconsistent implementation of kevent(2)
249 if (k1->flags & EV_ADD)
252 if (k1->ident != k2->ident || k1->filter != k2->filter ||
253 k1->flags != k2->flags || k1->fflags != k2->fflags ||
254 k1->data != k2->data || k1->udata != k2->udata ||
255 k1->ext[0] != k2->ext[0] || k1->ext[1] != k2->ext[1] ||
256 k1->ext[0] != k2->ext[2] || k1->ext[0] != k2->ext[3]) {
257 kev1_str = kevent_to_str(k1);
258 kev2_str = kevent_to_str(k2);
259 printf("[%s:%d]: kevent_cmp: mismatch:\n %s !=\n %s\n",
260 file, line, kev1_str, kev2_str);
268 test_begin(const char *func)
272 cur_test_id = strdup(func);
274 err(1, "strdup failed");
276 printf("\n\nTest %d: %s\n", testnum++, func);
282 printf("%-70s %s\n", cur_test_id, "passed");
290 test_begin("kqueue()");
291 if ((kqfd = kqueue()) < 0)
298 test_kqueue_close(void)
300 test_begin("close(kq)");
307 main(int argc, char **argv)
317 /* XXX-FIXME temporary */
322 if (strcmp(argv[0], "--no-proc") == 0)
324 if (strcmp(argv[0], "--no-socket") == 0)
326 if (strcmp(argv[0], "--no-timer") == 0)
328 if (strcmp(argv[0], "--no-signal") == 0)
330 if (strcmp(argv[0], "--no-vnode") == 0)
332 if (strcmp(argv[0], "--no-user") == 0)
339 * Some tests fork. If output is fully buffered,
340 * the children inherit some buffered data and flush
341 * it when they exit, causing some data to be printed twice.
342 * Use line buffering to avoid this problem.
353 test_evfilt_signal();
366 "+OK All %d tests completed.\n", testnum - 1);