xref: /openbsd/regress/sys/kern/fchdir/fchdir.c (revision 61e7dd5d)
1 /*	$OpenBSD: fchdir.c,v 1.2 2017/03/08 18:42:28 deraadt Exp $ */
2 
3 /*
4  *	Written by Philip Guenther <guenther@openbsd.org> 2011 Public Domain.
5  *
6  *	Verify errno returns from fchdir()
7  */
8 #include <sys/types.h>
9 #include <sys/socket.h>
10 #include <err.h>
11 #include <errno.h>
12 #include <fcntl.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <unistd.h>
16 
17 int
main(int argc,char * argv[])18 main(int argc, char *argv[])
19 {
20 	int fds[2];
21 	int fd;
22 
23 	if ((fd = open("/etc/passwd", O_RDONLY)) == -1)
24 		err(1, "open");
25 	if (fchdir(fd) == 0)
26 		errx(1, "fchdir file succeeded");
27 	if (errno != ENOTDIR)
28 		err(1, "fchdir file: wrong errno");
29 	close(fd);
30 
31 	if (pipe(fds))
32 		err(1, "pipe");
33 	if (fchdir(fds[0]) == 0)
34 		errx(1, "fchdir pipe succeeded");
35 	if (errno != ENOTDIR)
36 		err(1, "fchdir pipe: wrong errno");
37 	close(fds[0]);
38 	close(fds[1]);
39 
40 	if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
41 		err(1, "socket");
42 	if (fchdir(fd) == 0)
43 		errx(1, "fchdir socket succeeded");
44 	if (errno != ENOTDIR)
45 		err(1, "fchdir socket: wrong errno");
46 	close(fd);
47 
48 	if (fchdir(fd) == 0)
49 		errx(1, "fchdir bad fd succeeded");
50 	if (errno != EBADF)
51 		err(1, "fchdir bad fd: wrong errno");
52 
53 	return 0;
54 }
55