1272343Sngie/* $NetBSD: t_alarm.c,v 1.2 2011/05/10 06:58:17 jruoho Exp $ */ 2272343Sngie 3272343Sngie/*- 4272343Sngie * Copyright (c) 2011 The NetBSD Foundation, Inc. 5272343Sngie * All rights reserved. 6272343Sngie * 7272343Sngie * This code is derived from software contributed to The NetBSD Foundation 8272343Sngie * by Jukka Ruohonen. 9272343Sngie * 10272343Sngie * Redistribution and use in source and binary forms, with or without 11272343Sngie * modification, are permitted provided that the following conditions 12272343Sngie * are met: 13272343Sngie * 1. Redistributions of source code must retain the above copyright 14272343Sngie * notice, this list of conditions and the following disclaimer. 15272343Sngie * 2. Redistributions in binary form must reproduce the above copyright 16272343Sngie * notice, this list of conditions and the following disclaimer in the 17272343Sngie * documentation and/or other materials provided with the distribution. 18272343Sngie * 19272343Sngie * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20272343Sngie * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21272343Sngie * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22272343Sngie * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23272343Sngie * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24272343Sngie * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25272343Sngie * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26272343Sngie * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27272343Sngie * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28272343Sngie * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29272343Sngie * POSSIBILITY OF SUCH DAMAGE. 30272343Sngie */ 31272343Sngie#include <sys/cdefs.h> 32272343Sngie__RCSID("$NetBSD: t_alarm.c,v 1.2 2011/05/10 06:58:17 jruoho Exp $"); 33272343Sngie 34272343Sngie#include <sys/wait.h> 35272343Sngie 36272343Sngie#include <atf-c.h> 37272343Sngie#include <stdlib.h> 38272343Sngie#include <signal.h> 39272343Sngie#include <unistd.h> 40272343Sngie 41272343Sngiestatic bool fail; 42272343Sngiestatic void handler(int); 43272343Sngie 44272343Sngiestatic void 45272343Sngiehandler(int signo) 46272343Sngie{ 47272343Sngie 48272343Sngie if (signo == SIGALRM) 49272343Sngie fail = false; 50272343Sngie} 51272343Sngie 52272343SngieATF_TC(alarm_basic); 53272343SngieATF_TC_HEAD(alarm_basic, tc) 54272343Sngie{ 55272343Sngie atf_tc_set_md_var(tc, "descr", "A basic test of alarm(3)"); 56272343Sngie} 57272343Sngie 58272343SngieATF_TC_BODY(alarm_basic, tc) 59272343Sngie{ 60272343Sngie 61272343Sngie ATF_REQUIRE(signal(SIGALRM, handler) == 0); 62272343Sngie 63272343Sngie fail = true; 64272343Sngie 65272343Sngie (void)alarm(1); 66272343Sngie (void)sleep(2); 67272343Sngie 68272343Sngie if (fail != false) 69272343Sngie atf_tc_fail("alarm(3) failed to deliver signal"); 70272343Sngie} 71272343Sngie 72272343SngieATF_TC(alarm_fork); 73272343SngieATF_TC_HEAD(alarm_fork, tc) 74272343Sngie{ 75272343Sngie atf_tc_set_md_var(tc, "descr", "Does fork(2) clear a pending alarm?"); 76272343Sngie} 77272343Sngie 78272343SngieATF_TC_BODY(alarm_fork, tc) 79272343Sngie{ 80272343Sngie unsigned int rv; 81272343Sngie pid_t pid; 82272343Sngie int sta; 83272343Sngie 84272343Sngie /* 85272343Sngie * Any pending alarms should be 86272343Sngie * cleared in the child process. 87272343Sngie */ 88272343Sngie (void)alarm(60); 89272343Sngie 90272343Sngie pid = fork(); 91272343Sngie ATF_REQUIRE(pid >= 0); 92272343Sngie 93272343Sngie if (pid == 0) { 94272343Sngie 95272343Sngie rv = alarm(0); 96272343Sngie 97272343Sngie if (rv != 0) 98272343Sngie _exit(EXIT_FAILURE); 99272343Sngie 100272343Sngie _exit(EXIT_SUCCESS); 101272343Sngie } 102272343Sngie 103272343Sngie (void)alarm(0); 104272343Sngie (void)wait(&sta); 105272343Sngie 106272343Sngie if (WIFEXITED(sta) == 0 || WEXITSTATUS(sta) != EXIT_SUCCESS) 107272343Sngie atf_tc_fail("pending alarm was not cleared for child"); 108272343Sngie} 109272343Sngie 110272343SngieATF_TC(alarm_previous); 111272343SngieATF_TC_HEAD(alarm_previous, tc) 112272343Sngie{ 113272343Sngie atf_tc_set_md_var(tc, "descr", "Test return value from alarm(3)"); 114272343Sngie} 115272343Sngie 116272343SngieATF_TC_BODY(alarm_previous, tc) 117272343Sngie{ 118272343Sngie unsigned int rv; 119272343Sngie 120272343Sngie /* 121272343Sngie * See that alarm(3) returns the amount 122272343Sngie * left on the timer from the previous call. 123272343Sngie */ 124272343Sngie rv = alarm(60); 125272343Sngie 126272343Sngie if (rv != 0) 127272343Sngie goto fail; 128272343Sngie 129272343Sngie rv = alarm(0); 130272343Sngie 131272343Sngie if (rv < 50) 132272343Sngie goto fail; 133272343Sngie 134272343Sngie (void)alarm(0); 135272343Sngie 136272343Sngie return; 137272343Sngie 138272343Sngiefail: 139272343Sngie atf_tc_fail("invalid return value from alarm(3)"); 140272343Sngie} 141272343Sngie 142272343SngieATF_TP_ADD_TCS(tp) 143272343Sngie{ 144272343Sngie 145272343Sngie ATF_TP_ADD_TC(tp, alarm_basic); 146272343Sngie ATF_TP_ADD_TC(tp, alarm_fork); 147272343Sngie ATF_TP_ADD_TC(tp, alarm_previous); 148272343Sngie 149272343Sngie return atf_no_error(); 150272343Sngie} 151