terminal-util: make resolve_dev_console() less weird

Let's normalize the behaviour: return a negative errno style error code,
and return the resolved string directly as argument.
This commit is contained in:
Lennart Poettering 2018-02-14 17:30:37 +01:00
parent bef41af233
commit 7b91264852
3 changed files with 43 additions and 27 deletions

View file

@ -672,37 +672,55 @@ int vtnr_from_tty(const char *tty) {
return i; return i;
} }
char *resolve_dev_console(char **active) { int resolve_dev_console(char **ret) {
_cleanup_free_ char *active = NULL;
char *tty; char *tty;
int r;
/* Resolve where /dev/console is pointing to, if /sys is actually ours assert(ret);
* (i.e. not read-only-mounted which is a sign for container setups) */
/* 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) if (path_is_read_only_fs("/sys") > 0)
return NULL; return -ENOMEDIUM;
if (read_one_line_file("/sys/class/tty/console/active", active) < 0) r = read_one_line_file("/sys/class/tty/console/active", &active);
return NULL; if (r < 0)
return r;
/* If multiple log outputs are configured the last one is what /* If multiple log outputs are configured the last one is what /dev/console points to */
* /dev/console points to */ tty = strrchr(active, ' ');
tty = strrchr(*active, ' ');
if (tty) if (tty)
tty++; tty++;
else else
tty = *active; tty = active;
if (streq(tty, "tty0")) { if (streq(tty, "tty0")) {
char *tmp; active = mfree(active);
/* Get the active VC (e.g. tty1) */ /* Get the active VC (e.g. tty1) */
if (read_one_line_file("/sys/class/tty/tty0/active", &tmp) >= 0) { r = read_one_line_file("/sys/class/tty/tty0/active", &active);
free(*active); if (r < 0)
tty = *active = tmp; return r;
}
tty = active;
} }
return tty; if (tty == active) {
*ret = active;
active = NULL;
} else {
char *tmp;
tmp = strdup(tty);
if (!tmp)
return -ENOMEM;
*ret = tmp;
}
return 0;
} }
int get_kernel_consoles(char ***ret) { int get_kernel_consoles(char ***ret) {
@ -777,16 +795,17 @@ fallback:
} }
bool tty_is_vc_resolve(const char *tty) { bool tty_is_vc_resolve(const char *tty) {
_cleanup_free_ char *active = NULL; _cleanup_free_ char *resolved = NULL;
assert(tty); assert(tty);
tty = skip_dev_prefix(tty); tty = skip_dev_prefix(tty);
if (streq(tty, "console")) { if (streq(tty, "console")) {
tty = resolve_dev_console(&active); if (resolve_dev_console(&resolved) < 0)
if (!tty)
return false; return false;
tty = resolved;
} }
return tty_is_vc(tty); return tty_is_vc(tty);

View file

@ -82,7 +82,7 @@ int ask_string(char **ret, const char *text, ...) _printf_(2, 3);
int vt_disallocate(const char *name); int vt_disallocate(const char *name);
char *resolve_dev_console(char **active); int resolve_dev_console(char **ret);
int get_kernel_consoles(char ***ret); int get_kernel_consoles(char ***ret);
bool tty_is_vc(const char *tty); bool tty_is_vc(const char *tty);
bool tty_is_vc_resolve(const char *tty); bool tty_is_vc_resolve(const char *tty);

View file

@ -3871,8 +3871,7 @@ static int exec_context_load_environment(const Unit *unit, const ExecContext *c,
} }
static bool tty_may_match_dev_console(const char *tty) { static bool tty_may_match_dev_console(const char *tty) {
_cleanup_free_ char *active = NULL; _cleanup_free_ char *resolved = NULL;
char *console;
if (!tty) if (!tty)
return true; return true;
@ -3883,13 +3882,11 @@ static bool tty_may_match_dev_console(const char *tty) {
if (streq(tty, "console")) if (streq(tty, "console"))
return true; return true;
console = resolve_dev_console(&active); if (resolve_dev_console(&resolved) < 0)
/* if we could not resolve, assume it may */ return true; /* if we could not resolve, assume it may */
if (!console)
return true;
/* "tty0" means the active VC, so it may be the same sometimes */ /* "tty0" means the active VC, so it may be the same sometimes */
return streq(console, tty) || (streq(console, "tty0") && tty_is_vc(tty)); return streq(resolved, tty) || (streq(resolved, "tty0") && tty_is_vc(tty));
} }
bool exec_context_may_touch_console(const ExecContext *ec) { bool exec_context_may_touch_console(const ExecContext *ec) {