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., 2001
4*49cdfc7eSAndroid Build Coastguard Worker * 07/2001 Ported by Wayne Boyer
5*49cdfc7eSAndroid Build Coastguard Worker *
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 for failures:
12*49cdfc7eSAndroid Build Coastguard Worker *
13*49cdfc7eSAndroid Build Coastguard Worker * - ENOTDIR A component of the pathname, which is not a directory.
14*49cdfc7eSAndroid Build Coastguard Worker * - ENOENT A filename which doesn't exist.
15*49cdfc7eSAndroid Build Coastguard Worker * - ENAMETOOLONG A pathname which is longer than MAXNAMLEN.
16*49cdfc7eSAndroid Build Coastguard Worker * - EFAULT A pathname pointer outside the address space of the process.
17*49cdfc7eSAndroid Build Coastguard Worker * - EFAULT A buf pointer outside the address space of the process.
18*49cdfc7eSAndroid Build Coastguard Worker * - ELOOP A filename which has too many symbolic links.
19*49cdfc7eSAndroid Build Coastguard Worker */
20*49cdfc7eSAndroid Build Coastguard Worker
21*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
22*49cdfc7eSAndroid Build Coastguard Worker #include <fcntl.h>
23*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
24*49cdfc7eSAndroid Build Coastguard Worker #include <sys/statfs.h>
25*49cdfc7eSAndroid Build Coastguard Worker #include <sys/wait.h>
26*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
27*49cdfc7eSAndroid Build Coastguard Worker #include "tst_safe_macros.h"
28*49cdfc7eSAndroid Build Coastguard Worker
29*49cdfc7eSAndroid Build Coastguard Worker #define TEST_FILE "statfs_file"
30*49cdfc7eSAndroid Build Coastguard Worker #define TEST_FILE1 TEST_FILE"/statfs_file1"
31*49cdfc7eSAndroid Build Coastguard Worker #define TEST_NOEXIST "statfs_noexist"
32*49cdfc7eSAndroid Build Coastguard Worker #define TEST_SYMLINK "statfs_symlink"
33*49cdfc7eSAndroid Build Coastguard Worker
34*49cdfc7eSAndroid Build Coastguard Worker static int fd;
35*49cdfc7eSAndroid Build Coastguard Worker static char test_toolong[PATH_MAX+2];
36*49cdfc7eSAndroid Build Coastguard Worker static struct statfs buf;
37*49cdfc7eSAndroid Build Coastguard Worker
38*49cdfc7eSAndroid Build Coastguard Worker static struct test_case_t {
39*49cdfc7eSAndroid Build Coastguard Worker char *path;
40*49cdfc7eSAndroid Build Coastguard Worker struct statfs *buf;
41*49cdfc7eSAndroid Build Coastguard Worker int exp_error;
42*49cdfc7eSAndroid Build Coastguard Worker } tests[] = {
43*49cdfc7eSAndroid Build Coastguard Worker {TEST_FILE1, &buf, ENOTDIR},
44*49cdfc7eSAndroid Build Coastguard Worker {TEST_NOEXIST, &buf, ENOENT},
45*49cdfc7eSAndroid Build Coastguard Worker {test_toolong, &buf, ENAMETOOLONG},
46*49cdfc7eSAndroid Build Coastguard Worker {(char *)-1, &buf, EFAULT},
47*49cdfc7eSAndroid Build Coastguard Worker {TEST_FILE, (struct statfs *)-1, EFAULT},
48*49cdfc7eSAndroid Build Coastguard Worker {TEST_SYMLINK, &buf, ELOOP},
49*49cdfc7eSAndroid Build Coastguard Worker };
50*49cdfc7eSAndroid Build Coastguard Worker
statfs_verify(unsigned int n)51*49cdfc7eSAndroid Build Coastguard Worker static void statfs_verify(unsigned int n)
52*49cdfc7eSAndroid Build Coastguard Worker {
53*49cdfc7eSAndroid Build Coastguard Worker int pid, status;
54*49cdfc7eSAndroid Build Coastguard Worker
55*49cdfc7eSAndroid Build Coastguard Worker pid = SAFE_FORK();
56*49cdfc7eSAndroid Build Coastguard Worker if (!pid) {
57*49cdfc7eSAndroid Build Coastguard Worker TST_EXP_FAIL(statfs(tests[n].path, tests[n].buf), tests[n].exp_error, "statfs()");
58*49cdfc7eSAndroid Build Coastguard Worker exit(0);
59*49cdfc7eSAndroid Build Coastguard Worker }
60*49cdfc7eSAndroid Build Coastguard Worker
61*49cdfc7eSAndroid Build Coastguard Worker SAFE_WAITPID(pid, &status, 0);
62*49cdfc7eSAndroid Build Coastguard Worker
63*49cdfc7eSAndroid Build Coastguard Worker if (WIFEXITED(status) && WEXITSTATUS(status) == 0)
64*49cdfc7eSAndroid Build Coastguard Worker return;
65*49cdfc7eSAndroid Build Coastguard Worker
66*49cdfc7eSAndroid Build Coastguard Worker if (tests[n].exp_error == EFAULT &&
67*49cdfc7eSAndroid Build Coastguard Worker WIFSIGNALED(status) && WTERMSIG(status) == SIGSEGV) {
68*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS, "Got SIGSEGV instead of EFAULT");
69*49cdfc7eSAndroid Build Coastguard Worker return;
70*49cdfc7eSAndroid Build Coastguard Worker }
71*49cdfc7eSAndroid Build Coastguard Worker
72*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL, "Child %s", tst_strstatus(status));
73*49cdfc7eSAndroid Build Coastguard Worker }
74*49cdfc7eSAndroid Build Coastguard Worker
setup(void)75*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
76*49cdfc7eSAndroid Build Coastguard Worker {
77*49cdfc7eSAndroid Build Coastguard Worker unsigned int i;
78*49cdfc7eSAndroid Build Coastguard Worker
79*49cdfc7eSAndroid Build Coastguard Worker fd = SAFE_CREAT(TEST_FILE, 0444);
80*49cdfc7eSAndroid Build Coastguard Worker
81*49cdfc7eSAndroid Build Coastguard Worker memset(test_toolong, 'a', PATH_MAX+1);
82*49cdfc7eSAndroid Build Coastguard Worker
83*49cdfc7eSAndroid Build Coastguard Worker for (i = 0; i < ARRAY_SIZE(tests); i++) {
84*49cdfc7eSAndroid Build Coastguard Worker if (tests[i].path == (char *)-1)
85*49cdfc7eSAndroid Build Coastguard Worker tests[i].path = tst_get_bad_addr(NULL);
86*49cdfc7eSAndroid Build Coastguard Worker }
87*49cdfc7eSAndroid Build Coastguard Worker
88*49cdfc7eSAndroid Build Coastguard Worker SAFE_SYMLINK(TEST_SYMLINK, "statfs_symlink_2");
89*49cdfc7eSAndroid Build Coastguard Worker SAFE_SYMLINK("statfs_symlink_2", TEST_SYMLINK);
90*49cdfc7eSAndroid Build Coastguard Worker }
91*49cdfc7eSAndroid Build Coastguard Worker
cleanup(void)92*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
93*49cdfc7eSAndroid Build Coastguard Worker {
94*49cdfc7eSAndroid Build Coastguard Worker if (fd > 0)
95*49cdfc7eSAndroid Build Coastguard Worker close(fd);
96*49cdfc7eSAndroid Build Coastguard Worker }
97*49cdfc7eSAndroid Build Coastguard Worker
98*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
99*49cdfc7eSAndroid Build Coastguard Worker .test = statfs_verify,
100*49cdfc7eSAndroid Build Coastguard Worker .tcnt = ARRAY_SIZE(tests),
101*49cdfc7eSAndroid Build Coastguard Worker .setup = setup,
102*49cdfc7eSAndroid Build Coastguard Worker .cleanup = cleanup,
103*49cdfc7eSAndroid Build Coastguard Worker .needs_tmpdir = 1,
104*49cdfc7eSAndroid Build Coastguard Worker .forks_child = 1,
105*49cdfc7eSAndroid Build Coastguard Worker };
106