xref: /freebsd-13-stable/tests/sys/kqueue/libkqueue/user.c (revision 17da660ad5b3b9cd90e164dd4dbb9beaa7203054)
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 
17 #include "common.h"
18 
19 
20 static void
add_and_delete(void)21 add_and_delete(void)
22 {
23     const char *test_id = "kevent(EVFILT_USER, EV_ADD and EV_DELETE)";
24     struct kevent kev;
25 
26     test_begin(test_id);
27 
28     kevent_add(kqfd, &kev, 1, EVFILT_USER, EV_ADD, 0, 0, NULL);
29     test_no_kevents();
30 
31     kevent_add(kqfd, &kev, 1, EVFILT_USER, EV_DELETE, 0, 0, NULL);
32     test_no_kevents();
33 
34     success();
35 }
36 
37 static void
event_wait(void)38 event_wait(void)
39 {
40     const char *test_id = "kevent(EVFILT_USER, wait)";
41     struct kevent kev;
42 
43     test_begin(test_id);
44 
45     test_no_kevents();
46 
47     /* Add the event, and then trigger it */
48     kevent_add(kqfd, &kev, 1, EVFILT_USER, EV_ADD | EV_CLEAR, 0, 0, NULL);
49     kevent_add(kqfd, &kev, 1, EVFILT_USER, 0, NOTE_TRIGGER, 0, NULL);
50 
51     kev.fflags &= ~NOTE_FFCTRLMASK;
52     kev.fflags &= ~NOTE_TRIGGER;
53     kev.flags = EV_CLEAR;
54     kevent_cmp(&kev, kevent_get(kqfd));
55 
56     test_no_kevents();
57 
58     success();
59 }
60 
61 static void
disable_and_enable(void)62 disable_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     test_no_kevents();
70 
71     kevent_add(kqfd, &kev, 1, EVFILT_USER, EV_ADD, 0, 0, NULL);
72     kevent_add(kqfd, &kev, 1, EVFILT_USER, EV_DISABLE, 0, 0, NULL);
73 
74     /* Trigger the event, but since it is disabled, nothing will happen. */
75     kevent_add(kqfd, &kev, 1, EVFILT_USER, 0, NOTE_TRIGGER, 0, NULL);
76     test_no_kevents();
77 
78     kevent_add(kqfd, &kev, 1, EVFILT_USER, EV_ENABLE, 0, 0, NULL);
79     kevent_add(kqfd, &kev, 1, EVFILT_USER, 0, NOTE_TRIGGER, 0, NULL);
80 
81     kev.flags = EV_CLEAR;
82     kev.fflags &= ~NOTE_FFCTRLMASK;
83     kev.fflags &= ~NOTE_TRIGGER;
84     kevent_cmp(&kev, kevent_get(kqfd));
85 
86     success();
87 }
88 
89 static void
oneshot(void)90 oneshot(void)
91 {
92     const char *test_id = "kevent(EVFILT_USER, EV_ONESHOT)";
93     struct kevent kev;
94 
95     test_begin(test_id);
96 
97     test_no_kevents();
98 
99     kevent_add(kqfd, &kev, 2, EVFILT_USER, EV_ADD | EV_ONESHOT, 0, 0, NULL);
100 
101     puts("  -- event 1");
102     kevent_add(kqfd, &kev, 2, EVFILT_USER, 0, NOTE_TRIGGER, 0, NULL);
103 
104     kev.flags = EV_ONESHOT;
105     kev.fflags &= ~NOTE_FFCTRLMASK;
106     kev.fflags &= ~NOTE_TRIGGER;
107     kevent_cmp(&kev, kevent_get(kqfd));
108 
109     test_no_kevents();
110 
111     success();
112 }
113 
114 void
test_evfilt_user(void)115 test_evfilt_user(void)
116 {
117     kqfd = kqueue();
118 
119     add_and_delete();
120     event_wait();
121     disable_and_enable();
122     oneshot();
123     /* TODO: try different fflags operations */
124 
125     close(kqfd);
126 }
127