xref: /freebsd/contrib/netbsd-tests/dev/sysmon/t_swwdog.c (revision 38d120bc13ac1de5b739b67b87016b9122149374)
1 /*	$NetBSD: t_swwdog.c,v 1.5 2011/06/26 12:14:59 christos Exp $	*/
2 
3 /*
4  * Copyright (c) 2010 Antti Kantee.  All Rights Reserved.
5  *
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.
14  *
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.
26  */
27 
28 #include <sys/types.h>
29 #include <sys/wait.h>
30 #include <sys/wdog.h>
31 
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>
42 
43 #include <rump/rump.h>
44 #include <rump/rump_syscalls.h>
45 
46 #include "../../h_macros.h"
47 
48 static volatile sig_atomic_t tcount;
49 
50 static void
51 sigcount(int sig)
52 {
53 
54 	assert(sig == SIGUSR1);
55 	tcount++;
56 }
57 
58 /*
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.
62  */
63 static int
64 testbody(int max)
65 {
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;
72 
73 	signal(SIGUSR1, sigcount);
74 
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;
86 	}
87 
88 	rump_init();
89 
90 	fd = rump_sys_open("/dev/watchdog", O_RDWR);
91 	if (fd == -1)
92 		err(1, "open watchdog");
93 
94 	wc.wc_count = 1;
95 	wc.wc_names = wname;
96 
97 	if (rump_sys_ioctl(fd, WDOGIOC_GWDOGS, &wc) == -1)
98 		err(1, "can't fetch watchdog names");
99 
100 	if (wc.wc_count) {
101 		assert(wc.wc_count == 1);
102 
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");
108 
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);
115 		}
116 		kill(getppid(), SIGUSR1);
117 
118 		sleep(2);
119 		printf("staying alive\n");
120 		kill(getppid(), SIGUSR1);
121 		_exit(2);
122 	}
123 	/* fail */
124 	_exit(1);
125 }
126 
127 ATF_TC(reboot);
128 ATF_TC_HEAD(reboot, tc)
129 {
130 
131 	atf_tc_set_md_var(tc, "descr", "check swwdog reboot capability");
132 }
133 
134 ATF_TC_BODY(reboot, tc)
135 {
136 	extern bool rumpns_swwdog_reboot;
137 	int status;
138 
139 	/* XXX: should use sysctl */
140 	rumpns_swwdog_reboot = true;
141 	status = testbody(1);
142 
143 	ATF_REQUIRE(WIFEXITED(status));
144 	ATF_REQUIRE_EQ(WEXITSTATUS(status), 0);
145 }
146 
147 ATF_TC(panic);
148 ATF_TC_HEAD(panic, tc)
149 {
150 
151 	atf_tc_set_md_var(tc, "descr", "check swwdog panic capability");
152 }
153 
154 ATF_TC_BODY(panic, tc)
155 {
156 	extern bool rumpns_swwdog_reboot;
157 	int status;
158 
159 	/* XXX: should use sysctl */
160 	rumpns_swwdog_reboot = false;
161 	status = testbody(1);
162 
163 	ATF_REQUIRE(WIFSIGNALED(status));
164 	ATF_REQUIRE_EQ(WTERMSIG(status), SIGABRT);
165 }
166 
167 ATF_TC(disarm);
168 ATF_TC_HEAD(disarm, tc)
169 {
170 
171 	atf_tc_set_md_var(tc, "descr", "check swwdog disarm capability");
172 }
173 
174 ATF_TC_BODY(disarm, tc)
175 {
176 	int status;
177 
178 	status = testbody(2);
179 
180 	ATF_REQUIRE(WIFEXITED(status));
181 	ATF_REQUIRE_EQ(WEXITSTATUS(status), 2);
182 }
183 
184 ATF_TP_ADD_TCS(tp)
185 {
186 
187 	ATF_TP_ADD_TC(tp, panic);
188 	ATF_TP_ADD_TC(tp, reboot);
189 	ATF_TP_ADD_TC(tp, disarm);
190 
191 	return atf_no_error();
192 }
193