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