Commit 961655dc authored by Andrey Vagin's avatar Andrey Vagin Committed by Pavel Emelyanov

util: add a function to check output data in a file descriptor

We can't dump netlink socket, inotify, fanotify, if they have queued
data, so lets add a function to chech this.
Signed-off-by: 's avatarAndrey Vagin <avagin@openvz.org>
Acked-by: 's avatarCyrill Gorcunov <gorcunov@openvz.org>
Signed-off-by: 's avatarPavel Emelyanov <xemul@parallels.com>
parent 9089ce89
...@@ -316,4 +316,7 @@ int mkdirp(const char *path); ...@@ -316,4 +316,7 @@ int mkdirp(const char *path);
bool is_path_prefix(const char *path, const char *prefix); bool is_path_prefix(const char *path, const char *prefix);
FILE *fopenat(int dirfd, char *path, char *cflags); FILE *fopenat(int dirfd, char *path, char *cflags);
void split(char *str, char token, char ***out, int *n); void split(char *str, char token, char ***out, int *n);
int fd_has_data(int lfd);
#endif /* __CR_UTIL_H__ */ #endif /* __CR_UTIL_H__ */
#include <unistd.h> #include <unistd.h>
#include <linux/netlink.h> #include <linux/netlink.h>
#include <linux/rtnetlink.h> #include <linux/rtnetlink.h>
#include <poll.h>
#include "fdset.h" #include "fdset.h"
#include "files.h" #include "files.h"
...@@ -67,13 +66,10 @@ int netlink_receive_one(struct nlmsghdr *hdr, void *arg) ...@@ -67,13 +66,10 @@ int netlink_receive_one(struct nlmsghdr *hdr, void *arg)
static bool can_dump_netlink_sk(int lfd) static bool can_dump_netlink_sk(int lfd)
{ {
struct pollfd pfd = {lfd, POLLIN, 0};
int ret; int ret;
ret = poll(&pfd, 1, 0); ret = fd_has_data(lfd);
if (ret < 0) { if (ret == 1)
pr_perror("poll() failed");
} else if (ret == 1)
pr_err("The socket has data to read\n"); pr_err("The socket has data to read\n");
return ret == 0; return ret == 0;
......
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
#include <dirent.h> #include <dirent.h>
#include <sys/sendfile.h> #include <sys/sendfile.h>
#include <fcntl.h> #include <fcntl.h>
#include <poll.h>
#include <sys/param.h> #include <sys/param.h>
#include <sys/types.h> #include <sys/types.h>
...@@ -809,3 +810,16 @@ void split(char *str, char token, char ***out, int *n) ...@@ -809,3 +810,16 @@ void split(char *str, char token, char ***out, int *n)
i++; i++;
} while(cur); } 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;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment