xref: /netbsd-src/tests/dev/sysmon/t_swwdog.c (revision b1c86f5f087524e68db12794ee9c3e3da1ab17a0)
1 /*	$NetBSD: t_swwdog.c,v 1.2 2010/08/30 08:30:17 pooka 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 
42 #include <rump/rump.h>
43 #include <rump/rump_syscalls.h>
44 
45 #include "../../h_macros.h"
46 
47 static volatile sig_atomic_t tcount;
48 
49 static void
50 sigcount(int sig)
51 {
52 
53 	assert(sig == SIGUSR1);
54 	tcount++;
55 }
56 
57 /*
58  * Since we are testing for swwdog's ability to reboot/panic, we need
59  * to fork and monitor the exit status from the parent and report
60  * something sensible back to atf.
61  */
62 static int
63 testbody(void)
64 {
65 	char wname[WDOG_NAMESIZE];
66 	struct wdog_conf wc;
67 	struct wdog_mode wm;
68 	pid_t p1, p2;
69 	int status;
70 	int fd;
71 
72 	signal(SIGUSR1, sigcount);
73 
74 	switch ((p1 = fork())) {
75 	case 0:
76 		break;
77 	case -1:
78 		atf_tc_fail_errno("fork");
79 		break;
80 	default:
81 		p2 = wait(&status);
82 		ATF_REQUIRE_EQ(p1, p2);
83 		ATF_REQUIRE_EQ(tcount, 1);
84 		return status;
85 	}
86 
87 	rump_init();
88 
89 	fd = rump_sys_open("/dev/watchdog", O_RDWR);
90 	if (fd == -1)
91 		err(1, "open watchdog");
92 
93 	wc.wc_count = 1;
94 	wc.wc_names = wname;
95 
96 	if (rump_sys_ioctl(fd, WDOGIOC_GWDOGS, &wc) == -1)
97 		err(1, "can't fetch watchdog names");
98 
99 	if (wc.wc_count) {
100 		assert(wc.wc_count == 1);
101 
102 		strlcpy(wm.wm_name, wc.wc_names, sizeof(wm.wm_name));
103 		wm.wm_mode = WDOG_MODE_ETICKLE;
104 		wm.wm_period = 1;
105 		if (rump_sys_ioctl(fd, WDOGIOC_SMODE, &wm) == -1)
106 			atf_tc_fail_errno("failed to set tickle");
107 
108 		usleep(500000);
109 		rump_sys_ioctl(fd, WDOGIOC_TICKLE);
110 		kill(getppid(), SIGUSR1);
111 
112 		sleep(2);
113 		printf("staying alive\n");
114 	}
115 	/* fail */
116 	_exit(1);
117 }
118 
119 ATF_TC(reboot);
120 ATF_TC_HEAD(reboot, tc)
121 {
122 
123 	atf_tc_set_md_var(tc, "descr", "check swwdog reboot capability");
124 }
125 
126 ATF_TC_BODY(reboot, tc)
127 {
128 	extern bool rumpns_swwdog_reboot;
129 	int status;
130 
131 	/* XXX: should use sysctl */
132 	rumpns_swwdog_reboot = true;
133 	status = testbody();
134 
135 	ATF_REQUIRE(WIFEXITED(status));
136 	ATF_REQUIRE_EQ(WEXITSTATUS(status), 0);
137 }
138 
139 ATF_TC(panic);
140 ATF_TC_HEAD(panic, tc)
141 {
142 
143 	atf_tc_set_md_var(tc, "descr", "check swwdog panic capability");
144 }
145 
146 ATF_TC_BODY(panic, tc)
147 {
148 	extern bool rumpns_swwdog_reboot;
149 	int status;
150 
151 	/* XXX: should use sysctl */
152 	rumpns_swwdog_reboot = false;
153 	status = testbody();
154 
155 	ATF_REQUIRE(WIFSIGNALED(status));
156 	ATF_REQUIRE_EQ(WTERMSIG(status), SIGABRT);
157 }
158 
159 ATF_TP_ADD_TCS(tp)
160 {
161 
162 	ATF_TP_ADD_TC(tp, panic);
163 	ATF_TP_ADD_TC(tp, reboot);
164 
165 	return atf_no_error();
166 }
167