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: sigalstack01 |
| 22 | * |
| 23 | * Test Description: |
| 24 | * Send a signal using the main stack. While executing the signal handler |
| 25 | * compare a variable's address lying on the main stack with the stack |
| 26 | * boundaries returned by sigaltstack(). |
| 27 | * |
| 28 | * Expected Result: |
| 29 | * sigaltstack() should succeed to get/set signal alternate stack context. |
| 30 | * |
| 31 | * Algorithm: |
| 32 | * Setup: |
| 33 | * Setup signal handling. |
| 34 | * Pause for SIGUSR1 if option specified. |
| 35 | * |
| 36 | * Test: |
| 37 | * Loop if the proper options are given. |
| 38 | * Execute system call |
| 39 | * Check return code, if system call failed (return=-1) |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 40 | * Log the errno and Issue a FAIL message. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 41 | * Otherwise, |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 42 | * Verify the Functionality of system call |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 43 | * if successful, |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 44 | * Issue Functionality-Pass message. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 45 | * Otherwise, |
| 46 | * Issue Functionality-Fail message. |
| 47 | * Cleanup: |
| 48 | * Print errno log and/or timing stats if options given |
| 49 | * |
| 50 | * Usage: <for command-line> |
| 51 | * sigaltstack01 [-c n] [-e] [-f] [-i n] [-I x] [-p x] [-t] |
| 52 | * where, -c n : Run n copies concurrently. |
| 53 | * -e : Turn on errno logging. |
| 54 | * -f : Turn off functionality Testing. |
| 55 | * -i n : Execute test n times. |
| 56 | * -I x : Execute test for x seconds. |
| 57 | * -P x : Pause for x seconds between iterations. |
| 58 | * -t : Turn on syscall timing. |
| 59 | * |
| 60 | * History |
| 61 | * 07/2001 John George |
| 62 | * -Ported |
| 63 | * |
| 64 | * Restrictions: |
| 65 | * This test should be run by 'super-user' (root) only and must run from |
| 66 | * shell which sets up for test. |
| 67 | * |
| 68 | */ |
| 69 | |
| 70 | #include <stdio.h> |
| 71 | #include <sys/types.h> |
| 72 | #include <unistd.h> |
| 73 | #include <signal.h> |
| 74 | #include <string.h> |
| 75 | #include <ucontext.h> |
| 76 | #include <errno.h> |
| 77 | |
| 78 | #include "test.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 79 | |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 80 | char *TCID = "sigaltstack01"; |
| 81 | int TST_TOTAL = 1; |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 82 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 83 | void *addr, *main_stk; /* address of main stack for signal */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 84 | int got_signal = 0; |
| 85 | pid_t my_pid; /* test process id */ |
| 86 | |
| 87 | stack_t sigstk, osigstk; /* signal stack storing struct. */ |
| 88 | struct sigaction act, oact; /* sigaction() struct. */ |
| 89 | |
Jan Stancek | eb3aa4e | 2014-02-25 14:42:58 +0100 | [diff] [blame] | 90 | void setup(void); /* Main setup function of test */ |
| 91 | void cleanup(void); /* cleanup function for the test */ |
| 92 | void sig_handler(int); /* signal catching function */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 93 | |
plars | 74948ad | 2002-11-14 16:16:14 +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 | 74948ad | 2002-11-14 16:16:14 +0000 | [diff] [blame] | 98 | void *alt_stk; /* address of alternate stack for signal */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 99 | |
Garrett Cooper | 45e285d | 2010-11-22 12:19:25 -0800 | [diff] [blame] | 100 | msg = parse_opts(ac, av, NULL, NULL); |
| 101 | if (msg != NULL) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 102 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 103 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 104 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 105 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 106 | setup(); |
| 107 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 108 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 109 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 110 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 111 | |
| 112 | /* Call sigaltstack() to set up an alternate stack */ |
| 113 | sigstk.ss_size = SIGSTKSZ; |
| 114 | sigstk.ss_flags = 0; |
| 115 | TEST(sigaltstack(&sigstk, &osigstk)); |
| 116 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 117 | if (TEST_RETURN == -1) { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 118 | tst_resm(TFAIL, |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 119 | "sigaltstack() Failed, errno=%d : %s", |
| 120 | TEST_ERRNO, strerror(TEST_ERRNO)); |
| 121 | } else { |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 122 | /* Set up the signal handler for 'SIGUSR1' */ |
| 123 | act.sa_flags = SA_ONSTACK; |
| 124 | act.sa_handler = (void (*)())sig_handler; |
| 125 | if ((sigaction(SIGUSR1, &act, &oact)) == -1) { |
| 126 | tst_brkm(TFAIL, cleanup, "sigaction() " |
| 127 | "fails to trap signal " |
| 128 | "delivered on alt. stack, " |
| 129 | "error=%d", errno); |
| 130 | } |
| 131 | |
| 132 | /* Deliver signal onto the alternate stack */ |
| 133 | kill(my_pid, SIGUSR1); |
| 134 | |
| 135 | /* wait till the signal arrives */ |
| 136 | while (!got_signal) ; |
| 137 | |
| 138 | got_signal = 0; |
| 139 | alt_stk = addr; |
| 140 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 141 | /* |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 142 | * First, |
| 143 | * Check that alt_stk is within the |
| 144 | * alternate stk boundaries |
| 145 | * |
| 146 | * Second, |
| 147 | * Check that main_stk is outside the |
| 148 | * alternate stk boundaries. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 149 | */ |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 150 | if ((alt_stk < sigstk.ss_sp) && |
| 151 | (alt_stk > (sigstk.ss_sp + SIGSTKSZ))) { |
| 152 | tst_resm(TFAIL, |
| 153 | "alt. stack is not within the " |
| 154 | "alternate stk boundaries"); |
| 155 | } else if ((main_stk >= sigstk.ss_sp) && |
| 156 | (main_stk <= |
| 157 | (sigstk.ss_sp + SIGSTKSZ))) { |
| 158 | tst_resm(TFAIL, |
| 159 | "main stk. not outside the " |
| 160 | "alt. stack boundaries"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 161 | } else { |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 162 | tst_resm(TPASS, |
| 163 | "Functionality of " |
| 164 | "sigaltstack() successful"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 165 | } |
| 166 | } |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 167 | tst_count++; /* incr. TEST_LOOP counter */ |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 168 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 169 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 170 | cleanup(); |
Garrett Cooper | 7d0a4a5 | 2010-12-16 10:05:08 -0800 | [diff] [blame] | 171 | tst_exit(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 172 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 173 | |
| 174 | /* |
| 175 | * void |
| 176 | * setup() - performs all ONE TIME setup for this test. |
| 177 | * Capture SIGUSR1 on the main stack. |
| 178 | * send the signal 'SIGUSER1' to the process. |
| 179 | * wait till the signal arrives. |
| 180 | * Allocate memory for the alternative stack. |
| 181 | */ |
Jan Stancek | eb3aa4e | 2014-02-25 14:42:58 +0100 | [diff] [blame] | 182 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 183 | { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 184 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 185 | tst_sig(FORK, DEF_HANDLER, cleanup); |
| 186 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 187 | TEST_PAUSE; |
| 188 | |
| 189 | /* Get the process id of test process */ |
| 190 | my_pid = getpid(); |
| 191 | |
| 192 | /* Capture SIGUSR1 on the main stack */ |
Jan Stancek | eb3aa4e | 2014-02-25 14:42:58 +0100 | [diff] [blame] | 193 | act.sa_handler = (void (*)(int))sig_handler; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 194 | if ((sigaction(SIGUSR1, &act, &oact)) == -1) { |
| 195 | tst_brkm(TFAIL, cleanup, |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 196 | "sigaction() fails in setup, errno=%d", errno); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 197 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 198 | |
| 199 | /* Send the signal to the test process */ |
| 200 | kill(my_pid, SIGUSR1); |
| 201 | |
| 202 | /* Wait till the signal arrives */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 203 | while (!got_signal) ; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 204 | |
| 205 | got_signal = 0; |
| 206 | main_stk = addr; |
| 207 | |
| 208 | /* Allocate memory for the alternate stack */ |
Cyril Hrubis | d218f34 | 2014-09-23 13:14:56 +0200 | [diff] [blame] | 209 | if ((sigstk.ss_sp = malloc(SIGSTKSZ)) == NULL) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 210 | tst_brkm(TFAIL, cleanup, |
| 211 | "could not allocate memory for the alternate stack"); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 212 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | /* |
| 216 | * void |
| 217 | * sig_handler() - signal catching function. |
| 218 | * This functions is called when the signal 'SIGUSR1' is delivered to |
| 219 | * the test process and trapped by sigaction(). |
| 220 | * |
| 221 | * This function updates 'addr' variable and sets got_signal value. |
| 222 | */ |
Jan Stancek | eb3aa4e | 2014-02-25 14:42:58 +0100 | [diff] [blame] | 223 | void sig_handler(int n) |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 224 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 225 | int i; |
| 226 | |
Jan Stancek | eb3aa4e | 2014-02-25 14:42:58 +0100 | [diff] [blame] | 227 | (void) n; |
plars | 74948ad | 2002-11-14 16:16:14 +0000 | [diff] [blame] | 228 | addr = &i; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 229 | got_signal = 1; |
| 230 | } |
| 231 | |
| 232 | /* |
| 233 | * void |
| 234 | * cleanup() - performs all ONE TIME cleanup for this test at |
| 235 | * completion or premature exit. |
| 236 | * Free the memory allocated for alternate stack. |
| 237 | */ |
Jan Stancek | eb3aa4e | 2014-02-25 14:42:58 +0100 | [diff] [blame] | 238 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 239 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 240 | |
| 241 | free(sigstk.ss_sp); |
| 242 | |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 243 | } |