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