summaryrefslogtreecommitdiffstats
path: root/lib/consoles.c
diff options
context:
space:
mode:
authorKarel Zak2012-11-09 11:17:23 +0100
committerKarel Zak2012-11-09 11:17:23 +0100
commit615eada9b22255a1544b7a024e3ff4474121c5f7 (patch)
treef3195242b4acca75922e24d90fdf7c2562fcb10b /lib/consoles.c
parentlib/consoles: /proc/consoles code refactoring (diff)
downloadkernel-qcow2-util-linux-615eada9b22255a1544b7a024e3ff4474121c5f7.tar.gz
kernel-qcow2-util-linux-615eada9b22255a1544b7a024e3ff4474121c5f7.tar.xz
kernel-qcow2-util-linux-615eada9b22255a1544b7a024e3ff4474121c5f7.zip
lib/consoles: sysfs code refactoring
Signed-off-by: Karel Zak <kzak@redhat.com>
Diffstat (limited to 'lib/consoles.c')
-rw-r--r--lib/consoles.c101
1 files changed, 64 insertions, 37 deletions
diff --git a/lib/consoles.c b/lib/consoles.c
index 36325be96..6a104c937 100644
--- a/lib/consoles.c
+++ b/lib/consoles.c
@@ -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.