From 368ad52438ffe82744dfd9c02a3556045261f7c2 Mon Sep 17 00:00:00 2001 From: "Anthony G. Basile" Date: Sun, 6 Jul 2014 07:59:11 -0400 Subject: src/libudev/util.c: import flush_fd() from upstream Signed-off-by: Anthony G. Basile --- src/libudev/util.c | 36 ++++++++++++++++++++++++++++++++++++ src/libudev/util.h | 2 ++ 2 files changed, 38 insertions(+) diff --git a/src/libudev/util.c b/src/libudev/util.c index cfdf848cce..af7477ba6f 100644 --- a/src/libudev/util.c +++ b/src/libudev/util.c @@ -725,6 +725,42 @@ int execute_command(const char *command, char *const argv[]) } } +int flush_fd(int fd) { + struct pollfd pollfd = { + .fd = fd, + .events = POLLIN, + }; + + for (;;) { + char buf[LINE_MAX]; + ssize_t l; + int r; + + r = poll(&pollfd, 1, 0); + if (r < 0) { + if (errno == EINTR) + continue; + + return -errno; + + } else if (r == 0) + return 0; + + l = read(fd, buf, sizeof(buf)); + if (l < 0) { + + if (errno == EINTR) + continue; + + if (errno == EAGAIN) + return 0; + + return -errno; + } else if (l == 0) + return 0; + } +} + int fopen_temporary(const char *path, FILE **_f, char **_temp_path) { FILE *f; char *t; diff --git a/src/libudev/util.h b/src/libudev/util.h index 332dfb6207..bde7b91e6f 100644 --- a/src/libudev/util.h +++ b/src/libudev/util.h @@ -213,6 +213,8 @@ void random_bytes(void *p, size_t n); int open_terminal(const char *name, int mode); +int flush_fd(int fd); + int fopen_temporary(const char *path, FILE **_f, char **_temp_path); ssize_t loop_read(int fd, void *buf, size_t nbytes, bool do_poll); -- cgit v1.2.3-54-g00ecf