X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=src%2Fshared%2Futil.c;h=462b541b41d6d7560b9ec0b792eb1f801633079b;hp=70b159f8c314ead6fb62964ece54117d7e4bd240;hb=28917d7dc711746795f7e6468c06c1983a5cdf53;hpb=069cfc85f876bb6966cb5a9bbe0235f5064622cd diff --git a/src/shared/util.c b/src/shared/util.c index 70b159f8c..462b541b4 100644 --- a/src/shared/util.c +++ b/src/shared/util.c @@ -50,11 +50,13 @@ #include #include #include -#include #include #include #include #include +#include +#include +#include #include "macro.h" #include "util.h" @@ -70,6 +72,8 @@ int saved_argc = 0; char **saved_argv = NULL; +static volatile unsigned cached_columns = 0; + size_t page_size(void) { static __thread size_t pgsz = 0; long r; @@ -77,10 +81,10 @@ size_t page_size(void) { if (_likely_(pgsz > 0)) return pgsz; - assert_se((r = sysconf(_SC_PAGESIZE)) > 0); + r = sysconf(_SC_PAGESIZE); + assert(r > 0); pgsz = (size_t) r; - return pgsz; } @@ -139,6 +143,10 @@ dual_timestamp* dual_timestamp_from_realtime(dual_timestamp *ts, usec_t u) { usec_t timespec_load(const struct timespec *ts) { assert(ts); + if (ts->tv_sec == (time_t) -1 && + ts->tv_nsec == (long) -1) + return (usec_t) -1; + return (usec_t) ts->tv_sec * USEC_PER_SEC + (usec_t) ts->tv_nsec / NSEC_PER_USEC; @@ -147,6 +155,12 @@ usec_t timespec_load(const struct timespec *ts) { struct timespec *timespec_store(struct timespec *ts, usec_t u) { assert(ts); + if (u == (usec_t) -1) { + ts->tv_sec = (time_t) -1; + ts->tv_nsec = (long) -1; + return ts; + } + ts->tv_sec = (time_t) (u / USEC_PER_SEC); ts->tv_nsec = (long int) ((u % USEC_PER_SEC) * NSEC_PER_USEC); @@ -156,6 +170,10 @@ struct timespec *timespec_store(struct timespec *ts, usec_t u) { usec_t timeval_load(const struct timeval *tv) { assert(tv); + if (tv->tv_sec == (time_t) -1 && + tv->tv_usec == (suseconds_t) -1) + return (usec_t) -1; + return (usec_t) tv->tv_sec * USEC_PER_SEC + (usec_t) tv->tv_usec; @@ -164,6 +182,12 @@ usec_t timeval_load(const struct timeval *tv) { struct timeval *timeval_store(struct timeval *tv, usec_t u) { assert(tv); + if (u == (usec_t) -1) { + tv->tv_sec = (time_t) -1; + tv->tv_usec = (suseconds_t) -1; + return tv; + } + tv->tv_sec = (time_t) (u / USEC_PER_SEC); tv->tv_usec = (suseconds_t) (u % USEC_PER_SEC); @@ -222,10 +246,9 @@ bool startswith_no_case(const char *s, const char *prefix) { if (sl < pl) return false; - for(i = 0; i < pl; ++i) { + for(i = 0; i < pl; ++i) if (tolower(s[i]) != tolower(prefix[i])) return false; - } return true; } @@ -304,7 +327,8 @@ int parse_pid(const char *s, pid_t* ret_pid) { assert(s); assert(ret_pid); - if ((r = safe_atolu(s, &ul)) < 0) + r = safe_atolu(s, &ul); + if (r < 0) return r; pid = (pid_t) ul; @@ -327,7 +351,8 @@ int parse_uid(const char *s, uid_t* ret_uid) { assert(s); assert(ret_uid); - if ((r = safe_atolu(s, &ul)) < 0) + r = safe_atolu(s, &ul); + if (r < 0) return r; uid = (uid_t) ul; @@ -488,7 +513,7 @@ char *split_quoted(const char *c, size_t *l, char **state) { int get_parent_of_pid(pid_t pid, pid_t *_ppid) { int r; - FILE *f; + _cleanup_fclose_ FILE *f = NULL; char fn[PATH_MAX], line[LINE_MAX], *p; long unsigned ppid; @@ -498,22 +523,22 @@ int get_parent_of_pid(pid_t pid, pid_t *_ppid) { assert_se(snprintf(fn, sizeof(fn)-1, "/proc/%lu/stat", (unsigned long) pid) < (int) (sizeof(fn)-1)); char_array_0(fn); - if (!(f = fopen(fn, "re"))) + f = fopen(fn, "re"); + if (!f) return -errno; - if (!(fgets(line, sizeof(line), f))) { + if (!fgets(line, sizeof(line), f)) { r = feof(f) ? -EIO : -errno; fclose(f); return r; } - fclose(f); - /* Let's skip the pid and comm fields. The latter is enclosed * in () but does not escape any () in its value, so let's * skip over it manually */ - if (!(p = strrchr(line, ')'))) + p = strrchr(line, ')'); + if (!p) return -EIO; p++; @@ -533,8 +558,7 @@ int get_parent_of_pid(pid_t pid, pid_t *_ppid) { } int get_starttime_of_pid(pid_t pid, unsigned long long *st) { - int r; - FILE *f; + _cleanup_fclose_ FILE *f = NULL; char fn[PATH_MAX], line[LINE_MAX], *p; assert(pid > 0); @@ -543,22 +567,23 @@ int get_starttime_of_pid(pid_t pid, unsigned long long *st) { assert_se(snprintf(fn, sizeof(fn)-1, "/proc/%lu/stat", (unsigned long) pid) < (int) (sizeof(fn)-1)); char_array_0(fn); - if (!(f = fopen(fn, "re"))) + f = fopen(fn, "re"); + if (!f) return -errno; - if (!(fgets(line, sizeof(line), f))) { - r = feof(f) ? -EIO : -errno; - fclose(f); - return r; - } + if (!fgets(line, sizeof(line), f)) { + if (ferror(f)) + return -errno; - fclose(f); + return -EIO; + } /* Let's skip the pid and comm fields. The latter is enclosed * in () but does not escape any () in its value, so let's * skip over it manually */ - if (!(p = strrchr(line, ')'))) + p = strrchr(line, ')'); + if (!p) return -EIO; p++; @@ -591,8 +616,7 @@ int get_starttime_of_pid(pid_t pid, unsigned long long *st) { } int write_one_line_file(const char *fn, const char *line) { - FILE *f; - int r; + _cleanup_fclose_ FILE *f = NULL; assert(fn); assert(line); @@ -602,27 +626,18 @@ int write_one_line_file(const char *fn, const char *line) { return -errno; errno = 0; - if (fputs(line, f) < 0) { - r = -errno; - goto finish; - } + if (fputs(line, f) < 0) + return errno ? -errno : -EIO; if (!endswith(line, "\n")) fputc('\n', f); fflush(f); - if (ferror(f)) { - if (errno != 0) - r = -errno; - else - r = -EIO; - } else - r = 0; + if (ferror(f)) + return errno ? -errno : -EIO; -finish: - fclose(f); - return r; + return 0; } int fchmod_umask(int fd, mode_t m) { @@ -684,8 +699,7 @@ finish: } int read_one_line_file(const char *fn, char **line) { - FILE *f; - int r; + _cleanup_fclose_ FILE *f = NULL; char t[LINE_MAX], *c; assert(fn); @@ -697,50 +711,37 @@ int read_one_line_file(const char *fn, char **line) { if (!fgets(t, sizeof(t), f)) { - if (ferror(f)) { - r = -errno; - goto finish; - } + if (ferror(f)) + return errno ? -errno : -EIO; t[0] = 0; } c = strdup(t); - if (!c) { - r = -ENOMEM; - goto finish; - } - + if (!c) + return -ENOMEM; truncate_nl(c); *line = c; - r = 0; - -finish: - fclose(f); - return r; + return 0; } int read_full_file(const char *fn, char **contents, size_t *size) { - FILE *f; - int r; + _cleanup_fclose_ FILE *f = NULL; size_t n, l; - char *buf = NULL; + _cleanup_free_ char *buf = NULL; struct stat st; - if (!(f = fopen(fn, "re"))) + f = fopen(fn, "re"); + if (!f) return -errno; - if (fstat(fileno(f), &st) < 0) { - r = -errno; - goto finish; - } + if (fstat(fileno(f), &st) < 0) + return -errno; /* Safety check */ - if (st.st_size > 4*1024*1024) { - r = -E2BIG; - goto finish; - } + if (st.st_size > 4*1024*1024) + return -E2BIG; n = st.st_size > 0 ? st.st_size : LINE_MAX; l = 0; @@ -749,19 +750,16 @@ int read_full_file(const char *fn, char **contents, size_t *size) { char *t; size_t k; - if (!(t = realloc(buf, n+1))) { - r = -ENOMEM; - goto finish; - } + t = realloc(buf, n+1); + if (!t) + return -ENOMEM; buf = t; k = fread(buf + l, 1, n - l, f); if (k <= 0) { - if (ferror(f)) { - r = -errno; - goto finish; - } + if (ferror(f)) + return -errno; break; } @@ -770,10 +768,8 @@ int read_full_file(const char *fn, char **contents, size_t *size) { n *= 2; /* Safety check */ - if (n > 4*1024*1024) { - r = -E2BIG; - goto finish; - } + if (n > 4*1024*1024) + return -E2BIG; } buf[l] = 0; @@ -783,13 +779,7 @@ int read_full_file(const char *fn, char **contents, size_t *size) { if (size) *size = l; - r = 0; - -finish: - fclose(f); - free(buf); - - return r; + return 0; } int parse_env_file( @@ -908,8 +898,7 @@ int load_env_file( continue; if (!(u = normalize_env_assignment(p))) { - log_error("Out of memory"); - r = -ENOMEM; + r = log_oom(); goto finish; } @@ -917,8 +906,7 @@ int load_env_file( free(u); if (!t) { - log_error("Out of memory"); - r = -ENOMEM; + r = log_oom(); goto finish; } @@ -1082,7 +1070,7 @@ int get_process_cmdline(pid_t pid, size_t max_length, bool comm_fallback, char * if (h < 0) return h; - r = join("[", t, "]", NULL); + r = strjoin("[", t, "]", NULL); free(t); if (!r) @@ -1143,7 +1131,7 @@ int get_process_exe(pid_t pid, char **name) { return r; } -int get_process_uid(pid_t pid, uid_t *uid) { +static int get_process_id(pid_t pid, const char *field, uid_t *uid) { char *p; FILE *f; int r; @@ -1175,8 +1163,8 @@ int get_process_uid(pid_t pid, uid_t *uid) { l = strstrip(line); - if (startswith(l, "Uid:")) { - l += 4; + if (startswith(l, field)) { + l += strlen(field); l += strspn(l, WHITESPACE); l[strcspn(l, WHITESPACE)] = 0; @@ -1194,6 +1182,14 @@ finish: return r; } +int get_process_uid(pid_t pid, uid_t *uid) { + return get_process_id(pid, "Uid:", uid); +} + +int get_process_gid(pid_t pid, gid_t *gid) { + return get_process_id(pid, "Gid:", gid); +} + char *strnappend(const char *s, const char *suffix, size_t b) { size_t a; char *r; @@ -1211,8 +1207,11 @@ char *strnappend(const char *s, const char *suffix, size_t b) { assert(suffix); a = strlen(s); + if (b > ((size_t) -1) - a) + return NULL; - if (!(r = new(char, a+b+1))) + r = new(char, a+b+1); + if (!r) return NULL; memcpy(r, s, a); @@ -1563,19 +1562,25 @@ char *cescape(const char *s) { return r; } -char *cunescape_length(const char *s, size_t length) { +char *cunescape_length_with_prefix(const char *s, size_t length, const char *prefix) { char *r, *t; const char *f; + size_t pl; assert(s); - /* Undoes C style string escaping */ + /* Undoes C style string escaping, and optionally prefixes it. */ - r = new(char, length+1); + pl = prefix ? strlen(prefix) : 0; + + r = new(char, pl+length+1); if (!r) return r; - for (f = s, t = r; f < s + length; f++) { + if (prefix) + memcpy(r, prefix, pl); + + for (f = s, t = r + pl; f < s + length; f++) { if (*f != '\\') { *(t++) = *f; @@ -1686,7 +1691,13 @@ finish: return r; } +char *cunescape_length(const char *s, size_t length) { + return cunescape_length_with_prefix(s, length, NULL); +} + char *cunescape(const char *s) { + assert(s); + return cunescape_length(s, strlen(s)); } @@ -1698,7 +1709,8 @@ char *xescape(const char *s, const char *bad) { * chars, in \xFF style escaping. May be reversed with * cunescape. */ - if (!(r = new(char, strlen(s)*4+1))) + r = new(char, strlen(s) * 4 + 1); + if (!r) return NULL; for (f = s, t = r; *f; f++) { @@ -1789,7 +1801,7 @@ char *ascii_strlower(char *t) { return t; } -bool ignore_file(const char *filename) { +static bool ignore_file_allow_backup(const char *filename) { assert(filename); return @@ -1797,7 +1809,6 @@ bool ignore_file(const char *filename) { streq(filename, "lost+found") || streq(filename, "aquota.user") || streq(filename, "aquota.group") || - endswith(filename, "~") || endswith(filename, ".rpmnew") || endswith(filename, ".rpmsave") || endswith(filename, ".rpmorig") || @@ -1806,6 +1817,15 @@ bool ignore_file(const char *filename) { endswith(filename, ".swp"); } +bool ignore_file(const char *filename) { + assert(filename); + + if (endswith(filename, "~")) + return false; + + return ignore_file_allow_backup(filename); +} + int fd_nonblock(int fd, bool nonblock) { int flags; @@ -1939,7 +1959,7 @@ char *format_timestamp(char *buf, size_t l, usec_t t) { sec = (time_t) (t / USEC_PER_SEC); - if (strftime(buf, l, "%a, %d %b %Y %H:%M:%S %z", localtime_r(&sec, &tm)) <= 0) + if (strftime(buf, l, "%a, %Y-%m-%d %H:%M:%S %Z", localtime_r(&sec, &tm)) <= 0) return NULL; return buf; @@ -2058,29 +2078,23 @@ char *format_timespan(char *buf, size_t l, usec_t t) { } bool fstype_is_network(const char *fstype) { - static const char * const table[] = { - "cifs", - "smbfs", - "ncpfs", - "nfs", - "nfs4", - "gfs", - "gfs2" - }; - - unsigned i; - - for (i = 0; i < ELEMENTSOF(table); i++) - if (streq(table[i], fstype)) - return true; + static const char table[] = + "cifs\0" + "smbfs\0" + "ncpfs\0" + "nfs\0" + "nfs4\0" + "gfs\0" + "gfs2\0"; - return false; + return nulstr_contains(table, fstype); } int chvt(int vt) { - int fd, r = 0; + _cleanup_close_ int fd; - if ((fd = open_terminal("/dev/tty0", O_RDWR|O_NOCTTY|O_CLOEXEC)) < 0) + fd = open_terminal("/dev/tty0", O_RDWR|O_NOCTTY|O_CLOEXEC); + if (fd < 0) return -errno; if (vt < 0) { @@ -2089,20 +2103,16 @@ int chvt(int vt) { 0 }; - if (ioctl(fd, TIOCLINUX, tiocl) < 0) { - r = -errno; - goto fail; - } + if (ioctl(fd, TIOCLINUX, tiocl) < 0) + return -errno; vt = tiocl[0] <= 0 ? 1 : tiocl[0]; } if (ioctl(fd, VT_ACTIVATE, vt) < 0) - r = -errno; + return -errno; -fail: - close_nointr_nofail(fd); - return r; + return 0; } int read_one_char(FILE *f, char *ret, usec_t t, bool *need_nl) { @@ -2306,12 +2316,14 @@ int open_terminal(const char *name, int mode) { */ for (;;) { - if ((fd = open(name, mode)) >= 0) + fd = open(name, mode); + if (fd >= 0) break; if (errno != EIO) return -errno; + /* Max 1s in total */ if (c >= 20) return -errno; @@ -2322,7 +2334,8 @@ int open_terminal(const char *name, int mode) { if (fd < 0) return -errno; - if ((r = isatty(fd)) < 0) { + r = isatty(fd); + if (r < 0) { close_nointr_nofail(fd); return -errno; } @@ -2374,8 +2387,16 @@ int flush_fd(int fd) { } } -int acquire_terminal(const char *name, bool fail, bool force, bool ignore_tiocstty_eperm) { - int fd = -1, notify = -1, r, wd = -1; +int acquire_terminal( + const char *name, + bool fail, + bool force, + bool ignore_tiocstty_eperm, + usec_t timeout) { + + int fd = -1, notify = -1, r = 0, wd = -1; + usec_t ts = 0; + struct sigaction sa_old, sa_new; assert(name); @@ -2392,40 +2413,57 @@ int acquire_terminal(const char *name, bool fail, bool force, bool ignore_tiocst * on the same tty as an untrusted user this should not be a * problem. (Which he probably should not do anyway.) */ + if (timeout != (usec_t) -1) + ts = now(CLOCK_MONOTONIC); + if (!fail && !force) { - if ((notify = inotify_init1(IN_CLOEXEC)) < 0) { + notify = inotify_init1(IN_CLOEXEC | (timeout != (usec_t) -1 ? IN_NONBLOCK : 0)); + if (notify < 0) { r = -errno; goto fail; } - if ((wd = inotify_add_watch(notify, name, IN_CLOSE)) < 0) { + wd = inotify_add_watch(notify, name, IN_CLOSE); + if (wd < 0) { r = -errno; goto fail; } } for (;;) { - if (notify >= 0) - if ((r = flush_fd(notify)) < 0) + if (notify >= 0) { + r = flush_fd(notify); + if (r < 0) goto fail; + } /* We pass here O_NOCTTY only so that we can check the return * value TIOCSCTTY and have a reliable way to figure out if we * successfully became the controlling process of the tty */ - if ((fd = open_terminal(name, O_RDWR|O_NOCTTY|O_CLOEXEC)) < 0) + fd = open_terminal(name, O_RDWR|O_NOCTTY|O_CLOEXEC); + if (fd < 0) return fd; + /* Temporarily ignore SIGHUP, so that we don't get SIGHUP'ed + * if we already own the tty. */ + zero(sa_new); + sa_new.sa_handler = SIG_IGN; + sa_new.sa_flags = SA_RESTART; + assert_se(sigaction(SIGHUP, &sa_new, &sa_old) == 0); + /* First, try to get the tty */ - r = ioctl(fd, TIOCSCTTY, force); + if (ioctl(fd, TIOCSCTTY, force) < 0) + r = -errno; + + assert_se(sigaction(SIGHUP, &sa_old, NULL) == 0); /* Sometimes it makes sense to ignore TIOCSCTTY * returning EPERM, i.e. when very likely we already * are have this controlling terminal. */ - if (r < 0 && errno == EPERM && ignore_tiocstty_eperm) + if (r < 0 && r == -EPERM && ignore_tiocstty_eperm) r = 0; - if (r < 0 && (force || fail || errno != EPERM)) { - r = -errno; + if (r < 0 && (force || fail || r != -EPERM)) { goto fail; } @@ -2441,9 +2479,29 @@ int acquire_terminal(const char *name, bool fail, bool force, bool ignore_tiocst ssize_t l; struct inotify_event *e; - if ((l = read(notify, inotify_buffer, sizeof(inotify_buffer))) < 0) { + if (timeout != (usec_t) -1) { + usec_t n; + + n = now(CLOCK_MONOTONIC); + if (ts + timeout < n) { + r = -ETIMEDOUT; + goto fail; + } + + r = fd_wait_for_event(fd, POLLIN, ts + timeout - n); + if (r < 0) + goto fail; + + if (r == 0) { + r = -ETIMEDOUT; + goto fail; + } + } + + l = read(notify, inotify_buffer, sizeof(inotify_buffer)); + if (l < 0) { - if (errno == EINTR) + if (errno == EINTR || errno == EAGAIN) continue; r = -errno; @@ -2695,19 +2753,34 @@ int parse_usec(const char *t, usec_t *usec) { const char *suffix; usec_t usec; } table[] = { + { "seconds", USEC_PER_SEC }, + { "second", USEC_PER_SEC }, { "sec", USEC_PER_SEC }, { "s", USEC_PER_SEC }, + { "minutes", USEC_PER_MINUTE }, + { "minute", USEC_PER_MINUTE }, { "min", USEC_PER_MINUTE }, + { "months", USEC_PER_MONTH }, + { "month", USEC_PER_MONTH }, + { "msec", USEC_PER_MSEC }, + { "ms", USEC_PER_MSEC }, + { "m", USEC_PER_MINUTE }, + { "hours", USEC_PER_HOUR }, + { "hour", USEC_PER_HOUR }, { "hr", USEC_PER_HOUR }, { "h", USEC_PER_HOUR }, + { "days", USEC_PER_DAY }, + { "day", USEC_PER_DAY }, { "d", USEC_PER_DAY }, + { "weeks", USEC_PER_WEEK }, + { "week", USEC_PER_WEEK }, { "w", USEC_PER_WEEK }, - { "msec", USEC_PER_MSEC }, - { "ms", USEC_PER_MSEC }, - { "m", USEC_PER_MINUTE }, + { "years", USEC_PER_YEAR }, + { "year", USEC_PER_YEAR }, + { "y", USEC_PER_YEAR }, { "usec", 1ULL }, { "us", 1ULL }, - { "", USEC_PER_SEC }, + { "", USEC_PER_SEC }, /* default is sec */ }; const char *p; @@ -2753,6 +2826,86 @@ int parse_usec(const char *t, usec_t *usec) { return 0; } +int parse_nsec(const char *t, nsec_t *nsec) { + static const struct { + const char *suffix; + nsec_t nsec; + } table[] = { + { "seconds", NSEC_PER_SEC }, + { "second", NSEC_PER_SEC }, + { "sec", NSEC_PER_SEC }, + { "s", NSEC_PER_SEC }, + { "minutes", NSEC_PER_MINUTE }, + { "minute", NSEC_PER_MINUTE }, + { "min", NSEC_PER_MINUTE }, + { "months", NSEC_PER_MONTH }, + { "month", NSEC_PER_MONTH }, + { "msec", NSEC_PER_MSEC }, + { "ms", NSEC_PER_MSEC }, + { "m", NSEC_PER_MINUTE }, + { "hours", NSEC_PER_HOUR }, + { "hour", NSEC_PER_HOUR }, + { "hr", NSEC_PER_HOUR }, + { "h", NSEC_PER_HOUR }, + { "days", NSEC_PER_DAY }, + { "day", NSEC_PER_DAY }, + { "d", NSEC_PER_DAY }, + { "weeks", NSEC_PER_WEEK }, + { "week", NSEC_PER_WEEK }, + { "w", NSEC_PER_WEEK }, + { "years", NSEC_PER_YEAR }, + { "year", NSEC_PER_YEAR }, + { "y", NSEC_PER_YEAR }, + { "usec", NSEC_PER_USEC }, + { "us", NSEC_PER_USEC }, + { "nsec", 1ULL }, + { "ns", 1ULL }, + { "", 1ULL }, /* default is nsec */ + }; + + const char *p; + nsec_t r = 0; + + assert(t); + assert(nsec); + + p = t; + do { + long long l; + char *e; + unsigned i; + + errno = 0; + l = strtoll(p, &e, 10); + + if (errno != 0) + return -errno; + + if (l < 0) + return -ERANGE; + + if (e == p) + return -EINVAL; + + e += strspn(e, WHITESPACE); + + for (i = 0; i < ELEMENTSOF(table); i++) + if (startswith(e, table[i].suffix)) { + r += (nsec_t) l * table[i].nsec; + p = e + strlen(table[i].suffix); + break; + } + + if (i >= ELEMENTSOF(table)) + return -EINVAL; + + } while (*p != 0); + + *nsec = r; + + return 0; +} + int parse_bytes(const char *t, off_t *bytes) { static const struct { const char *suffix; @@ -2816,9 +2969,9 @@ int make_stdio(int fd) { assert(fd >= 0); - r = dup2(fd, STDIN_FILENO); - s = dup2(fd, STDOUT_FILENO); - t = dup2(fd, STDERR_FILENO); + r = dup3(fd, STDIN_FILENO, 0); + s = dup3(fd, STDOUT_FILENO, 0); + t = dup3(fd, STDERR_FILENO, 0); if (fd >= 3) close_nointr_nofail(fd); @@ -2826,9 +2979,7 @@ int make_stdio(int fd) { if (r < 0 || s < 0 || t < 0) return -errno; - fd_cloexec(STDIN_FILENO, false); - fd_cloexec(STDOUT_FILENO, false); - fd_cloexec(STDERR_FILENO, false); + /* We rely here that the new fd has O_CLOEXEC not set */ return 0; } @@ -2836,7 +2987,8 @@ int make_stdio(int fd) { int make_null_stdio(void) { int null_fd; - if ((null_fd = open("/dev/null", O_RDWR|O_NOCTTY)) < 0) + null_fd = open("/dev/null", O_RDWR|O_NOCTTY); + if (null_fd < 0) return -errno; return make_stdio(null_fd); @@ -2853,45 +3005,39 @@ bool is_device_path(const char *path) { } int dir_is_empty(const char *path) { - DIR *d; + _cleanup_closedir_ DIR *d; int r; - struct dirent buf, *de; - if (!(d = opendir(path))) + d = opendir(path); + if (!d) return -errno; for (;;) { - if ((r = readdir_r(d, &buf, &de)) > 0) { - r = -r; - break; - } + struct dirent *de; + union dirent_storage buf; - if (!de) { - r = 1; - break; - } + r = readdir_r(d, &buf.de, &de); + if (r > 0) + return -r; - if (!ignore_file(de->d_name)) { - r = 0; - break; - } - } + if (!de) + return 1; - closedir(d); - return r; + if (!ignore_file(de->d_name)) + return 0; + } } unsigned long long random_ull(void) { - int fd; + _cleanup_close_ int fd; uint64_t ull; ssize_t r; - if ((fd = open("/dev/urandom", O_RDONLY|O_CLOEXEC|O_NOCTTY)) < 0) + fd = open("/dev/urandom", O_RDONLY|O_CLOEXEC|O_NOCTTY); + if (fd < 0) goto fallback; r = loop_read(fd, &ull, sizeof(ull), true); - close_nointr_nofail(fd); - if (r != sizeof(ull)) goto fallback; @@ -2963,35 +3109,26 @@ bool hostname_is_set(void) { return !isempty(u.nodename) && !streq(u.nodename, "(none)"); } -char* getlogname_malloc(void) { - uid_t uid; +static char *lookup_uid(uid_t uid) { long bufsize; - char *buf, *name; + char *name; + _cleanup_free_ char *buf = NULL; struct passwd pwbuf, *pw = NULL; - struct stat st; - - if (isatty(STDIN_FILENO) && fstat(STDIN_FILENO, &st) >= 0) - uid = st.st_uid; - else - uid = getuid(); /* Shortcut things to avoid NSS lookups */ if (uid == 0) return strdup("root"); - if ((bufsize = sysconf(_SC_GETPW_R_SIZE_MAX)) <= 0) + bufsize = sysconf(_SC_GETPW_R_SIZE_MAX); + if (bufsize <= 0) bufsize = 4096; - if (!(buf = malloc(bufsize))) + buf = malloc(bufsize); + if (!buf) return NULL; - if (getpwuid_r(uid, &pwbuf, buf, bufsize, &pw) == 0 && pw) { - name = strdup(pw->pw_name); - free(buf); - return name; - } - - free(buf); + if (getpwuid_r(uid, &pwbuf, buf, bufsize, &pw) == 0 && pw) + return strdup(pw->pw_name); if (asprintf(&name, "%lu", (unsigned long) uid) < 0) return NULL; @@ -2999,18 +3136,42 @@ char* getlogname_malloc(void) { return name; } -int getttyname_malloc(int fd, char **r) { - char path[PATH_MAX], *c; - int k; +char* getlogname_malloc(void) { + uid_t uid; + struct stat st; - assert(r); + if (isatty(STDIN_FILENO) && fstat(STDIN_FILENO, &st) >= 0) + uid = st.st_uid; + else + uid = getuid(); - if ((k = ttyname_r(fd, path, sizeof(path))) != 0) - return -k; + return lookup_uid(uid); +} - char_array_0(path); +char *getusername_malloc(void) { + const char *e; + + e = getenv("USER"); + if (e) + return strdup(e); + + return lookup_uid(getuid()); +} + +int getttyname_malloc(int fd, char **r) { + char path[PATH_MAX], *c; + int k; + + assert(r); + + k = ttyname_r(fd, path, sizeof(path)); + if (k != 0) + return -k; - if (!(c = strdup(startswith(path, "/dev/") ? path + 5 : path))) + char_array_0(path); + + c = strdup(startswith(path, "/dev/") ? path + 5 : path); + if (!c) return -ENOMEM; *r = c; @@ -3021,7 +3182,8 @@ int getttyname_harder(int fd, char **r) { int k; char *s; - if ((k = getttyname_malloc(fd, &s)) < 0) + k = getttyname_malloc(fd, &s); + if (k < 0) return k; if (streq(s, "tty")) { @@ -3139,7 +3301,7 @@ int get_ctty(pid_t pid, dev_t *_devnr, char **r) { return 0; } -int rm_rf_children(int fd, bool only_dirs, bool honour_sticky, struct stat *root_dev) { +int rm_rf_children_dangerous(int fd, bool only_dirs, bool honour_sticky, struct stat *root_dev) { DIR *d; int ret = 0; @@ -3156,12 +3318,13 @@ int rm_rf_children(int fd, bool only_dirs, bool honour_sticky, struct stat *root } for (;;) { - struct dirent buf, *de; + struct dirent *de; + union dirent_storage buf; bool is_dir, keep_around; struct stat st; int r; - r = readdir_r(d, &buf, &de); + r = readdir_r(d, &buf.de, &de); if (r != 0 && ret == 0) { ret = -r; break; @@ -3207,7 +3370,7 @@ int rm_rf_children(int fd, bool only_dirs, bool honour_sticky, struct stat *root continue; } - r = rm_rf_children(subdir_fd, only_dirs, honour_sticky, root_dev); + r = rm_rf_children_dangerous(subdir_fd, only_dirs, honour_sticky, root_dev); if (r < 0 && ret == 0) ret = r; @@ -3231,18 +3394,61 @@ int rm_rf_children(int fd, bool only_dirs, bool honour_sticky, struct stat *root return ret; } -int rm_rf(const char *path, bool only_dirs, bool delete_root, bool honour_sticky) { - int fd; - int r; +int rm_rf_children(int fd, bool only_dirs, bool honour_sticky, struct stat *root_dev) { + struct statfs s; + + assert(fd >= 0); + + if (fstatfs(fd, &s) < 0) { + close_nointr_nofail(fd); + return -errno; + } + + /* We refuse to clean disk file systems with this call. This + * is extra paranoia just to be sure we never ever remove + * non-state data */ + + if (s.f_type != TMPFS_MAGIC && + s.f_type != RAMFS_MAGIC) { + log_error("Attempted to remove disk file system, and we can't allow that."); + close_nointr_nofail(fd); + return -EPERM; + } + + return rm_rf_children_dangerous(fd, only_dirs, honour_sticky, root_dev); +} + +static int rm_rf_internal(const char *path, bool only_dirs, bool delete_root, bool honour_sticky, bool dangerous) { + int fd, r; + struct statfs s; assert(path); + /* We refuse to clean the root file system with this + * call. This is extra paranoia to never cause a really + * seriously broken system. */ + if (path_equal(path, "/")) { + log_error("Attempted to remove entire root file system, and we can't allow that."); + return -EPERM; + } + fd = open(path, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW|O_NOATIME); if (fd < 0) { if (errno != ENOTDIR) return -errno; + if (!dangerous) { + if (statfs(path, &s) < 0) + return -errno; + + if (s.f_type != TMPFS_MAGIC && + s.f_type != RAMFS_MAGIC) { + log_error("Attempted to remove disk file system, and we can't allow that."); + return -EPERM; + } + } + if (delete_root && !only_dirs) if (unlink(path) < 0 && errno != ENOENT) return -errno; @@ -3250,8 +3456,21 @@ int rm_rf(const char *path, bool only_dirs, bool delete_root, bool honour_sticky return 0; } - r = rm_rf_children(fd, only_dirs, honour_sticky, NULL); + if (!dangerous) { + if (fstatfs(fd, &s) < 0) { + close_nointr_nofail(fd); + return -errno; + } + if (s.f_type != TMPFS_MAGIC && + s.f_type != RAMFS_MAGIC) { + log_error("Attempted to remove disk file system, and we can't allow that."); + close_nointr_nofail(fd); + return -EPERM; + } + } + + r = rm_rf_children_dangerous(fd, only_dirs, honour_sticky, NULL); if (delete_root) { if (honour_sticky && file_is_priv_sticky(path) > 0) @@ -3266,6 +3485,14 @@ int rm_rf(const char *path, bool only_dirs, bool delete_root, bool honour_sticky return r; } +int rm_rf(const char *path, bool only_dirs, bool delete_root, bool honour_sticky) { + return rm_rf_internal(path, only_dirs, delete_root, honour_sticky, false); +} + +int rm_rf_dangerous(const char *path, bool only_dirs, bool delete_root, bool honour_sticky) { + return rm_rf_internal(path, only_dirs, delete_root, honour_sticky, true); +} + int chmod_and_chown(const char *path, mode_t mode, uid_t uid, gid_t gid) { assert(path); @@ -3328,10 +3555,10 @@ cpu_set_t* cpu_set_malloc(unsigned *ncpus) { } } -void status_vprintf(const char *status, bool ellipse, const char *format, va_list ap) { - char *s = NULL; +int status_vprintf(const char *status, bool ellipse, const char *format, va_list ap) { static const char status_indent[] = " "; /* "[" STATUS "] " */ - int fd = -1; + _cleanup_free_ char *s = NULL; + _cleanup_close_ int fd = -1; struct iovec iovec[5]; int n = 0; @@ -3341,11 +3568,11 @@ void status_vprintf(const char *status, bool ellipse, const char *format, va_lis * optional and go exclusively to the console. */ if (vasprintf(&s, format, ap) < 0) - goto finish; + return log_oom(); fd = open_terminal("/dev/console", O_WRONLY|O_NOCTTY|O_CLOEXEC); if (fd < 0) - goto finish; + return fd; if (ellipse) { char *e; @@ -3356,7 +3583,7 @@ void status_vprintf(const char *status, bool ellipse, const char *format, va_lis if (c <= 0) c = 80; - sl = status ? strlen(status_indent) : 0; + sl = status ? sizeof(status_indent)-1 : 0; emax = c - sl - 1; if (emax < 3) @@ -3383,53 +3610,40 @@ void status_vprintf(const char *status, bool ellipse, const char *format, va_lis IOVEC_SET_STRING(iovec[n++], s); IOVEC_SET_STRING(iovec[n++], "\n"); - writev(fd, iovec, n); - -finish: - free(s); + if (writev(fd, iovec, n) < 0) + return -errno; - if (fd >= 0) - close_nointr_nofail(fd); + return 0; } -void status_printf(const char *status, bool ellipse, const char *format, ...) { +int status_printf(const char *status, bool ellipse, const char *format, ...) { va_list ap; + int r; assert(format); va_start(ap, format); - status_vprintf(status, ellipse, format, ap); + r = status_vprintf(status, ellipse, format, ap); va_end(ap); + + return r; } -void status_welcome(void) { - char *pretty_name = NULL, *ansi_color = NULL; - const char *const_pretty = NULL, *const_color = NULL; +int status_welcome(void) { int r; + _cleanup_free_ char *pretty_name = NULL, *ansi_color = NULL; - if ((r = parse_env_file("/etc/os-release", NEWLINE, - "PRETTY_NAME", &pretty_name, - "ANSI_COLOR", &ansi_color, - NULL)) < 0) { - - if (r != -ENOENT) - log_warning("Failed to read /etc/os-release: %s", strerror(-r)); - } - - if (!pretty_name && !const_pretty) - const_pretty = "Linux"; + r = parse_env_file("/etc/os-release", NEWLINE, + "PRETTY_NAME", &pretty_name, + "ANSI_COLOR", &ansi_color, + NULL); + if (r < 0 && r != -ENOENT) + log_warning("Failed to read /etc/os-release: %s", strerror(-r)); - if (!ansi_color && !const_color) - const_color = "1"; - - status_printf(NULL, - false, - "\nWelcome to \x1B[%sm%s\x1B[0m!\n", - const_color ? const_color : ansi_color, - const_pretty ? const_pretty : pretty_name); - - free(ansi_color); - free(pretty_name); + return status_printf(NULL, false, + "\nWelcome to \x1B[%sm%s\x1B[0m!\n", + isempty(ansi_color) ? "1" : ansi_color, + isempty(pretty_name) ? "Linux" : pretty_name); } char *replace_env(const char *format, char **env) { @@ -3580,23 +3794,30 @@ int fd_columns(int fd) { } unsigned columns(void) { - static __thread int parsed_columns = 0; const char *e; + unsigned c; - if (_likely_(parsed_columns > 0)) - return parsed_columns; + if (_likely_(cached_columns > 0)) + return cached_columns; + c = 0; e = getenv("COLUMNS"); if (e) - parsed_columns = atoi(e); + safe_atou(e, &c); - if (parsed_columns <= 0) - parsed_columns = fd_columns(STDOUT_FILENO); + if (c <= 0) + c = fd_columns(STDOUT_FILENO); - if (parsed_columns <= 0) - parsed_columns = 80; + if (c <= 0) + c = 80; - return parsed_columns; + cached_columns = c; + return c; +} + +/* intended to be used as a SIGWINCH sighandler */ +void columns_cache_reset(int signum) { + cached_columns = 0; } int fd_lines(int fd) { @@ -3691,7 +3912,12 @@ int touch(const char *path) { assert(path); - if ((fd = open(path, O_WRONLY|O_CREAT|O_CLOEXEC|O_NOCTTY, 0644)) < 0) + /* This just opens the file for writing, ensuring it + * exists. It doesn't call utimensat() the way /usr/bin/touch + * does it. */ + + fd = open(path, O_WRONLY|O_CREAT|O_CLOEXEC|O_NOCTTY, 0644); + if (fd < 0) return -errno; close_nointr_nofail(fd); @@ -3702,6 +3928,11 @@ char *unquote(const char *s, const char* quotes) { size_t l; assert(s); + /* This is rather stupid, simply removes the heading and + * trailing quotes if there is one. Doesn't care about + * escaping or anything. We should make this smarter one + * day...*/ + l = strlen(s); if (l < 2) return strdup(s); @@ -3713,39 +3944,40 @@ char *unquote(const char *s, const char* quotes) { } char *normalize_env_assignment(const char *s) { - char *name, *value, *p, *r; + _cleanup_free_ char *name = NULL, *value = NULL, *p = NULL; + char *eq, *r; - p = strchr(s, '='); + eq = strchr(s, '='); + if (!eq) { + char *t; - if (!p) { - if (!(r = strdup(s))) + r = strdup(s); + if (!r) return NULL; - return strstrip(r); + t = strstrip(r); + if (t == r) + return r; + + memmove(r, t, strlen(t) + 1); + return r; } - if (!(name = strndup(s, p - s))) + name = strndup(s, eq - s); + if (!name) return NULL; - if (!(p = strdup(p+1))) { - free(name); + p = strdup(eq + 1); + if (!p) return NULL; - } value = unquote(strstrip(p), QUOTES); - free(p); - - if (!value) { - free(name); + if (!value) return NULL; - } - if (asprintf(&r, "%s=%s", name, value) < 0) + if (asprintf(&r, "%s=%s", strstrip(name), value) < 0) r = NULL; - free(value); - free(name); - return r; } @@ -3779,7 +4011,8 @@ int wait_for_terminate_and_warn(const char *name, pid_t pid) { assert(name); assert(pid > 1); - if ((r = wait_for_terminate(pid, &status)) < 0) { + r = wait_for_terminate(pid, &status); + if (r < 0) { log_warning("Failed to wait for %s: %s", name, strerror(-r)); return r; } @@ -3802,7 +4035,6 @@ int wait_for_terminate_and_warn(const char *name, pid_t pid) { log_warning("%s failed due to unknown reason.", name); return -EPROTO; - } _noreturn_ void freeze(void) { @@ -3843,10 +4075,12 @@ DIR *xopendirat(int fd, const char *name, int flags) { int nfd; DIR *d; - if ((nfd = openat(fd, name, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|flags)) < 0) + nfd = openat(fd, name, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|flags); + if (nfd < 0) return NULL; - if (!(d = fdopendir(nfd))) { + d = fdopendir(nfd); + if (!d) { close_nointr_nofail(nfd); return NULL; } @@ -3858,7 +4092,8 @@ int signal_from_string_try_harder(const char *s) { int signo; assert(s); - if ((signo = signal_from_string(s)) <= 0) + signo = signal_from_string(s); + if (signo <= 0) if (startswith(s, "SIG")) return signal_from_string(s+3); @@ -3894,52 +4129,46 @@ void dual_timestamp_deserialize(const char *value, dual_timestamp *t) { } } -char *fstab_node_to_udev_node(const char *p) { +static char *tag_to_udev_node(const char *tagvalue, const char *by) { char *dn, *t, *u; int r; /* FIXME: to follow udev's logic 100% we need to leave valid * UTF8 chars unescaped */ - if (startswith(p, "LABEL=")) { - - if (!(u = unquote(p+6, "\"\'"))) - return NULL; - - t = xescape(u, "/ "); - free(u); - - if (!t) - return NULL; + u = unquote(tagvalue, "\"\'"); + if (u == NULL) + return NULL; - r = asprintf(&dn, "/dev/disk/by-label/%s", t); - free(t); + t = xescape(u, "/ "); + free(u); - if (r < 0) - return NULL; + if (t == NULL) + return NULL; - return dn; - } + r = asprintf(&dn, "/dev/disk/by-%s/%s", by, t); + free(t); - if (startswith(p, "UUID=")) { + if (r < 0) + return NULL; - if (!(u = unquote(p+5, "\"\'"))) - return NULL; + return dn; +} - t = xescape(u, "/ "); - free(u); +char *fstab_node_to_udev_node(const char *p) { + assert(p); - if (!t) - return NULL; + if (startswith(p, "LABEL=")) + return tag_to_udev_node(p+6, "label"); - r = asprintf(&dn, "/dev/disk/by-uuid/%s", t); - free(t); + if (startswith(p, "UUID=")) + return tag_to_udev_node(p+5, "uuid"); - if (r < 0) - return NULL; + if (startswith(p, "PARTUUID=")) + return tag_to_udev_node(p+9, "partuuid"); - return dn; - } + if (startswith(p, "PARTLABEL=")) + return tag_to_udev_node(p+10, "partlabel"); return strdup(p); } @@ -4038,7 +4267,12 @@ bool dirent_is_file(const struct dirent *de) { bool dirent_is_file_with_suffix(const struct dirent *de, const char *suffix) { assert(de); - if (!dirent_is_file(de)) + if (de->d_type != DT_REG && + de->d_type != DT_LNK && + de->d_type != DT_UNKNOWN) + return false; + + if (ignore_file_allow_backup(de->d_name)) return false; return endswith(de->d_name, suffix); @@ -4081,7 +4315,7 @@ void execute_directory(const char *directory, DIR *d, char *argv[]) { continue; if (asprintf(&path, "%s/%s", directory, de->d_name) < 0) { - log_error("Out of memory"); + log_oom(); continue; } @@ -4132,7 +4366,7 @@ void execute_directory(const char *directory, DIR *d, char *argv[]) { } if ((path = hashmap_remove(pids, UINT_TO_PTR(si.si_pid)))) { - if (!is_clean_exit(si.si_code, si.si_status)) { + if (!is_clean_exit(si.si_code, si.si_status, NULL)) { if (si.si_code == CLD_EXITED) log_error("%s exited with exit status %i.", path, si.si_status); else @@ -4180,134 +4414,6 @@ bool plymouth_running(void) { return access("/run/plymouth/pid", F_OK) >= 0; } -void parse_syslog_priority(char **p, int *priority) { - int a = 0, b = 0, c = 0; - int k; - - assert(p); - assert(*p); - assert(priority); - - if ((*p)[0] != '<') - return; - - if (!strchr(*p, '>')) - return; - - if ((*p)[2] == '>') { - c = undecchar((*p)[1]); - k = 3; - } else if ((*p)[3] == '>') { - b = undecchar((*p)[1]); - c = undecchar((*p)[2]); - k = 4; - } else if ((*p)[4] == '>') { - a = undecchar((*p)[1]); - b = undecchar((*p)[2]); - c = undecchar((*p)[3]); - k = 5; - } else - return; - - if (a < 0 || b < 0 || c < 0) - return; - - *priority = a*100+b*10+c; - *p += k; -} - -void skip_syslog_pid(char **buf) { - char *p; - - assert(buf); - assert(*buf); - - p = *buf; - - if (*p != '[') - return; - - p++; - p += strspn(p, "0123456789"); - - if (*p != ']') - return; - - p++; - - *buf = p; -} - -void skip_syslog_date(char **buf) { - enum { - LETTER, - SPACE, - NUMBER, - SPACE_OR_NUMBER, - COLON - } sequence[] = { - LETTER, LETTER, LETTER, - SPACE, - SPACE_OR_NUMBER, NUMBER, - SPACE, - SPACE_OR_NUMBER, NUMBER, - COLON, - SPACE_OR_NUMBER, NUMBER, - COLON, - SPACE_OR_NUMBER, NUMBER, - SPACE - }; - - char *p; - unsigned i; - - assert(buf); - assert(*buf); - - p = *buf; - - for (i = 0; i < ELEMENTSOF(sequence); i++, p++) { - - if (!*p) - return; - - switch (sequence[i]) { - - case SPACE: - if (*p != ' ') - return; - break; - - case SPACE_OR_NUMBER: - if (*p == ' ') - break; - - /* fall through */ - - case NUMBER: - if (*p < '0' || *p > '9') - return; - - break; - - case LETTER: - if (!(*p >= 'A' && *p <= 'Z') && - !(*p >= 'a' && *p <= 'z')) - return; - - break; - - case COLON: - if (*p != ':') - return; - break; - - } - } - - *buf = p; -} - char* strshorten(char *s, size_t l) { assert(s); @@ -4586,49 +4692,9 @@ int copy_file(const char *from, const char *to) { return 0; } -int symlink_or_copy(const char *from, const char *to) { - char *pf = NULL, *pt = NULL; - struct stat a, b; - int r; - - assert(from); - assert(to); - - if (path_get_parent(from, &pf) < 0 || - path_get_parent(to, &pt) < 0) { - r = -ENOMEM; - goto finish; - } - - if (stat(pf, &a) < 0 || - stat(pt, &b) < 0) { - r = -errno; - goto finish; - } - - if (a.st_dev != b.st_dev) { - free(pf); - free(pt); - - return copy_file(from, to); - } - - if (symlink(from, to) < 0) { - r = -errno; - goto finish; - } - - r = 0; - -finish: - free(pf); - free(pt); - - return r; -} - -int symlink_or_copy_atomic(const char *from, const char *to) { - char *t, *x; +int symlink_atomic(const char *from, const char *to) { + char *x; + _cleanup_free_ char *t; const char *fn; size_t k; unsigned long long ull; @@ -4656,22 +4722,16 @@ int symlink_or_copy_atomic(const char *from, const char *to) { *x = 0; - r = symlink_or_copy(from, t); - if (r < 0) { - unlink(t); - free(t); - return r; - } + if (symlink(from, t) < 0) + return -errno; if (rename(t, to) < 0) { r = -errno; unlink(t); - free(t); return r; } - free(t); - return r; + return 0; } bool display_is_local(const char *display) { @@ -4708,7 +4768,12 @@ int socket_from_display(const char *display, char **path) { return 0; } -int get_user_creds(const char **username, uid_t *uid, gid_t *gid, const char **home) { +int get_user_creds( + const char **username, + uid_t *uid, gid_t *gid, + const char **home, + const char **shell) { + struct passwd *p; uid_t u; @@ -4729,6 +4794,10 @@ int get_user_creds(const char **username, uid_t *uid, gid_t *gid, const char **h if (home) *home = "/root"; + + if (shell) + *shell = "/bin/sh"; + return 0; } @@ -4760,6 +4829,9 @@ int get_user_creds(const char **username, uid_t *uid, gid_t *gid, const char **h if (home) *home = p->pw_dir; + if (shell) + *shell = p->pw_shell; + return 0; } @@ -4919,10 +4991,11 @@ int get_files_in_directory(const char *path, char ***list) { return -errno; for (;;) { - struct dirent buffer, *de; + struct dirent *de; + union dirent_storage buf; int k; - k = readdir_r(d, &buffer, &de); + k = readdir_r(d, &buf.de, &de); if (k != 0) { r = -k; goto finish; @@ -4976,7 +5049,7 @@ finish: return r; } -char *join(const char *x, ...) { +char *strjoin(const char *x, ...) { va_list ap; size_t l; char *r, *p; @@ -4988,12 +5061,19 @@ char *join(const char *x, ...) { for (;;) { const char *t; + size_t n; t = va_arg(ap, const char *); if (!t) break; - l += strlen(t); + n = strlen(t); + if (n > ((size_t) -1) - l) { + va_end(ap); + return NULL; + } + + l += n; } } else l = 0; @@ -5265,7 +5345,7 @@ int signal_from_string(const char *s) { int offset = 0; unsigned u; - signo =__signal_from_string(s); + signo = __signal_from_string(s); if (signo > 0) return signo; @@ -5586,10 +5666,10 @@ int getenv_for_pid(pid_t pid, const char *field, char **_value) { } int can_sleep(const char *type) { - char *p, *w, *state; + char *w, *state; size_t l, k; - bool found = false; int r; + _cleanup_free_ char *p = NULL; assert(type); @@ -5598,16 +5678,11 @@ int can_sleep(const char *type) { return r == -ENOENT ? 0 : r; k = strlen(type); + FOREACH_WORD_SEPARATOR(w, l, p, WHITESPACE, state) + if (l == k && memcmp(w, type, l) == 0) + return true; - FOREACH_WORD_SEPARATOR(w, l, p, WHITESPACE, state) { - if (l == k && strncmp(w, type, l) == 0) { - found = true; - break; - } - } - - free(p); - return found; + return false; } bool is_valid_documentation_url(const char *url) { @@ -5632,22 +5707,37 @@ bool is_valid_documentation_url(const char *url) { } bool in_initrd(void) { - static int saved = -1; + static __thread int saved = -1; + struct statfs s; + + if (saved >= 0) + return saved; + + /* We make two checks here: + * + * 1. the flag file /etc/initrd-release must exist + * 2. the root file system must be a memory file system + * + * The second check is extra paranoia, since misdetecting an + * initrd can have bad bad consequences due the initrd + * emptying when transititioning to the main systemd. + */ - if (saved < 0) - saved = access("/etc/initrd-release", F_OK) >= 0; + saved = access("/etc/initrd-release", F_OK) >= 0 && + statfs("/", &s) >= 0 && + (s.f_type == TMPFS_MAGIC || s.f_type == RAMFS_MAGIC); return saved; } void warn_melody(void) { - int fd; + _cleanup_close_ int fd = -1; fd = open("/dev/console", O_WRONLY|O_CLOEXEC|O_NOCTTY); if (fd < 0) return; - /* Yeah, this is synchronous. Kinda sucks. Bute well... */ + /* Yeah, this is synchronous. Kinda sucks. But well... */ ioctl(fd, KIOCSOUND, (int)(1193180/440)); usleep(125*USEC_PER_MSEC); @@ -5659,5 +5749,310 @@ void warn_melody(void) { usleep(125*USEC_PER_MSEC); ioctl(fd, KIOCSOUND, 0); - close_nointr_nofail(fd); +} + +int make_console_stdio(void) { + int fd, r; + + /* Make /dev/console the controlling terminal and stdin/stdout/stderr */ + + fd = acquire_terminal("/dev/console", false, true, true, (usec_t) -1); + if (fd < 0) { + log_error("Failed to acquire terminal: %s", strerror(-fd)); + return fd; + } + + r = make_stdio(fd); + if (r < 0) { + log_error("Failed to duplicate terminal fd: %s", strerror(-r)); + return r; + } + + return 0; +} + +int get_home_dir(char **_h) { + char *h; + const char *e; + uid_t u; + struct passwd *p; + + assert(_h); + + /* Take the user specified one */ + e = getenv("HOME"); + if (e) { + h = strdup(e); + if (!h) + return -ENOMEM; + + *_h = h; + return 0; + } + + /* Hardcode home directory for root to avoid NSS */ + u = getuid(); + if (u == 0) { + h = strdup("/root"); + if (!h) + return -ENOMEM; + + *_h = h; + return 0; + } + + /* Check the database... */ + errno = 0; + p = getpwuid(u); + if (!p) + return errno ? -errno : -ESRCH; + + if (!path_is_absolute(p->pw_dir)) + return -EINVAL; + + h = strdup(p->pw_dir); + if (!h) + return -ENOMEM; + + *_h = h; + return 0; +} + +int get_shell(char **_sh) { + char *sh; + const char *e; + uid_t u; + struct passwd *p; + + assert(_sh); + + /* Take the user specified one */ + e = getenv("SHELL"); + if (e) { + sh = strdup(e); + if (!sh) + return -ENOMEM; + + *_sh = sh; + return 0; + } + + /* Hardcode home directory for root to avoid NSS */ + u = getuid(); + if (u == 0) { + sh = strdup("/bin/sh"); + if (!sh) + return -ENOMEM; + + *_sh = sh; + return 0; + } + + /* Check the database... */ + errno = 0; + p = getpwuid(u); + if (!p) + return errno ? -errno : -ESRCH; + + if (!path_is_absolute(p->pw_shell)) + return -EINVAL; + + sh = strdup(p->pw_shell); + if (!sh) + return -ENOMEM; + + *_sh = sh; + return 0; +} + +void freep(void *p) { + free(*(void**) p); +} + +void fclosep(FILE **f) { + if (*f) + fclose(*f); +} + +void closep(int *fd) { + if (*fd >= 0) + close_nointr_nofail(*fd); +} + +void closedirp(DIR **d) { + if (*d) + closedir(*d); +} + +void umaskp(mode_t *u) { + umask(*u); +} + +bool filename_is_safe(const char *p) { + + if (isempty(p)) + return false; + + if (strchr(p, '/')) + return false; + + if (streq(p, ".")) + return false; + + if (streq(p, "..")) + return false; + + if (strlen(p) > FILENAME_MAX) + return false; + + return true; +} + +bool string_is_safe(const char *p) { + const char *t; + + assert(p); + + for (t = p; *t; t++) { + if (*t > 0 && *t < ' ') + return false; + + if (strchr("\\\"\'", *t)) + return false; + } + + return true; +} + +int parse_timestamp(const char *t, usec_t *usec) { + const char *k; + struct tm tm, copy; + time_t x; + usec_t plus = 0, minus = 0, ret; + int r; + + /* + * Allowed syntaxes: + * + * 2012-09-22 16:34:22 + * 2012-09-22 16:34 (seconds will be set to 0) + * 2012-09-22 (time will be set to 00:00:00) + * 16:34:22 (date will be set to today) + * 16:34 (date will be set to today, seconds to 0) + * now + * yesterday (time is set to 00:00:00) + * today (time is set to 00:00:00) + * tomorrow (time is set to 00:00:00) + * +5min + * -5days + * + */ + + assert(t); + assert(usec); + + x = time(NULL); + assert_se(localtime_r(&x, &tm)); + + if (streq(t, "now")) + goto finish; + + else if (streq(t, "today")) { + tm.tm_sec = tm.tm_min = tm.tm_hour = 0; + goto finish; + + } else if (streq(t, "yesterday")) { + tm.tm_mday --; + tm.tm_sec = tm.tm_min = tm.tm_hour = 0; + goto finish; + + } else if (streq(t, "tomorrow")) { + tm.tm_mday ++; + tm.tm_sec = tm.tm_min = tm.tm_hour = 0; + goto finish; + + } else if (t[0] == '+') { + + r = parse_usec(t+1, &plus); + if (r < 0) + return r; + + goto finish; + } else if (t[0] == '-') { + + r = parse_usec(t+1, &minus); + if (r < 0) + return r; + + goto finish; + } + + copy = tm; + k = strptime(t, "%y-%m-%d %H:%M:%S", &tm); + if (k && *k == 0) + goto finish; + + tm = copy; + k = strptime(t, "%Y-%m-%d %H:%M:%S", &tm); + if (k && *k == 0) + goto finish; + + tm = copy; + k = strptime(t, "%y-%m-%d %H:%M", &tm); + if (k && *k == 0) { + tm.tm_sec = 0; + goto finish; + } + + tm = copy; + k = strptime(t, "%Y-%m-%d %H:%M", &tm); + if (k && *k == 0) { + tm.tm_sec = 0; + goto finish; + } + + tm = copy; + k = strptime(t, "%y-%m-%d", &tm); + if (k && *k == 0) { + tm.tm_sec = tm.tm_min = tm.tm_hour = 0; + goto finish; + } + + tm = copy; + k = strptime(t, "%Y-%m-%d", &tm); + if (k && *k == 0) { + tm.tm_sec = tm.tm_min = tm.tm_hour = 0; + goto finish; + } + + tm = copy; + k = strptime(t, "%H:%M:%S", &tm); + if (k && *k == 0) + goto finish; + + tm = copy; + k = strptime(t, "%H:%M", &tm); + if (k && *k == 0) { + tm.tm_sec = 0; + goto finish; + } + + return -EINVAL; + +finish: + x = mktime(&tm); + if (x == (time_t) -1) + return -EINVAL; + + ret = (usec_t) x * USEC_PER_SEC; + + ret += plus; + if (ret > minus) + ret -= minus; + else + ret = 0; + + *usec = ret; + + return 0; }