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 * [Description]
9f08c3bdfSopenharmony_ci *
10f08c3bdfSopenharmony_ci * Tests basic error handling of the pread syscall.
11f08c3bdfSopenharmony_ci *
12f08c3bdfSopenharmony_ci * - ESPIPE when attempted to read from an unnamed pipe
13f08c3bdfSopenharmony_ci * - EINVAL if the specified offset position was invalid
14f08c3bdfSopenharmony_ci * - EISDIR when fd refers to a directory
15f08c3bdfSopenharmony_ci */
16f08c3bdfSopenharmony_ci
17f08c3bdfSopenharmony_ci#include <fcntl.h>
18f08c3bdfSopenharmony_ci#include <stdlib.h>
19f08c3bdfSopenharmony_ci#include "tst_test.h"
20f08c3bdfSopenharmony_ci
21f08c3bdfSopenharmony_ci#define PREAD_TEMPFILE  "pread_file"
22f08c3bdfSopenharmony_ci#define PREAD_TEMPDIR	"pread_dir"
23f08c3bdfSopenharmony_ci#define K1              1024
24f08c3bdfSopenharmony_ci
25f08c3bdfSopenharmony_cistatic int pipe_fd[2], fd, dir_fd;
26f08c3bdfSopenharmony_ci
27f08c3bdfSopenharmony_cistruct test_case_t {
28f08c3bdfSopenharmony_ci	int *fd;
29f08c3bdfSopenharmony_ci	size_t nb;
30f08c3bdfSopenharmony_ci	off_t offst;
31f08c3bdfSopenharmony_ci	char *desc;
32f08c3bdfSopenharmony_ci	int exp_errno;
33f08c3bdfSopenharmony_ci} tcases[] = {
34f08c3bdfSopenharmony_ci	{&pipe_fd[0], K1, 0, "file descriptor is a PIPE or FIFO", ESPIPE},
35f08c3bdfSopenharmony_ci	{&fd, K1, -1, "specified offset is negative", EINVAL},
36f08c3bdfSopenharmony_ci	{&dir_fd, K1, 0, "file descriptor is a directory", EISDIR}
37f08c3bdfSopenharmony_ci};
38f08c3bdfSopenharmony_ci
39f08c3bdfSopenharmony_cistatic void verify_pread(unsigned int n)
40f08c3bdfSopenharmony_ci{
41f08c3bdfSopenharmony_ci	struct test_case_t *tc = &tcases[n];
42f08c3bdfSopenharmony_ci	char buf[K1];
43f08c3bdfSopenharmony_ci
44f08c3bdfSopenharmony_ci	TST_EXP_FAIL2(pread(*tc->fd, &buf, tc->nb, tc->offst), tc->exp_errno,
45f08c3bdfSopenharmony_ci		"pread(%d, %zu, %lld) %s", *tc->fd, tc->nb, (long long)tc->offst, tc->desc);
46f08c3bdfSopenharmony_ci}
47f08c3bdfSopenharmony_ci
48f08c3bdfSopenharmony_cistatic void setup(void)
49f08c3bdfSopenharmony_ci{
50f08c3bdfSopenharmony_ci	SAFE_PIPE(pipe_fd);
51f08c3bdfSopenharmony_ci	SAFE_WRITE(SAFE_WRITE_ALL, pipe_fd[1], "x", 1);
52f08c3bdfSopenharmony_ci
53f08c3bdfSopenharmony_ci	fd = SAFE_OPEN(PREAD_TEMPFILE, O_RDWR | O_CREAT, 0666);
54f08c3bdfSopenharmony_ci
55f08c3bdfSopenharmony_ci	SAFE_MKDIR(PREAD_TEMPDIR, 0777);
56f08c3bdfSopenharmony_ci	dir_fd = SAFE_OPEN(PREAD_TEMPDIR, O_RDONLY);
57f08c3bdfSopenharmony_ci}
58f08c3bdfSopenharmony_ci
59f08c3bdfSopenharmony_cistatic void cleanup(void)
60f08c3bdfSopenharmony_ci{
61f08c3bdfSopenharmony_ci	int i;
62f08c3bdfSopenharmony_ci
63f08c3bdfSopenharmony_ci	for (i = 0; i < 2; i++) {
64f08c3bdfSopenharmony_ci		if (pipe_fd[i] > 0)
65f08c3bdfSopenharmony_ci			SAFE_CLOSE(pipe_fd[i]);
66f08c3bdfSopenharmony_ci	}
67f08c3bdfSopenharmony_ci
68f08c3bdfSopenharmony_ci	if (fd > 0)
69f08c3bdfSopenharmony_ci		SAFE_CLOSE(fd);
70f08c3bdfSopenharmony_ci	if (dir_fd > 0)
71f08c3bdfSopenharmony_ci		SAFE_CLOSE(dir_fd);
72f08c3bdfSopenharmony_ci}
73f08c3bdfSopenharmony_ci
74f08c3bdfSopenharmony_cistatic struct tst_test test = {
75f08c3bdfSopenharmony_ci	.tcnt = ARRAY_SIZE(tcases),
76f08c3bdfSopenharmony_ci	.needs_tmpdir = 1,
77f08c3bdfSopenharmony_ci	.setup = setup,
78f08c3bdfSopenharmony_ci	.cleanup = cleanup,
79f08c3bdfSopenharmony_ci	.test = verify_pread,
80f08c3bdfSopenharmony_ci};
81