diff options
author | Anthony G. Basile <blueness@gentoo.org> | 2014-07-06 07:59:11 -0400 |
---|---|---|
committer | Anthony G. Basile <blueness@gentoo.org> | 2014-07-06 07:59:11 -0400 |
commit | 368ad52438ffe82744dfd9c02a3556045261f7c2 (patch) | |
tree | e11f8a9db528e764ecd0904da456683da65d99fa /src/libudev | |
parent | 231fb4972709c4b7de48761a0a3c656e394d37a5 (diff) |
src/libudev/util.c: import flush_fd() from upstream
Signed-off-by: Anthony G. Basile <blueness@gentoo.org>
Diffstat (limited to 'src/libudev')
-rw-r--r-- | src/libudev/util.c | 36 | ||||
-rw-r--r-- | src/libudev/util.h | 2 |
2 files changed, 38 insertions, 0 deletions
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); |