1*0a6a1f1dSLionel Sambuc /* $NetBSD: t_sigaction.c,v 1.3 2014/11/04 00:20:19 justin Exp $ */
211be35a1SLionel Sambuc
311be35a1SLionel Sambuc /*-
411be35a1SLionel Sambuc * Copyright (c) 2010 The NetBSD Foundation, Inc.
511be35a1SLionel Sambuc * All rights reserved.
611be35a1SLionel Sambuc *
711be35a1SLionel Sambuc * Redistribution and use in source and binary forms, with or without
811be35a1SLionel Sambuc * modification, are permitted provided that the following conditions
911be35a1SLionel Sambuc * are met:
1011be35a1SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
1111be35a1SLionel Sambuc * notice, this list of conditions and the following disclaimer.
1211be35a1SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
1311be35a1SLionel Sambuc * notice, this list of conditions and the following disclaimer in the
1411be35a1SLionel Sambuc * documentation and/or other materials provided with the distribution.
1511be35a1SLionel Sambuc *
1611be35a1SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
1711be35a1SLionel Sambuc * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
1811be35a1SLionel Sambuc * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1911be35a1SLionel Sambuc * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2011be35a1SLionel Sambuc * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2111be35a1SLionel Sambuc * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2211be35a1SLionel Sambuc * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2311be35a1SLionel Sambuc * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2411be35a1SLionel Sambuc * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2511be35a1SLionel Sambuc * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2611be35a1SLionel Sambuc * POSSIBILITY OF SUCH DAMAGE.
2711be35a1SLionel Sambuc */
2811be35a1SLionel Sambuc
2911be35a1SLionel Sambuc #include <sys/cdefs.h>
3011be35a1SLionel Sambuc __COPYRIGHT("@(#) Copyright (c) 2010\
3111be35a1SLionel Sambuc The NetBSD Foundation, inc. All rights reserved.");
32*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: t_sigaction.c,v 1.3 2014/11/04 00:20:19 justin Exp $");
3311be35a1SLionel Sambuc
3411be35a1SLionel Sambuc #include <sys/wait.h>
3511be35a1SLionel Sambuc
3611be35a1SLionel Sambuc #include <signal.h>
3711be35a1SLionel Sambuc #include <stdbool.h>
3811be35a1SLionel Sambuc #include <stdlib.h>
3911be35a1SLionel Sambuc #include <string.h>
4011be35a1SLionel Sambuc #include <unistd.h>
4111be35a1SLionel Sambuc
4211be35a1SLionel Sambuc #include <atf-c.h>
4311be35a1SLionel Sambuc
4411be35a1SLionel Sambuc #include "../../../h_macros.h"
4511be35a1SLionel Sambuc
4611be35a1SLionel Sambuc static bool handler_called = false;
4711be35a1SLionel Sambuc
4811be35a1SLionel Sambuc static void
handler(int signo)4911be35a1SLionel Sambuc handler(int signo)
5011be35a1SLionel Sambuc {
5111be35a1SLionel Sambuc handler_called = true;
5211be35a1SLionel Sambuc }
5311be35a1SLionel Sambuc
5411be35a1SLionel Sambuc static void
sa_resethand_child(const int flags)5511be35a1SLionel Sambuc sa_resethand_child(const int flags)
5611be35a1SLionel Sambuc {
5711be35a1SLionel Sambuc struct sigaction sa;
5811be35a1SLionel Sambuc
5911be35a1SLionel Sambuc sa.sa_flags = flags;
6011be35a1SLionel Sambuc sa.sa_handler = &handler;
6111be35a1SLionel Sambuc sigemptyset(&sa.sa_mask);
6211be35a1SLionel Sambuc
6311be35a1SLionel Sambuc sigaction(SIGUSR1, &sa, NULL);
6411be35a1SLionel Sambuc kill(getpid(), SIGUSR1);
6511be35a1SLionel Sambuc exit(handler_called ? EXIT_SUCCESS : EXIT_FAILURE);
6611be35a1SLionel Sambuc }
6711be35a1SLionel Sambuc
6811be35a1SLionel Sambuc static void
wait_and_check_child(const pid_t pid,const char * fail_message)6911be35a1SLionel Sambuc wait_and_check_child(const pid_t pid, const char *fail_message)
7011be35a1SLionel Sambuc {
7111be35a1SLionel Sambuc int status;
7211be35a1SLionel Sambuc
7311be35a1SLionel Sambuc (void)waitpid(pid, &status, 0);
7411be35a1SLionel Sambuc
7511be35a1SLionel Sambuc if (WIFEXITED(status))
7611be35a1SLionel Sambuc ATF_CHECK_EQ(EXIT_SUCCESS, WEXITSTATUS(status));
7711be35a1SLionel Sambuc else
7811be35a1SLionel Sambuc atf_tc_fail("%s; raw exit status was %d", fail_message, status);
7911be35a1SLionel Sambuc }
8011be35a1SLionel Sambuc
8111be35a1SLionel Sambuc static void
catch(int sig)8211be35a1SLionel Sambuc catch(int sig)
8311be35a1SLionel Sambuc {
8411be35a1SLionel Sambuc return;
8511be35a1SLionel Sambuc }
8611be35a1SLionel Sambuc
8711be35a1SLionel Sambuc ATF_TC(sigaction_basic);
ATF_TC_HEAD(sigaction_basic,tc)8811be35a1SLionel Sambuc ATF_TC_HEAD(sigaction_basic, tc)
8911be35a1SLionel Sambuc {
9011be35a1SLionel Sambuc
9111be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "Checks for correct I&D cache"
9211be35a1SLionel Sambuc "synchronization after copying out the trampoline code.");
9311be35a1SLionel Sambuc }
9411be35a1SLionel Sambuc
ATF_TC_BODY(sigaction_basic,tc)9511be35a1SLionel Sambuc ATF_TC_BODY(sigaction_basic, tc)
9611be35a1SLionel Sambuc {
9711be35a1SLionel Sambuc static struct sigaction sa;
9811be35a1SLionel Sambuc
9911be35a1SLionel Sambuc sa.sa_handler = catch;
10011be35a1SLionel Sambuc
10111be35a1SLionel Sambuc sigaction(SIGUSR1, &sa, 0);
10211be35a1SLionel Sambuc kill(getpid(), SIGUSR1);
10311be35a1SLionel Sambuc atf_tc_pass();
10411be35a1SLionel Sambuc }
10511be35a1SLionel Sambuc
10611be35a1SLionel Sambuc ATF_TC(sigaction_noflags);
ATF_TC_HEAD(sigaction_noflags,tc)10711be35a1SLionel Sambuc ATF_TC_HEAD(sigaction_noflags, tc)
10811be35a1SLionel Sambuc {
10911be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "Checks programming a signal with "
11011be35a1SLionel Sambuc "sigaction(2) but without any flags");
11111be35a1SLionel Sambuc }
11211be35a1SLionel Sambuc
ATF_TC_BODY(sigaction_noflags,tc)11311be35a1SLionel Sambuc ATF_TC_BODY(sigaction_noflags, tc)
11411be35a1SLionel Sambuc {
11511be35a1SLionel Sambuc const pid_t pid = fork();
11611be35a1SLionel Sambuc if (pid == -1)
11711be35a1SLionel Sambuc atf_tc_fail_errno("fork(2) failed");
11811be35a1SLionel Sambuc else if (pid == 0)
11911be35a1SLionel Sambuc sa_resethand_child(0);
12011be35a1SLionel Sambuc else
12111be35a1SLionel Sambuc wait_and_check_child(pid, "Child process did not exit cleanly;"
12211be35a1SLionel Sambuc " it failed to process the signal");
12311be35a1SLionel Sambuc }
12411be35a1SLionel Sambuc
12511be35a1SLionel Sambuc ATF_TC(sigaction_resethand);
ATF_TC_HEAD(sigaction_resethand,tc)12611be35a1SLionel Sambuc ATF_TC_HEAD(sigaction_resethand, tc)
12711be35a1SLionel Sambuc {
12811be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "Checks that SA_RESETHAND works");
12911be35a1SLionel Sambuc }
13011be35a1SLionel Sambuc
ATF_TC_BODY(sigaction_resethand,tc)13111be35a1SLionel Sambuc ATF_TC_BODY(sigaction_resethand, tc)
13211be35a1SLionel Sambuc {
13311be35a1SLionel Sambuc const pid_t pid = fork();
13411be35a1SLionel Sambuc if (pid == -1)
13511be35a1SLionel Sambuc atf_tc_fail_errno("fork(2) failed");
13611be35a1SLionel Sambuc else if (pid == 0)
13711be35a1SLionel Sambuc sa_resethand_child(SA_RESETHAND);
13811be35a1SLionel Sambuc else {
13911be35a1SLionel Sambuc wait_and_check_child(pid, "Child process did not exit cleanly;"
14011be35a1SLionel Sambuc " it either failed to process the signal or SA_RESETHAND"
14111be35a1SLionel Sambuc " is broken");
14211be35a1SLionel Sambuc }
14311be35a1SLionel Sambuc }
14411be35a1SLionel Sambuc
ATF_TP_ADD_TCS(tp)14511be35a1SLionel Sambuc ATF_TP_ADD_TCS(tp)
14611be35a1SLionel Sambuc {
14711be35a1SLionel Sambuc
14811be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, sigaction_basic);
14911be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, sigaction_noflags);
15011be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, sigaction_resethand);
15111be35a1SLionel Sambuc
15211be35a1SLionel Sambuc return atf_no_error();
15311be35a1SLionel Sambuc }
154