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