Remove building with NOCRYPTO option
[minix.git] / tests / dev / sysmon / t_swwdog.c
blob1635884447eae8d3248a9b5a4ace566cb75515b1
1 /* $NetBSD: t_swwdog.c,v 1.6 2015/04/23 04:49:37 pgoyette Exp $ */
3 /*
4 * Copyright (c) 2010 Antti Kantee. All Rights Reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
16 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
28 #include <sys/types.h>
29 #include <sys/wait.h>
30 #include <sys/wdog.h>
32 #include <assert.h>
33 #include <atf-c.h>
34 #include <err.h>
35 #include <errno.h>
36 #include <fcntl.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <unistd.h>
41 #include <signal.h>
43 #include <rump/rump.h>
44 #include <rump/rump_syscalls.h>
46 #include "../../h_macros.h"
48 static volatile sig_atomic_t tcount;
50 static void
51 sigcount(int sig)
54 assert(sig == SIGUSR1);
55 tcount++;
59 * Since we are testing for swwdog's ability to reboot/panic, we need
60 * to fork and monitor the exit status from the parent and report
61 * something sensible back to atf.
63 static int
64 testbody(int max)
66 char wname[WDOG_NAMESIZE];
67 struct wdog_conf wc;
68 struct wdog_mode wm;
69 pid_t p1, p2;
70 int status;
71 int fd;
73 signal(SIGUSR1, sigcount);
75 switch ((p1 = fork())) {
76 case 0:
77 break;
78 case -1:
79 atf_tc_fail_errno("fork");
80 break;
81 default:
82 p2 = wait(&status);
83 ATF_REQUIRE_EQ(p1, p2);
84 ATF_REQUIRE_EQ(tcount, max);
85 return status;
88 rump_init();
90 fd = rump_sys_open("/dev/watchdog", O_RDWR);
91 if (fd == -1)
92 err(1, "open watchdog");
94 wc.wc_count = 1;
95 wc.wc_names = wname;
97 if (rump_sys_ioctl(fd, WDOGIOC_GWDOGS, &wc) == -1)
98 err(1, "can't fetch watchdog names");
100 if (wc.wc_count) {
101 assert(wc.wc_count == 1);
103 strlcpy(wm.wm_name, wc.wc_names, sizeof(wm.wm_name));
104 wm.wm_mode = WDOG_MODE_ETICKLE;
105 wm.wm_period = 1;
106 if (rump_sys_ioctl(fd, WDOGIOC_SMODE, &wm) == -1)
107 atf_tc_fail_errno("failed to set tickle");
109 usleep(400000);
110 if (max == 1)
111 rump_sys_ioctl(fd, WDOGIOC_TICKLE);
112 else {
113 wm.wm_mode = WDOG_MODE_DISARMED;
114 rump_sys_ioctl(fd, WDOGIOC_SMODE, &wm);
116 kill(getppid(), SIGUSR1);
118 sleep(2);
119 printf("staying alive\n");
120 kill(getppid(), SIGUSR1);
121 _exit(2);
123 /* fail */
124 printf("no watchdog registered!\n");
125 _exit(1);
128 ATF_TC(reboot);
129 ATF_TC_HEAD(reboot, tc)
132 atf_tc_set_md_var(tc, "descr", "check swwdog reboot capability");
135 ATF_TC_BODY(reboot, tc)
137 extern bool rumpns_swwdog_reboot;
138 int status;
140 /* XXX: should use sysctl */
141 rumpns_swwdog_reboot = true;
142 status = testbody(1);
144 ATF_REQUIRE(WIFEXITED(status));
145 ATF_REQUIRE_EQ(WEXITSTATUS(status), 0);
148 ATF_TC(panic);
149 ATF_TC_HEAD(panic, tc)
152 atf_tc_set_md_var(tc, "descr", "check swwdog panic capability");
155 ATF_TC_BODY(panic, tc)
157 extern bool rumpns_swwdog_reboot;
158 int status;
160 /* XXX: should use sysctl */
161 rumpns_swwdog_reboot = false;
162 status = testbody(1);
164 ATF_REQUIRE(WIFSIGNALED(status));
165 ATF_REQUIRE_EQ(WTERMSIG(status), SIGABRT);
168 ATF_TC(disarm);
169 ATF_TC_HEAD(disarm, tc)
172 atf_tc_set_md_var(tc, "descr", "check swwdog disarm capability");
175 ATF_TC_BODY(disarm, tc)
177 int status;
179 status = testbody(2);
181 ATF_REQUIRE(WIFEXITED(status));
182 ATF_REQUIRE_EQ(WEXITSTATUS(status), 2);
185 ATF_TP_ADD_TCS(tp)
188 ATF_TP_ADD_TC(tp, panic);
189 ATF_TP_ADD_TC(tp, reboot);
190 ATF_TP_ADD_TC(tp, disarm);
192 return atf_no_error();