summaryrefslogtreecommitdiff
path: root/regress/sys/dev/fdesc/confuse.c
blob: e61b242a844a10dc4a3331fc0e2af5ca1d633d0b (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
40
41
42
43
/*	$OpenBSD: confuse.c,v 1.1 2005/12/07 01:43:39 pedro Exp $	*/

/*
 * Written by Artur Grabowski <art@openbsd.org> in 2002. Public Domain.
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <err.h>
#include <fcntl.h>
#include <errno.h>

int
main(int argc, char *argv[])
{
	char fname[64];
	int fd, newfd;

	if ((fd = open("/dev/null", O_RDONLY)) < 0)
		err(1, "open(/dev/null)");

	/* Try to confuse fdescfs by making it open into itself. */
	close(fd);

	snprintf(fname, sizeof(fname), "/dev/fd/%d", fd);

	if ((newfd = open(fname, O_RDONLY)) == fd)
		errx(1, "open of %s to %d succeeded, beware.", fname, fd);

	if (newfd >= 0)
		errx(1, "open(%s) gave us the unexpected %d", fname, fd);

	if (errno == ENOENT)
		err(1, "open(%s)", fname);

	if (errno == ENXIO)
		errx(1, "no support for fdesc in kernel"); 

	warn("errno was (%d)", errno);

	return 0;
}