1*c54cb811Schristos /* $NetBSD: t_swwdog.c,v 1.7 2017/01/13 21:30:39 christos Exp $ */
2edcef5fcSpooka
3edcef5fcSpooka /*
4edcef5fcSpooka * Copyright (c) 2010 Antti Kantee. All Rights Reserved.
5edcef5fcSpooka *
6edcef5fcSpooka * Redistribution and use in source and binary forms, with or without
7edcef5fcSpooka * modification, are permitted provided that the following conditions
8edcef5fcSpooka * are met:
9edcef5fcSpooka * 1. Redistributions of source code must retain the above copyright
10edcef5fcSpooka * notice, this list of conditions and the following disclaimer.
11edcef5fcSpooka * 2. Redistributions in binary form must reproduce the above copyright
12edcef5fcSpooka * notice, this list of conditions and the following disclaimer in the
13edcef5fcSpooka * documentation and/or other materials provided with the distribution.
14edcef5fcSpooka *
15edcef5fcSpooka * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
16edcef5fcSpooka * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17edcef5fcSpooka * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18edcef5fcSpooka * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19edcef5fcSpooka * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20edcef5fcSpooka * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21edcef5fcSpooka * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22edcef5fcSpooka * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23edcef5fcSpooka * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24edcef5fcSpooka * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25edcef5fcSpooka * SUCH DAMAGE.
26edcef5fcSpooka */
27edcef5fcSpooka
28edcef5fcSpooka #include <sys/types.h>
29edcef5fcSpooka #include <sys/wait.h>
30edcef5fcSpooka #include <sys/wdog.h>
31edcef5fcSpooka
32edcef5fcSpooka #include <assert.h>
33edcef5fcSpooka #include <atf-c.h>
34edcef5fcSpooka #include <err.h>
35edcef5fcSpooka #include <errno.h>
36edcef5fcSpooka #include <fcntl.h>
37edcef5fcSpooka #include <stdio.h>
38edcef5fcSpooka #include <stdlib.h>
39edcef5fcSpooka #include <string.h>
40edcef5fcSpooka #include <unistd.h>
417933a8d7Schristos #include <signal.h>
42edcef5fcSpooka
43edcef5fcSpooka #include <rump/rump.h>
44edcef5fcSpooka #include <rump/rump_syscalls.h>
45edcef5fcSpooka
46*c54cb811Schristos #include "h_macros.h"
47edcef5fcSpooka
48c825a763Spooka static volatile sig_atomic_t tcount;
49edcef5fcSpooka
50edcef5fcSpooka static void
sigcount(int sig)51edcef5fcSpooka sigcount(int sig)
52edcef5fcSpooka {
53edcef5fcSpooka
54edcef5fcSpooka assert(sig == SIGUSR1);
55edcef5fcSpooka tcount++;
56edcef5fcSpooka }
57edcef5fcSpooka
58edcef5fcSpooka /*
59edcef5fcSpooka * Since we are testing for swwdog's ability to reboot/panic, we need
60edcef5fcSpooka * to fork and monitor the exit status from the parent and report
61edcef5fcSpooka * something sensible back to atf.
62edcef5fcSpooka */
63edcef5fcSpooka static int
testbody(int max)647127b715Spgoyette testbody(int max)
65edcef5fcSpooka {
66edcef5fcSpooka char wname[WDOG_NAMESIZE];
67edcef5fcSpooka struct wdog_conf wc;
68edcef5fcSpooka struct wdog_mode wm;
69edcef5fcSpooka pid_t p1, p2;
70edcef5fcSpooka int status;
71edcef5fcSpooka int fd;
72edcef5fcSpooka
73edcef5fcSpooka signal(SIGUSR1, sigcount);
74edcef5fcSpooka
75edcef5fcSpooka switch ((p1 = fork())) {
76edcef5fcSpooka case 0:
77edcef5fcSpooka break;
78edcef5fcSpooka case -1:
79edcef5fcSpooka atf_tc_fail_errno("fork");
80edcef5fcSpooka break;
81edcef5fcSpooka default:
82edcef5fcSpooka p2 = wait(&status);
83edcef5fcSpooka ATF_REQUIRE_EQ(p1, p2);
847127b715Spgoyette ATF_REQUIRE_EQ(tcount, max);
85edcef5fcSpooka return status;
86edcef5fcSpooka }
87edcef5fcSpooka
88edcef5fcSpooka rump_init();
89edcef5fcSpooka
90edcef5fcSpooka fd = rump_sys_open("/dev/watchdog", O_RDWR);
91edcef5fcSpooka if (fd == -1)
92edcef5fcSpooka err(1, "open watchdog");
93edcef5fcSpooka
94edcef5fcSpooka wc.wc_count = 1;
95edcef5fcSpooka wc.wc_names = wname;
96edcef5fcSpooka
97edcef5fcSpooka if (rump_sys_ioctl(fd, WDOGIOC_GWDOGS, &wc) == -1)
98edcef5fcSpooka err(1, "can't fetch watchdog names");
99edcef5fcSpooka
100edcef5fcSpooka if (wc.wc_count) {
101edcef5fcSpooka assert(wc.wc_count == 1);
102edcef5fcSpooka
103edcef5fcSpooka strlcpy(wm.wm_name, wc.wc_names, sizeof(wm.wm_name));
104edcef5fcSpooka wm.wm_mode = WDOG_MODE_ETICKLE;
105edcef5fcSpooka wm.wm_period = 1;
106edcef5fcSpooka if (rump_sys_ioctl(fd, WDOGIOC_SMODE, &wm) == -1)
107edcef5fcSpooka atf_tc_fail_errno("failed to set tickle");
108edcef5fcSpooka
109c829d168Spgoyette usleep(400000);
1107127b715Spgoyette if (max == 1)
111edcef5fcSpooka rump_sys_ioctl(fd, WDOGIOC_TICKLE);
1127127b715Spgoyette else {
1137127b715Spgoyette wm.wm_mode = WDOG_MODE_DISARMED;
1147127b715Spgoyette rump_sys_ioctl(fd, WDOGIOC_SMODE, &wm);
1157127b715Spgoyette }
116edcef5fcSpooka kill(getppid(), SIGUSR1);
117edcef5fcSpooka
118edcef5fcSpooka sleep(2);
119edcef5fcSpooka printf("staying alive\n");
1207127b715Spgoyette kill(getppid(), SIGUSR1);
1217127b715Spgoyette _exit(2);
122edcef5fcSpooka }
123edcef5fcSpooka /* fail */
1248f206471Spgoyette printf("no watchdog registered!\n");
125edcef5fcSpooka _exit(1);
126edcef5fcSpooka }
127edcef5fcSpooka
128edcef5fcSpooka ATF_TC(reboot);
ATF_TC_HEAD(reboot,tc)129edcef5fcSpooka ATF_TC_HEAD(reboot, tc)
130edcef5fcSpooka {
131edcef5fcSpooka
132edcef5fcSpooka atf_tc_set_md_var(tc, "descr", "check swwdog reboot capability");
133edcef5fcSpooka }
134edcef5fcSpooka
ATF_TC_BODY(reboot,tc)135edcef5fcSpooka ATF_TC_BODY(reboot, tc)
136edcef5fcSpooka {
137edcef5fcSpooka extern bool rumpns_swwdog_reboot;
138edcef5fcSpooka int status;
139edcef5fcSpooka
140edcef5fcSpooka /* XXX: should use sysctl */
141edcef5fcSpooka rumpns_swwdog_reboot = true;
1427127b715Spgoyette status = testbody(1);
143edcef5fcSpooka
144edcef5fcSpooka ATF_REQUIRE(WIFEXITED(status));
145edcef5fcSpooka ATF_REQUIRE_EQ(WEXITSTATUS(status), 0);
146edcef5fcSpooka }
147edcef5fcSpooka
148edcef5fcSpooka ATF_TC(panic);
ATF_TC_HEAD(panic,tc)149edcef5fcSpooka ATF_TC_HEAD(panic, tc)
150edcef5fcSpooka {
151edcef5fcSpooka
152edcef5fcSpooka atf_tc_set_md_var(tc, "descr", "check swwdog panic capability");
153edcef5fcSpooka }
154edcef5fcSpooka
ATF_TC_BODY(panic,tc)155edcef5fcSpooka ATF_TC_BODY(panic, tc)
156edcef5fcSpooka {
157edcef5fcSpooka extern bool rumpns_swwdog_reboot;
158edcef5fcSpooka int status;
159edcef5fcSpooka
160edcef5fcSpooka /* XXX: should use sysctl */
161edcef5fcSpooka rumpns_swwdog_reboot = false;
1627127b715Spgoyette status = testbody(1);
163edcef5fcSpooka
164edcef5fcSpooka ATF_REQUIRE(WIFSIGNALED(status));
165edcef5fcSpooka ATF_REQUIRE_EQ(WTERMSIG(status), SIGABRT);
166edcef5fcSpooka }
167edcef5fcSpooka
1687127b715Spgoyette ATF_TC(disarm);
ATF_TC_HEAD(disarm,tc)1697127b715Spgoyette ATF_TC_HEAD(disarm, tc)
1707127b715Spgoyette {
1717127b715Spgoyette
1727127b715Spgoyette atf_tc_set_md_var(tc, "descr", "check swwdog disarm capability");
1737127b715Spgoyette }
1747127b715Spgoyette
ATF_TC_BODY(disarm,tc)1757127b715Spgoyette ATF_TC_BODY(disarm, tc)
1767127b715Spgoyette {
1777127b715Spgoyette int status;
1787127b715Spgoyette
1797127b715Spgoyette status = testbody(2);
1807127b715Spgoyette
1817127b715Spgoyette ATF_REQUIRE(WIFEXITED(status));
1827127b715Spgoyette ATF_REQUIRE_EQ(WEXITSTATUS(status), 2);
1837127b715Spgoyette }
1847127b715Spgoyette
ATF_TP_ADD_TCS(tp)185edcef5fcSpooka ATF_TP_ADD_TCS(tp)
186edcef5fcSpooka {
187edcef5fcSpooka
188edcef5fcSpooka ATF_TP_ADD_TC(tp, panic);
189edcef5fcSpooka ATF_TP_ADD_TC(tp, reboot);
1907127b715Spgoyette ATF_TP_ADD_TC(tp, disarm);
191edcef5fcSpooka
192edcef5fcSpooka return atf_no_error();
193edcef5fcSpooka }
194