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: sigalstack02 |
| 22 | * |
| 23 | * Test Description: |
| 24 | * Verify that, |
| 25 | * 1. sigaltstack() fails and sets errno to EINVAL when "ss_flags" field |
| 26 | * pointed to by 'ss' contains invalid flags. |
| 27 | * 2. sigaltstack() fails and sets errno to ENOMEM when the size of alternate |
| 28 | * stack area is less than MINSIGSTKSZ. |
| 29 | * |
| 30 | * Expected Result: |
| 31 | * sigaltstack() should fail with return value -1 and set expected errno. |
| 32 | * |
| 33 | * Algorithm: |
| 34 | * Setup: |
| 35 | * Setup signal handling. |
| 36 | * Create temporary directory. |
| 37 | * Pause for SIGUSR1 if option specified. |
| 38 | * |
| 39 | * Test: |
| 40 | * Loop if the proper options are given. |
| 41 | * Execute system call |
| 42 | * Check return code, if system call failed (return=-1) |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 43 | * if errno set == expected errno |
| 44 | * Issue sys call fails with expected return value and errno. |
| 45 | * Otherwise, |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 46 | * Issue sys call fails with unexpected errno. |
| 47 | * Otherwise, |
| 48 | * Issue sys call returns unexpected value. |
| 49 | * |
| 50 | * Cleanup: |
| 51 | * Print errno log and/or timing stats if options given |
| 52 | * Delete the temporary directory(s)/file(s) created. |
| 53 | * |
| 54 | * Usage: <for command-line> |
| 55 | * sigaltstack02 [-c n] [-e] [-f] [-i n] [-I x] [-p x] [-t] |
| 56 | * where, -c n : Run n copies concurrently. |
| 57 | * -e : Turn on errno logging. |
| 58 | * -f : Turn off functionality Testing. |
| 59 | * -i n : Execute test n times. |
| 60 | * -I x : Execute test for x seconds. |
| 61 | * -P x : Pause for x seconds between iterations. |
| 62 | * -t : Turn on syscall timing. |
| 63 | * |
| 64 | * History |
| 65 | * 07/2001 John George |
| 66 | * -Ported |
| 67 | * |
| 68 | * Restrictions: |
| 69 | * This test should be executed by super-user (root) only. |
| 70 | */ |
| 71 | |
| 72 | #include <stdio.h> |
| 73 | #include <sys/types.h> |
| 74 | #include <unistd.h> |
| 75 | #include <signal.h> |
| 76 | #include <string.h> |
| 77 | #include <ucontext.h> |
| 78 | #include <errno.h> |
| 79 | |
| 80 | #include "test.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 81 | |
| 82 | #define INVAL_FLAGS 9999 |
| 83 | |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 84 | char *TCID = "sigaltstack02"; |
| 85 | int TST_TOTAL = 2; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 86 | |
| 87 | stack_t sigstk; /* signal stack storing struct. */ |
| 88 | |
Jan Stancek | eb3aa4e | 2014-02-25 14:42:58 +0100 | [diff] [blame] | 89 | void setup(void); /* Main setup function of test */ |
| 90 | void cleanup(void); /* cleanup function for the test */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 91 | |
| 92 | struct test_case_t { /* test case struct. to hold diff. test.conds */ |
| 93 | int flag; |
| 94 | int size; |
| 95 | char *desc; |
| 96 | int exp_errno; |
| 97 | } Test_cases[] = { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 98 | { |
| 99 | INVAL_FLAGS, SIGSTKSZ, "Invalid Flag value", EINVAL}, |
Jan Stancek | 45e36e4 | 2014-01-13 10:32:59 +0100 | [diff] [blame] | 100 | /* use value low enough for all kernel versions |
| 101 | * avoid using MINSIGSTKSZ defined by glibc as it could be different |
| 102 | * from the one in kernel ABI |
| 103 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 104 | { |
Jan Stancek | 45e36e4 | 2014-01-13 10:32:59 +0100 | [diff] [blame] | 105 | 0, (2048 - 1), "alternate stack is < MINSIGSTKSZ", ENOMEM}, |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 106 | { |
| 107 | 0, 0, NULL, 0} |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 108 | }; |
| 109 | |
plars | 74948ad | 2002-11-14 16:16:14 +0000 | [diff] [blame] | 110 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 111 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 112 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 113 | const char *msg; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 114 | char *test_desc; /* test specific error message */ |
| 115 | int ind; /* counter to test different test conditions */ |
| 116 | |
Garrett Cooper | 45e285d | 2010-11-22 12:19:25 -0800 | [diff] [blame] | 117 | msg = parse_opts(ac, av, NULL, NULL); |
| 118 | if (msg != NULL) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 119 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 120 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 121 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 122 | setup(); |
| 123 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 124 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 125 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 126 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 127 | |
| 128 | for (ind = 0; Test_cases[ind].desc != NULL; ind++) { |
| 129 | sigstk.ss_size = Test_cases[ind].size; |
| 130 | sigstk.ss_flags = Test_cases[ind].flag; |
| 131 | test_desc = Test_cases[ind].desc; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 132 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 133 | /* Verify sigaltstack() fails and sets errno */ |
Cyril Hrubis | 4e2bab8 | 2014-09-24 16:34:35 +0200 | [diff] [blame] | 134 | TEST(sigaltstack(&sigstk, NULL)); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 135 | |
| 136 | /* Check return code from sigaltstack() */ |
| 137 | if (TEST_RETURN == -1) { |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 138 | if (TEST_ERRNO == |
| 139 | Test_cases[ind].exp_errno) { |
| 140 | tst_resm(TPASS, "stgaltstack() " |
| 141 | "fails, %s, errno:%d", |
| 142 | test_desc, TEST_ERRNO); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 143 | } else { |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 144 | tst_resm(TFAIL, "sigaltstack() " |
| 145 | "fails, %s, errno:%d, " |
| 146 | "expected errno:%d", |
| 147 | test_desc, TEST_ERRNO, |
| 148 | Test_cases |
| 149 | [ind].exp_errno); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 150 | } |
| 151 | } else { |
subrata_modak | 923b23f | 2009-11-02 13:57:16 +0000 | [diff] [blame] | 152 | tst_resm(TFAIL, "sigaltstack() returned %ld, " |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 153 | "expected -1, errno:%d", TEST_RETURN, |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 154 | Test_cases[ind].exp_errno); |
| 155 | } |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 156 | } |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 157 | tst_count++; /* incr. TEST_LOOP counter */ |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 158 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 159 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 160 | cleanup(); |
Garrett Cooper | 7d0a4a5 | 2010-12-16 10:05:08 -0800 | [diff] [blame] | 161 | tst_exit(); |
robbiew | fa451a1 | 2003-03-27 20:52:36 +0000 | [diff] [blame] | 162 | |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 163 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 164 | |
| 165 | /* |
| 166 | * void |
| 167 | * setup() - performs all ONE TIME setup for this test. |
| 168 | * Allocate memory for the alternative stack. |
| 169 | */ |
Jan Stancek | eb3aa4e | 2014-02-25 14:42:58 +0100 | [diff] [blame] | 170 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 171 | { |
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 | tst_sig(FORK, DEF_HANDLER, cleanup); |
| 174 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 175 | TEST_PAUSE; |
| 176 | |
| 177 | /* Allocate memory for the alternate stack */ |
Cyril Hrubis | d218f34 | 2014-09-23 13:14:56 +0200 | [diff] [blame] | 178 | if ((sigstk.ss_sp = malloc(SIGSTKSZ)) == NULL) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 179 | tst_brkm(TFAIL, cleanup, |
| 180 | "could not allocate memory for the alternate stack"); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 181 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | /* |
| 185 | * void |
| 186 | * cleanup() - performs all ONE TIME cleanup for this test at |
| 187 | * completion or premature exit. |
| 188 | * Free the memory allocated for alternate stack. |
| 189 | */ |
Jan Stancek | eb3aa4e | 2014-02-25 14:42:58 +0100 | [diff] [blame] | 190 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 191 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 192 | |
| 193 | free(sigstk.ss_sp); |
| 194 | |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 195 | } |