1 /* $OpenBSD: t_sendrecv.c,v 1.3 2021/12/13 16:56:48 deraadt 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 "atf-c.h" 36 #include <sys/types.h> 37 #include <sys/socket.h> 38 39 #include <string.h> 40 #include <stdint.h> 41 #include <errno.h> 42 #include <stdio.h> 43 #include <stdlib.h> 44 #include <sched.h> 45 #include <unistd.h> 46 #include <signal.h> 47 48 49 #define COUNT 100 50 51 union packet { 52 uint8_t buf[1316]; 53 uintmax_t seq; 54 }; 55 56 static volatile sig_atomic_t rdied; 57 58 static void 59 handle_sigchld(__unused int pid) 60 { 61 62 rdied = 1; 63 } 64 65 static void 66 sender(int sd) 67 { 68 union packet p; 69 ssize_t n; 70 p.seq = 0; 71 for (size_t i = 0; i < COUNT; i++) { 72 for (; (n = send(sd, &p, sizeof(p), 0)) == sizeof(p); 73 p.seq++) 74 continue; 75 // printf(">>%zd %d %ju\n", n, errno, p.seq); 76 ATF_REQUIRE_MSG(errno == ENOBUFS, "send %s", strerror(errno)); 77 } 78 close(sd); 79 // printf("sender done\n"); 80 } 81 82 static void 83 receiver(int sd) 84 { 85 union packet p; 86 ssize_t n; 87 uintmax_t seq = 0; 88 89 for (size_t i = 0; i < COUNT; i++) { 90 if (rdied) 91 return; 92 while ((n = recv(sd, &p, sizeof(p), 0), sizeof(p)) 93 == sizeof(p)) 94 { 95 if (rdied) 96 return; 97 if (p.seq != seq) 98 printf("%ju != %ju\n", p.seq, seq); 99 if (seq % 10 == 0) 100 sched_yield(); 101 seq = p.seq + 1; 102 } 103 // printf("<<%zd %d %ju\n", n, errno, seq); 104 if (n == 0) 105 return; 106 ATF_REQUIRE_EQ(n, -1); 107 ATF_REQUIRE_MSG(errno == ENOBUFS, "recv %s", strerror(errno)); 108 } 109 close(sd); 110 } 111 112 static void 113 sendrecv(int rerror) 114 { 115 int fd[2], sd[2], error; 116 char c = 0; 117 struct sigaction sa; 118 119 error = socketpair(AF_UNIX, SOCK_DGRAM, 0, sd); 120 ATF_REQUIRE_MSG(error != -1, "socketpair failed (%s)", strerror(errno)); 121 error = pipe(fd); 122 ATF_REQUIRE_MSG(error != -1, "pipe failed (%s)", strerror(errno)); 123 124 for (size_t i = 0; i < __arraycount(sd); i++) { 125 error = setsockopt(sd[i], SOL_SOCKET, SO_RERROR, &rerror, 126 sizeof(rerror)); 127 ATF_REQUIRE_MSG(error != -1, 128 "setsockopt(SO_RERROR) failed (%s)", strerror(errno)); 129 } 130 131 memset(&sa, 0, sizeof(sa)); 132 sa.sa_flags = 0; 133 sa.sa_handler = &handle_sigchld; 134 sigemptyset(&sa.sa_mask); 135 error = sigaction(SIGCHLD, &sa, 0); 136 ATF_REQUIRE_MSG(error != -1, "sigaction failed (%s)", 137 strerror(errno)); 138 139 switch (fork()) { 140 case -1: 141 ATF_REQUIRE_MSG(errno == 0, 142 "fork failed (%s)", strerror(errno)); 143 __unreachable(); 144 /*NOTREACHED*/ 145 case 0: 146 read(fd[1], &c, sizeof(c)); 147 sender(sd[0]); 148 close(sd[0]); 149 exit(EXIT_SUCCESS); 150 /*NOTREACHED*/ 151 default: 152 write(fd[0], &c, sizeof(c)); 153 receiver(sd[1]); 154 return; 155 } 156 } 157 158 ATF_TC(sendrecv_basic); 159 160 ATF_TC_HEAD(sendrecv_basic, tc) 161 { 162 atf_tc_set_md_var(tc, "descr", "A basic test of send/recv(2)"); 163 } 164 165 ATF_TC_BODY(sendrecv_basic, tc) 166 { 167 sendrecv(0); 168 } 169 170 ATF_TC(sendrecv_rerror); 171 172 ATF_TC_HEAD(sendrecv_rerror, tc) 173 { 174 atf_tc_set_md_var(tc, "descr", "Test send/recv(2) with receiver error"); 175 } 176 177 ATF_TC_BODY(sendrecv_rerror, tc) 178 { 179 sendrecv(1); 180 } 181 182 ATF_TP_ADD_TCS(tp) 183 { 184 185 ATF_TP_ADD_TC(tp, sendrecv_basic); 186 ATF_TP_ADD_TC(tp, sendrecv_rerror); 187 188 return atf_no_error(); 189 } 190