1*41fbaed0Stron /* $NetBSD: fifo_open.c,v 1.1.1.1 2009/06/23 10:08:59 tron Exp $ */
2*41fbaed0Stron
3*41fbaed0Stron /*++
4*41fbaed0Stron /* NAME
5*41fbaed0Stron /* fifo_open 1
6*41fbaed0Stron /* SUMMARY
7*41fbaed0Stron /* fifo client test program
8*41fbaed0Stron /* SYNOPSIS
9*41fbaed0Stron /* fifo_open
10*41fbaed0Stron /* DESCRIPTION
11*41fbaed0Stron /* fifo_open creates a FIFO, then attempts to open it for writing
12*41fbaed0Stron /* with non-blocking mode enabled. According to the POSIX standard
13*41fbaed0Stron /* the open should succeed.
14*41fbaed0Stron /* DIAGNOSTICS
15*41fbaed0Stron /* Problems are reported to the standard error stream.
16*41fbaed0Stron /* LICENSE
17*41fbaed0Stron /* .ad
18*41fbaed0Stron /* .fi
19*41fbaed0Stron /* The Secure Mailer license must be distributed with this software.
20*41fbaed0Stron /* AUTHOR(S)
21*41fbaed0Stron /* Wietse Venema
22*41fbaed0Stron /* IBM T.J. Watson Research
23*41fbaed0Stron /* P.O. Box 704
24*41fbaed0Stron /* Yorktown Heights, NY 10598, USA
25*41fbaed0Stron /*--*/
26*41fbaed0Stron
27*41fbaed0Stron #include <sys/stat.h>
28*41fbaed0Stron #include <stdio.h>
29*41fbaed0Stron #include <fcntl.h>
30*41fbaed0Stron #include <signal.h>
31*41fbaed0Stron #include <unistd.h>
32*41fbaed0Stron #include <stdlib.h>
33*41fbaed0Stron
34*41fbaed0Stron #define FIFO_PATH "test-fifo"
35*41fbaed0Stron #define perrorexit(s) { perror(s); exit(1); }
36*41fbaed0Stron
cleanup(void)37*41fbaed0Stron static void cleanup(void)
38*41fbaed0Stron {
39*41fbaed0Stron printf("Removing fifo %s...\n", FIFO_PATH);
40*41fbaed0Stron if (unlink(FIFO_PATH))
41*41fbaed0Stron perrorexit("unlink");
42*41fbaed0Stron printf("Done.\n");
43*41fbaed0Stron }
44*41fbaed0Stron
stuck(int unused_sig)45*41fbaed0Stron static void stuck(int unused_sig)
46*41fbaed0Stron {
47*41fbaed0Stron printf("Non-blocking, write-only open of FIFO blocked\n");
48*41fbaed0Stron cleanup();
49*41fbaed0Stron exit(1);
50*41fbaed0Stron }
51*41fbaed0Stron
main(int unused_argc,char ** unused_argv)52*41fbaed0Stron int main(int unused_argc, char **unused_argv)
53*41fbaed0Stron {
54*41fbaed0Stron (void) unlink(FIFO_PATH);
55*41fbaed0Stron printf("Creating fifo %s...\n", FIFO_PATH);
56*41fbaed0Stron if (mkfifo(FIFO_PATH, 0600) < 0)
57*41fbaed0Stron perrorexit("mkfifo");
58*41fbaed0Stron signal(SIGALRM, stuck);
59*41fbaed0Stron alarm(5);
60*41fbaed0Stron printf("Opening fifo %s, non-blocking, write-only mode...\n", FIFO_PATH);
61*41fbaed0Stron if (open(FIFO_PATH, O_WRONLY | O_NONBLOCK, 0) < 0) {
62*41fbaed0Stron perror("open");
63*41fbaed0Stron cleanup();
64*41fbaed0Stron exit(1);
65*41fbaed0Stron }
66*41fbaed0Stron printf("Non-blocking, write-only open of FIFO succeeded\n");
67*41fbaed0Stron cleanup();
68*41fbaed0Stron exit(0);
69*41fbaed0Stron }
70