t_fdrestart.c revision 1.2 1 1.2 riastrad /* $NetBSD: t_fdrestart.c,v 1.2 2023/10/15 14:30:51 riastradh Exp $ */
2 1.1 riastrad
3 1.1 riastrad /*-
4 1.1 riastrad * Copyright (c) 2023 The NetBSD Foundation, Inc.
5 1.1 riastrad * All rights reserved.
6 1.1 riastrad *
7 1.1 riastrad * Redistribution and use in source and binary forms, with or without
8 1.1 riastrad * modification, are permitted provided that the following conditions
9 1.1 riastrad * are met:
10 1.1 riastrad * 1. Redistributions of source code must retain the above copyright
11 1.1 riastrad * notice, this list of conditions and the following disclaimer.
12 1.1 riastrad * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 riastrad * notice, this list of conditions and the following disclaimer in the
14 1.1 riastrad * documentation and/or other materials provided with the distribution.
15 1.1 riastrad *
16 1.1 riastrad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 1.1 riastrad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.1 riastrad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.1 riastrad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 1.1 riastrad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 1.1 riastrad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 1.1 riastrad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.1 riastrad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 1.1 riastrad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 1.1 riastrad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.1 riastrad * POSSIBILITY OF SUCH DAMAGE.
27 1.1 riastrad */
28 1.1 riastrad
29 1.1 riastrad #define _KMEMUSER /* ERESTART */
30 1.1 riastrad
31 1.1 riastrad #include <sys/cdefs.h>
32 1.2 riastrad __RCSID("$NetBSD: t_fdrestart.c,v 1.2 2023/10/15 14:30:51 riastradh Exp $");
33 1.1 riastrad
34 1.1 riastrad #include <sys/socket.h>
35 1.1 riastrad #include <sys/un.h>
36 1.1 riastrad
37 1.1 riastrad #include <atf-c.h>
38 1.1 riastrad #include <errno.h>
39 1.1 riastrad #include <pthread.h>
40 1.1 riastrad #include <unistd.h>
41 1.1 riastrad
42 1.1 riastrad #include <rump/rump.h>
43 1.1 riastrad #include <rump/rump_syscalls.h>
44 1.1 riastrad
45 1.1 riastrad #include "h_macros.h"
46 1.1 riastrad
47 1.1 riastrad struct fdrestart {
48 1.1 riastrad void (*op)(struct fdrestart *);
49 1.1 riastrad int fd;
50 1.1 riastrad pthread_barrier_t barrier;
51 1.1 riastrad };
52 1.1 riastrad
53 1.1 riastrad static void
54 1.1 riastrad doread(struct fdrestart *F)
55 1.1 riastrad {
56 1.1 riastrad char c;
57 1.1 riastrad ssize_t nread;
58 1.1 riastrad int error;
59 1.1 riastrad
60 1.1 riastrad nread = rump_sys_read(F->fd, &c, sizeof(c));
61 1.1 riastrad ATF_REQUIRE_EQ_MSG(nread, -1, "nread=%zd", nread);
62 1.1 riastrad error = errno;
63 1.1 riastrad ATF_REQUIRE_EQ_MSG(error, ERESTART, "errno=%d (%s)", error,
64 1.1 riastrad strerror(error));
65 1.1 riastrad
66 1.1 riastrad nread = rump_sys_read(F->fd, &c, sizeof(c));
67 1.1 riastrad ATF_REQUIRE_EQ_MSG(nread, -1, "nread=%zd", nread);
68 1.1 riastrad error = errno;
69 1.1 riastrad ATF_REQUIRE_EQ_MSG(error, EBADF, "errno=%d (%s)", error,
70 1.1 riastrad strerror(error));
71 1.1 riastrad }
72 1.1 riastrad
73 1.1 riastrad static void
74 1.1 riastrad dowrite(struct fdrestart *F)
75 1.1 riastrad {
76 1.1 riastrad static const char buf[1024*1024]; /* XXX >BIG_PIPE_SIZE */
77 1.1 riastrad ssize_t nwrit;
78 1.1 riastrad int error;
79 1.1 riastrad
80 1.1 riastrad nwrit = rump_sys_write(F->fd, buf, sizeof(buf));
81 1.1 riastrad if (nwrit != -1) /* filled buffer, try again */
82 1.1 riastrad nwrit = rump_sys_write(F->fd, buf, sizeof(buf));
83 1.1 riastrad ATF_REQUIRE_EQ_MSG(nwrit, -1, "nwrit=%zd", nwrit);
84 1.1 riastrad error = errno;
85 1.1 riastrad ATF_REQUIRE_EQ_MSG(error, ERESTART, "errno=%d (%s)", error,
86 1.1 riastrad strerror(error));
87 1.1 riastrad
88 1.1 riastrad nwrit = rump_sys_write(F->fd, buf, sizeof(buf));
89 1.2 riastrad ATF_REQUIRE_EQ_MSG(nwrit, -1, "nwrit=%zd", nwrit);
90 1.1 riastrad error = errno;
91 1.1 riastrad ATF_REQUIRE_EQ_MSG(error, EBADF, "errno=%d (%s)", error,
92 1.1 riastrad strerror(error));
93 1.1 riastrad }
94 1.1 riastrad
95 1.1 riastrad static void
96 1.1 riastrad waitforbarrier(struct fdrestart *F, const char *caller)
97 1.1 riastrad {
98 1.1 riastrad int error;
99 1.1 riastrad
100 1.1 riastrad error = pthread_barrier_wait(&F->barrier);
101 1.1 riastrad switch (error) {
102 1.1 riastrad case 0:
103 1.1 riastrad case PTHREAD_BARRIER_SERIAL_THREAD:
104 1.1 riastrad break;
105 1.1 riastrad default:
106 1.1 riastrad atf_tc_fail("%s: pthread_barrier_wait: %d, %s", caller, error,
107 1.1 riastrad strerror(error));
108 1.1 riastrad }
109 1.1 riastrad }
110 1.1 riastrad
111 1.1 riastrad static void *
112 1.1 riastrad doit(void *cookie)
113 1.1 riastrad {
114 1.1 riastrad struct fdrestart *F = cookie;
115 1.1 riastrad
116 1.1 riastrad waitforbarrier(F, "user");
117 1.1 riastrad (*F->op)(F);
118 1.1 riastrad
119 1.1 riastrad return NULL;
120 1.1 riastrad }
121 1.1 riastrad
122 1.1 riastrad static void
123 1.1 riastrad on_sigalrm(int signo)
124 1.1 riastrad {
125 1.1 riastrad
126 1.1 riastrad atf_tc_fail("timed out");
127 1.1 riastrad }
128 1.1 riastrad
129 1.1 riastrad static void
130 1.1 riastrad testfdrestart(struct fdrestart *F)
131 1.1 riastrad {
132 1.1 riastrad pthread_t t;
133 1.1 riastrad
134 1.1 riastrad ATF_REQUIRE_MSG(signal(SIGALRM, &on_sigalrm) != SIG_ERR,
135 1.1 riastrad "errno=%d (%s)", errno, strerror(errno));
136 1.1 riastrad
137 1.1 riastrad RZ(pthread_barrier_init(&F->barrier, NULL, 2));
138 1.1 riastrad RZ(pthread_create(&t, NULL, &doit, F));
139 1.1 riastrad waitforbarrier(F, "closer"); /* wait for thread to start */
140 1.1 riastrad (void)sleep(1); /* wait for op to start */
141 1.1 riastrad (void)alarm(1);
142 1.1 riastrad RL(rump_sys_close(F->fd));
143 1.1 riastrad RZ(pthread_join(t, NULL));
144 1.1 riastrad }
145 1.1 riastrad
146 1.1 riastrad ATF_TC(pipe_read);
147 1.1 riastrad ATF_TC_HEAD(pipe_read, tc)
148 1.1 riastrad {
149 1.1 riastrad atf_tc_set_md_var(tc, "descr", "Test pipe read fails on close");
150 1.1 riastrad }
151 1.1 riastrad ATF_TC_BODY(pipe_read, tc)
152 1.1 riastrad {
153 1.1 riastrad struct fdrestart fdrestart, *F = &fdrestart;
154 1.1 riastrad int fd[2];
155 1.1 riastrad
156 1.1 riastrad rump_init();
157 1.1 riastrad
158 1.1 riastrad RL(rump_sys_pipe(fd));
159 1.1 riastrad
160 1.1 riastrad memset(F, 0, sizeof(*F));
161 1.1 riastrad F->op = &doread;
162 1.1 riastrad F->fd = fd[0];
163 1.1 riastrad atf_tc_expect_fail("PR kern/57659");
164 1.1 riastrad testfdrestart(F);
165 1.1 riastrad }
166 1.1 riastrad
167 1.1 riastrad ATF_TC(pipe_write);
168 1.1 riastrad ATF_TC_HEAD(pipe_write, tc)
169 1.1 riastrad {
170 1.1 riastrad atf_tc_set_md_var(tc, "descr", "Test pipe write fails on close");
171 1.1 riastrad }
172 1.1 riastrad ATF_TC_BODY(pipe_write, tc)
173 1.1 riastrad {
174 1.1 riastrad struct fdrestart fdrestart, *F = &fdrestart;
175 1.1 riastrad int fd[2];
176 1.1 riastrad
177 1.1 riastrad rump_init();
178 1.1 riastrad
179 1.1 riastrad RL(rump_sys_pipe(fd));
180 1.1 riastrad
181 1.1 riastrad memset(F, 0, sizeof(*F));
182 1.1 riastrad F->op = &dowrite;
183 1.1 riastrad F->fd = fd[1];
184 1.1 riastrad atf_tc_expect_fail("PR kern/57659");
185 1.1 riastrad testfdrestart(F);
186 1.1 riastrad }
187 1.1 riastrad
188 1.1 riastrad ATF_TC(socketpair_read);
189 1.1 riastrad ATF_TC_HEAD(socketpair_read, tc)
190 1.1 riastrad {
191 1.1 riastrad atf_tc_set_md_var(tc, "descr", "Test socketpair read fails on close");
192 1.1 riastrad }
193 1.1 riastrad ATF_TC_BODY(socketpair_read, tc)
194 1.1 riastrad {
195 1.1 riastrad struct fdrestart fdrestart, *F = &fdrestart;
196 1.1 riastrad int fd[2];
197 1.1 riastrad
198 1.1 riastrad rump_init();
199 1.1 riastrad
200 1.1 riastrad RL(rump_sys_socketpair(AF_LOCAL, SOCK_STREAM, 0, fd));
201 1.1 riastrad
202 1.1 riastrad memset(F, 0, sizeof(*F));
203 1.1 riastrad F->op = &doread;
204 1.1 riastrad F->fd = fd[0];
205 1.1 riastrad atf_tc_expect_fail("PR kern/57659");
206 1.1 riastrad testfdrestart(F);
207 1.1 riastrad }
208 1.1 riastrad
209 1.1 riastrad ATF_TC(socketpair_write);
210 1.1 riastrad ATF_TC_HEAD(socketpair_write, tc)
211 1.1 riastrad {
212 1.1 riastrad atf_tc_set_md_var(tc, "descr", "Test socketpair write fails on close");
213 1.1 riastrad }
214 1.1 riastrad ATF_TC_BODY(socketpair_write, tc)
215 1.1 riastrad {
216 1.1 riastrad struct fdrestart fdrestart, *F = &fdrestart;
217 1.1 riastrad int fd[2];
218 1.1 riastrad
219 1.1 riastrad rump_init();
220 1.1 riastrad
221 1.1 riastrad RL(rump_sys_socketpair(AF_LOCAL, SOCK_STREAM, 0, fd));
222 1.1 riastrad
223 1.1 riastrad memset(F, 0, sizeof(*F));
224 1.1 riastrad F->op = &dowrite;
225 1.1 riastrad F->fd = fd[0];
226 1.1 riastrad atf_tc_expect_fail("PR kern/57659");
227 1.1 riastrad testfdrestart(F);
228 1.1 riastrad }
229 1.1 riastrad
230 1.1 riastrad ATF_TP_ADD_TCS(tp)
231 1.1 riastrad {
232 1.1 riastrad
233 1.1 riastrad ATF_TP_ADD_TC(tp, pipe_read);
234 1.1 riastrad ATF_TP_ADD_TC(tp, pipe_write);
235 1.1 riastrad ATF_TP_ADD_TC(tp, socketpair_read);
236 1.1 riastrad ATF_TP_ADD_TC(tp, socketpair_write);
237 1.1 riastrad
238 1.1 riastrad return atf_no_error();
239 1.1 riastrad }
240