t_fifo.c revision 1.1.4.2 1 1.1.4.2 yamt /* Test case written by Bharat Joshi */
2 1.1.4.2 yamt #include <sys/cdefs.h>
3 1.1.4.2 yamt __RCSID("$NetBSD: t_fifo.c,v 1.1.4.2 2012/04/17 00:09:03 yamt Exp $");
4 1.1.4.2 yamt
5 1.1.4.2 yamt #include <sys/types.h>
6 1.1.4.2 yamt #include <sys/wait.h>
7 1.1.4.2 yamt
8 1.1.4.2 yamt #include <stdio.h>
9 1.1.4.2 yamt #include <stdlib.h>
10 1.1.4.2 yamt #include <unistd.h>
11 1.1.4.2 yamt #include <fcntl.h>
12 1.1.4.2 yamt #include <errno.h>
13 1.1.4.2 yamt #include <string.h>
14 1.1.4.2 yamt #include <err.h>
15 1.1.4.2 yamt #include <signal.h>
16 1.1.4.2 yamt
17 1.1.4.2 yamt #ifndef STANDALONE
18 1.1.4.2 yamt #include <atf-c.h>
19 1.1.4.2 yamt #endif
20 1.1.4.2 yamt
21 1.1.4.2 yamt #define FIFO_FILE_PATH "./fifo_file"
22 1.1.4.2 yamt #define NUM_MESSAGES 20
23 1.1.4.2 yamt #define MSG_SIZE 240
24 1.1.4.2 yamt #define MESSAGE "I am fine"
25 1.1.4.2 yamt
26 1.1.4.2 yamt static int verbose = 0;
27 1.1.4.2 yamt
28 1.1.4.2 yamt /*
29 1.1.4.2 yamt * child_writer
30 1.1.4.2 yamt *
31 1.1.4.2 yamt * Function that runs in child context and opens and write to the FIFO.
32 1.1.4.2 yamt */
33 1.1.4.2 yamt static void
34 1.1.4.2 yamt child_writer(void)
35 1.1.4.2 yamt {
36 1.1.4.2 yamt ssize_t rv;
37 1.1.4.2 yamt int fd;
38 1.1.4.2 yamt size_t count;
39 1.1.4.2 yamt char message[MSG_SIZE] = MESSAGE;
40 1.1.4.2 yamt static const struct timespec ts = { 0, 10000 };
41 1.1.4.2 yamt
42 1.1.4.2 yamt /* Open the fifo in write-mode */
43 1.1.4.2 yamt for (;;) {
44 1.1.4.2 yamt fd = open(FIFO_FILE_PATH, O_WRONLY, 0);
45 1.1.4.2 yamt if (fd == -1) {
46 1.1.4.2 yamt if (errno == EINTR)
47 1.1.4.2 yamt continue;
48 1.1.4.2 yamt err(1, "Child: can't open fifo in write mode");
49 1.1.4.2 yamt }
50 1.1.4.2 yamt break;
51 1.1.4.2 yamt }
52 1.1.4.2 yamt
53 1.1.4.2 yamt for (count = 0; count < NUM_MESSAGES; count++) {
54 1.1.4.2 yamt rv = write(fd, message, MSG_SIZE);
55 1.1.4.2 yamt if (rv == -1) {
56 1.1.4.2 yamt warn("Child: Failed to write");
57 1.1.4.2 yamt break;
58 1.1.4.2 yamt }
59 1.1.4.2 yamt if (rv != MSG_SIZE)
60 1.1.4.2 yamt warnx("Child: wrote only %zd", rv);
61 1.1.4.2 yamt nanosleep(&ts, NULL);
62 1.1.4.2 yamt }
63 1.1.4.2 yamt
64 1.1.4.2 yamt close(fd);
65 1.1.4.2 yamt if (verbose) {
66 1.1.4.2 yamt printf("Child: Closed the fifo file\n");
67 1.1.4.2 yamt fflush(stdout);
68 1.1.4.2 yamt }
69 1.1.4.2 yamt }
70 1.1.4.2 yamt
71 1.1.4.2 yamt /*
72 1.1.4.2 yamt * _sigchild_handler
73 1.1.4.2 yamt *
74 1.1.4.2 yamt * Called when a sigchild is delivered
75 1.1.4.2 yamt */
76 1.1.4.2 yamt static void
77 1.1.4.2 yamt sigchild_handler(int signo)
78 1.1.4.2 yamt {
79 1.1.4.2 yamt if (verbose) {
80 1.1.4.2 yamt if (signo == SIGCHLD) {
81 1.1.4.2 yamt printf("Got sigchild\n");
82 1.1.4.2 yamt } else {
83 1.1.4.2 yamt printf("Got %d signal\n", signo);
84 1.1.4.2 yamt }
85 1.1.4.2 yamt fflush(stdout);
86 1.1.4.2 yamt }
87 1.1.4.2 yamt
88 1.1.4.2 yamt }
89 1.1.4.2 yamt
90 1.1.4.2 yamt static int
91 1.1.4.2 yamt run(void)
92 1.1.4.2 yamt {
93 1.1.4.2 yamt pid_t pid;
94 1.1.4.2 yamt ssize_t rv;
95 1.1.4.2 yamt int fd, status;
96 1.1.4.2 yamt size_t buf_size = MSG_SIZE;
97 1.1.4.2 yamt char buf[MSG_SIZE];
98 1.1.4.2 yamt struct sigaction action;
99 1.1.4.2 yamt static const struct timespec ts = { 0, 500000000 };
100 1.1.4.2 yamt
101 1.1.4.2 yamt /* Catch sigchild Signal */
102 1.1.4.2 yamt memset(&action, 0, sizeof(action));
103 1.1.4.2 yamt action.sa_handler = sigchild_handler;
104 1.1.4.2 yamt sigemptyset(&action.sa_mask);
105 1.1.4.2 yamt
106 1.1.4.2 yamt if (sigaction(SIGCHLD, &action, NULL) == -1)
107 1.1.4.2 yamt err(1, "sigaction");
108 1.1.4.2 yamt
109 1.1.4.2 yamt (void)unlink(FIFO_FILE_PATH);
110 1.1.4.2 yamt /* First create a fifo */
111 1.1.4.2 yamt if (mkfifo(FIFO_FILE_PATH, S_IRUSR | S_IWUSR) == -1)
112 1.1.4.2 yamt err(1, "mkfifo");
113 1.1.4.2 yamt
114 1.1.4.2 yamt switch ((pid = fork())) {
115 1.1.4.2 yamt case -1:
116 1.1.4.2 yamt err(1, "fork");
117 1.1.4.2 yamt case 0:
118 1.1.4.2 yamt /* Open the file in write mode so that subsequent read
119 1.1.4.2 yamt * from parent side does not block the parent..
120 1.1.4.2 yamt */
121 1.1.4.2 yamt if ((fd = open(FIFO_FILE_PATH, O_WRONLY, 0)) == -1)
122 1.1.4.2 yamt err(1, "failed to open fifo");
123 1.1.4.2 yamt
124 1.1.4.2 yamt /* In child */
125 1.1.4.2 yamt child_writer();
126 1.1.4.2 yamt return 0;
127 1.1.4.2 yamt
128 1.1.4.2 yamt default:
129 1.1.4.2 yamt break;
130 1.1.4.2 yamt }
131 1.1.4.2 yamt
132 1.1.4.2 yamt if (verbose) {
133 1.1.4.2 yamt printf("Child pid is %d\n", pid );
134 1.1.4.2 yamt fflush(stdout);
135 1.1.4.2 yamt }
136 1.1.4.2 yamt
137 1.1.4.2 yamt /* In parent */
138 1.1.4.2 yamt for (;;) {
139 1.1.4.2 yamt if ((fd = open(FIFO_FILE_PATH, O_RDONLY, 0)) == -1) {
140 1.1.4.2 yamt if (errno == EINTR)
141 1.1.4.2 yamt continue;
142 1.1.4.2 yamt else
143 1.1.4.2 yamt err(1, "Failed to open the fifo in read mode");
144 1.1.4.2 yamt }
145 1.1.4.2 yamt /* Read mode is opened */
146 1.1.4.2 yamt break;
147 1.1.4.2 yamt
148 1.1.4.2 yamt }
149 1.1.4.2 yamt
150 1.1.4.2 yamt nanosleep(&ts, NULL);
151 1.1.4.2 yamt if (verbose) {
152 1.1.4.2 yamt printf("Was sleeping...\n");
153 1.1.4.2 yamt fflush(stdout);
154 1.1.4.2 yamt }
155 1.1.4.2 yamt
156 1.1.4.2 yamt for (;;) {
157 1.1.4.2 yamt rv = read(fd, buf, buf_size);
158 1.1.4.2 yamt
159 1.1.4.2 yamt if (rv == -1) {
160 1.1.4.2 yamt warn("Failed to read");
161 1.1.4.2 yamt if (errno == EINTR) {
162 1.1.4.2 yamt if (verbose) {
163 1.1.4.2 yamt printf("Parent interrupted, "
164 1.1.4.2 yamt "continuing...\n");
165 1.1.4.2 yamt fflush(stdout);
166 1.1.4.2 yamt }
167 1.1.4.2 yamt continue;
168 1.1.4.2 yamt }
169 1.1.4.2 yamt
170 1.1.4.2 yamt break;
171 1.1.4.2 yamt }
172 1.1.4.2 yamt
173 1.1.4.2 yamt if (rv == 0) {
174 1.1.4.2 yamt if (verbose) {
175 1.1.4.2 yamt printf("Writers have closed, looks like we "
176 1.1.4.2 yamt "are done\n");
177 1.1.4.2 yamt fflush(stdout);
178 1.1.4.2 yamt }
179 1.1.4.2 yamt break;
180 1.1.4.2 yamt }
181 1.1.4.2 yamt
182 1.1.4.2 yamt if (verbose) {
183 1.1.4.2 yamt printf("Received %zd bytes message '%s'\n", rv, buf);
184 1.1.4.2 yamt fflush(stdout);
185 1.1.4.2 yamt }
186 1.1.4.2 yamt }
187 1.1.4.2 yamt
188 1.1.4.2 yamt close(fd);
189 1.1.4.2 yamt
190 1.1.4.2 yamt if (verbose) {
191 1.1.4.2 yamt printf("We are done.. now reap the child");
192 1.1.4.2 yamt fflush(stdout);
193 1.1.4.2 yamt }
194 1.1.4.2 yamt
195 1.1.4.2 yamt // Read the child...
196 1.1.4.2 yamt while (waitpid(pid, &status, 0) == -1)
197 1.1.4.2 yamt if (errno != EINTR) {
198 1.1.4.2 yamt warn("Failed to reap the child");
199 1.1.4.2 yamt return 1;
200 1.1.4.2 yamt }
201 1.1.4.2 yamt
202 1.1.4.2 yamt if (verbose) {
203 1.1.4.2 yamt printf("We are done completely\n");
204 1.1.4.2 yamt fflush(stdout);
205 1.1.4.2 yamt }
206 1.1.4.2 yamt return 0;
207 1.1.4.2 yamt }
208 1.1.4.2 yamt
209 1.1.4.2 yamt #ifndef STANDALONE
210 1.1.4.2 yamt ATF_TC(parent_child);
211 1.1.4.2 yamt
212 1.1.4.2 yamt ATF_TC_HEAD(parent_child, tc)
213 1.1.4.2 yamt {
214 1.1.4.2 yamt atf_tc_set_md_var(tc, "descr", "Checks that when a fifo is shared "
215 1.1.4.2 yamt "between a reader parent and a writer child, that read will "
216 1.1.4.2 yamt "return EOF, and not get stuck after the child exits");
217 1.1.4.2 yamt }
218 1.1.4.2 yamt
219 1.1.4.2 yamt ATF_TC_BODY(parent_child, tc)
220 1.1.4.2 yamt {
221 1.1.4.2 yamt ATF_REQUIRE(run() == 0);
222 1.1.4.2 yamt }
223 1.1.4.2 yamt
224 1.1.4.2 yamt ATF_TP_ADD_TCS(tp)
225 1.1.4.2 yamt {
226 1.1.4.2 yamt ATF_TP_ADD_TC(tp, parent_child);
227 1.1.4.2 yamt
228 1.1.4.2 yamt return atf_no_error();
229 1.1.4.2 yamt }
230 1.1.4.2 yamt #else
231 1.1.4.2 yamt int
232 1.1.4.2 yamt main(void)
233 1.1.4.2 yamt {
234 1.1.4.2 yamt verbose = 1;
235 1.1.4.2 yamt return run();
236 1.1.4.2 yamt }
237 1.1.4.2 yamt #endif
238