blob: 865042ab78194ec7e116dff1363a5d6ce1c6c3f5 [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
2 *
3 * Copyright (c) International Business Machines Corp., 2001
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
13 * the GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
Wanlong Gao4548c6c2012-10-19 18:03:36 +080017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
plars865695b2001-08-27 22:15:12 +000018 */
19
20/*
plars865695b2001-08-27 22:15:12 +000021 * Test Description:
22 * Verify that,
Cyril Hrubis435448c2014-05-28 18:10:46 +020023 * 1) pwrite() fails when attempted to write to an unnamed pipe,
24 * returns ESPIPE.
25 * 2) pwrite() fails if the specified offset position was invalid,
26 * returns EINVAL.
27 * 3) pwrite() fails if fd is not a valid file descriptor,
28 * returns EBADF.
Cyril Hrubis0b791f42014-05-28 18:22:46 +020029 * 4) pwrite() fails if fd is not open for writing, return EBADF.
30 * 5) pwrite() fails when attempted to write with buf outside
Cyril Hrubis435448c2014-05-28 18:10:46 +020031 * accessible address space, returns EFAULT.
plars865695b2001-08-27 22:15:12 +000032 */
plars74948ad2002-11-14 16:16:14 +000033
34#define _XOPEN_SOURCE 500
35
plars865695b2001-08-27 22:15:12 +000036#include <errno.h>
37#include <unistd.h>
38#include <fcntl.h>
39
40#include "test.h"
Cyril Hrubis435448c2014-05-28 18:10:46 +020041#include "safe_macros.h"
plars865695b2001-08-27 22:15:12 +000042
43#define TEMPFILE "pwrite_file"
Cyril Hrubis435448c2014-05-28 18:10:46 +020044#define K1 1024
plars865695b2001-08-27 22:15:12 +000045
Cyril Hrubis435448c2014-05-28 18:10:46 +020046TCID_DEFINE(pwrite02);
plars865695b2001-08-27 22:15:12 +000047
Cyril Hrubis435448c2014-05-28 18:10:46 +020048static char write_buf[K1];
plars865695b2001-08-27 22:15:12 +000049
Cyril Hrubis435448c2014-05-28 18:10:46 +020050static void setup(void);
51static void cleanup(void);
plars865695b2001-08-27 22:15:12 +000052
Cyril Hrubis435448c2014-05-28 18:10:46 +020053static void test_espipe(void);
54static void test_einval(void);
Cyril Hrubis0b791f42014-05-28 18:22:46 +020055static void test_ebadf1(void);
56static void test_ebadf2(void);
Cyril Hrubis435448c2014-05-28 18:10:46 +020057
58#if !defined(UCLINUX)
59static void test_efault(void);
60#endif
61
62static void (*testfunc[])(void) = {
Cyril Hrubis0b791f42014-05-28 18:22:46 +020063 test_espipe, test_einval, test_ebadf1, test_ebadf2,
Cyril Hrubis435448c2014-05-28 18:10:46 +020064#if !defined(UCLINUX)
65 test_efault
66#endif
plars865695b2001-08-27 22:15:12 +000067};
68
Cyril Hrubis435448c2014-05-28 18:10:46 +020069int TST_TOTAL = ARRAY_SIZE(testfunc);
70
subrata_modak56207ce2009-03-23 13:35:39 +000071int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000072{
Cyril Hrubis435448c2014-05-28 18:10:46 +020073 int i, lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020074 const char *msg;
subrata_modakbdbaec52009-02-26 12:14:51 +000075
Garrett Cooper7d0a4a52010-12-16 10:05:08 -080076 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -080077 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +000078
plars865695b2001-08-27 22:15:12 +000079 setup();
80
plars865695b2001-08-27 22:15:12 +000081 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +080082 tst_count = 0;
plars865695b2001-08-27 22:15:12 +000083
Cyril Hrubis435448c2014-05-28 18:10:46 +020084 for (i = 0; i < TST_TOTAL; i++)
85 (*testfunc[i])();
Garrett Cooper2c282152010-12-16 00:55:50 -080086 }
plars865695b2001-08-27 22:15:12 +000087
plars865695b2001-08-27 22:15:12 +000088 cleanup();
Garrett Cooper2c282152010-12-16 00:55:50 -080089 tst_exit();
90}
plars865695b2001-08-27 22:15:12 +000091
92/*
93 * sighandler - handle SIGXFSZ
94 *
95 * This is here to start looking at a failure in test case #2. This
96 * test case passes on a machine running RedHat 6.2 but it will fail
97 * on a machine running RedHat 7.1.
98 */
Cyril Hrubis435448c2014-05-28 18:10:46 +020099static void sighandler(int sig)
plars865695b2001-08-27 22:15:12 +0000100{
Cyril Hrubis435448c2014-05-28 18:10:46 +0200101 int ret;
102
plars865695b2001-08-27 22:15:12 +0000103 if (sig != SIGXFSZ) {
Cyril Hrubis435448c2014-05-28 18:10:46 +0200104 ret = write(STDOUT_FILENO, "get wrong signal\n",
105 sizeof("get wrong signal\n"));
plars865695b2001-08-27 22:15:12 +0000106 } else {
Cyril Hrubis435448c2014-05-28 18:10:46 +0200107 ret = write(STDOUT_FILENO, "caught SIGXFSZ\n",
108 sizeof("caught SIGXFSZ\n"));
plars865695b2001-08-27 22:15:12 +0000109 }
Cyril Hrubis435448c2014-05-28 18:10:46 +0200110
111 (void)ret;
plars865695b2001-08-27 22:15:12 +0000112}
113
Cyril Hrubis435448c2014-05-28 18:10:46 +0200114static void setup(void)
plars865695b2001-08-27 22:15:12 +0000115{
plars865695b2001-08-27 22:15:12 +0000116 tst_sig(NOFORK, DEF_HANDLER, cleanup);
117
118 /* see the comment in the sighandler() function */
119 /* call signal() to trap the signal generated */
Cyril Hrubis435448c2014-05-28 18:10:46 +0200120 if (signal(SIGXFSZ, sighandler) == SIG_ERR)
plars865695b2001-08-27 22:15:12 +0000121 tst_brkm(TBROK, cleanup, "signal() failed");
Cyril Hrubis435448c2014-05-28 18:10:46 +0200122
plars865695b2001-08-27 22:15:12 +0000123 TEST_PAUSE;
124
plars865695b2001-08-27 22:15:12 +0000125 tst_tmpdir();
126
Cyril Hrubis435448c2014-05-28 18:10:46 +0200127 memset(write_buf, 'a', K1);
plars865695b2001-08-27 22:15:12 +0000128}
129
Cyril Hrubis435448c2014-05-28 18:10:46 +0200130static void print_test_result(int err, int exp_errno)
plars865695b2001-08-27 22:15:12 +0000131{
Cyril Hrubis435448c2014-05-28 18:10:46 +0200132 if (err == 0) {
133 tst_resm(TFAIL, "call succeeded unexpectedly");
134 return;
135 }
plars865695b2001-08-27 22:15:12 +0000136
Cyril Hrubis435448c2014-05-28 18:10:46 +0200137 if (err == exp_errno) {
138 tst_resm(TPASS, "pwrite failed as expected: %d - %s",
139 err, strerror(err));
140 } else {
141 tst_resm(TFAIL, "pwrite failed unexpectedly; expected: %d - %s"
142 "return: %d - %s", exp_errno, strerror(exp_errno),
143 err, strerror(err));
plars865695b2001-08-27 22:15:12 +0000144 }
145}
146
Cyril Hrubis435448c2014-05-28 18:10:46 +0200147static void test_espipe(void)
plars865695b2001-08-27 22:15:12 +0000148{
Cyril Hrubis435448c2014-05-28 18:10:46 +0200149 int pipe_fds[2];
plars865695b2001-08-27 22:15:12 +0000150
Cyril Hrubis435448c2014-05-28 18:10:46 +0200151 SAFE_PIPE(cleanup, pipe_fds);
152
153 TEST(pwrite(pipe_fds[1], write_buf, K1, 0));
154
155 print_test_result(errno, ESPIPE);
156
157 SAFE_CLOSE(cleanup, pipe_fds[0]);
158 SAFE_CLOSE(cleanup, pipe_fds[1]);
159}
160
161static void test_einval(void)
162{
163 int fd;
164
165 fd = SAFE_OPEN(cleanup, TEMPFILE, O_RDWR | O_CREAT, 0666);
166
167 /* the specified offset was invalid */
168 TEST(pwrite(fd, write_buf, K1, -1));
169
170 print_test_result(errno, EINVAL);
171
172 SAFE_CLOSE(cleanup, fd);
173}
174
Cyril Hrubis0b791f42014-05-28 18:22:46 +0200175static void test_ebadf1(void)
Cyril Hrubis435448c2014-05-28 18:10:46 +0200176{
177 int fd = -1;
178
179 TEST(pwrite(fd, write_buf, K1, 0));
180
181 print_test_result(errno, EBADF);
182}
183
Cyril Hrubis0b791f42014-05-28 18:22:46 +0200184static void test_ebadf2(void)
185{
186 int fd;
187
188 fd = SAFE_OPEN(cleanup, TEMPFILE, O_RDONLY | O_CREAT, 0666);
189
190 TEST(pwrite(fd, write_buf, K1, 0));
191
192 print_test_result(errno, EBADF);
193
194 SAFE_CLOSE(cleanup, fd);
195}
196
Cyril Hrubis435448c2014-05-28 18:10:46 +0200197#if !defined(UCLINUX)
198static void test_efault(void)
199{
200 int fd;
201 char *buf = sbrk(0);
202
203 fd = SAFE_OPEN(cleanup, TEMPFILE, O_RDWR | O_CREAT, 0666);
204
205 TEST(pwrite(fd, buf, K1, 0));
206
207 print_test_result(errno, EFAULT);
208
209 SAFE_CLOSE(cleanup, fd);
210}
211#endif
212
213static void cleanup(void)
214{
plars865695b2001-08-27 22:15:12 +0000215 tst_rmdir();
Chris Dearmanec6edca2012-10-17 19:54:01 -0700216}