Home | History | Annotate | Line # | Download | only in sys
t_sigaction.c revision 1.1
      1  1.1  jruoho /* $NetBSD: t_sigaction.c,v 1.1 2011/10/15 07:00:48 jruoho Exp $ */
      2  1.1  jruoho 
      3  1.1  jruoho /*-
      4  1.1  jruoho  * Copyright (c) 2010 The NetBSD Foundation, Inc.
      5  1.1  jruoho  * All rights reserved.
      6  1.1  jruoho  *
      7  1.1  jruoho  * Redistribution and use in source and binary forms, with or without
      8  1.1  jruoho  * modification, are permitted provided that the following conditions
      9  1.1  jruoho  * are met:
     10  1.1  jruoho  * 1. Redistributions of source code must retain the above copyright
     11  1.1  jruoho  *    notice, this list of conditions and the following disclaimer.
     12  1.1  jruoho  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  jruoho  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  jruoho  *    documentation and/or other materials provided with the distribution.
     15  1.1  jruoho  *
     16  1.1  jruoho  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  1.1  jruoho  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  1.1  jruoho  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  1.1  jruoho  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  1.1  jruoho  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  1.1  jruoho  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  1.1  jruoho  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  1.1  jruoho  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  1.1  jruoho  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  1.1  jruoho  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  1.1  jruoho  * POSSIBILITY OF SUCH DAMAGE.
     27  1.1  jruoho  */
     28  1.1  jruoho 
     29  1.1  jruoho #include <sys/cdefs.h>
     30  1.1  jruoho __COPYRIGHT("@(#) Copyright (c) 2010\
     31  1.1  jruoho  The NetBSD Foundation, inc. All rights reserved.");
     32  1.1  jruoho __RCSID("$NetBSD: t_sigaction.c,v 1.1 2011/10/15 07:00:48 jruoho Exp $");
     33  1.1  jruoho 
     34  1.1  jruoho #include <sys/wait.h>
     35  1.1  jruoho 
     36  1.1  jruoho #include <signal.h>
     37  1.1  jruoho #include <stdbool.h>
     38  1.1  jruoho #include <stdlib.h>
     39  1.1  jruoho #include <string.h>
     40  1.1  jruoho #include <unistd.h>
     41  1.1  jruoho 
     42  1.1  jruoho #include <atf-c.h>
     43  1.1  jruoho #include <atf-c/config.h>
     44  1.1  jruoho 
     45  1.1  jruoho #include "../../../h_macros.h"
     46  1.1  jruoho 
     47  1.1  jruoho static bool handler_called = false;
     48  1.1  jruoho 
     49  1.1  jruoho static void
     50  1.1  jruoho handler(int signo)
     51  1.1  jruoho {
     52  1.1  jruoho     handler_called = true;
     53  1.1  jruoho }
     54  1.1  jruoho 
     55  1.1  jruoho static void
     56  1.1  jruoho sa_resethand_child(const int flags)
     57  1.1  jruoho {
     58  1.1  jruoho     struct sigaction sa;
     59  1.1  jruoho 
     60  1.1  jruoho     sa.sa_flags = flags;
     61  1.1  jruoho     sa.sa_handler = &handler;
     62  1.1  jruoho     sigemptyset(&sa.sa_mask);
     63  1.1  jruoho 
     64  1.1  jruoho     sigaction(SIGUSR1, &sa, NULL);
     65  1.1  jruoho     kill(getpid(), SIGUSR1);
     66  1.1  jruoho     exit(handler_called ? EXIT_SUCCESS : EXIT_FAILURE);
     67  1.1  jruoho }
     68  1.1  jruoho 
     69  1.1  jruoho static void
     70  1.1  jruoho wait_and_check_child(const pid_t pid, const char *fail_message)
     71  1.1  jruoho {
     72  1.1  jruoho 	int status;
     73  1.1  jruoho 
     74  1.1  jruoho 	(void)waitpid(pid, &status, 0);
     75  1.1  jruoho 
     76  1.1  jruoho 	if (WIFEXITED(status))
     77  1.1  jruoho 		ATF_CHECK_EQ(EXIT_SUCCESS, WEXITSTATUS(status));
     78  1.1  jruoho 	else
     79  1.1  jruoho 		atf_tc_fail("%s; raw exit status was %d", fail_message, status);
     80  1.1  jruoho }
     81  1.1  jruoho 
     82  1.1  jruoho ATF_TC(sigaction_noflags);
     83  1.1  jruoho ATF_TC_HEAD(sigaction_noflags, tc)
     84  1.1  jruoho {
     85  1.1  jruoho 	atf_tc_set_md_var(tc, "descr", "Checks programming a signal with "
     86  1.1  jruoho 	    "sigaction(2) but without any flags");
     87  1.1  jruoho }
     88  1.1  jruoho 
     89  1.1  jruoho ATF_TC_BODY(sigaction_noflags, tc)
     90  1.1  jruoho {
     91  1.1  jruoho 	const pid_t pid = fork();
     92  1.1  jruoho 	if (pid == -1)
     93  1.1  jruoho 		atf_tc_fail_errno("fork(2) failed");
     94  1.1  jruoho 	else if (pid == 0)
     95  1.1  jruoho 		sa_resethand_child(0);
     96  1.1  jruoho 	else
     97  1.1  jruoho 		wait_and_check_child(pid, "Child process did not exit cleanly;"
     98  1.1  jruoho 		    " it failed to process the signal");
     99  1.1  jruoho }
    100  1.1  jruoho 
    101  1.1  jruoho ATF_TC(sigaction_resethand);
    102  1.1  jruoho ATF_TC_HEAD(sigaction_resethand, tc)
    103  1.1  jruoho {
    104  1.1  jruoho 	atf_tc_set_md_var(tc, "descr", "Checks that SA_RESETHAND works");
    105  1.1  jruoho }
    106  1.1  jruoho 
    107  1.1  jruoho ATF_TC_BODY(sigaction_resethand, tc)
    108  1.1  jruoho {
    109  1.1  jruoho 	const pid_t pid = fork();
    110  1.1  jruoho 	if (pid == -1)
    111  1.1  jruoho 		atf_tc_fail_errno("fork(2) failed");
    112  1.1  jruoho 	else if (pid == 0)
    113  1.1  jruoho 		sa_resethand_child(SA_RESETHAND);
    114  1.1  jruoho 	else {
    115  1.1  jruoho 		wait_and_check_child(pid, "Child process did not exit cleanly;"
    116  1.1  jruoho 		    " it either failed to process the signal or SA_RESETHAND"
    117  1.1  jruoho 		    " is broken");
    118  1.1  jruoho 	}
    119  1.1  jruoho }
    120  1.1  jruoho 
    121  1.1  jruoho ATF_TP_ADD_TCS(tp)
    122  1.1  jruoho {
    123  1.1  jruoho 
    124  1.1  jruoho 	ATF_TP_ADD_TC(tp, sigaction_noflags);
    125  1.1  jruoho 	ATF_TP_ADD_TC(tp, sigaction_resethand);
    126  1.1  jruoho 
    127  1.1  jruoho 	return atf_no_error();
    128  1.1  jruoho }
    129