xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/kill/kill11.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later
2*49cdfc7eSAndroid Build Coastguard Worker /* IBM Corporation
3*49cdfc7eSAndroid Build Coastguard Worker  * 01/02/2003	Port to LTP	[email protected]
4*49cdfc7eSAndroid Build Coastguard Worker  * 06/30/2001	Port to Linux	[email protected]
5*49cdfc7eSAndroid Build Coastguard Worker  *
6*49cdfc7eSAndroid Build Coastguard Worker  *   Copyright (c) International Business Machines  Corp., 2002
7*49cdfc7eSAndroid Build Coastguard Worker  *   Copyright (c) Cyril Hrubis <[email protected]> 2014
8*49cdfc7eSAndroid Build Coastguard Worker  *
9*49cdfc7eSAndroid Build Coastguard Worker  * Test checks that when a child is killed by its parent with sig, it
10*49cdfc7eSAndroid Build Coastguard Worker  * returns the correct values(sig and core dump bit) to the waiting parent.
11*49cdfc7eSAndroid Build Coastguard Worker  *
12*49cdfc7eSAndroid Build Coastguard Worker  * RESTRICTIONS
13*49cdfc7eSAndroid Build Coastguard Worker  * The ulimit for core file size must be greater than 0.
14*49cdfc7eSAndroid Build Coastguard Worker  */
15*49cdfc7eSAndroid Build Coastguard Worker 
16*49cdfc7eSAndroid Build Coastguard Worker #define _GNU_SOURCE
17*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
18*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
19*49cdfc7eSAndroid Build Coastguard Worker #include <signal.h>
20*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
21*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
22*49cdfc7eSAndroid Build Coastguard Worker #include <sys/wait.h>
23*49cdfc7eSAndroid Build Coastguard Worker #include <sys/resource.h>
24*49cdfc7eSAndroid Build Coastguard Worker 
25*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
26*49cdfc7eSAndroid Build Coastguard Worker 
27*49cdfc7eSAndroid Build Coastguard Worker static struct tcase {
28*49cdfc7eSAndroid Build Coastguard Worker 	int sig;
29*49cdfc7eSAndroid Build Coastguard Worker 	int dumps_core;
30*49cdfc7eSAndroid Build Coastguard Worker } tcases[] = {
31*49cdfc7eSAndroid Build Coastguard Worker 	{SIGHUP, 0},
32*49cdfc7eSAndroid Build Coastguard Worker 	{SIGINT, 0},
33*49cdfc7eSAndroid Build Coastguard Worker 	{SIGQUIT, 1},
34*49cdfc7eSAndroid Build Coastguard Worker 	{SIGILL, 1},
35*49cdfc7eSAndroid Build Coastguard Worker 	{SIGTRAP, 1},
36*49cdfc7eSAndroid Build Coastguard Worker 	{SIGABRT, 1},
37*49cdfc7eSAndroid Build Coastguard Worker 	{SIGIOT, 1},
38*49cdfc7eSAndroid Build Coastguard Worker 	{SIGBUS, 1},
39*49cdfc7eSAndroid Build Coastguard Worker 	{SIGFPE, 1},
40*49cdfc7eSAndroid Build Coastguard Worker 	{SIGKILL, 0},
41*49cdfc7eSAndroid Build Coastguard Worker 	{SIGUSR1, 0},
42*49cdfc7eSAndroid Build Coastguard Worker 	{SIGSEGV, 1},
43*49cdfc7eSAndroid Build Coastguard Worker 	{SIGUSR2, 0},
44*49cdfc7eSAndroid Build Coastguard Worker 	{SIGPIPE, 0},
45*49cdfc7eSAndroid Build Coastguard Worker 	{SIGALRM, 0},
46*49cdfc7eSAndroid Build Coastguard Worker 	{SIGTERM, 0},
47*49cdfc7eSAndroid Build Coastguard Worker #ifdef SIGSTKFLT
48*49cdfc7eSAndroid Build Coastguard Worker 	{SIGSTKFLT, 0},
49*49cdfc7eSAndroid Build Coastguard Worker #endif
50*49cdfc7eSAndroid Build Coastguard Worker 	{SIGXCPU, 1},
51*49cdfc7eSAndroid Build Coastguard Worker 	{SIGXFSZ, 1},
52*49cdfc7eSAndroid Build Coastguard Worker 	{SIGVTALRM, 0},
53*49cdfc7eSAndroid Build Coastguard Worker 	{SIGPROF, 0},
54*49cdfc7eSAndroid Build Coastguard Worker 	{SIGIO, 0},
55*49cdfc7eSAndroid Build Coastguard Worker 	{SIGPWR, 0},
56*49cdfc7eSAndroid Build Coastguard Worker 	{SIGSYS, 1},
57*49cdfc7eSAndroid Build Coastguard Worker };
58*49cdfc7eSAndroid Build Coastguard Worker 
verify_kill(unsigned int n)59*49cdfc7eSAndroid Build Coastguard Worker static void verify_kill(unsigned int n)
60*49cdfc7eSAndroid Build Coastguard Worker {
61*49cdfc7eSAndroid Build Coastguard Worker 	int core;
62*49cdfc7eSAndroid Build Coastguard Worker 	pid_t pid, npid;
63*49cdfc7eSAndroid Build Coastguard Worker 	int nsig, status;
64*49cdfc7eSAndroid Build Coastguard Worker 	struct tcase *tc = &tcases[n];
65*49cdfc7eSAndroid Build Coastguard Worker 
66*49cdfc7eSAndroid Build Coastguard Worker 	pid = SAFE_FORK();
67*49cdfc7eSAndroid Build Coastguard Worker 	if (!pid)
68*49cdfc7eSAndroid Build Coastguard Worker 		pause();
69*49cdfc7eSAndroid Build Coastguard Worker 
70*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_KILL(pid, tc->sig);
71*49cdfc7eSAndroid Build Coastguard Worker 	npid = SAFE_WAIT(&status);
72*49cdfc7eSAndroid Build Coastguard Worker 
73*49cdfc7eSAndroid Build Coastguard Worker 	if (npid != pid) {
74*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "wait() returned %d, expected %d", npid, pid);
75*49cdfc7eSAndroid Build Coastguard Worker 		return;
76*49cdfc7eSAndroid Build Coastguard Worker 	}
77*49cdfc7eSAndroid Build Coastguard Worker 
78*49cdfc7eSAndroid Build Coastguard Worker 	nsig = WTERMSIG(status);
79*49cdfc7eSAndroid Build Coastguard Worker 	core = WCOREDUMP(status);
80*49cdfc7eSAndroid Build Coastguard Worker 
81*49cdfc7eSAndroid Build Coastguard Worker 	if (tc->dumps_core) {
82*49cdfc7eSAndroid Build Coastguard Worker 		if (!core) {
83*49cdfc7eSAndroid Build Coastguard Worker 			tst_res(TFAIL, "core dump bit not set for %s", tst_strsig(tc->sig));
84*49cdfc7eSAndroid Build Coastguard Worker 			return;
85*49cdfc7eSAndroid Build Coastguard Worker 		}
86*49cdfc7eSAndroid Build Coastguard Worker 	} else {
87*49cdfc7eSAndroid Build Coastguard Worker 		if (core) {
88*49cdfc7eSAndroid Build Coastguard Worker 			tst_res(TFAIL, "core dump bit set for %s", tst_strsig(tc->sig));
89*49cdfc7eSAndroid Build Coastguard Worker 			return;
90*49cdfc7eSAndroid Build Coastguard Worker 		}
91*49cdfc7eSAndroid Build Coastguard Worker 	}
92*49cdfc7eSAndroid Build Coastguard Worker 
93*49cdfc7eSAndroid Build Coastguard Worker 	if (nsig != tc->sig) {
94*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "wait: unexpected signal %d returned, expected %d", nsig, tc->sig);
95*49cdfc7eSAndroid Build Coastguard Worker 		return;
96*49cdfc7eSAndroid Build Coastguard Worker 	}
97*49cdfc7eSAndroid Build Coastguard Worker 
98*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TPASS, "signal %-16s%s", tst_strsig(tc->sig),
99*49cdfc7eSAndroid Build Coastguard Worker 			tc->dumps_core ? " dumped core" : "");
100*49cdfc7eSAndroid Build Coastguard Worker }
101*49cdfc7eSAndroid Build Coastguard Worker 
102*49cdfc7eSAndroid Build Coastguard Worker #define MIN_RLIMIT_CORE (512 * 1024)
103*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)104*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
105*49cdfc7eSAndroid Build Coastguard Worker {
106*49cdfc7eSAndroid Build Coastguard Worker 	struct rlimit rlim;
107*49cdfc7eSAndroid Build Coastguard Worker 
108*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_GETRLIMIT(RLIMIT_CORE, &rlim);
109*49cdfc7eSAndroid Build Coastguard Worker 
110*49cdfc7eSAndroid Build Coastguard Worker 	if (rlim.rlim_max < MIN_RLIMIT_CORE) {
111*49cdfc7eSAndroid Build Coastguard Worker 		if (geteuid() != 0) {
112*49cdfc7eSAndroid Build Coastguard Worker 			tst_brk(TCONF, "hard limit(%lu)less than MIN_RLIMT_CORE(%i)",
113*49cdfc7eSAndroid Build Coastguard Worker 				rlim.rlim_max, MIN_RLIMIT_CORE);
114*49cdfc7eSAndroid Build Coastguard Worker 		}
115*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TINFO, "Raising rlim_max to %i", MIN_RLIMIT_CORE);
116*49cdfc7eSAndroid Build Coastguard Worker 		rlim.rlim_max = MIN_RLIMIT_CORE;
117*49cdfc7eSAndroid Build Coastguard Worker 	}
118*49cdfc7eSAndroid Build Coastguard Worker 	if (rlim.rlim_cur < MIN_RLIMIT_CORE) {
119*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TINFO, "Adjusting RLIMIT_CORE to %i", MIN_RLIMIT_CORE);
120*49cdfc7eSAndroid Build Coastguard Worker 		rlim.rlim_cur = MIN_RLIMIT_CORE;
121*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_SETRLIMIT(RLIMIT_CORE, &rlim);
122*49cdfc7eSAndroid Build Coastguard Worker 	}
123*49cdfc7eSAndroid Build Coastguard Worker }
124*49cdfc7eSAndroid Build Coastguard Worker 
125*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
126*49cdfc7eSAndroid Build Coastguard Worker 	.needs_tmpdir = 1,
127*49cdfc7eSAndroid Build Coastguard Worker 	.forks_child = 1,
128*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
129*49cdfc7eSAndroid Build Coastguard Worker 	.tcnt = ARRAY_SIZE(tcases),
130*49cdfc7eSAndroid Build Coastguard Worker 	.test = verify_kill,
131*49cdfc7eSAndroid Build Coastguard Worker };
132