• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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@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 #include "safe_macros.h"
52 #include "lapi/signal.h"
53 
54 /* _XOPEN_SOURCE disables NSIG */
55 #ifndef NSIG
56 # define NSIG _NSIG
57 #endif
58 
59 /* ensure NUMSIGS is defined */
60 #ifndef NUMSIGS
61 # define NUMSIGS NSIG
62 #endif
63 
64 char *TCID = "sighold02";
65 int TST_TOTAL = 2;
66 
67 static int pid;
68 static void do_child(void);
69 static void setup(void);
70 static void cleanup(void);
71 
72 static int sigs_catched;
73 static int sigs_map[NUMSIGS];
74 
skip_sig(int sig)75 static int skip_sig(int sig)
76 {
77 	if (sig >= __SIGRTMIN && sig < SIGRTMIN)
78 		return 1;
79 
80 	switch (sig) {
81 	case SIGCHLD:
82 	case SIGKILL:
83 	case SIGALRM:
84 	case SIGSTOP:
85 		return 1;
86 	default:
87 		return 0;
88 	}
89 }
90 
main(int ac,char ** av)91 int main(int ac, char **av)
92 {
93 	int sig;
94 	int lc;
95 
96 	tst_parse_opts(ac, av, NULL, NULL);
97 
98 #ifdef UCLINUX
99 	maybe_run_child(&do_child, "");
100 #endif
101 
102 	setup();
103 
104 	for (lc = 0; TEST_LOOPING(lc); lc++) {
105 		if ((pid = FORK_OR_VFORK()) < 0) {
106 			tst_brkm(TBROK | TERRNO, NULL, "fork() failed");
107 		} else if (pid > 0) {
108 			TST_SAFE_CHECKPOINT_WAIT(NULL, 0);
109 
110 			for (sig = 1; sig < NUMSIGS; sig++) {
111 				if (skip_sig(sig))
112 					continue;
113 				SAFE_KILL(NULL, pid, sig);
114 			}
115 
116 			TST_SAFE_CHECKPOINT_WAKE(NULL, 0);
117 			tst_record_childstatus(cleanup, pid);
118 		} else {
119 
120 #ifdef UCLINUX
121 			if (self_exec(av[0], "") < 0) {
122 				tst_brkm(TBROK | TERRNO, NULL,
123 					 "self_exec() failed");
124 			}
125 #else
126 			do_child();
127 #endif
128 		}
129 	}
130 
131 	cleanup();
132 	tst_exit();
133 }
134 
handle_sigs(int sig)135 static void handle_sigs(int sig)
136 {
137 	sigs_map[sig] = 1;
138 	sigs_catched++;
139 }
140 
do_child(void)141 void do_child(void)
142 {
143 	int cnt;
144 	int sig;
145 
146 	/* set up signal handler routine */
147 	for (sig = 1; sig < NUMSIGS; sig++) {
148 		if (skip_sig(sig))
149 			continue;
150 
151 		if (signal(sig, handle_sigs) == SIG_ERR) {
152 			tst_resm(TBROK | TERRNO, "signal() %i(%s) failed",
153 				 sig, tst_strsig(sig));
154 		}
155 	}
156 
157 	/* all set up to catch signals, now hold them */
158 	for (cnt = 0, sig = 1; sig < NUMSIGS; sig++) {
159 		if (skip_sig(sig))
160 			continue;
161 		cnt++;
162 		TEST(sighold(sig));
163 		if (TEST_RETURN != 0) {
164 			tst_resm(TBROK | TTERRNO, "sighold() %i(%s) failed",
165 				 sig, tst_strsig(sig));
166 		}
167 	}
168 
169 	TST_SAFE_CHECKPOINT_WAKE_AND_WAIT(NULL, 0);
170 
171 	if (!sigs_catched) {
172 		tst_resm(TPASS, "All signals were hold");
173 		tst_exit();
174 	}
175 
176 	tst_resm(TFAIL, "Signal handler was executed");
177 
178 	for (sig = 1; sig < NUMSIGS; sig++) {
179 		if (sigs_map[sig]) {
180 			tst_resm(TINFO, "Signal %i(%s) catched",
181 			         sig, tst_strsig(sig));
182 		}
183 	}
184 
185 	tst_exit();
186 }
187 
setup(void)188 static void setup(void)
189 {
190 	tst_sig(FORK, DEF_HANDLER, NULL);
191 
192 	tst_tmpdir();
193 
194 	TST_CHECKPOINT_INIT(tst_rmdir);
195 
196 	TEST_PAUSE;
197 }
198 
cleanup(void)199 static void cleanup(void)
200 {
201 	tst_rmdir();
202 }
203