xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/write/write05.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  * 07/2001 Ported by John George
5*49cdfc7eSAndroid Build Coastguard Worker  * 04/2002 wjhuie sigset cleanups
6*49cdfc7eSAndroid Build Coastguard Worker  * 08/2007 Ricardo Salveti de Araujo <[email protected]>
7*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) Linux Test Project, 2002-2023
8*49cdfc7eSAndroid Build Coastguard Worker  */
9*49cdfc7eSAndroid Build Coastguard Worker 
10*49cdfc7eSAndroid Build Coastguard Worker /*\
11*49cdfc7eSAndroid Build Coastguard Worker  * [Description]
12*49cdfc7eSAndroid Build Coastguard Worker  *
13*49cdfc7eSAndroid Build Coastguard Worker  *	Check the return value, and errnos of write(2)
14*49cdfc7eSAndroid Build Coastguard Worker  *
15*49cdfc7eSAndroid Build Coastguard Worker  *	- when the file descriptor is invalid - EBADF
16*49cdfc7eSAndroid Build Coastguard Worker  *	- when the buf parameter is invalid - EFAULT
17*49cdfc7eSAndroid Build Coastguard Worker  *	- on an attempt to write to a pipe that is not open for reading - EPIPE
18*49cdfc7eSAndroid Build Coastguard Worker  */
19*49cdfc7eSAndroid Build Coastguard Worker 
20*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
21*49cdfc7eSAndroid Build Coastguard Worker #include <sys/stat.h>
22*49cdfc7eSAndroid Build Coastguard Worker #include <fcntl.h>
23*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
24*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
25*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
26*49cdfc7eSAndroid Build Coastguard Worker #include <sys/wait.h>
27*49cdfc7eSAndroid Build Coastguard Worker #include <sys/mman.h>
28*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
29*49cdfc7eSAndroid Build Coastguard Worker 
30*49cdfc7eSAndroid Build Coastguard Worker static int fd;
31*49cdfc7eSAndroid Build Coastguard Worker static int inv_fd = -1;
32*49cdfc7eSAndroid Build Coastguard Worker static char b[32];
33*49cdfc7eSAndroid Build Coastguard Worker static char *buf = b;
34*49cdfc7eSAndroid Build Coastguard Worker static char *bad_addr;
35*49cdfc7eSAndroid Build Coastguard Worker static int pipefd[2];
36*49cdfc7eSAndroid Build Coastguard Worker 
37*49cdfc7eSAndroid Build Coastguard Worker static struct tcase {
38*49cdfc7eSAndroid Build Coastguard Worker 	int *fd;
39*49cdfc7eSAndroid Build Coastguard Worker 	char **buf;
40*49cdfc7eSAndroid Build Coastguard Worker 	size_t size;
41*49cdfc7eSAndroid Build Coastguard Worker 	int exp_errno;
42*49cdfc7eSAndroid Build Coastguard Worker } tcases[] = {
43*49cdfc7eSAndroid Build Coastguard Worker 	{&inv_fd, &buf, sizeof(buf), EBADF},
44*49cdfc7eSAndroid Build Coastguard Worker 	{&fd, &bad_addr, sizeof(buf), EFAULT},
45*49cdfc7eSAndroid Build Coastguard Worker 	{&pipefd[1], &buf, sizeof(buf), EPIPE},
46*49cdfc7eSAndroid Build Coastguard Worker };
47*49cdfc7eSAndroid Build Coastguard Worker 
48*49cdfc7eSAndroid Build Coastguard Worker static volatile int sigpipe_cnt;
49*49cdfc7eSAndroid Build Coastguard Worker 
sighandler(int sig)50*49cdfc7eSAndroid Build Coastguard Worker static void sighandler(int sig)
51*49cdfc7eSAndroid Build Coastguard Worker {
52*49cdfc7eSAndroid Build Coastguard Worker 	if (sig == SIGPIPE)
53*49cdfc7eSAndroid Build Coastguard Worker 		sigpipe_cnt++;
54*49cdfc7eSAndroid Build Coastguard Worker }
55*49cdfc7eSAndroid Build Coastguard Worker 
verify_write(unsigned int i)56*49cdfc7eSAndroid Build Coastguard Worker static void verify_write(unsigned int i)
57*49cdfc7eSAndroid Build Coastguard Worker {
58*49cdfc7eSAndroid Build Coastguard Worker 	struct tcase *tc = &tcases[i];
59*49cdfc7eSAndroid Build Coastguard Worker 
60*49cdfc7eSAndroid Build Coastguard Worker 	sigpipe_cnt = 0;
61*49cdfc7eSAndroid Build Coastguard Worker 
62*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_FAIL2(write(*tc->fd, *tc->buf, tc->size), tc->exp_errno);
63*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_RET != -1)
64*49cdfc7eSAndroid Build Coastguard Worker 		return;
65*49cdfc7eSAndroid Build Coastguard Worker 
66*49cdfc7eSAndroid Build Coastguard Worker 	if (tc->exp_errno == EPIPE && sigpipe_cnt != 1)
67*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "sigpipe_cnt = %i", sigpipe_cnt);
68*49cdfc7eSAndroid Build Coastguard Worker }
69*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)70*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
71*49cdfc7eSAndroid Build Coastguard Worker {
72*49cdfc7eSAndroid Build Coastguard Worker 	fd = SAFE_OPEN("write_test", O_RDWR | O_CREAT, 0644);
73*49cdfc7eSAndroid Build Coastguard Worker 
74*49cdfc7eSAndroid Build Coastguard Worker 	bad_addr = SAFE_MMAP(0, 1, PROT_NONE,
75*49cdfc7eSAndroid Build Coastguard Worker 			MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
76*49cdfc7eSAndroid Build Coastguard Worker 
77*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_PIPE(pipefd);
78*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CLOSE(pipefd[0]);
79*49cdfc7eSAndroid Build Coastguard Worker 
80*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_SIGNAL(SIGPIPE, sighandler);
81*49cdfc7eSAndroid Build Coastguard Worker }
82*49cdfc7eSAndroid Build Coastguard Worker 
cleanup(void)83*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
84*49cdfc7eSAndroid Build Coastguard Worker {
85*49cdfc7eSAndroid Build Coastguard Worker 	if (fd > 0)
86*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CLOSE(fd);
87*49cdfc7eSAndroid Build Coastguard Worker 
88*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_MUNMAP(bad_addr, 1);
89*49cdfc7eSAndroid Build Coastguard Worker }
90*49cdfc7eSAndroid Build Coastguard Worker 
91*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
92*49cdfc7eSAndroid Build Coastguard Worker 	.needs_tmpdir = 1,
93*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
94*49cdfc7eSAndroid Build Coastguard Worker 	.cleanup = cleanup,
95*49cdfc7eSAndroid Build Coastguard Worker 	.test = verify_write,
96*49cdfc7eSAndroid Build Coastguard Worker 	.tcnt = ARRAY_SIZE(tcases),
97*49cdfc7eSAndroid Build Coastguard Worker };
98