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) 2000 Silicon Graphics, Inc. All Rights Reserved.
4*49cdfc7eSAndroid Build Coastguard Worker * Author: Wayne Boyer and William Roske
5*49cdfc7eSAndroid Build Coastguard Worker *
6*49cdfc7eSAndroid Build Coastguard Worker * Test Description:
7*49cdfc7eSAndroid Build Coastguard Worker * fchmod() will succeed to change the mode permissions of a file specified
8*49cdfc7eSAndroid Build Coastguard Worker * by file descriptor.
9*49cdfc7eSAndroid Build Coastguard Worker */
10*49cdfc7eSAndroid Build Coastguard Worker
11*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
12*49cdfc7eSAndroid Build Coastguard Worker
13*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
14*49cdfc7eSAndroid Build Coastguard Worker #include "fchmod.h"
15*49cdfc7eSAndroid Build Coastguard Worker
16*49cdfc7eSAndroid Build Coastguard Worker static int fd;
17*49cdfc7eSAndroid Build Coastguard Worker static int modes[] = { 0, 07, 070, 0700, 0777, 02777, 04777, 06777 };
18*49cdfc7eSAndroid Build Coastguard Worker
verify_fchmod(void)19*49cdfc7eSAndroid Build Coastguard Worker static void verify_fchmod(void)
20*49cdfc7eSAndroid Build Coastguard Worker {
21*49cdfc7eSAndroid Build Coastguard Worker struct stat stat_buf;
22*49cdfc7eSAndroid Build Coastguard Worker int ind;
23*49cdfc7eSAndroid Build Coastguard Worker mode_t file_mode, mode;
24*49cdfc7eSAndroid Build Coastguard Worker
25*49cdfc7eSAndroid Build Coastguard Worker for (ind = 0; ind < 8; ind++) {
26*49cdfc7eSAndroid Build Coastguard Worker mode = (mode_t)modes[ind];
27*49cdfc7eSAndroid Build Coastguard Worker
28*49cdfc7eSAndroid Build Coastguard Worker TEST(fchmod(fd, mode));
29*49cdfc7eSAndroid Build Coastguard Worker if (TST_RET == -1)
30*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL | TTERRNO, "fchmod() failed unexpectly");
31*49cdfc7eSAndroid Build Coastguard Worker
32*49cdfc7eSAndroid Build Coastguard Worker SAFE_FSTAT(fd, &stat_buf);
33*49cdfc7eSAndroid Build Coastguard Worker file_mode = stat_buf.st_mode;
34*49cdfc7eSAndroid Build Coastguard Worker
35*49cdfc7eSAndroid Build Coastguard Worker if ((file_mode & ~S_IFREG) != mode) {
36*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL,
37*49cdfc7eSAndroid Build Coastguard Worker "%s: Incorrect modes 0%03o, Expected 0%03o",
38*49cdfc7eSAndroid Build Coastguard Worker TESTFILE, file_mode & ~S_IFREG, mode);
39*49cdfc7eSAndroid Build Coastguard Worker } else {
40*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS,
41*49cdfc7eSAndroid Build Coastguard Worker "Functionality of fchmod(%d, %#o) successful",
42*49cdfc7eSAndroid Build Coastguard Worker fd, mode);
43*49cdfc7eSAndroid Build Coastguard Worker }
44*49cdfc7eSAndroid Build Coastguard Worker }
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 fd = SAFE_OPEN(TESTFILE, O_RDWR | O_CREAT, FILE_MODE);
50*49cdfc7eSAndroid Build Coastguard Worker }
51*49cdfc7eSAndroid Build Coastguard Worker
cleanup(void)52*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
53*49cdfc7eSAndroid Build Coastguard Worker {
54*49cdfc7eSAndroid Build Coastguard Worker if (fd > 0)
55*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(fd);
56*49cdfc7eSAndroid Build Coastguard Worker }
57*49cdfc7eSAndroid Build Coastguard Worker
58*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
59*49cdfc7eSAndroid Build Coastguard Worker .test_all = verify_fchmod,
60*49cdfc7eSAndroid Build Coastguard Worker .setup = setup,
61*49cdfc7eSAndroid Build Coastguard Worker .cleanup = cleanup,
62*49cdfc7eSAndroid Build Coastguard Worker .needs_tmpdir = 1,
63*49cdfc7eSAndroid Build Coastguard Worker };
64