From cca9abc62ac63f880d029e335788ed34fbd2093c Mon Sep 17 00:00:00 2001 From: Artur Grabowski Date: Mon, 11 Feb 2002 15:40:32 +0000 Subject: Test some obscrue behavior of accept. --- regress/sys/kern/accept/Makefile | 5 ++ regress/sys/kern/accept/accept.c | 107 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 112 insertions(+) create mode 100644 regress/sys/kern/accept/Makefile create mode 100644 regress/sys/kern/accept/accept.c (limited to 'regress') diff --git a/regress/sys/kern/accept/Makefile b/regress/sys/kern/accept/Makefile new file mode 100644 index 00000000000..58a2d63605e --- /dev/null +++ b/regress/sys/kern/accept/Makefile @@ -0,0 +1,5 @@ +# $OpenBSD: Makefile,v 1.1 2002/02/11 15:40:31 art Exp $ + +PROG= accept + +.include diff --git a/regress/sys/kern/accept/accept.c b/regress/sys/kern/accept/accept.c new file mode 100644 index 00000000000..2974142733c --- /dev/null +++ b/regress/sys/kern/accept/accept.c @@ -0,0 +1,107 @@ +/* $OpenBSD: accept.c,v 1.1 2002/02/11 15:40:31 art Exp $ */ +/* + * Written by Artur Grabowski , 2002 Public Domain. + */ +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +#define SOCK_NAME "test-sock" + +int child(void); + +int +main(argc, argv) + int argc; + char *argv[]; +{ + int listensock, sock; + struct sockaddr_un sun, csun; + int csunlen; + int fd, lastfd; + int status; + int ischild; + + /* + * Create the listen socket. + */ + if ((listensock = socket(PF_LOCAL, SOCK_STREAM, 0)) == -1) + err(1, "socket"); + + unlink(SOCK_NAME); + memset(&sun, 0, sizeof(sun)); + sun.sun_family = AF_LOCAL; + strcpy(sun.sun_path, SOCK_NAME); + sun.sun_len = SUN_LEN(&sun); + + + if (bind(listensock, (struct sockaddr *)&sun, sizeof(sun)) == -1) + err(1, "bind"); + + if (listen(listensock, 1) == -1) + err(1, "listen"); + + switch (fork()) { + case -1: + err(1, "fork"); + case 0: + return child(); + } + + while ((fd = open("/dev/null", O_RDONLY)) >= 0) + lastfd = fd; + + switch (fork()) { + case -1: + err(1, "fork"); + case 0: + ischild = 1; + close(lastfd); /* Close one fd so that accept can succeed */ + sleep(2); /* sleep a bit so that we're the second to accept */ + } + + sock = accept(listensock, (struct sockaddr *)&csun, &csunlen); + + if (!ischild && sock >= 0) + errx(1, "accept succeeded in parent"); + if (ischild && sock < 0) + err(1, "accept failed in child"); + + while (!ischild && wait4(-1, &status, 0, NULL) > 0) + ; + + return (0); +} + +int +child() +{ + int i, fd, sock; + struct sockaddr_un sun; + + /* + * Create socket and connect to the receiver. + */ + if ((sock = socket(PF_LOCAL, SOCK_STREAM, 0)) == -1) + errx(1, "child socket"); + + (void) memset(&sun, 0, sizeof(sun)); + sun.sun_family = AF_LOCAL; + (void) strcpy(sun.sun_path, SOCK_NAME); + sun.sun_len = SUN_LEN(&sun); + + if (connect(sock, (struct sockaddr *)&sun, sizeof(sun)) == -1) + err(1, "child connect"); + + return (0); +} -- cgit v1.2.3