t_swwdog.c revision 1.3
11.3Spgoyette/*	$NetBSD: t_swwdog.c,v 1.3 2010/10/24 13:11:41 pgoyette Exp $	*/
21.1Spooka
31.1Spooka/*
41.1Spooka * Copyright (c) 2010 Antti Kantee.  All Rights Reserved.
51.1Spooka *
61.1Spooka * Redistribution and use in source and binary forms, with or without
71.1Spooka * modification, are permitted provided that the following conditions
81.1Spooka * are met:
91.1Spooka * 1. Redistributions of source code must retain the above copyright
101.1Spooka *    notice, this list of conditions and the following disclaimer.
111.1Spooka * 2. Redistributions in binary form must reproduce the above copyright
121.1Spooka *    notice, this list of conditions and the following disclaimer in the
131.1Spooka *    documentation and/or other materials provided with the distribution.
141.1Spooka *
151.1Spooka * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
161.1Spooka * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
171.1Spooka * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
181.1Spooka * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
191.1Spooka * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
201.1Spooka * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
211.1Spooka * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
221.1Spooka * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
231.1Spooka * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
241.1Spooka * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
251.1Spooka * SUCH DAMAGE.
261.1Spooka */
271.1Spooka
281.1Spooka#include <sys/types.h>
291.1Spooka#include <sys/wait.h>
301.1Spooka#include <sys/wdog.h>
311.1Spooka
321.1Spooka#include <assert.h>
331.1Spooka#include <atf-c.h>
341.1Spooka#include <err.h>
351.1Spooka#include <errno.h>
361.1Spooka#include <fcntl.h>
371.1Spooka#include <stdio.h>
381.1Spooka#include <stdlib.h>
391.1Spooka#include <string.h>
401.1Spooka#include <unistd.h>
411.1Spooka
421.1Spooka#include <rump/rump.h>
431.1Spooka#include <rump/rump_syscalls.h>
441.1Spooka
451.1Spooka#include "../../h_macros.h"
461.1Spooka
471.2Spookastatic volatile sig_atomic_t tcount;
481.1Spooka
491.1Spookastatic void
501.1Spookasigcount(int sig)
511.1Spooka{
521.1Spooka
531.1Spooka	assert(sig == SIGUSR1);
541.1Spooka	tcount++;
551.1Spooka}
561.1Spooka
571.1Spooka/*
581.1Spooka * Since we are testing for swwdog's ability to reboot/panic, we need
591.1Spooka * to fork and monitor the exit status from the parent and report
601.1Spooka * something sensible back to atf.
611.1Spooka */
621.1Spookastatic int
631.3Spgoyettetestbody(int max)
641.1Spooka{
651.1Spooka	char wname[WDOG_NAMESIZE];
661.1Spooka	struct wdog_conf wc;
671.1Spooka	struct wdog_mode wm;
681.1Spooka	pid_t p1, p2;
691.1Spooka	int status;
701.1Spooka	int fd;
711.1Spooka
721.1Spooka	signal(SIGUSR1, sigcount);
731.1Spooka
741.1Spooka	switch ((p1 = fork())) {
751.1Spooka	case 0:
761.1Spooka		break;
771.1Spooka	case -1:
781.1Spooka		atf_tc_fail_errno("fork");
791.1Spooka		break;
801.1Spooka	default:
811.1Spooka		p2 = wait(&status);
821.1Spooka		ATF_REQUIRE_EQ(p1, p2);
831.3Spgoyette		ATF_REQUIRE_EQ(tcount, max);
841.1Spooka		return status;
851.1Spooka	}
861.1Spooka
871.1Spooka	rump_init();
881.1Spooka
891.1Spooka	fd = rump_sys_open("/dev/watchdog", O_RDWR);
901.1Spooka	if (fd == -1)
911.1Spooka		err(1, "open watchdog");
921.1Spooka
931.1Spooka	wc.wc_count = 1;
941.1Spooka	wc.wc_names = wname;
951.1Spooka
961.1Spooka	if (rump_sys_ioctl(fd, WDOGIOC_GWDOGS, &wc) == -1)
971.1Spooka		err(1, "can't fetch watchdog names");
981.1Spooka
991.1Spooka	if (wc.wc_count) {
1001.1Spooka		assert(wc.wc_count == 1);
1011.1Spooka
1021.1Spooka		strlcpy(wm.wm_name, wc.wc_names, sizeof(wm.wm_name));
1031.1Spooka		wm.wm_mode = WDOG_MODE_ETICKLE;
1041.1Spooka		wm.wm_period = 1;
1051.1Spooka		if (rump_sys_ioctl(fd, WDOGIOC_SMODE, &wm) == -1)
1061.1Spooka			atf_tc_fail_errno("failed to set tickle");
1071.1Spooka
1081.1Spooka		usleep(500000);
1091.3Spgoyette		if (max == 1)
1101.3Spgoyette			rump_sys_ioctl(fd, WDOGIOC_TICKLE);
1111.3Spgoyette		else {
1121.3Spgoyette			wm.wm_mode = WDOG_MODE_DISARMED;
1131.3Spgoyette			rump_sys_ioctl(fd, WDOGIOC_SMODE, &wm);
1141.3Spgoyette		}
1151.1Spooka		kill(getppid(), SIGUSR1);
1161.1Spooka
1171.1Spooka		sleep(2);
1181.1Spooka		printf("staying alive\n");
1191.3Spgoyette		kill(getppid(), SIGUSR1);
1201.3Spgoyette		_exit(2);
1211.1Spooka	}
1221.1Spooka	/* fail */
1231.1Spooka	_exit(1);
1241.1Spooka}
1251.1Spooka
1261.1SpookaATF_TC(reboot);
1271.1SpookaATF_TC_HEAD(reboot, tc)
1281.1Spooka{
1291.1Spooka
1301.1Spooka	atf_tc_set_md_var(tc, "descr", "check swwdog reboot capability");
1311.1Spooka}
1321.1Spooka
1331.1SpookaATF_TC_BODY(reboot, tc)
1341.1Spooka{
1351.1Spooka	extern bool rumpns_swwdog_reboot;
1361.1Spooka	int status;
1371.1Spooka
1381.1Spooka	/* XXX: should use sysctl */
1391.1Spooka	rumpns_swwdog_reboot = true;
1401.3Spgoyette	status = testbody(1);
1411.1Spooka
1421.1Spooka	ATF_REQUIRE(WIFEXITED(status));
1431.1Spooka	ATF_REQUIRE_EQ(WEXITSTATUS(status), 0);
1441.1Spooka}
1451.1Spooka
1461.1SpookaATF_TC(panic);
1471.1SpookaATF_TC_HEAD(panic, tc)
1481.1Spooka{
1491.1Spooka
1501.1Spooka	atf_tc_set_md_var(tc, "descr", "check swwdog panic capability");
1511.1Spooka}
1521.1Spooka
1531.1SpookaATF_TC_BODY(panic, tc)
1541.1Spooka{
1551.1Spooka	extern bool rumpns_swwdog_reboot;
1561.1Spooka	int status;
1571.1Spooka
1581.1Spooka	/* XXX: should use sysctl */
1591.1Spooka	rumpns_swwdog_reboot = false;
1601.3Spgoyette	status = testbody(1);
1611.1Spooka
1621.1Spooka	ATF_REQUIRE(WIFSIGNALED(status));
1631.1Spooka	ATF_REQUIRE_EQ(WTERMSIG(status), SIGABRT);
1641.1Spooka}
1651.1Spooka
1661.3SpgoyetteATF_TC(disarm);
1671.3SpgoyetteATF_TC_HEAD(disarm, tc)
1681.3Spgoyette{
1691.3Spgoyette
1701.3Spgoyette	atf_tc_set_md_var(tc, "descr", "check swwdog disarm capability");
1711.3Spgoyette}
1721.3Spgoyette
1731.3SpgoyetteATF_TC_BODY(disarm, tc)
1741.3Spgoyette{
1751.3Spgoyette	int status;
1761.3Spgoyette
1771.3Spgoyette	status = testbody(2);
1781.3Spgoyette
1791.3Spgoyette	ATF_REQUIRE(WIFEXITED(status));
1801.3Spgoyette	ATF_REQUIRE_EQ(WEXITSTATUS(status), 2);
1811.3Spgoyette}
1821.3Spgoyette
1831.1SpookaATF_TP_ADD_TCS(tp)
1841.1Spooka{
1851.1Spooka
1861.1Spooka	ATF_TP_ADD_TC(tp, panic);
1871.1Spooka	ATF_TP_ADD_TC(tp, reboot);
1881.3Spgoyette	ATF_TP_ADD_TC(tp, disarm);
1891.1Spooka
1901.1Spooka	return atf_no_error();
1911.1Spooka}
192