blob: 11ed794f3eba6c8c60982c46a5894e1cd6c3b07d [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 * 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.
robbiew783f9652001-09-18 20:38:40 +000031 * 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.
plars865695b2001-08-27 22:15:12 +000033 *
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 */
plarsa9933882003-07-08 18:36:59 +000051#define DEBUG 0
plars865695b2001-08-27 22:15:12 +000052#include <stdio.h>
53#include <stdlib.h>
subrata_modak4bb656a2009-02-26 12:02:09 +000054#include <unistd.h>
plars865695b2001-08-27 22:15:12 +000055#include <signal.h>
56#include <errno.h>
57#include "test.h"
plars865695b2001-08-27 22:15:12 +000058
59#define SIGBAD 9999
60
61void setup();
62void cleanup();
63
64char *TCID = "sigaction02";
65int TST_TOTAL = 1;
plars865695b2001-08-27 22:15:12 +000066
67volatile sig_atomic_t testcase_no;
68
plars865695b2001-08-27 22:15:12 +000069/*
70 * handler()
71 * A dummy signal handler for attempting to catch signals.
72 */
subrata_modak56207ce2009-03-23 13:35:39 +000073void handler(int sig)
plars865695b2001-08-27 22:15:12 +000074{
plarsa9933882003-07-08 18:36:59 +000075 if (DEBUG)
76 tst_resm(TINFO, "Inside signal handler. Got signal: %d", sig);
plars865695b2001-08-27 22:15:12 +000077 return;
78}
79
80/*
81 * set_handler()
82 * Establish a signal handler for "sig" with the specified flags and
83 * signal to mask while the handler executes.
84 * Returns
85 * 0 on success, errno on failure
86 */
subrata_modak56207ce2009-03-23 13:35:39 +000087int set_handler(int sig, int sig_to_mask, int flag)
plars865695b2001-08-27 22:15:12 +000088{
89 struct sigaction sa;
90 int err;
91
92 if (flag == 0) {
93 sa.sa_sigaction = (void *)handler;
94 sa.sa_flags = SA_NOMASK;
95 sigemptyset(&sa.sa_mask);
96 sigaddset(&sa.sa_mask, sig_to_mask);
97 err = sigaction(sig, &sa, NULL);
98 } else if (flag == 1) {
99 err = sigaction(sig, (void *)-1, NULL);
100 } else if (flag == 2) {
101 err = sigaction(sig, NULL, (void *)-1);
Garrett Cooper67c64ff2010-12-19 08:44:03 -0800102 } else
103 err = -1;
plars865695b2001-08-27 22:15:12 +0000104
Garrett Cooper67c64ff2010-12-19 08:44:03 -0800105 if (err == 0)
plars865695b2001-08-27 22:15:12 +0000106 return 0;
Garrett Cooper67c64ff2010-12-19 08:44:03 -0800107 else
plars865695b2001-08-27 22:15:12 +0000108 return errno;
plars865695b2001-08-27 22:15:12 +0000109}
110
subrata_modak56207ce2009-03-23 13:35:39 +0000111int main(int ac, char **av)
robbiewfa451a12003-03-27 20:52:36 +0000112{
Cyril Hrubis0b9589f2014-05-27 17:40:33 +0200113 const char *msg; /* message got from parse_opts */
robbiewfa451a12003-03-27 20:52:36 +0000114
115 int ret;
116
Garrett Cooper45e285d2010-11-22 12:19:25 -0800117 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) {
Garrett Cooper60fa8012010-11-22 13:50:58 -0800118 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
robbiewfa451a12003-03-27 20:52:36 +0000119 }
robbiewfa451a12003-03-27 20:52:36 +0000120//test1:
121 testcase_no = 1;
122
plarsa9933882003-07-08 18:36:59 +0000123 if (DEBUG)
subrata_modak4bb656a2009-02-26 12:02:09 +0000124 tst_resm(TINFO, "Enter test %d: set handler for SIGKILL",
subrata_modak56207ce2009-03-23 13:35:39 +0000125 testcase_no);
robbiewfa451a12003-03-27 20:52:36 +0000126 if ((ret = set_handler(SIGKILL, 0, 0)) == 0) {
127 tst_resm(TFAIL, "sigaction() succeeded, should have failed");
128 }
129 if (ret != EINVAL) {
130 tst_resm(TFAIL, "sigaction set incorrect errno. Expected "
131 "EINVAL, got: %d", ret);
132 } else {
133 tst_resm(TPASS, "call failed with expected EINVAL error");
134 }
135
136//test2:
137 testcase_no++;
138
plarsa9933882003-07-08 18:36:59 +0000139 if (DEBUG)
subrata_modak4bb656a2009-02-26 12:02:09 +0000140 tst_resm(TINFO, "Enter test %d: set handler for SIGSTOP",
subrata_modak56207ce2009-03-23 13:35:39 +0000141 testcase_no);
robbiewfa451a12003-03-27 20:52:36 +0000142 if ((ret = set_handler(SIGSTOP, 0, 0)) == 0) {
143 tst_resm(TFAIL, "sigaction() succeeded, should have failed");
144 }
145 if (ret != EINVAL) {
146 tst_resm(TFAIL, "sigaction set incorrect errno. Expected "
147 "EINVAL, got: %d", ret);
148 } else {
149 tst_resm(TPASS, "call failed with expected EINVAL error");
150 }
151
152//test3:
153 testcase_no++;
plarsa9933882003-07-08 18:36:59 +0000154 if (DEBUG)
155 tst_resm(TINFO, "Enter test %d: set handler for bad "
subrata_modak56207ce2009-03-23 13:35:39 +0000156 "signal number", testcase_no);
robbiewfa451a12003-03-27 20:52:36 +0000157 if ((ret = set_handler(SIGBAD, 0, 0)) == 0) {
158 tst_resm(TFAIL, "sigaction() succeeded, should have failed");
159 }
160 if (ret != EINVAL) {
161 tst_resm(TFAIL, "sigaction set incorrect errno. Expected "
162 "EINVAL, got: %d", ret);
163 } else {
164 tst_resm(TPASS, "call failed with expected EINVAL error");
165 }
166
167#ifndef GLIBC_SIGACTION_BUG
168
169//test4:
170 testcase_no++;
plarsa9933882003-07-08 18:36:59 +0000171 if (DEBUG)
172 tst_resm(TINFO, "Enter test %d: set handler with "
subrata_modak56207ce2009-03-23 13:35:39 +0000173 "bad \"act\" param", testcase_no);
robbiewfa451a12003-03-27 20:52:36 +0000174 if ((ret = set_handler(SIGUSR1, 0, 1)) == 0) {
175 tst_resm(TFAIL, "sigaction() succeeded, should have failed");
176 }
177 if (ret != EFAULT) {
178 tst_resm(TFAIL, "sigaction set incorrect errno. Expected "
179 "EFAULT, got: %d", ret);
180 } else {
181 tst_resm(TPASS, "call failed with expected EFAULT error");
182 }
183
184//test5:
185 testcase_no++;
plarsa9933882003-07-08 18:36:59 +0000186 if (DEBUG)
187 tst_resm(TINFO, "Enter test %d: set handler with "
subrata_modak56207ce2009-03-23 13:35:39 +0000188 "bad \"oact\" param", testcase_no);
robbiewfa451a12003-03-27 20:52:36 +0000189 if ((ret = set_handler(SIGUSR1, 0, 2)) == 0) {
190 tst_resm(TFAIL, "sigaction() succeeded, should have failed");
191 }
192 if (ret != EFAULT) {
193 tst_resm(TFAIL, "sigaction set incorrect errno. Expected "
194 "EFAULT, got: %d", ret);
195 } else {
196 tst_resm(TPASS, "call failed with expected EFAULT error");
197 }
subrata_modak56207ce2009-03-23 13:35:39 +0000198#endif /* GLIBC_SIGACTION_BUG */
robbiewfa451a12003-03-27 20:52:36 +0000199
200 tst_exit();
201
Chris Dearmanec6edca2012-10-17 19:54:01 -0700202}