1 /* $NetBSD: t_timer_create.c,v 1.4 2012/03/18 07:00:52 jruoho Exp $ */ 2 3 /*- 4 * Copyright (c) 2010 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 * POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #include <atf-c.h> 30 #include <errno.h> 31 #include <stdio.h> 32 #include <signal.h> 33 #include <string.h> 34 #include <time.h> 35 #include <unistd.h> 36 37 static timer_t t; 38 static bool fail = true; 39 40 static void 41 #ifdef __FreeBSD__ 42 timer_signal_handler(int signo, siginfo_t *si, void *osi __unused) 43 #else 44 timer_signal_handler(int signo, siginfo_t *si, void *osi) 45 #endif 46 { 47 timer_t *tp; 48 49 tp = si->si_value.sival_ptr; 50 51 if (*tp == t && signo == SIGALRM) 52 fail = false; 53 54 (void)fprintf(stderr, "%s: %s\n", __func__, strsignal(signo)); 55 } 56 57 static void 58 timer_signal_create(clockid_t cid, bool expire) 59 { 60 struct itimerspec tim; 61 struct sigaction act; 62 struct sigevent evt; 63 sigset_t set; 64 65 t = 0; 66 fail = true; 67 68 (void)memset(&evt, 0, sizeof(struct sigevent)); 69 (void)memset(&act, 0, sizeof(struct sigaction)); 70 (void)memset(&tim, 0, sizeof(struct itimerspec)); 71 72 /* 73 * Set handler. 74 */ 75 act.sa_flags = SA_SIGINFO; 76 act.sa_sigaction = timer_signal_handler; 77 78 ATF_REQUIRE(sigemptyset(&set) == 0); 79 ATF_REQUIRE(sigemptyset(&act.sa_mask) == 0); 80 81 /* 82 * Block SIGALRM while configuring the timer. 83 */ 84 ATF_REQUIRE(sigaction(SIGALRM, &act, NULL) == 0); 85 ATF_REQUIRE(sigaddset(&set, SIGALRM) == 0); 86 ATF_REQUIRE(sigprocmask(SIG_SETMASK, &set, NULL) == 0); 87 88 /* 89 * Create the timer (SIGEV_SIGNAL). 90 */ 91 evt.sigev_signo = SIGALRM; 92 evt.sigev_value.sival_ptr = &t; 93 evt.sigev_notify = SIGEV_SIGNAL; 94 95 ATF_REQUIRE(timer_create(cid, &evt, &t) == 0); 96 97 /* 98 * Start the timer. After this, unblock the signal. 99 */ 100 tim.it_value.tv_sec = expire ? 5 : 1; 101 tim.it_value.tv_nsec = 0; 102 103 ATF_REQUIRE(timer_settime(t, 0, &tim, NULL) == 0); 104 105 (void)sigprocmask(SIG_UNBLOCK, &set, NULL); 106 (void)sleep(2); 107 108 if (expire) { 109 if (!fail) 110 atf_tc_fail("timer fired too soon"); 111 } else { 112 if (fail) 113 atf_tc_fail("timer failed to fire"); 114 } 115 116 ATF_REQUIRE(timer_delete(t) == 0); 117 } 118 119 #ifdef __FreeBSD__ 120 static void 121 timer_callback(union sigval value) 122 { 123 timer_t *tp; 124 125 tp = value.sival_ptr; 126 127 if (*tp == t) 128 fail = false; 129 } 130 131 static void 132 timer_thread_create(clockid_t cid, bool expire) 133 { 134 struct itimerspec tim; 135 struct sigevent evt; 136 137 t = 0; 138 fail = true; 139 140 (void)memset(&evt, 0, sizeof(struct sigevent)); 141 (void)memset(&tim, 0, sizeof(struct itimerspec)); 142 143 /* 144 * Create the timer (SIGEV_THREAD). 145 */ 146 evt.sigev_notify_function = timer_callback; 147 evt.sigev_value.sival_ptr = &t; 148 evt.sigev_notify = SIGEV_THREAD; 149 150 ATF_REQUIRE(timer_create(cid, &evt, &t) == 0); 151 152 /* 153 * Start the timer. 154 */ 155 tim.it_value.tv_sec = expire ? 5 : 1; 156 tim.it_value.tv_nsec = 0; 157 158 ATF_REQUIRE(timer_settime(t, 0, &tim, NULL) == 0); 159 160 (void)sleep(2); 161 162 if (expire) { 163 if (!fail) 164 atf_tc_fail("timer fired too soon"); 165 } else { 166 if (fail) 167 atf_tc_fail("timer failed to fire"); 168 } 169 170 ATF_REQUIRE(timer_delete(t) == 0); 171 } 172 #endif 173 174 ATF_TC(timer_create_err); 175 ATF_TC_HEAD(timer_create_err, tc) 176 { 177 atf_tc_set_md_var(tc, "descr", 178 "Check errors from timer_create(2) (PR lib/42434"); 179 } 180 181 ATF_TC_BODY(timer_create_err, tc) 182 { 183 struct sigevent ev; 184 185 (void)memset(&ev, 0, sizeof(struct sigevent)); 186 187 errno = 0; 188 ev.sigev_signo = -1; 189 ev.sigev_notify = SIGEV_SIGNAL; 190 191 ATF_REQUIRE_ERRNO(EINVAL, timer_create(CLOCK_REALTIME, &ev, &t) == -1); 192 193 errno = 0; 194 ev.sigev_signo = SIGUSR1; 195 ev.sigev_notify = SIGEV_THREAD + 100; 196 197 ATF_REQUIRE_ERRNO(EINVAL, timer_create(CLOCK_REALTIME, &ev, &t) == -1); 198 } 199 200 ATF_TC(timer_create_real); 201 ATF_TC_HEAD(timer_create_real, tc) 202 { 203 204 atf_tc_set_md_var(tc, "descr", 205 "Checks timer_create(2) with CLOCK_REALTIME and sigevent(3), " 206 "SIGEV_SIGNAL"); 207 } 208 209 ATF_TC_BODY(timer_create_real, tc) 210 { 211 timer_signal_create(CLOCK_REALTIME, false); 212 } 213 214 ATF_TC(timer_create_mono); 215 ATF_TC_HEAD(timer_create_mono, tc) 216 { 217 218 atf_tc_set_md_var(tc, "descr", 219 "Checks timer_create(2) with CLOCK_MONOTONIC and sigevent(3), " 220 "SIGEV_SIGNAL"); 221 } 222 223 ATF_TC_BODY(timer_create_mono, tc) 224 { 225 timer_signal_create(CLOCK_MONOTONIC, false); 226 } 227 228 ATF_TC(timer_create_real_expire); 229 ATF_TC_HEAD(timer_create_real_expire, tc) 230 { 231 232 atf_tc_set_md_var(tc, "descr", 233 "Checks timer_create(2) with CLOCK_REALTIME and sigevent(3), " 234 "SIGEV_SIGNAL, with expiration"); 235 } 236 237 ATF_TC_BODY(timer_create_real_expire, tc) 238 { 239 timer_signal_create(CLOCK_REALTIME, true); 240 } 241 242 ATF_TC(timer_create_mono_expire); 243 ATF_TC_HEAD(timer_create_mono_expire, tc) 244 { 245 246 atf_tc_set_md_var(tc, "descr", 247 "Checks timer_create(2) with CLOCK_MONOTONIC and sigevent(3), " 248 "SIGEV_SIGNAL, with expiration"); 249 } 250 251 ATF_TC_BODY(timer_create_mono_expire, tc) 252 { 253 timer_signal_create(CLOCK_MONOTONIC, true); 254 } 255 256 ATF_TC(timer_thread_create_real); 257 ATF_TC_HEAD(timer_thread_create_real, tc) 258 { 259 260 atf_tc_set_md_var(tc, "descr", 261 "Checks timer_create(2) with CLOCK_REALTIME and sigevent(3), " 262 "SIGEV_THREAD"); 263 } 264 265 #ifdef __FreeBSD__ 266 ATF_TC_BODY(timer_thread_create_real, tc) 267 { 268 timer_thread_create(CLOCK_REALTIME, false); 269 } 270 271 ATF_TC(timer_thread_create_mono); 272 ATF_TC_HEAD(timer_thread_create_mono, tc) 273 { 274 275 atf_tc_set_md_var(tc, "descr", 276 "Checks timer_create(2) with CLOCK_MONOTONIC and sigevent(3), " 277 "SIGEV_THREAD"); 278 } 279 280 ATF_TC_BODY(timer_thread_create_mono, tc) 281 { 282 timer_thread_create(CLOCK_MONOTONIC, false); 283 } 284 285 ATF_TC(timer_thread_create_real_expire); 286 ATF_TC_HEAD(timer_thread_create_real_expire, tc) 287 { 288 289 atf_tc_set_md_var(tc, "descr", 290 "Checks timer_create(2) with CLOCK_REALTIME and sigevent(3), " 291 "SIGEV_THREAD, with expiration"); 292 } 293 294 ATF_TC_BODY(timer_thread_create_real_expire, tc) 295 { 296 timer_thread_create(CLOCK_REALTIME, true); 297 } 298 299 ATF_TC(timer_thread_create_mono_expire); 300 ATF_TC_HEAD(timer_thread_create_mono_expire, tc) 301 { 302 303 atf_tc_set_md_var(tc, "descr", 304 "Checks timer_create(2) with CLOCK_MONOTONIC and sigevent(3), " 305 "SIGEV_THREAD, with expiration"); 306 } 307 308 ATF_TC_BODY(timer_thread_create_mono_expire, tc) 309 { 310 timer_thread_create(CLOCK_MONOTONIC, true); 311 } 312 #endif 313 314 ATF_TP_ADD_TCS(tp) 315 { 316 317 ATF_TP_ADD_TC(tp, timer_create_err); 318 ATF_TP_ADD_TC(tp, timer_create_real); 319 ATF_TP_ADD_TC(tp, timer_create_mono); 320 ATF_TP_ADD_TC(tp, timer_create_real_expire); 321 ATF_TP_ADD_TC(tp, timer_create_mono_expire); 322 #ifdef __FreeBSD__ 323 ATF_TP_ADD_TC(tp, timer_thread_create_real); 324 ATF_TP_ADD_TC(tp, timer_thread_create_mono); 325 ATF_TP_ADD_TC(tp, timer_thread_create_real_expire); 326 ATF_TP_ADD_TC(tp, timer_thread_create_mono_expire); 327 #endif 328 329 return atf_no_error(); 330 } 331