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 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) (limited to 'src/libudev/util.c') 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; -- cgit v1.2.3-54-g00ecf