1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci * Copyright (c) International Business Machines  Corp., 2001
4f08c3bdfSopenharmony_ci */
5f08c3bdfSopenharmony_ci
6f08c3bdfSopenharmony_ci/*\
7f08c3bdfSopenharmony_ci * [Description]
8f08c3bdfSopenharmony_ci *
9f08c3bdfSopenharmony_ci * Testcase to check if fstatfs() sets errno correctly.
10f08c3bdfSopenharmony_ci */
11f08c3bdfSopenharmony_ci
12f08c3bdfSopenharmony_ci#include <errno.h>
13f08c3bdfSopenharmony_ci#include <stdlib.h>
14f08c3bdfSopenharmony_ci#include <sys/types.h>
15f08c3bdfSopenharmony_ci#include <sys/statfs.h>
16f08c3bdfSopenharmony_ci#include <sys/wait.h>
17f08c3bdfSopenharmony_ci#include "tst_test.h"
18f08c3bdfSopenharmony_ci#include "tst_safe_macros.h"
19f08c3bdfSopenharmony_ci
20f08c3bdfSopenharmony_cistatic struct statfs buf;
21f08c3bdfSopenharmony_ci
22f08c3bdfSopenharmony_cistatic int fd;
23f08c3bdfSopenharmony_cistatic int bad_fd = -1;
24f08c3bdfSopenharmony_ci
25f08c3bdfSopenharmony_cistatic struct test_case_t {
26f08c3bdfSopenharmony_ci	int *fd;
27f08c3bdfSopenharmony_ci	struct statfs *sbuf;
28f08c3bdfSopenharmony_ci	int error;
29f08c3bdfSopenharmony_ci} tests[] = {
30f08c3bdfSopenharmony_ci	{&bad_fd, &buf, EBADF},
31f08c3bdfSopenharmony_ci	{&fd, (void *)-1, EFAULT},
32f08c3bdfSopenharmony_ci};
33f08c3bdfSopenharmony_ci
34f08c3bdfSopenharmony_cistatic void fstatfs_verify(unsigned int n)
35f08c3bdfSopenharmony_ci{
36f08c3bdfSopenharmony_ci	int pid, status;
37f08c3bdfSopenharmony_ci
38f08c3bdfSopenharmony_ci	pid = SAFE_FORK();
39f08c3bdfSopenharmony_ci	if (!pid) {
40f08c3bdfSopenharmony_ci		TST_EXP_FAIL(fstatfs(*tests[n].fd, tests[n].sbuf), tests[n].error, "fstatfs()");
41f08c3bdfSopenharmony_ci		exit(0);
42f08c3bdfSopenharmony_ci	}
43f08c3bdfSopenharmony_ci
44f08c3bdfSopenharmony_ci	SAFE_WAITPID(pid, &status, 0);
45f08c3bdfSopenharmony_ci
46f08c3bdfSopenharmony_ci	if (WIFEXITED(status) && WEXITSTATUS(status) == 0)
47f08c3bdfSopenharmony_ci		return;
48f08c3bdfSopenharmony_ci
49f08c3bdfSopenharmony_ci	if (tests[n].error == EFAULT &&
50f08c3bdfSopenharmony_ci	    WIFSIGNALED(status) && WTERMSIG(status) == SIGSEGV) {
51f08c3bdfSopenharmony_ci		tst_res(TPASS, "Got SIGSEGV instead of EFAULT");
52f08c3bdfSopenharmony_ci		return;
53f08c3bdfSopenharmony_ci	}
54f08c3bdfSopenharmony_ci
55f08c3bdfSopenharmony_ci	tst_res(TFAIL, "Child %s", tst_strstatus(status));
56f08c3bdfSopenharmony_ci}
57f08c3bdfSopenharmony_ci
58f08c3bdfSopenharmony_cistatic void setup(void)
59f08c3bdfSopenharmony_ci{
60f08c3bdfSopenharmony_ci	fd = SAFE_OPEN("tempfile", O_RDWR | O_CREAT, 0700);
61f08c3bdfSopenharmony_ci}
62f08c3bdfSopenharmony_ci
63f08c3bdfSopenharmony_cistatic void cleanup(void)
64f08c3bdfSopenharmony_ci{
65f08c3bdfSopenharmony_ci	if (fd > 0)
66f08c3bdfSopenharmony_ci		SAFE_CLOSE(fd);
67f08c3bdfSopenharmony_ci}
68f08c3bdfSopenharmony_ci
69f08c3bdfSopenharmony_cistatic struct tst_test test = {
70f08c3bdfSopenharmony_ci	.test = fstatfs_verify,
71f08c3bdfSopenharmony_ci	.tcnt = ARRAY_SIZE(tests),
72f08c3bdfSopenharmony_ci	.setup = setup,
73f08c3bdfSopenharmony_ci	.cleanup = cleanup,
74f08c3bdfSopenharmony_ci	.needs_tmpdir = 1,
75f08c3bdfSopenharmony_ci	.forks_child = 1,
76f08c3bdfSopenharmony_ci};
77