1*8328a6edSriastradh /* $NetBSD: t_fdrestart.c,v 1.4 2023/11/18 19:46:55 riastradh Exp $ */
25ba0017fSriastradh
35ba0017fSriastradh /*-
45ba0017fSriastradh * Copyright (c) 2023 The NetBSD Foundation, Inc.
55ba0017fSriastradh * All rights reserved.
65ba0017fSriastradh *
75ba0017fSriastradh * Redistribution and use in source and binary forms, with or without
85ba0017fSriastradh * modification, are permitted provided that the following conditions
95ba0017fSriastradh * are met:
105ba0017fSriastradh * 1. Redistributions of source code must retain the above copyright
115ba0017fSriastradh * notice, this list of conditions and the following disclaimer.
125ba0017fSriastradh * 2. Redistributions in binary form must reproduce the above copyright
135ba0017fSriastradh * notice, this list of conditions and the following disclaimer in the
145ba0017fSriastradh * documentation and/or other materials provided with the distribution.
155ba0017fSriastradh *
165ba0017fSriastradh * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
175ba0017fSriastradh * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
185ba0017fSriastradh * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
195ba0017fSriastradh * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
205ba0017fSriastradh * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
215ba0017fSriastradh * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
225ba0017fSriastradh * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
235ba0017fSriastradh * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
245ba0017fSriastradh * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
255ba0017fSriastradh * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
265ba0017fSriastradh * POSSIBILITY OF SUCH DAMAGE.
275ba0017fSriastradh */
285ba0017fSriastradh
295ba0017fSriastradh #define _KMEMUSER /* ERESTART */
305ba0017fSriastradh
315ba0017fSriastradh #include <sys/cdefs.h>
32*8328a6edSriastradh __RCSID("$NetBSD: t_fdrestart.c,v 1.4 2023/11/18 19:46:55 riastradh Exp $");
335ba0017fSriastradh
34349eda5cSriastradh #include <sys/ioctl.h>
355ba0017fSriastradh #include <sys/socket.h>
365ba0017fSriastradh #include <sys/un.h>
375ba0017fSriastradh
385ba0017fSriastradh #include <atf-c.h>
395ba0017fSriastradh #include <errno.h>
405ba0017fSriastradh #include <pthread.h>
415ba0017fSriastradh #include <unistd.h>
425ba0017fSriastradh
435ba0017fSriastradh #include <rump/rump.h>
445ba0017fSriastradh #include <rump/rump_syscalls.h>
455ba0017fSriastradh
465ba0017fSriastradh #include "h_macros.h"
475ba0017fSriastradh
485ba0017fSriastradh struct fdrestart {
495ba0017fSriastradh void (*op)(struct fdrestart *);
505ba0017fSriastradh int fd;
515ba0017fSriastradh pthread_barrier_t barrier;
525ba0017fSriastradh };
535ba0017fSriastradh
545ba0017fSriastradh static void
waitforbarrier(struct fdrestart * F,const char * caller)555ba0017fSriastradh waitforbarrier(struct fdrestart *F, const char *caller)
565ba0017fSriastradh {
575ba0017fSriastradh int error;
585ba0017fSriastradh
595ba0017fSriastradh error = pthread_barrier_wait(&F->barrier);
605ba0017fSriastradh switch (error) {
615ba0017fSriastradh case 0:
625ba0017fSriastradh case PTHREAD_BARRIER_SERIAL_THREAD:
635ba0017fSriastradh break;
645ba0017fSriastradh default:
655ba0017fSriastradh atf_tc_fail("%s: pthread_barrier_wait: %d, %s", caller, error,
665ba0017fSriastradh strerror(error));
675ba0017fSriastradh }
685ba0017fSriastradh }
695ba0017fSriastradh
70349eda5cSriastradh static void
doread(struct fdrestart * F)71349eda5cSriastradh doread(struct fdrestart *F)
72349eda5cSriastradh {
73349eda5cSriastradh char c;
74349eda5cSriastradh ssize_t nread;
75349eda5cSriastradh int error;
76349eda5cSriastradh
77349eda5cSriastradh /*
78349eda5cSriastradh * Wait for the other thread to be ready.
79349eda5cSriastradh */
80349eda5cSriastradh waitforbarrier(F, "reader");
81349eda5cSriastradh
82349eda5cSriastradh /*
83349eda5cSriastradh * Start a read. This should block, and then, when the other
84349eda5cSriastradh * thread closes the fd, should be woken to fail with ERESTART.
85349eda5cSriastradh */
86349eda5cSriastradh nread = rump_sys_read(F->fd, &c, sizeof(c));
87349eda5cSriastradh ATF_REQUIRE_EQ_MSG(nread, -1, "nread=%zd", nread);
88349eda5cSriastradh error = errno;
89349eda5cSriastradh ATF_REQUIRE_EQ_MSG(error, ERESTART, "errno=%d (%s)", error,
90349eda5cSriastradh strerror(error));
91349eda5cSriastradh
92349eda5cSriastradh /*
93349eda5cSriastradh * Now further attempts at I/O should fail with EBADF because
94349eda5cSriastradh * the fd has been closed.
95349eda5cSriastradh */
96349eda5cSriastradh nread = rump_sys_read(F->fd, &c, sizeof(c));
97349eda5cSriastradh ATF_REQUIRE_EQ_MSG(nread, -1, "nread=%zd", nread);
98349eda5cSriastradh error = errno;
99349eda5cSriastradh ATF_REQUIRE_EQ_MSG(error, EBADF, "errno=%d (%s)", error,
100349eda5cSriastradh strerror(error));
101349eda5cSriastradh }
102349eda5cSriastradh
103349eda5cSriastradh static void
dowrite(struct fdrestart * F)104349eda5cSriastradh dowrite(struct fdrestart *F)
105349eda5cSriastradh {
106349eda5cSriastradh static const char buf[1024*1024]; /* XXX >BIG_PIPE_SIZE */
107349eda5cSriastradh ssize_t nwrit;
108349eda5cSriastradh int error;
109349eda5cSriastradh
110349eda5cSriastradh /*
111349eda5cSriastradh * Make sure the pipe's buffer is full first.
112349eda5cSriastradh */
113349eda5cSriastradh for (;;) {
114349eda5cSriastradh int nspace;
115349eda5cSriastradh
116349eda5cSriastradh RL(rump_sys_ioctl(F->fd, FIONSPACE, &nspace));
117349eda5cSriastradh ATF_REQUIRE_MSG(nspace >= 0, "nspace=%d", nspace);
118349eda5cSriastradh if (nspace == 0)
119349eda5cSriastradh break;
120349eda5cSriastradh RL(rump_sys_write(F->fd, buf, (size_t)nspace));
121349eda5cSriastradh }
122349eda5cSriastradh
123349eda5cSriastradh /*
124349eda5cSriastradh * Wait for the other thread to be ready.
125349eda5cSriastradh */
126349eda5cSriastradh waitforbarrier(F, "writer");
127349eda5cSriastradh
128349eda5cSriastradh /*
129349eda5cSriastradh * Start a write. This should block, and then, when the other
130349eda5cSriastradh * thread closes the fd, should be woken to fail with ERESTART.
131349eda5cSriastradh */
132349eda5cSriastradh nwrit = rump_sys_write(F->fd, buf, sizeof(buf));
133349eda5cSriastradh ATF_REQUIRE_EQ_MSG(nwrit, -1, "nwrit=%zd", nwrit);
134349eda5cSriastradh error = errno;
135349eda5cSriastradh ATF_REQUIRE_EQ_MSG(error, ERESTART, "errno=%d (%s)", error,
136349eda5cSriastradh strerror(error));
137349eda5cSriastradh
138349eda5cSriastradh /*
139349eda5cSriastradh * Now further attempts at I/O should fail with EBADF because
140349eda5cSriastradh * the fd has been closed.
141349eda5cSriastradh */
142349eda5cSriastradh nwrit = rump_sys_write(F->fd, buf, sizeof(buf));
143349eda5cSriastradh ATF_REQUIRE_EQ_MSG(nwrit, -1, "nwrit=%zd", nwrit);
144349eda5cSriastradh error = errno;
145349eda5cSriastradh ATF_REQUIRE_EQ_MSG(error, EBADF, "errno=%d (%s)", error,
146349eda5cSriastradh strerror(error));
147349eda5cSriastradh }
148349eda5cSriastradh
1495ba0017fSriastradh static void *
doit(void * cookie)1505ba0017fSriastradh doit(void *cookie)
1515ba0017fSriastradh {
1525ba0017fSriastradh struct fdrestart *F = cookie;
1535ba0017fSriastradh
1545ba0017fSriastradh (*F->op)(F);
1555ba0017fSriastradh
1565ba0017fSriastradh return NULL;
1575ba0017fSriastradh }
1585ba0017fSriastradh
1595ba0017fSriastradh static void
on_sigalrm(int signo)1605ba0017fSriastradh on_sigalrm(int signo)
1615ba0017fSriastradh {
1625ba0017fSriastradh
1635ba0017fSriastradh atf_tc_fail("timed out");
1645ba0017fSriastradh }
1655ba0017fSriastradh
1665ba0017fSriastradh static void
testfdrestart(struct fdrestart * F)1675ba0017fSriastradh testfdrestart(struct fdrestart *F)
1685ba0017fSriastradh {
1695ba0017fSriastradh pthread_t t;
1705ba0017fSriastradh
1715ba0017fSriastradh ATF_REQUIRE_MSG(signal(SIGALRM, &on_sigalrm) != SIG_ERR,
1725ba0017fSriastradh "errno=%d (%s)", errno, strerror(errno));
1735ba0017fSriastradh
1745ba0017fSriastradh RZ(pthread_barrier_init(&F->barrier, NULL, 2));
1755ba0017fSriastradh RZ(pthread_create(&t, NULL, &doit, F));
1765ba0017fSriastradh waitforbarrier(F, "closer"); /* wait for thread to start */
1775ba0017fSriastradh (void)sleep(1); /* wait for op to start */
178349eda5cSriastradh (void)alarm(1); /* set a deadline */
179349eda5cSriastradh RL(rump_sys_close(F->fd)); /* wake op in other thread */
180349eda5cSriastradh RZ(pthread_join(t, NULL)); /* wait for op to wake and fail */
181349eda5cSriastradh (void)alarm(0); /* clear the deadline */
1825ba0017fSriastradh }
1835ba0017fSriastradh
1845ba0017fSriastradh ATF_TC(pipe_read);
ATF_TC_HEAD(pipe_read,tc)1855ba0017fSriastradh ATF_TC_HEAD(pipe_read, tc)
1865ba0017fSriastradh {
1875ba0017fSriastradh atf_tc_set_md_var(tc, "descr", "Test pipe read fails on close");
1885ba0017fSriastradh }
ATF_TC_BODY(pipe_read,tc)1895ba0017fSriastradh ATF_TC_BODY(pipe_read, tc)
1905ba0017fSriastradh {
1915ba0017fSriastradh struct fdrestart fdrestart, *F = &fdrestart;
1925ba0017fSriastradh int fd[2];
1935ba0017fSriastradh
1945ba0017fSriastradh rump_init();
1955ba0017fSriastradh
1965ba0017fSriastradh RL(rump_sys_pipe(fd));
1975ba0017fSriastradh
1985ba0017fSriastradh memset(F, 0, sizeof(*F));
1995ba0017fSriastradh F->op = &doread;
2005ba0017fSriastradh F->fd = fd[0];
2015ba0017fSriastradh testfdrestart(F);
2025ba0017fSriastradh }
2035ba0017fSriastradh
2045ba0017fSriastradh ATF_TC(pipe_write);
ATF_TC_HEAD(pipe_write,tc)2055ba0017fSriastradh ATF_TC_HEAD(pipe_write, tc)
2065ba0017fSriastradh {
2075ba0017fSriastradh atf_tc_set_md_var(tc, "descr", "Test pipe write fails on close");
2085ba0017fSriastradh }
ATF_TC_BODY(pipe_write,tc)2095ba0017fSriastradh ATF_TC_BODY(pipe_write, tc)
2105ba0017fSriastradh {
2115ba0017fSriastradh struct fdrestart fdrestart, *F = &fdrestart;
2125ba0017fSriastradh int fd[2];
2135ba0017fSriastradh
2145ba0017fSriastradh rump_init();
2155ba0017fSriastradh
2165ba0017fSriastradh RL(rump_sys_pipe(fd));
2175ba0017fSriastradh
2185ba0017fSriastradh memset(F, 0, sizeof(*F));
2195ba0017fSriastradh F->op = &dowrite;
2205ba0017fSriastradh F->fd = fd[1];
2215ba0017fSriastradh atf_tc_expect_fail("PR kern/57659");
2225ba0017fSriastradh testfdrestart(F);
2235ba0017fSriastradh }
2245ba0017fSriastradh
2255ba0017fSriastradh ATF_TC(socketpair_read);
ATF_TC_HEAD(socketpair_read,tc)2265ba0017fSriastradh ATF_TC_HEAD(socketpair_read, tc)
2275ba0017fSriastradh {
2285ba0017fSriastradh atf_tc_set_md_var(tc, "descr", "Test socketpair read fails on close");
2295ba0017fSriastradh }
ATF_TC_BODY(socketpair_read,tc)2305ba0017fSriastradh ATF_TC_BODY(socketpair_read, tc)
2315ba0017fSriastradh {
2325ba0017fSriastradh struct fdrestart fdrestart, *F = &fdrestart;
2335ba0017fSriastradh int fd[2];
2345ba0017fSriastradh
2355ba0017fSriastradh rump_init();
2365ba0017fSriastradh
2375ba0017fSriastradh RL(rump_sys_socketpair(AF_LOCAL, SOCK_STREAM, 0, fd));
2385ba0017fSriastradh
2395ba0017fSriastradh memset(F, 0, sizeof(*F));
2405ba0017fSriastradh F->op = &doread;
2415ba0017fSriastradh F->fd = fd[0];
2425ba0017fSriastradh testfdrestart(F);
2435ba0017fSriastradh }
2445ba0017fSriastradh
2455ba0017fSriastradh ATF_TC(socketpair_write);
ATF_TC_HEAD(socketpair_write,tc)2465ba0017fSriastradh ATF_TC_HEAD(socketpair_write, tc)
2475ba0017fSriastradh {
2485ba0017fSriastradh atf_tc_set_md_var(tc, "descr", "Test socketpair write fails on close");
2495ba0017fSriastradh }
ATF_TC_BODY(socketpair_write,tc)2505ba0017fSriastradh ATF_TC_BODY(socketpair_write, tc)
2515ba0017fSriastradh {
2525ba0017fSriastradh struct fdrestart fdrestart, *F = &fdrestart;
2535ba0017fSriastradh int fd[2];
2545ba0017fSriastradh
2555ba0017fSriastradh rump_init();
2565ba0017fSriastradh
2575ba0017fSriastradh RL(rump_sys_socketpair(AF_LOCAL, SOCK_STREAM, 0, fd));
2585ba0017fSriastradh
2595ba0017fSriastradh memset(F, 0, sizeof(*F));
2605ba0017fSriastradh F->op = &dowrite;
2615ba0017fSriastradh F->fd = fd[0];
2625ba0017fSriastradh testfdrestart(F);
2635ba0017fSriastradh }
2645ba0017fSriastradh
ATF_TP_ADD_TCS(tp)2655ba0017fSriastradh ATF_TP_ADD_TCS(tp)
2665ba0017fSriastradh {
2675ba0017fSriastradh
2685ba0017fSriastradh ATF_TP_ADD_TC(tp, pipe_read);
2695ba0017fSriastradh ATF_TP_ADD_TC(tp, pipe_write);
2705ba0017fSriastradh ATF_TP_ADD_TC(tp, socketpair_read);
2715ba0017fSriastradh ATF_TP_ADD_TC(tp, socketpair_write);
2725ba0017fSriastradh
2735ba0017fSriastradh return atf_no_error();
2745ba0017fSriastradh }
275