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