xref: /openbsd/regress/sys/dev/fdesc/confuse.c (revision 898184e3)
1 /*	$OpenBSD: confuse.c,v 1.1 2005/12/07 01:43:39 pedro Exp $	*/
2 
3 /*
4  * Written by Artur Grabowski <art@openbsd.org> in 2002. Public Domain.
5  */
6 
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <unistd.h>
10 #include <err.h>
11 #include <fcntl.h>
12 #include <errno.h>
13 
14 int
15 main(int argc, char *argv[])
16 {
17 	char fname[64];
18 	int fd, newfd;
19 
20 	if ((fd = open("/dev/null", O_RDONLY)) < 0)
21 		err(1, "open(/dev/null)");
22 
23 	/* Try to confuse fdescfs by making it open into itself. */
24 	close(fd);
25 
26 	snprintf(fname, sizeof(fname), "/dev/fd/%d", fd);
27 
28 	if ((newfd = open(fname, O_RDONLY)) == fd)
29 		errx(1, "open of %s to %d succeeded, beware.", fname, fd);
30 
31 	if (newfd >= 0)
32 		errx(1, "open(%s) gave us the unexpected %d", fname, fd);
33 
34 	if (errno == ENOENT)
35 		err(1, "open(%s)", fname);
36 
37 	if (errno == ENXIO)
38 		errx(1, "no support for fdesc in kernel");
39 
40 	warn("errno was (%d)", errno);
41 
42 	return 0;
43 }
44