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.
19 #include <sys/types.h>
25 char *cur_test_id = NULL;
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();
34 extern void test_evfilt_user();
37 /* Checks if any events are pending, which is an error. */
42 struct timespec timeo;
46 puts("confirming that there are no events pending");
47 memset(&timeo, 0, sizeof(timeo));
48 nfds = kevent(kqfd, NULL, 0, &kev, 1, &timeo);
50 puts("\nUnexpected event:");
51 kev_str = kevent_to_str(&kev);
54 errx(1, "%d event(s) pending, but none expected:", nfds);
58 /* Checks if any events are pending, which is an error. Do not print
59 * out anything unless events are found.
62 test_no_kevents_quietly(void)
65 struct timespec timeo;
69 memset(&timeo, 0, sizeof(timeo));
70 nfds = kevent(kqfd, NULL, 0, &kev, 1, &timeo);
72 puts("\nUnexpected event:");
73 kev_str = kevent_to_str(&kev);
76 errx(1, "%d event(s) pending, but none expected:", nfds);
80 /* Retrieve a single kevent */
87 if ((kev = calloc(1, sizeof(*kev))) == NULL)
88 err(1, "out of memory");
90 nfds = kevent(kqfd, NULL, 0, kev, 1, NULL);
97 /* Retrieve a single kevent, specifying a maximum time to wait for it. */
99 kevent_get_timeout(int kqfd, int seconds)
103 struct timespec timeout = {seconds, 0};
105 if ((kev = calloc(1, sizeof(*kev))) == NULL)
106 err(1, "out of memory");
108 nfds = kevent(kqfd, NULL, 0, kev, 1, &timeout);
111 } else if (nfds == 0) {
120 kevent_fflags_dump(struct kevent *kev)
124 #define KEVFFL_DUMP(attrib) \
125 if (kev->fflags & attrib) \
126 strncat(buf, #attrib" ", 64);
128 if ((buf = calloc(1, 1024)) == NULL)
131 /* Not every filter has meaningful fflags */
132 if (kev->filter == EVFILT_PROC) {
133 snprintf(buf, 1024, "fflags = %x (", kev->fflags);
134 KEVFFL_DUMP(NOTE_EXIT);
135 KEVFFL_DUMP(NOTE_FORK);
136 KEVFFL_DUMP(NOTE_EXEC);
137 KEVFFL_DUMP(NOTE_CHILD);
138 KEVFFL_DUMP(NOTE_TRACKERR);
139 KEVFFL_DUMP(NOTE_TRACK);
140 buf[strlen(buf) - 1] = ')';
141 } else if (kev->filter == EVFILT_PROCDESC) {
142 snprintf(buf, 1024, "fflags = %x (", kev->fflags);
143 KEVFFL_DUMP(NOTE_EXIT);
144 KEVFFL_DUMP(NOTE_FORK);
145 KEVFFL_DUMP(NOTE_EXEC);
146 buf[strlen(buf) - 1] = ')';
147 } else if (kev->filter == EVFILT_VNODE) {
148 snprintf(buf, 1024, "fflags = %x (", kev->fflags);
149 KEVFFL_DUMP(NOTE_DELETE);
150 KEVFFL_DUMP(NOTE_WRITE);
151 KEVFFL_DUMP(NOTE_EXTEND);
152 #if HAVE_NOTE_TRUNCATE
153 KEVFFL_DUMP(NOTE_TRUNCATE);
155 KEVFFL_DUMP(NOTE_ATTRIB);
156 KEVFFL_DUMP(NOTE_LINK);
157 KEVFFL_DUMP(NOTE_RENAME);
159 KEVFFL_DUMP(NOTE_REVOKE);
161 buf[strlen(buf) - 1] = ')';
163 snprintf(buf, 1024, "fflags = %x", kev->fflags);
170 kevent_flags_dump(struct kevent *kev)
174 #define KEVFL_DUMP(attrib) \
175 if (kev->flags & attrib) \
176 strncat(buf, #attrib" ", 64);
178 if ((buf = calloc(1, 1024)) == NULL)
181 snprintf(buf, 1024, "flags = %d (", kev->flags);
183 KEVFL_DUMP(EV_ENABLE);
184 KEVFL_DUMP(EV_DISABLE);
185 KEVFL_DUMP(EV_DELETE);
186 KEVFL_DUMP(EV_ONESHOT);
187 KEVFL_DUMP(EV_CLEAR);
189 KEVFL_DUMP(EV_ERROR);
191 KEVFL_DUMP(EV_DISPATCH);
194 KEVFL_DUMP(EV_RECEIPT);
196 buf[strlen(buf) - 1] = ')';
201 /* Copied from ../kevent.c kevent_dump() and improved */
203 kevent_to_str(struct kevent *kev)
206 char *flags_str = kevent_flags_dump(kev);
207 char *fflags_str = kevent_fflags_dump(kev);
209 snprintf(&buf[0], sizeof(buf),
210 "[ident=%ju, filter=%d, %s, %s, data=%jd, udata=%p, "
211 "ext=[%jx %jx %jx %jx]",
212 (uintmax_t) kev->ident,
216 (uintmax_t)kev->data,
218 (uintmax_t)kev->ext[0],
219 (uintmax_t)kev->ext[1],
220 (uintmax_t)kev->ext[2],
221 (uintmax_t)kev->ext[3]);
226 return (strdup(buf));
230 kevent_add(int kqfd, struct kevent *kev,
240 EV_SET(kev, ident, filter, flags, fflags, data, NULL);
241 if (kevent(kqfd, kev, 1, NULL, 0, NULL) < 0) {
242 kev_str = kevent_to_str(kev);
243 printf("Unable to add the following kevent:\n%s\n",
246 err(1, "kevent(): %s", strerror(errno));
251 kevent_cmp(struct kevent *k1, struct kevent *k2)
257 Workaround for inconsistent implementation of kevent(2)
260 if (k1->flags & EV_ADD)
263 if (k1->ident != k2->ident || k1->filter != k2->filter ||
264 k1->flags != k2->flags || k1->fflags != k2->fflags ||
265 k1->data != k2->data || k1->udata != k2->udata ||
266 k1->ext[0] != k2->ext[0] || k1->ext[1] != k2->ext[1] ||
267 k1->ext[0] != k2->ext[2] || k1->ext[0] != k2->ext[3]) {
268 kev1_str = kevent_to_str(k1);
269 kev2_str = kevent_to_str(k2);
270 printf("kevent_cmp: mismatch:\n %s !=\n %s\n",
279 test_begin(const char *func)
283 cur_test_id = strdup(func);
285 err(1, "strdup failed");
287 printf("\n\nTest %d: %s\n", testnum++, func);
293 printf("%-70s %s\n", cur_test_id, "passed");
301 test_begin("kqueue()");
302 if ((kqfd = kqueue()) < 0)
309 test_kqueue_close(void)
311 test_begin("close(kq)");
318 main(int argc, char **argv)
328 /* XXX-FIXME temporary */
333 if (strcmp(argv[0], "--no-proc") == 0)
335 if (strcmp(argv[0], "--no-socket") == 0)
337 if (strcmp(argv[0], "--no-timer") == 0)
339 if (strcmp(argv[0], "--no-signal") == 0)
341 if (strcmp(argv[0], "--no-vnode") == 0)
343 if (strcmp(argv[0], "--no-user") == 0)
350 * Some tests fork. If output is fully buffered,
351 * the children inherit some buffered data and flush
352 * it when they exit, causing some data to be printed twice.
353 * Use line buffering to avoid this problem.
364 test_evfilt_signal();
377 "+OK All %d tests completed.\n", testnum - 1);