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