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