Clean-up: modernize pretty_xml.cpp
[lttng-tools.git] / tests / regression / kernel / select_poll_epoll.cpp
index 225855ef747f18aa8ceb231009f15e403de4e0a7..b6f0210d503d24a03bf9e50235019d4b37c718be 100644 (file)
@@ -16,6 +16,7 @@
 #include <common/compat/time.hpp>
 #include <common/error.hpp>
 
+#include <errno.h>
 #include <fcntl.h>
 #include <limits.h>
 #include <poll.h>
@@ -55,35 +56,84 @@ static int wait_fd;
 int lttng_opt_quiet, lttng_opt_verbose, lttng_opt_mi;
 
 static void run_working_cases(FILE *validation_output_file);
-static void pselect_invalid_fd(FILE *validation_output_file);
 static void test_ppoll_big(FILE *validation_output_file);
-static void ppoll_fds_buffer_overflow(FILE *validation_output_file);
 static void pselect_invalid_pointer(FILE *validation_output_file);
+static void pselect_invalid_fd(FILE *validation_output_file);
 static void ppoll_fds_ulong_max(FILE *validation_output_file);
+static void ppoll_fds_buffer_overflow(FILE *validation_output_file);
 static void epoll_pwait_invalid_pointer(FILE *validation_output_file);
 static void epoll_pwait_int_max(FILE *validation_output_file);
 static void ppoll_concurrent_write(FILE *validation_output_file);
 static void epoll_pwait_concurrent_munmap(FILE *validation_output_file);
 
