X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=src%2Fshared%2Futil.c;h=34c5330838908227199eae79cd85b0d2350e0951;hp=f5adedc531a2521f46924cda1619df5af5463055;hb=c17ec25e4d9bd6c8e8617416f813e25b2ebbafc5;hpb=f7900e258dfb8ab55f333d02d96f908ca0ea8899 diff --git a/src/shared/util.c b/src/shared/util.c index f5adedc53..34c533083 100644 --- a/src/shared/util.c +++ b/src/shared/util.c @@ -1325,16 +1325,24 @@ char *bus_path_escape(const char *s) { assert(s); /* Escapes all chars that D-Bus' object path cannot deal - * with. Can be reverse with bus_path_unescape() */ + * with. Can be reverse with bus_path_unescape(). We special + * case the empty string. */ - if (!(r = new(char, strlen(s)*3+1))) + if (*s == 0) + return strdup("_"); + + r = new(char, strlen(s)*3 + 1); + if (!r) return NULL; for (f = s, t = r; *f; f++) { + /* Escape everything that is not a-zA-Z0-9. We also + * escape 0-9 if it's the first character */ + if (!(*f >= 'A' && *f <= 'Z') && !(*f >= 'a' && *f <= 'z') && - !(*f >= '0' && *f <= '9')) { + !(f > s && *f >= '0' && *f <= '9')) { *(t++) = '_'; *(t++) = hexchar(*f >> 4); *(t++) = hexchar(*f); @@ -1352,7 +1360,12 @@ char *bus_path_unescape(const char *f) { assert(f); - if (!(r = strdup(f))) + /* Special case for the empty string */ + if (streq(f, "_")) + return strdup(""); + + r = new(char, strlen(f) + 1); + if (!r) return NULL; for (t = r; *f; f++) { @@ -2859,12 +2872,13 @@ cpu_set_t* cpu_set_malloc(unsigned *ncpus) { } } -int status_vprintf(const char *status, bool ellipse, const char *format, va_list ap) { +int status_vprintf(const char *status, bool ellipse, bool ephemeral, const char *format, va_list ap) { static const char status_indent[] = " "; /* "[" STATUS "] " */ _cleanup_free_ char *s = NULL; _cleanup_close_ int fd = -1; - struct iovec iovec[5]; + struct iovec iovec[6]; int n = 0; + static bool prev_ephemeral; assert(format); @@ -2902,6 +2916,10 @@ int status_vprintf(const char *status, bool ellipse, const char *format, va_list zero(iovec); + if (prev_ephemeral) + IOVEC_SET_STRING(iovec[n++], "\r" ANSI_ERASE_TO_END_OF_LINE); + prev_ephemeral = ephemeral; + if (status) { if (!isempty(status)) { IOVEC_SET_STRING(iovec[n++], "["); @@ -2912,7 +2930,8 @@ int status_vprintf(const char *status, bool ellipse, const char *format, va_list } IOVEC_SET_STRING(iovec[n++], s); - IOVEC_SET_STRING(iovec[n++], "\n"); + if (!ephemeral) + IOVEC_SET_STRING(iovec[n++], "\n"); if (writev(fd, iovec, n) < 0) return -errno; @@ -2920,14 +2939,14 @@ int status_vprintf(const char *status, bool ellipse, const char *format, va_list return 0; } -int status_printf(const char *status, bool ellipse, const char *format, ...) { +int status_printf(const char *status, bool ellipse, bool ephemeral, const char *format, ...) { va_list ap; int r; assert(format); va_start(ap, format); - r = status_vprintf(status, ellipse, format, ap); + r = status_vprintf(status, ellipse, ephemeral, format, ap); va_end(ap); return r; @@ -2944,7 +2963,7 @@ int status_welcome(void) { if (r < 0 && r != -ENOENT) log_warning("Failed to read /etc/os-release: %s", strerror(-r)); - return status_printf(NULL, false, + return status_printf(NULL, false, false, "\nWelcome to \x1B[%sm%s\x1B[0m!\n", isempty(ansi_color) ? "1" : ansi_color, isempty(pretty_name) ? "Linux" : pretty_name); @@ -3503,6 +3522,29 @@ int vtnr_from_tty(const char *tty) { return i; } +char *resolve_dev_console(char **active) { + char *tty; + + /* Resolve where /dev/console is pointing to, if /sys is actually ours + * (i.e. not read-only-mounted which is a sign for container setups) */ + + if (path_is_read_only_fs("/sys") > 0) + return NULL; + + if (read_one_line_file("/sys/class/tty/console/active", active) < 0) + return NULL; + + /* If multiple log outputs are configured the last one is what + * /dev/console points to */ + tty = strrchr(*active, ' '); + if (tty) + tty++; + else + tty = *active; + + return tty; +} + bool tty_is_vc_resolve(const char *tty) { char *active = NULL; bool b; @@ -3512,19 +3554,11 @@ bool tty_is_vc_resolve(const char *tty) { if (startswith(tty, "/dev/")) tty += 5; - /* Resolve where /dev/console is pointing to, if /sys is - * actually ours (i.e. not read-only-mounted which is a sign - * for container setups) */ - if (streq(tty, "console") && path_is_read_only_fs("/sys") <= 0) - if (read_one_line_file("/sys/class/tty/console/active", &active) >= 0) { - /* If multiple log outputs are configured the - * last one is what /dev/console points to */ - tty = strrchr(active, ' '); - if (tty) - tty++; - else - tty = active; - } + if (streq(tty, "console")) { + tty = resolve_dev_console(&active); + if (!tty) + return false; + } b = tty_is_vc(tty); free(active); @@ -3573,8 +3607,8 @@ void execute_directory(const char *directory, DIR *d, char *argv[]) { assert(directory); - /* Executes all binaries in a directory in parallel and waits - * until all they all finished. */ + /* Executes all binaries in a directory in parallel and + * waits for them to finish. */ if (!d) { if (!(_d = opendir(directory))) { @@ -5203,10 +5237,6 @@ int get_shell(char **_sh) { return 0; } -void freep(void *p) { - free(*(void**) p); -} - void fclosep(FILE **f) { if (*f) fclose(*f); @@ -5227,10 +5257,6 @@ void closedirp(DIR **d) { closedir(*d); } -void umaskp(mode_t *u) { - umask(*u); -} - bool filename_is_safe(const char *p) { if (isempty(p)) @@ -5656,3 +5682,47 @@ int search_and_fopen_nulstr(const char *path, const char *mode, const char *sear return search_and_fopen_internal(path, mode, s, _f); } + +int create_tmp_dir(char template[], mode_t mask, bool need_sticky, char** dir_name) { + int r = 0; + char *d = NULL; + bool remove = false; + mode_t _cleanup_umask_ u; + + assert(dir_name); + + u = umask(mask); + d = mkdtemp(template); + if (!d) { + r = -errno; + log_debug("Can't create directory"); + goto fail; + } + + remove = true; + + log_debug("Created temporary directory : %s", template); + + d = strdup(template); + if (!d) { + r = log_oom(); + goto fail; + } + + if (need_sticky) { + r = chmod(template, 0777 | S_ISVTX); + if (r < 0) { + r = -errno; + goto fail; + } + log_debug("Setting sticky bit on : %s", template); + } + + *dir_name = d; + + return 0; +fail: + if (remove) + rmdir(template); + return r; +}