1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) International Business Machines Corp., 2006
4 *
5 * 08/28/2006 AUTHOR: Yi Yang <yyangcdl@cn.ibm.com>
6 */
7
8 /*\
9 * [Description]
10 *
11 * This test case will verify basic function of fchmodat.
12 */
13
14 #define _GNU_SOURCE
15
16 #include <unistd.h>
17 #include <string.h>
18 #include <stdlib.h>
19 #include <stdio.h>
20 #include "tst_test.h"
21 #include "lapi/syscalls.h"
22
23 #ifndef AT_FDCWD
24 #define AT_FDCWD -100
25 #endif
26
27 static char pathname[256];
28 static char testfile[256];
29 static char testfile2[256];
30 static char testfile3[256];
31
32 static struct tcase {
33 int exp_errno;
34 char *exp_errval;
35 } tcases[] = {
36 { 0, NULL},
37 { 0, NULL},
38 { ENOTDIR, "ENOTDIR"},
39 { EBADF, "EBADF"},
40 { 0, NULL},
41 { 0, NULL},
42 };
43 static int fds[ARRAY_SIZE(tcases)];
44 static char *filenames[ARRAY_SIZE(tcases)];
45
verify_fchmodat(unsigned int i)46 static void verify_fchmodat(unsigned int i)
47 {
48 struct tcase *tc = &tcases[i];
49
50 if (tc->exp_errno == 0)
51 TST_EXP_PASS(tst_syscall(__NR_fchmodat, fds[i], filenames[i], 0600),
52 "fchmodat() returned the expected errno %d: %s",
53 TST_ERR, strerror(TST_ERR));
54 else
55 TST_EXP_FAIL(tst_syscall(__NR_fchmodat, fds[i], filenames[i], 0600),
56 tc->exp_errno,
57 "fchmodat() returned the expected errno %d: %s",
58 TST_ERR, strerror(TST_ERR));
59 }
60
setup(void)61 static void setup(void)
62 {
63 /* Initialize test dir and file names */
64 char *abs_path = tst_get_tmpdir();
65 int p = getpid();
66
67 sprintf(pathname, "fchmodattestdir%d", p);
68 sprintf(testfile, "fchmodattest%d.txt", p);
69 sprintf(testfile2, "%s/fchmodattest%d.txt", abs_path, p);
70 sprintf(testfile3, "fchmodattestdir%d/fchmodattest%d.txt", p, p);
71
72 free(abs_path);
73
74 SAFE_MKDIR(pathname, 0700);
75
76 fds[0] = SAFE_OPEN(pathname, O_DIRECTORY);
77 fds[1] = fds[4] = fds[0];
78
79 SAFE_FILE_PRINTF(testfile, "%s", testfile);
80 SAFE_FILE_PRINTF(testfile2, "%s", testfile2);
81
82 fds[2] = SAFE_OPEN(testfile3, O_CREAT | O_RDWR, 0600);
83 fds[3] = 100;
84 fds[5] = AT_FDCWD;
85
86 filenames[0] = filenames[2] = filenames[3] = filenames[4] = testfile;
87 filenames[1] = testfile2;
88 filenames[5] = testfile3;
89 }
90
cleanup(void)91 static void cleanup(void)
92 {
93 if (fds[0] > 0)
94 close(fds[0]);
95 if (fds[2] > 0)
96 close(fds[2]);
97 }
98
99 static struct tst_test test = {
100 .tcnt = ARRAY_SIZE(tcases),
101 .test = verify_fchmodat,
102 .setup = setup,
103 .cleanup = cleanup,
104 .needs_tmpdir = 1,
105 };
106