1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) International Business Machines Corp., 2006
4 * Copyright (c) Linux Test Project, 2003-2023
5 * Author: Yi Yang <[email protected]>
6 */
7
8 /*\
9 * [Description]
10 *
11 * Tests basic error handling of the fchmodat() syscall.
12 *
13 * - fchmodat() fails with ENOTDIR if dir_fd is file descriptor
14 * to the file and pathname is relative path of the file.
15 * - fchmodat() fails with EBADF if dir_fd is invalid.
16 * - fchmodat() fails with EFAULT if pathname points outside
17 * the accessible address space.
18 * - fchmodat() fails with ENAMETOOLONG if path is too long.
19 * - fchmodat() fails with ENOENT if pathname does not exist.
20 * - fchmodat() fails with EINVAL if flag is invalid.
21 */
22
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include "tst_test.h"
26
27 #define TESTFILE "fchmodatfile"
28
29 static int file_fd;
30 static int bad_fd = -1;
31 static char path[PATH_MAX + 2];
32 static char *long_path = path;
33 static int fd_atcwd = AT_FDCWD;
34 static char *bad_path;
35 static char *test_path;
36 static char *empty_path;
37
38 static struct tcase {
39 int *fd;
40 char **filenames;
41 int flag;
42 int exp_errno;
43 const char *desc;
44 } tcases[] = {
45 {&file_fd, &test_path, 0, ENOTDIR, "fd pointing to file"},
46 {&bad_fd, &test_path, 0, EBADF, "invalid fd"},
47 {&file_fd, &bad_path, 0, EFAULT, "invalid address"},
48 {&file_fd, &long_path, 0, ENAMETOOLONG, "pathname too long"},
49 {&file_fd, &empty_path, 0, ENOENT, "path is empty"},
50 {&fd_atcwd, &test_path, -1, EINVAL, "invalid flag"},
51 };
52
verify_fchmodat(unsigned int i)53 static void verify_fchmodat(unsigned int i)
54 {
55 struct tcase *tc = &tcases[i];
56
57 TST_EXP_FAIL(fchmodat(*tc->fd, *tc->filenames, 0600, tc->flag),
58 tc->exp_errno, "fchmodat() with %s", tc->desc);
59 }
60
setup(void)61 static void setup(void)
62 {
63 file_fd = SAFE_OPEN(TESTFILE, O_CREAT | O_RDWR, 0600);
64
65 bad_path = tst_get_bad_addr(NULL);
66
67 memset(path, 'a', PATH_MAX + 2);
68 }
69
cleanup(void)70 static void cleanup(void)
71 {
72 if (file_fd > -1)
73 SAFE_CLOSE(file_fd);
74 }
75
76 static struct tst_test test = {
77 .test = verify_fchmodat,
78 .tcnt = ARRAY_SIZE(tcases),
79 .setup = setup,
80 .cleanup = cleanup,
81 .bufs = (struct tst_buffers []) {
82 {&test_path, .str = TESTFILE},
83 {&empty_path, .str = ""},
84 {},
85 },
86 .needs_tmpdir = 1,
87 };
88