1 /* $NetBSD: t_pipe.c,v 1.5 2017/01/13 21:30:41 christos Exp $ */ 2 3 /*- 4 * Copyright (c) 2001, 2008 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 * POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 __COPYRIGHT("@(#) Copyright (c) 2008\ 31 The NetBSD Foundation, inc. All rights reserved."); 32 __RCSID("$NetBSD: t_pipe.c,v 1.5 2017/01/13 21:30:41 christos Exp $"); 33 34 #include <sys/types.h> 35 #include <sys/wait.h> 36 37 #include <errno.h> 38 #include <fcntl.h> 39 #include <poll.h> 40 #include <sched.h> 41 #include <signal.h> 42 #include <stdio.h> 43 #include <stdlib.h> 44 #include <unistd.h> 45 46 #include <atf-c.h> 47 48 #include "h_macros.h" 49 50 static pid_t pid; 51 static int nsiginfo = 0; 52 53 /* 54 * This is used for both parent and child. Handle parent's SIGALRM, 55 * the childs SIGINFO doesn't need anything. 56 */ 57 static void 58 sighand(int sig) 59 { 60 if (sig == SIGALRM) { 61 kill(pid, SIGINFO); 62 } 63 if (sig == SIGINFO) { 64 nsiginfo++; 65 } 66 } 67 68 ATF_TC(pipe_restart); 69 ATF_TC_HEAD(pipe_restart, tc) 70 { 71 atf_tc_set_md_var(tc, "descr", "Checks that writing to pipe " 72 "works correctly after being interrupted and restarted " 73 "(kern/14087)"); 74 } 75 76 ATF_TC_BODY(pipe_restart, tc) 77 { 78 int pp[2], st; 79 ssize_t sz, todo, done; 80 char *f; 81 sigset_t asigset, osigset, emptysigset; 82 83 /* Initialise signal masks */ 84 RL(sigemptyset(&emptysigset)); 85 RL(sigemptyset(&asigset)); 86 RL(sigaddset(&asigset, SIGINFO)); 87 88 /* Register signal handlers for both read and writer */ 89 REQUIRE_LIBC(signal(SIGINFO, sighand), SIG_ERR); 90 REQUIRE_LIBC(signal(SIGALRM, sighand), SIG_ERR); 91 92 todo = 2 * 1024 * 1024; 93 REQUIRE_LIBC(f = malloc(todo), NULL); 94 95 RL(pipe(pp)); 96 97 RL(pid = fork()); 98 if (pid == 0) { 99 /* child */ 100 RL(close(pp[1])); 101 102 /* Do inital write. This should succeed, make 103 * the other side do partial write and wait for us to pick 104 * rest up. 105 */ 106 RL(done = read(pp[0], f, 128 * 1024)); 107 108 /* Wait until parent is alarmed and awakens us */ 109 RL(sigprocmask(SIG_BLOCK, &asigset, &osigset)); 110 while (nsiginfo == 0) { 111 if (sigsuspend(&emptysigset) != -1 || errno != EINTR) 112 atf_tc_fail("sigsuspend(&emptysigset): %s", 113 strerror(errno)); 114 } 115 RL(sigprocmask(SIG_SETMASK, &osigset, NULL)); 116 117 /* Read all what parent wants to give us */ 118 while((sz = read(pp[0], f, 1024 * 1024)) > 0) 119 done += sz; 120 121 /* 122 * Exit with 1 if number of bytes read doesn't match 123 * number of expected bytes 124 */ 125 printf("Read: %#zx\n", (size_t)done); 126 printf("Expected: %#zx\n", (size_t)todo); 127 128 exit(done != todo); 129 130 /* NOTREACHED */ 131 } else { 132 RL(close(pp[0])); 133 134 /* 135 * Arrange for alarm after two seconds. Since we have 136 * handler setup for SIGARLM, the write(2) call should 137 * be restarted internally by kernel. 138 */ 139 (void)alarm(2); 140 141 /* We write exactly 'todo' bytes. The very first write(2) 142 * should partially succeed, block and eventually 143 * be restarted by kernel 144 */ 145 while(todo > 0 && ((sz = write(pp[1], f, todo)) > 0)) 146 todo -= sz; 147 148 /* Close the pipe, so that child would stop reading */ 149 RL(close(pp[1])); 150 151 /* And pickup child's exit status */ 152 RL(waitpid(pid, &st, 0)); 153 154 ATF_REQUIRE_EQ(WEXITSTATUS(st), 0); 155 } 156 free(f); 157 } 158 159 ATF_TP_ADD_TCS(tp) 160 { 161 ATF_TP_ADD_TC(tp, pipe_restart); 162 163 return atf_no_error(); 164 } 165