1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci * Copyright (c) International Business Machines Corp., 2001
4f08c3bdfSopenharmony_ci * Author: Wayne Boyer
5f08c3bdfSopenharmony_ci * Copyright (c) 2018 Cyril Hrubis <chrubis@suse.cz>
6f08c3bdfSopenharmony_ci */
7f08c3bdfSopenharmony_ci/*
8f08c3bdfSopenharmony_ci * Test that fchmod() fails and sets the proper errno values.
9f08c3bdfSopenharmony_ci */
10f08c3bdfSopenharmony_ci
11f08c3bdfSopenharmony_ci#ifndef _GNU_SOURCE
12f08c3bdfSopenharmony_ci# define _GNU_SOURCE
13f08c3bdfSopenharmony_ci#endif
14f08c3bdfSopenharmony_ci#include <errno.h>
15f08c3bdfSopenharmony_ci#include <sys/types.h>
16f08c3bdfSopenharmony_ci#include <pwd.h>
17f08c3bdfSopenharmony_ci#include "tst_test.h"
18f08c3bdfSopenharmony_ci
19f08c3bdfSopenharmony_ci#define MNT_POINT "mntpoint"
20f08c3bdfSopenharmony_ci
21f08c3bdfSopenharmony_cistatic int fd1;
22f08c3bdfSopenharmony_cistatic int fd2;
23f08c3bdfSopenharmony_cistatic int fd3;
24f08c3bdfSopenharmony_ci
25f08c3bdfSopenharmony_cistatic struct tcase {
26f08c3bdfSopenharmony_ci	int *fd;
27f08c3bdfSopenharmony_ci	int mode;
28f08c3bdfSopenharmony_ci	int exp_errno;
29f08c3bdfSopenharmony_ci} tcases[] = {
30f08c3bdfSopenharmony_ci	{&fd1, 0644, EPERM},
31f08c3bdfSopenharmony_ci	{&fd2, 0644, EBADF},
32f08c3bdfSopenharmony_ci	{&fd3, 0644, EROFS},
33f08c3bdfSopenharmony_ci};
34f08c3bdfSopenharmony_ci
35f08c3bdfSopenharmony_cistatic void verify_fchmod(unsigned int i)
36f08c3bdfSopenharmony_ci{
37f08c3bdfSopenharmony_ci	struct tcase *tc = &tcases[i];
38f08c3bdfSopenharmony_ci
39f08c3bdfSopenharmony_ci	TEST(fchmod(*tc->fd, tc->mode));
40f08c3bdfSopenharmony_ci
41f08c3bdfSopenharmony_ci	if (TST_RET != -1) {
42f08c3bdfSopenharmony_ci		tst_res(TFAIL, "fchmod() passed unexpectedly (%li)",
43f08c3bdfSopenharmony_ci			TST_RET);
44f08c3bdfSopenharmony_ci		return;
45f08c3bdfSopenharmony_ci	}
46f08c3bdfSopenharmony_ci
47f08c3bdfSopenharmony_ci	if (TST_ERR == tcases[i].exp_errno) {
48f08c3bdfSopenharmony_ci		tst_res(TPASS | TTERRNO, "fchmod() failed expectedly");
49f08c3bdfSopenharmony_ci		return;
50f08c3bdfSopenharmony_ci	}
51f08c3bdfSopenharmony_ci
52f08c3bdfSopenharmony_ci	tst_res(TFAIL | TTERRNO,
53f08c3bdfSopenharmony_ci		"fchmod() failed unexpectedly, expected %i - %s",
54f08c3bdfSopenharmony_ci		TST_ERR, tst_strerrno(TST_ERR));
55f08c3bdfSopenharmony_ci}
56f08c3bdfSopenharmony_ci
57f08c3bdfSopenharmony_cistatic void setup(void)
58f08c3bdfSopenharmony_ci{
59f08c3bdfSopenharmony_ci	struct passwd *ltpuser = SAFE_GETPWNAM("nobody");
60f08c3bdfSopenharmony_ci
61f08c3bdfSopenharmony_ci	fd3 = SAFE_OPEN(MNT_POINT"/file", O_RDONLY);
62f08c3bdfSopenharmony_ci	fd1 = SAFE_OPEN("tfile_1", O_RDWR | O_CREAT, 0666);
63f08c3bdfSopenharmony_ci	fd2 = SAFE_OPEN("tfile_2", O_RDWR | O_CREAT, 0666);
64f08c3bdfSopenharmony_ci	SAFE_CLOSE(fd2);
65f08c3bdfSopenharmony_ci
66f08c3bdfSopenharmony_ci	SAFE_SETEUID(ltpuser->pw_uid);
67f08c3bdfSopenharmony_ci}
68f08c3bdfSopenharmony_ci
69f08c3bdfSopenharmony_cistatic void cleanup(void)
70f08c3bdfSopenharmony_ci{
71f08c3bdfSopenharmony_ci	if (fd1 > 0)
72f08c3bdfSopenharmony_ci		SAFE_CLOSE(fd1);
73f08c3bdfSopenharmony_ci
74f08c3bdfSopenharmony_ci	if (fd3 > 0)
75f08c3bdfSopenharmony_ci		SAFE_CLOSE(fd3);
76f08c3bdfSopenharmony_ci}
77f08c3bdfSopenharmony_ci
78f08c3bdfSopenharmony_cistatic struct tst_test test = {
79f08c3bdfSopenharmony_ci	.setup = setup,
80f08c3bdfSopenharmony_ci	.cleanup = cleanup,
81f08c3bdfSopenharmony_ci	.test = verify_fchmod,
82f08c3bdfSopenharmony_ci	.tcnt = ARRAY_SIZE(tcases),
83f08c3bdfSopenharmony_ci	.needs_root = 1,
84f08c3bdfSopenharmony_ci	.needs_rofs = 1,
85f08c3bdfSopenharmony_ci	.mntpoint = MNT_POINT,
86f08c3bdfSopenharmony_ci};
87