1 /*
2  * Copyright (c) International Business Machines  Corp., 2001
3  *	07/2001 Ported by Wayne Boyer
4  * Copyright (c) 2017 Cyril Hrubis <chrubis@suse.cz>
5  *
6  * This program is free software;  you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY;  without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
14  * the GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program;  if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 /*
21  * Testcase to test the different errnos set by setrlimit(2) system call.
22  */
23 #include <pwd.h>
24 #include <errno.h>
25 #include "tst_test.h"
26 
27 static char nobody_uid[] = "nobody";
28 static struct rlimit rlim;
29 
30 static struct tcase {
31 	int resource;
32 	struct rlimit *rlim;
33 	int exp_errno;
34 } tcases[] = {
35 	{-1, &rlim, EINVAL},
36 	{RLIMIT_NOFILE, &rlim, EPERM}
37 };
38 
39 static void verify_setrlimit(unsigned int n)
40 {
41 	struct tcase *tc = &tcases[n];
42 
43 	TEST(setrlimit(tc->resource, tc->rlim));
44 
45 	if (TEST_RETURN != -1) {
46 		tst_res(TFAIL, "call succeeded unexpectedly");
47 		return;
48 	}
49 
50 	if (TEST_ERRNO != tc->exp_errno) {
51 		tst_res(TFAIL | TTERRNO,
52 			"setrlimit() should fail with %s got",
53 			tst_strerrno(tc->exp_errno));
54 		return;
55 	}
56 
57 	tst_res(TPASS | TTERRNO, "setrlimit() failed as expected");
58 }
59 
60 static void setup(void)
61 {
62 	struct passwd *ltpuser = SAFE_GETPWNAM(nobody_uid);
63 
64 	SAFE_SETUID(ltpuser->pw_uid);
65 
66 	SAFE_GETRLIMIT(RLIMIT_NOFILE, &rlim);
67 	rlim.rlim_max++;
68 }
69 
70 static struct tst_test test = {
71 	.setup = setup,
72 	.test = verify_setrlimit,
73 	.tcnt = ARRAY_SIZE(tcases),
74 	.needs_root = 1,
75 };
76