xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/signal/signal01.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) 2000 Silicon Graphics, Inc.  All Rights Reserved.
4*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (C) 2015 Cyril Hrubis <[email protected]>
5*49cdfc7eSAndroid Build Coastguard Worker  * AUTHOR	: Dave Baumgartner
6*49cdfc7eSAndroid Build Coastguard Worker  * CO-PILOT	: Barrie Kletscher
7*49cdfc7eSAndroid Build Coastguard Worker  */
8*49cdfc7eSAndroid Build Coastguard Worker 
9*49cdfc7eSAndroid Build Coastguard Worker /*\
10*49cdfc7eSAndroid Build Coastguard Worker  * [Description]
11*49cdfc7eSAndroid Build Coastguard Worker  *
12*49cdfc7eSAndroid Build Coastguard Worker  * Test SIGKILL for these items:
13*49cdfc7eSAndroid Build Coastguard Worker  *	1. SIGKILL can not be set to be ignored, errno:EINVAL (POSIX).
14*49cdfc7eSAndroid Build Coastguard Worker  *	2. SIGKILL can not be reset to default, errno:EINVAL (POSIX).
15*49cdfc7eSAndroid Build Coastguard Worker  *	3. SIGKILL can not be set to be caught, errno:EINVAL (POSIX).
16*49cdfc7eSAndroid Build Coastguard Worker  *	4. SIGKILL can not be ignored.
17*49cdfc7eSAndroid Build Coastguard Worker  *	5. SIGKILL is reset to default failed but processed by default.
18*49cdfc7eSAndroid Build Coastguard Worker  *	6. SIGKILL can not be caught.
19*49cdfc7eSAndroid Build Coastguard Worker  */
20*49cdfc7eSAndroid Build Coastguard Worker 
21*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
22*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
23*49cdfc7eSAndroid Build Coastguard Worker 
catchsig(int sig)24*49cdfc7eSAndroid Build Coastguard Worker static void catchsig(int sig)
25*49cdfc7eSAndroid Build Coastguard Worker {
26*49cdfc7eSAndroid Build Coastguard Worker 	(void)sig;
27*49cdfc7eSAndroid Build Coastguard Worker }
28*49cdfc7eSAndroid Build Coastguard Worker 
29*49cdfc7eSAndroid Build Coastguard Worker static struct tcase {
30*49cdfc7eSAndroid Build Coastguard Worker 	void (*sighandler)(int i);
31*49cdfc7eSAndroid Build Coastguard Worker 	int kill;
32*49cdfc7eSAndroid Build Coastguard Worker } tcases[] = {
33*49cdfc7eSAndroid Build Coastguard Worker 	{SIG_IGN, 0},
34*49cdfc7eSAndroid Build Coastguard Worker 	{SIG_DFL, 0},
35*49cdfc7eSAndroid Build Coastguard Worker 	{catchsig, 0},
36*49cdfc7eSAndroid Build Coastguard Worker 	{SIG_IGN, 1},
37*49cdfc7eSAndroid Build Coastguard Worker 	{SIG_DFL, 1},
38*49cdfc7eSAndroid Build Coastguard Worker 	{catchsig, 1},
39*49cdfc7eSAndroid Build Coastguard Worker };
40*49cdfc7eSAndroid Build Coastguard Worker 
do_test(unsigned int n)41*49cdfc7eSAndroid Build Coastguard Worker static void do_test(unsigned int n)
42*49cdfc7eSAndroid Build Coastguard Worker {
43*49cdfc7eSAndroid Build Coastguard Worker 	pid_t pid;
44*49cdfc7eSAndroid Build Coastguard Worker 	int res;
45*49cdfc7eSAndroid Build Coastguard Worker 
46*49cdfc7eSAndroid Build Coastguard Worker 	struct tcase *tc = &tcases[n];
47*49cdfc7eSAndroid Build Coastguard Worker 
48*49cdfc7eSAndroid Build Coastguard Worker 	pid = SAFE_FORK();
49*49cdfc7eSAndroid Build Coastguard Worker 
50*49cdfc7eSAndroid Build Coastguard Worker 	if (!pid) {
51*49cdfc7eSAndroid Build Coastguard Worker 		if (tc->kill) {
52*49cdfc7eSAndroid Build Coastguard Worker 			signal(SIGKILL, tc->sighandler);
53*49cdfc7eSAndroid Build Coastguard Worker 			pause();
54*49cdfc7eSAndroid Build Coastguard Worker 		}
55*49cdfc7eSAndroid Build Coastguard Worker 
56*49cdfc7eSAndroid Build Coastguard Worker 		TST_EXP_FAIL2((long)signal(SIGKILL, tc->sighandler), EINVAL);
57*49cdfc7eSAndroid Build Coastguard Worker 		return;
58*49cdfc7eSAndroid Build Coastguard Worker 	}
59*49cdfc7eSAndroid Build Coastguard Worker 
60*49cdfc7eSAndroid Build Coastguard Worker 	if (!tc->kill) {
61*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_WAITPID(pid, &res, 0);
62*49cdfc7eSAndroid Build Coastguard Worker 		return;
63*49cdfc7eSAndroid Build Coastguard Worker 	}
64*49cdfc7eSAndroid Build Coastguard Worker 
65*49cdfc7eSAndroid Build Coastguard Worker 	TST_PROCESS_STATE_WAIT(pid, 'S', 0);
66*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_KILL(pid, SIGKILL);
67*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_WAITPID(pid, &res, 0);
68*49cdfc7eSAndroid Build Coastguard Worker 
69*49cdfc7eSAndroid Build Coastguard Worker 	if (WIFSIGNALED(res))
70*49cdfc7eSAndroid Build Coastguard Worker 		TST_EXP_EQ_SSZ(WTERMSIG(res), SIGKILL);
71*49cdfc7eSAndroid Build Coastguard Worker 	else
72*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "Child not killed by signal");
73*49cdfc7eSAndroid Build Coastguard Worker }
74*49cdfc7eSAndroid Build Coastguard Worker 
75*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
76*49cdfc7eSAndroid Build Coastguard Worker 	.tcnt = ARRAY_SIZE(tcases),
77*49cdfc7eSAndroid Build Coastguard Worker 	.forks_child = 1,
78*49cdfc7eSAndroid Build Coastguard Worker 	.test = do_test,
79*49cdfc7eSAndroid Build Coastguard Worker };
80