1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci *   Copyright (c) Wayne Boyer, International Business Machines  Corp., 2001
4f08c3bdfSopenharmony_ci *   Copyright (c) 2019 SUSE LLC <mdoucha@suse.cz>
5f08c3bdfSopenharmony_ci */
6f08c3bdfSopenharmony_ci
7f08c3bdfSopenharmony_ci/*
8f08c3bdfSopenharmony_ci * Test Description:
9f08c3bdfSopenharmony_ci *  Testcase to check that fsync(2) sets errno correctly.
10f08c3bdfSopenharmony_ci *  1. Call fsync() on a pipe(fd), and expect EINVAL.
11f08c3bdfSopenharmony_ci *  2. Call fsync() on a socket(fd), and expect EINVAL.
12f08c3bdfSopenharmony_ci *  3. Call fsync() on a closed fd, and test for EBADF.
13f08c3bdfSopenharmony_ci *  4. Call fsync() on an invalid fd, and test for EBADF.
14f08c3bdfSopenharmony_ci *  5. Call fsync() on a fifo(fd), and expect EINVAL.
15f08c3bdfSopenharmony_ci */
16f08c3bdfSopenharmony_ci
17f08c3bdfSopenharmony_ci#include <unistd.h>
18f08c3bdfSopenharmony_ci#include <errno.h>
19f08c3bdfSopenharmony_ci#include "tst_test.h"
20f08c3bdfSopenharmony_ci
21f08c3bdfSopenharmony_ci#define FIFO_PATH "fifo"
22f08c3bdfSopenharmony_ci
23f08c3bdfSopenharmony_cistatic int fifo_rfd, fifo_wfd;
24f08c3bdfSopenharmony_cistatic int pipe_fd[2];
25f08c3bdfSopenharmony_cistatic int sock_fd, bad_fd = -1;
26f08c3bdfSopenharmony_ci
27f08c3bdfSopenharmony_cistatic const struct test_case {
28f08c3bdfSopenharmony_ci	int *fd;
29f08c3bdfSopenharmony_ci	int error;
30f08c3bdfSopenharmony_ci} testcase_list[] = {
31f08c3bdfSopenharmony_ci	/* EINVAL - fsync() on pipe should not succeed. */
32f08c3bdfSopenharmony_ci	{&pipe_fd[1], EINVAL},
33f08c3bdfSopenharmony_ci	/* EINVAL - fsync() on socket should not succeed. */
34f08c3bdfSopenharmony_ci	{&sock_fd, EINVAL},
35f08c3bdfSopenharmony_ci	/* EBADF - fd is closed */
36f08c3bdfSopenharmony_ci	{&pipe_fd[0], EBADF},
37f08c3bdfSopenharmony_ci	/* EBADF - fd is invalid (-1) */
38f08c3bdfSopenharmony_ci	{&bad_fd, EBADF},
39f08c3bdfSopenharmony_ci	/* EINVAL - fsync() on fifo should not succeed. */
40f08c3bdfSopenharmony_ci	{&fifo_wfd, EINVAL},
41f08c3bdfSopenharmony_ci};
42f08c3bdfSopenharmony_ci
43f08c3bdfSopenharmony_cistatic void setup(void)
44f08c3bdfSopenharmony_ci{
45f08c3bdfSopenharmony_ci	SAFE_MKFIFO(FIFO_PATH, 0644);
46f08c3bdfSopenharmony_ci	SAFE_PIPE(pipe_fd);
47f08c3bdfSopenharmony_ci
48f08c3bdfSopenharmony_ci	// FIFO must be opened for reading first, otherwise
49f08c3bdfSopenharmony_ci	// open(fifo, O_WRONLY) will block.
50f08c3bdfSopenharmony_ci	fifo_rfd = SAFE_OPEN(FIFO_PATH, O_RDONLY | O_NONBLOCK);
51f08c3bdfSopenharmony_ci	fifo_wfd = SAFE_OPEN(FIFO_PATH, O_WRONLY);
52f08c3bdfSopenharmony_ci	sock_fd = SAFE_SOCKET(AF_UNIX, SOCK_STREAM, 0);
53f08c3bdfSopenharmony_ci
54f08c3bdfSopenharmony_ci	// Do not open any file descriptors after this line unless you close
55f08c3bdfSopenharmony_ci	// them before the next test run.
56f08c3bdfSopenharmony_ci	SAFE_CLOSE(pipe_fd[0]);
57f08c3bdfSopenharmony_ci}
58f08c3bdfSopenharmony_ci
59f08c3bdfSopenharmony_cistatic void test_fsync(unsigned int n)
60f08c3bdfSopenharmony_ci{
61f08c3bdfSopenharmony_ci	const struct test_case *tc = testcase_list + n;
62f08c3bdfSopenharmony_ci
63f08c3bdfSopenharmony_ci	TEST(fsync(*tc->fd));
64f08c3bdfSopenharmony_ci
65f08c3bdfSopenharmony_ci	if (TST_RET != -1) {
66f08c3bdfSopenharmony_ci		tst_res(TFAIL, "fsync() returned unexpected value %ld",
67f08c3bdfSopenharmony_ci			TST_RET);
68f08c3bdfSopenharmony_ci	} else if (TST_ERR != tc->error) {
69f08c3bdfSopenharmony_ci		tst_res(TFAIL | TTERRNO, "fsync(): unexpected error");
70f08c3bdfSopenharmony_ci	} else {
71f08c3bdfSopenharmony_ci		tst_res(TPASS | TTERRNO, "fsync() failed as expected");
72f08c3bdfSopenharmony_ci	}
73f08c3bdfSopenharmony_ci}
74f08c3bdfSopenharmony_ci
75f08c3bdfSopenharmony_cistatic void cleanup(void)
76f08c3bdfSopenharmony_ci{
77f08c3bdfSopenharmony_ci	SAFE_CLOSE(fifo_wfd);
78f08c3bdfSopenharmony_ci	SAFE_CLOSE(fifo_rfd);
79f08c3bdfSopenharmony_ci	SAFE_CLOSE(pipe_fd[1]);
80f08c3bdfSopenharmony_ci	SAFE_CLOSE(sock_fd);
81f08c3bdfSopenharmony_ci}
82f08c3bdfSopenharmony_ci
83f08c3bdfSopenharmony_cistatic struct tst_test test = {
84f08c3bdfSopenharmony_ci	.test = test_fsync,
85f08c3bdfSopenharmony_ci	.tcnt = ARRAY_SIZE(testcase_list),
86f08c3bdfSopenharmony_ci	.needs_tmpdir = 1,
87f08c3bdfSopenharmony_ci	.setup = setup,
88f08c3bdfSopenharmony_ci	.cleanup = cleanup
89f08c3bdfSopenharmony_ci};
90