X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Fshared%2Futil.c;h=b69e7e882a8803546e205e97cd63a99e76f1057b;hb=5c0aa72a4999bdcf03fe93ed5c8213c2b4c681f0;hp=873c95820ae09aad32f10b1b7a8d2e9ee8bfd339;hpb=2181a7f558eb52a22f09f8add9ac0abb4f2ee016;p=elogind.git diff --git a/src/shared/util.c b/src/shared/util.c index 873c95820..b69e7e882 100644 --- a/src/shared/util.c +++ b/src/shared/util.c @@ -207,14 +207,12 @@ int close_nointr(int fd) { } void close_nointr_nofail(int fd) { - int saved_errno = errno; + PROTECT_ERRNO; /* like close_nointr() but cannot fail, and guarantees errno * is unchanged */ assert_se(close_nointr(fd) == 0); - - errno = saved_errno; } void close_many(const int fds[], unsigned n_fd) { @@ -1873,18 +1871,18 @@ int flush_fd(int fd) { ssize_t l; int r; - if ((r = poll(&pollfd, 1, 0)) < 0) { - + r = poll(&pollfd, 1, 0); + if (r < 0) { if (errno == EINTR) continue; return -errno; - } - if (r == 0) + } else if (r == 0) return 0; - if ((l = read(fd, buf, sizeof(buf))) < 0) { + l = read(fd, buf, sizeof(buf)); + if (l < 0) { if (errno == EINTR) continue; @@ -1893,9 +1891,7 @@ int flush_fd(int fd) { return 0; return -errno; - } - - if (l <= 0) + } else if (l == 0) return 0; } } @@ -2068,10 +2064,12 @@ fail: } int release_terminal(void) { - int r = 0, fd; + int r = 0; struct sigaction sa_old, sa_new; + int _cleanup_close_ fd; - if ((fd = open("/dev/tty", O_RDWR|O_NOCTTY|O_NDELAY|O_CLOEXEC)) < 0) + fd = open("/dev/tty", O_RDWR|O_NOCTTY|O_NDELAY|O_CLOEXEC); + if (fd < 0) return -errno; /* Temporarily ignore SIGHUP, so that we don't get SIGHUP'ed @@ -2087,7 +2085,6 @@ int release_terminal(void) { assert_se(sigaction(SIGHUP, &sa_old, NULL) == 0); - close_nointr_nofail(fd); return r; } @@ -5855,3 +5852,20 @@ char *strrep(const char *s, unsigned n) { *p = 0; return r; } + +void* greedy_realloc(void **p, size_t *allocated, size_t need) { + size_t a; + void *q; + + if (*allocated >= need) + return *p; + + a = MAX(64u, need * 2); + q = realloc(*p, a); + if (!q) + return NULL; + + *p = q; + *allocated = a; + return q; +}