Fix: poll and epoll fd set reallocation
[lttng-tools.git] / src / common / compat / poll.h
index 1580a4fb211a24def1146d7ec5c721431c34e3b4..2cfad9a25fa1c7e0339ff8e810796c58f1b5b552 100644 (file)
@@ -1,18 +1,18 @@
 /*
  * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
  *
- * This program is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License as published by the Free
- * Software Foundation; only version 2 of the License.
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License, version 2 only,
+ * as published by the Free Software Foundation.
  *
  * This program is distributed in the hope that it will be useful, but WITHOUT
  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  * more details.
  *
- * You should have received a copy of the GNU General Public License along with
- * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
- * Place - Suite 330, Boston, MA  02111-1307, USA.
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 
 #ifndef _LTT_POLL_H
@@ -42,7 +42,9 @@ extern unsigned int poll_max_size;
  */
 static inline void __lttng_poll_free(void *events)
 {
-       free(events);
+       if (events) {
+               free(events);
+       }
 }
 
 /*
@@ -50,6 +52,7 @@ static inline void __lttng_poll_free(void *events)
  */
 #ifdef HAVE_EPOLL
 #include <sys/epoll.h>
+#include <stdio.h>
 
 /* See man epoll(7) for this define path */
 #define COMPAT_EPOLL_PROC_PATH "/proc/sys/fs/epoll/max_user_watches"
@@ -75,7 +78,8 @@ enum {
 struct compat_epoll_event {
        int epfd;
        uint32_t nb_fd;       /* Current number of fd in events */
-       uint32_t events_size; /* Size of events array */
+       uint32_t alloc_size; /* Size of events array */
+       uint32_t init_size;     /* Initial size of events array */
        struct epoll_event *events;
 };
 #define lttng_poll_event compat_epoll_event
@@ -146,8 +150,13 @@ static inline void lttng_poll_reset(struct lttng_poll_event *events)
  */
 static inline void lttng_poll_clean(struct lttng_poll_event *events)
 {
+       int ret;
+
        if (events) {
-               close(events->epfd);
+               ret = close(events->epfd);
+               if (ret) {
+                       perror("close");
+               }
                __lttng_poll_free((void *) events->events);
        }
 }
@@ -179,29 +188,55 @@ enum {
        LPOLLRDBAND = POLLRDBAND,
        LPOLLWRNORM = POLLWRNORM,
        LPOLLWRBAND = POLLWRBAND,
+#if __linux__
        LPOLLMSG = POLLMSG,
+       LPOLLRDHUP = POLLRDHUP,
+#elif (defined(__FreeBSD__) || defined(__CYGWIN__))
+       LPOLLMSG = 0,
+       LPOLLRDHUP = 0,
+#else
+#error "Please add support for your OS."
+#endif /* __linux__ */
        LPOLLERR = POLLERR,
        LPOLLHUP = POLLHUP | POLLNVAL,
-       LPOLLRDHUP = POLLRDHUP,
        /* Close on exec feature does not exist for poll(2) */
        LTTNG_CLOEXEC = 0xdead,
 };
 
-struct compat_poll_event {
+struct compat_poll_event_array {
        uint32_t nb_fd;       /* Current number of fd in events */
-       uint32_t events_size; /* Size of events array */
+       uint32_t alloc_size; /* Size of events array */
+       /* Initial size of the pollset. We never shrink below that. */
+       uint32_t init_size;
        struct pollfd *events;
 };
+
+struct compat_poll_event {
+       /*
+        * Modified by the wait action. Updated using current fields if the
+        * need_update flag is set.
+        */
+       struct compat_poll_event_array wait;
+       /*
+        * This is modified by add/del actions being the _current_ flow of
+        * execution before a poll wait is done.
+        */
+       struct compat_poll_event_array current;
+       /* Indicate if wait.events needs to be realloc. */
+       int need_realloc:1;
+       /* Indicate if wait.events need to be updated from current. */
+       int need_update:1;
+};
 #define lttng_poll_event compat_poll_event
 
 /*
  * For the following calls, consider 'e' to be a lttng_poll_event pointer and i
  * being the index of the events array.
  */
-#define LTTNG_POLL_GETFD(e, i) LTTNG_REF(e)->events[i].fd
-#define LTTNG_POLL_GETEV(e, i) LTTNG_REF(e)->events[i].revents
-#define LTTNG_POLL_GETNB(e) LTTNG_REF(e)->nb_fd
-#define LTTNG_POLL_GETSZ(e) LTTNG_REF(e)->events_size
+#define LTTNG_POLL_GETFD(e, i) LTTNG_REF(e)->wait.events[i].fd
+#define LTTNG_POLL_GETEV(e, i) LTTNG_REF(e)->wait.events[i].revents
+#define LTTNG_POLL_GETNB(e) LTTNG_REF(e)->wait.nb_fd
+#define LTTNG_POLL_GETSZ(e) LTTNG_REF(e)->wait.events_size
 
 /*
  * Create a pollfd structure of size 'size'.
@@ -255,7 +290,8 @@ static inline void lttng_poll_reset(struct lttng_poll_event *events)
 static inline void lttng_poll_clean(struct lttng_poll_event *events)
 {
        if (events) {
-               __lttng_poll_free((void *) events->events);
+               __lttng_poll_free((void *) events->wait.events);
+               __lttng_poll_free((void *) events->current.events);
        }
 }
 
This page took 0.02601 seconds and 4 git commands to generate.