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