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