From b984825aa98dfbc10222d5d5f8a1f66c83672700 Mon Sep 17 00:00:00 2001 From: Artur Grabowski Date: Fri, 8 Feb 2002 18:47:47 +0000 Subject: Test an obscure case of dup2 behavior. --- regress/sys/kern/dup2_self/Makefile | 5 ++++ regress/sys/kern/dup2_self/dup2_self.c | 43 ++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 regress/sys/kern/dup2_self/Makefile create mode 100644 regress/sys/kern/dup2_self/dup2_self.c (limited to 'regress/sys') diff --git a/regress/sys/kern/dup2_self/Makefile b/regress/sys/kern/dup2_self/Makefile new file mode 100644 index 00000000000..938a41a6f93 --- /dev/null +++ b/regress/sys/kern/dup2_self/Makefile @@ -0,0 +1,5 @@ +# $OpenBSD: Makefile,v 1.1 2002/02/08 18:47:46 art Exp $ + +PROG= dup2_self + +.include diff --git a/regress/sys/kern/dup2_self/dup2_self.c b/regress/sys/kern/dup2_self/dup2_self.c new file mode 100644 index 00000000000..3b0a3936ae7 --- /dev/null +++ b/regress/sys/kern/dup2_self/dup2_self.c @@ -0,0 +1,43 @@ +/* $OpenBSD: dup2_self.c,v 1.1 2002/02/08 18:47:46 art Exp $ */ +/* + * Written by Artur Grabowski 2002 Public Domain. + */ +#include +#include +#include +#include +#include + +int +main() +{ + int orgfd, fd1, fd2; + char temp[] = "/tmp/dup2XXXXXXXXX"; + + if ((orgfd = mkstemp(temp)) < 0) + err(1, "mkstemp"); + remove(temp); + + if (ftruncate(orgfd, 1024) != 0) + err(1, "ftruncate"); + + if ((fd1 = dup(orgfd)) < 0) + err(1, "dup"); + + /* Set close-on-exec */ + if (fcntl(fd1, F_SETFD, 1) != 0) + err(1, "fcntl(F_SETFD)"); + + if ((fd2 = dup2(fd1, fd1)) < 0) + err(1, "dup2"); + + /* Test 1: Do we get the right fd? */ + if (fd2 != fd1) + errx(1, "dup2 didn't give us the right fd"); + + /* Test 2: Was close-on-exec cleared? */ + if (fcntl(fd2, F_GETFD) == 0) + errx(1, "dup2 cleared close-on-exec"); + + return 0; +} -- cgit v1.2.3