1276478Sngie/* $NetBSD: t_sigaction.c,v 1.3 2014/11/04 00:20:19 justin Exp $ */
2272343Sngie
3272343Sngie/*-
4272343Sngie * Copyright (c) 2010 The NetBSD Foundation, Inc.
5272343Sngie * All rights reserved.
6272343Sngie *
7272343Sngie * Redistribution and use in source and binary forms, with or without
8272343Sngie * modification, are permitted provided that the following conditions
9272343Sngie * are met:
10272343Sngie * 1. Redistributions of source code must retain the above copyright
11272343Sngie *    notice, this list of conditions and the following disclaimer.
12272343Sngie * 2. Redistributions in binary form must reproduce the above copyright
13272343Sngie *    notice, this list of conditions and the following disclaimer in the
14272343Sngie *    documentation and/or other materials provided with the distribution.
15272343Sngie *
16272343Sngie * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17272343Sngie * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18272343Sngie * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19272343Sngie * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20272343Sngie * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21272343Sngie * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22272343Sngie * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23272343Sngie * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24272343Sngie * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25272343Sngie * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26272343Sngie * POSSIBILITY OF SUCH DAMAGE.
27272343Sngie */
28272343Sngie
29272343Sngie#include <sys/cdefs.h>
30272343Sngie__COPYRIGHT("@(#) Copyright (c) 2010\
31272343Sngie The NetBSD Foundation, inc. All rights reserved.");
32276478Sngie__RCSID("$NetBSD: t_sigaction.c,v 1.3 2014/11/04 00:20:19 justin Exp $");
33272343Sngie
34272343Sngie#include <sys/wait.h>
35272343Sngie
36272343Sngie#include <signal.h>
37272343Sngie#include <stdbool.h>
38272343Sngie#include <stdlib.h>
39272343Sngie#include <string.h>
40272343Sngie#include <unistd.h>
41272343Sngie
42272343Sngie#include <atf-c.h>
43272343Sngie
44276478Sngie#ifdef __NetBSD__
45272343Sngie#include "../../../h_macros.h"
46276478Sngie#else
47276478Sngie#include "h_macros.h"
48276478Sngie#endif
49272343Sngie
50272343Sngiestatic bool handler_called = false;
51272343Sngie
52272343Sngiestatic void
53276478Sngie#ifdef __FreeBSD__
54276478Sngiehandler(int signo __unused)
55276478Sngie#else
56272343Sngiehandler(int signo)
57276478Sngie#endif
58272343Sngie{
59272343Sngie    handler_called = true;
60272343Sngie}
61272343Sngie
62272343Sngiestatic void
63272343Sngiesa_resethand_child(const int flags)
64272343Sngie{
65272343Sngie    struct sigaction sa;
66272343Sngie
67272343Sngie    sa.sa_flags = flags;
68272343Sngie    sa.sa_handler = &handler;
69272343Sngie    sigemptyset(&sa.sa_mask);
70272343Sngie
71272343Sngie    sigaction(SIGUSR1, &sa, NULL);
72272343Sngie    kill(getpid(), SIGUSR1);
73272343Sngie    exit(handler_called ? EXIT_SUCCESS : EXIT_FAILURE);
74272343Sngie}
75272343Sngie
76272343Sngiestatic void
77272343Sngiewait_and_check_child(const pid_t pid, const char *fail_message)
78272343Sngie{
79272343Sngie	int status;
80272343Sngie
81272343Sngie	(void)waitpid(pid, &status, 0);
82272343Sngie
83272343Sngie	if (WIFEXITED(status))
84272343Sngie		ATF_CHECK_EQ(EXIT_SUCCESS, WEXITSTATUS(status));
85272343Sngie	else
86272343Sngie		atf_tc_fail("%s; raw exit status was %d", fail_message, status);
87272343Sngie}
88272343Sngie
89272343Sngiestatic void
90276478Sngie#ifdef __FreeBSD__
91276478Sngiecatch(int sig __unused)
92276478Sngie#else
93272343Sngiecatch(int sig)
94276478Sngie#endif
95272343Sngie{
96272343Sngie	return;
97272343Sngie}
98272343Sngie
99272343SngieATF_TC(sigaction_basic);
100272343SngieATF_TC_HEAD(sigaction_basic, tc)
101272343Sngie{
102272343Sngie
103272343Sngie	atf_tc_set_md_var(tc, "descr", "Checks for correct I&D cache"
104272343Sngie	    "synchronization after copying out the trampoline code.");
105272343Sngie}
106272343Sngie
107272343SngieATF_TC_BODY(sigaction_basic, tc)
108272343Sngie{
109272343Sngie	static struct sigaction sa;
110272343Sngie
111272343Sngie	sa.sa_handler = catch;
112272343Sngie
113272343Sngie	sigaction(SIGUSR1, &sa, 0);
114272343Sngie	kill(getpid(), SIGUSR1);
115272343Sngie	atf_tc_pass();
116272343Sngie}
117272343Sngie
118272343SngieATF_TC(sigaction_noflags);
119272343SngieATF_TC_HEAD(sigaction_noflags, tc)
120272343Sngie{
121272343Sngie	atf_tc_set_md_var(tc, "descr", "Checks programming a signal with "
122272343Sngie	    "sigaction(2) but without any flags");
123272343Sngie}
124272343Sngie
125272343SngieATF_TC_BODY(sigaction_noflags, tc)
126272343Sngie{
127272343Sngie	const pid_t pid = fork();
128272343Sngie	if (pid == -1)
129272343Sngie		atf_tc_fail_errno("fork(2) failed");
130272343Sngie	else if (pid == 0)
131272343Sngie		sa_resethand_child(0);
132272343Sngie	else
133272343Sngie		wait_and_check_child(pid, "Child process did not exit cleanly;"
134272343Sngie		    " it failed to process the signal");
135272343Sngie}
136272343Sngie
137272343SngieATF_TC(sigaction_resethand);
138272343SngieATF_TC_HEAD(sigaction_resethand, tc)
139272343Sngie{
140272343Sngie	atf_tc_set_md_var(tc, "descr", "Checks that SA_RESETHAND works");
141272343Sngie}
142272343Sngie
143272343SngieATF_TC_BODY(sigaction_resethand, tc)
144272343Sngie{
145272343Sngie	const pid_t pid = fork();
146272343Sngie	if (pid == -1)
147272343Sngie		atf_tc_fail_errno("fork(2) failed");
148272343Sngie	else if (pid == 0)
149272343Sngie		sa_resethand_child(SA_RESETHAND);
150272343Sngie	else {
151272343Sngie		wait_and_check_child(pid, "Child process did not exit cleanly;"
152272343Sngie		    " it either failed to process the signal or SA_RESETHAND"
153272343Sngie		    " is broken");
154272343Sngie	}
155272343Sngie}
156272343Sngie
157272343SngieATF_TP_ADD_TCS(tp)
158272343Sngie{
159272343Sngie
160272343Sngie	ATF_TP_ADD_TC(tp, sigaction_basic);
161272343Sngie	ATF_TP_ADD_TC(tp, sigaction_noflags);
162272343Sngie	ATF_TP_ADD_TC(tp, sigaction_resethand);
163272343Sngie
164272343Sngie	return atf_no_error();
165272343Sngie}
166