-using test_case_cb = void (*)(FILE *);
+typedef void (*test_case_cb)(FILE *output_file);
 
 namespace {
 const struct test_case {
        test_case_cb run;
        bool produces_validation_info;
        int timeout;
+       const char *name;
+       const char *description;
 } test_cases[] = {
-       { .run = run_working_cases, .produces_validation_info = true, .timeout = -1 },
-       { .run = run_working_cases, .produces_validation_info = true, .timeout = 1 },
-       { .run = pselect_invalid_fd, .produces_validation_info = false, .timeout = 0 },
-       { .run = test_ppoll_big, .produces_validation_info = false, .timeout = 0 },
-       { .run = ppoll_fds_buffer_overflow, .produces_validation_info = false, .timeout = 0 },
-       { .run = pselect_invalid_pointer, .produces_validation_info = false, .timeout = 0 },
-       { .run = ppoll_fds_ulong_max, .produces_validation_info = false, .timeout = 0 },
-       { .run = epoll_pwait_invalid_pointer, .produces_validation_info = true, .timeout = 0 },
-       { .run = epoll_pwait_int_max, .produces_validation_info = true, .timeout = 0 },
-       { .run = ppoll_concurrent_write, .produces_validation_info = false, .timeout = 0 },
-       { .run = epoll_pwait_concurrent_munmap, .produces_validation_info = true, .timeout = 0 },
+       { .run = run_working_cases,
+         .produces_validation_info = true,
+         .timeout = -1,
+         .name = "working_cases",
+         .description =
+                 "Working cases for select, pselect6, poll, ppoll and epoll, waiting for input" },
+       { .run = run_working_cases,
+         .produces_validation_info = true,
+         .timeout = 1,
+         .name = "working_cases_timeout",
+         .description = "Timeout cases (1ms) for select, pselect6, poll, ppoll and epoll" },
+       { .run = test_ppoll_big,
+         .produces_validation_info = false,
+         .timeout = 0,
+         .name = "ppoll_big",
+         .description = "ppoll with " XSTR(MAX_FDS) " FDs" },
+       { .run = epoll_pwait_invalid_pointer,
+         .produces_validation_info = true,
+         .timeout = 0,
+         .name = "epoll_pwait_invalid_pointer",
+         .description = "epoll_pwait with an invalid pointer, waits for input" },
+       { .run = epoll_pwait_int_max,
+         .produces_validation_info = true,
+         .timeout = 0,
+         .name = "epoll_pwait_int_max",
+         .description = "epoll_pwait with maxevents set to INT_MAX waits for input" },
+       { .run = ppoll_concurrent_write,
+         .produces_validation_info = false,
+         .timeout = 0,
+         .name = "ppoll_concurrent_write",
+         .description =
+                 "ppoll with concurrent updates of the structure from user-space, stress test (3000 iterations) waits for input + timeout 1ms" },
+       { .run = epoll_pwait_concurrent_munmap,
+         .produces_validation_info = true,
+         .timeout = 0,
+         .name = "epoll_pwait_concurrent_munmap",
+         .description =
+                 "epoll_pwait with concurrent munmap of the buffer from user-space, should randomly segfault, run multiple times, waits for input + timeout 1ms" },
+       { .run = pselect_invalid_pointer,
+         .produces_validation_info = false,
+         .timeout = 0,
+         .name = "pselect_invalid_pointer",
+         .description = "pselect with an invalid pointer, waits for input" },
+       { .run = pselect_invalid_fd,
+         .produces_validation_info = false,
+         .timeout = 0,
+         .name = "pselect_invalid_fd",
+         .description = "pselect with an invalid fd" },
+       { .run = ppoll_fds_ulong_max,
+         .produces_validation_info = false,
+         .timeout = 0,
+         .name = "ppoll_fds_ulong_max",
+         .description = "ppoll with ulong_max fds, waits for input" },
+       { .run = ppoll_fds_buffer_overflow,
+         .produces_validation_info = false,
+         .timeout = 0,
+         .name = "ppoll_fds_buffer_overflow",
+         .description = "ppoll buffer overflow, should segfault, waits for input" },
 };
 
 struct ppoll_thread_data {
@@ -138,7 +188,7 @@ end:
        return;
 }
 
-static void test_pselect(void)
+static void test_pselect_generic(long int syscall_id)
 {
        fd_set rfds;
        struct timespec tv;
@@ -152,9 +202,9 @@ static void test_pselect(void)
        tv.tv_nsec = timeout * MSEC_PER_NSEC;
 
        if (timeout > 0) {
-               ret = pselect(1, &rfds, NULL, NULL, &tv, NULL);
+               ret = syscall(syscall_id, 1, &rfds, NULL, NULL, &tv, NULL);
        } else {
-               ret = pselect(1, &rfds, NULL, NULL, NULL, NULL);
+               ret = syscall(syscall_id, 1, &rfds, NULL, NULL, NULL, NULL);
        }
 
        if (ret == -1) {
@@ -167,6 +217,11 @@ static void test_pselect(void)
        }
 }
 
+static void test_pselect(void)
+{
+       test_pselect_generic(SYS_pselect6);
+}
+
 static void test_select(void)
 {
        fd_set rfds;
@@ -217,7 +272,7 @@ static void test_poll(void)
        }
 }
 
-static void test_ppoll(void)
+static void test_ppoll_generic(long int syscall_id)
 {
        struct pollfd ufds[NB_FD];
        char buf[BUF_SIZE];
@@ -230,9 +285,9 @@ static void test_ppoll(void)
        if (timeout > 0) {
                ts.tv_sec = 0;
                ts.tv_nsec = timeout * MSEC_PER_NSEC;
-               ret = ppoll(ufds, 1, &ts, NULL);
+               ret = syscall(syscall_id, ufds, 1, &ts, NULL);
        } else {
-               ret = ppoll(ufds, 1, NULL, NULL);
+               ret = syscall(syscall_id, ufds, 1, NULL, NULL);
        }
 
        if (ret < 0) {
@@ -245,6 +300,11 @@ static void test_ppoll(void)
        }
 }
 
+static void test_ppoll(void)
+{
+       test_ppoll_generic(SYS_ppoll);
+}
+
 static void test_ppoll_big(FILE *validation_output_file __attribute__((unused)))
 {
        struct pollfd ufds[MAX_FDS];
@@ -400,12 +460,21 @@ static void run_working_cases(FILE *validation_output_file)
                wait_fd = pipe_fds[0];
        }
        test_select();
