X-Git-Url: https://git.lttng.org/?p=lttng-tools.git;a=blobdiff_plain;f=src%2Fcommon%2Fcompat%2Fpoll.h;h=77c46a1d08d60ac8bca806628f368fc46fc915c2;hp=e69d59b42d054efbc900fd5412db093362fc5190;hb=a97dd6ce0d6f658c50b7cca73724fd8e1d5a36a1;hpb=3cc048812bf25b42003b40b6b868b9366faefafb diff --git a/src/common/compat/poll.h b/src/common/compat/poll.h index e69d59b42..77c46a1d0 100644 --- a/src/common/compat/poll.h +++ b/src/common/compat/poll.h @@ -52,6 +52,8 @@ static inline void __lttng_poll_free(void *events) #ifdef HAVE_EPOLL #include #include +#include +#include /* See man epoll(7) for this define path */ #define COMPAT_EPOLL_PROC_PATH "/proc/sys/fs/epoll/max_user_watches" @@ -71,7 +73,17 @@ enum { LPOLLNVAL = EPOLLHUP, LPOLLRDHUP = EPOLLRDHUP, /* Close on exec feature of epoll */ +#if defined(HAVE_EPOLL_CREATE1) && defined(EPOLL_CLOEXEC) LTTNG_CLOEXEC = EPOLL_CLOEXEC, +#else + /* + * EPOLL_CLOEXEC was added in glibc 2.8 (usually used in conjunction with + * epoll_create1(..)), but since neither EPOLL_CLOEXEC exists nor + * epoll_create1(..), we set it to FD_CLOEXEC so that we can pass it + * directly to fcntl(..) instead. + */ + LTTNG_CLOEXEC = FD_CLOEXEC, +#endif }; struct compat_epoll_event { @@ -115,6 +127,27 @@ extern int compat_epoll_create(struct lttng_poll_event *events, #define lttng_poll_create(events, size, flags) \ compat_epoll_create(events, size, flags) +#if defined(HAVE_EPOLL_CREATE1) && defined(EPOLL_CLOEXEC) +static inline int compat_glibc_epoll_create(int size __attribute__((unused)), + int flags) +{ + return epoll_create1(flags); +} +#else +static inline int compat_glibc_epoll_create(int size, int flags) +{ + /* + * epoll_create1 was added in glibc 2.9, but unfortunatly reverting to + * epoll_create(..) also means that we lose the possibility to + * directly set the EPOLL_CLOEXEC, so try and do it anyway but through + * fcntl(..). + */ + int efd = epoll_create(size); + assert(fcntl(efd, F_SETFD, flags) != -1); + return efd; +} +#endif + /* * Wait on epoll set with the number of fd registered to the lttng_poll_event * data structure (events). @@ -141,7 +174,7 @@ extern int compat_epoll_del(struct lttng_poll_event *events, int fd); /* * Set up the poll set limits variable poll_max_size */ -extern void compat_epoll_set_max_size(void); +extern int compat_epoll_set_max_size(void); #define lttng_poll_set_max_size() \ compat_epoll_set_max_size() @@ -158,6 +191,17 @@ static inline void lttng_poll_reset(struct lttng_poll_event *events) } } +/* + * Initialize an already allocated poll event data structure. For epoll(), the + * epfd is set to -1 to indicate that it's not usable. + */ +static inline void lttng_poll_init(struct lttng_poll_event *events) +{ + memset(events, 0, sizeof(struct lttng_poll_event)); + /* Set fd to -1 so if clean before created, we don't close 0. */ + events->epfd = -1; +} + /* * Clean the events structure of a lttng_poll_event. It's the caller * responsability to free the lttng_poll_event memory. @@ -173,7 +217,7 @@ static inline void lttng_poll_clean(struct lttng_poll_event *events) if (events->epfd >= 0) { ret = close(events->epfd); if (ret) { - perror("close"); + PERROR("close"); } } @@ -210,7 +254,7 @@ enum { #if __linux__ LPOLLMSG = POLLMSG, LPOLLRDHUP = POLLRDHUP, -#elif (defined(__FreeBSD__) || defined(__CYGWIN__)) +#elif (defined(__FreeBSD__) || defined(__CYGWIN__) || defined(__sun__) || defined(__APPLE__)) LPOLLMSG = 0, LPOLLRDHUP = 0, #else @@ -241,8 +285,7 @@ struct compat_poll_event { * 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; }; @@ -307,7 +350,7 @@ extern int compat_poll_del(struct lttng_poll_event *events, int fd); /* * Set up the poll set limits variable poll_max_size */ -extern void compat_poll_set_max_size(void); +extern int compat_poll_set_max_size(void); #define lttng_poll_set_max_size() \ compat_poll_set_max_size() @@ -317,6 +360,14 @@ extern void compat_poll_set_max_size(void); static inline void lttng_poll_reset(struct lttng_poll_event *events) {} +/* + * Initialize an already allocated poll event data structure. + */ +static inline void lttng_poll_init(struct lttng_poll_event *events) +{ + memset(events, 0, sizeof(struct lttng_poll_event)); +} + /* * Clean the events structure of a lttng_poll_event. It's the caller * responsability to free the lttng_poll_event memory.