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