X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=util.c;h=49f5b4be13f46723b48da397487a7c79dc4419e3;hb=48507e6621596b0d5503fad6cd9e0685917603f5;hp=5f36819fa31f432181989130822d6f047c013da7;hpb=9e2f7c11fb6ba35ffec2274da3e2d08b10d23965;p=elogind.git diff --git a/util.c b/util.c index 5f36819fa..49f5b4be1 100644 --- a/util.c +++ b/util.c @@ -41,6 +41,7 @@ #include #include #include +#include #include "macro.h" #include "util.h" @@ -1214,7 +1215,7 @@ int close_all_fds(const int except[], unsigned n_except) { while ((de = readdir(d))) { int fd = -1; - if (de->d_name[0] == '.') + if (ignore_file(de->d_name)) continue; if ((r = safe_atoi(de->d_name, &fd)) < 0) @@ -1323,7 +1324,7 @@ int chvt(int vt) { if (ioctl(fd, VT_ACTIVATE, vt) < 0) r = -errno; - close_nointr(r); + close_nointr_nofail(r); return r; } @@ -1611,7 +1612,7 @@ int acquire_terminal(const char *name, bool fail, bool force) { } if (notify >= 0) - close_nointr(notify); + close_nointr_nofail(notify); if ((r = reset_terminal(fd)) < 0) log_warning("Failed to reset terminal: %s", strerror(-r)); @@ -1620,10 +1621,10 @@ int acquire_terminal(const char *name, bool fail, bool force) { fail: if (fd >= 0) - close_nointr(fd); + close_nointr_nofail(fd); if (notify >= 0) - close_nointr(notify); + close_nointr_nofail(notify); return r; } @@ -1651,6 +1652,97 @@ int ignore_signal(int sig) { return sigaction(sig, &sa, NULL); } +int close_pipe(int p[]) { + int a = 0, b = 0; + + assert(p); + + if (p[0] >= 0) { + a = close_nointr(p[0]); + p[0] = -1; + } + + if (p[1] >= 0) { + b = close_nointr(p[1]); + p[1] = -1; + } + + return a < 0 ? a : b; +} + +ssize_t loop_read(int fd, void *buf, size_t nbytes) { + uint8_t *p; + ssize_t n = 0; + + assert(fd >= 0); + assert(buf); + + p = buf; + + while (nbytes > 0) { + ssize_t k; + + if ((k = read(fd, p, nbytes)) <= 0) { + + if (errno == EINTR) + continue; + + if (errno == EAGAIN) { + struct pollfd pollfd; + + zero(pollfd); + pollfd.fd = fd; + pollfd.events = POLLIN; + + if (poll(&pollfd, 1, -1) < 0) { + if (errno == EINTR) + continue; + + return n > 0 ? n : -errno; + } + + if (pollfd.revents != POLLIN) + return n > 0 ? n : -EIO; + + continue; + } + + return n > 0 ? n : (k < 0 ? -errno : 0); + } + + p += k; + nbytes -= k; + n += k; + } + + return n; +} + +int path_is_mount_point(const char *t) { + struct stat a, b; + char *copy; + + if (lstat(t, &a) < 0) { + + if (errno == ENOENT) + return 0; + + return -errno; + } + + if (!(copy = strdup(t))) + return -ENOMEM; + + if (lstat(dirname(copy), &b) < 0) { + free(copy); + return -errno; + } + + free(copy); + + return a.st_dev != b.st_dev; +} + static const char *const ioprio_class_table[] = { [IOPRIO_CLASS_NONE] = "none", [IOPRIO_CLASS_RT] = "realtime",