]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lib/consoles: sysfs code refactoring
authorKarel Zak <kzak@redhat.com>
Fri, 9 Nov 2012 10:17:23 +0000 (11:17 +0100)
committerKarel Zak <kzak@redhat.com>
Fri, 9 Nov 2012 10:17:23 +0000 (11:17 +0100)
Signed-off-by: Karel Zak <kzak@redhat.com>
lib/consoles.c

index 36325be96a7c21f6d918ad6aca0e9edab36828ab..6a104c9376ebcb29f14409af34d8035ead8e1e40 100644 (file)
@@ -259,6 +259,62 @@ done:
                fclose(fc);
        return rc;
 }
+
+/*
+ * return codes:
+ *     < 0     - fatal error (no mem or so... )
+ *       0     - success
+ *       1     - recoverable error
+ *       2     - detection not available
+ */
+static int detect_consoles_from_sysfs(struct console **consoles)
+{
+       char *attrib = NULL, *words, *token;
+       DIR *dir = NULL;
+       int rc = 1;
+
+       attrib = actattr("console");
+       if (!attrib)
+               return 2;
+
+       words = attrib;
+
+       dir = opendir("/dev");
+       if (!dir)
+               goto done;
+
+       while ((token = strsep(&words, " \t\r\n"))) {
+               char *name;
+               dev_t comparedev;
+
+               if (*token == '\0')
+                       continue;
+
+               comparedev = devattr(token);
+               if (comparedev == makedev(TTY_MAJOR, 0)) {
+                       char *tmp = actattr(token);
+                       if (!tmp)
+                               continue;
+                       comparedev = devattr(tmp);
+                       free(tmp);
+               }
+
+               name = scandev(dir, comparedev);
+               if (!name)
+                       continue;
+               rc = append_console(consoles, name);
+               if (rc < 0)
+                       goto done;
+       }
+
+       rc = *consoles ? 0 : 1;
+done:
+       free(attrib);
+       if (dir)
+               closedir(dir);
+       return rc;
+}
+
 #endif /* __linux__ */
 
 /*
@@ -275,7 +331,7 @@ int detect_consoles(const char *device, int fallback, struct console **consoles)
        int fd, reconnect = 0, rc;
        dev_t comparedev = 0;
 #ifdef __linux__
-       char *attrib, *cmdline;
+       char *cmdline;
 #endif
        if (!device || !*device)
                fd = dup(fallback);
@@ -368,43 +424,14 @@ console:
         * Detection of devices used for Linux system console using
         * the sysfs /sys/class/tty/ API with kernel 2.6.37 and higher.
         */
-       if ((attrib = actattr("console"))) {
-               char *words = attrib, *token;
-               DIR *dir;
-
-               dir = opendir("/dev");
-               if (!dir) {
-                       free(attrib);
-                       goto fallback;
-               }
-               while ((token = strsep(&words, " \t\r\n"))) {
-                       char * name;
-
-                       if (*token == '\0')
-                               continue;
-                       comparedev = devattr(token);
-                       if (comparedev == makedev(TTY_MAJOR, 0)) {
-                               char *tmp = actattr(token);
-                               if (!tmp)
-                                       continue;
-                               comparedev = devattr(tmp);
-                               free(tmp);
-                       }
-
-                       name = scandev(dir, comparedev);
-                       if (!name)
-                               continue;
-                       rc = append_console(consoles, name);
-                       if (rc < 0)
-                               return rc;
-               }
-               closedir(dir);
-               free(attrib);
-               if (!*consoles)
-                       goto fallback;
-               return reconnect;
+       rc = detect_consoles_from_sysfs(consoles);
+       if (rc == 0)
+               return reconnect;       /* success */
+       if (rc < 0)
+               return rc;              /* fatal error */
+       if (rc == 1)
+               goto fallback;          /* detection error */
 
-       }
        /*
         * Detection of devices used for Linux system console using
         * kernel parameter on the kernels command line.