]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - contrib/netbsd-tests/dev/sysmon/t_swwdog.c
MFC r314450,r313439:
[FreeBSD/stable/10.git] / contrib / netbsd-tests / dev / sysmon / t_swwdog.c
1 /*      $NetBSD: t_swwdog.c,v 1.7 2017/01/13 21:30:39 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         printf("no watchdog registered!\n");
125         _exit(1);
126 }
127
128 ATF_TC(reboot);
129 ATF_TC_HEAD(reboot, tc)
130 {
131
132         atf_tc_set_md_var(tc, "descr", "check swwdog reboot capability");
133 }
134
135 ATF_TC_BODY(reboot, tc)
136 {
137         extern bool rumpns_swwdog_reboot;
138         int status;
139
140         /* XXX: should use sysctl */
141         rumpns_swwdog_reboot = true;
142         status = testbody(1);
143
144         ATF_REQUIRE(WIFEXITED(status));
145         ATF_REQUIRE_EQ(WEXITSTATUS(status), 0);
146 }
147
148 ATF_TC(panic);
149 ATF_TC_HEAD(panic, tc)
150 {
151
152         atf_tc_set_md_var(tc, "descr", "check swwdog panic capability");
153 }
154
155 ATF_TC_BODY(panic, tc)
156 {
157         extern bool rumpns_swwdog_reboot;
158         int status;
159
160         /* XXX: should use sysctl */
161         rumpns_swwdog_reboot = false;
162         status = testbody(1);
163
164         ATF_REQUIRE(WIFSIGNALED(status));
165         ATF_REQUIRE_EQ(WTERMSIG(status), SIGABRT);
166 }
167
168 ATF_TC(disarm);
169 ATF_TC_HEAD(disarm, tc)
170 {
171
172         atf_tc_set_md_var(tc, "descr", "check swwdog disarm capability");
173 }
174
175 ATF_TC_BODY(disarm, tc)
176 {
177         int status;
178
179         status = testbody(2);
180
181         ATF_REQUIRE(WIFEXITED(status));
182         ATF_REQUIRE_EQ(WEXITSTATUS(status), 2);
183 }
184
185 ATF_TP_ADD_TCS(tp)
186 {
187
188         ATF_TP_ADD_TC(tp, panic);
189         ATF_TP_ADD_TC(tp, reboot);
190         ATF_TP_ADD_TC(tp, disarm);
191
192         return atf_no_error();
193 }