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