1 /* Test case written by Bharat Joshi */
3 __RCSID("$NetBSD: t_fifo.c,v 1.2 2017/01/10 22:36:29 christos Exp $");
22 #define FIFO_FILE_PATH "./fifo_file"
23 #define NUM_MESSAGES 20
25 #define MESSAGE "I am fine"
27 static int verbose = 0;
32 * Function that runs in child context and opens and write to the FIFO.
40 char message[MSG_SIZE] = MESSAGE;
41 static const struct timespec ts = { 0, 10000 };
43 /* Open the fifo in write-mode */
45 fd = open(FIFO_FILE_PATH, O_WRONLY, 0);
49 err(1, "Child: can't open fifo in write mode");
54 for (count = 0; count < NUM_MESSAGES; count++) {
55 rv = write(fd, message, MSG_SIZE);
57 warn("Child: Failed to write");
61 warnx("Child: wrote only %zd", rv);
67 printf("Child: Closed the fifo file\n");
75 * Called when a sigchild is delivered
78 sigchild_handler(int signo)
81 if (signo == SIGCHLD) {
82 printf("Got sigchild\n");
84 printf("Got %d signal\n", signo);
97 size_t buf_size = MSG_SIZE;
99 struct sigaction action;
100 static const struct timespec ts = { 0, 500000000 };
102 /* Catch sigchild Signal */
103 memset(&action, 0, sizeof(action));
104 action.sa_handler = sigchild_handler;
105 sigemptyset(&action.sa_mask);
107 if (sigaction(SIGCHLD, &action, NULL) == -1)
110 (void)unlink(FIFO_FILE_PATH);
111 /* First create a fifo */
112 if (mkfifo(FIFO_FILE_PATH, S_IRUSR | S_IWUSR) == -1)
115 switch ((pid = fork())) {
119 /* Open the file in write mode so that subsequent read
120 * from parent side does not block the parent..
122 if ((fd = open(FIFO_FILE_PATH, O_WRONLY, 0)) == -1)
123 err(1, "failed to open fifo");
134 printf("Child pid is %d\n", pid );
140 if ((fd = open(FIFO_FILE_PATH, O_RDONLY, 0)) == -1) {
144 err(1, "Failed to open the fifo in read mode");
146 /* Read mode is opened */
151 nanosleep(&ts, NULL);
153 printf("Was sleeping...\n");
158 rv = read(fd, buf, buf_size);
161 warn("Failed to read");
162 if (errno == EINTR) {
164 printf("Parent interrupted, "
176 printf("Writers have closed, looks like we "
184 printf("Received %zd bytes message '%s'\n", rv, buf);
192 printf("We are done.. now reap the child");
197 while (waitpid(pid, &status, 0) == -1)
198 if (errno != EINTR) {
199 warn("Failed to reap the child");
204 printf("We are done completely\n");
211 ATF_TC(parent_child);
213 ATF_TC_HEAD(parent_child, tc)
215 atf_tc_set_md_var(tc, "descr", "Checks that when a fifo is shared "
216 "between a reader parent and a writer child, that read will "
217 "return EOF, and not get stuck after the child exits");
220 ATF_TC_BODY(parent_child, tc)
222 ATF_REQUIRE(run() == 0);
227 ATF_TP_ADD_TC(tp, parent_child);
229 return atf_no_error();