2021-04-04 01:28:12 +03:00
|
|
|
/* $NetBSD: t_sendrecv.c,v 1.6.8.1 2021/04/03 22:34:59 thorpej Exp $ */
|
2018-08-21 13:41:00 +03:00
|
|
|
|
|
|
|
/*-
|
|
|
|
* Copyright (c) 2018 The NetBSD Foundation, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to The NetBSD Foundation
|
|
|
|
* by Christos Zoulas.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
|
|
|
|
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
|
|
|
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
|
|
|
|
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
#include <sys/cdefs.h>
|
2021-04-04 01:28:12 +03:00
|
|
|
__RCSID("$NetBSD: t_sendrecv.c,v 1.6.8.1 2021/04/03 22:34:59 thorpej Exp $");
|
2018-08-21 13:41:00 +03:00
|
|
|
|
|
|
|
#include <atf-c.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sched.h>
|
2021-04-04 01:28:12 +03:00
|
|
|
#include <unistd.h>
|
2018-08-21 14:03:27 +03:00
|
|
|
#include <signal.h>
|
2018-08-21 13:41:00 +03:00
|
|
|
|
|
|
|
|
2018-08-21 14:03:27 +03:00
|
|
|
#define COUNT 100
|
2018-08-21 13:41:00 +03:00
|
|
|
|
|
|
|
union packet {
|
|
|
|
uint8_t buf[1316];
|
|
|
|
uintmax_t seq;
|
|
|
|
};
|
|
|
|
|
2018-08-21 14:03:27 +03:00
|
|
|
static volatile sig_atomic_t rdied;
|
|
|
|
|
|
|
|
static void
|
|
|
|
handle_sigchld(__unused int pid)
|
|
|
|
{
|
|
|
|
|
|
|
|
rdied = 1;
|
|
|
|
}
|
|
|
|
|
2018-08-21 13:41:00 +03:00
|
|
|
static void
|
2021-04-04 01:28:12 +03:00
|
|
|
sender(int sd)
|
2018-08-21 13:41:00 +03:00
|
|
|
{
|
|
|
|
union packet p;
|
|
|
|
ssize_t n;
|
|
|
|
p.seq = 0;
|
|
|
|
for (size_t i = 0; i < COUNT; i++) {
|
2021-04-04 01:28:12 +03:00
|
|
|
for (; (n = send(sd, &p, sizeof(p), 0)) == sizeof(p);
|
2018-08-21 13:41:00 +03:00
|
|
|
p.seq++)
|
|
|
|
continue;
|
2021-04-04 01:28:12 +03:00
|
|
|
// printf(">>%zd %d %ju\n", n, errno, p.seq);
|
2018-08-21 13:41:00 +03:00
|
|
|
ATF_REQUIRE_MSG(errno == ENOBUFS, "send %s", strerror(errno));
|
|
|
|
}
|
2021-04-04 01:28:12 +03:00
|
|
|
close(sd);
|
|
|
|
// printf("sender done\n");
|
2018-08-21 13:41:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2021-04-04 01:28:12 +03:00
|
|
|
receiver(int sd)
|
2018-08-21 13:41:00 +03:00
|
|
|
{
|
|
|
|
union packet p;
|
|
|
|
ssize_t n;
|
|
|
|
uintmax_t seq = 0;
|
|
|
|
|
2021-04-04 01:28:12 +03:00
|
|
|
for (size_t i = 0; i < COUNT; i++) {
|
2018-08-21 14:03:27 +03:00
|
|
|
if (rdied)
|
|
|
|
return;
|
2021-04-04 01:28:12 +03:00
|
|
|
while ((n = recv(sd, &p, sizeof(p), 0), sizeof(p))
|
2018-08-21 13:41:00 +03:00
|
|
|
== sizeof(p))
|
|
|
|
{
|
2018-08-21 14:03:27 +03:00
|
|
|
if (rdied)
|
|
|
|
return;
|
|
|
|
if (p.seq != seq)
|
|
|
|
printf("%ju != %ju\n", p.seq, seq);
|
2021-04-04 01:28:12 +03:00
|
|
|
if (seq % 10 == 0)
|
|
|
|
sched_yield();
|
2018-08-21 13:41:00 +03:00
|
|
|
seq = p.seq + 1;
|
|
|
|
}
|
2021-04-04 01:28:12 +03:00
|
|
|
// printf("<<%zd %d %ju\n", n, errno, seq);
|
2018-08-21 13:41:00 +03:00
|
|
|
if (n == 0)
|
|
|
|
return;
|
|
|
|
ATF_REQUIRE_EQ(n, -1);
|
|
|
|
ATF_REQUIRE_MSG(errno == ENOBUFS, "recv %s", strerror(errno));
|
2021-04-04 01:28:12 +03:00
|
|
|
}
|
|
|
|
close(sd);
|
2018-08-21 13:41:00 +03:00
|
|
|
}
|
|
|
|
|
2018-11-06 20:55:04 +03:00
|
|
|
static void
|
|
|
|
sendrecv(int rerror)
|
2018-08-21 13:41:00 +03:00
|
|
|
{
|
2021-04-04 01:28:12 +03:00
|
|
|
int fd[2], sd[2], error;
|
|
|
|
char c = 0;
|
2018-08-21 14:03:27 +03:00
|
|
|
struct sigaction sa;
|
2018-08-21 13:41:00 +03:00
|
|
|
|
2021-04-04 01:28:12 +03:00
|
|
|
error = socketpair(AF_UNIX, SOCK_DGRAM, 0, sd);
|
2018-08-21 13:41:00 +03:00
|
|
|
ATF_REQUIRE_MSG(error != -1, "socketpair failed (%s)", strerror(errno));
|
2021-04-04 01:28:12 +03:00
|
|
|
error = pipe(fd);
|
|
|
|
ATF_REQUIRE_MSG(error != -1, "pipe failed (%s)", strerror(errno));
|
2018-08-21 13:41:00 +03:00
|
|
|
|
2021-04-04 01:28:12 +03:00
|
|
|
for (size_t i = 0; i < __arraycount(sd); i++) {
|
|
|
|
error = setsockopt(sd[i], SOL_SOCKET, SO_RERROR, &rerror,
|
2018-11-06 20:55:04 +03:00
|
|
|
sizeof(rerror));
|
|
|
|
ATF_REQUIRE_MSG(error != -1,
|
|
|
|
"setsockopt(SO_RERROR) failed (%s)", strerror(errno));
|
|
|
|
}
|
|
|
|
|
2018-08-21 14:03:27 +03:00
|
|
|
memset(&sa, 0, sizeof(sa));
|
2018-08-22 09:31:37 +03:00
|
|
|
sa.sa_flags = 0;
|
2018-08-21 14:03:27 +03:00
|
|
|
sa.sa_handler = &handle_sigchld;
|
|
|
|
sigemptyset(&sa.sa_mask);
|
|
|
|
error = sigaction(SIGCHLD, &sa, 0);
|
|
|
|
ATF_REQUIRE_MSG(error != -1, "sigaction failed (%s)",
|
|
|
|
strerror(errno));
|
|
|
|
|
2018-08-21 13:41:00 +03:00
|
|
|
switch (fork()) {
|
|
|
|
case -1:
|
|
|
|
ATF_REQUIRE_MSG(errno == 0,
|
2021-04-04 01:28:12 +03:00
|
|
|
"fork failed (%s)", strerror(errno));
|
2019-02-03 06:19:25 +03:00
|
|
|
__unreachable();
|
2018-08-21 13:41:00 +03:00
|
|
|
/*NOTREACHED*/
|
|
|
|
case 0:
|
2021-04-04 01:28:12 +03:00
|
|
|
read(fd[1], &c, sizeof(c));
|
|
|
|
sender(sd[0]);
|
|
|
|
close(sd[0]);
|
2018-08-21 13:41:00 +03:00
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
/*NOTREACHED*/
|
|
|
|
default:
|
2021-04-04 01:28:12 +03:00
|
|
|
write(fd[0], &c, sizeof(c));
|
|
|
|
receiver(sd[1]);
|
2018-08-21 13:41:00 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-06 20:55:04 +03:00
|
|
|
ATF_TC(sendrecv_basic);
|
|
|
|
|
|
|
|
ATF_TC_HEAD(sendrecv_basic, tc)
|
|
|
|
{
|
|
|
|
atf_tc_set_md_var(tc, "descr", "A basic test of send/recv(2)");
|
|
|
|
}
|
|
|
|
|
|
|
|
ATF_TC_BODY(sendrecv_basic, tc)
|
|
|
|
{
|
|
|
|
sendrecv(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
ATF_TC(sendrecv_rerror);
|
|
|
|
|
|
|
|
ATF_TC_HEAD(sendrecv_rerror, tc)
|
|
|
|
{
|
|
|
|
atf_tc_set_md_var(tc, "descr", "Test send/recv(2) with receiver error");
|
|
|
|
}
|
|
|
|
|
|
|
|
ATF_TC_BODY(sendrecv_rerror, tc)
|
|
|
|
{
|
|
|
|
sendrecv(1);
|
|
|
|
}
|
|
|
|
|
2018-08-21 13:41:00 +03:00
|
|
|
ATF_TP_ADD_TCS(tp)
|
|
|
|
{
|
|
|
|
|
|
|
|
ATF_TP_ADD_TC(tp, sendrecv_basic);
|
2018-11-06 20:55:04 +03:00
|
|
|
ATF_TP_ADD_TC(tp, sendrecv_rerror);
|
2018-08-21 13:41:00 +03:00
|
|
|
|
|
|
|
return atf_no_error();
|
|
|
|
}
|