1 /*
2  *   Copyright (C) 2017 Red Hat, Inc.  All rights reserved.
3  *
4  *   This program is free software;  you can redistribute it and/or modify
5  *   it under the terms of the GNU General Public License as published by
6  *   the Free Software Foundation; either version 2 of the License, or
7  *   (at your option) any later version.
8  *
9  *   This program is distributed in the hope that it will be useful,
10  *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
11  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
12  *   the GNU General Public License for more details.
13  *
14  *   DESCRIPTION
15  *     Attempt to run a trivial binary with stack < 1MB.
16  *
17  *     Early patches for stack guard gap caused that gap size was
18  *     contributing to stack limit. This caused failures
19  *     for new processes (E2BIG) when ulimit was set to anything
20  *     lower than size of gap. commit 1be7107fbe18 "mm: larger
21  *     stack guard gap, between vmas" sets default gap size to 1M
22  *     (for systems with 4k pages), so let's set stack limit to 512kB
23  *     and confirm we can still run some trivial binary.
24  */
25 
26 #define _GNU_SOURCE
27 #include <sys/resource.h>
28 #include <sys/time.h>
29 #include <sys/wait.h>
30 
31 #include "tst_test.h"
32 
33 #define STACK_LIMIT (512 * 1024)
34 
35 static void test_setrlimit(void)
36 {
37 	int status;
38 	struct rlimit rlim;
39 	pid_t child;
40 
41 	rlim.rlim_cur = STACK_LIMIT;
42 	rlim.rlim_max = STACK_LIMIT;
43 
44 	SAFE_SETRLIMIT(RLIMIT_STACK, &rlim);
45 
46 	child = SAFE_FORK();
47 	if (child == 0)
48 		SAFE_EXECLP("/bin/true", "/bin/true", NULL);
49 	SAFE_WAITPID(child, &status, 0);
50 
51 	if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
52 		tst_res(TPASS, "child process completed OK");
53 		return;
54 	}
55 
56 	tst_res(TFAIL, "child %s", tst_strstatus(status));
57 }
58 
59 static struct tst_test test = {
60 	.test_all     = test_setrlimit,
61 	.forks_child  = 1,
62 	.needs_root = 1,
63 };
64