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