Fix: Tests: leaking epoll fd
[lttng-tools.git] / tests / regression / kernel / select_poll_epoll.c
index 08e7fce0d898df547da42e371dc694f90bea4141..b1eaf6045f9b3b16dcffc9bee88f61b0d358197b 100644 (file)
@@ -1,3 +1,10 @@
+/*
+ * Copyright (C) 2016 Julien Desfossez <jdesfossez@efficios.com>
+ *
+ * SPDX-License-Identifier: GPL-2.0-only
+ *
+ */
+
 #include <stdio.h>
 #include <poll.h>
 #include <signal.h>
@@ -31,7 +38,7 @@
 #define MSEC_PER_NSEC (MSEC_PER_USEC * 1000)
 
 static int timeout; /* seconds, -1 to disable */
-volatile static int stop_thread;
+static volatile int stop_thread;
 static int wait_fd;
 
 struct ppoll_thread_data {
@@ -39,6 +46,7 @@ struct ppoll_thread_data {
        int value;
 };
 
+static
 void test_select_big(void)
 {
        fd_set rfds, wfds, exfds;
@@ -88,6 +96,7 @@ end:
        return;
 }
 
+static
 void test_pselect(void)
 {
        fd_set rfds;
@@ -121,6 +130,7 @@ void test_pselect(void)
 
 }
 
+static
 void test_select(void)
 {
        fd_set rfds;
@@ -154,6 +164,7 @@ void test_select(void)
 
 }
 
+static
 void test_poll(void)
 {
        struct pollfd ufds[NB_FD];
@@ -178,6 +189,7 @@ void test_poll(void)
        }
 }
 
+static
 void test_ppoll(void)
 {
        struct pollfd ufds[NB_FD];
@@ -210,6 +222,7 @@ void test_ppoll(void)
        }
 }
 
+static
 void test_ppoll_big(void)
 {
        struct pollfd ufds[MAX_FDS];
@@ -249,6 +262,7 @@ void test_ppoll_big(void)
        return;
 }
 
+static
 void test_epoll(void)
 {
        int ret, epollfd;
@@ -266,7 +280,7 @@ void test_epoll(void)
        ret = epoll_ctl(epollfd, EPOLL_CTL_ADD, wait_fd, &epoll_event);
        if (ret < 0) {
                perror("[epoll] add");
-               goto end;
+               goto error;
        }
 
        if (timeout > 0) {
@@ -287,10 +301,13 @@ void test_epoll(void)
                perror("epoll_wait");
        }
 
+error:
+       close(epollfd);
 end:
        return;
 }
 
+static
 void test_pepoll(void)
 {
        int ret, epollfd;
@@ -308,7 +325,7 @@ void test_pepoll(void)
        ret = epoll_ctl(epollfd, EPOLL_CTL_ADD, wait_fd, &epoll_event);
        if (ret < 0) {
                perror("[eppoll] add");
-               goto end;
+               goto error;
        }
 
        if (timeout > 0) {
@@ -329,10 +346,13 @@ void test_pepoll(void)
                perror("epoll_pwait");
        }
 
+error:
+       close(epollfd);
 end:
        return;
 }
 
