xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/waitid/waitid10.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) Crackerjack Project., 2007
4*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (C) 2022 SUSE LLC Andrea Cervesato <[email protected]>
5*49cdfc7eSAndroid Build Coastguard Worker  */
6*49cdfc7eSAndroid Build Coastguard Worker 
7*49cdfc7eSAndroid Build Coastguard Worker /*\
8*49cdfc7eSAndroid Build Coastguard Worker  * [Description]
9*49cdfc7eSAndroid Build Coastguard Worker  *
10*49cdfc7eSAndroid Build Coastguard Worker  * This test is checking if waitid() syscall recognizes a process that ended
11*49cdfc7eSAndroid Build Coastguard Worker  * with division by zero error.
12*49cdfc7eSAndroid Build Coastguard Worker  */
13*49cdfc7eSAndroid Build Coastguard Worker 
14*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
15*49cdfc7eSAndroid Build Coastguard Worker #include <sys/wait.h>
16*49cdfc7eSAndroid Build Coastguard Worker #include <sys/prctl.h>
17*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
18*49cdfc7eSAndroid Build Coastguard Worker 
19*49cdfc7eSAndroid Build Coastguard Worker static siginfo_t *infop;
20*49cdfc7eSAndroid Build Coastguard Worker static int core_dumps = 1;
21*49cdfc7eSAndroid Build Coastguard Worker 
run(void)22*49cdfc7eSAndroid Build Coastguard Worker static void run(void)
23*49cdfc7eSAndroid Build Coastguard Worker {
24*49cdfc7eSAndroid Build Coastguard Worker 	pid_t pidchild;
25*49cdfc7eSAndroid Build Coastguard Worker 
26*49cdfc7eSAndroid Build Coastguard Worker 	/*
27*49cdfc7eSAndroid Build Coastguard Worker 	 * Triggering SIGFPE by invalid instruction is not always possible,
28*49cdfc7eSAndroid Build Coastguard Worker 	 * some architectures does not trap division-by-zero at all and even
29*49cdfc7eSAndroid Build Coastguard Worker 	 * when it's possible we would have to fight the compiler optimizations
30*49cdfc7eSAndroid Build Coastguard Worker 	 * that have tendency to remove undefined operations.
31*49cdfc7eSAndroid Build Coastguard Worker 	 */
32*49cdfc7eSAndroid Build Coastguard Worker 	pidchild = SAFE_FORK();
33*49cdfc7eSAndroid Build Coastguard Worker 	if (!pidchild)
34*49cdfc7eSAndroid Build Coastguard Worker 		raise(SIGFPE);
35*49cdfc7eSAndroid Build Coastguard Worker 
36*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_PASS(waitid(P_ALL, 0, infop, WEXITED));
37*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_EQ_LI(infop->si_pid, pidchild);
38*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_EQ_LI(infop->si_status, SIGFPE);
39*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_EQ_LI(infop->si_signo, SIGCHLD);
40*49cdfc7eSAndroid Build Coastguard Worker 
41*49cdfc7eSAndroid Build Coastguard Worker 	if (core_dumps)
42*49cdfc7eSAndroid Build Coastguard Worker 		TST_EXP_EQ_LI(infop->si_code, CLD_DUMPED);
43*49cdfc7eSAndroid Build Coastguard Worker 	else
44*49cdfc7eSAndroid Build Coastguard Worker 		TST_EXP_EQ_LI(infop->si_code, CLD_KILLED);
45*49cdfc7eSAndroid Build Coastguard Worker }
46*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)47*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
48*49cdfc7eSAndroid Build Coastguard Worker {
49*49cdfc7eSAndroid Build Coastguard Worker 	struct rlimit rlim;
50*49cdfc7eSAndroid Build Coastguard Worker 	char c;
51*49cdfc7eSAndroid Build Coastguard Worker 
52*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_GETRLIMIT(RLIMIT_CORE, &rlim);
53*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_FILE_SCANF("/proc/sys/kernel/core_pattern", "%c", &c);
54*49cdfc7eSAndroid Build Coastguard Worker 
55*49cdfc7eSAndroid Build Coastguard Worker 	if (rlim.rlim_cur)
56*49cdfc7eSAndroid Build Coastguard Worker 		return;
57*49cdfc7eSAndroid Build Coastguard Worker 
58*49cdfc7eSAndroid Build Coastguard Worker 	if (!rlim.rlim_max) {
59*49cdfc7eSAndroid Build Coastguard Worker 		if (c != '|')
60*49cdfc7eSAndroid Build Coastguard Worker 			core_dumps = 0;
61*49cdfc7eSAndroid Build Coastguard Worker 		return;
62*49cdfc7eSAndroid Build Coastguard Worker 	}
63*49cdfc7eSAndroid Build Coastguard Worker 
64*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TINFO, "Raising RLIMIT_CORE rlim_cur=%li -> %li",
65*49cdfc7eSAndroid Build Coastguard Worker 	        rlim.rlim_cur, rlim.rlim_max);
66*49cdfc7eSAndroid Build Coastguard Worker 
67*49cdfc7eSAndroid Build Coastguard Worker 	rlim.rlim_cur = rlim.rlim_max;
68*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_SETRLIMIT(RLIMIT_CORE, &rlim);
69*49cdfc7eSAndroid Build Coastguard Worker }
70*49cdfc7eSAndroid Build Coastguard Worker 
71*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
72*49cdfc7eSAndroid Build Coastguard Worker 	.test_all = run,
73*49cdfc7eSAndroid Build Coastguard Worker 	.forks_child = 1,
74*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
75*49cdfc7eSAndroid Build Coastguard Worker 	.bufs =	(struct tst_buffers[]) {
76*49cdfc7eSAndroid Build Coastguard Worker 		{&infop, .size = sizeof(*infop)},
77*49cdfc7eSAndroid Build Coastguard Worker 		{},
78*49cdfc7eSAndroid Build Coastguard Worker 	},
79*49cdfc7eSAndroid Build Coastguard Worker 	.needs_tmpdir = 1,
80*49cdfc7eSAndroid Build Coastguard Worker };
81