t_sigaction.c revision 314817
1/* $NetBSD: t_sigaction.c,v 1.5 2017/01/13 21:30:41 christos Exp $ */
2
3/*-
4 * Copyright (c) 2010 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <sys/cdefs.h>
30__COPYRIGHT("@(#) Copyright (c) 2010\
31 The NetBSD Foundation, inc. All rights reserved.");
32__RCSID("$NetBSD: t_sigaction.c,v 1.5 2017/01/13 21:30:41 christos Exp $");
33
34#include <sys/wait.h>
35
36#include <signal.h>
37#include <stdbool.h>
38#include <stdlib.h>
39#include <string.h>
40#include <unistd.h>
41
42#include <atf-c.h>
43
44#include "h_macros.h"
45
46static bool handler_called = false;
47
48static void
49handler(int signo __unused)
50{
51    handler_called = true;
52}
53
54static void
55sa_resethand_child(const int flags)
56{
57    struct sigaction sa;
58
59    sa.sa_flags = flags;
60    sa.sa_handler = &handler;
61    sigemptyset(&sa.sa_mask);
62
63    sigaction(SIGUSR1, &sa, NULL);
64    kill(getpid(), SIGUSR1);
65    exit(handler_called ? EXIT_SUCCESS : EXIT_FAILURE);
66}
67
68static void
69wait_and_check_child(const pid_t pid, const char *fail_message)
70{
71	int status;
72
73	(void)waitpid(pid, &status, 0);
74
75	if (WIFEXITED(status))
76		ATF_CHECK_EQ(EXIT_SUCCESS, WEXITSTATUS(status));
77	else
78		atf_tc_fail("%s; raw exit status was %d", fail_message, status);
79}
80
81static void
82catch(int sig __unused)
83{
84	return;
85}
86
87ATF_TC(sigaction_basic);
88ATF_TC_HEAD(sigaction_basic, tc)
89{
90
91	atf_tc_set_md_var(tc, "descr", "Checks for correct I&D cache"
92	    "synchronization after copying out the trampoline code.");
93}
94
95ATF_TC_BODY(sigaction_basic, tc)
96{
97	static struct sigaction sa;
98
99	sa.sa_handler = catch;
100
101	sigaction(SIGUSR1, &sa, 0);
102	kill(getpid(), SIGUSR1);
103	atf_tc_pass();
104}
105
106ATF_TC(sigaction_noflags);
107ATF_TC_HEAD(sigaction_noflags, tc)
108{
109	atf_tc_set_md_var(tc, "descr", "Checks programming a signal with "
110	    "sigaction(2) but without any flags");
111}
112
113ATF_TC_BODY(sigaction_noflags, tc)
114{
115	const pid_t pid = fork();
116	if (pid == -1)
117		atf_tc_fail_errno("fork(2) failed");
118	else if (pid == 0)
119		sa_resethand_child(0);
120	else
121		wait_and_check_child(pid, "Child process did not exit cleanly;"
122		    " it failed to process the signal");
123}
124
125ATF_TC(sigaction_resethand);
126ATF_TC_HEAD(sigaction_resethand, tc)
127{
128	atf_tc_set_md_var(tc, "descr", "Checks that SA_RESETHAND works");
129}
130
131ATF_TC_BODY(sigaction_resethand, tc)
132{
133	const pid_t pid = fork();
134	if (pid == -1)
135		atf_tc_fail_errno("fork(2) failed");
136	else if (pid == 0)
137		sa_resethand_child(SA_RESETHAND);
138	else {
139		wait_and_check_child(pid, "Child process did not exit cleanly;"
140		    " it either failed to process the signal or SA_RESETHAND"
141		    " is broken");
142	}
143}
144
145ATF_TP_ADD_TCS(tp)
146{
147
148	ATF_TP_ADD_TC(tp, sigaction_basic);
149	ATF_TP_ADD_TC(tp, sigaction_noflags);
150	ATF_TP_ADD_TC(tp, sigaction_resethand);
151
152	return atf_no_error();
153}
154