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