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