]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - contrib/netbsd-tests/lib/libc/sys/t_sigaction.c
- Copy stable/10@285827 to releng/10.2 in preparation for 10.2-RC1
[FreeBSD/releng/10.2.git] / contrib / netbsd-tests / lib / libc / sys / t_sigaction.c
1 /* $NetBSD: t_sigaction.c,v 1.3 2014/11/04 00:20:19 justin 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.3 2014/11/04 00:20:19 justin 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 #ifdef __NetBSD__
45 #include "../../../h_macros.h"
46 #else
47 #include "h_macros.h"
48 #endif
49
50 static bool handler_called = false;
51
52 static void
53 #ifdef __FreeBSD__
54 handler(int signo __unused)
55 #else
56 handler(int signo)
57 #endif
58 {
59     handler_called = true;
60 }
61
62 static void
63 sa_resethand_child(const int flags)
64 {
65     struct sigaction sa;
66
67     sa.sa_flags = flags;
68     sa.sa_handler = &handler;
69     sigemptyset(&sa.sa_mask);
70
71     sigaction(SIGUSR1, &sa, NULL);
72     kill(getpid(), SIGUSR1);
73     exit(handler_called ? EXIT_SUCCESS : EXIT_FAILURE);
74 }
75
76 static void
77 wait_and_check_child(const pid_t pid, const char *fail_message)
78 {
79         int status;
80
81         (void)waitpid(pid, &status, 0);
82
83         if (WIFEXITED(status))
84                 ATF_CHECK_EQ(EXIT_SUCCESS, WEXITSTATUS(status));
85         else
86                 atf_tc_fail("%s; raw exit status was %d", fail_message, status);
87 }
88
89 static void
90 #ifdef __FreeBSD__
91 catch(int sig __unused)
92 #else
93 catch(int sig)
94 #endif
95 {
96         return;
97 }
98
99 ATF_TC(sigaction_basic);
100 ATF_TC_HEAD(sigaction_basic, tc)
101 {
102
103         atf_tc_set_md_var(tc, "descr", "Checks for correct I&D cache"
104             "synchronization after copying out the trampoline code.");
105 }
106
107 ATF_TC_BODY(sigaction_basic, tc)
108 {
109         static struct sigaction sa;
110
111         sa.sa_handler = catch;
112
113         sigaction(SIGUSR1, &sa, 0);
114         kill(getpid(), SIGUSR1);
115         atf_tc_pass();
116 }
117
118 ATF_TC(sigaction_noflags);
119 ATF_TC_HEAD(sigaction_noflags, tc)
120 {
121         atf_tc_set_md_var(tc, "descr", "Checks programming a signal with "
122             "sigaction(2) but without any flags");
123 }
124
125 ATF_TC_BODY(sigaction_noflags, tc)
126 {
127         const pid_t pid = fork();
128         if (pid == -1)
129                 atf_tc_fail_errno("fork(2) failed");
130         else if (pid == 0)
131                 sa_resethand_child(0);
132         else
133                 wait_and_check_child(pid, "Child process did not exit cleanly;"
134                     " it failed to process the signal");
135 }
136
137 ATF_TC(sigaction_resethand);
138 ATF_TC_HEAD(sigaction_resethand, tc)
139 {
140         atf_tc_set_md_var(tc, "descr", "Checks that SA_RESETHAND works");
141 }
142
143 ATF_TC_BODY(sigaction_resethand, tc)
144 {
145         const pid_t pid = fork();
146         if (pid == -1)
147                 atf_tc_fail_errno("fork(2) failed");
148         else if (pid == 0)
149                 sa_resethand_child(SA_RESETHAND);
150         else {
151                 wait_and_check_child(pid, "Child process did not exit cleanly;"
152                     " it either failed to process the signal or SA_RESETHAND"
153                     " is broken");
154         }
155 }
156
157 ATF_TP_ADD_TCS(tp)
158 {
159
160         ATF_TP_ADD_TC(tp, sigaction_basic);
161         ATF_TP_ADD_TC(tp, sigaction_noflags);
162         ATF_TP_ADD_TC(tp, sigaction_resethand);
163
164         return atf_no_error();
165 }