1 /* Test case written by Bharat Joshi */
3 __RCSID("$NetBSD: t_fifo.c,v 1.1 2011/12/21 00:17:07 christos Exp $");
21 #define FIFO_FILE_PATH "./fifo_file"
22 #define NUM_MESSAGES 20
24 #define MESSAGE "I am fine"
26 static int verbose
= 0;
31 * Function that runs in child context and opens and write to the FIFO.
39 char message
[MSG_SIZE
] = MESSAGE
;
40 static const struct timespec ts
= { 0, 10000 };
42 /* Open the fifo in write-mode */
44 fd
= open(FIFO_FILE_PATH
, O_WRONLY
, 0);
48 err(1, "Child: can't open fifo in write mode");
53 for (count
= 0; count
< NUM_MESSAGES
; count
++) {
54 rv
= write(fd
, message
, MSG_SIZE
);
56 warn("Child: Failed to write");
60 warnx("Child: wrote only %zd", rv
);
66 printf("Child: Closed the fifo file\n");
74 * Called when a sigchild is delivered
77 sigchild_handler(int signo
)
80 if (signo
== SIGCHLD
) {
81 printf("Got sigchild\n");
83 printf("Got %d signal\n", signo
);
96 size_t buf_size
= MSG_SIZE
;
98 struct sigaction action
;
99 static const struct timespec ts
= { 0, 500000000 };
101 /* Catch sigchild Signal */
102 memset(&action
, 0, sizeof(action
));
103 action
.sa_handler
= sigchild_handler
;
104 sigemptyset(&action
.sa_mask
);
106 if (sigaction(SIGCHLD
, &action
, NULL
) == -1)
109 (void)unlink(FIFO_FILE_PATH
);
110 /* First create a fifo */
111 if (mkfifo(FIFO_FILE_PATH
, S_IRUSR
| S_IWUSR
) == -1)
114 switch ((pid
= fork())) {
118 /* Open the file in write mode so that subsequent read
119 * from parent side does not block the parent..
121 if ((fd
= open(FIFO_FILE_PATH
, O_WRONLY
, 0)) == -1)
122 err(1, "failed to open fifo");
133 printf("Child pid is %d\n", pid
);
139 if ((fd
= open(FIFO_FILE_PATH
, O_RDONLY
, 0)) == -1) {
143 err(1, "Failed to open the fifo in read mode");
145 /* Read mode is opened */
150 nanosleep(&ts
, NULL
);
152 printf("Was sleeping...\n");
157 rv
= read(fd
, buf
, buf_size
);
160 warn("Failed to read");
161 if (errno
== EINTR
) {
163 printf("Parent interrupted, "
175 printf("Writers have closed, looks like we "
183 printf("Received %zd bytes message '%s'\n", rv
, buf
);
191 printf("We are done.. now reap the child");
196 while (waitpid(pid
, &status
, 0) == -1)
197 if (errno
!= EINTR
) {
198 warn("Failed to reap the child");
203 printf("We are done completely\n");
210 ATF_TC(parent_child
);
212 ATF_TC_HEAD(parent_child
, tc
)
214 atf_tc_set_md_var(tc
, "descr", "Checks that when a fifo is shared "
215 "between a reader parent and a writer child, that read will "
216 "return EOF, and not get stuck after the child exits");
219 ATF_TC_BODY(parent_child
, tc
)
221 ATF_REQUIRE(run() == 0);
226 ATF_TP_ADD_TC(tp
, parent_child
);
228 return atf_no_error();