blob: ba05d0624daa7e1c9d88ce6d5defd638ae04d82c [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
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.
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"
58#include "usctest.h"
59
60#define SIGBAD 9999
61
62void setup();
63void cleanup();
64
65char *TCID = "sigaction02";
66int TST_TOTAL = 1;
67extern int Tst_count;
68
69volatile sig_atomic_t testcase_no;
70
plars865695b2001-08-27 22:15:12 +000071/*
72 * handler()
73 * A dummy signal handler for attempting to catch signals.
74 */
subrata_modak56207ce2009-03-23 13:35:39 +000075void handler(int sig)
plars865695b2001-08-27 22:15:12 +000076{
plarsa9933882003-07-08 18:36:59 +000077 if (DEBUG)
78 tst_resm(TINFO, "Inside signal handler. Got signal: %d", sig);
plars865695b2001-08-27 22:15:12 +000079 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_modak56207ce2009-03-23 13:35:39 +000089int set_handler(int sig, int sig_to_mask, int flag)
plars865695b2001-08-27 22:15:12 +000090{
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_modak56207ce2009-03-23 13:35:39 +0000113int main(int ac, char **av)
robbiewfa451a12003-03-27 20:52:36 +0000114{
subrata_modak56207ce2009-03-23 13:35:39 +0000115 char *msg; /* message got from parse_opts */
robbiewfa451a12003-03-27 20:52:36 +0000116
117 int ret;
118
Garrett Cooper45e285d2010-11-22 12:19:25 -0800119 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) {
Garrett Cooper60fa8012010-11-22 13:50:58 -0800120 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
robbiewfa451a12003-03-27 20:52:36 +0000121 }
robbiewfa451a12003-03-27 20:52:36 +0000122//test1:
123 testcase_no = 1;
124
plarsa9933882003-07-08 18:36:59 +0000125 if (DEBUG)
subrata_modak4bb656a2009-02-26 12:02:09 +0000126 tst_resm(TINFO, "Enter test %d: set handler for SIGKILL",
subrata_modak56207ce2009-03-23 13:35:39 +0000127 testcase_no);
robbiewfa451a12003-03-27 20:52:36 +0000128 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
plarsa9933882003-07-08 18:36:59 +0000141 if (DEBUG)
subrata_modak4bb656a2009-02-26 12:02:09 +0000142 tst_resm(TINFO, "Enter test %d: set handler for SIGSTOP",
subrata_modak56207ce2009-03-23 13:35:39 +0000143 testcase_no);
robbiewfa451a12003-03-27 20:52:36 +0000144 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++;
plarsa9933882003-07-08 18:36:59 +0000156 if (DEBUG)
157 tst_resm(TINFO, "Enter test %d: set handler for bad "
subrata_modak56207ce2009-03-23 13:35:39 +0000158 "signal number", testcase_no);
robbiewfa451a12003-03-27 20:52:36 +0000159 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++;
plarsa9933882003-07-08 18:36:59 +0000173 if (DEBUG)
174 tst_resm(TINFO, "Enter test %d: set handler with "
subrata_modak56207ce2009-03-23 13:35:39 +0000175 "bad \"act\" param", testcase_no);
robbiewfa451a12003-03-27 20:52:36 +0000176 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++;
plarsa9933882003-07-08 18:36:59 +0000188 if (DEBUG)
189 tst_resm(TINFO, "Enter test %d: set handler with "
subrata_modak56207ce2009-03-23 13:35:39 +0000190 "bad \"oact\" param", testcase_no);
robbiewfa451a12003-03-27 20:52:36 +0000191 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_modak56207ce2009-03-23 13:35:39 +0000200#endif /* GLIBC_SIGACTION_BUG */
robbiewfa451a12003-03-27 20:52:36 +0000201
202 tst_exit();
203
subrata_modak56207ce2009-03-23 13:35:39 +0000204 return 0;
robbiewfa451a12003-03-27 20:52:36 +0000205
206}