Debian Codegraph @

tests/t-poll.c

raw
1 /* t-poll.c - Check the poll function
2 * Copyright (C) 2015 g10 Code GmbH
3 *
4 * This file is part of libgpg-error.
5 *
6 * libgpg-error is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public License
8 * as published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
10 *
11 * libgpg-error is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, see <https://www.gnu.org/licenses/>.
18 */
19
20 /* FIXME: We need much better tests that this very basic one. */
21
22 #if HAVE_CONFIG_H
23 # include <config.h>
24 #endif
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <assert.h>
30 #include <sys/types.h>
31 #include <unistd.h>
32 #include <errno.h>
33 #ifdef _WIN32
34 # include <windows.h>
35 # include <time.h>
36 #else
37 # ifdef USE_POSIX_THREADS
38 # include <pthread.h>
39 # endif
40 #endif
41
42 #define PGM "t-poll"
43
44 #include "t-common.h"
45
46 #ifdef _WIN32
47 # define THREAD_RET_TYPE DWORD WINAPI
48 # define THREAD_RET_VALUE 0
49 #else
50 # define THREAD_RET_TYPE void *
51 # define THREAD_RET_VALUE NULL
52 #endif
53
54
55 /* Object to convey data to a thread. */
56 struct thread_arg
57 {
58 const char *name;
59 estream_t stream;
60 volatile int stop_me;
61 #ifdef USE_POSIX_THREADS
62 pthread_t thread;
63 #elif _WIN32
64 HANDLE thread;
65 #endif
66 };
67
68
69 static struct thread_arg peer_stdin; /* Thread to feed the stdin. */
70 static struct thread_arg peer_stdout; /* Thread to feed the stdout. */
71 static struct thread_arg peer_stderr; /* Thread to feed the stderr. */
72
73 static estream_t test_stdin;
74 static estream_t test_stdout;
75 static estream_t test_stderr;
76
77 #if defined(_WIN32) || defined(USE_POSIX_THREADS)
78
79 /* This thread feeds data to the given stream. */
80 static THREAD_RET_TYPE
81 producer_thread (void *argaddr)
82 {
83 struct thread_arg *arg = argaddr;
84 int i = 0;
85
86 (void)arg;
87
88 while (!arg->stop_me && i++ < 3)
89 {
90 show ("thread '%s' about to write\n", arg->name);
91 es_fprintf (arg->stream, "This is '%s' count=%d\n", arg->name, i);
92 es_fflush (arg->stream);
93 }
94 es_fclose (arg->stream);
95 return THREAD_RET_VALUE;
96 }
97
98 /* This thread eats data from the given stream. */
99 static THREAD_RET_TYPE
100 consumer_thread (void *argaddr)
101 {
102 struct thread_arg *arg = argaddr;
103 char buf[15];
104
105 (void)arg;
106
107 while (!arg->stop_me)
108 {
109 show ("thread '%s' ready to read\n", arg->name);
110 if (!es_fgets (buf, sizeof buf, arg->stream))
111 {
112 show ("Thread '%s' received EOF or error\n", arg->name);
113 break;
114 }
115 show ("Thread '%s' got: '%s'\n", arg->name, buf);
116 }
117 es_fclose (arg->stream);
118 return THREAD_RET_VALUE;
119 }
120
121 #endif /*_WIN32 || USE_POSIX_THREADS */
122
123
124 static void
125 launch_thread (THREAD_RET_TYPE (*fnc)(void *), struct thread_arg *th)
126 {
127 int fd;
128
129 th->stop_me = 0;
130 fd = es_fileno (th->stream);
131 #ifdef _WIN32
132
133 th->thread = CreateThread (NULL, 0, fnc, th, 0, NULL);
134 if (!th->thread)
135 die ("creating thread '%s' failed: rc=%d", th->name, (int)GetLastError ());
136 show ("thread '%s' launched (fd=%d)\n", th->name, fd);
137
138 #elif USE_POSIX_THREADS
139
140 if (pthread_create (&th->thread, NULL, fnc, th))
141 die ("creating thread '%s' failed: %s\n", th->name, strerror (errno));
142 show ("thread '%s' launched (fd=%d)\n", th->name, fd);
143
144 # else /* no thread support */
145
146 verbose++;
147 show ("no thread support - skipping test\n", PGM);
148 verbose--;
149
150 #endif /* no thread support */
151 }
152
153
154 static void
155 join_thread (struct thread_arg *th)
156 {
157 #ifdef _WIN32
158 int rc;
159
160 rc = WaitForSingleObject (th->thread, INFINITE);
161 if (rc == WAIT_OBJECT_0)
162 show ("thread '%s' has terminated\n", th->name);
163 else
164 fail ("waiting for thread '%s' failed: %d", th->name, (int)GetLastError ());
165 CloseHandle (th->thread);
166
167 #elif USE_POSIX_THREADS
168
169 pthread_join (th->thread, NULL);
170 show ("thread '%s' has terminated\n", th->name);
171
172 #endif
173 }
174
175
176 static void
177 create_pipe (estream_t *r_in, estream_t *r_out)
178 {
179 gpg_error_t err;
180 #ifdef _WIN32
181 HANDLE pipe_r;
182 HANDLE pipe_w;
183 #else
184 int filedes[2];
185 #endif
186 es_syshd_t syshd[2];
187
188 #ifdef _WIN32
189 if (CreatePipe (&pipe_r, &pipe_w, NULL, 512) == 0)
190 die ("error creating a pipe: rc=%d\n", (int)GetLastError ());
191
192 syshd[0].type = syshd[1].type = ES_SYSHD_HANDLE;
193 syshd[0].u.handle = pipe_r;
194 syshd[1].u.handle = pipe_w;
195
196 show ("created pipe [%p, %p]\n", pipe_r, pipe_w);
197 #else
198 if (pipe (filedes) == -1)
199 {
200 err = gpg_error_from_syserror ();
201 die ("error creating a pipe: %s\n", gpg_strerror (err));
202 }
203
204 syshd[0].type = syshd[1].type = ES_SYSHD_FD;
205 syshd[0].u.fd = filedes[0];
206 syshd[1].u.fd = filedes[1];
207
208 show ("created pipe [%d, %d]\n", filedes[0], filedes[1]);
209 #endif
210
211 *r_in = es_sysopen (&syshd[0], "r,pollable");
212 if (!*r_in)
213 {
214 err = gpg_error_from_syserror ();
215 die ("error creating a stream for a pipe: %s\n", gpg_strerror (err));
216 }
217
218 *r_out = es_sysopen (&syshd[1], "w,pollable");
219 if (!*r_out)
220 {
221 err = gpg_error_from_syserror ();
222 die ("error creating a stream for a pipe: %s\n", gpg_strerror (err));
223 }
224 }
225
226
227 static void
228 test_poll (void)
229 {
230 int ret;
231 gpgrt_poll_t fds[3];
232 char buffer[16];
233 size_t used, nwritten;
234 int c;
235
236 memset (fds, 0, sizeof fds);
237 fds[0].stream = test_stdin;
238 fds[0].want_read = 1;
239 fds[1].stream = test_stdout;
240 fds[1].want_write = 1;
241 /* FIXME: We don't use the next stream at all. */
242 fds[2].stream = test_stderr;
243 fds[2].want_write = 1;
244 fds[2].ignore = 1;
245
246
247 used = 0;
248 while (used || !fds[0].ignore)
249 {
250 ret = gpgrt_poll (fds, DIM(fds), -1);
251 if (ret == -1)
252 {
253 fail ("gpgrt_poll failed: %s\n", strerror (errno));
254 continue;
255 }
256 if (!ret)
257 {
258 fail ("gpgrt_poll unexpectedly timed out\n");
259 continue;
260 }
261
262 show ("gpgrt_poll detected %d events\n", ret);
263 if (debug)
264 show ("gpgrt_poll: r=%d"
265 " 0:%c%c%c%c%c%c%c%c%c%c%c%c"
266 " 1:%c%c%c%c%c%c%c%c%c%c%c%c"
267 " 2:%c%c%c%c%c%c%c%c%c%c%c%c"
268 "\n",
269 ret,
270 fds[0].want_read? 'r':'-',
271 fds[0].want_write? 'w':'-',
272 fds[0].want_oob? 'o':'-',
273 fds[0].want_rdhup? 'h':'-',
274 fds[0].ignore? '!':'=',
275 fds[0].got_read? 'r':'-',
276 fds[0].got_write? 'w':'-',
277 fds[0].got_oob? 'o':'-',
278 fds[0].got_rdhup? 'h':'-',
279 fds[0].got_hup? 'H':' ',
280 fds[0].got_err? 'e':' ',
281 fds[0].got_nval? 'n':' ',
282
283 fds[1].want_read? 'r':'-',
284 fds[1].want_write? 'w':'-',
285 fds[1].want_oob? 'o':'-',
286 fds[1].want_rdhup? 'h':'-',
287 fds[1].ignore? '!':'=',
288 fds[1].got_read? 'r':'-',
289 fds[1].got_write? 'w':'-',
290 fds[1].got_oob? 'o':'-',
291 fds[1].got_rdhup? 'h':'-',
292 fds[1].got_hup? 'H':' ',
293 fds[1].got_err? 'e':' ',
294 fds[1].got_nval? 'n':' ',
295
296 fds[2].want_read? 'r':'-',
297 fds[2].want_write? 'w':'-',
298 fds[2].want_oob? 'o':'-',
299 fds[2].want_rdhup? 'h':'-',
300 fds[2].ignore? '!':'=',
301 fds[2].got_read? 'r':'-',
302 fds[2].got_write? 'w':'-',
303 fds[2].got_oob? 'o':'-',
304 fds[2].got_rdhup? 'h':'-',
305 fds[2].got_hup? 'H':' ',
306 fds[2].got_err? 'e':' ',
307 fds[2].got_nval? 'n':' '
308 );
309 else
310 show ("gpgrt_poll detected %d events\n", ret);
311
312 if (fds[0].got_read)
313 {
314 /* Read from the producer. */
315 for (;;)
316 {
317 c = es_fgetc (fds[0].stream);
318 if (c == EOF)
319 {
320 if (es_feof (fds[0].stream))
321 {
322 show ("reading '%s': EOF\n", peer_stdin.name);
323 fds[0].ignore = 1; /* Not anymore needed. */
324 peer_stdin.stop_me = 1; /* Tell the thread to stop. */
325 }
326 else if (es_ferror (fds[0].stream))
327 {
328 fail ("error reading '%s': %s\n",
329 peer_stdin.name, strerror (errno));
330 fds[0].ignore = 1; /* Disable. */
331 peer_stdin.stop_me = 1; /* Tell the thread to stop. */
332 }
333 else
334 show ("reading '%s': EAGAIN\n", peer_stdin.name);
335 break;
336 }
337 else
338 {
339 if (used <= sizeof buffer -1)
340 buffer[used++] = c;
341 if (used == sizeof buffer)
342 {
343 show ("throttling reading from '%s'\n", peer_stdin.name);
344 fds[0].ignore = 1;
345 break;
346 }
347 }
348 }
349 show ("read from '%s': %zu bytes\n", peer_stdin.name, used);
350 if (used)
351 fds[1].ignore = 0; /* Data to send. */
352 }
353 if (fds[1].got_write)
354 {
355 if (used)
356 {
357 ret = es_write (fds[1].stream, buffer, used, &nwritten);
358 show ("result for writing to '%s': ret=%d, n=%zu, nwritten=%zu\n",
359 peer_stdout.name, ret, used, nwritten);
360 if (!ret)
361 {
362 assert (nwritten <= used);
363 /* Move the remaining data to the front of buffer. */
364 memmove (buffer, buffer + nwritten,
365 sizeof buffer - nwritten);
366 used -= nwritten;
367 }
368 ret = es_fflush (fds[1].stream);
369 if (ret)
370 fail ("Flushing for '%s' failed: %s\n",
371 peer_stdout.name, strerror (errno));
372 }
373 if (!used)
374 fds[1].ignore = 1; /* No need to send data. */
375 }
376
377 if (used < sizeof buffer / 2 && !peer_stdin.stop_me && fds[0].ignore)
378 {
379 show ("accelerate reading from '%s'\n", peer_stdin.name);
380 fds[0].ignore = 0;
381 }
382 }
383 }
384
385
386 int
387 main (int argc, char **argv)
388 {
389 int last_argc = -1;
390
391 if (argc)
392 {
393 argc--; argv++;
394 }
395 while (argc && last_argc != argc )
396 {
397 last_argc = argc;
398 if (!strcmp (*argv, "--help"))
399 {
400 puts (
401 "usage: ./t-poll [options]\n"
402 "\n"
403 "Options:\n"
404 " --verbose Show what is going on\n"
405 " --debug Flyswatter\n"
406 );
407 exit (0);
408 }
409 if (!strcmp (*argv, "--verbose"))
410 {
411 verbose = 1;
412 argc--; argv++;
413 }
414 else if (!strcmp (*argv, "--debug"))
415 {
416 verbose = debug = 1;
417 argc--; argv++;
418 }
419 }
420
421 if (!gpg_error_check_version (GPG_ERROR_VERSION))
422 {
423 die ("gpg_error_check_version returned an error");
424 errorcount++;
425 }
426
427 peer_stdin.name = "stdin producer";
428 create_pipe (&test_stdin, &peer_stdin.stream);
429 peer_stdout.name = "stdout consumer";
430 create_pipe (&peer_stdout.stream, &test_stdout);
431 peer_stderr.name = "stderr consumer";
432 create_pipe (&peer_stderr.stream, &test_stderr);
433
434 if (es_set_nonblock (test_stdin, 1))
435 fail ("error setting test_stdin to nonblock: %s\n", strerror (errno));
436 if (es_set_nonblock (test_stdout, 1))
437 fail ("error setting test_stdout to nonblock: %s\n", strerror (errno));
438 if (es_set_nonblock (test_stderr, 1))
439 fail ("error setting test_stderr to nonblock: %s\n", strerror (errno));
440
441 launch_thread (producer_thread, &peer_stdin );
442 launch_thread (consumer_thread, &peer_stdout);
443 launch_thread (consumer_thread, &peer_stderr);
444 test_poll ();
445 show ("Waiting for threads to terminate...\n");
446 es_fclose (test_stdin);
447 es_fclose (test_stdout);
448 es_fclose (test_stderr);
449 peer_stdin.stop_me = 1;
450 peer_stdout.stop_me = 1;
451 peer_stderr.stop_me = 1;
452 join_thread (&peer_stdin);
453 join_thread (&peer_stdout);
454 join_thread (&peer_stderr);
455
456 return errorcount ? 1 : 0;
457 }