]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
use poll instead of select
authorSerge Hallyn <serge.hallyn@ubuntu.com>
Tue, 21 Apr 2015 18:34:45 +0000 (13:34 -0500)
committerStéphane Graber <stgraber@ubuntu.com>
Wed, 1 Jul 2015 15:51:14 +0000 (11:51 -0400)
Particularly when using the go-lxc api with lots of threads, it
happens that if the open files limit is > 1024, we will try to
select on fd > 1024 which breaks on glibc.

So use poll instead of select.

Signed-off-by: Serge Hallyn <serge.hallyn@ubuntu.com>
Acked-by: Stéphane Graber <stgraber@ubuntu.com>
src/lxc/cgmanager.c
src/lxc/lxc.h
src/lxc/lxc_monitor.c
src/lxc/monitor.c
src/lxc/monitor.h

index fe8913e41c078690421e71a84338c0ba67bebc18..c8d0745b415cc6e1b3e4167f46cc07724e2cff25 100644 (file)
@@ -39,6 +39,7 @@
 #include <sys/mount.h>
 #include <netinet/in.h>
 #include <net/if.h>
+#include <poll.h>
 
 #include "error.h"
 #include "commands.h"
@@ -347,6 +348,7 @@ static int do_chown_cgroup(const char *controller, const char *cgroup_path,
 {
        int sv[2] = {-1, -1}, optval = 1, ret = -1;
        char buf[1];
+       struct pollfd fds;
 
        if (socketpair(AF_UNIX, SOCK_DGRAM, 0, sv) < 0) {
                SYSERROR("Error creating socketpair");
@@ -370,10 +372,10 @@ static int do_chown_cgroup(const char *controller, const char *cgroup_path,
        }
        /* now send credentials */
 
-       fd_set rfds;
-       FD_ZERO(&rfds);
-       FD_SET(sv[0], &rfds);
-       if (select(sv[0]+1, &rfds, NULL, NULL, NULL) < 0) {
+       fds.fd = sv[0];
+       fds.events = POLLIN;
+       fds.revents = 0;
+       if (poll(&fds, 1, -1) <= 0) {
                ERROR("Error getting go-ahead from server: %s", strerror(errno));
                goto out;
        }
@@ -385,9 +387,10 @@ static int do_chown_cgroup(const char *controller, const char *cgroup_path,
                SYSERROR("%s: Error sending pid over SCM_CREDENTIAL", __func__);
                goto out;
        }
-       FD_ZERO(&rfds);
-       FD_SET(sv[0], &rfds);
-       if (select(sv[0]+1, &rfds, NULL, NULL, NULL) < 0) {
+       fds.fd = sv[0];
+       fds.events = POLLIN;
+       fds.revents = 0;
+       if (poll(&fds, 1, -1) <= 0) {
                ERROR("Error getting go-ahead from server: %s", strerror(errno));
                goto out;
        }
@@ -399,9 +402,10 @@ static int do_chown_cgroup(const char *controller, const char *cgroup_path,
                SYSERROR("%s: Error sending pid over SCM_CREDENTIAL", __func__);
                goto out;
        }
-       FD_ZERO(&rfds);
-       FD_SET(sv[0], &rfds);
-       if (select(sv[0]+1, &rfds, NULL, NULL, NULL) < 0) {
+       fds.fd = sv[0];
+       fds.events = POLLIN;
+       fds.revents = 0;
+       if (poll(&fds, 1, -1) <= 0) {
                ERROR("Error getting go-ahead from server: %s", strerror(errno));
                goto out;
        }
index b1b858dc3885cd1df1ef2aaa45992a46c8478d3f..a9fa80e6abd89f493e2695f00dc681259d3b1a5e 100644 (file)
@@ -67,44 +67,6 @@ extern int lxc_execute(const char *name, char *const argv[], int quiet,
                       struct lxc_conf *conf, const char *lxcpath,
                       bool backgrounded);
 
-/*
- * Open the monitoring mechanism for a specific container
- * The function will return an fd corresponding to the events
- * Returns a file descriptor on success, < 0 otherwise
- */
-extern int lxc_monitor_open(const char *lxcpath);
-
-/*
- * Blocking read for the next container state change
- * @fd  : the file descriptor provided by lxc_monitor_open
- * @msg : the variable which will be filled with the state
- * Returns 0 if the monitored container has exited, > 0 if
- * data was read, < 0 otherwise
- */
-extern int lxc_monitor_read(int fd, struct lxc_msg *msg);
-
-/*
- * Blocking read for the next container state change with timeout
- * @fd      : the file descriptor provided by lxc_monitor_open
- * @msg     : the variable which will be filled with the state
- * @timeout : the timeout in seconds to wait for a state change
- * Returns 0 if the monitored container has exited, > 0 if
- * data was read, < 0 otherwise
- */
-extern int lxc_monitor_read_timeout(int fd, struct lxc_msg *msg, int timeout);
-
-/*
- * Blocking read from multiple monitors for the next container state
- * change with timeout
- * @rfds    : an fd_set of file descriptors provided by lxc_monitor_open
- * @nfds    : the maximum fd number in rfds + 1
- * @msg     : the variable which will be filled with the state
- * @timeout : the timeout in seconds to wait for a state change
- * Returns 0 if the monitored container has exited, > 0 if
- * data was read, < 0 otherwise
- */
-extern int lxc_monitor_read_fdset(fd_set *rfds, int nfds, struct lxc_msg *msg, int timeout);
-
 /*
  * Close the fd associated with the monitoring
  * @fd : the file descriptor provided by lxc_monitor_open
index ede19ff920545e285c208fce04d79fb67affdb9a..b3d8912c2729e807e0738c23f46c99392cc6146e 100644 (file)
@@ -29,6 +29,7 @@
 #include <sys/types.h>
 #include <sys/wait.h>
 #include <errno.h>
+#include <poll.h>
 
 #include "lxc.h"
 #include "log.h"
@@ -75,8 +76,9 @@ int main(int argc, char *argv[])
        char *regexp;
        struct lxc_msg msg;
        regex_t preg;
-       fd_set rfds, rfds_save;
-       int len, rc, i, nfds = -1;
+       struct pollfd *fds;
+       nfds_t nfds;
+       int len, rc, i;
 
        if (lxc_arguments_parse(&my_args, argc, argv))
                return 1;
@@ -131,13 +133,14 @@ int main(int argc, char *argv[])
        }
        free(regexp);
 
-       if (my_args.lxcpath_cnt > FD_SETSIZE) {
-               ERROR("too many paths requested, only the first %d will be monitored", FD_SETSIZE);
-               my_args.lxcpath_cnt = FD_SETSIZE;
+       fds = malloc(my_args.lxcpath_cnt * sizeof(struct pollfd));
+       if (!fds) {
+               SYSERROR("out of memory");
+               return -1;
        }
 
-       FD_ZERO(&rfds);
-       for (i = 0; i < my_args.lxcpath_cnt; i++) {
+       nfds = my_args.lxcpath_cnt;
+       for (i = 0; i < nfds; i++) {
                int fd;
 
                lxc_monitord_spawn(my_args.lxcpath[i]);
@@ -147,19 +150,15 @@ int main(int argc, char *argv[])
                        regfree(&preg);
                        return 1;
                }
-               FD_SET(fd, &rfds);
-               if (fd > nfds)
-                       nfds = fd;
+               fds[i].fd = fd;
+               fds[i].events = POLLIN;
+               fds[i].revents = 0;
        }
-       memcpy(&rfds_save, &rfds, sizeof(rfds_save));
-       nfds++;
 
        setlinebuf(stdout);
 
        for (;;) {
-               memcpy(&rfds, &rfds_save, sizeof(rfds));
-
-               if (lxc_monitor_read_fdset(&rfds, nfds, &msg, -1) < 0) {
+               if (lxc_monitor_read_fdset(fds, nfds, &msg, -1) < 0) {
                        regfree(&preg);
                        return 1;
                }
index 1e1c094a9fbec30a55d839d3928750f808787bb4..0e56f75ee88dc788683297c1ad73ecaafb14f5a9 100644 (file)
@@ -38,6 +38,7 @@
 #include <sys/wait.h>
 #include <netinet/in.h>
 #include <net/if.h>
+#include <poll.h>
 
 #include "error.h"
 #include "af_unix.h"
@@ -219,19 +220,13 @@ err1:
        return ret;
 }
 
-int lxc_monitor_read_fdset(fd_set *rfds, int nfds, struct lxc_msg *msg,
+int lxc_monitor_read_fdset(struct pollfd *fds, nfds_t nfds, struct lxc_msg *msg,
                           int timeout)
 {
-       struct timeval tval,*tv = NULL;
-       int ret,i;
-
-       if (timeout != -1) {
-               tv = &tval;
-               tv->tv_sec = timeout;
-               tv->tv_usec = 0;
-       }
+       long i;
+       int ret;
 
-       ret = select(nfds, rfds, NULL, NULL, tv);
+       ret = poll(fds, nfds, timeout * 1000);
        if (ret == -1)
                return -1;
        else if (ret == 0)
@@ -241,8 +236,9 @@ int lxc_monitor_read_fdset(fd_set *rfds, int nfds, struct lxc_msg *msg,
         * for when this routine is called again
         */
        for (i = 0; i < nfds; i++) {
-               if (FD_ISSET(i, rfds)) {
-                       ret = recv(i, msg, sizeof(*msg), 0);
+               if (fds[i].revents != 0) {
+                       fds[i].revents = 0;
+                       ret = recv(fds[i].fd, msg, sizeof(*msg), 0);
                        if (ret <= 0) {
                                SYSERROR("client failed to recv (monitord died?) %s",
                                         strerror(errno));
@@ -257,12 +253,13 @@ int lxc_monitor_read_fdset(fd_set *rfds, int nfds, struct lxc_msg *msg,
 
 int lxc_monitor_read_timeout(int fd, struct lxc_msg *msg, int timeout)
 {
-       fd_set rfds;
+       struct pollfd fds;
 
-       FD_ZERO(&rfds);
-       FD_SET(fd, &rfds);
+       fds.fd = fd;
+       fds.events = POLLIN | POLLPRI;
+       fds.revents = 0;
 
-       return lxc_monitor_read_fdset(&rfds, fd+1, msg, timeout);
+       return lxc_monitor_read_fdset(&fds, 1, msg, timeout);
 }
 
 int lxc_monitor_read(int fd, struct lxc_msg *msg)
index 500a9f250c27a79e16de745228e3147f7d25db13..7a350d8518b2133241ad94be2a8384801e14f5ae 100644 (file)
@@ -26,6 +26,7 @@
 #include <limits.h>
 #include <sys/param.h>
 #include <sys/un.h>
+#include <poll.h>
 
 #include "conf.h"
 
@@ -41,7 +42,6 @@ struct lxc_msg {
        int value;
 };
 
-extern int lxc_monitor_open(const char *lxcpath);
 extern int lxc_monitor_sock_name(const char *lxcpath, struct sockaddr_un *addr);
 extern int lxc_monitor_fifo_name(const char *lxcpath, char *fifo_path,
                                 size_t fifo_path_sz, int do_mkdirp);
@@ -51,4 +51,44 @@ extern void lxc_monitor_send_exit_code(const char *name, int exit_code,
                            const char *lxcpath);
 extern int lxc_monitord_spawn(const char *lxcpath);
 
+/*
+ * Open the monitoring mechanism for a specific container
+ * The function will return an fd corresponding to the events
+ * Returns a file descriptor on success, < 0 otherwise
+ */
+extern int lxc_monitor_open(const char *lxcpath);
+
+/*
+ * Blocking read for the next container state change
+ * @fd  : the file descriptor provided by lxc_monitor_open
+ * @msg : the variable which will be filled with the state
+ * Returns 0 if the monitored container has exited, > 0 if
+ * data was read, < 0 otherwise
+ */
+extern int lxc_monitor_read(int fd, struct lxc_msg *msg);
+
+/*
+ * Blocking read for the next container state change with timeout
+ * @fd      : the file descriptor provided by lxc_monitor_open
+ * @msg     : the variable which will be filled with the state
+ * @timeout : the timeout in seconds to wait for a state change
+ * Returns 0 if the monitored container has exited, > 0 if
+ * data was read, < 0 otherwise
+ */
+extern int lxc_monitor_read_timeout(int fd, struct lxc_msg *msg, int timeout);
+
+/*
+ * Blocking read from multiple monitors for the next container state
+ * change with timeout
+ * @fds     : struct pollfd descripting the fds to use
+ * @nfds    : the number of entries in fds
+ * @msg     : the variable which will be filled with the state
+ * @timeout : the timeout in seconds to wait for a state change
+ * Returns 0 if the monitored container has exited, > 0 if
+ * data was read, < 0 otherwise
+ */
+extern int lxc_monitor_read_fdset(struct pollfd *fds, nfds_t nfds, struct lxc_msg *msg,
+                          int timeout);
+
+
 #endif