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