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