]> git.proxmox.com Git - mirror_lxc.git/blobdiff - src/lxc/monitor.c
Merge the liblxc API work by Serge Hallyn.
[mirror_lxc.git] / src / lxc / monitor.c
index e51b59aace07594088fbd90b6cccbaa6ec9e9cac..2eb03978f98eb89696d2b3398fe3462d44ca1652 100644 (file)
@@ -98,11 +98,28 @@ int lxc_monitor_open(void)
        return fd;
 }
 
-int lxc_monitor_read(int fd, struct lxc_msg *msg)
+/* timeout of 0 means return immediately;  -1 means wait forever */
+int lxc_monitor_read_timeout(int fd, struct lxc_msg *msg, int timeout)
 {
        struct sockaddr_un from;
        socklen_t len = sizeof(from);
        int ret;
+       fd_set rfds;
+       struct timeval tv;
+
+       if (timeout != -1) {
+               FD_ZERO(&rfds);
+               FD_SET(fd, &rfds);
+
+               tv.tv_sec = timeout;
+               tv.tv_usec = 0;
+
+               ret = select(fd+1, &rfds, NULL, NULL, &tv);
+               if (ret == -1)
+                       return -1;
+               else if (!ret)
+                       return -2;  // timed out
+       }
 
        ret = recvfrom(fd, msg, sizeof(*msg), 0,
                       (struct sockaddr *)&from, &len);
@@ -114,6 +131,11 @@ int lxc_monitor_read(int fd, struct lxc_msg *msg)
        return ret;
 }
 
+int lxc_monitor_read(int fd, struct lxc_msg *msg)
+{
+       return lxc_monitor_read_timeout(fd, msg, -1);
+}
+
 int lxc_monitor_close(int fd)
 {
        return close(fd);