diff options
Diffstat (limited to 'src/regress/lib/libc/sys/t_sendrecv.c')
| -rw-r--r-- | src/regress/lib/libc/sys/t_sendrecv.c | 186 |
1 files changed, 186 insertions, 0 deletions
diff --git a/src/regress/lib/libc/sys/t_sendrecv.c b/src/regress/lib/libc/sys/t_sendrecv.c new file mode 100644 index 0000000000..cf44af2b3e --- /dev/null +++ b/src/regress/lib/libc/sys/t_sendrecv.c | |||
| @@ -0,0 +1,186 @@ | |||
| 1 | /* $OpenBSD: t_sendrecv.c,v 1.1.1.1 2019/11/19 19:57:04 bluhm Exp $ */ | ||
| 2 | /* $NetBSD: t_sendrecv.c,v 1.6 2019/02/03 03:19:28 mrg Exp $ */ | ||
| 3 | |||
| 4 | /*- | ||
| 5 | * Copyright (c) 2018 The NetBSD Foundation, Inc. | ||
| 6 | * All rights reserved. | ||
| 7 | * | ||
| 8 | * This code is derived from software contributed to The NetBSD Foundation | ||
| 9 | * by Christos Zoulas. | ||
| 10 | * | ||
| 11 | * Redistribution and use in source and binary forms, with or without | ||
| 12 | * modification, are permitted provided that the following conditions | ||
| 13 | * are met: | ||
| 14 | * 1. Redistributions of source code must retain the above copyright | ||
| 15 | * notice, this list of conditions and the following disclaimer. | ||
| 16 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 17 | * notice, this list of conditions and the following disclaimer in the | ||
| 18 | * documentation and/or other materials provided with the distribution. | ||
| 19 | * | ||
| 20 | * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS | ||
| 21 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED | ||
| 22 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
| 23 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS | ||
| 24 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
| 25 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
| 26 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
| 27 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
| 28 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
| 29 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
| 30 | * POSSIBILITY OF SUCH DAMAGE. | ||
| 31 | */ | ||
| 32 | |||
| 33 | #include "macros.h" | ||
| 34 | |||
| 35 | #include <sys/cdefs.h> | ||
| 36 | __RCSID("$NetBSD: t_sendrecv.c,v 1.6 2019/02/03 03:19:28 mrg Exp $"); | ||
| 37 | |||
| 38 | #include "atf-c.h" | ||
| 39 | #include <sys/types.h> | ||
| 40 | #include <sys/socket.h> | ||
| 41 | |||
| 42 | #include <string.h> | ||
| 43 | #include <stdint.h> | ||
| 44 | #include <errno.h> | ||
| 45 | #include <stdio.h> | ||
| 46 | #include <stdlib.h> | ||
| 47 | #include <unistd.h> | ||
| 48 | #include <sched.h> | ||
| 49 | #include <signal.h> | ||
| 50 | |||
| 51 | |||
| 52 | #define COUNT 100 | ||
| 53 | |||
| 54 | union packet { | ||
| 55 | uint8_t buf[1316]; | ||
| 56 | uintmax_t seq; | ||
| 57 | }; | ||
| 58 | |||
| 59 | static volatile sig_atomic_t rdied; | ||
| 60 | |||
| 61 | static void | ||
| 62 | handle_sigchld(__unused int pid) | ||
| 63 | { | ||
| 64 | |||
| 65 | rdied = 1; | ||
| 66 | } | ||
| 67 | |||
| 68 | static void | ||
| 69 | sender(int fd) | ||
| 70 | { | ||
| 71 | union packet p; | ||
| 72 | ssize_t n; | ||
| 73 | p.seq = 0; | ||
| 74 | for (size_t i = 0; i < COUNT; i++) { | ||
| 75 | for (; (n = send(fd, &p, sizeof(p), 0)) == sizeof(p); | ||
| 76 | p.seq++) | ||
| 77 | continue; | ||
| 78 | printf(">>%zd %d %ju\n", n, errno, p.seq); | ||
| 79 | ATF_REQUIRE_MSG(errno == ENOBUFS, "send %s", strerror(errno)); | ||
| 80 | // sched_yield(); | ||
| 81 | } | ||
| 82 | printf("sender done\n"); | ||
| 83 | } | ||
| 84 | |||
| 85 | static void | ||
| 86 | receiver(int fd) | ||
| 87 | { | ||
| 88 | union packet p; | ||
| 89 | ssize_t n; | ||
| 90 | uintmax_t seq = 0; | ||
| 91 | |||
| 92 | do { | ||
| 93 | if (rdied) | ||
| 94 | return; | ||
| 95 | while ((n = recv(fd, &p, sizeof(p), 0), sizeof(p)) | ||
| 96 | == sizeof(p)) | ||
| 97 | { | ||
| 98 | if (rdied) | ||
| 99 | return; | ||
| 100 | if (p.seq != seq) | ||
| 101 | printf("%ju != %ju\n", p.seq, seq); | ||
| 102 | seq = p.seq + 1; | ||
| 103 | } | ||
| 104 | printf("<<%zd %d %ju\n", n, errno, seq); | ||
| 105 | if (n == 0) | ||
| 106 | return; | ||
| 107 | ATF_REQUIRE_EQ(n, -1); | ||
| 108 | ATF_REQUIRE_MSG(errno == ENOBUFS, "recv %s", strerror(errno)); | ||
| 109 | } while (p.seq < COUNT); | ||
| 110 | } | ||
| 111 | |||
| 112 | static void | ||
| 113 | sendrecv(int rerror) | ||
| 114 | { | ||
| 115 | int fd[2], error; | ||
| 116 | struct sigaction sa; | ||
| 117 | |||
| 118 | error = socketpair(AF_UNIX, SOCK_DGRAM, 0, fd); | ||
| 119 | // error = pipe(fd); | ||
| 120 | ATF_REQUIRE_MSG(error != -1, "socketpair failed (%s)", strerror(errno)); | ||
| 121 | |||
| 122 | for (size_t i = 0; i < __arraycount(fd); i++) { | ||
| 123 | error = setsockopt(fd[i], SOL_SOCKET, SO_RERROR, &rerror, | ||
| 124 | sizeof(rerror)); | ||
| 125 | ATF_REQUIRE_MSG(error != -1, | ||
| 126 | "setsockopt(SO_RERROR) failed (%s)", strerror(errno)); | ||
| 127 | } | ||
| 128 | |||
| 129 | memset(&sa, 0, sizeof(sa)); | ||
| 130 | sa.sa_flags = 0; | ||
| 131 | sa.sa_handler = &handle_sigchld; | ||
| 132 | sigemptyset(&sa.sa_mask); | ||
| 133 | error = sigaction(SIGCHLD, &sa, 0); | ||
| 134 | ATF_REQUIRE_MSG(error != -1, "sigaction failed (%s)", | ||
| 135 | strerror(errno)); | ||
| 136 | |||
| 137 | switch (fork()) { | ||
| 138 | case -1: | ||
| 139 | ATF_REQUIRE_MSG(errno == 0, | ||
| 140 | "socketpair failed (%s)", strerror(errno)); | ||
| 141 | __unreachable(); | ||
| 142 | /*NOTREACHED*/ | ||
| 143 | case 0: | ||
| 144 | sched_yield(); | ||
| 145 | sender(fd[0]); | ||
| 146 | close(fd[0]); | ||
| 147 | exit(EXIT_SUCCESS); | ||
| 148 | /*NOTREACHED*/ | ||
| 149 | default: | ||
| 150 | receiver(fd[1]); | ||
| 151 | return; | ||
| 152 | } | ||
| 153 | } | ||
| 154 | |||
| 155 | ATF_TC(sendrecv_basic); | ||
| 156 | |||
| 157 | ATF_TC_HEAD(sendrecv_basic, tc) | ||
| 158 | { | ||
| 159 | atf_tc_set_md_var(tc, "descr", "A basic test of send/recv(2)"); | ||
| 160 | } | ||
| 161 | |||
| 162 | ATF_TC_BODY(sendrecv_basic, tc) | ||
| 163 | { | ||
| 164 | sendrecv(0); | ||
| 165 | } | ||
| 166 | |||
| 167 | ATF_TC(sendrecv_rerror); | ||
| 168 | |||
| 169 | ATF_TC_HEAD(sendrecv_rerror, tc) | ||
| 170 | { | ||
| 171 | atf_tc_set_md_var(tc, "descr", "Test send/recv(2) with receiver error"); | ||
| 172 | } | ||
| 173 | |||
| 174 | ATF_TC_BODY(sendrecv_rerror, tc) | ||
| 175 | { | ||
| 176 | sendrecv(1); | ||
| 177 | } | ||
| 178 | |||
| 179 | ATF_TP_ADD_TCS(tp) | ||
| 180 | { | ||
| 181 | |||
| 182 | ATF_TP_ADD_TC(tp, sendrecv_basic); | ||
| 183 | ATF_TP_ADD_TC(tp, sendrecv_rerror); | ||
| 184 | |||
| 185 | return atf_no_error(); | ||
| 186 | } | ||
