#include #include #include #include #include #include static void fatal (register char const *const msg) { fputs (msg, stderr); fputc ('\n', stderr); exit (2); } static void fatal_perror (register char const *const msg) { perror (msg); exit (2); } static int get_fd_flags (register int const fd) { register int result; if ((result = fcntl (fd, F_GETFL)) == -1) fatal_perror ("Error in fcntl()"); return result; } static void set_fd_flags (register int const fd, register int const flags) { if (fcntl (fd, F_SETFL, flags) == -1) fatal_perror ("Error in fcntl()"); } int main (void) { register int fd; register int flags; { enum { fdmode = 0600 }; auto char fd_path[80]; sprintf (fd_path, "/tmp/pid-%u-up.mq", getpid ()); #if defined(TRY_FIFO) if (mkfifo (fd_path, fdmode) == -1) fatal_perror ("Error in mkfifo()"); enum { fdflags = O_RDWR | O_NONBLOCK }; #else enum { fdflags = O_CREAT | O_EXCL | O_RDWR | O_NONBLOCK }; #endif if ((fd = open (fd_path, fdflags, fdmode)) == -1) fatal_perror ("Error in open()"); if (unlink (fd_path) == -1) fatal_perror ("Error in unlink()"); } if (((flags = get_fd_flags (fd)) & O_NONBLOCK) == 0) fatal ("O_NONBLOCK magically unset before fork"); if (!fork ()) { /* child */ set_fd_flags (fd, 0); sleep (5); } else { /* parent */ sleep (1); if (((flags = get_fd_flags (fd)) & O_NONBLOCK) == 0) fatal ("O_NONBLOCK magically unset in parent"); } return 0; }