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 "common.h" 20 21 int kqfd; 22 23 void 24 test_kevent_timer_add(void) 25 { 26 const char *test_id = "kevent(EVFILT_TIMER, EV_ADD)"; 27 struct kevent kev; 28 29 test_begin(test_id); 30 31 EV_SET(&kev, 1, EVFILT_TIMER, EV_ADD, 0, 1000, NULL); 32 if (kevent(kqfd, &kev, 1, NULL, 0, NULL) < 0) 33 err(1, "%s", test_id); 34 35 success(); 36 } 37 38 void 39 test_kevent_timer_del(void) 40 { 41 const char *test_id = "kevent(EVFILT_TIMER, EV_DELETE)"; 42 struct kevent kev; 43 44 test_begin(test_id); 45 46 EV_SET(&kev, 1, EVFILT_TIMER, EV_DELETE, 0, 0, NULL); 47 if (kevent(kqfd, &kev, 1, NULL, 0, NULL) < 0) 48 err(1, "%s", test_id); 49 50 test_no_kevents(); 51 52 success(); 53 } 54 55 void 56 test_kevent_timer_get(void) 57 { 58 const char *test_id = "kevent(EVFILT_TIMER, wait)"; 59 struct kevent kev; 60 61 test_begin(test_id); 62 63 EV_SET(&kev, 1, EVFILT_TIMER, EV_ADD, 0, 1000, NULL); 64 if (kevent(kqfd, &kev, 1, NULL, 0, NULL) < 0) 65 err(1, "%s", test_id); 66 67 kev.flags |= EV_CLEAR; 68 kev.data = 1; 69 kevent_cmp(&kev, kevent_get(kqfd)); 70 71 EV_SET(&kev, 1, EVFILT_TIMER, EV_DELETE, 0, 0, NULL); 72 if (kevent(kqfd, &kev, 1, NULL, 0, NULL) < 0) 73 err(1, "%s", test_id); 74 75 success(); 76 } 77 78 static void 79 test_oneshot(void) 80 { 81 const char *test_id = "kevent(EVFILT_TIMER, EV_ONESHOT)"; 82 struct kevent kev; 83 84 test_begin(test_id); 85 86 test_no_kevents(); 87 88 EV_SET(&kev, vnode_fd, EVFILT_TIMER, EV_ADD | EV_ONESHOT, 0, 500,NULL); 89 if (kevent(kqfd, &kev, 1, NULL, 0, NULL) < 0) 90 err(1, "%s", test_id); 91 92 /* Retrieve the event */ 93 kev.flags = EV_ADD | EV_CLEAR | EV_ONESHOT; 94 kev.data = 1; 95 kevent_cmp(&kev, kevent_get(kqfd)); 96 97 /* Check if the event occurs again */ 98 sleep(3); 99 test_no_kevents(); 100 101 102 success(); 103 } 104 105 static void 106 test_periodic(void) 107 { 108 const char *test_id = "kevent(EVFILT_TIMER, periodic)"; 109 struct kevent kev; 110 111 test_begin(test_id); 112 113 test_no_kevents(); 114 115 EV_SET(&kev, vnode_fd, EVFILT_TIMER, EV_ADD, 0, 1000,NULL); 116 if (kevent(kqfd, &kev, 1, NULL, 0, NULL) < 0) 117 err(1, "%s", test_id); 118 119 /* Retrieve the event */ 120 kev.flags = EV_ADD | EV_CLEAR; 121 kev.data = 1; 122 kevent_cmp(&kev, kevent_get(kqfd)); 123 124 /* Check if the event occurs again */ 125 sleep(1); 126 kevent_cmp(&kev, kevent_get(kqfd)); 127 128 /* Delete the event */ 129 kev.flags = EV_DELETE; 130 if (kevent(kqfd, &kev, 1, NULL, 0, NULL) < 0) 131 err(1, "%s", test_id); 132 133 success(); 134 } 135 136 static void 137 disable_and_enable(void) 138 { 139 const char *test_id = "kevent(EVFILT_TIMER, EV_DISABLE and EV_ENABLE)"; 140 struct kevent kev; 141 142 test_begin(test_id); 143 144 test_no_kevents(); 145 146 /* Add the watch and immediately disable it */ 147 EV_SET(&kev, vnode_fd, EVFILT_TIMER, EV_ADD | EV_ONESHOT, 0, 2000,NULL); 148 if (kevent(kqfd, &kev, 1, NULL, 0, NULL) < 0) 149 err(1, "%s", test_id); 150 kev.flags = EV_DISABLE; 151 if (kevent(kqfd, &kev, 1, NULL, 0, NULL) < 0) 152 err(1, "%s", test_id); 153 test_no_kevents(); 154 155 /* Re-enable and check again */ 156 kev.flags = EV_ENABLE; 157 if (kevent(kqfd, &kev, 1, NULL, 0, NULL) < 0) 158 err(1, "%s", test_id); 159 160 kev.flags = EV_ADD | EV_CLEAR | EV_ONESHOT; 161 kev.data = 1; 162 kevent_cmp(&kev, kevent_get(kqfd)); 163 164 success(); 165 } 166 167 void 168 test_evfilt_timer() 169 { 170 kqfd = kqueue(); 171 test_kevent_timer_add(); 172 test_kevent_timer_del(); 173 test_kevent_timer_get(); 174 test_oneshot(); 175 test_periodic(); 176 disable_and_enable(); 177 close(kqfd); 178 } 179