chiark / gitweb /
terminal-util: make resolve_dev_console() less weird
authorLennart Poettering <lennart@poettering.net>
Wed, 14 Feb 2018 16:30:37 +0000 (17:30 +0100)
committerSven Eden <yamakuzure@gmx.net>
Wed, 30 May 2018 05:58:56 +0000 (07:58 +0200)
Let's normalize the behaviour: return a negative errno style error code,
and return the resolved string directly as argument.

src/basic/terminal-util.c
src/basic/terminal-util.h

index 3c2c145b6a5f0a0f15071cf7aabd20b02b4598ef..17548ddf0f863530aaa5318b1d9dc8ae72c66f9e 100644 (file)
@@ -682,37 +682,55 @@ int vtnr_from_tty(const char *tty) {
 }
 
 #if 0 /// UNNEEDED by elogind
-char *resolve_dev_console(char **active) {
+ int resolve_dev_console(char **ret) {
+        _cleanup_free_ char *active = NULL;
         char *tty;
+        int r;
+
+        assert(ret);
 
-        /* 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) */
+        /* 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;
+                return -ENOMEDIUM;
 
-        if (read_one_line_file("/sys/class/tty/console/active", active) < 0)
-                return NULL;
+        r = read_one_line_file("/sys/class/tty/console/active", &active);
+        if (r < 0)
+                return r;
 
-        /* If multiple log outputs are configured the last one is what
-         * /dev/console points to */
-        tty = strrchr(*active, ' ');
+        /* If multiple log outputs are configured the last one is what /dev/console points to */
+        tty = strrchr(active, ' ');
         if (tty)
                 tty++;
         else
-                tty = *active;
+                tty = active;
 
         if (streq(tty, "tty0")) {
-                char *tmp;
+                active = mfree(active);
 
                 /* Get the active VC (e.g. tty1) */
-                if (read_one_line_file("/sys/class/tty/tty0/active", &tmp) >= 0) {
-                        free(*active);
-                        tty = *active = tmp;
-                }
+                r = read_one_line_file("/sys/class/tty/tty0/active", &active);
+                if (r < 0)
+                        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) {
@@ -787,16 +805,17 @@ fallback:
 }
 
 bool tty_is_vc_resolve(const char *tty) {
-        _cleanup_free_ char *active = NULL;
+        _cleanup_free_ char *resolved = NULL;
 
         assert(tty);
 
         tty = skip_dev_prefix(tty);
 
         if (streq(tty, "console")) {
-                tty = resolve_dev_console(&active);
-                if (!tty)
+                if (resolve_dev_console(&resolved) < 0)
                         return false;
+
+                tty = resolved;
         }
 
         return tty_is_vc(tty);
index b115ad95e1c50de0d0d807d5695c097127f160ac..21ca41eff6a168795d4104621cbb17eb542b9c0e 100644 (file)
@@ -87,8 +87,8 @@ int ask_string(char **ret, const char *text, ...) _printf_(2, 3);
 
 int vt_disallocate(const char *name);
 
-char *resolve_dev_console(char **active);
 #endif // 0
+int resolve_dev_console(char **ret);
 int get_kernel_consoles(char ***ret);
 bool tty_is_vc(const char *tty);
 #if 0 /// UNNEEDED by elogind