diff options
-rw-r--r-- | regress/sys/kern/dup2_self/Makefile | 5 | ||||
-rw-r--r-- | regress/sys/kern/dup2_self/dup2_self.c | 43 |
2 files changed, 48 insertions, 0 deletions
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 <bsd.regress.mk> 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 <art@openbsd.org> 2002 Public Domain. + */ +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include <err.h> +#include <fcntl.h> + +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; +} |