Home | History | Annotate | Download | only in sigaction
      1 /*
      2  *
      3  *   Copyright (c) International Business Machines  Corp., 2001
      4  *
      5  *   This program is free software;  you can redistribute it and/or modify
      6  *   it under the terms of the GNU General Public License as published by
      7  *   the Free Software Foundation; either version 2 of the License, or
      8  *   (at your option) any later version.
      9  *
     10  *   This program is distributed in the hope that it will be useful,
     11  *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
     12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
     13  *   the GNU General Public License for more details.
     14  *
     15  *   You should have received a copy of the GNU General Public License
     16  *   along with this program;  if not, write to the Free Software
     17  *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
     18  */
     19 
     20 /*
     21  * NAME
     22  * 	sigaction02.c
     23  *
     24  * DESCRIPTION
     25  * 	Testcase to check the basic errnos set by the sigaction(2) syscall.
     26  *
     27  * ALGORITHM
     28  *	1. Pass an invalid signal as the "sig" parameter, and expect EINVAL.
     29  *	2. Attempt to catch the SIGKILL, and expect EINVAL.
     30  *	3. Attempt to catch the SIGSTOP, and expect EINVAL.
     31  *	4. Pass an invalid address as the "act" parameter, expect an EFAULT.
     32  *	5. Pass an invalid address as the "oact" parameter, and expect EFAULT.
     33  *
     34  * USAGE
     35  *	sigaction02
     36  *
     37  * HISTORY
     38  *	07/2001 Ported by Wayne Boyer
     39  *
     40  * RESTRICTIONS
     41  *	Tests #4 and #5 will fail as long as the glibc implementation
     42  *	of sigaction() is not fixed.  The glibc wrapper around of sigaction()
     43  *	doesn't handle the invalid addresses of the "act" and "oact" parameters
     44  *	correctly.  If an invalid address is passed, glibc dumps core.
     45  *	Temporarily, tests 4 and 5 are put inside "#ifdef GLIBC_SIGACTION_BUG"
     46  *	in order to skip these tests.  This should be removed from the Makefile
     47  *	and this program when the glibc bug gets fixed.
     48  *
     49  *	This test doesn't follow the correct LTP format - PLEASE FIX!
     50  */
     51 #define DEBUG 0
     52 #include <stdio.h>
     53 #include <stdlib.h>
     54 #include <unistd.h>
     55 #include <signal.h>
     56 #include <errno.h>
     57 #include "test.h"
     58 
     59 #define SIGBAD 9999
     60 
     61 void setup();
     62 void cleanup();
     63 
     64 char *TCID = "sigaction02";
     65 int TST_TOTAL = 1;
     66 
     67 volatile sig_atomic_t testcase_no;
     68 
     69 /*
     70  * handler()
     71  *	A dummy signal handler for attempting to catch signals.
     72  */
     73 void handler(int sig)
     74 {
     75 	if (DEBUG)
     76 		tst_resm(TINFO, "Inside signal handler. Got signal: %d", sig);
     77 	return;
     78 }
     79 
     80 /*
     81  * set_handler()
     82  * 	Establish a signal handler for "sig" with the specified flags and
     83  * 	signal to mask while the handler executes.
     84  * Returns
     85  *	0 on success, errno on failure
     86  */
     87 int set_handler(int sig, int sig_to_mask, int flag)
     88 {
     89 	struct sigaction sa;
     90 	int err;
     91 
     92 	if (flag == 0) {
     93 		sa.sa_sigaction = (void *)handler;
     94 		sa.sa_flags = SA_NOMASK;
     95 		sigemptyset(&sa.sa_mask);
     96 		sigaddset(&sa.sa_mask, sig_to_mask);
     97 		err = sigaction(sig, &sa, NULL);
     98 	} else if (flag == 1) {
     99 		err = sigaction(sig, (void *)-1, NULL);
    100 	} else if (flag == 2) {
    101 		err = sigaction(sig, NULL, (void *)-1);
    102 	} else
    103 		err = -1;
    104 
    105 	if (err == 0)
    106 		return 0;
    107 	else
    108 		return errno;
    109 }
    110 
    111 int main(int ac, char **av)
    112 {
    113 	int ret;
    114 
    115 	tst_parse_opts(ac, av, NULL, NULL);
    116 //test1:
    117 	testcase_no = 1;
    118 
    119 	if (DEBUG)
    120 		tst_resm(TINFO, "Enter test %d: set handler for SIGKILL",
    121 			 testcase_no);
    122 	if ((ret = set_handler(SIGKILL, 0, 0)) == 0) {
    123 		tst_resm(TFAIL, "sigaction() succeeded, should have failed");
    124 	}
    125 	if (ret != EINVAL) {
    126 		tst_resm(TFAIL, "sigaction set incorrect errno. Expected "
    127 			 "EINVAL, got: %d", ret);
    128 	} else {
    129 		tst_resm(TPASS, "call failed with expected EINVAL error");
    130 	}
    131 
    132 //test2:
    133 	testcase_no++;
    134 
    135 	if (DEBUG)
    136 		tst_resm(TINFO, "Enter test %d: set handler for SIGSTOP",
    137 			 testcase_no);
    138 	if ((ret = set_handler(SIGSTOP, 0, 0)) == 0) {
    139 		tst_resm(TFAIL, "sigaction() succeeded, should have failed");
    140 	}
    141 	if (ret != EINVAL) {
    142 		tst_resm(TFAIL, "sigaction set incorrect errno. Expected "
    143 			 "EINVAL, got: %d", ret);
    144 	} else {
    145 		tst_resm(TPASS, "call failed with expected EINVAL error");
    146 	}
    147 
    148 //test3:
    149 	testcase_no++;
    150 	if (DEBUG)
    151 		tst_resm(TINFO, "Enter test %d: set handler for bad "
    152 			 "signal number", testcase_no);
    153 	if ((ret = set_handler(SIGBAD, 0, 0)) == 0) {
    154 		tst_resm(TFAIL, "sigaction() succeeded, should have failed");
    155 	}
    156 	if (ret != EINVAL) {
    157 		tst_resm(TFAIL, "sigaction set incorrect errno. Expected "
    158 			 "EINVAL, got: %d", ret);
    159 	} else {
    160 		tst_resm(TPASS, "call failed with expected EINVAL error");
    161 	}
    162 
    163 #ifndef GLIBC_SIGACTION_BUG
    164 
    165 //test4:
    166 	testcase_no++;
    167 	if (DEBUG)
    168 		tst_resm(TINFO, "Enter test %d: set handler with "
    169 			 "bad \"act\" param", testcase_no);
    170 	if ((ret = set_handler(SIGUSR1, 0, 1)) == 0) {
    171 		tst_resm(TFAIL, "sigaction() succeeded, should have failed");
    172 	}
    173 	if (ret != EFAULT) {
    174 		tst_resm(TFAIL, "sigaction set incorrect errno. Expected "
    175 			 "EFAULT, got: %d", ret);
    176 	} else {
    177 		tst_resm(TPASS, "call failed with expected EFAULT error");
    178 	}
    179 
    180 //test5:
    181 	testcase_no++;
    182 	if (DEBUG)
    183 		tst_resm(TINFO, "Enter test %d: set handler with "
    184 			 "bad \"oact\" param", testcase_no);
    185 	if ((ret = set_handler(SIGUSR1, 0, 2)) == 0) {
    186 		tst_resm(TFAIL, "sigaction() succeeded, should have failed");
    187 	}
    188 	if (ret != EFAULT) {
    189 		tst_resm(TFAIL, "sigaction set incorrect errno. Expected "
    190 			 "EFAULT, got: %d", ret);
    191 	} else {
    192 		tst_resm(TPASS, "call failed with expected EFAULT error");
    193 	}
    194 #endif /* GLIBC_SIGACTION_BUG */
    195 
    196 	tst_exit();
    197 
    198 }
    199