Fix: futex wait: handle spurious futex wakeups
[lttng-tools.git] / tests / regression / kernel / select_poll_epoll.cpp
1 /*
2 * Copyright (C) 2016 Julien Desfossez <jdesfossez@efficios.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 */
7
8 #include <fcntl.h>
9 #include <limits.h>
10 #include <poll.h>
11 #include <popt.h>
12 #include <pthread.h>
13 #include <signal.h>
14 #include <stddef.h>
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <sys/epoll.h>
19 #include <sys/mman.h>
20 #include <sys/resource.h>
21 #include <sys/select.h>
22 #include <sys/stat.h>
23 #include <sys/syscall.h>
24 #include <sys/time.h>
25 #include <sys/types.h>
26 #include <unistd.h>
27
28 #include <common/compat/time.hpp>
29 #include <common/error.hpp>
30
31 #define BUF_SIZE 256
32 #define NB_FD 1
33 #define MAX_FDS 2047
34 #define NR_ITER 1000 /* for stress-tests */
35
36 #define MIN_NR_FDS 5 /* the minimum number of open FDs required for the test to run */
37 #define BIG_SELECT_FD 1022
38
39 #define MSEC_PER_USEC 1000
40 #define MSEC_PER_NSEC (MSEC_PER_USEC * 1000)
41
42 static int timeout; /* seconds, -1 to disable */
43 static volatile int stop_thread;
44 static int wait_fd;
45
46 /* Used by logging utils. */
47 int lttng_opt_quiet, lttng_opt_verbose, lttng_opt_mi;
48
49 static void run_working_cases(FILE *validation_output_file);
50 static void pselect_invalid_fd(FILE *validation_output_file);
51 static void test_ppoll_big(FILE *validation_output_file);
52 static void ppoll_fds_buffer_overflow(FILE *validation_output_file);
53 static void pselect_invalid_pointer(FILE *validation_output_file);
54 static void ppoll_fds_ulong_max(FILE *validation_output_file);
55 static void epoll_pwait_invalid_pointer(FILE *validation_output_file);
56 static void epoll_pwait_int_max(FILE *validation_output_file);
57 static void ppoll_concurrent_write(FILE *validation_output_file);
58 static void epoll_pwait_concurrent_munmap(FILE *validation_output_file);
59
60 typedef void (*test_case_cb)(FILE *output_file);
61
62 namespace {
63 const struct test_case {
64 test_case_cb run;
65 bool produces_validation_info;
66 int timeout;
67 } test_cases [] =
68 {
69 { .run = run_working_cases, .produces_validation_info = true, .timeout = -1 },
70 { .run = run_working_cases, .produces_validation_info = true, .timeout = 1 },
71 { .run = pselect_invalid_fd, .produces_validation_info = false, .timeout = 0 },
72 { .run = test_ppoll_big, .produces_validation_info = false, .timeout = 0 },
73 { .run = ppoll_fds_buffer_overflow, .produces_validation_info = false, .timeout = 0 },
74 { .run = pselect_invalid_pointer, .produces_validation_info = false, .timeout = 0 },
75 { .run = ppoll_fds_ulong_max, .produces_validation_info = false, .timeout = 0 },
76 { .run = epoll_pwait_invalid_pointer, .produces_validation_info = true, .timeout = 0 },
77 { .run = epoll_pwait_int_max, .produces_validation_info = true, .timeout = 0 },
78 { .run = ppoll_concurrent_write, .produces_validation_info = false, .timeout = 0 },
79 { .run = epoll_pwait_concurrent_munmap, .produces_validation_info = true, .timeout = 0 },
80 };
81
82 struct ppoll_thread_data {
83 struct pollfd *ufds;
84 int value;
85 };
86 } /* namespace */
87
88 static
89 void test_select_big(void)
90 {
91 fd_set rfds, wfds, exfds;
92 struct timeval tv;
93 int ret;
94 int fd2;
95 char buf[BUF_SIZE];
96
97 FD_ZERO(&rfds);
98 FD_ZERO(&wfds);
99 FD_ZERO(&exfds);
100
101 fd2 = dup2(wait_fd, BIG_SELECT_FD);
102 if (fd2 < 0) {
103 PERROR("dup2");
104 goto end;
105 }
106 FD_SET(fd2, &rfds);
107
108 tv.tv_sec = 0;
109 tv.tv_usec = timeout * MSEC_PER_USEC;
110
111 if (timeout > 0) {
112 ret = select(fd2 + 1, &rfds, &wfds, &exfds, &tv);
113 } else {
114 ret = select(fd2 + 1, &rfds, &wfds, &exfds, NULL);
115 }
116
117 if (ret == -1) {
118 PERROR("select()");
119 } else if (ret) {
120 ret = read(wait_fd, buf, BUF_SIZE);
121 if (ret < 0) {
122 PERROR("[select] read");
123 }
124 }
125
126 ret = close(BIG_SELECT_FD);
127 if (ret) {
128 PERROR("close");
129 }
130
131 end:
132 return;
133 }
134
135 static
136 void test_pselect(void)
137 {
138 fd_set rfds;
139 struct timespec tv;
140 int ret;
141 char buf[BUF_SIZE];
142
143 FD_ZERO(&rfds);
144 FD_SET(wait_fd, &rfds);
145
146 tv.tv_sec = 0;
147 tv.tv_nsec = timeout * MSEC_PER_NSEC;
148
149 if (timeout > 0) {
150 ret = pselect(1, &rfds, NULL, NULL, &tv, NULL);
151 } else {
152 ret = pselect(1, &rfds, NULL, NULL, NULL, NULL);
153 }
154
155 if (ret == -1) {
156 PERROR("pselect()");
157 } else if (ret) {
158 ret = read(wait_fd, buf, BUF_SIZE);
159 if (ret < 0) {
160 PERROR("[pselect] read");
161 }
162 }
163 }
164
165 static
166 void test_select(void)
167 {
168 fd_set rfds;
169 struct timeval tv;
170 int ret;
171 char buf[BUF_SIZE];
172
173 FD_ZERO(&rfds);
174 FD_SET(wait_fd, &rfds);
175
176 tv.tv_sec = 0;
177 tv.tv_usec = timeout * MSEC_PER_USEC;
178
179 if (timeout > 0) {
180 ret = select(1, &rfds, NULL, NULL, &tv);
181 } else {
182 ret = select(1, &rfds, NULL, NULL, NULL);
183 }
184
185 if (ret == -1) {
186 PERROR("select()");
187 } else if (ret) {
188 ret = read(wait_fd, buf, BUF_SIZE);
189 if (ret < 0) {
190 PERROR("[select] read");
191 }
192 }
193 }
194
195 static
196 void test_poll(void)
197 {
198 struct pollfd ufds[NB_FD];
199 char buf[BUF_SIZE];
200 int ret;
201
202 ufds[0].fd = wait_fd;
203 ufds[0].events = POLLIN|POLLPRI;
204
205 ret = poll(ufds, 1, timeout);
206
207 if (ret < 0) {
208 PERROR("poll");
209 } else if (ret > 0) {
210 ret = read(wait_fd, buf, BUF_SIZE);
211 if (ret < 0) {
212 PERROR("[poll] read");
213 }
214 }
215 }
216
217 static
218 void test_ppoll(void)
219 {
220 struct pollfd ufds[NB_FD];
221 char buf[BUF_SIZE];
222 int ret;
223 struct timespec ts;
224
225 ufds[0].fd = wait_fd;
226 ufds[0].events = POLLIN|POLLPRI;
227
228 if (timeout > 0) {
229 ts.tv_sec = 0;
230 ts.tv_nsec = timeout * MSEC_PER_NSEC;
231 ret = ppoll(ufds, 1, &ts, NULL);
232 } else {
233 ret = ppoll(ufds, 1, NULL, NULL);
234 }
235
236
237 if (ret < 0) {
238 PERROR("ppoll");
239 } else if (ret > 0) {
240 ret = read(wait_fd, buf, BUF_SIZE);
241 if (ret < 0) {
242 PERROR("[ppoll] read");
243 }
244 }
245 }
246
247 static
248 void test_ppoll_big(FILE *validation_output_file __attribute__((unused)))
249 {
250 struct pollfd ufds[MAX_FDS];
251 char buf[BUF_SIZE];
252 int ret, i, fds[MAX_FDS];
253
254 for (i = 0; i < MAX_FDS; i++) {
255 fds[i] = dup(wait_fd);
256 if (fds[i] < 0) {
257 PERROR("dup");
258 }
259 ufds[i].fd = fds[i];
260 ufds[i].events = POLLIN|POLLPRI;
261 }
262
263 ret = ppoll(ufds, MAX_FDS, NULL, NULL);
264
265 if (ret < 0) {
266 PERROR("ppoll");
267 } else if (ret > 0) {
268 ret = read(wait_fd, buf, BUF_SIZE);
269 if (ret < 0) {
270 PERROR("[ppoll] read");
271 }
272 }
273
274 for (i = 0; i < MAX_FDS; i++) {
275 ret = close(fds[i]);
276 if (ret != 0) {
277 PERROR("close");
278 }
279 }
280
281 return;
282 }
283
284 static
285 void test_epoll(FILE *validation_output_file)
286 {
287 int ret, epollfd;
288 char buf[BUF_SIZE];
289 struct epoll_event epoll_event;
290
291 epollfd = epoll_create(NB_FD);
292 if (epollfd < 0) {
293 PERROR("[epoll] create");
294 goto end;
295 }
296
297 ret = fprintf(validation_output_file,
298 ", \"epoll_wait_fd\": %i", epollfd);
299 if (ret < 0) {
300 PERROR("[epoll] Failed to write test validation output");
301 goto error;
302 }
303
304 epoll_event.events = EPOLLIN | EPOLLPRI | EPOLLET;
305 epoll_event.data.fd = wait_fd;
306 ret = epoll_ctl(epollfd, EPOLL_CTL_ADD, wait_fd, &epoll_event);
307 if (ret < 0) {
308 PERROR("[epoll] add");
309 goto error;
310 }
311
312 if (timeout > 0) {
313 ret = epoll_wait(epollfd, &epoll_event, 1, timeout);
314 } else {
315 ret = epoll_wait(epollfd, &epoll_event, 1, -1);
316 }
317
318 if (ret == 1) {
319 ret = read(wait_fd, buf, BUF_SIZE);
320 if (ret < 0) {
321 PERROR("[epoll] read");
322 }
323 } else if (ret != 0) {
324 PERROR("epoll_wait");
325 }
326
327 error:
328 ret = close(epollfd);
329 if (ret) {
330 PERROR("close");
331 }
332 end:
333 return;
334 }
335
336 static
337 void test_epoll_pwait(FILE *validation_output_file)
338 {
339 int ret, epollfd;
340 char buf[BUF_SIZE];
341 struct epoll_event epoll_event;
342
343 epollfd = epoll_create(NB_FD);
344 if (epollfd < 0) {
345 PERROR("[epoll_pwait] create");
346 goto end;
347 }
348
349 ret = fprintf(validation_output_file,
350 ", \"epoll_pwait_fd\": %i", epollfd);
351 if (ret < 0) {
352 PERROR("[epoll_pwait] Failed to write test validation output");
353 goto error;
354 }
355
356 epoll_event.events = EPOLLIN | EPOLLPRI | EPOLLET;
357 epoll_event.data.fd = wait_fd;
358 ret = epoll_ctl(epollfd, EPOLL_CTL_ADD, wait_fd, &epoll_event);
359 if (ret < 0) {
360 PERROR("[epoll_pwait] add");
361 goto error;
362 }
363
364 if (timeout > 0) {
365 ret = epoll_pwait(epollfd, &epoll_event, 1, timeout, NULL);
366 } else {
367 ret = epoll_pwait(epollfd, &epoll_event, 1, -1, NULL);
368 }
369
370 if (ret == 1) {
371 ret = read(wait_fd, buf, BUF_SIZE);
372 if (ret < 0) {
373 PERROR("[epoll_pwait] read");
374 }
375 } else if (ret != 0) {
376 PERROR("epoll_pwait");
377 }
378
379 error:
380 ret = close(epollfd);
381 if (ret) {
382 PERROR("close");
383 }
384 end:
385 return;
386 }
387
388 static
389 void run_working_cases(FILE *validation_output_file)
390 {
391 int ret;
392 int pipe_fds[2];
393
394 if (timeout > 0) {
395 /*
396 * We need an input pipe for some cases and stdin might
397 * have random data, so we create a dummy pipe for this
398 * test to make sure we are running under clean conditions.
399 */
400 ret = pipe(pipe_fds);
401 if (ret != 0) {
402 PERROR("pipe");
403 goto end;
404 }
405 wait_fd = pipe_fds[0];
406 }
407 test_select();
408 test_pselect();
409 test_select_big();
410 test_poll();
411 test_ppoll();
412
413 ret = fprintf(validation_output_file, "{ \"pid\": %i", getpid());
414 if (ret < 0) {
415 PERROR("Failed to write pid to test validation file");
416 goto end;
417 }
418
419 test_epoll(validation_output_file);
420 test_epoll_pwait(validation_output_file);
421
422 if (timeout > 0) {
423 ret = close(pipe_fds[0]);
424 if (ret) {
425 PERROR("close");
426 }
427 ret = close(pipe_fds[1]);
428 if (ret) {
429 PERROR("close");
430 }
431 }
432
433 ret = fputs(" }", validation_output_file);
434 if (ret < 0) {
435 PERROR("Failed to close JSON dictionary in test validation file");
436 goto end;
437 }
438
439 end:
440 return;
441 }
442
443 /*
444 * Ask for 100 FDs in a buffer for allocated for only 1 FD, should
445 * segfault (eventually with a "*** stack smashing detected ***" message).
446 * The event should contain an array of 100 FDs filled with garbage.
447 */
448 static
449 void ppoll_fds_buffer_overflow(
450 FILE *validation_output_file __attribute__((unused)))
451 {
452 struct pollfd ufds[NB_FD];
453 char buf[BUF_SIZE];
454 int ret;
455
456 ufds[0].fd = wait_fd;
457 ufds[0].events = POLLIN|POLLPRI;
458
459 ret = syscall(SYS_ppoll, ufds, 100, NULL, NULL);
460
461 if (ret < 0) {
462 PERROR("ppoll");
463 } else if (ret > 0) {
464 ret = read(wait_fd, buf, BUF_SIZE);
465 if (ret < 0) {
466 PERROR("[ppoll] read");
467 }
468 }
469 }
470
471 /*
472 * Ask for ULONG_MAX FDs in a buffer for allocated for only 1 FD, should
473 * cleanly fail with a "Invalid argument".
474 * The event should contain an empty array of FDs and overflow = 1.
475 */
476 static
477 void ppoll_fds_ulong_max(FILE *validation_output_file __attribute__((unused)))
478 {
479 struct pollfd ufds[NB_FD];
480 char buf[BUF_SIZE];
481 int ret;
482
483 ufds[0].fd = wait_fd;
484 ufds[0].events = POLLIN|POLLPRI;
485
486 ret = syscall(SYS_ppoll, ufds, ULONG_MAX, NULL, NULL);
487 if (ret < 0) {
488 /* Expected error. */
489 } else if (ret > 0) {
490 ret = read(wait_fd, buf, BUF_SIZE);
491 if (ret < 0) {
492 PERROR("[ppoll] read");
493 }
494 }
495 }
496
497 /*
498 * Pass an invalid file descriptor to pselect6(). The syscall should return
499 * -EBADF. The recorded event should contain a "ret = -EBADF (-9)".
500 */
501 static
502 void pselect_invalid_fd(FILE *validation_output_file __attribute__((unused)))
503 {
504 fd_set rfds;
505 int ret;
506 int fd;
507 char buf[BUF_SIZE];
508
509 /*
510 * Open a file, close it and use the closed FD in the pselect6 call.
511 */
512 fd = open("/dev/null", O_RDONLY);
513 if (fd == -1) {
514 PERROR("open");
515 goto error;
516 }
517
518 ret = close(fd);
519 if (ret == -1) {
520 PERROR("close");
521 goto error;
522 }
523
524 FD_ZERO(&rfds);
525 FD_SET(fd, &rfds);
526
527 ret = syscall(SYS_pselect6, fd + 1, &rfds, NULL, NULL, NULL, NULL);
528 if (ret == -1) {
529 /* Expected error. */
530 } else if (ret) {
531 ret = read(wait_fd, buf, BUF_SIZE);
532 if (ret < 0) {
533 PERROR("[pselect] read");
534 }
535 }
536 error:
537 return;
538 }
539
540 /*
541 * Invalid pointer as writefds, should output a ppoll event
542 * with 0 FDs.
543 */
544 static
545 void pselect_invalid_pointer(
546 FILE *validation_output_file __attribute__((unused)))
547 {
548 fd_set rfds;
549 int ret;
550 char buf[BUF_SIZE];
551 void *invalid = (void *) 0x42;
552
553 FD_ZERO(&rfds);
554 FD_SET(wait_fd, &rfds);
555
556 ret = syscall(SYS_pselect6, 1, &rfds, (fd_set *) invalid, NULL, NULL,
557 NULL);
558 if (ret == -1) {
559 /* Expected error. */
560 } else if (ret) {
561 ret = read(wait_fd, buf, BUF_SIZE);
562 if (ret < 0) {
563 PERROR("[pselect] read");
564 }
565 }
566 }
567
568 /*
569 * Pass an invalid pointer to epoll_pwait, should fail with
570 * "Bad address", the event returns 0 FDs.
571 */
572 static
573 void epoll_pwait_invalid_pointer(FILE *validation_output_file)
574 {
575 int ret, epollfd;
576 char buf[BUF_SIZE];
577 struct epoll_event epoll_event;
578 void *invalid = (void *) 0x42;
579
580 epollfd = epoll_create(NB_FD);
581 if (epollfd < 0) {
582 PERROR("[epoll_pwait] create");
583 goto end;
584 }
585
586 ret = fprintf(validation_output_file,
587 "{ \"epollfd\": %i, \"pid\": %i }", epollfd,
588 getpid());
589 if (ret < 0) {
590 PERROR("[epoll_pwait] Failed to write test validation output");
591 goto error;
592 }
593
594 epoll_event.events = EPOLLIN | EPOLLPRI | EPOLLET;
595 epoll_event.data.fd = wait_fd;
596 ret = epoll_ctl(epollfd, EPOLL_CTL_ADD, wait_fd, &epoll_event);
597 if (ret < 0) {
598 PERROR("[epoll_pwait] add");
599 goto error;
600 }
601
602 ret = syscall(SYS_epoll_pwait, epollfd,
603 (struct epoll_event *) invalid, 1, -1, NULL);
604
605 if (ret == 1) {
606 ret = read(wait_fd, buf, BUF_SIZE);
607 if (ret < 0) {
608 PERROR("[epoll_pwait] read");
609 }
610 } else if (ret != 0) {
611 /* Expected error. */
612 }
613
614 error:
615 ret = close(epollfd);
616 if (ret) {
617 PERROR("close");
618 }
619 end:
620 return;
621 }
622
623 /*
624 * Set maxevents to INT_MAX, should output "Invalid argument"
625 * The event should return an empty array.
626 */
627 static
628 void epoll_pwait_int_max(FILE *validation_output_file)
629 {
630 int ret, epollfd;
631 char buf[BUF_SIZE];
632 struct epoll_event epoll_event;
633
634 epollfd = epoll_create(NB_FD);
635 if (epollfd < 0) {
636 PERROR("[epoll_pwait] create");
637 goto end;
638 }
639
640 ret = fprintf(validation_output_file,
641 "{ \"epollfd\": %i, \"pid\": %i }", epollfd,
642 getpid());
643 if (ret < 0) {
644 PERROR("[epoll_pwait] Failed to write test validation output");
645 goto error;
646 }
647
648 epoll_event.events = EPOLLIN | EPOLLPRI | EPOLLET;
649 epoll_event.data.fd = wait_fd;
650 ret = epoll_ctl(epollfd, EPOLL_CTL_ADD, wait_fd, &epoll_event);
651 if (ret < 0) {
652 PERROR("[epoll_pwait] add");
653 goto error;
654 }
655
656 ret = syscall(SYS_epoll_pwait, epollfd, &epoll_event, INT_MAX, -1,
657 NULL);
658
659 if (ret == 1) {
660 ret = read(wait_fd, buf, BUF_SIZE);
661 if (ret < 0) {
662 PERROR("[epoll_pwait] read");
663 }
664 } else if (ret != 0) {
665 /* Expected error. */
666 }
667
668 error:
669 ret = close(epollfd);
670 if (ret) {
671 PERROR("close");
672 }
673 end:
674 return;
675 }
676
677 static
678 void *ppoll_writer(void *arg)
679 {
680 struct ppoll_thread_data *data = (struct ppoll_thread_data *) arg;
681
682 while (!stop_thread) {
683 memset(data->ufds, data->value,
684 MAX_FDS * sizeof(struct pollfd));
685 usleep(100);
686 }
687
688 return NULL;
689 }
690
691 static
692 void do_ppoll(int *fds, struct pollfd *ufds)
693 {
694 int i, ret;
695 struct timespec ts;
696 char buf[BUF_SIZE];
697
698 ts.tv_sec = 0;
699 ts.tv_nsec = 1 * MSEC_PER_NSEC;
700
701 for (i = 0; i < MAX_FDS; i++) {
702 ufds[i].fd = fds[i];
703 ufds[i].events = POLLIN|POLLPRI;
704 }
705
706 ret = ppoll(ufds, MAX_FDS, &ts, NULL);
707
708 if (ret < 0) {
709 PERROR("ppoll");
710 } else if (ret > 0) {
711 ret = read(wait_fd, buf, BUF_SIZE);
712 if (ret < 0) {
713 PERROR("[ppoll] read");
714 }
715 }
716 }
717
718 static
719 void stress_ppoll(int *fds, int value)
720 {
721 pthread_t writer;
722 int iter, ret;
723 struct ppoll_thread_data thread_data;
724 struct pollfd ufds[MAX_FDS];
725
726 thread_data.ufds = ufds;
727 thread_data.value = value;
728
729 stop_thread = 0;
730 ret = pthread_create(&writer, NULL, &ppoll_writer, (void *) &thread_data);
731 if (ret != 0) {
732 fprintf(stderr, "[error] pthread_create\n");
733 goto end;
734 }
735 for (iter = 0; iter < NR_ITER; iter++) {
736 do_ppoll(fds, ufds);
737 }
738 stop_thread = 1;
739 ret = pthread_join(writer, NULL);
740 if (ret) {
741 fprintf(stderr, "[error] pthread_join\n");
742 goto end;
743 }
744 end:
745 return;
746 }
747
748 /*
749 * 3 rounds of NR_ITER iterations with concurrent updates of the pollfd
750 * structure:
751 * - memset to 0
752 * - memset to 1
753 * - memset to INT_MAX
754 * Waits for input, but also set a timeout in case the input FD is overwritten
755 * before entering in the syscall. We use MAX_FDS FDs (dup of stdin), so the
756 * resulting trace is big (20MB).
757 *
758 * ppoll should work as expected and the trace should be readable at the end.
759 */
760 static
761 void ppoll_concurrent_write(
762 FILE *validation_output_file __attribute__((unused)))
763 {
764 int i, ret, fds[MAX_FDS];
765
766 for (i = 0; i < MAX_FDS; i++) {
767 fds[i] = dup(wait_fd);
768 if (fds[i] < 0) {
769 PERROR("dup");
770 }
771 }
772
773 stress_ppoll(fds, 0);
774 stress_ppoll(fds, 1);
775 stress_ppoll(fds, INT_MAX);
776
777 for (i = 0; i < MAX_FDS; i++) {
778 ret = close(fds[i]);
779 if (ret != 0) {
780 PERROR("close");
781 }
782 }
783
784 return;
785 }
786
787 static
788 void *epoll_pwait_writer(void *addr)
789 {
790 srand(time(NULL));
791
792 while (!stop_thread) {
793 usleep(rand() % 30);
794 munmap(addr, MAX_FDS * sizeof(struct epoll_event));
795 }
796
797 return NULL;
798 }
799
800 /*
801 * epoll_pwait on MAX_FDS fds while a concurrent thread munmaps the
802 * buffer allocated for the returned data. This should randomly segfault.
803 * The trace should be readable and no kernel OOPS should occur.
804 */
805 static
806 void epoll_pwait_concurrent_munmap(FILE *validation_output_file)
807 {
808 int ret, epollfd, i, fds[MAX_FDS];
809 char buf[BUF_SIZE];
810 struct epoll_event *epoll_event;
811 pthread_t writer;
812
813 for (i = 0; i < MAX_FDS; i++) {
814 fds[i] = -1;
815 }
816 epollfd = epoll_create(MAX_FDS);
817 if (epollfd < 0) {
818 PERROR("[epoll_pwait] create");
819 goto end;
820 }
821
822 ret = fprintf(validation_output_file,
823 "{ \"epollfd\": %i, \"pid\": %i }", epollfd,
824 getpid());
825 if (ret < 0) {
826 PERROR("[epoll_pwait] Failed to write test validation output");
827 goto error;
828 }
829
830 epoll_event = (struct epoll_event *) mmap(NULL,
831 MAX_FDS * sizeof(struct epoll_event),
832 PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1,
833 0);
834 if (epoll_event == MAP_FAILED) {
835 PERROR("mmap");
836 goto error;
837 }
838
839 for (i = 0; i < MAX_FDS; i++) {
840 fds[i] = dup(wait_fd);
841 if (fds[i] < 0) {
842 PERROR("dup");
843 }
844 epoll_event[i].events = EPOLLIN | EPOLLPRI | EPOLLET;
845 epoll_event[i].data.fd = fds[i];
846 ret = epoll_ctl(epollfd, EPOLL_CTL_ADD, fds[i], epoll_event);
847 if (ret < 0) {
848 PERROR("[epoll_pwait] add");
849 goto error_unmap;
850 }
851 }
852 stop_thread = 0;
853 ret = pthread_create(&writer, NULL, &epoll_pwait_writer,
854 (void *) epoll_event);
855 if (ret != 0) {
856 fprintf(stderr, "[error] pthread_create\n");
857 goto error_unmap;
858 }
859
860 ret = epoll_pwait(epollfd, epoll_event, 1, 1, NULL);
861
862 if (ret == 1) {
863 ret = read(wait_fd, buf, BUF_SIZE);
864 if (ret < 0) {
865 PERROR("[epoll_pwait] read");
866 }
867 } else if (ret != 0) {
868 /* Expected error. */
869 }
870
871 stop_thread = 1;
872 ret = pthread_join(writer, NULL);
873 if (ret) {
874 fprintf(stderr, "[error] pthread_join\n");
875 goto error_unmap;
876 }
877 error_unmap:
878 for (i = 0; i < MAX_FDS; i++) {
879 ret = close(fds[i]);
880 if (ret != 0) {
881 PERROR("close");
882 }
883 }
884
885 ret = munmap(epoll_event, MAX_FDS * sizeof(struct epoll_event));
886 if (ret != 0) {
887 PERROR("munmap");
888 }
889
890 error:
891 ret = close(epollfd);
892 if (ret) {
893 PERROR("close");
894 }
895 end:
896 return;
897 }
898
899 static
900 void print_list(void)
901 {
902 fprintf(stderr, "Test list (-t X):\n");
903 fprintf(stderr, "\t1: Working cases for select, pselect6, poll, ppoll "
904 "and epoll, waiting for input\n");
905 fprintf(stderr, "\t2: Timeout cases (1ms) for select, pselect6, poll, "
906 "ppoll and epoll\n");
907 fprintf(stderr, "\t3: pselect with an invalid fd\n");
908 fprintf(stderr, "\t4: ppoll with %d FDs\n", MAX_FDS);
909 fprintf(stderr, "\t5: ppoll buffer overflow, should segfault, waits "
910 "for input\n");
911 fprintf(stderr, "\t6: pselect with an invalid pointer, waits for "
912 "input\n");
913 fprintf(stderr, "\t7: ppoll with ulong_max fds, waits for input\n");
914 fprintf(stderr, "\t8: epoll_pwait with an invalid pointer, waits for "
915 "input\n");
916 fprintf(stderr, "\t9: epoll_pwait with maxevents set to INT_MAX, "
917 "waits for input\n");
918 fprintf(stderr, "\t10: ppoll with concurrent updates of the structure "
919 "from user-space, stress test (3000 iterations), "
920 "waits for input + timeout 1ms\n");
921 fprintf(stderr, "\t11: epoll_pwait with concurrent munmap of the buffer "
922 "from user-space, should randomly segfault, run "
923 "multiple times, waits for input + timeout 1ms\n");
924 }
925
926 int main(int argc, const char **argv)
927 {
928 int c, ret, test = -1;
929 poptContext optCon;
930 struct rlimit open_lim;
931 FILE *test_validation_output_file = NULL;
932 const char *test_validation_output_file_path = NULL;
933 struct poptOption optionsTable[] = {
934 { "test", 't', POPT_ARG_INT, &test, 0,
935 "Test to run", NULL },
936 { "list", 'l', 0, 0, 'l',
937 "List of tests (-t X)", NULL },
938 { "validation-file", 'o', POPT_ARG_STRING, &test_validation_output_file_path, 0,
939 "Test case output", NULL },
940 POPT_AUTOHELP
941 { NULL, 0, 0, NULL, 0, NULL, NULL }
942 };
943 const struct test_case *test_case;
944
945 optCon = poptGetContext(NULL, argc, argv, optionsTable, 0);
946
947 if (argc < 2) {
948 poptPrintUsage(optCon, stderr, 0);
949 ret = -1;
950 goto end;
951 }
952
953 ret = 0;
954
955 while ((c = poptGetNextOpt(optCon)) >= 0) {
956 switch (c) {
957 case 'l':
958 print_list();
959 goto end;
960 }
961 }
962
963 if (!test_validation_output_file_path) {
964 fprintf(stderr, "A test validation file path is required (--validation-file/-o)\n");
965 ret = -1;
966 goto end;
967 }
968
969 test_validation_output_file = fopen(test_validation_output_file_path, "w+");
970 if (!test_validation_output_file) {
971 PERROR("Failed to create test validation output file at '%s'",
972 test_validation_output_file_path);
973 ret = -1;
974 goto end;
975 }
976
977 open_lim.rlim_cur = MAX_FDS + MIN_NR_FDS;
978 open_lim.rlim_max = MAX_FDS + MIN_NR_FDS;
979
980 ret = setrlimit(RLIMIT_NOFILE, &open_lim);
981 if (ret < 0) {
982 PERROR("setrlimit");
983 goto end;
984 }
985
986 /*
987 * Some tests might segfault, but we need the getpid() to be output
988 * for the validation, disabling the buffering on the validation file
989 * works.
990 */
991 setbuf(test_validation_output_file, NULL);
992 wait_fd = STDIN_FILENO;
993
994 /* Test case id is 1-based. */
995 if (test < 1 || test > ARRAY_SIZE(test_cases)) {
996 poptPrintUsage(optCon, stderr, 0);
997 ret = -1;
998 }
999
1000 test_case = &test_cases[test - 1];
1001
1002 timeout = test_case->timeout;
1003 if (!test_case->produces_validation_info) {
1004 /*
1005 * All test cases need to provide, at minimum, the pid of the
1006 * test application.
1007 */
1008 ret = fprintf(test_validation_output_file, "{ \"pid\": %i }", getpid());
1009 if (ret < 0) {
1010 PERROR("Failed to write application pid to test validation file");
1011 goto end;
1012 }
1013 }
1014
1015 test_case->run(test_validation_output_file);
1016
1017 end:
1018 if (test_validation_output_file) {
1019 const int close_ret = fclose(test_validation_output_file);
1020
1021 if (close_ret) {
1022 PERROR("Failed to close test output file");
1023 }
1024 }
1025 poptFreeContext(optCon);
1026 return ret;
1027 }
This page took 0.050471 seconds and 4 git commands to generate.