blob: de5edb77cffac5758b9bb5cab04ad0429c8c6eba [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
2 * Copyright (c) 2000 Silicon Graphics, Inc. All Rights Reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11 *
12 * Further, this software is distributed without any warranty that it is
13 * free of the rightful claim of any third person regarding infringement
14 * or the like. Any license provided herein, whether implied or
15 * otherwise, applies only to this software file. Patent licenses, if
16 * any, provided herein do not apply to combinations of this program with
17 * other software, or any other product whatsoever.
18 *
19 * You should have received a copy of the GNU General Public License along
Wanlong Gaofed96412012-10-24 10:10:29 +080020 * with this program; if not, write the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
plars865695b2001-08-27 22:15:12 +000022 *
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
25 *
26 * http://www.sgi.com
27 *
28 * For further information regarding this notice, see:
29 *
30 * http://oss.sgi.com/projects/GenInfo/NoticeExplan/
31 *
32 */
vapier74813062009-08-28 13:58:53 +000033/* $Id: signal01.c,v 1.11 2009/08/28 13:58:53 vapier Exp $ */
plars865695b2001-08-27 22:15:12 +000034/***********************************************************************************
subrata_modak4bb656a2009-02-26 12:02:09 +000035 *
plars865695b2001-08-27 22:15:12 +000036 * OS Test - Silicon Graphics, Inc. Eagan, Minnesota
subrata_modak4bb656a2009-02-26 12:02:09 +000037 *
nstrazfa31d552002-05-14 16:50:06 +000038 * TEST IDENTIFIER : signal01 Boundary value and other invalid value checking
plars865695b2001-08-27 22:15:12 +000039 * of signal setup and signal sending.
subrata_modak4bb656a2009-02-26 12:02:09 +000040 *
plars865695b2001-08-27 22:15:12 +000041 * PARENT DOCUMENT : sgntds01 Signal System Call
Garrett Cooper2c282152010-12-16 00:55:50 -080042 *
plars865695b2001-08-27 22:15:12 +000043 * AUTHOR : Dave Baumgartner
44 * : Rewrote 12/92 by Richard Logan
subrata_modak4bb656a2009-02-26 12:02:09 +000045 *
plars865695b2001-08-27 22:15:12 +000046 * CO-PILOT : Barrie Kletscher
subrata_modak4bb656a2009-02-26 12:02:09 +000047 *
plars865695b2001-08-27 22:15:12 +000048 * DATE STARTED : 10/17/85
subrata_modak4bb656a2009-02-26 12:02:09 +000049 *
plars865695b2001-08-27 22:15:12 +000050 * TEST ITEMS
subrata_modak4bb656a2009-02-26 12:02:09 +000051 *
plars865695b2001-08-27 22:15:12 +000052 * 1. SIGKILL can not be set to be caught, errno:EINVAL (POSIX).
53 * 2. SIGKILL can not be caught.
54 * 3. SIGKILL can not be set to be ignored, errno:EINVAL (POSIX).
55 * 4. SIGKILL can not be ignored.
56 * 5. SIGKILL can not be reset to default, errno:EINVAL (POSIX.
subrata_modak4bb656a2009-02-26 12:02:09 +000057 *
plars865695b2001-08-27 22:15:12 +000058 * ENVIRONMENTAL NEEDS
subrata_modak4bb656a2009-02-26 12:02:09 +000059 *
plars865695b2001-08-27 22:15:12 +000060 * NONE
subrata_modak4bb656a2009-02-26 12:02:09 +000061 *
plars865695b2001-08-27 22:15:12 +000062 * SPECIAL PROCEDURAL REQUIREMENTS
subrata_modak4bb656a2009-02-26 12:02:09 +000063 *
plars865695b2001-08-27 22:15:12 +000064 * None
subrata_modak4bb656a2009-02-26 12:02:09 +000065 *
plars865695b2001-08-27 22:15:12 +000066 * INTERCASE DEPENDENCIES
subrata_modak4bb656a2009-02-26 12:02:09 +000067 *
plars865695b2001-08-27 22:15:12 +000068 * 2 depends on 1 and 4 on 3.
subrata_modak4bb656a2009-02-26 12:02:09 +000069 *
plars865695b2001-08-27 22:15:12 +000070 * DETAILED DESCRIPTION
subrata_modak4bb656a2009-02-26 12:02:09 +000071 *
plars865695b2001-08-27 22:15:12 +000072 * main()
73 * Call catch_test to test setup and catching of SIGKILL.
subrata_modak4bb656a2009-02-26 12:02:09 +000074 *
75 *
plars865695b2001-08-27 22:15:12 +000076 * Call ignore_test to test setup and ignoring of SIGKILL.
subrata_modak4bb656a2009-02-26 12:02:09 +000077 *
78 *
plars865695b2001-08-27 22:15:12 +000079 * Call sigdfl_test to test setting SIGKILL to default.
subrata_modak4bb656a2009-02-26 12:02:09 +000080 *
plars865695b2001-08-27 22:15:12 +000081 * * END OF MAIN *
subrata_modak4bb656a2009-02-26 12:02:09 +000082 *
83 *
plars865695b2001-08-27 22:15:12 +000084 * catch_test()
subrata_modak4bb656a2009-02-26 12:02:09 +000085 *
plars865695b2001-08-27 22:15:12 +000086 * fork a child
87 * if this is the parent
88 * sleep to let child start.
89 * send sig to child.
90 * wait for the child to terminate.
subrata_modak4bb656a2009-02-26 12:02:09 +000091 *
plars865695b2001-08-27 22:15:12 +000092 * if the termination status of the child equals the signal sent to it
93 * Test item 1 PASSED the child was killed.
94 * else if status equals the exit value of SIG_CAUGHT
95 * Test item 2 FAILED sig was caught.
subrata_modak4bb656a2009-02-26 12:02:09 +000096 * else
plars865695b2001-08-27 22:15:12 +000097 * Test item 2 FAILED because the child was not killed
98 * but sig was not caught either.
subrata_modak4bb656a2009-02-26 12:02:09 +000099 *
plars865695b2001-08-27 22:15:12 +0000100 * else this the child
101 * set exit_val to SIG_NOT_CAUGHT.
subrata_modak4bb656a2009-02-26 12:02:09 +0000102 * set to catch sig, where the interrupt routine just sets
plars865695b2001-08-27 22:15:12 +0000103 * exit_val to SIG_CAUGHT.
subrata_modak4bb656a2009-02-26 12:02:09 +0000104 *
plars865695b2001-08-27 22:15:12 +0000105 * If the return value and errno, after trying to set to catch sig,
106 * do not indicate that an error has occurred.
107 * Test item 1 FAILED bad return, return value:X, errno:X.
108 * else
109 * Test item 1 PASSED sig was not set to be caught.
subrata_modak4bb656a2009-02-26 12:02:09 +0000110 *
plars865695b2001-08-27 22:15:12 +0000111 * pause until the parent sends a signal.
112 * The child should be killed by the signal but if not exit
113 * with exit_val.
subrata_modak4bb656a2009-02-26 12:02:09 +0000114 *
plars865695b2001-08-27 22:15:12 +0000115 * * End of catch_test. *
subrata_modak4bb656a2009-02-26 12:02:09 +0000116 *
117 *
plars865695b2001-08-27 22:15:12 +0000118 * ignore_test()
subrata_modak4bb656a2009-02-26 12:02:09 +0000119 *
plars865695b2001-08-27 22:15:12 +0000120 * fork a child
121 * if this is the parent
122 * sleep to let child start.
123 * send SIGKILL to child.
124 * wait for the child to terminate.
subrata_modak4bb656a2009-02-26 12:02:09 +0000125 *
plars865695b2001-08-27 22:15:12 +0000126 * if the termination status of the child equals SIGKILL
127 * Test item 4 PASSED the child was killed.
128 * else if the status equals the exit value of SIG_IGNORED
129 * Test item 4 FAILED SIGKILL was ignored.
subrata_modak4bb656a2009-02-26 12:02:09 +0000130 *
plars865695b2001-08-27 22:15:12 +0000131 * else this the child
subrata_modak4bb656a2009-02-26 12:02:09 +0000132 *
plars865695b2001-08-27 22:15:12 +0000133 * If the return value and errno, after trying to set to ignore SIGKILL,
134 * do not indicate that an error has occurred.
135 * Test item 3 FAILED bad return, return value:X, errno:X.
136 * else
137 * Test item 3 PASSED SIGKILL was not set to be ignored.
subrata_modak4bb656a2009-02-26 12:02:09 +0000138 *
plars865695b2001-08-27 22:15:12 +0000139 * pause until the parent sends SIGKILL.
140 * The child should be killed by the signal but if not exit
141 * with SIG_IGNORED.
subrata_modak4bb656a2009-02-26 12:02:09 +0000142 *
plars865695b2001-08-27 22:15:12 +0000143 * * End of ignore_test. *
subrata_modak4bb656a2009-02-26 12:02:09 +0000144 *
145 *
plars865695b2001-08-27 22:15:12 +0000146 * sigdfl_test()
subrata_modak4bb656a2009-02-26 12:02:09 +0000147 *
plars865695b2001-08-27 22:15:12 +0000148 * If the return value and errno, after trying to set to SIGKILL to default,
149 * do not indicate that an error has occurred.
150 * Test item 5 FAILED bad return, return value:X, errno:X.
151 * else
152 * Test item 5 PASSED SIGKILL was not set to default.
subrata_modak4bb656a2009-02-26 12:02:09 +0000153 *
plars865695b2001-08-27 22:15:12 +0000154 * * End of sigdfl_test. *
subrata_modak4bb656a2009-02-26 12:02:09 +0000155 *
plars865695b2001-08-27 22:15:12 +0000156 * BUGS/NOTES
157 * Since the system call under test is executed in the child, no
158 * timings on this system call will be reported.
subrata_modak4bb656a2009-02-26 12:02:09 +0000159 *
plars865695b2001-08-27 22:15:12 +0000160***********************************************************************************/
plars865695b2001-08-27 22:15:12 +0000161#include <signal.h>
subrata_modak56207ce2009-03-23 13:35:39 +0000162#include <errno.h>
plars865695b2001-08-27 22:15:12 +0000163#include <unistd.h>
164#include <fcntl.h>
165#include <string.h>
166#include <stdlib.h>
167#include <sys/wait.h>
168
169#include "test.h"
plars865695b2001-08-27 22:15:12 +0000170
Mike Frysingerc57fba52014-04-09 18:56:30 -0400171void setup(void);
172void cleanup(void);
173void do_test(int test_case, int tst_cnt);
174void do_child(int test_case);
175void sigdfl_test(void);
176struct tblock;
177void update_timings(struct tblock atblock);
Mike Frysingere61ddba2014-04-09 23:24:32 -0400178void p_timeout_handler(int sig);
179void c_timeout_handler(int sig);
180void catchsig(int sig);
plars865695b2001-08-27 22:15:12 +0000181
subrata_modak4bb656a2009-02-26 12:02:09 +0000182#if defined(linux)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800183#define SIG_PF sig_t /* This might need to be sighandler_t on some systems */
plars865695b2001-08-27 22:15:12 +0000184#endif
185
186#define SIG_CAUGHT 1
187#define SIG_NOT_CAUGHT 0
188#define SIG_IGNORED 5
189#define TIMED_OUT 99
190
191#define TIMEOUT 20
192
193#define GO_FLAG 1
194#define ERROR_FLAG 2
195#define PASS_FLAG 3
196#define FAIL_FLAG 4
197
198#define IGNORE_TEST 1
199#define CATCH_TEST 2
200
subrata_modak56207ce2009-03-23 13:35:39 +0000201#define MAXMESG 150 /* The Maximum message that can be created. */
plars865695b2001-08-27 22:15:12 +0000202
subrata_modak56207ce2009-03-23 13:35:39 +0000203int exit_val; /* Global variable, used to tell whether the */
204 /* child exited instead of being killed. */
plars865695b2001-08-27 22:15:12 +0000205
206struct ipc_t {
subrata_modak56207ce2009-03-23 13:35:39 +0000207 int status;
208 char mesg[MAXMESG];
209 struct tblock timings;
plars865695b2001-08-27 22:15:12 +0000210} Ipc_info;
211
nstrazfa31d552002-05-14 16:50:06 +0000212char *TCID = "signal01";
plars865695b2001-08-27 22:15:12 +0000213int TST_TOTAL = 5;
plars865695b2001-08-27 22:15:12 +0000214
subrata_modak56207ce2009-03-23 13:35:39 +0000215int Pid; /* Return value from fork. */
216static int fd1[2]; /* ipc fd, shared between do_test and do_child */
plars865695b2001-08-27 22:15:12 +0000217
subrata_modak56207ce2009-03-23 13:35:39 +0000218typedef void (*sighandler_t) (int);
plars74948ad2002-11-14 16:16:14 +0000219
subrata_modak56207ce2009-03-23 13:35:39 +0000220sighandler_t Tret;
plars865695b2001-08-27 22:15:12 +0000221
robbiewd34d5812005-07-11 22:28:09 +0000222#ifdef UCLINUX
subrata_modak56207ce2009-03-23 13:35:39 +0000223static char *argv0;
robbiewd34d5812005-07-11 22:28:09 +0000224
Mike Frysingerc57fba52014-04-09 18:56:30 -0400225void do_child_uclinux(void);
robbiewd34d5812005-07-11 22:28:09 +0000226static int test_case_uclinux;
227#endif
228
plars865695b2001-08-27 22:15:12 +0000229/***********************************************************************
230 * M A I N
231 ***********************************************************************/
Mike Frysingerc57fba52014-04-09 18:56:30 -0400232int main(int argc, char *argv[])
plars865695b2001-08-27 22:15:12 +0000233{
subrata_modak56207ce2009-03-23 13:35:39 +0000234 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +0200235 const char *msg;
plars865695b2001-08-27 22:15:12 +0000236
237 /***************************************************************
238 * parse standard options
239 ***************************************************************/
Wanlong Gao354ebb42012-12-07 10:10:04 +0800240 if ((msg = parse_opts(argc, argv, NULL, NULL)) != NULL) {
subrata_modak56207ce2009-03-23 13:35:39 +0000241 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
subrata_modak56207ce2009-03-23 13:35:39 +0000242 }
robbiewd34d5812005-07-11 22:28:09 +0000243#ifdef UCLINUX
subrata_modak56207ce2009-03-23 13:35:39 +0000244 argv0 = argv[0];
245 maybe_run_child(&do_child_uclinux, "dd", &test_case_uclinux, &fd1[1]);
robbiewd34d5812005-07-11 22:28:09 +0000246#endif
247
plars865695b2001-08-27 22:15:12 +0000248 /***************************************************************
249 * perform global setup for test
250 ***************************************************************/
subrata_modak56207ce2009-03-23 13:35:39 +0000251 setup();
plars865695b2001-08-27 22:15:12 +0000252
253 /***************************************************************
254 * check looping state if -c option given
255 ***************************************************************/
subrata_modak56207ce2009-03-23 13:35:39 +0000256 for (lc = 0; TEST_LOOPING(lc); lc++) {
plars865695b2001-08-27 22:15:12 +0000257
Caspar Zhangd59a6592013-03-07 14:59:12 +0800258 tst_count = 0;
plars865695b2001-08-27 22:15:12 +0000259
subrata_modak56207ce2009-03-23 13:35:39 +0000260 errno = -4;
plars865695b2001-08-27 22:15:12 +0000261
subrata_modak56207ce2009-03-23 13:35:39 +0000262 /*
263 * Call catch_test to test setup and catching of SIGKILL.
264 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400265 do_test(CATCH_TEST, tst_count);
plars865695b2001-08-27 22:15:12 +0000266
subrata_modak56207ce2009-03-23 13:35:39 +0000267 /*
268 * Call ignore_test to test setup and ignoring of SIGKILL.
269 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400270 do_test(IGNORE_TEST, tst_count);
plars865695b2001-08-27 22:15:12 +0000271
subrata_modak56207ce2009-03-23 13:35:39 +0000272 /*
273 * Call sigdfl_test to test setting SIGKILL to default.
274 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400275 sigdfl_test();
plars865695b2001-08-27 22:15:12 +0000276
subrata_modak56207ce2009-03-23 13:35:39 +0000277 }
plars865695b2001-08-27 22:15:12 +0000278
subrata_modak56207ce2009-03-23 13:35:39 +0000279 cleanup();
plars865695b2001-08-27 22:15:12 +0000280
Garrett Cooper2c282152010-12-16 00:55:50 -0800281 tst_exit();
subrata_modak56207ce2009-03-23 13:35:39 +0000282} /*End of main */
plars865695b2001-08-27 22:15:12 +0000283
284/***********************************************************************
285 *
286 ***********************************************************************/
Mike Frysingerc57fba52014-04-09 18:56:30 -0400287void do_test(int test_case, int tst_cnt)
plars865695b2001-08-27 22:15:12 +0000288{
subrata_modak56207ce2009-03-23 13:35:39 +0000289 int term_stat; /* Termination status of the child returned to */
290 /* the parent. */
291 int rd_sz; /* size of read */
plars865695b2001-08-27 22:15:12 +0000292
Caspar Zhangd59a6592013-03-07 14:59:12 +0800293 tst_count = tst_cnt;
plars865695b2001-08-27 22:15:12 +0000294
295 /*
subrata_modak56207ce2009-03-23 13:35:39 +0000296 * Create a pipe of ipc
plars865695b2001-08-27 22:15:12 +0000297 */
subrata_modak56207ce2009-03-23 13:35:39 +0000298 if (pipe(fd1) == -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800299 tst_resm(TBROK | TERRNO, "pipe() failed");
subrata_modak56207ce2009-03-23 13:35:39 +0000300 return;
301 }
plars865695b2001-08-27 22:15:12 +0000302
subrata_modak56207ce2009-03-23 13:35:39 +0000303 /*
304 * Cause the read to return 0 once EOF is encountered and the
305 * read to return -1 if pipe is empty.
306 */
plars865695b2001-08-27 22:15:12 +0000307
subrata_modak56207ce2009-03-23 13:35:39 +0000308 if (fcntl(fd1[0], F_SETFL, O_NONBLOCK) == -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800309 tst_resm(TBROK | TERRNO,
310 "fcntl(fd1[0], F_SETFL, O_NONBLOCK) failed");
plars865695b2001-08-27 22:15:12 +0000311 close(fd1[0]);
subrata_modak56207ce2009-03-23 13:35:39 +0000312 close(fd1[1]);
313 return;
314 }
plars865695b2001-08-27 22:15:12 +0000315
subrata_modak56207ce2009-03-23 13:35:39 +0000316 if ((Pid = FORK_OR_VFORK()) > 0) { /* parent */
317
318 signal(SIGALRM, p_timeout_handler);
319
320 alarm(TIMEOUT);
321
322 close(fd1[1]); /* close write side */
323
324 /*
325 * Deal with child's messages.
326 * Only the GO_FLAG status will allow parent to
327 * go on. All pipe io will be in the ipc_t structure sizes
328 * to avoid reading part of next message.
329 */
330 while (1) {
331
332 while ((rd_sz =
333 read(fd1[0], (char *)&Ipc_info,
334 sizeof(Ipc_info))) != 0) {
335 if (rd_sz > 0)
336 break; /* read something */
337 }
338
339 if (rd_sz == 0) { /* if EOF encountered */
Wanlong Gao354ebb42012-12-07 10:10:04 +0800340 tst_resm(TBROK,
341 "child's pipe is closed before 'go' message received");
subrata_modak56207ce2009-03-23 13:35:39 +0000342 close(fd1[0]);
343 return;
344 }
345
346 else if (Ipc_info.status == GO_FLAG) {
347 break; /* go on */
348 } else if (Ipc_info.status == ERROR_FLAG) {
349 tst_resm(TBROK, "From child: %s",
350 Ipc_info.mesg);
351 tst_resm(TBROK, "From child: %s",
352 Ipc_info.mesg);
353 close(fd1[0]);
354 return;
355 } else if (Ipc_info.status == PASS_FLAG) {
356
Cyril Hrubise38b9612014-06-02 17:20:57 +0200357 tst_resm(TPASS, "From child: %s",
358 Ipc_info.mesg);
subrata_modak56207ce2009-03-23 13:35:39 +0000359 update_timings(Ipc_info.timings);
360 } else if (Ipc_info.status == FAIL_FLAG) {
361 tst_resm(TFAIL, "From child: %s",
362 Ipc_info.mesg);
363 update_timings(Ipc_info.timings);
364 } else {
365 tst_resm(TINFO,
366 "Unknown message from child: %s",
vapier74813062009-08-28 13:58:53 +0000367 Ipc_info.mesg);
subrata_modak56207ce2009-03-23 13:35:39 +0000368 }
369 }
370
371 /*
372 * Send the signal SIGKILL to the child.
373 */
374 if (kill(Pid, SIGKILL) == -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800375 tst_resm(TBROK | TERRNO, "kill(%d) failed", Pid);
subrata_modak56207ce2009-03-23 13:35:39 +0000376 close(fd1[0]);
377 return;
378 }
379
380 /*
381 * Wait for the child to terminate and check the termination status.
382 */
383 if (wait(&term_stat) == -1) {
384 /*
385 * The wait system call failed.
386 */
Wanlong Gao354ebb42012-12-07 10:10:04 +0800387 tst_resm(TBROK | TERRNO, "wait() failed");
subrata_modak56207ce2009-03-23 13:35:39 +0000388 close(fd1[0]);
389 return;
Cyril Hrubise38b9612014-06-02 17:20:57 +0200390 } else {
subrata_modak56207ce2009-03-23 13:35:39 +0000391 if ((term_stat & 0377) == SIGKILL) {
392 /*
393 * The child was killed by the signal sent,
394 * which is correct.
395 */
396 tst_resm(TPASS,
397 "The child was killed by SIGKILL.");
398 } else if ((term_stat >> 8) == TIMED_OUT) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800399 tst_resm(TBROK,
400 "child exited with a timed out exit status");
subrata_modak56207ce2009-03-23 13:35:39 +0000401 } else {
402 if ((term_stat >> 8) == SIG_IGNORED
403 && test_case == IGNORE_TEST) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800404 tst_resm(TFAIL,
405 "SIGKILL was ignored by child after sent by parent.");
subrata_modak56207ce2009-03-23 13:35:39 +0000406 } else if ((term_stat >> 8) == SIG_CAUGHT
407 && test_case == CATCH_TEST) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800408 tst_resm(TFAIL,
409 "SIGKILL was caught by child after sent by parent.");
subrata_modak56207ce2009-03-23 13:35:39 +0000410 } else {
vapier74813062009-08-28 13:58:53 +0000411 tst_resm(TFAIL,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800412 "Child's termination status is unexpected. Status: %d (%#o).",
413 term_stat, term_stat);
subrata_modak56207ce2009-03-23 13:35:39 +0000414 }
subrata_modak56207ce2009-03-23 13:35:39 +0000415 }
subrata_modak56207ce2009-03-23 13:35:39 +0000416 }
plars865695b2001-08-27 22:15:12 +0000417 close(fd1[0]);
plars865695b2001-08-27 22:15:12 +0000418
Wanlong Gao354ebb42012-12-07 10:10:04 +0800419 } else if (Pid == 0) {
plars865695b2001-08-27 22:15:12 +0000420 /*
subrata_modak56207ce2009-03-23 13:35:39 +0000421 * This is the child.
422 * Set up to ignore/catch SIGKILL and check the return values.
plars865695b2001-08-27 22:15:12 +0000423 */
robbiewd34d5812005-07-11 22:28:09 +0000424#ifdef UCLINUX
subrata_modak56207ce2009-03-23 13:35:39 +0000425 if (self_exec(argv0, "dd", test_case, fd1[1]) < 0) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800426 tst_resm(TBROK | TERRNO, "self_exec() failed");
subrata_modak56207ce2009-03-23 13:35:39 +0000427 close(fd1[0]);
428 close(fd1[1]);
429 return;
430 }
robbiewd34d5812005-07-11 22:28:09 +0000431#else
subrata_modak56207ce2009-03-23 13:35:39 +0000432 do_child(test_case);
robbiewd34d5812005-07-11 22:28:09 +0000433#endif
subrata_modak56207ce2009-03-23 13:35:39 +0000434
Wanlong Gao354ebb42012-12-07 10:10:04 +0800435 } else {
436 tst_resm(TBROK | TERRNO, "fork() failed");
subrata_modak56207ce2009-03-23 13:35:39 +0000437 close(fd1[0]);
438 close(fd1[1]);
439 return;
440 }
Garrett Cooper2c282152010-12-16 00:55:50 -0800441}
plars865695b2001-08-27 22:15:12 +0000442
443/***********************************************************************
robbiewd34d5812005-07-11 22:28:09 +0000444 * do_child()
445 ***********************************************************************/
Mike Frysingerc57fba52014-04-09 18:56:30 -0400446void do_child(int test_case)
robbiewd34d5812005-07-11 22:28:09 +0000447{
subrata_modak56207ce2009-03-23 13:35:39 +0000448 char string[30];
robbiewd34d5812005-07-11 22:28:09 +0000449
subrata_modak56207ce2009-03-23 13:35:39 +0000450 errno = 0;
451 if (test_case == IGNORE_TEST) {
452 exit_val = SIG_IGNORED;
453 strcpy(string, "signal(SIGKILL, SIG_IGN)");
subrata_modakbdbaec52009-02-26 12:14:51 +0000454
subrata_modak56207ce2009-03-23 13:35:39 +0000455 Tret = signal(SIGKILL, SIG_IGN);
456 TEST_ERRNO = errno;
457 } else {
458 exit_val = SIG_NOT_CAUGHT;
459 strcpy(string, "signal(SIGKILL, catchsig)");
460 Tret = signal(SIGKILL, catchsig);
461 TEST_ERRNO = errno;
robbiewd34d5812005-07-11 22:28:09 +0000462 }
subrata_modak56207ce2009-03-23 13:35:39 +0000463 Ipc_info.timings = tblock;
464
465 if (Tret == SIG_ERR) {
466 if (TEST_ERRNO == EINVAL) {
467 sprintf(Ipc_info.mesg,
468 "%s ret:%p SIG_ERR (%ld) as expected", string,
469 Tret, (long)SIG_ERR);
470 Ipc_info.status = PASS_FLAG;
471 } else {
472 sprintf(Ipc_info.mesg,
vapier74813062009-08-28 13:58:53 +0000473 "%s ret:%p, errno:%d expected ret:%ld, errno:%d",
subrata_modak56207ce2009-03-23 13:35:39 +0000474 string, Tret, TEST_ERRNO, (long)SIG_ERR,
475 EINVAL);
476 Ipc_info.status = FAIL_FLAG;
477 }
478
479 write(fd1[1], (char *)&Ipc_info, sizeof(Ipc_info));
480 } else {
481 /*
482 * The child was not allowed to set the signal to
483 * be ignored and errno was correct.
484 */
485 sprintf(Ipc_info.mesg,
vapier74813062009-08-28 13:58:53 +0000486 "%s ret:%p, errno:%d expected ret:%ld, errno:%d",
subrata_modak56207ce2009-03-23 13:35:39 +0000487 string, Tret, TEST_ERRNO, (long)SIG_ERR, EINVAL);
488 Ipc_info.status = FAIL_FLAG;
489 write(fd1[1], (char *)&Ipc_info, sizeof(Ipc_info));
robbiewd34d5812005-07-11 22:28:09 +0000490 }
subrata_modak56207ce2009-03-23 13:35:39 +0000491
robbiewd34d5812005-07-11 22:28:09 +0000492 /*
subrata_modak56207ce2009-03-23 13:35:39 +0000493 * tell parent we are ready - setup by child is done
robbiewd34d5812005-07-11 22:28:09 +0000494 */
subrata_modak56207ce2009-03-23 13:35:39 +0000495 Ipc_info.status = GO_FLAG;
robbiewd34d5812005-07-11 22:28:09 +0000496 write(fd1[1], (char *)&Ipc_info, sizeof(Ipc_info));
robbiewd34d5812005-07-11 22:28:09 +0000497
subrata_modak56207ce2009-03-23 13:35:39 +0000498 /*
499 * Set the alarm to wake up from the pause below if
500 * the parents signal is ignored.
501 */
502 signal(SIGALRM, p_timeout_handler);
503 alarm(TIMEOUT);
robbiewd34d5812005-07-11 22:28:09 +0000504
subrata_modak56207ce2009-03-23 13:35:39 +0000505 /*
506 * Pause until the parent sends a signal or until alarm is received.
507 */
508 pause();
robbiewd34d5812005-07-11 22:28:09 +0000509
subrata_modak56207ce2009-03-23 13:35:39 +0000510 exit(exit_val);
Garrett Cooper2c282152010-12-16 00:55:50 -0800511}
robbiewd34d5812005-07-11 22:28:09 +0000512
513#ifdef UCLINUX
514/***********************************************************************
515 * do_child_uclinux(): call do_child with the global used to store test_case
516 ***********************************************************************/
Mike Frysingerc57fba52014-04-09 18:56:30 -0400517void do_child_uclinux(void)
robbiewd34d5812005-07-11 22:28:09 +0000518{
subrata_modak56207ce2009-03-23 13:35:39 +0000519 do_child(test_case_uclinux);
Garrett Cooper2c282152010-12-16 00:55:50 -0800520}
robbiewd34d5812005-07-11 22:28:09 +0000521#endif
522
523/***********************************************************************
subrata_modak4bb656a2009-02-26 12:02:09 +0000524 * sigdfl_test - test for attempt to set SIGKILL to default
plars865695b2001-08-27 22:15:12 +0000525 ***********************************************************************/
Mike Frysingerc57fba52014-04-09 18:56:30 -0400526void sigdfl_test(void)
plars865695b2001-08-27 22:15:12 +0000527{
subrata_modak56207ce2009-03-23 13:35:39 +0000528 /*
529 * Try to set SIGKILL to default and check the return values.
530 */
531 errno = -4;
plars865695b2001-08-27 22:15:12 +0000532
subrata_modak56207ce2009-03-23 13:35:39 +0000533 Tret = signal(SIGKILL, SIG_DFL);
534 TEST_ERRNO = errno;
plars865695b2001-08-27 22:15:12 +0000535
subrata_modak56207ce2009-03-23 13:35:39 +0000536 if (Tret == SIG_ERR) {
Cyril Hrubise38b9612014-06-02 17:20:57 +0200537 if (TEST_ERRNO != EINVAL) {
538 tst_resm(TFAIL | TTERRNO,
539 "signal(SIGKILL,SIG_DFL) expected ret:-1, errno:EINVAL, got ret:%p",
540 Tret);
541 } else {
542 tst_resm(TPASS,
543 "signal(SIGKILL,SIG_DFL) ret:%p, errno EINVAL as expected",
544 Tret);
545 }
subrata_modak56207ce2009-03-23 13:35:39 +0000546 } else {
vapier74813062009-08-28 13:58:53 +0000547 tst_resm(TFAIL,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800548 "signal(SIGKILL,SIG_DFL) ret:%p, errno:%d expected ret:-1, errno:%d",
549 Tret, TEST_ERRNO, EINVAL);
plars865695b2001-08-27 22:15:12 +0000550 }
plars865695b2001-08-27 22:15:12 +0000551
Garrett Cooper2c282152010-12-16 00:55:50 -0800552}
plars865695b2001-08-27 22:15:12 +0000553
554/***************************************************************
555 * setup() - performs all ONE TIME setup for this test.
556 ***************************************************************/
Mike Frysingerc57fba52014-04-09 18:56:30 -0400557void setup(void)
plars865695b2001-08-27 22:15:12 +0000558{
Garrett Cooper2c282152010-12-16 00:55:50 -0800559
subrata_modak56207ce2009-03-23 13:35:39 +0000560 tst_sig(FORK, DEF_HANDLER, cleanup);
plars865695b2001-08-27 22:15:12 +0000561
subrata_modak56207ce2009-03-23 13:35:39 +0000562 TEST_PAUSE;
plars865695b2001-08-27 22:15:12 +0000563
subrata_modak56207ce2009-03-23 13:35:39 +0000564 /* make and change to a temporary directory */
565 tst_tmpdir();
plars865695b2001-08-27 22:15:12 +0000566
Garrett Cooper2c282152010-12-16 00:55:50 -0800567}
plars865695b2001-08-27 22:15:12 +0000568
569/***************************************************************
570 * cleanup() - performs all ONE TIME cleanup for this test at
571 * completion or premature exit.
572 ***************************************************************/
Mike Frysingerc57fba52014-04-09 18:56:30 -0400573void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000574{
575
subrata_modak56207ce2009-03-23 13:35:39 +0000576 /*
subrata_modak56207ce2009-03-23 13:35:39 +0000577 * remove the temporary directory and exit with
578 * return code appropriate for results
579 */
plars865695b2001-08-27 22:15:12 +0000580
subrata_modak56207ce2009-03-23 13:35:39 +0000581 tst_rmdir();
plars865695b2001-08-27 22:15:12 +0000582
Garrett Cooper2c282152010-12-16 00:55:50 -0800583}
plars865695b2001-08-27 22:15:12 +0000584
585/***********************************************************************
586 * Signal handler routine that used by the parent to handler
587 * a time out situation. It will attempt to kill the child and
588 * call cleanup.
589 ***********************************************************************/
Mike Frysingere61ddba2014-04-09 23:24:32 -0400590void p_timeout_handler(int sig)
plars865695b2001-08-27 22:15:12 +0000591{
subrata_modak56207ce2009-03-23 13:35:39 +0000592 kill(Pid, SIGKILL);
593 cleanup();
plars865695b2001-08-27 22:15:12 +0000594}
595
596/***********************************************************************
597 * Signal handler routine that used by the child to handle
598 * a time out situation. It will set a global varaible and return
599 * if called.
600 ***********************************************************************/
Mike Frysingere61ddba2014-04-09 23:24:32 -0400601void c_timeout_handler(int sig)
plars865695b2001-08-27 22:15:12 +0000602{
subrata_modak56207ce2009-03-23 13:35:39 +0000603 exit_val = TIMED_OUT;
604 return;
plars865695b2001-08-27 22:15:12 +0000605}
606
607/***********************************************************************
608 * This signal handling routine will set a global variable and return
609 * if called.
610 ***********************************************************************/
Mike Frysingere61ddba2014-04-09 23:24:32 -0400611void catchsig(int sig)
plars865695b2001-08-27 22:15:12 +0000612{
subrata_modak56207ce2009-03-23 13:35:39 +0000613 exit_val = SIG_CAUGHT;
614 return;
plars865695b2001-08-27 22:15:12 +0000615}
616
617/***********************************************************************
618 * Update timing information
619 ***********************************************************************/
Mike Frysingerc57fba52014-04-09 18:56:30 -0400620void update_timings(struct tblock atblock)
plars865695b2001-08-27 22:15:12 +0000621{
subrata_modak56207ce2009-03-23 13:35:39 +0000622 tblock.tb_max += atblock.tb_max;
623 tblock.tb_min += atblock.tb_min;
624 tblock.tb_total += atblock.tb_total;
625 tblock.tb_count += atblock.tb_count;
Chris Dearmanec6edca2012-10-17 19:54:01 -0700626}