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 |
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 | */ |
| 19 | |
| 20 | /* |
| 21 | * NAME |
| 22 | * sigaction02.c |
| 23 | * |
| 24 | * DESCRIPTION |
| 25 | * Testcase to check the basic errnos set by the sigaction(2) syscall. |
| 26 | * |
| 27 | * ALGORITHM |
| 28 | * 1. Pass an invalid signal as the "sig" parameter, and expect EINVAL. |
| 29 | * 2. Attempt to catch the SIGKILL, and expect EINVAL. |
| 30 | * 3. Attempt to catch the SIGSTOP, and expect EINVAL. |
robbiew | 783f965 | 2001-09-18 20:38:40 +0000 | [diff] [blame] | 31 | * 4. Pass an invalid address as the "act" parameter, expect an EFAULT. |
| 32 | * 5. Pass an invalid address as the "oact" parameter, and expect EFAULT. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 33 | * |
| 34 | * USAGE |
| 35 | * sigaction02 |
| 36 | * |
| 37 | * HISTORY |
| 38 | * 07/2001 Ported by Wayne Boyer |
| 39 | * |
| 40 | * RESTRICTIONS |
| 41 | * Tests #4 and #5 will fail as long as the glibc implementation |
| 42 | * of sigaction() is not fixed. The glibc wrapper around of sigaction() |
| 43 | * doesn't handle the invalid addresses of the "act" and "oact" parameters |
| 44 | * correctly. If an invalid address is passed, glibc dumps core. |
| 45 | * Temporarily, tests 4 and 5 are put inside "#ifdef GLIBC_SIGACTION_BUG" |
| 46 | * in order to skip these tests. This should be removed from the Makefile |
| 47 | * and this program when the glibc bug gets fixed. |
| 48 | * |
| 49 | * This test doesn't follow the correct LTP format - PLEASE FIX! |
| 50 | */ |
plars | a993388 | 2003-07-08 18:36:59 +0000 | [diff] [blame] | 51 | #define DEBUG 0 |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 52 | #include <stdio.h> |
| 53 | #include <stdlib.h> |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 54 | #include <unistd.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 55 | #include <signal.h> |
| 56 | #include <errno.h> |
| 57 | #include "test.h" |
| 58 | #include "usctest.h" |
| 59 | |
| 60 | #define SIGBAD 9999 |
| 61 | |
| 62 | void setup(); |
| 63 | void cleanup(); |
| 64 | |
| 65 | char *TCID = "sigaction02"; |
| 66 | int TST_TOTAL = 1; |
| 67 | extern int Tst_count; |
| 68 | |
| 69 | volatile sig_atomic_t testcase_no; |
| 70 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 71 | /* |
| 72 | * handler() |
| 73 | * A dummy signal handler for attempting to catch signals. |
| 74 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 75 | void handler(int sig) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 76 | { |
plars | a993388 | 2003-07-08 18:36:59 +0000 | [diff] [blame] | 77 | if (DEBUG) |
| 78 | tst_resm(TINFO, "Inside signal handler. Got signal: %d", sig); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 79 | return; |
| 80 | } |
| 81 | |
| 82 | /* |
| 83 | * set_handler() |
| 84 | * Establish a signal handler for "sig" with the specified flags and |
| 85 | * signal to mask while the handler executes. |
| 86 | * Returns |
| 87 | * 0 on success, errno on failure |
| 88 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 89 | int set_handler(int sig, int sig_to_mask, int flag) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 90 | { |
| 91 | struct sigaction sa; |
| 92 | int err; |
| 93 | |
| 94 | if (flag == 0) { |
| 95 | sa.sa_sigaction = (void *)handler; |
| 96 | sa.sa_flags = SA_NOMASK; |
| 97 | sigemptyset(&sa.sa_mask); |
| 98 | sigaddset(&sa.sa_mask, sig_to_mask); |
| 99 | err = sigaction(sig, &sa, NULL); |
| 100 | } else if (flag == 1) { |
| 101 | err = sigaction(sig, (void *)-1, NULL); |
| 102 | } else if (flag == 2) { |
| 103 | err = sigaction(sig, NULL, (void *)-1); |
| 104 | } |
| 105 | |
| 106 | if (err == 0) { |
| 107 | return 0; |
| 108 | } else { |
| 109 | return errno; |
| 110 | } |
| 111 | } |
| 112 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 113 | int main(int ac, char **av) |
robbiew | fa451a1 | 2003-03-27 20:52:36 +0000 | [diff] [blame] | 114 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 115 | char *msg; /* message got from parse_opts */ |
robbiew | fa451a1 | 2003-03-27 20:52:36 +0000 | [diff] [blame] | 116 | |
| 117 | int ret; |
| 118 | |
Garrett Cooper | 45e285d | 2010-11-22 12:19:25 -0800 | [diff] [blame] | 119 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) { |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame^] | 120 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
robbiew | fa451a1 | 2003-03-27 20:52:36 +0000 | [diff] [blame] | 121 | } |
robbiew | fa451a1 | 2003-03-27 20:52:36 +0000 | [diff] [blame] | 122 | //test1: |
| 123 | testcase_no = 1; |
| 124 | |
plars | a993388 | 2003-07-08 18:36:59 +0000 | [diff] [blame] | 125 | if (DEBUG) |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 126 | tst_resm(TINFO, "Enter test %d: set handler for SIGKILL", |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 127 | testcase_no); |
robbiew | fa451a1 | 2003-03-27 20:52:36 +0000 | [diff] [blame] | 128 | if ((ret = set_handler(SIGKILL, 0, 0)) == 0) { |
| 129 | tst_resm(TFAIL, "sigaction() succeeded, should have failed"); |
| 130 | } |
| 131 | if (ret != EINVAL) { |
| 132 | tst_resm(TFAIL, "sigaction set incorrect errno. Expected " |
| 133 | "EINVAL, got: %d", ret); |
| 134 | } else { |
| 135 | tst_resm(TPASS, "call failed with expected EINVAL error"); |
| 136 | } |
| 137 | |
| 138 | //test2: |
| 139 | testcase_no++; |
| 140 | |
plars | a993388 | 2003-07-08 18:36:59 +0000 | [diff] [blame] | 141 | if (DEBUG) |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 142 | tst_resm(TINFO, "Enter test %d: set handler for SIGSTOP", |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 143 | testcase_no); |
robbiew | fa451a1 | 2003-03-27 20:52:36 +0000 | [diff] [blame] | 144 | if ((ret = set_handler(SIGSTOP, 0, 0)) == 0) { |
| 145 | tst_resm(TFAIL, "sigaction() succeeded, should have failed"); |
| 146 | } |
| 147 | if (ret != EINVAL) { |
| 148 | tst_resm(TFAIL, "sigaction set incorrect errno. Expected " |
| 149 | "EINVAL, got: %d", ret); |
| 150 | } else { |
| 151 | tst_resm(TPASS, "call failed with expected EINVAL error"); |
| 152 | } |
| 153 | |
| 154 | //test3: |
| 155 | testcase_no++; |
plars | a993388 | 2003-07-08 18:36:59 +0000 | [diff] [blame] | 156 | if (DEBUG) |
| 157 | tst_resm(TINFO, "Enter test %d: set handler for bad " |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 158 | "signal number", testcase_no); |
robbiew | fa451a1 | 2003-03-27 20:52:36 +0000 | [diff] [blame] | 159 | if ((ret = set_handler(SIGBAD, 0, 0)) == 0) { |
| 160 | tst_resm(TFAIL, "sigaction() succeeded, should have failed"); |
| 161 | } |
| 162 | if (ret != EINVAL) { |
| 163 | tst_resm(TFAIL, "sigaction set incorrect errno. Expected " |
| 164 | "EINVAL, got: %d", ret); |
| 165 | } else { |
| 166 | tst_resm(TPASS, "call failed with expected EINVAL error"); |
| 167 | } |
| 168 | |
| 169 | #ifndef GLIBC_SIGACTION_BUG |
| 170 | |
| 171 | //test4: |
| 172 | testcase_no++; |
plars | a993388 | 2003-07-08 18:36:59 +0000 | [diff] [blame] | 173 | if (DEBUG) |
| 174 | tst_resm(TINFO, "Enter test %d: set handler with " |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 175 | "bad \"act\" param", testcase_no); |
robbiew | fa451a1 | 2003-03-27 20:52:36 +0000 | [diff] [blame] | 176 | if ((ret = set_handler(SIGUSR1, 0, 1)) == 0) { |
| 177 | tst_resm(TFAIL, "sigaction() succeeded, should have failed"); |
| 178 | } |
| 179 | if (ret != EFAULT) { |
| 180 | tst_resm(TFAIL, "sigaction set incorrect errno. Expected " |
| 181 | "EFAULT, got: %d", ret); |
| 182 | } else { |
| 183 | tst_resm(TPASS, "call failed with expected EFAULT error"); |
| 184 | } |
| 185 | |
| 186 | //test5: |
| 187 | testcase_no++; |
plars | a993388 | 2003-07-08 18:36:59 +0000 | [diff] [blame] | 188 | if (DEBUG) |
| 189 | tst_resm(TINFO, "Enter test %d: set handler with " |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 190 | "bad \"oact\" param", testcase_no); |
robbiew | fa451a1 | 2003-03-27 20:52:36 +0000 | [diff] [blame] | 191 | if ((ret = set_handler(SIGUSR1, 0, 2)) == 0) { |
| 192 | tst_resm(TFAIL, "sigaction() succeeded, should have failed"); |
| 193 | } |
| 194 | if (ret != EFAULT) { |
| 195 | tst_resm(TFAIL, "sigaction set incorrect errno. Expected " |
| 196 | "EFAULT, got: %d", ret); |
| 197 | } else { |
| 198 | tst_resm(TPASS, "call failed with expected EFAULT error"); |
| 199 | } |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 200 | #endif /* GLIBC_SIGACTION_BUG */ |
robbiew | fa451a1 | 2003-03-27 20:52:36 +0000 | [diff] [blame] | 201 | |
| 202 | tst_exit(); |
| 203 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 204 | return 0; |
robbiew | fa451a1 | 2003-03-27 20:52:36 +0000 | [diff] [blame] | 205 | |
| 206 | } |