#define _GNU_SOURCE #include #include #include #include #include #include #include #include #include #include #include #define FIFO_PATH "/tmp/myfifo" int main () { struct stat s; int fd; if (mkfifo (FIFO_PATH, S_IRUSR | S_IWUSR | S_IWGRP) < 0 && errno != EEXIST) { perror ("mkfifo"); exit (1); } fd = open (FIFO_PATH, O_PATH); if (fd < 0) { perror ("open"); exit (1); } printf ("The following calls should fail with EBADF:\n"); errno = 0; if (read (fd, NULL, 0) < 0 && errno == EBADF) printf ("read: OK\n"); else perror ("read"); errno = 0; if (write (fd, NULL, 0) < 0 && errno == EBADF) printf ("write: OK\n"); else perror ("write"); errno = 0; if (fchmod (fd, 0) < 0 && errno == EBADF) printf ("fchmod: OK\n"); else perror ("fchmod"); errno = 0; if (fchown (fd, -1, -1) < 0 && errno == EBADF) printf ("fchown: OK\n"); else perror ("fchown"); errno = 0; if (ioctl (fd, FIONBIO, NULL) < 0 && errno == EBADF) printf ("ioctl: OK\n"); else perror ("ioctl"); errno = 0; if (fgetxattr (fd, "", NULL, 0) < 0 && errno == EBADF) printf ("fgetxattr: OK\n"); else perror ("fgetxattr"); errno = 0; if (mmap (NULL, 1, 0, MAP_SHARED, fd, 0) == MAP_FAILED && errno == EBADF) printf ("mmap: OK\n"); else perror ("mmap"); printf ("\nThe following calls should succeed:\n"); if (fstat (fd, &s) < 0) perror ("fstat"); else printf ("fstat: OK\n"); struct statfs st; if (fstatfs (fd, &st) < 0) perror ("fstatfs"); else printf ("fstatfs: OK\n"); if (fcntl (fd, F_DUPFD, 0) < 0) perror ("fcntl_dup"); else printf ("fcntl_dup: OK\n"); int flags = fcntl (fd, F_GETFL); if (flags < 0) perror ("fcntl_getfl"); else printf ("fcntl_getfl: OK\n"); if (flags >= 0) { if (fcntl (fd, F_SETFL, flags) < 0) perror ("fcntl_setfl"); else printf ("fcntl_setfl: OK\n"); } flags = fcntl (fd, F_GETFD); if (flags < 0) perror ("fcntl_getfd"); else printf ("fcntl_getfd: OK\n"); if (flags >= 0) { if (fcntl (fd, F_SETFD, flags) < 0) perror ("fcntl_setfd"); else printf ("fcntl_setfd: OK\n"); } if (close (fd) < 0) perror ("close"); else printf ("close: OK\n"); }