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 */
5*49cdfc7eSAndroid Build Coastguard Worker
6*49cdfc7eSAndroid Build Coastguard Worker /*\
7*49cdfc7eSAndroid Build Coastguard Worker * [Description]
8*49cdfc7eSAndroid Build Coastguard Worker *
9*49cdfc7eSAndroid Build Coastguard Worker * Verify that, fchmod(2) will succeed to change the mode of a directory
10*49cdfc7eSAndroid Build Coastguard Worker * and set the sticky bit on it if invoked by non-root (uid != 0) process
11*49cdfc7eSAndroid Build Coastguard Worker * with the following constraints:
12*49cdfc7eSAndroid Build Coastguard Worker *
13*49cdfc7eSAndroid Build Coastguard Worker * - the process is the owner of the directory
14*49cdfc7eSAndroid Build Coastguard Worker * - the effective group ID or one of the supplementary group ID's of the
15*49cdfc7eSAndroid Build Coastguard Worker * process is equal to the group ID of the directory
16*49cdfc7eSAndroid Build Coastguard Worker */
17*49cdfc7eSAndroid Build Coastguard Worker
18*49cdfc7eSAndroid Build Coastguard Worker #include <pwd.h>
19*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
20*49cdfc7eSAndroid Build Coastguard Worker #include "fchmod.h"
21*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
22*49cdfc7eSAndroid Build Coastguard Worker
23*49cdfc7eSAndroid Build Coastguard Worker static int fd;
24*49cdfc7eSAndroid Build Coastguard Worker static const char nobody_uid[] = "nobody";
25*49cdfc7eSAndroid Build Coastguard Worker
verify_fchmod(void)26*49cdfc7eSAndroid Build Coastguard Worker static void verify_fchmod(void)
27*49cdfc7eSAndroid Build Coastguard Worker {
28*49cdfc7eSAndroid Build Coastguard Worker struct stat stat_buf;
29*49cdfc7eSAndroid Build Coastguard Worker mode_t dir_mode;
30*49cdfc7eSAndroid Build Coastguard Worker
31*49cdfc7eSAndroid Build Coastguard Worker TST_EXP_PASS_SILENT(fchmod(fd, PERMS));
32*49cdfc7eSAndroid Build Coastguard Worker
33*49cdfc7eSAndroid Build Coastguard Worker if (fstat(fd, &stat_buf) == -1)
34*49cdfc7eSAndroid Build Coastguard Worker tst_brk(TFAIL | TERRNO, "fstat failed");
35*49cdfc7eSAndroid Build Coastguard Worker dir_mode = stat_buf.st_mode;
36*49cdfc7eSAndroid Build Coastguard Worker
37*49cdfc7eSAndroid Build Coastguard Worker if ((dir_mode & PERMS) == PERMS)
38*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS, "Functionality of fchmod(%d, "
39*49cdfc7eSAndroid Build Coastguard Worker "%#o) successful", fd, PERMS);
40*49cdfc7eSAndroid Build Coastguard Worker else
41*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL, "%s: Incorrect modes 0%03o, "
42*49cdfc7eSAndroid Build Coastguard Worker "Expected 0%03o",
43*49cdfc7eSAndroid Build Coastguard Worker TESTDIR, dir_mode, PERMS);
44*49cdfc7eSAndroid Build Coastguard Worker }
45*49cdfc7eSAndroid Build Coastguard Worker
setup(void)46*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
47*49cdfc7eSAndroid Build Coastguard Worker {
48*49cdfc7eSAndroid Build Coastguard Worker SAFE_GETPWNAM(nobody_uid);
49*49cdfc7eSAndroid Build Coastguard Worker SAFE_MKDIR(TESTDIR, DIR_MODE);
50*49cdfc7eSAndroid Build Coastguard Worker fd = SAFE_OPEN(TESTDIR, O_RDONLY);
51*49cdfc7eSAndroid Build Coastguard Worker }
52*49cdfc7eSAndroid Build Coastguard Worker
cleanup(void)53*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
54*49cdfc7eSAndroid Build Coastguard Worker {
55*49cdfc7eSAndroid Build Coastguard Worker if (fd > 0)
56*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(fd);
57*49cdfc7eSAndroid Build Coastguard Worker }
58*49cdfc7eSAndroid Build Coastguard Worker
59*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
60*49cdfc7eSAndroid Build Coastguard Worker .test_all = verify_fchmod,
61*49cdfc7eSAndroid Build Coastguard Worker .setup = setup,
62*49cdfc7eSAndroid Build Coastguard Worker .cleanup = cleanup,
63*49cdfc7eSAndroid Build Coastguard Worker .needs_tmpdir = 1,
64*49cdfc7eSAndroid Build Coastguard Worker .needs_root = 1,
65*49cdfc7eSAndroid Build Coastguard Worker };
66