Path: blob/main/tools/test/stress2/testcases/socket/socket.c
39566 views
/*-1* Copyright (c) 2008 Peter Holm <[email protected]>2* All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9* 2. Redistributions in binary form must reproduce the above copyright10* notice, this list of conditions and the following disclaimer in the11* documentation and/or other materials provided with the distribution.12*13* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND14* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE15* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE16* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE17* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL18* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS19* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)20* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT21* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY22* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF23* SUCH DAMAGE.24*25*/2627#include <sys/param.h>28#include <sys/socket.h>29#include <netinet/in.h>30#include <err.h>31#include <errno.h>32#include <netdb.h>33#include <signal.h>34#include <stdio.h>35#include <stdlib.h>36#include <string.h>37#include <unistd.h>3839#include "stress.h"4041#define NB (400 * 1024 * 1024)4243static int port;44static int bufsize;45static int sv[2];4647static void48reader(void) {49int n, *buf;5051if ((buf = malloc(bufsize)) == NULL)52err(1, "malloc(%d), %s:%d", bufsize, __FILE__, __LINE__);53while (done_testing == 0) {54if ((n = read(sv[0], buf, bufsize)) < 0)55err(1, "read(), %s:%d", __FILE__, __LINE__);56if (n == 0) break;57}58close(sv[0]);59return;60}6162static void63writer(void) {64int i, *buf;6566if ((buf = malloc(bufsize)) == NULL)67err(1, "malloc(%d), %s:%d", bufsize, __FILE__, __LINE__);68for (i = 0; i < bufsize / (int)sizeof(int); i++)69buf[i] = i;7071for (;;) {72for (i = 0; i < NB; i+= bufsize) {73if (write(sv[1], buf, bufsize) < 0) {74if (errno == EPIPE)75return;76err(1, "write(%d), %s:%d", sv[1],77__FILE__, __LINE__);78}79}80}81return;82}8384int85setup(int nb)86{87port = 12340 + nb;88bufsize = 2 << random_int(2, 12);89return (0);90}9192void93cleanup(void)94{95}9697int98test(void)99{100pid_t pid;101102if (socketpair(PF_UNIX, SOCK_STREAM, 0, sv) != 0)103err(1, "socketpair()");104if ((pid = fork()) == 0) {105writer();106_exit(EXIT_SUCCESS);107108} else if (pid > 0) {109reader();110kill(pid, SIGINT);111} else112err(1, "fork(), %s:%d", __FILE__, __LINE__);113114return (0);115}116117118