subrata_modak | ded2c1e | 2009-12-07 11:44:52 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) Jiri Palecek<jpalecek@web.de>, 2009 |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify it |
| 5 | * under the terms of version 2 of the GNU General Public License as |
| 6 | * published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it would be useful, but |
| 9 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 11 | * |
| 12 | * Further, this software is distributed without any warranty that it is |
| 13 | * free of the rightful claim of any third person regarding infringement |
| 14 | * or the like. Any license provided herein, whether implied or |
| 15 | * otherwise, applies only to this software file. Patent licenses, if |
| 16 | * any, provided herein do not apply to combinations of this program with |
| 17 | * other software, or any other product whatsoever. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License along |
Wanlong Gao | fed9641 | 2012-10-24 10:10:29 +0800 | [diff] [blame] | 20 | * with this program; if not, write the Free Software Foundation, Inc., |
| 21 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
subrata_modak | ded2c1e | 2009-12-07 11:44:52 +0000 | [diff] [blame] | 22 | */ |
| 23 | #include "test.h" |
subrata_modak | ded2c1e | 2009-12-07 11:44:52 +0000 | [diff] [blame] | 24 | #include <errno.h> |
| 25 | #include <signal.h> |
| 26 | #include "../utils/include_j_h.h" |
| 27 | #include "../utils/common_j_h.c" |
| 28 | #include <limits.h> |
| 29 | #include "linux_syscall_numbers.h" |
| 30 | |
Garrett Cooper | 6061109 | 2011-02-23 00:04:58 -0800 | [diff] [blame] | 31 | #define SUCCEED_OR_DIE(syscall, message, ...) \ |
| 32 | (errno = 0, \ |
| 33 | ({int ret=syscall(__VA_ARGS__); \ |
| 34 | if (ret==-1) \ |
| 35 | tst_brkm(TBROK|TERRNO, cleanup, message);\ |
subrata_modak | ded2c1e | 2009-12-07 11:44:52 +0000 | [diff] [blame] | 36 | ret;})) |
| 37 | |
| 38 | /* Report success iff TEST_RETURN and TEST_ERRNO are equal to |
| 39 | exp_return and exp_errno, resp., and cond is true. If cond is not |
| 40 | true, report condition_errmsg |
| 41 | */ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 42 | static void report_success_cond(const char *func, const char *file, int line, |
| 43 | long exp_return, int exp_errno, int condition, |
| 44 | char *condition_errmsg) |
subrata_modak | ded2c1e | 2009-12-07 11:44:52 +0000 | [diff] [blame] | 45 | { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 46 | if (exp_return == TEST_RETURN |
| 47 | && (exp_return != -1 || exp_errno == TEST_ERRNO)) |
Garrett Cooper | df3eb16 | 2010-11-28 22:44:32 -0800 | [diff] [blame] | 48 | if (condition) |
subrata_modak | ded2c1e | 2009-12-07 11:44:52 +0000 | [diff] [blame] | 49 | tst_resm(TPASS, "Test passed"); |
| 50 | else |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 51 | tst_resm(TFAIL, "%s (%s: %d): %s", func, file, line, |
| 52 | condition_errmsg); |
Garrett Cooper | df3eb16 | 2010-11-28 22:44:32 -0800 | [diff] [blame] | 53 | else if (TEST_RETURN != -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 54 | tst_resm(TFAIL, |
| 55 | "%s (%s: %d): Unexpected return value; expected %ld, got %ld", |
| 56 | func, file, line, exp_return, TEST_RETURN); |
subrata_modak | ded2c1e | 2009-12-07 11:44:52 +0000 | [diff] [blame] | 57 | else |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 58 | tst_resm(TFAIL | TTERRNO, "%s (%s: %d): Unexpected failure", |
| 59 | func, file, line); |
subrata_modak | ded2c1e | 2009-12-07 11:44:52 +0000 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | #define REPORT_SUCCESS_COND(exp_return, exp_errno, condition, condition_errmsg) \ |
| 63 | report_success_cond(__FUNCTION__, __FILE__, __LINE__, exp_return, exp_errno, condition, condition_errmsg); |
| 64 | |
| 65 | /* Report success iff TEST_RETURN and TEST_ERRNO are equal to |
| 66 | exp_return and exp_errno, resp. |
| 67 | */ |
| 68 | #define REPORT_SUCCESS(exp_return, exp_errno) \ |
| 69 | REPORT_SUCCESS_COND(exp_return, exp_errno, 1, ""); |
| 70 | |
Garrett Cooper | ab5336e | 2010-12-19 08:41:59 -0800 | [diff] [blame] | 71 | static void cleanup(void); |
subrata_modak | ded2c1e | 2009-12-07 11:44:52 +0000 | [diff] [blame] | 72 | |
| 73 | static void empty_handler(int sig) |
| 74 | { |
| 75 | } |
| 76 | |
| 77 | static void setup(void) |
| 78 | { |
| 79 | tst_sig(FORK, DEF_HANDLER, cleanup); |
| 80 | signal(SIGUSR1, empty_handler); |
| 81 | signal(SIGALRM, empty_handler); |
| 82 | signal(SIGUSR2, SIG_IGN); |
| 83 | |
| 84 | TEST_PAUSE; |
| 85 | } |
| 86 | |
| 87 | static void cleanup(void) |
| 88 | { |
subrata_modak | ded2c1e | 2009-12-07 11:44:52 +0000 | [diff] [blame] | 89 | } |
| 90 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 91 | typedef int (*swi_func) (const sigset_t * set, siginfo_t * info, |
| 92 | struct timespec * timeout); |
| 93 | typedef void (*test_func) (swi_func, int); |
subrata_modak | ded2c1e | 2009-12-07 11:44:52 +0000 | [diff] [blame] | 94 | |
Garrett Cooper | ab5336e | 2010-12-19 08:41:59 -0800 | [diff] [blame] | 95 | #ifdef TEST_SIGWAIT |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 96 | static int my_sigwait(const sigset_t * set, siginfo_t * info, |
| 97 | struct timespec *timeout) |
subrata_modak | ded2c1e | 2009-12-07 11:44:52 +0000 | [diff] [blame] | 98 | { |
| 99 | int ret; |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 100 | int err = sigwait(set, &ret); |
subrata_modak | ded2c1e | 2009-12-07 11:44:52 +0000 | [diff] [blame] | 101 | |
Garrett Cooper | df3eb16 | 2010-11-28 22:44:32 -0800 | [diff] [blame] | 102 | if (err == 0) |
subrata_modak | ded2c1e | 2009-12-07 11:44:52 +0000 | [diff] [blame] | 103 | return ret; |
| 104 | errno = err; |
| 105 | return -1; |
| 106 | } |
Garrett Cooper | ab5336e | 2010-12-19 08:41:59 -0800 | [diff] [blame] | 107 | #endif |
subrata_modak | ded2c1e | 2009-12-07 11:44:52 +0000 | [diff] [blame] | 108 | |
Garrett Cooper | ab5336e | 2010-12-19 08:41:59 -0800 | [diff] [blame] | 109 | #ifdef TEST_SIGWAITINFO |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 110 | static int my_sigwaitinfo(const sigset_t * set, siginfo_t * info, |
| 111 | struct timespec *timeout) |
subrata_modak | ded2c1e | 2009-12-07 11:44:52 +0000 | [diff] [blame] | 112 | { |
Garrett Cooper | 6061109 | 2011-02-23 00:04:58 -0800 | [diff] [blame] | 113 | |
subrata_modak | ded2c1e | 2009-12-07 11:44:52 +0000 | [diff] [blame] | 114 | return sigwaitinfo(set, info); |
| 115 | } |
Garrett Cooper | ab5336e | 2010-12-19 08:41:59 -0800 | [diff] [blame] | 116 | #endif |
subrata_modak | ded2c1e | 2009-12-07 11:44:52 +0000 | [diff] [blame] | 117 | |
Garrett Cooper | ab5336e | 2010-12-19 08:41:59 -0800 | [diff] [blame] | 118 | #ifdef TEST_SIGTIMEDWAIT |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 119 | static int my_sigtimedwait(const sigset_t * set, siginfo_t * info, |
| 120 | struct timespec *timeout) |
subrata_modak | ded2c1e | 2009-12-07 11:44:52 +0000 | [diff] [blame] | 121 | { |
Garrett Cooper | 6061109 | 2011-02-23 00:04:58 -0800 | [diff] [blame] | 122 | |
subrata_modak | ded2c1e | 2009-12-07 11:44:52 +0000 | [diff] [blame] | 123 | return sigtimedwait(set, info, timeout); |
| 124 | } |
Garrett Cooper | ab5336e | 2010-12-19 08:41:59 -0800 | [diff] [blame] | 125 | #endif |
subrata_modak | ded2c1e | 2009-12-07 11:44:52 +0000 | [diff] [blame] | 126 | |
Garrett Cooper | ab5336e | 2010-12-19 08:41:59 -0800 | [diff] [blame] | 127 | #ifdef TEST_RT_SIGTIMEDWAIT |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 128 | static int my_rt_sigtimedwait(const sigset_t * set, siginfo_t * info, |
| 129 | struct timespec *timeout) |
subrata_modak | ded2c1e | 2009-12-07 11:44:52 +0000 | [diff] [blame] | 130 | { |
Garrett Cooper | 6061109 | 2011-02-23 00:04:58 -0800 | [diff] [blame] | 131 | |
subrata_modak | ded2c1e | 2009-12-07 11:44:52 +0000 | [diff] [blame] | 132 | /* The last argument is (number_of_signals)/(bits_per_byte), which are 64 and 8, resp. */ |
Jan Stancek | 359980f | 2013-02-15 10:16:05 +0100 | [diff] [blame] | 133 | return ltp_syscall(__NR_rt_sigtimedwait, set, info, timeout, 8); |
subrata_modak | ded2c1e | 2009-12-07 11:44:52 +0000 | [diff] [blame] | 134 | } |
Garrett Cooper | ab5336e | 2010-12-19 08:41:59 -0800 | [diff] [blame] | 135 | #endif |
subrata_modak | ded2c1e | 2009-12-07 11:44:52 +0000 | [diff] [blame] | 136 | |
| 137 | void test_empty_set(swi_func sigwaitinfo, int signo) |
| 138 | { |
| 139 | sigset_t sigs; |
| 140 | siginfo_t si; |
| 141 | pid_t child; |
| 142 | |
| 143 | SUCCEED_OR_DIE(sigemptyset, "sigemptyset failed", &sigs); |
| 144 | /* Run a child that will wake us up */ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 145 | child = create_sig_proc(100000, signo, UINT_MAX); |
subrata_modak | ded2c1e | 2009-12-07 11:44:52 +0000 | [diff] [blame] | 146 | |
| 147 | TEST(sigwaitinfo(&sigs, &si, NULL)); |
| 148 | REPORT_SUCCESS(-1, EINTR); |
| 149 | |
| 150 | kill(child, SIGTERM); |
| 151 | } |
| 152 | |
| 153 | void test_timeout(swi_func sigwaitinfo, int signo) |
| 154 | { |
| 155 | sigset_t sigs; |
| 156 | siginfo_t si; |
| 157 | pid_t child; |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 158 | struct timespec ts = {.tv_sec = 1 }; |
subrata_modak | ded2c1e | 2009-12-07 11:44:52 +0000 | [diff] [blame] | 159 | |
| 160 | SUCCEED_OR_DIE(sigemptyset, "sigemptyset failed", &sigs); |
| 161 | |
| 162 | /* Run a child that will wake us up */ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 163 | child = create_sig_proc(100000, signo, UINT_MAX); |
subrata_modak | ded2c1e | 2009-12-07 11:44:52 +0000 | [diff] [blame] | 164 | |
| 165 | TEST(sigwaitinfo(&sigs, &si, &ts)); |
| 166 | REPORT_SUCCESS(-1, EAGAIN); |
| 167 | |
| 168 | kill(child, SIGTERM); |
| 169 | } |
| 170 | |
| 171 | /* Note: sigwait-ing for a signal that is not blocked is unspecified |
| 172 | * by POSIX; but works for non-ignored signals under Linux |
| 173 | */ |
| 174 | void test_unmasked_matching(swi_func sigwaitinfo, int signo) |
| 175 | { |
| 176 | sigset_t sigs; |
| 177 | siginfo_t si; |
| 178 | pid_t child; |
| 179 | |
| 180 | SUCCEED_OR_DIE(sigemptyset, "sigemptyset failed", &sigs); |
| 181 | SUCCEED_OR_DIE(sigaddset, "sigaddset failed", &sigs, signo); |
| 182 | |
| 183 | /* Run a child that will wake us up */ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 184 | child = create_sig_proc(100000, signo, UINT_MAX); |
subrata_modak | ded2c1e | 2009-12-07 11:44:52 +0000 | [diff] [blame] | 185 | |
| 186 | TEST(sigwaitinfo(&sigs, &si, NULL)); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 187 | REPORT_SUCCESS_COND(signo, 0, si.si_pid == child |
| 188 | && si.si_code == SI_USER |
| 189 | && si.si_signo == signo, "Struct siginfo mismatch"); |
subrata_modak | ded2c1e | 2009-12-07 11:44:52 +0000 | [diff] [blame] | 190 | |
| 191 | kill(child, SIGTERM); |
| 192 | } |
| 193 | |
| 194 | void test_unmasked_matching_noinfo(swi_func sigwaitinfo, int signo) |
| 195 | { |
| 196 | sigset_t sigs; |
| 197 | pid_t child; |
| 198 | |
| 199 | SUCCEED_OR_DIE(sigemptyset, "sigemptyset failed", &sigs); |
| 200 | SUCCEED_OR_DIE(sigaddset, "sigaddset failed", &sigs, signo); |
| 201 | /* Run a child that will wake us up */ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 202 | child = create_sig_proc(100000, signo, UINT_MAX); |
subrata_modak | ded2c1e | 2009-12-07 11:44:52 +0000 | [diff] [blame] | 203 | |
| 204 | TEST(sigwaitinfo(&sigs, NULL, NULL)); |
| 205 | REPORT_SUCCESS(signo, 0); |
| 206 | |
| 207 | kill(child, SIGTERM); |
| 208 | } |
| 209 | |
| 210 | void test_masked_matching(swi_func sigwaitinfo, int signo) |
| 211 | { |
| 212 | sigset_t sigs, oldmask; |
| 213 | siginfo_t si; |
| 214 | pid_t child; |
| 215 | |
| 216 | SUCCEED_OR_DIE(sigemptyset, "sigemptyset failed", &sigs); |
| 217 | SUCCEED_OR_DIE(sigaddset, "sigaddset failed", &sigs, signo); |
| 218 | /* let's not get interrupted by our dying child */ |
| 219 | SUCCEED_OR_DIE(sigaddset, "sigaddset failed", &sigs, SIGCHLD); |
| 220 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 221 | SUCCEED_OR_DIE(sigprocmask, "sigprocmask failed", SIG_SETMASK, &sigs, |
| 222 | &oldmask); |
subrata_modak | ded2c1e | 2009-12-07 11:44:52 +0000 | [diff] [blame] | 223 | |
| 224 | /* don't wait on a SIGCHLD */ |
| 225 | SUCCEED_OR_DIE(sigdelset, "sigaddset failed", &sigs, SIGCHLD); |
| 226 | |
| 227 | /* Run a child that will wake us up */ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 228 | child = create_sig_proc(0, signo, 1); |
subrata_modak | ded2c1e | 2009-12-07 11:44:52 +0000 | [diff] [blame] | 229 | |
| 230 | TEST(sigwaitinfo(&sigs, &si, NULL)); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 231 | REPORT_SUCCESS_COND(signo, 0, si.si_pid == child |
| 232 | && si.si_code == SI_USER |
| 233 | && si.si_signo == signo, "Struct siginfo mismatch"); |
subrata_modak | ded2c1e | 2009-12-07 11:44:52 +0000 | [diff] [blame] | 234 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 235 | SUCCEED_OR_DIE(sigprocmask, "restoring original signal mask failed", |
| 236 | SIG_SETMASK, &oldmask, &oldmask); |
subrata_modak | ded2c1e | 2009-12-07 11:44:52 +0000 | [diff] [blame] | 237 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 238 | tst_count--; |
subrata_modak | ded2c1e | 2009-12-07 11:44:52 +0000 | [diff] [blame] | 239 | |
Garrett Cooper | df3eb16 | 2010-11-28 22:44:32 -0800 | [diff] [blame] | 240 | if (sigismember(&oldmask, signo)) |
subrata_modak | ded2c1e | 2009-12-07 11:44:52 +0000 | [diff] [blame] | 241 | tst_resm(TPASS, "sigwaitinfo restored the original mask"); |
| 242 | else |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 243 | tst_resm(TFAIL, |
| 244 | "sigwaitinfo failed to restore the original mask"); |
subrata_modak | ded2c1e | 2009-12-07 11:44:52 +0000 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | void test_masked_matching_rt(swi_func sigwaitinfo, int signo) |
| 248 | { |
| 249 | sigset_t sigs, oldmask; |
| 250 | siginfo_t si; |
| 251 | pid_t child[2]; |
| 252 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 253 | signo = SIGRTMIN + 1; |
subrata_modak | ded2c1e | 2009-12-07 11:44:52 +0000 | [diff] [blame] | 254 | |
| 255 | SUCCEED_OR_DIE(sigemptyset, "sigemptyset failed", &sigs); |
| 256 | SUCCEED_OR_DIE(sigaddset, "sigaddset failed", &sigs, signo); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 257 | SUCCEED_OR_DIE(sigaddset, "sigaddset failed", &sigs, signo + 1); |
subrata_modak | ded2c1e | 2009-12-07 11:44:52 +0000 | [diff] [blame] | 258 | /* let's not get interrupted by our dying child */ |
| 259 | SUCCEED_OR_DIE(sigaddset, "sigaddset failed", &sigs, SIGCHLD); |
| 260 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 261 | SUCCEED_OR_DIE(sigprocmask, "sigprocmask failed", SIG_SETMASK, &sigs, |
| 262 | &oldmask); |
subrata_modak | ded2c1e | 2009-12-07 11:44:52 +0000 | [diff] [blame] | 263 | |
| 264 | /* don't wait on a SIGCHLD */ |
| 265 | SUCCEED_OR_DIE(sigdelset, "sigdelset failed", &sigs, SIGCHLD); |
| 266 | |
| 267 | /* Run a child that will wake us up */ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 268 | child[0] = create_sig_proc(0, signo, 1); |
| 269 | child[1] = create_sig_proc(0, signo + 1, 1); |
subrata_modak | ded2c1e | 2009-12-07 11:44:52 +0000 | [diff] [blame] | 270 | |
| 271 | TEST(sigwaitinfo(&sigs, &si, NULL)); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 272 | REPORT_SUCCESS_COND(signo, 0, si.si_pid == child[0] |
| 273 | && si.si_code == SI_USER |
| 274 | && si.si_signo == signo, "Struct siginfo mismatch"); |
subrata_modak | ded2c1e | 2009-12-07 11:44:52 +0000 | [diff] [blame] | 275 | |
| 276 | /* eat the other signal */ |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 277 | tst_count--; |
subrata_modak | ded2c1e | 2009-12-07 11:44:52 +0000 | [diff] [blame] | 278 | TEST(sigwaitinfo(&sigs, &si, NULL)); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 279 | REPORT_SUCCESS_COND(signo + 1, 0, si.si_pid == child[1] |
| 280 | && si.si_code == SI_USER |
| 281 | && si.si_signo == signo + 1, |
| 282 | "Struct siginfo mismatch"); |
subrata_modak | ded2c1e | 2009-12-07 11:44:52 +0000 | [diff] [blame] | 283 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 284 | SUCCEED_OR_DIE(sigprocmask, "restoring original signal mask failed", |
| 285 | SIG_SETMASK, &oldmask, &oldmask); |
subrata_modak | ded2c1e | 2009-12-07 11:44:52 +0000 | [diff] [blame] | 286 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 287 | tst_count--; |
subrata_modak | ded2c1e | 2009-12-07 11:44:52 +0000 | [diff] [blame] | 288 | |
Garrett Cooper | df3eb16 | 2010-11-28 22:44:32 -0800 | [diff] [blame] | 289 | if (sigismember(&oldmask, signo)) |
subrata_modak | ded2c1e | 2009-12-07 11:44:52 +0000 | [diff] [blame] | 290 | tst_resm(TPASS, "sigwaitinfo restored the original mask"); |
| 291 | else |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 292 | tst_resm(TFAIL, |
| 293 | "sigwaitinfo failed to restore the original mask"); |
subrata_modak | ded2c1e | 2009-12-07 11:44:52 +0000 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | void test_masked_matching_noinfo(swi_func sigwaitinfo, int signo) |
| 297 | { |
| 298 | sigset_t sigs, oldmask; |
| 299 | pid_t child; |
| 300 | |
| 301 | SUCCEED_OR_DIE(sigemptyset, "sigemptyset failed", &sigs); |
| 302 | SUCCEED_OR_DIE(sigaddset, "sigaddset failed", &sigs, signo); |
| 303 | /* let's not get interrupted by our dying child */ |
| 304 | SUCCEED_OR_DIE(sigaddset, "sigaddset failed", &sigs, SIGCHLD); |
| 305 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 306 | SUCCEED_OR_DIE(sigprocmask, "sigprocmask failed", SIG_SETMASK, &sigs, |
| 307 | &oldmask); |
subrata_modak | ded2c1e | 2009-12-07 11:44:52 +0000 | [diff] [blame] | 308 | |
| 309 | /* don't wait on a SIGCHLD */ |
| 310 | SUCCEED_OR_DIE(sigdelset, "sigaddset failed", &sigs, SIGCHLD); |
| 311 | |
| 312 | /* Run a child that will wake us up */ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 313 | child = create_sig_proc(0, signo, 1); |
subrata_modak | ded2c1e | 2009-12-07 11:44:52 +0000 | [diff] [blame] | 314 | |
| 315 | TEST(sigwaitinfo(&sigs, NULL, NULL)); |
| 316 | REPORT_SUCCESS(signo, 0); |
| 317 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 318 | SUCCEED_OR_DIE(sigprocmask, "restoring original signal mask failed", |
| 319 | SIG_SETMASK, &oldmask, &oldmask); |
subrata_modak | ded2c1e | 2009-12-07 11:44:52 +0000 | [diff] [blame] | 320 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 321 | tst_count--; |
subrata_modak | ded2c1e | 2009-12-07 11:44:52 +0000 | [diff] [blame] | 322 | |
Garrett Cooper | df3eb16 | 2010-11-28 22:44:32 -0800 | [diff] [blame] | 323 | if (sigismember(&oldmask, signo)) |
subrata_modak | ded2c1e | 2009-12-07 11:44:52 +0000 | [diff] [blame] | 324 | tst_resm(TPASS, "sigwaitinfo restored the original mask"); |
| 325 | else |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 326 | tst_resm(TFAIL, |
| 327 | "sigwaitinfo failed to restore the original mask"); |
subrata_modak | ded2c1e | 2009-12-07 11:44:52 +0000 | [diff] [blame] | 328 | |
| 329 | } |
| 330 | |
| 331 | void test_bad_address(swi_func sigwaitinfo, int signo) |
| 332 | { |
| 333 | sigset_t sigs, oldmask; |
| 334 | pid_t child; |
| 335 | |
| 336 | SUCCEED_OR_DIE(sigemptyset, "sigemptyset failed", &sigs); |
| 337 | SUCCEED_OR_DIE(sigaddset, "sigaddset failed", &sigs, signo); |
| 338 | /* let's not get interrupted by our dying child */ |
| 339 | SUCCEED_OR_DIE(sigaddset, "sigaddset failed", &sigs, SIGCHLD); |
| 340 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 341 | SUCCEED_OR_DIE(sigprocmask, "sigprocmask failed", SIG_SETMASK, &sigs, |
| 342 | &oldmask); |
subrata_modak | ded2c1e | 2009-12-07 11:44:52 +0000 | [diff] [blame] | 343 | |
| 344 | /* don't wait on a SIGCHLD */ |
| 345 | SUCCEED_OR_DIE(sigdelset, "sigaddset failed", &sigs, SIGCHLD); |
| 346 | |
| 347 | /* Run a child that will wake us up */ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 348 | child = create_sig_proc(0, signo, 1); |
subrata_modak | ded2c1e | 2009-12-07 11:44:52 +0000 | [diff] [blame] | 349 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 350 | TEST(sigwaitinfo(&sigs, (void *)1, NULL)); |
subrata_modak | ded2c1e | 2009-12-07 11:44:52 +0000 | [diff] [blame] | 351 | REPORT_SUCCESS(-1, EFAULT); |
| 352 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 353 | SUCCEED_OR_DIE(sigprocmask, "sigprocmask failed", SIG_SETMASK, &oldmask, |
| 354 | &oldmask); |
subrata_modak | ded2c1e | 2009-12-07 11:44:52 +0000 | [diff] [blame] | 355 | |
| 356 | kill(child, SIGTERM); |
| 357 | } |
| 358 | |
| 359 | void test_bad_address2(swi_func sigwaitinfo, int signo) |
| 360 | { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 361 | TEST(sigwaitinfo((void *)1, NULL, NULL)); |
subrata_modak | ded2c1e | 2009-12-07 11:44:52 +0000 | [diff] [blame] | 362 | REPORT_SUCCESS(-1, EFAULT); |
| 363 | } |
| 364 | |
| 365 | void test_bad_address3(swi_func sigwaitinfo, int signo) |
| 366 | { |
| 367 | sigset_t sigs; |
| 368 | SUCCEED_OR_DIE(sigemptyset, "sigemptyset failed", &sigs); |
| 369 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 370 | TEST(sigwaitinfo(&sigs, NULL, (void *)1)); |
subrata_modak | ded2c1e | 2009-12-07 11:44:52 +0000 | [diff] [blame] | 371 | REPORT_SUCCESS(-1, EFAULT); |
| 372 | } |
| 373 | |
| 374 | struct test_desc { |
| 375 | test_func tf; |
| 376 | swi_func swi; |
| 377 | int signo; |
Garrett Cooper | ab5336e | 2010-12-19 08:41:59 -0800 | [diff] [blame] | 378 | } tests[] = { |
| 379 | #ifdef TEST_RT_SIGTIMEDWAIT |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 380 | { |
| 381 | test_empty_set, my_rt_sigtimedwait, SIGUSR1}, { |
| 382 | test_unmasked_matching, my_rt_sigtimedwait, SIGUSR1}, { |
| 383 | test_masked_matching, my_rt_sigtimedwait, SIGUSR1}, { |
| 384 | test_unmasked_matching_noinfo, my_rt_sigtimedwait, SIGUSR1}, { |
| 385 | test_masked_matching_noinfo, my_rt_sigtimedwait, SIGUSR1}, { |
| 386 | test_bad_address, my_rt_sigtimedwait, SIGUSR1}, { |
| 387 | test_bad_address2, my_rt_sigtimedwait, SIGUSR1}, { |
| 388 | test_bad_address3, my_rt_sigtimedwait, SIGUSR1}, { |
| 389 | test_timeout, my_rt_sigtimedwait, 0}, |
| 390 | /* Special cases */ |
| 391 | /* 1: sigwaitinfo does respond to ignored signal */ |
| 392 | { |
| 393 | test_masked_matching, my_rt_sigtimedwait, SIGUSR2}, |
| 394 | /* 2: An ignored signal doesn't cause sigwaitinfo to return EINTR */ |
| 395 | { |
| 396 | test_timeout, my_rt_sigtimedwait, SIGUSR2}, |
| 397 | /* 3: The handler is not called when the signal is waited for by sigwaitinfo */ |
| 398 | { |
| 399 | test_masked_matching, my_rt_sigtimedwait, SIGTERM}, |
| 400 | /* 4: Simultaneous realtime signals are delivered in the order of increasing signal number */ |
| 401 | { |
| 402 | test_masked_matching_rt, my_rt_sigtimedwait, -1}, |
subrata_modak | ded2c1e | 2009-12-07 11:44:52 +0000 | [diff] [blame] | 403 | #endif |
| 404 | #if defined TEST_SIGWAIT |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 405 | { |
| 406 | test_unmasked_matching_noinfo, my_sigwait, SIGUSR1}, { |
| 407 | test_masked_matching_noinfo, my_sigwait, SIGUSR1}, |
subrata_modak | ded2c1e | 2009-12-07 11:44:52 +0000 | [diff] [blame] | 408 | #endif |
| 409 | #if defined TEST_SIGWAITINFO |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 410 | { |
| 411 | test_empty_set, my_sigwaitinfo, SIGUSR1}, { |
| 412 | test_unmasked_matching, my_sigwaitinfo, SIGUSR1}, { |
| 413 | test_masked_matching, my_sigwaitinfo, SIGUSR1}, { |
| 414 | test_unmasked_matching_noinfo, my_sigwaitinfo, SIGUSR1}, { |
| 415 | test_masked_matching_noinfo, my_sigwaitinfo, SIGUSR1}, { |
| 416 | test_bad_address, my_sigwaitinfo, SIGUSR1}, { |
| 417 | test_bad_address2, my_sigwaitinfo, SIGUSR1}, |
subrata_modak | ded2c1e | 2009-12-07 11:44:52 +0000 | [diff] [blame] | 418 | #endif |
| 419 | #if defined TEST_SIGTIMEDWAIT |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 420 | { |
| 421 | test_empty_set, my_sigtimedwait, SIGUSR1}, { |
| 422 | test_unmasked_matching, my_sigtimedwait, SIGUSR1}, { |
| 423 | test_masked_matching, my_sigtimedwait, SIGUSR1}, { |
| 424 | test_unmasked_matching_noinfo, my_sigtimedwait, SIGUSR1}, { |
| 425 | test_masked_matching_noinfo, my_sigtimedwait, SIGUSR1}, { |
| 426 | test_bad_address, my_sigtimedwait, SIGUSR1}, { |
| 427 | test_bad_address2, my_sigtimedwait, SIGUSR1}, { |
| 428 | test_bad_address3, my_sigtimedwait, SIGUSR1}, { |
| 429 | test_timeout, my_sigtimedwait, 0}, |
subrata_modak | ded2c1e | 2009-12-07 11:44:52 +0000 | [diff] [blame] | 430 | #endif |
| 431 | }; |
| 432 | |
| 433 | #if defined TEST_SIGWAITINFO |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 434 | const char *TCID = "sigwaitinfo01"; |
subrata_modak | ded2c1e | 2009-12-07 11:44:52 +0000 | [diff] [blame] | 435 | #elif defined TEST_RT_SIGTIMEDWAIT |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 436 | const char *TCID = "rt_sigtimedwait01"; |
subrata_modak | ded2c1e | 2009-12-07 11:44:52 +0000 | [diff] [blame] | 437 | #elif defined TEST_SIGTIMEDWAIT |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 438 | const char *TCID = "sigtimedwait01"; |
subrata_modak | ded2c1e | 2009-12-07 11:44:52 +0000 | [diff] [blame] | 439 | #elif defined TEST_SIGWAIT |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 440 | const char *TCID = "sigwait01"; |
subrata_modak | ded2c1e | 2009-12-07 11:44:52 +0000 | [diff] [blame] | 441 | #endif |
| 442 | |
Cyril Hrubis | b863a0b | 2014-09-24 13:15:29 +0200 | [diff] [blame] | 443 | int TST_TOTAL = ARRAY_SIZE(tests); |
subrata_modak | ded2c1e | 2009-12-07 11:44:52 +0000 | [diff] [blame] | 444 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 445 | int main(int argc, char **argv) |
subrata_modak | ded2c1e | 2009-12-07 11:44:52 +0000 | [diff] [blame] | 446 | { |
| 447 | unsigned i; |
| 448 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 449 | const char *msg; |
subrata_modak | ded2c1e | 2009-12-07 11:44:52 +0000 | [diff] [blame] | 450 | |
Garrett Cooper | ab5336e | 2010-12-19 08:41:59 -0800 | [diff] [blame] | 451 | if ((msg = parse_opts(argc, argv, NULL, NULL)) != NULL) |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 452 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 453 | |
subrata_modak | ded2c1e | 2009-12-07 11:44:52 +0000 | [diff] [blame] | 454 | setup(); |
| 455 | |
Garrett Cooper | df3eb16 | 2010-11-28 22:44:32 -0800 | [diff] [blame] | 456 | for (lc = 0; TEST_LOOPING(lc); ++lc) { |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 457 | tst_count = 0; |
subrata_modak | ded2c1e | 2009-12-07 11:44:52 +0000 | [diff] [blame] | 458 | |
Cyril Hrubis | b863a0b | 2014-09-24 13:15:29 +0200 | [diff] [blame] | 459 | for (i = 0; i < ARRAY_SIZE(tests); i++) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 460 | alarm(10); /* arrange a 10 second timeout */ |
Garrett Cooper | ab5336e | 2010-12-19 08:41:59 -0800 | [diff] [blame] | 461 | tst_resm(TINFO, "%p, %d", tests[i].swi, tests[i].signo); |
subrata_modak | ded2c1e | 2009-12-07 11:44:52 +0000 | [diff] [blame] | 462 | tests[i].tf(tests[i].swi, tests[i].signo); |
| 463 | } |
| 464 | alarm(0); |
| 465 | } |
| 466 | |
| 467 | cleanup(); |
Garrett Cooper | ab5336e | 2010-12-19 08:41:59 -0800 | [diff] [blame] | 468 | tst_exit(); |
Garrett Cooper | 6061109 | 2011-02-23 00:04:58 -0800 | [diff] [blame] | 469 | } |