1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) International Business Machines Corp., 2001
4 * Ported to LTP: Wayne Boyer
5 * Copyright (c) 2017 Cyril Hrubis <chrubis@suse.cz>
6 */
7 /*
8 * Testcase to test the different errnos set by setrlimit(2) system call.
9 */
10 #include <pwd.h>
11 #include <errno.h>
12 #include "tst_test.h"
13
14 static char nobody_uid[] = "nobody";
15 static struct rlimit rlim;
16
17 static struct tcase {
18 int resource;
19 struct rlimit *rlim;
20 int exp_errno;
21 } tcases[] = {
22 {-1, &rlim, EINVAL},
23 {RLIMIT_NOFILE, &rlim, EPERM}
24 };
25
verify_setrlimit(unsigned int n)26 static void verify_setrlimit(unsigned int n)
27 {
28 struct tcase *tc = &tcases[n];
29
30 TEST(setrlimit(tc->resource, tc->rlim));
31
32 if (TST_RET != -1) {
33 tst_res(TFAIL, "call succeeded unexpectedly");
34 return;
35 }
36
37 if (TST_ERR != tc->exp_errno) {
38 tst_res(TFAIL | TTERRNO,
39 "setrlimit() should fail with %s got",
40 tst_strerrno(tc->exp_errno));
41 return;
42 }
43
44 tst_res(TPASS | TTERRNO, "setrlimit() failed as expected");
45 }
46
setup(void)47 static void setup(void)
48 {
49 struct passwd *ltpuser = SAFE_GETPWNAM(nobody_uid);
50
51 SAFE_SETUID(ltpuser->pw_uid);
52
53 SAFE_GETRLIMIT(RLIMIT_NOFILE, &rlim);
54 rlim.rlim_max++;
55 }
56
57 static struct tst_test test = {
58 .setup = setup,
59 .test = verify_setrlimit,
60 .tcnt = ARRAY_SIZE(tcases),
61 .needs_root = 1,
62 };
63