blob: 74d8a625b72de51b17732dbc6f205d43dca2a014 [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
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 Gao4548c6c2012-10-19 18:03:36 +080017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
plars865695b2001-08-27 22:15:12 +000018 */
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_modak56207ce2009-03-23 13:35:39 +000043 * if errno set == expected errno
44 * Issue sys call fails with expected return value and errno.
45 * Otherwise,
plars865695b2001-08-27 22:15:12 +000046 * 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"
plars865695b2001-08-27 22:15:12 +000081
82#define INVAL_FLAGS 9999
83
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020084char *TCID = "sigaltstack02";
85int TST_TOTAL = 2;
plars865695b2001-08-27 22:15:12 +000086
87stack_t sigstk; /* signal stack storing struct. */
88
Jan Stancekeb3aa4e2014-02-25 14:42:58 +010089void setup(void); /* Main setup function of test */
90void cleanup(void); /* cleanup function for the test */
plars865695b2001-08-27 22:15:12 +000091
92struct 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_modak56207ce2009-03-23 13:35:39 +000098 {
99 INVAL_FLAGS, SIGSTKSZ, "Invalid Flag value", EINVAL},
Jan Stancek45e36e42014-01-13 10:32:59 +0100100 /* 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_modak56207ce2009-03-23 13:35:39 +0000104 {
Jan Stancek45e36e42014-01-13 10:32:59 +0100105 0, (2048 - 1), "alternate stack is < MINSIGSTKSZ", ENOMEM},
subrata_modak56207ce2009-03-23 13:35:39 +0000106 {
107 0, 0, NULL, 0}
plars865695b2001-08-27 22:15:12 +0000108};
109
plars74948ad2002-11-14 16:16:14 +0000110int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +0000111{
Cyril Hrubis89af32a2012-10-24 16:39:11 +0200112 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +0200113 const char *msg;
plars865695b2001-08-27 22:15:12 +0000114 char *test_desc; /* test specific error message */
115 int ind; /* counter to test different test conditions */
116
Garrett Cooper45e285d2010-11-22 12:19:25 -0800117 msg = parse_opts(ac, av, NULL, NULL);
118 if (msg != NULL) {
plars865695b2001-08-27 22:15:12 +0000119 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800120 }
plars865695b2001-08-27 22:15:12 +0000121
plars865695b2001-08-27 22:15:12 +0000122 setup();
123
plars865695b2001-08-27 22:15:12 +0000124 for (lc = 0; TEST_LOOPING(lc); lc++) {
Garrett Cooper2c282152010-12-16 00:55:50 -0800125
Caspar Zhangd59a6592013-03-07 14:59:12 +0800126 tst_count = 0;
plars865695b2001-08-27 22:15:12 +0000127
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_modak56207ce2009-03-23 13:35:39 +0000132
plars865695b2001-08-27 22:15:12 +0000133 /* Verify sigaltstack() fails and sets errno */
Cyril Hrubis4e2bab82014-09-24 16:34:35 +0200134 TEST(sigaltstack(&sigstk, NULL));
plars865695b2001-08-27 22:15:12 +0000135
136 /* Check return code from sigaltstack() */
137 if (TEST_RETURN == -1) {
Cyril Hrubise38b9612014-06-02 17:20:57 +0200138 if (TEST_ERRNO ==
139 Test_cases[ind].exp_errno) {
140 tst_resm(TPASS, "stgaltstack() "
141 "fails, %s, errno:%d",
142 test_desc, TEST_ERRNO);
plars865695b2001-08-27 22:15:12 +0000143 } else {
Cyril Hrubise38b9612014-06-02 17:20:57 +0200144 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);
plars865695b2001-08-27 22:15:12 +0000150 }
151 } else {
subrata_modak923b23f2009-11-02 13:57:16 +0000152 tst_resm(TFAIL, "sigaltstack() returned %ld, "
subrata_modak56207ce2009-03-23 13:35:39 +0000153 "expected -1, errno:%d", TEST_RETURN,
plars865695b2001-08-27 22:15:12 +0000154 Test_cases[ind].exp_errno);
155 }
Garrett Cooper2c282152010-12-16 00:55:50 -0800156 }
Caspar Zhangd59a6592013-03-07 14:59:12 +0800157 tst_count++; /* incr. TEST_LOOP counter */
Garrett Cooper2c282152010-12-16 00:55:50 -0800158 }
plars865695b2001-08-27 22:15:12 +0000159
plars865695b2001-08-27 22:15:12 +0000160 cleanup();
Garrett Cooper7d0a4a52010-12-16 10:05:08 -0800161 tst_exit();
robbiewfa451a12003-03-27 20:52:36 +0000162
Garrett Cooper2c282152010-12-16 00:55:50 -0800163}
plars865695b2001-08-27 22:15:12 +0000164
165/*
166 * void
167 * setup() - performs all ONE TIME setup for this test.
168 * Allocate memory for the alternative stack.
169 */
Jan Stancekeb3aa4e2014-02-25 14:42:58 +0100170void setup(void)
plars865695b2001-08-27 22:15:12 +0000171{
Garrett Cooper2c282152010-12-16 00:55:50 -0800172
plars865695b2001-08-27 22:15:12 +0000173 tst_sig(FORK, DEF_HANDLER, cleanup);
174
plars865695b2001-08-27 22:15:12 +0000175 TEST_PAUSE;
176
177 /* Allocate memory for the alternate stack */
Cyril Hrubisd218f342014-09-23 13:14:56 +0200178 if ((sigstk.ss_sp = malloc(SIGSTKSZ)) == NULL) {
plars865695b2001-08-27 22:15:12 +0000179 tst_brkm(TFAIL, cleanup,
180 "could not allocate memory for the alternate stack");
Wanlong Gao354ebb42012-12-07 10:10:04 +0800181 }
plars865695b2001-08-27 22:15:12 +0000182}
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 Stancekeb3aa4e2014-02-25 14:42:58 +0100190void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000191{
plars865695b2001-08-27 22:15:12 +0000192
193 free(sigstk.ss_sp);
194
Chris Dearmanec6edca2012-10-17 19:54:01 -0700195}