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