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