From: Lennart Poettering Date: Mon, 17 Sep 2012 16:23:10 +0000 (+0200) Subject: util: various cleanups for printing boot status X-Git-Tag: v190~52 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=669bec5d170c7b6cec5194179d2c14244c06817c;hp=84b98e6f5e059515fcfe2677e798f01463cbdd2f;p=elogind.git util: various cleanups for printing boot status --- diff --git a/src/shared/util.c b/src/shared/util.c index 27b668339..84f8565e0 100644 --- a/src/shared/util.c +++ b/src/shared/util.c @@ -3488,10 +3488,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; @@ -3501,11 +3501,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; @@ -3516,7 +3516,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) @@ -3543,53 +3543,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; - - 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"; - - 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); + _cleanup_free_ char *pretty_name = NULL, *ansi_color = NULL; + + 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)); + + 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) { diff --git a/src/shared/util.h b/src/shared/util.h index 1a607cf37..71b5c52c4 100644 --- a/src/shared/util.h +++ b/src/shared/util.h @@ -370,9 +370,9 @@ int pipe_eof(int fd); cpu_set_t* cpu_set_malloc(unsigned *ncpus); -void status_vprintf(const char *status, bool ellipse, const char *format, va_list ap); -void status_printf(const char *status, bool ellipse, const char *format, ...); -void status_welcome(void); +int status_vprintf(const char *status, bool ellipse, const char *format, va_list ap); +int status_printf(const char *status, bool ellipse, const char *format, ...); +int status_welcome(void); int fd_columns(int fd); unsigned columns(void);