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 *  05/2019 Ported to new library: Christian Amann <camann@suse.com>
6f08c3bdfSopenharmony_ci */
7f08c3bdfSopenharmony_ci/*
8f08c3bdfSopenharmony_ci * Tests different error scenarios:
9f08c3bdfSopenharmony_ci *
10f08c3bdfSopenharmony_ci * 1) Calls fstat() with closed file descriptor
11f08c3bdfSopenharmony_ci *    -> EBADF
12f08c3bdfSopenharmony_ci * 2) Calls fstat() with an invalid address for stat structure
13f08c3bdfSopenharmony_ci *    -> EFAULT (or receive signal SIGSEGV)
14f08c3bdfSopenharmony_ci */
15f08c3bdfSopenharmony_ci
16f08c3bdfSopenharmony_ci#include <errno.h>
17f08c3bdfSopenharmony_ci#include <stdlib.h>
18f08c3bdfSopenharmony_ci#include <unistd.h>
19f08c3bdfSopenharmony_ci#include <wait.h>
20f08c3bdfSopenharmony_ci#include <sys/types.h>
21f08c3bdfSopenharmony_ci#include <sys/stat.h>
22f08c3bdfSopenharmony_ci#include "tst_test.h"
23f08c3bdfSopenharmony_ci#include "tst_safe_macros.h"
24f08c3bdfSopenharmony_ci
25f08c3bdfSopenharmony_ci#define TESTFILE	"test_file"
26f08c3bdfSopenharmony_ci
27f08c3bdfSopenharmony_cistatic int fd_ok;
28f08c3bdfSopenharmony_cistatic int fd_ebadf = -1;
29f08c3bdfSopenharmony_cistatic struct stat stat_buf;
30f08c3bdfSopenharmony_ci
31f08c3bdfSopenharmony_cistatic struct tcase {
32f08c3bdfSopenharmony_ci	int *fd;
33f08c3bdfSopenharmony_ci	struct stat *stat_buf;
34f08c3bdfSopenharmony_ci	int exp_err;
35f08c3bdfSopenharmony_ci} tcases[] = {
36f08c3bdfSopenharmony_ci	{&fd_ebadf, &stat_buf, EBADF},
37f08c3bdfSopenharmony_ci	{&fd_ok, NULL, EFAULT},
38f08c3bdfSopenharmony_ci};
39f08c3bdfSopenharmony_ci
40f08c3bdfSopenharmony_cistatic void check_fstat(unsigned int tc_num)
41f08c3bdfSopenharmony_ci{
42f08c3bdfSopenharmony_ci	struct tcase *tc = &tcases[tc_num];
43f08c3bdfSopenharmony_ci
44f08c3bdfSopenharmony_ci	TEST(fstat(*tc->fd, tc->stat_buf));
45f08c3bdfSopenharmony_ci	if (TST_RET == -1) {
46f08c3bdfSopenharmony_ci		if (tc->exp_err == TST_ERR) {
47f08c3bdfSopenharmony_ci			tst_res(TPASS,
48f08c3bdfSopenharmony_ci				 "fstat() fails with expected error %s",
49f08c3bdfSopenharmony_ci				 tst_strerrno(tc->exp_err));
50f08c3bdfSopenharmony_ci		} else {
51f08c3bdfSopenharmony_ci			tst_res(TFAIL | TTERRNO,
52f08c3bdfSopenharmony_ci				 "fstat() did not fail with %s, but with",
53f08c3bdfSopenharmony_ci				 tst_strerrno(tc->exp_err));
54f08c3bdfSopenharmony_ci		}
55f08c3bdfSopenharmony_ci	} else {
56f08c3bdfSopenharmony_ci		tst_res(TFAIL, "fstat() returned %ld, expected -1",
57f08c3bdfSopenharmony_ci			 TST_RET);
58f08c3bdfSopenharmony_ci	}
59f08c3bdfSopenharmony_ci}
60f08c3bdfSopenharmony_ci
61f08c3bdfSopenharmony_cistatic void run(unsigned int tc_num)
62f08c3bdfSopenharmony_ci{
63f08c3bdfSopenharmony_ci	pid_t pid;
64f08c3bdfSopenharmony_ci	int status;
65f08c3bdfSopenharmony_ci
66f08c3bdfSopenharmony_ci	pid = SAFE_FORK();
67f08c3bdfSopenharmony_ci	if (pid == 0) {
68f08c3bdfSopenharmony_ci		check_fstat(tc_num);
69f08c3bdfSopenharmony_ci		return;
70f08c3bdfSopenharmony_ci	}
71f08c3bdfSopenharmony_ci	SAFE_WAITPID(pid, &status, 0);
72f08c3bdfSopenharmony_ci
73f08c3bdfSopenharmony_ci	if (tcases[tc_num].exp_err == EFAULT && WTERMSIG(status) == SIGSEGV) {
74f08c3bdfSopenharmony_ci		tst_res(TPASS, "fstat() failed as expected with SIGSEGV");
75f08c3bdfSopenharmony_ci		return;
76f08c3bdfSopenharmony_ci	}
77f08c3bdfSopenharmony_ci
78f08c3bdfSopenharmony_ci	if (WIFEXITED(status) && WEXITSTATUS(status) == 0)
79f08c3bdfSopenharmony_ci		return;
80f08c3bdfSopenharmony_ci
81f08c3bdfSopenharmony_ci	tst_res(TFAIL, "child %s", tst_strstatus(status));
82f08c3bdfSopenharmony_ci}
83f08c3bdfSopenharmony_ci
84f08c3bdfSopenharmony_cistatic void setup(void)
85f08c3bdfSopenharmony_ci{
86f08c3bdfSopenharmony_ci	fd_ok = SAFE_OPEN(TESTFILE, O_RDWR | O_CREAT, 0644);
87f08c3bdfSopenharmony_ci}
88f08c3bdfSopenharmony_ci
89f08c3bdfSopenharmony_cistatic void cleanup(void)
90f08c3bdfSopenharmony_ci{
91f08c3bdfSopenharmony_ci	if (fd_ok > 0)
92f08c3bdfSopenharmony_ci		SAFE_CLOSE(fd_ok);
93f08c3bdfSopenharmony_ci}
94f08c3bdfSopenharmony_ci
95f08c3bdfSopenharmony_cistatic struct tst_test test = {
96f08c3bdfSopenharmony_ci	.test = run,
97f08c3bdfSopenharmony_ci	.tcnt = ARRAY_SIZE(tcases),
98f08c3bdfSopenharmony_ci	.setup = setup,
99f08c3bdfSopenharmony_ci	.cleanup = cleanup,
100f08c3bdfSopenharmony_ci	.needs_tmpdir = 1,
101f08c3bdfSopenharmony_ci	.forks_child = 1,
102f08c3bdfSopenharmony_ci};
103