Annotation of src/tests/dev/sysmon/t_swwdog.c, Revision 1.4
1.4 ! pgoyette 1: /* $NetBSD: t_swwdog.c,v 1.3 2010/10/24 13:11:41 pgoyette Exp $ */
1.1 pooka 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:
1.2 pooka 47: static volatile sig_atomic_t tcount;
1.1 pooka 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
1.3 pgoyette 63: testbody(int max)
1.1 pooka 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);
1.3 pgoyette 83: ATF_REQUIRE_EQ(tcount, max);
1.1 pooka 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:
1.4 ! pgoyette 108: usleep(400000);
1.3 pgoyette 109: if (max == 1)
110: rump_sys_ioctl(fd, WDOGIOC_TICKLE);
111: else {
112: wm.wm_mode = WDOG_MODE_DISARMED;
113: rump_sys_ioctl(fd, WDOGIOC_SMODE, &wm);
114: }
1.1 pooka 115: kill(getppid(), SIGUSR1);
116:
117: sleep(2);
118: printf("staying alive\n");
1.3 pgoyette 119: kill(getppid(), SIGUSR1);
120: _exit(2);
1.1 pooka 121: }
122: /* fail */
123: _exit(1);
124: }
125:
126: ATF_TC(reboot);
127: ATF_TC_HEAD(reboot, tc)
128: {
129:
130: atf_tc_set_md_var(tc, "descr", "check swwdog reboot capability");
131: }
132:
133: ATF_TC_BODY(reboot, tc)
134: {
135: extern bool rumpns_swwdog_reboot;
136: int status;
137:
138: /* XXX: should use sysctl */
139: rumpns_swwdog_reboot = true;
1.3 pgoyette 140: status = testbody(1);
1.1 pooka 141:
142: ATF_REQUIRE(WIFEXITED(status));
143: ATF_REQUIRE_EQ(WEXITSTATUS(status), 0);
144: }
145:
146: ATF_TC(panic);
147: ATF_TC_HEAD(panic, tc)
148: {
149:
150: atf_tc_set_md_var(tc, "descr", "check swwdog panic capability");
151: }
152:
153: ATF_TC_BODY(panic, tc)
154: {
155: extern bool rumpns_swwdog_reboot;
156: int status;
157:
158: /* XXX: should use sysctl */
159: rumpns_swwdog_reboot = false;
1.3 pgoyette 160: status = testbody(1);
1.1 pooka 161:
162: ATF_REQUIRE(WIFSIGNALED(status));
163: ATF_REQUIRE_EQ(WTERMSIG(status), SIGABRT);
164: }
165:
1.3 pgoyette 166: ATF_TC(disarm);
167: ATF_TC_HEAD(disarm, tc)
168: {
169:
170: atf_tc_set_md_var(tc, "descr", "check swwdog disarm capability");
171: }
172:
173: ATF_TC_BODY(disarm, tc)
174: {
175: int status;
176:
177: status = testbody(2);
178:
179: ATF_REQUIRE(WIFEXITED(status));
180: ATF_REQUIRE_EQ(WEXITSTATUS(status), 2);
181: }
182:
1.1 pooka 183: ATF_TP_ADD_TCS(tp)
184: {
185:
186: ATF_TP_ADD_TC(tp, panic);
187: ATF_TP_ADD_TC(tp, reboot);
1.3 pgoyette 188: ATF_TP_ADD_TC(tp, disarm);
1.1 pooka 189:
190: return atf_no_error();
191: }
CVSweb <webmaster@jp.NetBSD.org>