#include <sys/mount.h>
#include <netinet/in.h>
#include <net/if.h>
+#include <poll.h>
#include "error.h"
#include "commands.h"
{
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");
}
/* 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;
}
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;
}
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;
}
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
#include <sys/types.h>
#include <sys/wait.h>
#include <errno.h>
+#include <poll.h>
#include "lxc.h"
#include "log.h"
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;
}
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]);
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;
}
#include <sys/wait.h>
#include <netinet/in.h>
#include <net/if.h>
+#include <poll.h>
#include "error.h"
#include "af_unix.h"
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)
* 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));
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)
#include <limits.h>
#include <sys/param.h>
#include <sys/un.h>
+#include <poll.h>
#include "conf.h"
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);
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