1f08c3bdfSopenharmony_ci/*
2f08c3bdfSopenharmony_ci *
3f08c3bdfSopenharmony_ci *   Copyright (c) International Business Machines  Corp., 2001
4f08c3bdfSopenharmony_ci *
5f08c3bdfSopenharmony_ci *   This program is free software;  you can redistribute it and/or modify
6f08c3bdfSopenharmony_ci *   it under the terms of the GNU General Public License as published by
7f08c3bdfSopenharmony_ci *   the Free Software Foundation; either version 2 of the License, or
8f08c3bdfSopenharmony_ci *   (at your option) any later version.
9f08c3bdfSopenharmony_ci *
10f08c3bdfSopenharmony_ci *   This program is distributed in the hope that it will be useful,
11f08c3bdfSopenharmony_ci *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
12f08c3bdfSopenharmony_ci *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
13f08c3bdfSopenharmony_ci *   the GNU General Public License for more details.
14f08c3bdfSopenharmony_ci *
15f08c3bdfSopenharmony_ci *   You should have received a copy of the GNU General Public License
16f08c3bdfSopenharmony_ci *   along with this program;  if not, write to the Free Software
17f08c3bdfSopenharmony_ci *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18f08c3bdfSopenharmony_ci */
19f08c3bdfSopenharmony_ci
20f08c3bdfSopenharmony_ci/*
21f08c3bdfSopenharmony_ci * NAME
22f08c3bdfSopenharmony_ci * 	sigaction02.c
23f08c3bdfSopenharmony_ci *
24f08c3bdfSopenharmony_ci * DESCRIPTION
25f08c3bdfSopenharmony_ci * 	Testcase to check the basic errnos set by the sigaction(2) syscall.
26f08c3bdfSopenharmony_ci *
27f08c3bdfSopenharmony_ci * ALGORITHM
28f08c3bdfSopenharmony_ci *	1. Pass an invalid signal as the "sig" parameter, and expect EINVAL.
29f08c3bdfSopenharmony_ci *	2. Attempt to catch the SIGKILL, and expect EINVAL.
30f08c3bdfSopenharmony_ci *	3. Attempt to catch the SIGSTOP, and expect EINVAL.
31f08c3bdfSopenharmony_ci *	4. Pass an invalid address as the "act" parameter, expect an EFAULT.
32f08c3bdfSopenharmony_ci *	5. Pass an invalid address as the "oact" parameter, and expect EFAULT.
33f08c3bdfSopenharmony_ci *
34f08c3bdfSopenharmony_ci * USAGE
35f08c3bdfSopenharmony_ci *	sigaction02
36f08c3bdfSopenharmony_ci *
37f08c3bdfSopenharmony_ci * HISTORY
38f08c3bdfSopenharmony_ci *	07/2001 Ported by Wayne Boyer
39f08c3bdfSopenharmony_ci *
40f08c3bdfSopenharmony_ci * RESTRICTIONS
41f08c3bdfSopenharmony_ci *	Tests #4 and #5 will fail as long as the glibc implementation
42f08c3bdfSopenharmony_ci *	of sigaction() is not fixed.  The glibc wrapper around of sigaction()
43f08c3bdfSopenharmony_ci *	doesn't handle the invalid addresses of the "act" and "oact" parameters
44f08c3bdfSopenharmony_ci *	correctly.  If an invalid address is passed, glibc dumps core.
45f08c3bdfSopenharmony_ci *	Temporarily, tests 4 and 5 are put inside "#ifdef GLIBC_SIGACTION_BUG"
46f08c3bdfSopenharmony_ci *	in order to skip these tests.  This should be removed from the Makefile
47f08c3bdfSopenharmony_ci *	and this program when the glibc bug gets fixed.
48f08c3bdfSopenharmony_ci *
49f08c3bdfSopenharmony_ci *	This test doesn't follow the correct LTP format - PLEASE FIX!
50f08c3bdfSopenharmony_ci */
51f08c3bdfSopenharmony_ci#define DEBUG 0
52f08c3bdfSopenharmony_ci
53f08c3bdfSopenharmony_ci#define _GNU_SOURCE
54f08c3bdfSopenharmony_ci#include <stdio.h>
55f08c3bdfSopenharmony_ci#include <stdlib.h>
56f08c3bdfSopenharmony_ci#include <unistd.h>
57f08c3bdfSopenharmony_ci#include <signal.h>
58f08c3bdfSopenharmony_ci#include <errno.h>
59f08c3bdfSopenharmony_ci#include "test.h"
60f08c3bdfSopenharmony_ci
61f08c3bdfSopenharmony_ci#define SIGBAD 9999
62f08c3bdfSopenharmony_ci
63f08c3bdfSopenharmony_civoid setup();
64f08c3bdfSopenharmony_civoid cleanup();
65f08c3bdfSopenharmony_ci
66f08c3bdfSopenharmony_cichar *TCID = "sigaction02";
67f08c3bdfSopenharmony_ciint TST_TOTAL = 1;
68f08c3bdfSopenharmony_ci
69f08c3bdfSopenharmony_civolatile sig_atomic_t testcase_no;
70f08c3bdfSopenharmony_ci
71f08c3bdfSopenharmony_ci/*
72f08c3bdfSopenharmony_ci * handler()
73f08c3bdfSopenharmony_ci *	A dummy signal handler for attempting to catch signals.
74f08c3bdfSopenharmony_ci */
75f08c3bdfSopenharmony_civoid handler(int sig)
76f08c3bdfSopenharmony_ci{
77f08c3bdfSopenharmony_ci	if (DEBUG)
78f08c3bdfSopenharmony_ci		tst_resm(TINFO, "Inside signal handler. Got signal: %d", sig);
79f08c3bdfSopenharmony_ci	return;
80f08c3bdfSopenharmony_ci}
81f08c3bdfSopenharmony_ci
82f08c3bdfSopenharmony_ci/*
83f08c3bdfSopenharmony_ci * set_handler()
84f08c3bdfSopenharmony_ci * 	Establish a signal handler for "sig" with the specified flags and
85f08c3bdfSopenharmony_ci * 	signal to mask while the handler executes.
86f08c3bdfSopenharmony_ci * Returns
87f08c3bdfSopenharmony_ci *	0 on success, errno on failure
88f08c3bdfSopenharmony_ci */
89f08c3bdfSopenharmony_ciint set_handler(int sig, int sig_to_mask, int flag)
90f08c3bdfSopenharmony_ci{
91f08c3bdfSopenharmony_ci	struct sigaction sa;
92f08c3bdfSopenharmony_ci	int err;
93f08c3bdfSopenharmony_ci
94f08c3bdfSopenharmony_ci	if (flag == 0) {
95f08c3bdfSopenharmony_ci		sa.sa_sigaction = (void *)handler;
96f08c3bdfSopenharmony_ci		sa.sa_flags = SA_NOMASK;
97f08c3bdfSopenharmony_ci		sigemptyset(&sa.sa_mask);
98f08c3bdfSopenharmony_ci		sigaddset(&sa.sa_mask, sig_to_mask);
99f08c3bdfSopenharmony_ci		err = sigaction(sig, &sa, NULL);
100f08c3bdfSopenharmony_ci	} else if (flag == 1) {
101f08c3bdfSopenharmony_ci		err = sigaction(sig, (void *)-1, NULL);
102f08c3bdfSopenharmony_ci	} else if (flag == 2) {
103f08c3bdfSopenharmony_ci		err = sigaction(sig, NULL, (void *)-1);
104f08c3bdfSopenharmony_ci	} else
105f08c3bdfSopenharmony_ci		err = -1;
106f08c3bdfSopenharmony_ci
107f08c3bdfSopenharmony_ci	if (err == 0)
108f08c3bdfSopenharmony_ci		return 0;
109f08c3bdfSopenharmony_ci	else
110f08c3bdfSopenharmony_ci		return errno;
111f08c3bdfSopenharmony_ci}
112f08c3bdfSopenharmony_ci
113f08c3bdfSopenharmony_ciint main(int ac, char **av)
114f08c3bdfSopenharmony_ci{
115f08c3bdfSopenharmony_ci	int ret;
116f08c3bdfSopenharmony_ci
117f08c3bdfSopenharmony_ci	tst_parse_opts(ac, av, NULL, NULL);
118f08c3bdfSopenharmony_ci//test1:
119f08c3bdfSopenharmony_ci	testcase_no = 1;
120f08c3bdfSopenharmony_ci
121f08c3bdfSopenharmony_ci	if (DEBUG)
122f08c3bdfSopenharmony_ci		tst_resm(TINFO, "Enter test %d: set handler for SIGKILL",
123f08c3bdfSopenharmony_ci			 testcase_no);
124f08c3bdfSopenharmony_ci	if ((ret = set_handler(SIGKILL, 0, 0)) == 0) {
125f08c3bdfSopenharmony_ci		tst_resm(TFAIL, "sigaction() succeeded, should have failed");
126f08c3bdfSopenharmony_ci	}
127f08c3bdfSopenharmony_ci	if (ret != EINVAL) {
128f08c3bdfSopenharmony_ci		tst_resm(TFAIL, "sigaction set incorrect errno. Expected "
129f08c3bdfSopenharmony_ci			 "EINVAL, got: %d", ret);
130f08c3bdfSopenharmony_ci	} else {
131f08c3bdfSopenharmony_ci		tst_resm(TPASS, "call failed with expected EINVAL error");
132f08c3bdfSopenharmony_ci	}
133f08c3bdfSopenharmony_ci
134f08c3bdfSopenharmony_ci//test2:
135f08c3bdfSopenharmony_ci	testcase_no++;
136f08c3bdfSopenharmony_ci
137f08c3bdfSopenharmony_ci	if (DEBUG)
138f08c3bdfSopenharmony_ci		tst_resm(TINFO, "Enter test %d: set handler for SIGSTOP",
139f08c3bdfSopenharmony_ci			 testcase_no);
140f08c3bdfSopenharmony_ci	if ((ret = set_handler(SIGSTOP, 0, 0)) == 0) {
141f08c3bdfSopenharmony_ci		tst_resm(TFAIL, "sigaction() succeeded, should have failed");
142f08c3bdfSopenharmony_ci	}
143f08c3bdfSopenharmony_ci	if (ret != EINVAL) {
144f08c3bdfSopenharmony_ci		tst_resm(TFAIL, "sigaction set incorrect errno. Expected "
145f08c3bdfSopenharmony_ci			 "EINVAL, got: %d", ret);
146f08c3bdfSopenharmony_ci	} else {
147f08c3bdfSopenharmony_ci		tst_resm(TPASS, "call failed with expected EINVAL error");
148f08c3bdfSopenharmony_ci	}
149f08c3bdfSopenharmony_ci
150f08c3bdfSopenharmony_ci//test3:
151f08c3bdfSopenharmony_ci	testcase_no++;
152f08c3bdfSopenharmony_ci	if (DEBUG)
153f08c3bdfSopenharmony_ci		tst_resm(TINFO, "Enter test %d: set handler for bad "
154f08c3bdfSopenharmony_ci			 "signal number", testcase_no);
155f08c3bdfSopenharmony_ci	if ((ret = set_handler(SIGBAD, 0, 0)) == 0) {
156f08c3bdfSopenharmony_ci		tst_resm(TFAIL, "sigaction() succeeded, should have failed");
157f08c3bdfSopenharmony_ci	}
158f08c3bdfSopenharmony_ci	if (ret != EINVAL) {
159f08c3bdfSopenharmony_ci		tst_resm(TFAIL, "sigaction set incorrect errno. Expected "
160f08c3bdfSopenharmony_ci			 "EINVAL, got: %d", ret);
161f08c3bdfSopenharmony_ci	} else {
162f08c3bdfSopenharmony_ci		tst_resm(TPASS, "call failed with expected EINVAL error");
163f08c3bdfSopenharmony_ci	}
164f08c3bdfSopenharmony_ci
165f08c3bdfSopenharmony_ci#ifndef GLIBC_SIGACTION_BUG
166f08c3bdfSopenharmony_ci
167f08c3bdfSopenharmony_ci//test4:
168f08c3bdfSopenharmony_ci	testcase_no++;
169f08c3bdfSopenharmony_ci	if (DEBUG)
170f08c3bdfSopenharmony_ci		tst_resm(TINFO, "Enter test %d: set handler with "
171f08c3bdfSopenharmony_ci			 "bad \"act\" param", testcase_no);
172f08c3bdfSopenharmony_ci	if ((ret = set_handler(SIGUSR1, 0, 1)) == 0) {
173f08c3bdfSopenharmony_ci		tst_resm(TFAIL, "sigaction() succeeded, should have failed");
174f08c3bdfSopenharmony_ci	}
175f08c3bdfSopenharmony_ci	if (ret != EFAULT) {
176f08c3bdfSopenharmony_ci		tst_resm(TFAIL, "sigaction set incorrect errno. Expected "
177f08c3bdfSopenharmony_ci			 "EFAULT, got: %d", ret);
178f08c3bdfSopenharmony_ci	} else {
179f08c3bdfSopenharmony_ci		tst_resm(TPASS, "call failed with expected EFAULT error");
180f08c3bdfSopenharmony_ci	}
181f08c3bdfSopenharmony_ci
182f08c3bdfSopenharmony_ci//test5:
183f08c3bdfSopenharmony_ci	testcase_no++;
184f08c3bdfSopenharmony_ci	if (DEBUG)
185f08c3bdfSopenharmony_ci		tst_resm(TINFO, "Enter test %d: set handler with "
186f08c3bdfSopenharmony_ci			 "bad \"oact\" param", testcase_no);
187f08c3bdfSopenharmony_ci	if ((ret = set_handler(SIGUSR1, 0, 2)) == 0) {
188f08c3bdfSopenharmony_ci		tst_resm(TFAIL, "sigaction() succeeded, should have failed");
189f08c3bdfSopenharmony_ci	}
190f08c3bdfSopenharmony_ci	if (ret != EFAULT) {
191f08c3bdfSopenharmony_ci		tst_resm(TFAIL, "sigaction set incorrect errno. Expected "
192f08c3bdfSopenharmony_ci			 "EFAULT, got: %d", ret);
193f08c3bdfSopenharmony_ci	} else {
194f08c3bdfSopenharmony_ci		tst_resm(TPASS, "call failed with expected EFAULT error");
195f08c3bdfSopenharmony_ci	}
196f08c3bdfSopenharmony_ci#endif /* GLIBC_SIGACTION_BUG */
197f08c3bdfSopenharmony_ci
198f08c3bdfSopenharmony_ci	tst_exit();
199f08c3bdfSopenharmony_ci
200f08c3bdfSopenharmony_ci}
201