| 1 | /** |
| 2 | * Copyright © 2012 Red Hat, Inc. |
| 3 | * |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 5 | * copy of this software and associated documentation files (the "Software"), |
| 6 | * to deal in the Software without restriction, including without limitation |
| 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 8 | * and/or sell copies of the Software, and to permit persons to whom the |
| 9 | * Software is furnished to do so, subject to the following conditions: |
| 10 | * |
| 11 | * The above copyright notice and this permission notice (including the next |
| 12 | * paragraph) shall be included in all copies or substantial portions of the |
| 13 | * Software. |
| 14 | * |
| 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 18 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 20 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 21 | * DEALINGS IN THE SOFTWARE. |
| 22 | */ |
| 23 | |
| 24 | #ifdef HAVE_DIX_CONFIG_H |
| 25 | #include <dix-config.h> |
| 26 | #endif |
| 27 | |
| 28 | #include <signal.h> |
| 29 | #include "os.h" |
| 30 | |
| 31 | static int last_signal = 0; |
| 32 | static int expect_signal = 0; |
| 33 | |
| 34 | static void sighandler(int signal) |
| 35 | { |
| 36 | assert(expect_signal); |
| 37 | expect_signal = 0; |
| 38 | if (!last_signal) |
| 39 | raise(signal); |
| 40 | OsBlockSignals(); |
| 41 | OsReleaseSignals(); |
| 42 | last_signal = 1; |
| 43 | expect_signal = 1; |
| 44 | } |
| 45 | |
| 46 | static int |
| 47 | sig_is_blocked(int sig) |
| 48 | { |
| 49 | sigset_t current; |
| 50 | |
| 51 | sigemptyset(¤t); |
| 52 | assert(sigprocmask(SIG_BLOCK, NULL, ¤t) == 0); |
| 53 | return sigismember(¤t, sig); |
| 54 | } |
| 55 | |
| 56 | static void block_sigio_test(void) |
| 57 | { |
| 58 | #ifdef SIG_BLOCK |
| 59 | sigset_t current; |
| 60 | |
| 61 | sigemptyset(¤t); |
| 62 | assert(!sig_is_blocked(SIGIO)); |
| 63 | |
| 64 | /* block once */ |
| 65 | OsBlockSIGIO(); |
| 66 | assert(sig_is_blocked(SIGIO)); |
| 67 | OsReleaseSIGIO(); |
| 68 | assert(!sig_is_blocked(SIGIO)); |
| 69 | |
| 70 | /* block twice, nested */ |
| 71 | OsBlockSIGIO(); |
| 72 | assert(sig_is_blocked(SIGIO)); |
| 73 | OsBlockSIGIO(); |
| 74 | assert(sig_is_blocked(SIGIO)); |
| 75 | OsReleaseSIGIO(); |
| 76 | assert(sig_is_blocked(SIGIO)); |
| 77 | OsReleaseSIGIO(); |
| 78 | assert(!sig_is_blocked(SIGIO)); |
| 79 | |
| 80 | /* block all */ |
| 81 | OsBlockSignals(); |
| 82 | assert(sig_is_blocked(SIGIO)); |
| 83 | OsReleaseSignals(); |
| 84 | assert(!sig_is_blocked(SIGIO)); |
| 85 | |
| 86 | /* block all nested */ |
| 87 | OsBlockSignals(); |
| 88 | assert(sig_is_blocked(SIGIO)); |
| 89 | OsBlockSignals(); |
| 90 | assert(sig_is_blocked(SIGIO)); |
| 91 | OsReleaseSignals(); |
| 92 | assert(sig_is_blocked(SIGIO)); |
| 93 | OsReleaseSignals(); |
| 94 | assert(!sig_is_blocked(SIGIO)); |
| 95 | |
| 96 | /* mix the two */ |
| 97 | /* ABBA */ |
| 98 | OsBlockSignals(); |
| 99 | assert(sig_is_blocked(SIGIO)); |
| 100 | OsBlockSIGIO(); |
| 101 | assert(sig_is_blocked(SIGIO)); |
| 102 | OsReleaseSIGIO(); |
| 103 | assert(sig_is_blocked(SIGIO)); |
| 104 | OsReleaseSignals(); |
| 105 | assert(!sig_is_blocked(SIGIO)); |
| 106 | |
| 107 | /* ABAB */ |
| 108 | OsBlockSignals(); |
| 109 | assert(sig_is_blocked(SIGIO)); |
| 110 | OsBlockSIGIO(); |
| 111 | assert(sig_is_blocked(SIGIO)); |
| 112 | OsReleaseSignals(); |
| 113 | assert(sig_is_blocked(SIGIO)); |
| 114 | OsReleaseSIGIO(); |
| 115 | assert(!sig_is_blocked(SIGIO)); |
| 116 | |
| 117 | /* BAAB */ |
| 118 | OsBlockSIGIO(); |
| 119 | assert(sig_is_blocked(SIGIO)); |
| 120 | OsBlockSignals(); |
| 121 | assert(sig_is_blocked(SIGIO)); |
| 122 | OsReleaseSignals(); |
| 123 | assert(sig_is_blocked(SIGIO)); |
| 124 | OsReleaseSIGIO(); |
| 125 | assert(!sig_is_blocked(SIGIO)); |
| 126 | |
| 127 | /* BABA */ |
| 128 | OsBlockSIGIO(); |
| 129 | assert(sig_is_blocked(SIGIO)); |
| 130 | OsBlockSignals(); |
| 131 | assert(sig_is_blocked(SIGIO)); |
| 132 | OsReleaseSIGIO(); |
| 133 | assert(sig_is_blocked(SIGIO)); |
| 134 | OsReleaseSignals(); |
| 135 | assert(!sig_is_blocked(SIGIO)); |
| 136 | #endif |
| 137 | } |
| 138 | |
| 139 | static void block_sigio_test_nested(void) |
| 140 | { |
| 141 | #ifdef SIG_BLOCK |
| 142 | /* Check for bug releasing SIGIO during SIGIO signal handling. |
| 143 | test case: |
| 144 | raise signal |
| 145 | → in signal handler: |
| 146 | raise signal |
| 147 | OsBlockSignals() |
| 148 | OsReleaseSignals() |
| 149 | tail guard |
| 150 | tail guard must be hit. |
| 151 | */ |
| 152 | void (*old_handler)(int); |
| 153 | old_handler = signal(SIGIO, sighandler); |
| 154 | expect_signal = 1; |
| 155 | assert(raise(SIGIO) == 0); |
| 156 | assert(signal(SIGIO, old_handler) == sighandler); |
| 157 | #endif |
| 158 | } |
| 159 | |
| 160 | int |
| 161 | main(int argc, char **argv) |
| 162 | { |
| 163 | block_sigio_test(); |
| 164 | block_sigio_test_nested(); |
| 165 | return 0; |
| 166 | } |