xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/setrlimit/setrlimit04.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) 2017 Red Hat, Inc.  All rights reserved.
4*49cdfc7eSAndroid Build Coastguard Worker  *
5*49cdfc7eSAndroid Build Coastguard Worker  * Attempt to run a trivial binary with stack < 1MB.
6*49cdfc7eSAndroid Build Coastguard Worker  *
7*49cdfc7eSAndroid Build Coastguard Worker  * Early patches for stack guard gap caused that gap size was
8*49cdfc7eSAndroid Build Coastguard Worker  * contributing to stack limit. This caused failures
9*49cdfc7eSAndroid Build Coastguard Worker  * for new processes (E2BIG) when ulimit was set to anything
10*49cdfc7eSAndroid Build Coastguard Worker  * lower than size of gap. commit 1be7107fbe18 "mm: larger
11*49cdfc7eSAndroid Build Coastguard Worker  * stack guard gap, between vmas" sets default gap size to 1M
12*49cdfc7eSAndroid Build Coastguard Worker  * (for systems with 4k pages), so let's set stack limit to 512kB
13*49cdfc7eSAndroid Build Coastguard Worker  * and confirm we can still run some trivial binary.
14*49cdfc7eSAndroid Build Coastguard Worker  */
15*49cdfc7eSAndroid Build Coastguard Worker 
16*49cdfc7eSAndroid Build Coastguard Worker #define _GNU_SOURCE
17*49cdfc7eSAndroid Build Coastguard Worker #include <sys/resource.h>
18*49cdfc7eSAndroid Build Coastguard Worker #include <sys/time.h>
19*49cdfc7eSAndroid Build Coastguard Worker #include <sys/wait.h>
20*49cdfc7eSAndroid Build Coastguard Worker 
21*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
22*49cdfc7eSAndroid Build Coastguard Worker 
23*49cdfc7eSAndroid Build Coastguard Worker #define STACK_LIMIT (512 * 1024)
24*49cdfc7eSAndroid Build Coastguard Worker 
test_setrlimit(void)25*49cdfc7eSAndroid Build Coastguard Worker static void test_setrlimit(void)
26*49cdfc7eSAndroid Build Coastguard Worker {
27*49cdfc7eSAndroid Build Coastguard Worker 	int status;
28*49cdfc7eSAndroid Build Coastguard Worker 	struct rlimit rlim;
29*49cdfc7eSAndroid Build Coastguard Worker 	pid_t child;
30*49cdfc7eSAndroid Build Coastguard Worker 
31*49cdfc7eSAndroid Build Coastguard Worker 	rlim.rlim_cur = STACK_LIMIT;
32*49cdfc7eSAndroid Build Coastguard Worker 	rlim.rlim_max = STACK_LIMIT;
33*49cdfc7eSAndroid Build Coastguard Worker 
34*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_SETRLIMIT(RLIMIT_STACK, &rlim);
35*49cdfc7eSAndroid Build Coastguard Worker 
36*49cdfc7eSAndroid Build Coastguard Worker 	child = SAFE_FORK();
37*49cdfc7eSAndroid Build Coastguard Worker 	if (child == 0)
38*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_EXECLP("/bin/true", "/bin/true", NULL);
39*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_WAITPID(child, &status, 0);
40*49cdfc7eSAndroid Build Coastguard Worker 
41*49cdfc7eSAndroid Build Coastguard Worker 	if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
42*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS, "child process completed OK");
43*49cdfc7eSAndroid Build Coastguard Worker 		return;
44*49cdfc7eSAndroid Build Coastguard Worker 	}
45*49cdfc7eSAndroid Build Coastguard Worker 
46*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TFAIL, "child %s", tst_strstatus(status));
47*49cdfc7eSAndroid Build Coastguard Worker }
48*49cdfc7eSAndroid Build Coastguard Worker 
49*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
50*49cdfc7eSAndroid Build Coastguard Worker 	.test_all     = test_setrlimit,
51*49cdfc7eSAndroid Build Coastguard Worker 	.forks_child  = 1,
52*49cdfc7eSAndroid Build Coastguard Worker 	.needs_root = 1,
53*49cdfc7eSAndroid Build Coastguard Worker };
54