1 /* $NetBSD: t_timer_create.c,v 1.2 2011/09/17 18:52:21 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 timer_signal_handler(int, siginfo_t *, void *); 41 static void timer_signal_create(clockid_t); 42 43 static void 44 timer_signal_handler(int signo, siginfo_t *si, void *osi) 45 { 46 timer_t *tp; 47 48 tp = si->si_value.sival_ptr; 49 50 if (*tp == t && signo == SIGALRM) 51 fail = false; 52 53 (void)fprintf(stderr, "%s: %s\n", __func__, strsignal(signo)); 54 } 55 56 static void 57 timer_signal_create(clockid_t cid) 58 { 59 struct itimerspec tim; 60 struct sigaction act; 61 struct sigevent evt; 62 sigset_t set; 63 64 t = 0; 65 fail = true; 66 67 (void)memset(&evt, 0, sizeof(struct sigevent)); 68 (void)memset(&act, 0, sizeof(struct sigaction)); 69 (void)memset(&tim, 0, sizeof(struct itimerspec)); 70 71 /* 72 * Set handler. 73 */ 74 act.sa_flags = SA_SIGINFO; 75 act.sa_sigaction = timer_signal_handler; 76 77 ATF_REQUIRE(sigemptyset(&set) == 0); 78 ATF_REQUIRE(sigemptyset(&act.sa_mask) == 0); 79 80 /* 81 * Block SIGALRM while configuring the timer. 82 */ 83 ATF_REQUIRE(sigaction(SIGALRM, &act, NULL) == 0); 84 ATF_REQUIRE(sigaddset(&set, SIGALRM) == 0); 85 ATF_REQUIRE(sigprocmask(SIG_SETMASK, &set, NULL) == 0); 86 87 /* 88 * Create the timer (SIGEV_SIGNAL). 89 */ 90 evt.sigev_signo = SIGALRM; 91 evt.sigev_value.sival_ptr = &t; 92 evt.sigev_notify = SIGEV_SIGNAL; 93 94 ATF_REQUIRE(timer_create(cid, &evt, &t) == 0); 95 96 /* 97 * Start the timer. After this, unblock the signal. 98 */ 99 tim.it_value.tv_sec = 1; 100 tim.it_value.tv_nsec = 0; 101 102 ATF_REQUIRE(timer_settime(t, 0, &tim, NULL) == 0); 103 104 (void)sigprocmask(SIG_UNBLOCK, &set, NULL); 105 (void)sleep(2); 106 107 if (fail != false) 108 atf_tc_fail("timer failed to fire"); 109 } 110 111 ATF_TC(timer_create_err); 112 ATF_TC_HEAD(timer_create_err, tc) 113 { 114 /* Cf. PR lib/42434. */ 115 atf_tc_set_md_var(tc, "descr", "Check errors from timer_create(2)"); 116 } 117 118 ATF_TC_BODY(timer_create_err, tc) 119 { 120 struct sigevent ev; 121 122 (void)memset(&ev, 0, sizeof(struct sigevent)); 123 124 errno = 0; 125 ev.sigev_signo = -1; 126 ev.sigev_notify = SIGEV_SIGNAL; 127 128 ATF_REQUIRE_ERRNO(EINVAL, timer_create(CLOCK_REALTIME, &ev, &t) == -1); 129 130 errno = 0; 131 ev.sigev_signo = SIGUSR1; 132 ev.sigev_notify = SIGEV_THREAD + 100; 133 134 ATF_REQUIRE_ERRNO(EINVAL, timer_create(CLOCK_REALTIME, &ev, &t) == -1); 135 } 136 137 ATF_TC(timer_create_real); 138 ATF_TC_HEAD(timer_create_real, tc) 139 { 140 141 atf_tc_set_md_var(tc, "descr", 142 "Checks timer_create(2) with CLOCK_REALTIME and sigevent(3), " 143 "SIGEV_SIGNAL"); 144 } 145 146 ATF_TC_BODY(timer_create_real, tc) 147 { 148 timer_signal_create(CLOCK_REALTIME); 149 } 150 151 ATF_TC(timer_create_mono); 152 ATF_TC_HEAD(timer_create_mono, tc) 153 { 154 155 atf_tc_set_md_var(tc, "descr", 156 "Checks timer_create(2) with CLOCK_MONOTONIC and sigevent(3), " 157 "SIGEV_SIGNAL"); 158 } 159 160 ATF_TC_BODY(timer_create_mono, tc) 161 { 162 timer_signal_create(CLOCK_MONOTONIC); 163 } 164 165 ATF_TP_ADD_TCS(tp) 166 { 167 168 ATF_TP_ADD_TC(tp, timer_create_err); 169 ATF_TP_ADD_TC(tp, timer_create_real); 170 ATF_TP_ADD_TC(tp, timer_create_mono); 171 172 return atf_no_error(); 173 } 174