Home | History | Annotate | Line # | Download | only in gen
t_raise.c revision 1.4
      1 /*	$NetBSD: t_raise.c,v 1.4 2011/05/09 09:27:37 jruoho Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2011 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jukka Ruohonen.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 #include <sys/cdefs.h>
     32 __RCSID("$NetBSD: t_raise.c,v 1.4 2011/05/09 09:27:37 jruoho Exp $");
     33 
     34 #include <atf-c.h>
     35 
     36 #include <signal.h>
     37 #include <string.h>
     38 #include <time.h>
     39 #include <unistd.h>
     40 
     41 static bool	fail;
     42 static void	handler_err(int);
     43 static void	handler_ret(int);
     44 static int	sig[] = { SIGALRM, SIGIO, SIGUSR1, SIGUSR2, SIGPWR };
     45 
     46 static void
     47 handler_err(int signo)
     48 {
     49 	size_t i;
     50 
     51 	for (i = 0; i < __arraycount(sig); i++) {
     52 
     53 		if (sig[i] == signo) {
     54 			fail = false;
     55 			break;
     56 		}
     57 	}
     58 }
     59 
     60 static void
     61 handler_ret(int signo)
     62 {
     63 
     64 	(void)sleep(1);
     65 
     66 	fail = false;
     67 }
     68 
     69 ATF_TC(raise_err);
     70 ATF_TC_HEAD(raise_err, tc)
     71 {
     72 	atf_tc_set_md_var(tc, "descr", "Test raise(3) for invalid parameters");
     73 }
     74 
     75 ATF_TC_BODY(raise_err, tc)
     76 {
     77 	int i = 0;
     78 
     79 	while (i < 10) {
     80 
     81 		ATF_REQUIRE(raise(10240 + i) == -1);
     82 
     83 		i++;
     84 	}
     85 }
     86 
     87 ATF_TC(raise_ret);
     88 ATF_TC_HEAD(raise_ret, tc)
     89 {
     90 	atf_tc_set_md_var(tc, "descr", "Test return order of raise(3)");
     91 }
     92 
     93 ATF_TC_BODY(raise_ret, tc)
     94 {
     95 	struct sigaction sa;
     96 
     97 	fail = true;
     98 
     99 	sa.sa_flags = 0;
    100 	sa.sa_handler = handler_ret;
    101 
    102 	/*
    103 	 * Verify that raise(3) does not return
    104 	 * before the signal handler returns.
    105 	 */
    106 	ATF_REQUIRE(sigemptyset(&sa.sa_mask) == 0);
    107 	ATF_REQUIRE(sigaction(SIGUSR1, &sa, 0) == 0);
    108 	ATF_REQUIRE(raise(SIGUSR1) == 0);
    109 
    110 	if (fail != false)
    111 		atf_tc_fail("raise(3) returned before signal handler");
    112 }
    113 
    114 ATF_TC(raise_sig);
    115 ATF_TC_HEAD(raise_sig, tc)
    116 {
    117 	atf_tc_set_md_var(tc, "descr", "A basic test of raise(3)");
    118 }
    119 
    120 ATF_TC_BODY(raise_sig, tc)
    121 {
    122 	struct timespec tv, tr;
    123 	struct sigaction sa;
    124 	size_t i;
    125 
    126 	for (i = 0; i < __arraycount(sig); i++) {
    127 
    128 		(void)memset(&sa, 0, sizeof(struct sigaction));
    129 
    130 		fail = true;
    131 
    132 		tv.tv_sec = 0;
    133 		tv.tv_nsec = 2;
    134 
    135 		sa.sa_flags = 0;
    136 		sa.sa_handler = handler_err;
    137 
    138 		ATF_REQUIRE(sigemptyset(&sa.sa_mask) == 0);
    139 		ATF_REQUIRE(sigaction(sig[i], &sa, 0) == 0);
    140 
    141 		ATF_REQUIRE(raise(sig[i]) == 0);
    142 		ATF_REQUIRE(nanosleep(&tv, &tr) == 0);
    143 
    144 		if (fail != false)
    145 			atf_tc_fail("raise(3) did not raise a signal");
    146 	}
    147 }
    148 
    149 ATF_TP_ADD_TCS(tp)
    150 {
    151 	ATF_TP_ADD_TC(tp, raise_err);
    152 	ATF_TP_ADD_TC(tp, raise_ret);
    153 	ATF_TP_ADD_TC(tp, raise_sig);
    154 
    155 	return atf_no_error();
    156 }
    157