[CRIU] [PATCH 1/2] util: add a function to check output data in a file descriptor

Andrey Vagin avagin at openvz.org
Mon Sep 1 13:25:13 PDT 2014


We can't dump netlink socket, inotify, fanotify, if they have queued
data, so lets add a function to chech this.

Signed-off-by: Andrey Vagin <avagin at openvz.org>
---
 include/util.h |  3 +++
 sk-netlink.c   |  8 ++------
 util.c         | 14 ++++++++++++++
 3 files changed, 19 insertions(+), 6 deletions(-)

diff --git a/include/util.h b/include/util.h
index 7d16aa2..908172a 100644
--- a/include/util.h
+++ b/include/util.h
@@ -316,4 +316,7 @@ int mkdirp(const char *path);
 bool is_path_prefix(const char *path, const char *prefix);
 FILE *fopenat(int dirfd, char *path, char *cflags);
 void split(char *str, char token, char ***out, int *n);
+
+int fd_has_data(int lfd);
+
 #endif /* __CR_UTIL_H__ */
diff --git a/sk-netlink.c b/sk-netlink.c
index 83a2090..93fd8d1 100644
--- a/sk-netlink.c
+++ b/sk-netlink.c
@@ -1,7 +1,6 @@
 #include <unistd.h>
 #include <linux/netlink.h>
 #include <linux/rtnetlink.h>
-#include <poll.h>
 
 #include "fdset.h"
 #include "files.h"
@@ -67,13 +66,10 @@ int netlink_receive_one(struct nlmsghdr *hdr, void *arg)
 
 static bool can_dump_netlink_sk(int lfd)
 {
-	struct pollfd pfd = {lfd, POLLIN, 0};
 	int ret;
 
-	ret = poll(&pfd, 1, 0);
-	if (ret < 0) {
-		pr_perror("poll() failed");
-	} else if (ret == 1)
+	ret = fd_has_data(lfd);
+	if (ret == 1)
 		pr_err("The socket has data to read\n");
 
 	return ret == 0;
diff --git a/util.c b/util.c
index 58b3b88..3b31537 100644
--- a/util.c
+++ b/util.c
@@ -16,6 +16,7 @@
 #include <dirent.h>
 #include <sys/sendfile.h>
 #include <fcntl.h>
+#include <poll.h>
 
 #include <sys/param.h>
 #include <sys/types.h>
@@ -809,3 +810,16 @@ void split(char *str, char token, char ***out, int *n)
 		i++;
 	} while(cur);
 }
+
+int fd_has_data(int lfd)
+{
+	struct pollfd pfd = {lfd, POLLIN, 0};
+	int ret;
+
+	ret = poll(&pfd, 1, 0);
+	if (ret < 0) {
+		pr_perror("poll() failed");
+	}
+
+	return ret;
+}
-- 
1.9.3



More information about the CRIU mailing list