11.7Schristos/*	$NetBSD: t_swwdog.c,v 1.7 2017/01/13 21:30:39 christos 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.5Schristos#include <signal.h>
421.1Spooka
431.1Spooka#include <rump/rump.h>
441.1Spooka#include <rump/rump_syscalls.h>
451.1Spooka
461.7Schristos#include "h_macros.h"
471.1Spooka
481.2Spookastatic volatile sig_atomic_t tcount;
491.1Spooka
501.1Spookastatic void
511.1Spookasigcount(int sig)
521.1Spooka{
531.1Spooka
541.1Spooka	assert(sig == SIGUSR1);
551.1Spooka	tcount++;
561.1Spooka}
571.1Spooka
581.1Spooka/*
591.1Spooka * Since we are testing for swwdog's ability to reboot/panic, we need
601.1Spooka * to fork and monitor the exit status from the parent and report
611.1Spooka * something sensible back to atf.
621.1Spooka */
631.1Spookastatic int
641.3Spgoyettetestbody(int max)
651.1Spooka{
661.1Spooka	char wname[WDOG_NAMESIZE];
671.1Spooka	struct wdog_conf wc;
681.1Spooka	struct wdog_mode wm;
691.1Spooka	pid_t p1, p2;
701.1Spooka	int status;
711.1Spooka	int fd;
721.1Spooka
731.1Spooka	signal(SIGUSR1, sigcount);
741.1Spooka
751.1Spooka	switch ((p1 = fork())) {
761.1Spooka	case 0:
771.1Spooka		break;
781.1Spooka	case -1:
791.1Spooka		atf_tc_fail_errno("fork");
801.1Spooka		break;
811.1Spooka	default:
821.1Spooka		p2 = wait(&status);
831.1Spooka		ATF_REQUIRE_EQ(p1, p2);
841.3Spgoyette		ATF_REQUIRE_EQ(tcount, max);
851.1Spooka		return status;
861.1Spooka	}
871.1Spooka
881.1Spooka	rump_init();
891.1Spooka
901.1Spooka	fd = rump_sys_open("/dev/watchdog", O_RDWR);
911.1Spooka	if (fd == -1)
921.1Spooka		err(1, "open watchdog");
931.1Spooka
941.1Spooka	wc.wc_count = 1;
951.1Spooka	wc.wc_names = wname;
961.1Spooka
971.1Spooka	if (rump_sys_ioctl(fd, WDOGIOC_GWDOGS, &wc) == -1)
981.1Spooka		err(1, "can't fetch watchdog names");
991.1Spooka
1001.1Spooka	if (wc.wc_count) {
1011.1Spooka		assert(wc.wc_count == 1);
1021.1Spooka
1031.1Spooka		strlcpy(wm.wm_name, wc.wc_names, sizeof(wm.wm_name));
1041.1Spooka		wm.wm_mode = WDOG_MODE_ETICKLE;
1051.1Spooka		wm.wm_period = 1;
1061.1Spooka		if (rump_sys_ioctl(fd, WDOGIOC_SMODE, &wm) == -1)
1071.1Spooka			atf_tc_fail_errno("failed to set tickle");
1081.1Spooka
1091.4Spgoyette		usleep(400000);
1101.3Spgoyette		if (max == 1)
1111.3Spgoyette			rump_sys_ioctl(fd, WDOGIOC_TICKLE);
1121.3Spgoyette		else {
1131.3Spgoyette			wm.wm_mode = WDOG_MODE_DISARMED;
1141.3Spgoyette			rump_sys_ioctl(fd, WDOGIOC_SMODE, &wm);
1151.3Spgoyette		}
1161.1Spooka		kill(getppid(), SIGUSR1);
1171.1Spooka
1181.1Spooka		sleep(2);
1191.1Spooka		printf("staying alive\n");
1201.3Spgoyette		kill(getppid(), SIGUSR1);
1211.3Spgoyette		_exit(2);
1221.1Spooka	}
1231.1Spooka	/* fail */
1241.6Spgoyette	printf("no watchdog registered!\n");
1251.1Spooka	_exit(1);
1261.1Spooka}
1271.1Spooka
1281.1SpookaATF_TC(reboot);
1291.1SpookaATF_TC_HEAD(reboot, tc)
1301.1Spooka{
1311.1Spooka
1321.1Spooka	atf_tc_set_md_var(tc, "descr", "check swwdog reboot capability");
1331.1Spooka}
1341.1Spooka
1351.1SpookaATF_TC_BODY(reboot, tc)
1361.1Spooka{
1371.1Spooka	extern bool rumpns_swwdog_reboot;
1381.1Spooka	int status;
1391.1Spooka
1401.1Spooka	/* XXX: should use sysctl */
1411.1Spooka	rumpns_swwdog_reboot = true;
1421.3Spgoyette	status = testbody(1);
1431.1Spooka
1441.1Spooka	ATF_REQUIRE(WIFEXITED(status));
1451.1Spooka	ATF_REQUIRE_EQ(WEXITSTATUS(status), 0);
1461.1Spooka}
1471.1Spooka
1481.1SpookaATF_TC(panic);
1491.1SpookaATF_TC_HEAD(panic, tc)
1501.1Spooka{
1511.1Spooka
1521.1Spooka	atf_tc_set_md_var(tc, "descr", "check swwdog panic capability");
1531.1Spooka}
1541.1Spooka
1551.1SpookaATF_TC_BODY(panic, tc)
1561.1Spooka{
1571.1Spooka	extern bool rumpns_swwdog_reboot;
1581.1Spooka	int status;
1591.1Spooka
1601.1Spooka	/* XXX: should use sysctl */
1611.1Spooka	rumpns_swwdog_reboot = false;
1621.3Spgoyette	status = testbody(1);
1631.1Spooka
1641.1Spooka	ATF_REQUIRE(WIFSIGNALED(status));
1651.1Spooka	ATF_REQUIRE_EQ(WTERMSIG(status), SIGABRT);
1661.1Spooka}
1671.1Spooka
1681.3SpgoyetteATF_TC(disarm);
1691.3SpgoyetteATF_TC_HEAD(disarm, tc)
1701.3Spgoyette{
1711.3Spgoyette
1721.3Spgoyette	atf_tc_set_md_var(tc, "descr", "check swwdog disarm capability");
1731.3Spgoyette}
1741.3Spgoyette
1751.3SpgoyetteATF_TC_BODY(disarm, tc)
1761.3Spgoyette{
1771.3Spgoyette	int status;
1781.3Spgoyette
1791.3Spgoyette	status = testbody(2);
1801.3Spgoyette
1811.3Spgoyette	ATF_REQUIRE(WIFEXITED(status));
1821.3Spgoyette	ATF_REQUIRE_EQ(WEXITSTATUS(status), 2);
1831.3Spgoyette}
1841.3Spgoyette
1851.1SpookaATF_TP_ADD_TCS(tp)
1861.1Spooka{
1871.1Spooka
1881.1Spooka	ATF_TP_ADD_TC(tp, panic);
1891.1Spooka	ATF_TP_ADD_TC(tp, reboot);
1901.3Spgoyette	ATF_TP_ADD_TC(tp, disarm);
1911.1Spooka
1921.1Spooka	return atf_no_error();
1931.1Spooka}
194