plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright (c) International Business Machines Corp., 2001 |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See |
| 13 | * the GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
Wanlong Gao | 4548c6c | 2012-10-19 18:03:36 +0800 | [diff] [blame] | 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | /* |
| 21 | * Test Name: sigsuspend01 |
| 22 | * |
| 23 | * Test Description: |
| 24 | * Verify that sigsuspend() succeeds to change process's current signal |
| 25 | * mask with the specified signal mask and suspends the process execution |
| 26 | * until the delivery of a signal. |
| 27 | * |
| 28 | * Expected Result: |
| 29 | * sigsuspend() should return after the execution of signal catching |
| 30 | * function and the previous signal mask should be restored. |
| 31 | * |
| 32 | * Algorithm: |
| 33 | * Setup: |
| 34 | * Setup signal handling. |
| 35 | * Create temporary directory. |
| 36 | * Pause for SIGUSR1 if option specified. |
| 37 | * |
| 38 | * Test: |
| 39 | * Loop if the proper options are given. |
| 40 | * Execute system call |
| 41 | * Check return code, if system call failed (return=-1) |
| 42 | * Log the errno and Issue a FAIL message. |
| 43 | * Otherwise, |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 44 | * Verify the Functionality of system call |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 45 | * if successful, |
| 46 | * Issue Functionality-Pass message. |
| 47 | * Otherwise, |
| 48 | * Issue Functionality-Fail message. |
| 49 | * Cleanup: |
| 50 | * Print errno log and/or timing stats if options given |
| 51 | * Delete the temporary directory created. |
| 52 | * |
| 53 | * Usage: <for command-line> |
| 54 | * sigsuspend01 [-c n] [-e] [-f] [-i n] [-I x] [-p x] [-t] |
| 55 | * where, -c n : Run n copies concurrently. |
| 56 | * -e : Turn on errno logging. |
| 57 | * -f : Turn off functionality Testing. |
| 58 | * -i n : Execute test n times. |
| 59 | * -I x : Execute test for x seconds. |
| 60 | * -P x : Pause for x seconds between iterations. |
| 61 | * -t : Turn on syscall timing. |
| 62 | * |
| 63 | * History |
| 64 | * 07/2001 John George |
| 65 | * -Ported |
| 66 | * |
| 67 | * Restrictions: |
| 68 | * None. |
| 69 | */ |
| 70 | |
| 71 | #include <stdio.h> |
| 72 | #include <unistd.h> |
| 73 | #include <sys/types.h> |
| 74 | #include <errno.h> |
| 75 | #include <fcntl.h> |
| 76 | #include <string.h> |
| 77 | #include <signal.h> |
| 78 | #include <ucontext.h> |
| 79 | |
| 80 | #include "test.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 81 | |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 82 | char *TCID = "sigsuspend01"; |
| 83 | int TST_TOTAL = 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 84 | |
| 85 | struct sigaction sa_new; /* struct to hold signal info */ |
| 86 | sigset_t sigset; /* signal set to hold signal lists */ |
| 87 | sigset_t sigset1; |
| 88 | sigset_t sigset2; |
| 89 | |
| 90 | void setup(); /* Main setup function of test */ |
| 91 | void cleanup(); /* cleanup function for the test */ |
| 92 | void sig_handler(int sig); /* signal catching function */ |
| 93 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 94 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 95 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 96 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 97 | const char *msg; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 98 | |
Garrett Cooper | 45e285d | 2010-11-22 12:19:25 -0800 | [diff] [blame] | 99 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 100 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 101 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 102 | setup(); |
| 103 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 104 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 105 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 106 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 107 | |
| 108 | /* Set the alarm timer */ |
| 109 | alarm(5); |
| 110 | |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 111 | /* |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 112 | * Call sigsuspend() to replace current signal mask |
| 113 | * of the process and suspend process execution till |
| 114 | * receipt of a signal 'SIGALRM'. |
| 115 | */ |
| 116 | TEST(sigsuspend(&sigset)); |
| 117 | |
| 118 | /* Reset the alarm timer */ |
| 119 | alarm(0); |
| 120 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 121 | if ((TEST_RETURN == -1) && (TEST_ERRNO == EINTR)) { |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 122 | if (sigprocmask(SIG_UNBLOCK, 0, &sigset2) == -1) { |
| 123 | tst_resm(TFAIL, "sigprocmask() Failed " |
| 124 | "to get previous signal mask " |
| 125 | "of process"); |
| 126 | } else if (sigset2.__val[0] != sigset1.__val[0]) { |
| 127 | tst_resm(TFAIL, "sigsuspend failed to " |
| 128 | "preserve signal mask"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 129 | } else { |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 130 | tst_resm(TPASS, "Functionality of " |
| 131 | "sigsuspend() successful"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 132 | } |
| 133 | } else { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 134 | tst_resm(TFAIL | TTERRNO, |
subrata_modak | 923b23f | 2009-11-02 13:57:16 +0000 | [diff] [blame] | 135 | "sigsuspend() returned value %ld", |
| 136 | TEST_RETURN); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 137 | } |
| 138 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 139 | tst_count++; /* incr TEST_LOOP counter */ |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 140 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 141 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 142 | cleanup(); |
Garrett Cooper | 1e6f5a6 | 2010-12-19 09:58:10 -0800 | [diff] [blame] | 143 | tst_exit(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 144 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 145 | |
| 146 | /* |
| 147 | * void |
| 148 | * setup() - performs all ONE TIME setup for this test. |
| 149 | * Initialise signal set with the list that includes/excludes |
| 150 | * all system-defined signals. |
| 151 | * Set the signal handler to catch SIGALRM signal. |
| 152 | * Get the current signal mask of test process using sigprocmask(). |
| 153 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 154 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 155 | { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 156 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 157 | tst_sig(FORK, DEF_HANDLER, cleanup); |
| 158 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 159 | TEST_PAUSE; |
| 160 | |
| 161 | /* |
| 162 | * Initialise the signal sets with the list that |
| 163 | * excludes/includes all system-defined signals. |
| 164 | */ |
| 165 | if (sigemptyset(&sigset) == -1) { |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 166 | tst_brkm(TFAIL, cleanup, |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 167 | "sigemptyset() failed, errno=%d : %s", |
| 168 | errno, strerror(errno)); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 169 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 170 | if (sigfillset(&sigset2) == -1) { |
| 171 | tst_brkm(TFAIL, cleanup, |
| 172 | "sigfillset() failed, errno=%d : %s", |
| 173 | errno, strerror(errno)); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 174 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 175 | |
| 176 | /* Set the signal handler function to catch the signal */ |
| 177 | sa_new.sa_handler = sig_handler; |
| 178 | if (sigaction(SIGALRM, &sa_new, 0) == -1) { |
| 179 | tst_brkm(TFAIL, cleanup, |
| 180 | "sigaction() failed, errno=%d : %s", |
| 181 | errno, strerror(errno)); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 182 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 183 | |
| 184 | /* Read the test process's current signal mask. */ |
| 185 | if (sigprocmask(SIG_UNBLOCK, 0, &sigset1) == -1) { |
| 186 | tst_brkm(TFAIL, cleanup, |
| 187 | "sigprocmask() Failed, errno=%d : %s", |
| 188 | errno, strerror(errno)); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 189 | } |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 190 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 191 | |
| 192 | /* |
| 193 | * void |
| 194 | * sig_handler(int sig) - Signal catching function. |
| 195 | * This function gets executed when the signal SIGALRM is delivered |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 196 | * to the test process after the expiry of alarm time and the signal was |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 197 | * trapped by sigaction() to execute this function. |
| 198 | * |
| 199 | * This function simply returns without doing anything. |
| 200 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 201 | void sig_handler(int sig) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 202 | { |
| 203 | } |
| 204 | |
| 205 | /* |
| 206 | * void |
| 207 | * cleanup() - performs all ONE TIME cleanup for this test at |
| 208 | * completion or premature exit. |
| 209 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 210 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 211 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 212 | |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 213 | } |