1*55908713Sjruoho /* $NetBSD: t_pipe.c,v 1.7 2020/06/26 07:50:11 jruoho Exp $ */
2599acddbSjruoho
3599acddbSjruoho /*-
4599acddbSjruoho * Copyright (c) 2001, 2008 The NetBSD Foundation, Inc.
5599acddbSjruoho * All rights reserved.
6599acddbSjruoho *
7599acddbSjruoho * Redistribution and use in source and binary forms, with or without
8599acddbSjruoho * modification, are permitted provided that the following conditions
9599acddbSjruoho * are met:
10599acddbSjruoho * 1. Redistributions of source code must retain the above copyright
11599acddbSjruoho * notice, this list of conditions and the following disclaimer.
12599acddbSjruoho * 2. Redistributions in binary form must reproduce the above copyright
13599acddbSjruoho * notice, this list of conditions and the following disclaimer in the
14599acddbSjruoho * documentation and/or other materials provided with the distribution.
15599acddbSjruoho *
16599acddbSjruoho * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17599acddbSjruoho * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18599acddbSjruoho * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19599acddbSjruoho * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20599acddbSjruoho * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21599acddbSjruoho * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22599acddbSjruoho * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23599acddbSjruoho * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24599acddbSjruoho * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25599acddbSjruoho * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26599acddbSjruoho * POSSIBILITY OF SUCH DAMAGE.
27599acddbSjruoho */
28599acddbSjruoho
29599acddbSjruoho #include <sys/cdefs.h>
30599acddbSjruoho __COPYRIGHT("@(#) Copyright (c) 2008\
31599acddbSjruoho The NetBSD Foundation, inc. All rights reserved.");
32*55908713Sjruoho __RCSID("$NetBSD: t_pipe.c,v 1.7 2020/06/26 07:50:11 jruoho Exp $");
33599acddbSjruoho
34599acddbSjruoho #include <sys/types.h>
35599acddbSjruoho #include <sys/wait.h>
36599acddbSjruoho
37599acddbSjruoho #include <errno.h>
380ab97f9bSnjoly #include <fcntl.h>
39599acddbSjruoho #include <poll.h>
40599acddbSjruoho #include <sched.h>
41599acddbSjruoho #include <signal.h>
42599acddbSjruoho #include <stdio.h>
43599acddbSjruoho #include <stdlib.h>
44599acddbSjruoho #include <unistd.h>
45599acddbSjruoho
46599acddbSjruoho #include <atf-c.h>
47599acddbSjruoho
48c54cb811Schristos #include "h_macros.h"
49599acddbSjruoho
50599acddbSjruoho static pid_t pid;
51599acddbSjruoho static int nsiginfo = 0;
52599acddbSjruoho
53599acddbSjruoho /*
54599acddbSjruoho * This is used for both parent and child. Handle parent's SIGALRM,
55599acddbSjruoho * the childs SIGINFO doesn't need anything.
56599acddbSjruoho */
57599acddbSjruoho static void
sighand(int sig)58599acddbSjruoho sighand(int sig)
59599acddbSjruoho {
60599acddbSjruoho if (sig == SIGALRM) {
61599acddbSjruoho kill(pid, SIGINFO);
62599acddbSjruoho }
63599acddbSjruoho if (sig == SIGINFO) {
64599acddbSjruoho nsiginfo++;
65599acddbSjruoho }
66599acddbSjruoho }
67599acddbSjruoho
68599acddbSjruoho ATF_TC(pipe_restart);
ATF_TC_HEAD(pipe_restart,tc)69599acddbSjruoho ATF_TC_HEAD(pipe_restart, tc)
70599acddbSjruoho {
71599acddbSjruoho atf_tc_set_md_var(tc, "descr", "Checks that writing to pipe "
72599acddbSjruoho "works correctly after being interrupted and restarted "
73*55908713Sjruoho "(PR kern/14087)");
74599acddbSjruoho }
75599acddbSjruoho
ATF_TC_BODY(pipe_restart,tc)76599acddbSjruoho ATF_TC_BODY(pipe_restart, tc)
77599acddbSjruoho {
78599acddbSjruoho int pp[2], st;
79599acddbSjruoho ssize_t sz, todo, done;
80599acddbSjruoho char *f;
81599acddbSjruoho sigset_t asigset, osigset, emptysigset;
82599acddbSjruoho
83599acddbSjruoho /* Initialise signal masks */
84599acddbSjruoho RL(sigemptyset(&emptysigset));
85599acddbSjruoho RL(sigemptyset(&asigset));
86599acddbSjruoho RL(sigaddset(&asigset, SIGINFO));
87599acddbSjruoho
88599acddbSjruoho /* Register signal handlers for both read and writer */
89599acddbSjruoho REQUIRE_LIBC(signal(SIGINFO, sighand), SIG_ERR);
90599acddbSjruoho REQUIRE_LIBC(signal(SIGALRM, sighand), SIG_ERR);
91599acddbSjruoho
92599acddbSjruoho todo = 2 * 1024 * 1024;
93599acddbSjruoho REQUIRE_LIBC(f = malloc(todo), NULL);
94599acddbSjruoho
95599acddbSjruoho RL(pipe(pp));
96599acddbSjruoho
97599acddbSjruoho RL(pid = fork());
98599acddbSjruoho if (pid == 0) {
99599acddbSjruoho /* child */
100599acddbSjruoho RL(close(pp[1]));
101599acddbSjruoho
102a5effc3cSmsaitoh /* Do initial write. This should succeed, make
103599acddbSjruoho * the other side do partial write and wait for us to pick
104599acddbSjruoho * rest up.
105599acddbSjruoho */
106599acddbSjruoho RL(done = read(pp[0], f, 128 * 1024));
107599acddbSjruoho
108599acddbSjruoho /* Wait until parent is alarmed and awakens us */
109599acddbSjruoho RL(sigprocmask(SIG_BLOCK, &asigset, &osigset));
110599acddbSjruoho while (nsiginfo == 0) {
111599acddbSjruoho if (sigsuspend(&emptysigset) != -1 || errno != EINTR)
112599acddbSjruoho atf_tc_fail("sigsuspend(&emptysigset): %s",
113599acddbSjruoho strerror(errno));
114599acddbSjruoho }
115599acddbSjruoho RL(sigprocmask(SIG_SETMASK, &osigset, NULL));
116599acddbSjruoho
117599acddbSjruoho /* Read all what parent wants to give us */
118599acddbSjruoho while((sz = read(pp[0], f, 1024 * 1024)) > 0)
119599acddbSjruoho done += sz;
120599acddbSjruoho
121599acddbSjruoho /*
122599acddbSjruoho * Exit with 1 if number of bytes read doesn't match
123599acddbSjruoho * number of expected bytes
124599acddbSjruoho */
125599acddbSjruoho printf("Read: %#zx\n", (size_t)done);
126599acddbSjruoho printf("Expected: %#zx\n", (size_t)todo);
127599acddbSjruoho
128599acddbSjruoho exit(done != todo);
129599acddbSjruoho
130599acddbSjruoho /* NOTREACHED */
131599acddbSjruoho } else {
132599acddbSjruoho RL(close(pp[0]));
133599acddbSjruoho
134599acddbSjruoho /*
135599acddbSjruoho * Arrange for alarm after two seconds. Since we have
136599acddbSjruoho * handler setup for SIGARLM, the write(2) call should
137599acddbSjruoho * be restarted internally by kernel.
138599acddbSjruoho */
139599acddbSjruoho (void)alarm(2);
140599acddbSjruoho
141599acddbSjruoho /* We write exactly 'todo' bytes. The very first write(2)
142599acddbSjruoho * should partially succeed, block and eventually
143599acddbSjruoho * be restarted by kernel
144599acddbSjruoho */
145599acddbSjruoho while(todo > 0 && ((sz = write(pp[1], f, todo)) > 0))
146599acddbSjruoho todo -= sz;
147599acddbSjruoho
148599acddbSjruoho /* Close the pipe, so that child would stop reading */
149599acddbSjruoho RL(close(pp[1]));
150599acddbSjruoho
151599acddbSjruoho /* And pickup child's exit status */
152599acddbSjruoho RL(waitpid(pid, &st, 0));
153599acddbSjruoho
154599acddbSjruoho ATF_REQUIRE_EQ(WEXITSTATUS(st), 0);
155599acddbSjruoho }
15635c57c2fSchristos free(f);
157599acddbSjruoho }
158599acddbSjruoho
ATF_TP_ADD_TCS(tp)159599acddbSjruoho ATF_TP_ADD_TCS(tp)
160599acddbSjruoho {
161599acddbSjruoho ATF_TP_ADD_TC(tp, pipe_restart);
162599acddbSjruoho
163599acddbSjruoho return atf_no_error();
164599acddbSjruoho }
165