user.c revision 200483
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: head/tools/regression/kqueue/user.c 200483 2009-12-13 20:27:46Z rwatson $
17 */
18
19#include "common.h"
20
21int kqfd;
22
23static void
24add_and_delete(void)
25{
26    const char *test_id = "kevent(EVFILT_USER, EV_ADD and EV_DELETE)";
27    struct kevent kev;
28
29    test_begin(test_id);
30
31    kevent_add(kqfd, &kev, 1, EVFILT_USER, EV_ADD, 0, 0, NULL);
32    test_no_kevents();
33
34    kevent_add(kqfd, &kev, 1, EVFILT_USER, EV_DELETE, 0, 0, NULL);
35    test_no_kevents();
36
37    success();
38}
39
40static void
41event_wait(void)
42{
43    const char *test_id = "kevent(EVFILT_USER, wait)";
44    struct kevent kev;
45
46    test_begin(test_id);
47
48    /* Add the event, and then trigger it */
49    kevent_add(kqfd, &kev, 1, EVFILT_USER, EV_ADD, 0, 0, NULL);
50    kevent_add(kqfd, &kev, 1, EVFILT_USER, 0, NOTE_TRIGGER, 0, NULL);
51
52    kev.fflags &= ~NOTE_FFCTRLMASK;
53    kev.fflags &= ~NOTE_TRIGGER;
54    kevent_cmp(&kev, kevent_get(kqfd));
55
56    test_no_kevents();
57
58    success();
59}
60
61static void
62disable_and_enable(void)
63{
64    const char *test_id = "kevent(EVFILT_USER, EV_DISABLE and EV_ENABLE)";
65    struct kevent kev;
66
67    test_begin(test_id);
68
69    kevent_add(kqfd, &kev, 1, EVFILT_USER, EV_ADD, 0, 0, NULL);
70    kevent_add(kqfd, &kev, 1, EVFILT_USER, EV_DISABLE, 0, 0, NULL);
71
72    /* Trigger the event, but since it is disabled, nothing will happen. */
73    kevent_add(kqfd, &kev, 1, EVFILT_USER, 0, NOTE_TRIGGER, 0, NULL);
74    test_no_kevents();
75
76    kevent_add(kqfd, &kev, 1, EVFILT_USER, EV_ENABLE, 0, 0, NULL);
77    kevent_add(kqfd, &kev, 1, EVFILT_USER, 0, NOTE_TRIGGER, 0, NULL);
78
79    kev.flags = 0;
80    kev.fflags &= ~NOTE_FFCTRLMASK;
81    kev.fflags &= ~NOTE_TRIGGER;
82    kevent_cmp(&kev, kevent_get(kqfd));
83
84    success();
85}
86
87static void
88oneshot(void)
89{
90    const char *test_id = "kevent(EVFILT_USER, EV_ONESHOT)";
91    struct kevent kev;
92
93    test_begin(test_id);
94
95    kevent_add(kqfd, &kev, 1, EVFILT_USER, EV_ADD | EV_ONESHOT, 0, 0, NULL);
96
97    puts("  -- event 1");
98    kevent_add(kqfd, &kev, 1, EVFILT_USER, 0, NOTE_TRIGGER, 0, NULL);
99
100    kev.flags = 0;
101    kev.fflags &= ~NOTE_FFCTRLMASK;
102    kev.fflags &= ~NOTE_TRIGGER;
103    kevent_cmp(&kev, kevent_get(kqfd));
104
105    /* Try to trigger the event again. It is deleted, so that
106       should be an error. However, on FreeBSD 8 it is not an error,
107       so the event is ignored.
108     */
109    puts("  -- triggering an event that will be ignored");
110    kev.flags = 0;
111    kev.fflags |= NOTE_TRIGGER;
112    if (kevent(kqfd, &kev, 1, NULL, 0, NULL) < 0)
113        err(1, "%s", test_id);
114
115    test_no_kevents();
116
117    success();
118}
119
120void
121test_evfilt_user()
122{
123	kqfd = kqueue();
124
125    add_and_delete();
126    event_wait();
127    disable_and_enable();
128    oneshot();
129    /* TODO: try different fflags operations */
130
131	close(kqfd);
132}
133