summaryrefslogtreecommitdiff
path: root/regress/sys/kern/fcntl_dup/fcntl_dup.c
blob: 88469c611f97d282ab5db6e45a9585cb274bcf90 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/*	$OpenBSD: fcntl_dup.c,v 1.2 2003/07/31 21:48:08 deraadt 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 argc, char *argv[])
{
	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 = fcntl(fd1, F_DUPFD)) < 0)
		err(1, "fcntl(F_DUPFD)");

	/* Test 2: Was close-on-exec cleared? */
	if (fcntl(fd2, F_GETFD) != 0)
		errx(1, "fcntl(F_DUPFD) didn't clear close-on-exec");

	return 0;
}