Home | History | Annotate | Download | only in sighold
      1 /*
      2  * Copyright (c) 2000 Silicon Graphics, Inc.  All Rights Reserved.
      3  *  AUTHOR          : Bob Clark
      4  *  CO-PILOT        : Barrie Kletscher
      5  *  DATE STARTED    : 9/26/86
      6  * Copyright (C) 2015 Cyril Hrubis <chrubis (at) suse.cz>
      7  *
      8  * This program is free software; you can redistribute it and/or modify it
      9  * under the terms of version 2 of the GNU General Public License as
     10  * published by the Free Software Foundation.
     11  *
     12  * This program is distributed in the hope that it would be useful, but
     13  * WITHOUT ANY WARRANTY; without even the implied warranty of
     14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
     15  *
     16  * Further, this software is distributed without any warranty that it is
     17  * free of the rightful claim of any third person regarding infringement
     18  * or the like.  Any license provided herein, whether implied or
     19  * otherwise, applies only to this software file.  Patent licenses, if
     20  * any, provided herein do not apply to combinations of this program with
     21  * other software, or any other product whatsoever.
     22  *
     23  * You should have received a copy of the GNU General Public License along
     24  * with this program; if not, write the Free Software Foundation, Inc.,
     25  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
     26  *
     27  * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
     28  * Mountain View, CA  94043, or:
     29  *
     30  * http://www.sgi.com
     31  *
     32  * For further information regarding this notice, see:
     33  *
     34  * http://oss.sgi.com/projects/GenInfo/NoticeExplan/
     35  */
     36 /*
     37  * TEST ITEMS
     38  *	1. sighold action to turn off the receipt of all signals was done
     39  *	   without error.
     40  *	2. After signals were held, and sent, no signals were trapped.
     41  */
     42 #define _XOPEN_SOURCE 500
     43 #include <errno.h>
     44 #include <signal.h>
     45 #include <string.h>
     46 #include <fcntl.h>
     47 #include <stdlib.h>
     48 #include <sys/types.h>
     49 #include <sys/wait.h>
     50 #include "test.h"
     51 
     52 /* _XOPEN_SOURCE disables NSIG */
     53 #ifndef NSIG
     54 # define NSIG _NSIG
     55 #endif
     56 
     57 /* Needed for NPTL */
     58 #define SIGCANCEL 32
     59 #define SIGTIMER 33
     60 
     61 /* ensure NUMSIGS is defined */
     62 #ifndef NUMSIGS
     63 # define NUMSIGS NSIG
     64 #endif
     65 
     66 char *TCID = "sighold02";
     67 int TST_TOTAL = 2;
     68 
     69 static int pid;
     70 static void do_child(void);
     71 static void setup(void);
     72 static void cleanup(void);
     73 
     74 static int sigs_catched;
     75 static int sigs_map[NUMSIGS];
     76 
     77 static int skip_sig(int sig)
     78 {
     79 	switch (sig) {
     80 	case SIGCLD:
     81 	case SIGKILL:
     82 	case SIGALRM:
     83 	case SIGSTOP:
     84 	case SIGCANCEL:
     85 	case SIGTIMER:
     86 		return 1;
     87 	default:
     88 		return 0;
     89 	}
     90 }
     91 
     92 int main(int ac, char **av)
     93 {
     94 	int sig;
     95 	int lc;
     96 
     97 	tst_parse_opts(ac, av, NULL, NULL);
     98 
     99 #ifdef UCLINUX
    100 	maybe_run_child(&do_child, "");
    101 #endif
    102 
    103 	setup();
    104 
    105 	for (lc = 0; TEST_LOOPING(lc); lc++) {
    106 		if ((pid = FORK_OR_VFORK()) < 0) {
    107 			tst_brkm(TBROK | TERRNO, NULL, "fork() failed");
    108 		} else if (pid > 0) {
    109 			TST_SAFE_CHECKPOINT_WAIT(NULL, 0);
    110 
    111 			for (sig = 1; sig < NUMSIGS; sig++) {
    112 				if (skip_sig(sig))
    113 					continue;
    114 				if (kill(pid, sig) < 0) {
    115 					tst_brkm(TBROK | TERRNO, NULL,
    116 						 "kill(%d, %d(%s)) failed",
    117 						 pid, sig, tst_strsig(sig));
    118 				}
    119 			}
    120 
    121 			TST_SAFE_CHECKPOINT_WAKE(NULL, 0);
    122 			tst_record_childstatus(cleanup, pid);
    123 		} else {
    124 
    125 #ifdef UCLINUX
    126 			if (self_exec(av[0], "") < 0) {
    127 				tst_brkm(TBROK | TERRNO, NULL,
    128 					 "self_exec() failed");
    129 			}
    130 #else
    131 			do_child();
    132 #endif
    133 		}
    134 	}
    135 
    136 	cleanup();
    137 	tst_exit();
    138 }
    139 
    140 static void handle_sigs(int sig)
    141 {
    142 	sigs_map[sig] = 1;
    143 	sigs_catched++;
    144 }
    145 
    146 void do_child(void)
    147 {
    148 	int cnt;
    149 	int sig;
    150 
    151 	/* set up signal handler routine */
    152 	for (sig = 1; sig < NUMSIGS; sig++) {
    153 		if (skip_sig(sig))
    154 			continue;
    155 
    156 		if (signal(sig, handle_sigs) == SIG_ERR) {
    157 			tst_resm(TBROK | TERRNO, "signal() %i(%s) failed",
    158 				 sig, tst_strsig(sig));
    159 		}
    160 	}
    161 
    162 	/* all set up to catch signals, now hold them */
    163 	for (cnt = 0, sig = 1; sig < NUMSIGS; sig++) {
    164 		if (skip_sig(sig))
    165 			continue;
    166 		cnt++;
    167 		TEST(sighold(sig));
    168 		if (TEST_RETURN != 0) {
    169 			tst_resm(TBROK | TTERRNO, "sighold() %i(%s) failed",
    170 				 sig, tst_strsig(sig));
    171 		}
    172 	}
    173 
    174 	TST_SAFE_CHECKPOINT_WAKE_AND_WAIT(NULL, 0);
    175 
    176 	if (!sigs_catched) {
    177 		tst_resm(TPASS, "All signals were hold");
    178 		tst_exit();
    179 	}
    180 
    181 	tst_resm(TFAIL, "Signal handler was executed");
    182 
    183 	for (sig = 1; sig < NUMSIGS; sig++) {
    184 		if (sigs_map[sig]) {
    185 			tst_resm(TINFO, "Signal %i(%s) catched",
    186 			         sig, tst_strsig(sig));
    187 		}
    188 	}
    189 
    190 	tst_exit();
    191 }
    192 
    193 static void setup(void)
    194 {
    195 	tst_sig(FORK, DEF_HANDLER, NULL);
    196 
    197 	tst_tmpdir();
    198 
    199 	TST_CHECKPOINT_INIT(tst_rmdir);
    200 
    201 	TEST_PAUSE;
    202 }
    203 
    204 static void cleanup(void)
    205 {
    206 	tst_rmdir();
    207 }
    208