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