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