1272343Sngie/*	$NetBSD: t_signals.c,v 1.2 2011/02/20 19:45:45 pooka Exp $	*/
2272343Sngie
3272343Sngie/*-
4272343Sngie * Copyright (c) 2011 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
17272343Sngie * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18272343Sngie * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19272343Sngie * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20272343Sngie * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21272343Sngie * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22272343Sngie * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23272343Sngie * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24272343Sngie * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25272343Sngie * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26272343Sngie * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27272343Sngie * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28272343Sngie */
29272343Sngie
30272343Sngie#include <sys/types.h>
31272343Sngie#include <sys/wait.h>
32272343Sngie
33272343Sngie#include <atf-c.h>
34272343Sngie#include <errno.h>
35272343Sngie#include <string.h>
36272343Sngie#include <signal.h>
37272343Sngie#include <unistd.h>
38272343Sngie
39272343Sngie#include <rump/rump.h>
40272343Sngie
41272343Sngie#include "../kernspace/kernspace.h"
42272343Sngie#include "../../h_macros.h"
43272343Sngie
44272343SngieATF_TC(sigraise);
45272343SngieATF_TC_HEAD(sigraise, tc)
46272343Sngie{
47272343Sngie
48272343Sngie	atf_tc_set_md_var(tc, "descr", "RUMP_SIGMODEL_RAISE");
49272343Sngie}
50272343Sngie
51272343SngieATF_TC(sigignore);
52272343SngieATF_TC_HEAD(sigignore, tc)
53272343Sngie{
54272343Sngie
55272343Sngie	atf_tc_set_md_var(tc, "descr", "RUMP_SIGMODEL_IGNORE");
56272343Sngie}
57272343Sngie
58272343SngieATF_TC(sigpanic);
59272343SngieATF_TC_HEAD(sigpanic, tc)
60272343Sngie{
61272343Sngie
62272343Sngie	atf_tc_set_md_var(tc, "descr", "RUMP_SIGMODEL_PANIC");
63272343Sngie}
64272343Sngie
65272343Sngiestatic volatile sig_atomic_t sigcnt;
66272343Sngiestatic void
67272343Sngiethehand(int sig)
68272343Sngie{
69272343Sngie
70272343Sngie	sigcnt++;
71272343Sngie}
72272343Sngie
73272343SngieATF_TC_BODY(sigraise, tc)
74272343Sngie{
75272343Sngie
76272343Sngie	signal(SIGUSR2, thehand);
77272343Sngie	rump_boot_setsigmodel(RUMP_SIGMODEL_RAISE);
78272343Sngie
79272343Sngie	rump_init();
80272343Sngie	rump_schedule();
81272343Sngie	rumptest_localsig(SIGUSR2);
82272343Sngie	rump_unschedule();
83272343Sngie	ATF_REQUIRE_EQ(sigcnt, 1);
84272343Sngie}
85272343Sngie
86272343SngieATF_TC_BODY(sigignore, tc)
87272343Sngie{
88272343Sngie
89272343Sngie	rump_boot_setsigmodel(RUMP_SIGMODEL_IGNORE);
90272343Sngie
91272343Sngie	rump_init();
92272343Sngie	rump_schedule();
93272343Sngie	rumptest_localsig(SIGKILL);
94272343Sngie	rump_unschedule();
95272343Sngie}
96272343Sngie
97272343SngieATF_TC_BODY(sigpanic, tc)
98272343Sngie{
99272343Sngie	int status;
100272343Sngie
101272343Sngie	rump_boot_setsigmodel(RUMP_SIGMODEL_PANIC);
102272343Sngie
103272343Sngie	switch (fork()) {
104272343Sngie	case 0:
105272343Sngie		rump_init();
106272343Sngie		rump_schedule();
107272343Sngie		rumptest_localsig(SIGCONT);
108272343Sngie		/* NOTREACHED */
109272343Sngie		exit(1);
110272343Sngie	default:
111272343Sngie		wait(&status);
112272343Sngie		ATF_REQUIRE(WIFSIGNALED(status) && WTERMSIG(status) == SIGABRT);
113272343Sngie		break;
114272343Sngie	case -1:
115272343Sngie		atf_tc_fail_errno("fork");
116272343Sngie	}
117272343Sngie}
118272343Sngie
119272343SngieATF_TP_ADD_TCS(tp)
120272343Sngie{
121272343Sngie
122272343Sngie	ATF_TP_ADD_TC(tp, sigraise);
123272343Sngie	ATF_TP_ADD_TC(tp, sigignore);
124272343Sngie	ATF_TP_ADD_TC(tp, sigpanic);
125272343Sngie
126272343Sngie	return atf_no_error();
127272343Sngie}
128