+#ifdef sys_pselect6_time64
+       test_pselect_time64();
+#else
        test_pselect();
+#endif /* sys_pselect6_time64 */
        test_select_big();
        test_poll();
        test_ppoll();
+#ifdef sys_ppoll_time64
+       test_ppoll_time64();
+#else
+       test_ppoll();
+#endif /* sys_ppoll_time64 */
 
-       ret = fprintf(validation_output_file, "{ \"pid\": %i", getpid());
+       ret = fprintf(validation_output_file, "{\"pid\": %i", getpid());
        if (ret < 0) {
                PERROR("Failed to write pid to test validation file");
                goto end;
@@ -436,11 +505,11 @@ end:
 }
 
 /*
- * Ask for 100 FDs in a buffer for allocated for only 1 FD, should
- * segfault (eventually with a "*** stack smashing detected ***" message).
- * The event should contain an array of 100 FDs filled with garbage.
+ * Ask for ULONG_MAX FDs in a buffer for allocated for only 1 FD, should
+ * cleanly fail with a "Invalid argument".
+ * The event should contain an empty array of FDs and overflow = 1.
  */
-static void ppoll_fds_buffer_overflow(FILE *validation_output_file __attribute__((unused)))
+static void generic_ppoll_fds_ulong_max(long int syscall_id)
 {
        struct pollfd ufds[NB_FD];
        char buf[BUF_SIZE];
@@ -449,24 +518,24 @@ static void ppoll_fds_buffer_overflow(FILE *validation_output_file __attribute__
        ufds[0].fd = wait_fd;
        ufds[0].events = POLLIN | POLLPRI;
 
-       ret = syscall(SYS_ppoll, ufds, 100, NULL, NULL);
-
+       /* ppoll and/or ppoll_time64 are used, depending on platform support. */
+       ret = syscall(syscall_id, ufds, ULONG_MAX, NULL, NULL);
        if (ret < 0) {
-               PERROR("ppoll");
+               /* Expected error. */
        } else if (ret > 0) {
                ret = read(wait_fd, buf, BUF_SIZE);
                if (ret < 0) {
-                       PERROR("[ppoll] read");
+                       PERROR("Failed to read from wait file descriptor");
                }
        }
 }
 
 /*
- * Ask for ULONG_MAX FDs in a buffer for allocated for only 1 FD, should
- * cleanly fail with a "Invalid argument".
- * The event should contain an empty array of FDs and overflow = 1.
+ * Ask for 100 FDs in a buffer for allocated for only 1 FD, should
+ * segfault (eventually with a "*** stack smashing detected ***" message).
+ * The event should contain an array of 100 FDs filled with garbage.
  */
-static void ppoll_fds_ulong_max(FILE *validation_output_file __attribute__((unused)))
+static void generic_ppoll_fds_buffer_overflow(long int syscall_id)
 {
        struct pollfd ufds[NB_FD];
        char buf[BUF_SIZE];
@@ -475,22 +544,41 @@ static void ppoll_fds_ulong_max(FILE *validation_output_file __attribute__((unus
        ufds[0].fd = wait_fd;
        ufds[0].events = POLLIN | POLLPRI;
 
-       ret = syscall(SYS_ppoll, ufds, ULONG_MAX, NULL, NULL);
+       /* ppoll and/or ppoll_time64 are used, depending on platform support. */
+       ret = syscall(syscall_id, ufds, 100, NULL, NULL);
        if (ret < 0) {
-               /* Expected error. */
+               PERROR("Failed to wait using ppoll/ppoll_time64");
        } else if (ret > 0) {
                ret = read(wait_fd, buf, BUF_SIZE);
                if (ret < 0) {
-                       PERROR("[ppoll] read");
+                       PERROR("Failed to read from wait file descriptor");
                }
        }
 }
 
+static void ppoll_fds_ulong_max(FILE *validation_output_file __attribute__((unused)))
+{
+#ifdef SYS_ppoll_time64
+       generic_ppoll_fds_ulong_max(SYS_ppoll_time64);
+#else
+       generic_ppoll_fds_ulong_max(SYS_ppoll);
+#endif /* SYS_ppoll_time64 */
+}
+
+static void ppoll_fds_buffer_overflow(FILE *validation_output_file __attribute__((unused)))
+{
+#ifdef SYS_ppoll_time64
+       generic_ppoll_fds_buffer_overflow(SYS_ppoll_time64);
+#else
+       generic_ppoll_fds_buffer_overflow(SYS_ppoll);
+#endif /* SYS_ppoll_time64 */
+}
+
 /*
  * Pass an invalid file descriptor to pselect6(). The syscall should return
  * -EBADF. The recorded event should contain a "ret = -EBADF (-9)".
  */
-static void pselect_invalid_fd(FILE *validation_output_file __attribute__((unused)))
+static void generic_invalid_fd(long int syscall_id)
 {
        fd_set rfds;
        int ret;
@@ -502,26 +590,26 @@ static void pselect_invalid_fd(FILE *validation_output_file __attribute__((unuse
         */
        fd = open("/dev/null", O_RDONLY);
        if (fd == -1) {
-               PERROR("open");
+               PERROR("Failed to open /dev/null");
                goto error;
        }
 
        ret = close(fd);
        if (ret == -1) {
-               PERROR("close");
+               PERROR("Failed to close /dev/null file descriptor");
                goto error;
        }
 
        FD_ZERO(&rfds);
        FD_SET(fd, &rfds);
 
-       ret = syscall(SYS_pselect6, fd + 1, &rfds, NULL, NULL, NULL, NULL);
+       ret = syscall(syscall_id, fd + 1, &rfds, NULL, NULL, NULL, NULL);
        if (ret == -1) {
                /* Expected error. */
        } else if (ret) {
                ret = read(wait_fd, buf, BUF_SIZE);
                if (ret < 0) {
-                       PERROR("[pselect] read");
+                       PERROR("Failed to read from wait file descriptor");
                }
        }
 error:
@@ -532,7 +620,7 @@ error:
  * Invalid pointer as writefds, should output a ppoll event
  * with 0 FDs.
  */
-static void pselect_invalid_pointer(FILE *validation_output_file __attribute__((unused)))
+static void generic_invalid_pointer(int syscall_id)
 {
        fd_set rfds;
        int ret;
@@ -542,17 +630,39 @@ static void pselect_invalid_pointer(FILE *validation_output_file __attribute__((
        FD_ZERO(&rfds);
        FD_SET(wait_fd, &rfds);
 
-       ret = syscall(SYS_pselect6, 1, &rfds, (fd_set *) invalid, NULL, NULL, NULL);
+       ret = syscall(syscall_id, 1, &rfds, (fd_set *) invalid, NULL, NULL, NULL);
        if (ret == -1) {
                /* Expected error. */
        } else if (ret) {
                ret = read(wait_fd, buf, BUF_SIZE);
                if (ret < 0) {
-                       PERROR("[pselect] read");
+                       PERROR("Failed to read from wait file descriptor");
                }
        }
 }
 
+static void pselect_invalid_fd(FILE *validation_output_file __attribute__((unused)))
+{
+#ifdef SYS_pselect6_time64
+       generic_invalid_fd(SYS_pselect6_time64);
+#else
+       generic_invalid_fd(SYS_pselect6);
+#endif /* SYS_pselect6_time64 */
+}
+
+/*
+ * Invalid pointer as writefds, should output a ppoll event
+ * with 0 FDs.
+ */
+static void pselect_invalid_pointer(FILE *validation_output_file __attribute__((unused)))
+{
+#ifdef SYS_pselect6_time64
+       generic_invalid_pointer(SYS_pselect6_time64);
+#else
+       generic_invalid_pointer(SYS_pselect6);
+#endif /* SYS_pselect6_time64 */
+}
+
 /*
  * Pass an invalid pointer to epoll_pwait, should fail with
  * "Bad address", the event returns 0 FDs.
@@ -570,8 +680,7 @@ static void epoll_pwait_invalid_pointer(FILE *validation_output_file)
                goto end;
        }
 
-       ret = fprintf(
-               validation_output_file, "{ \"epollfd\": %i, \"pid\": %i }", epollfd, getpid());
+       ret = fprintf(validation_output_file, "{\"epollfd\": %i, \"pid\": %i }", epollfd, getpid());
        if (ret < 0) {
                PERROR("[epoll_pwait] Failed to write test validation output");
                goto error;
@@ -621,8 +730,7 @@ static void epoll_pwait_int_max(FILE *validation_output_file)
                goto end;
        }
 
-       ret = fprintf(
-               validation_output_file, "{ \"epollfd\": %i, \"pid\": %i }", epollfd, getpid());
+       ret = fprintf(validation_output_file, "{\"epollfd\": %i, \"pid\": %i }", epollfd, getpid());
        if (ret < 0) {
                PERROR("[epoll_pwait] Failed to write test validation output");
                goto error;
@@ -793,8 +901,7 @@ static void epoll_pwait_concurrent_munmap(FILE *validation_output_file)
                goto end;
        }
 
-       ret = fprintf(
-               validation_output_file, "{ \"epollfd\": %i, \"pid\": %i }", epollfd, getpid());
+       ret = fprintf(validation_output_file, "{\"epollfd\": %i, \"pid\": %i }", epollfd, getpid());
        if (ret < 0) {
                PERROR("[epoll_pwait] Failed to write test validation output");
                goto error;
@@ -872,48 +979,72 @@ end:
 
 static void print_list(void)
 {
-       fprintf(stderr, "Test list (-t X):\n");
-       fprintf(stderr,
-               "\t1: Working cases for select, pselect6, poll, ppoll "
-               "and epoll, waiting for input\n");
-       fprintf(stderr,
-               "\t2: Timeout cases (1ms) for select, pselect6, poll, "
-               "ppoll and epoll\n");
-       fprintf(stderr, "\t3: pselect with an invalid fd\n");
-       fprintf(stderr, "\t4: ppoll with %d FDs\n", MAX_FDS);
-       fprintf(stderr,
-               "\t5: ppoll buffer overflow, should segfault, waits "
-               "for input\n");
-       fprintf(stderr,
-               "\t6: pselect with an invalid pointer, waits for "
-               "input\n");
-       fprintf(stderr, "\t7: ppoll with ulong_max fds, waits for input\n");
-       fprintf(stderr,
-               "\t8: epoll_pwait with an invalid pointer, waits for "
-               "input\n");
-       fprintf(stderr,
-               "\t9: epoll_pwait with maxevents set to INT_MAX, "
-               "waits for input\n");
-       fprintf(stderr,
-               "\t10: ppoll with concurrent updates of the structure "
-               "from user-space, stress test (3000 iterations), "
-               "waits for input + timeout 1ms\n");
-       fprintf(stderr,
-               "\t11: epoll_pwait with concurrent munmap of the buffer "
-               "from user-space, should randomly segfault, run "
-               "multiple times, waits for input + timeout 1ms\n");
+       printf("Test list (-t X):\n");
+
+       for (size_t test_id = 0; test_id < ARRAY_SIZE(test_cases); test_id++) {
+               printf("\t%zu: %s - %s\n",
+                      test_id + 1,
+                      test_cases[test_id].name,
+                      test_cases[test_id].description);
+       }
+}
+
+static void print_test_syscalls(void)
+{
+       const char *supported_syscalls[] = {
+#ifdef SYS_select
+               "select",
+#endif
+#if defined SYS_pselect6_time64 && defined SYS_pselect6
+               "pselect6",
+               "pselect6_time32",
+#elif defined SYS_pselect6_time32 ^ defined SYS_pselect6
+               "pselect6",
+#endif /* SYS_pselect6_time64 && defined SYS_pselect6 */
+#ifdef SYS_poll
+               "poll",
+#endif
+#if defined SYS_ppoll && defined SYS_ppoll_time64
+               "ppoll",
+               "ppoll_time32",
+#elif defined SYS_ppoll ^ defined SYS_ppoll_time64
+               "ppoll",
+#endif /* defined SYS_ppoll && defined SYS_ppoll_time64 */
+#ifdef SYS_epoll_ctl
+               "epoll_ctl",
+#endif
+#ifdef SYS_epoll_wait
+               "epoll_wait",
+#endif
+#ifdef SYS_epoll_pwait
+               "epoll_pwait",
+#endif
+       };
+
+       for (size_t i = 0; i < ARRAY_SIZE(supported_syscalls); i++) {
+               fputs(supported_syscalls[i], stdout);
+               fputs(i != ARRAY_SIZE(supported_syscalls) - 1 ? "," : "\n", stdout);
+       }
 }
 
 int main(int argc, const char **argv)
 {
-       int c, ret, test = -1;
+       int c, ret;
+       const char *test_name;
        poptContext optCon;
        struct rlimit open_lim;
        FILE *test_validation_output_file = NULL;
        const char *test_validation_output_file_path = NULL;
        struct poptOption optionsTable[] = {
-               { "test", 't', POPT_ARG_INT, &test, 0, "Test to run", NULL },
-               { "list", 'l', 0, 0, 'l', "List of tests (-t X)", NULL },
+               { "test", 't', POPT_ARG_STRING, &test_name, 0, "Name of test to run", NULL },
+               { "list-tests", 'l', 0, 0, 'l', "List tests (-t X)", NULL },
+               { "list-supported-test-syscalls",
+                 's',
+                 0,
+                 0,
+                 's',
+                 "List supported test syscalls",
+                 NULL },
                { "validation-file",
                  'o',
                  POPT_ARG_STRING,
@@ -924,6 +1055,7 @@ int main(int argc, const char **argv)
                POPT_AUTOHELP{ NULL, 0, 0, NULL, 0, NULL, NULL }
        };
        const struct test_case *test_case;
+       size_t test_case_id;
 
        optCon = poptGetContext(NULL, argc, argv, optionsTable, 0);
 
@@ -940,6 +1072,9 @@ int main(int argc, const char **argv)
                case 'l':
                        print_list();
                        goto end;
+               case 's':
+                       print_test_syscalls();
+                       goto end;
                }
        }
 
@@ -975,12 +1110,19 @@ int main(int argc, const char **argv)
        wait_fd = STDIN_FILENO;
 
        /* Test case id is 1-based. */
-       if (test < 1 || test > ARRAY_SIZE(test_cases)) {
+       for (test_case_id = 0; test_case_id < ARRAY_SIZE(test_cases); test_case_id++) {
+               if (!strcmp(test_cases[test_case_id].name, test_name)) {
+                       break;
+               }
+       }
+
+       if (test_case_id == ARRAY_SIZE(test_cases)) {
                poptPrintUsage(optCon, stderr, 0);
                ret = -1;
+               goto end;
        }
 
-       test_case = &test_cases[test - 1];
+       test_case = &test_cases[test_case_id];
 
        timeout = test_case->timeout;
        if (!test_case->produces_validation_info) {
@@ -988,7 +1130,7 @@ int main(int argc, const char **argv)
                 * All test cases need to provide, at minimum, the pid of the
                 * test application.
                 */
-               ret = fprintf(test_validation_output_file, "{ \"pid\": %i }", getpid());
+               ret = fprintf(test_validation_output_file, "{\"pid\": %i }", getpid());
                if (ret < 0) {
                        PERROR("Failed to write application pid to test validation file");
                        goto end;
This page took 0.030252 seconds and 4 git commands to generate.