1 /* $OpenBSD: t_sendrecv.c,v 1.2 2021/05/31 16:56:27 bluhm Exp $ */ 2 /* $NetBSD: t_sendrecv.c,v 1.8 2021/03/28 17:30:01 christos 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.8 2021/03/28 17:30:01 christos 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 <sched.h> 48 #include <unistd.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 sd) 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(sd, &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 } 81 close(sd); 82 // printf("sender done\n"); 83 } 84 85 static void 86 receiver(int sd) 87 { 88 union packet p; 89 ssize_t n; 90 uintmax_t seq = 0; 91 92 for (size_t i = 0; i < COUNT; i++) { 93 if (rdied) 94 return; 95 while ((n = recv(sd, &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 if (seq % 10 == 0) 103 sched_yield(); 104 seq = p.seq + 1; 105 } 106 // printf("<<%zd %d %ju\n", n, errno, seq); 107 if (n == 0) 108 return; 109 ATF_REQUIRE_EQ(n, -1); 110 ATF_REQUIRE_MSG(errno == ENOBUFS, "recv %s", strerror(errno)); 111 } 112 close(sd); 113 } 114 115 static void 116 sendrecv(int rerror) 117 { 118 int fd[2], sd[2], error; 119 char c = 0; 120 struct sigaction sa; 121 122 error = socketpair(AF_UNIX, SOCK_DGRAM, 0, sd); 123 ATF_REQUIRE_MSG(error != -1, "socketpair failed (%s)", strerror(errno)); 124 error = pipe(fd); 125 ATF_REQUIRE_MSG(error != -1, "pipe failed (%s)", strerror(errno)); 126 127 for (size_t i = 0; i < __arraycount(sd); i++) { 128 error = setsockopt(sd[i], SOL_SOCKET, SO_RERROR, &rerror, 129 sizeof(rerror)); 130 ATF_REQUIRE_MSG(error != -1, 131 "setsockopt(SO_RERROR) failed (%s)", strerror(errno)); 132 } 133 134 memset(&sa, 0, sizeof(sa)); 135 sa.sa_flags = 0; 136 sa.sa_handler = &handle_sigchld; 137 sigemptyset(&sa.sa_mask); 138 error = sigaction(SIGCHLD, &sa, 0); 139 ATF_REQUIRE_MSG(error != -1, "sigaction failed (%s)", 140 strerror(errno)); 141 142 switch (fork()) { 143 case -1: 144 ATF_REQUIRE_MSG(errno == 0, 145 "fork failed (%s)", strerror(errno)); 146 __unreachable(); 147 /*NOTREACHED*/ 148 case 0: 149 read(fd[1], &c, sizeof(c)); 150 sender(sd[0]); 151 close(sd[0]); 152 exit(EXIT_SUCCESS); 153 /*NOTREACHED*/ 154 default: 155 write(fd[0], &c, sizeof(c)); 156 receiver(sd[1]); 157 return; 158 } 159 } 160 161 ATF_TC(sendrecv_basic); 162 163 ATF_TC_HEAD(sendrecv_basic, tc) 164 { 165 atf_tc_set_md_var(tc, "descr", "A basic test of send/recv(2)"); 166 } 167 168 ATF_TC_BODY(sendrecv_basic, tc) 169 { 170 sendrecv(0); 171 } 172 173 ATF_TC(sendrecv_rerror); 174 175 ATF_TC_HEAD(sendrecv_rerror, tc) 176 { 177 atf_tc_set_md_var(tc, "descr", "Test send/recv(2) with receiver error"); 178 } 179 180 ATF_TC_BODY(sendrecv_rerror, tc) 181 { 182 sendrecv(1); 183 } 184 185 ATF_TP_ADD_TCS(tp) 186 { 187 188 ATF_TP_ADD_TC(tp, sendrecv_basic); 189 ATF_TP_ADD_TC(tp, sendrecv_rerror); 190 191 return atf_no_error(); 192 } 193