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