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 * Test basic error handling of the pwrite syscall.
10f08c3bdfSopenharmony_ci *
11f08c3bdfSopenharmony_ci * - ESPIPE when attempted to write to an unnamed pipe
12f08c3bdfSopenharmony_ci * - EINVAL the specified offset position was invalid
13f08c3bdfSopenharmony_ci * - EBADF fd is not a valid file descriptor
14f08c3bdfSopenharmony_ci * - EBADF fd is not open for writing
15f08c3bdfSopenharmony_ci * - EFAULT when attempted to write with buf outside accessible address space
16f08c3bdfSopenharmony_ci */
17f08c3bdfSopenharmony_ci
18f08c3bdfSopenharmony_ci#include <errno.h>
19f08c3bdfSopenharmony_ci#include <unistd.h>
20f08c3bdfSopenharmony_ci#include <string.h>
21f08c3bdfSopenharmony_ci
22f08c3bdfSopenharmony_ci#include "tst_test.h"
23f08c3bdfSopenharmony_ci
24f08c3bdfSopenharmony_ci#define TEMPFILE "pwrite_file"
25f08c3bdfSopenharmony_ci#define BS 1024
26f08c3bdfSopenharmony_ci
27f08c3bdfSopenharmony_cistatic int fd;
28f08c3bdfSopenharmony_cistatic int fd_ro;
29f08c3bdfSopenharmony_cistatic int inv_fd = -1;
30f08c3bdfSopenharmony_cistatic int pipe_fds[2];
31f08c3bdfSopenharmony_cistatic char buf[BS];
32f08c3bdfSopenharmony_ci
33f08c3bdfSopenharmony_cistatic struct tcase {
34f08c3bdfSopenharmony_ci	void *buf;
35f08c3bdfSopenharmony_ci	size_t size;
36f08c3bdfSopenharmony_ci	int *fd;
37f08c3bdfSopenharmony_ci	off_t off;
38f08c3bdfSopenharmony_ci	int exp_errno;
39f08c3bdfSopenharmony_ci} tcases[] = {
40f08c3bdfSopenharmony_ci	{buf, sizeof(buf), &pipe_fds[1], 0, ESPIPE},
41f08c3bdfSopenharmony_ci	{buf, sizeof(buf), &fd, -1, EINVAL},
42f08c3bdfSopenharmony_ci	{buf, sizeof(buf), &inv_fd, 0, EBADF},
43f08c3bdfSopenharmony_ci	{buf, sizeof(buf), &fd_ro, 0, EBADF},
44f08c3bdfSopenharmony_ci	{NULL, sizeof(buf), &fd, 0, EFAULT},
45f08c3bdfSopenharmony_ci};
46f08c3bdfSopenharmony_ci
47f08c3bdfSopenharmony_ci/*
48f08c3bdfSopenharmony_ci * sighandler - handle SIGXFSZ
49f08c3bdfSopenharmony_ci *
50f08c3bdfSopenharmony_ci * This is here to start looking at a failure in test case #2.  This
51f08c3bdfSopenharmony_ci * test case passes on a machine running RedHat 6.2 but it will fail
52f08c3bdfSopenharmony_ci * on a machine running RedHat 7.1.
53f08c3bdfSopenharmony_ci */
54f08c3bdfSopenharmony_cistatic void sighandler(int sig)
55f08c3bdfSopenharmony_ci{
56f08c3bdfSopenharmony_ci	int ret;
57f08c3bdfSopenharmony_ci
58f08c3bdfSopenharmony_ci	if (sig != SIGXFSZ) {
59f08c3bdfSopenharmony_ci		ret = write(STDOUT_FILENO, "get wrong signal\n",
60f08c3bdfSopenharmony_ci			sizeof("get wrong signal\n"));
61f08c3bdfSopenharmony_ci	} else {
62f08c3bdfSopenharmony_ci		ret = write(STDOUT_FILENO, "caught SIGXFSZ\n",
63f08c3bdfSopenharmony_ci			sizeof("caught SIGXFSZ\n"));
64f08c3bdfSopenharmony_ci	}
65f08c3bdfSopenharmony_ci
66f08c3bdfSopenharmony_ci	(void)ret;
67f08c3bdfSopenharmony_ci}
68f08c3bdfSopenharmony_ci
69f08c3bdfSopenharmony_cistatic void verify_pwrite(unsigned int i)
70f08c3bdfSopenharmony_ci{
71f08c3bdfSopenharmony_ci	struct tcase *tc = &tcases[i];
72f08c3bdfSopenharmony_ci
73f08c3bdfSopenharmony_ci	TST_EXP_FAIL2(pwrite(*tc->fd, tc->buf, BS, tc->off), tc->exp_errno,
74f08c3bdfSopenharmony_ci		"pwrite(%d, %d, %ld)", *tc->fd, BS, tc->off);
75f08c3bdfSopenharmony_ci}
76f08c3bdfSopenharmony_ci
77f08c3bdfSopenharmony_cistatic void setup(void)
78f08c3bdfSopenharmony_ci{
79f08c3bdfSopenharmony_ci	SAFE_SIGNAL(SIGXFSZ, sighandler);
80f08c3bdfSopenharmony_ci
81f08c3bdfSopenharmony_ci	SAFE_PIPE(pipe_fds);
82f08c3bdfSopenharmony_ci
83f08c3bdfSopenharmony_ci	fd = SAFE_OPEN(TEMPFILE, O_RDWR | O_CREAT, 0666);
84f08c3bdfSopenharmony_ci	fd_ro = SAFE_OPEN(TEMPFILE, O_RDONLY | O_CREAT, 0666);
85f08c3bdfSopenharmony_ci}
86f08c3bdfSopenharmony_ci
87f08c3bdfSopenharmony_cistatic void cleanup(void)
88f08c3bdfSopenharmony_ci{
89f08c3bdfSopenharmony_ci	if (fd > 0)
90f08c3bdfSopenharmony_ci		SAFE_CLOSE(fd);
91f08c3bdfSopenharmony_ci
92f08c3bdfSopenharmony_ci	if (fd_ro > 0)
93f08c3bdfSopenharmony_ci		SAFE_CLOSE(fd_ro);
94f08c3bdfSopenharmony_ci
95f08c3bdfSopenharmony_ci	if (pipe_fds[0] > 0)
96f08c3bdfSopenharmony_ci		SAFE_CLOSE(pipe_fds[0]);
97f08c3bdfSopenharmony_ci
98f08c3bdfSopenharmony_ci	if (pipe_fds[1] > 0)
99f08c3bdfSopenharmony_ci		SAFE_CLOSE(pipe_fds[1]);
100f08c3bdfSopenharmony_ci}
101f08c3bdfSopenharmony_ci
102f08c3bdfSopenharmony_cistatic struct tst_test test = {
103f08c3bdfSopenharmony_ci	.needs_tmpdir = 1,
104f08c3bdfSopenharmony_ci	.setup = setup,
105f08c3bdfSopenharmony_ci	.cleanup = cleanup,
106f08c3bdfSopenharmony_ci	.test = verify_pwrite,
107f08c3bdfSopenharmony_ci	.tcnt = ARRAY_SIZE(tcases),
108f08c3bdfSopenharmony_ci};
109