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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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.
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.
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 */
51#define DEBUG 0
52
53#define _GNU_SOURCE
54#include <stdio.h>
55#include <stdlib.h>
56#include <unistd.h>
57#include <signal.h>
58#include <errno.h>
59#include "test.h"
60
61#define SIGBAD 9999
62
63void setup();
64void cleanup();
65
66char *TCID = "sigaction02";
67int TST_TOTAL = 1;
68
69volatile sig_atomic_t testcase_no;
70
71/*
72 * handler()
73 *	A dummy signal handler for attempting to catch signals.
74 */
75void handler(int sig)
76{
77	if (DEBUG)
78		tst_resm(TINFO, "Inside signal handler. Got signal: %d", sig);
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 */
89int set_handler(int sig, int sig_to_mask, int flag)
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	} else
105		err = -1;
106
107	if (err == 0)
108		return 0;
109	else
110		return errno;
111}
112
113int main(int ac, char **av)
114{
115	int ret;
116
117	tst_parse_opts(ac, av, NULL, NULL);
118//test1:
119	testcase_no = 1;
120
121	if (DEBUG)
122		tst_resm(TINFO, "Enter test %d: set handler for SIGKILL",
123			 testcase_no);
124	if ((ret = set_handler(SIGKILL, 0, 0)) == 0) {
125		tst_resm(TFAIL, "sigaction() succeeded, should have failed");
126	}
127	if (ret != EINVAL) {
128		tst_resm(TFAIL, "sigaction set incorrect errno. Expected "
129			 "EINVAL, got: %d", ret);
130	} else {
131		tst_resm(TPASS, "call failed with expected EINVAL error");
132	}
133
134//test2:
135	testcase_no++;
136
137	if (DEBUG)
138		tst_resm(TINFO, "Enter test %d: set handler for SIGSTOP",
139			 testcase_no);
140	if ((ret = set_handler(SIGSTOP, 0, 0)) == 0) {
141		tst_resm(TFAIL, "sigaction() succeeded, should have failed");
142	}
143	if (ret != EINVAL) {
144		tst_resm(TFAIL, "sigaction set incorrect errno. Expected "
145			 "EINVAL, got: %d", ret);
146	} else {
147		tst_resm(TPASS, "call failed with expected EINVAL error");
148	}
149
150//test3:
151	testcase_no++;
152	if (DEBUG)
153		tst_resm(TINFO, "Enter test %d: set handler for bad "
154			 "signal number", testcase_no);
155	if ((ret = set_handler(SIGBAD, 0, 0)) == 0) {
156		tst_resm(TFAIL, "sigaction() succeeded, should have failed");
157	}
158	if (ret != EINVAL) {
159		tst_resm(TFAIL, "sigaction set incorrect errno. Expected "
160			 "EINVAL, got: %d", ret);
161	} else {
162		tst_resm(TPASS, "call failed with expected EINVAL error");
163	}
164
165#ifndef GLIBC_SIGACTION_BUG
166
167//test4:
168	testcase_no++;
169	if (DEBUG)
170		tst_resm(TINFO, "Enter test %d: set handler with "
171			 "bad \"act\" param", testcase_no);
172	if ((ret = set_handler(SIGUSR1, 0, 1)) == 0) {
173		tst_resm(TFAIL, "sigaction() succeeded, should have failed");
174	}
175	if (ret != EFAULT) {
176		tst_resm(TFAIL, "sigaction set incorrect errno. Expected "
177			 "EFAULT, got: %d", ret);
178	} else {
179		tst_resm(TPASS, "call failed with expected EFAULT error");
180	}
181
182//test5:
183	testcase_no++;
184	if (DEBUG)
185		tst_resm(TINFO, "Enter test %d: set handler with "
186			 "bad \"oact\" param", testcase_no);
187	if ((ret = set_handler(SIGUSR1, 0, 2)) == 0) {
188		tst_resm(TFAIL, "sigaction() succeeded, should have failed");
189	}
190	if (ret != EFAULT) {
191		tst_resm(TFAIL, "sigaction set incorrect errno. Expected "
192			 "EFAULT, got: %d", ret);
193	} else {
194		tst_resm(TPASS, "call failed with expected EFAULT error");
195	}
196#endif /* GLIBC_SIGACTION_BUG */
197
198	tst_exit();
199
200}
201