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