+static
 void run_working_cases(void)
 {
        int ret;
@@ -379,6 +399,7 @@ end:
  * segfault (eventually with a "*** stack smashing detected ***" message).
  * The event should contain an array of 100 FDs filled with garbage.
  */
+static
 void ppoll_fds_buffer_overflow(void)
 {
        struct pollfd ufds[NB_FD];
@@ -410,6 +431,7 @@ void ppoll_fds_buffer_overflow(void)
  * cleanly fail with a "Invalid argument".
  * The event should contain an empty array of FDs and overflow = 1.
  */
+static
 void ppoll_fds_ulong_max(void)
 {
        struct pollfd ufds[NB_FD];
@@ -440,6 +462,7 @@ void ppoll_fds_ulong_max(void)
  * 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(void)
 {
        fd_set rfds;
@@ -486,6 +509,7 @@ error:
  * Invalid pointer as writefds, should output a ppoll event
  * with 0 FDs.
  */
+static
 void pselect_invalid_pointer(void)
 {
        fd_set rfds;
@@ -517,6 +541,7 @@ void pselect_invalid_pointer(void)
  * Pass an invalid pointer to epoll_pwait, should fail with
  * "Bad address", the event returns 0 FDs.
  */
+static
 void epoll_pwait_invalid_pointer(void)
 {
        int ret, epollfd;
@@ -535,7 +560,7 @@ void epoll_pwait_invalid_pointer(void)
        ret = epoll_ctl(epollfd, EPOLL_CTL_ADD, wait_fd, &epoll_event);
        if (ret < 0) {
                perror("[eppoll] add");
-               goto end;
+               goto error;
        }
 
        ret = syscall(SYS_epoll_pwait, epollfd,
@@ -553,6 +578,8 @@ void epoll_pwait_invalid_pointer(void)
                perror("# epoll_pwait");
        }
 
+error:
+       close(epollfd);
 end:
        return;
 }
@@ -561,6 +588,7 @@ end:
  * Set maxevents to INT_MAX, should output "Invalid argument"
  * The event should return an empty array.
  */
+static
 void epoll_pwait_int_max(void)
 {
        int ret, epollfd;
@@ -578,7 +606,7 @@ void epoll_pwait_int_max(void)
        ret = epoll_ctl(epollfd, EPOLL_CTL_ADD, wait_fd, &epoll_event);
        if (ret < 0) {
                perror("[eppoll] add");
-               goto end;
+               goto error;
        }
 
        ret = syscall(SYS_epoll_pwait, epollfd, &epoll_event, INT_MAX, -1,
@@ -596,10 +624,13 @@ void epoll_pwait_int_max(void)
                perror("# epoll_pwait");
        }
 
+error:
+       close(epollfd);
 end:
        return;
 }
 
+static
 void *ppoll_writer(void *arg)
 {
        struct ppoll_thread_data *data = (struct ppoll_thread_data *) arg;
@@ -613,6 +644,7 @@ void *ppoll_writer(void *arg)
        return NULL;
 }
 
+static
 void do_ppoll(int *fds, struct pollfd *ufds)
 {
        int i, ret;
@@ -642,6 +674,7 @@ void do_ppoll(int *fds, struct pollfd *ufds)
        }
 }
 
+static
 void stress_ppoll(int *fds, int value)
 {
        pthread_t writer;
@@ -683,6 +716,7 @@ end:
  *
  * ppoll should work as expected and the trace should be readable at the end.
  */
+static
 void ppoll_concurrent_write(void)
 {
        int i, ret, fds[MAX_FDS];
@@ -708,6 +742,7 @@ void ppoll_concurrent_write(void)
        return;
 }
 
+static
 void *epoll_pwait_writer(void *addr)
 {
        srand(time(NULL));
@@ -725,6 +760,7 @@ void *epoll_pwait_writer(void *addr)
  * buffer allocated for the returned data. This should randomly segfault.
  * The trace should be readable and no kernel OOPS should occur.
  */
+static
 void epoll_pwait_concurrent_munmap(void)
 {
        int ret, epollfd, i, fds[MAX_FDS];
@@ -732,6 +768,9 @@ void epoll_pwait_concurrent_munmap(void)
        struct epoll_event *epoll_event;
        pthread_t writer;
 
+       for (i = 0; i < MAX_FDS; i++) {
+               fds[i] = -1;
+       }
        epollfd = epoll_create(MAX_FDS);
        if (epollfd < 0) {
                perror("[eppoll] create");
@@ -743,7 +782,7 @@ void epoll_pwait_concurrent_munmap(void)
                        -1, 0);
        if (epoll_event == MAP_FAILED) {
                perror("mmap");
-               goto end;
+               goto error;
        }
 
        for (i = 0; i < MAX_FDS; i++) {
@@ -756,7 +795,7 @@ void epoll_pwait_concurrent_munmap(void)
                ret = epoll_ctl(epollfd, EPOLL_CTL_ADD, fds[i], epoll_event);
                if (ret < 0) {
                        perror("[eppoll] add");
-                       goto end_unmap;
+                       goto error_unmap;
                }
        }
        stop_thread = 0;
@@ -764,7 +803,7 @@ void epoll_pwait_concurrent_munmap(void)
                        (void *) epoll_event);
        if (ret != 0) {
                fprintf(stderr, "[error] pthread_create\n");
-               goto end_unmap;
+               goto error_unmap;
        }
 
        ret = epoll_pwait(epollfd, epoll_event, 1, 1, NULL);
@@ -785,9 +824,9 @@ void epoll_pwait_concurrent_munmap(void)
        ret = pthread_join(writer, NULL);
        if (ret) {
                fprintf(stderr, "[error] pthread_join\n");
-               goto end_unmap;
+               goto error_unmap;
        }
-end_unmap:
+error_unmap:
        for (i = 0; i < MAX_FDS; i++) {
                ret = close(fds[i]);
                if (ret != 0) {
@@ -800,19 +839,13 @@ end_unmap:
                perror("munmap");
        }
 
+error:
+       close(epollfd);
 end:
        return;
 }
 
-void usage(poptContext optCon, int exitcode, char *error, char *addl)
-{
-       poptPrintUsage(optCon, stderr, 0);
-       if (error) {
-               fprintf(stderr, "%s: %s\n", error, addl);
-       }
-       exit(exitcode);
-}
-
+static
 void print_list(void)
 {
        fprintf(stderr, "Test list (-t X):\n");
This page took 0.028768 seconds and 4 git commands to generate.