From 6ea0921e16eeb94c591c994d71bb0a9ffcb4834e Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Fri, 9 Nov 2012 12:18:31 +0100 Subject: lib/consoles: TIOCGDEV code refactoring - add missing scandev() otherwise TIOCGDEV make no sense - use this method independent on /proc/cmdline code Signed-off-by: Karel Zak --- lib/consoles.c | 109 +++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 67 insertions(+), 42 deletions(-) (limited to 'lib/consoles.c') diff --git a/lib/consoles.c b/lib/consoles.c index 5f6be7108..206efecff 100644 --- a/lib/consoles.c +++ b/lib/consoles.c @@ -397,6 +397,60 @@ done: return rc; } +static int detect_consoles_from_tiocgdev(struct console **consoles, + int fallback, + const char *device) +{ +#ifdef TIOCGDEV + unsigned int devnum; + char *name; + int rc = 1, fd = -1; + dev_t comparedev; + DIR *dir = NULL; + + if (!device || !*device) + fd = dup(fallback); + else + fd = open(device, O_RDWR|O_NONBLOCK|O_NOCTTY|O_CLOEXEC); + + if (fd < 0) + goto done; + if (ioctl (fd, TIOCGDEV, &devnum) < 0) + goto done; + + comparedev = (dev_t) devnum; + dir = opendir("/dev"); + if (!dir) + goto done; + + name = scandev(dir, comparedev); + closedir(dir); + + if (!name) { + name = (char *) (device && *device ? device : ttyname(fallback)); + if (!name) + name = "/dev/tty1"; + + name = strdup(name); + if (!name) { + rc = -ENOMEM; + goto done; + } + } + rc = append_console(consoles, name); + if (rc < 0) + goto done; + if (*consoles && (!device || !*device)) + (*consoles)->fd = fallback; + + rc = *consoles ? 0 : 1; +done: + if (fd >= 0) + close(fd); + return rc; +#endif + return 2; +} #endif /* __linux__ */ /* @@ -524,50 +578,21 @@ console: if (rc == 1) goto fallback; /* detection error */ - /* - * Detection of the device used for Linux system console using - * the ioctl TIOCGDEV if available (e.g. official 2.6.38). - */ - if (!*consoles) { -#ifdef TIOCGDEV - unsigned int devnum; - const char *name; - - if (!device || *device == '\0') - fd = dup(fallback); - else fd = open(device, O_RDWR|O_NONBLOCK|O_NOCTTY|O_CLOEXEC); - - if (fd < 0) - goto fallback; - - if (ioctl (fd, TIOCGDEV, &devnum) < 0) { - close(fd); - goto fallback; - } - comparedev = (dev_t)devnum; - close(fd); - - if (device && *device != '\0') - name = device; - else name = ttyname(fallback); - - if (!name) - name = "/dev/tty1"; - - rc = append_console(consoles, strdup(name)); - if (rc < 0) - return rc; - if (*consoles) { - if (!device || *device == '\0') - (*consoles)->fd = fallback; - return reconnect; - } -#endif - goto fallback; - } - return reconnect; + /* + * Detection of the device used for Linux system console using + * the ioctl TIOCGDEV if available (e.g. official 2.6.38). + */ + rc = detect_consoles_from_tiocgdev(consoles, fallback, device); + if (rc == 0) + return reconnect; /* success */ + if (rc < 0) + return rc; /* fatal error */ + if (rc == 1) + goto fallback; /* detection error */ + return reconnect; #endif /* __linux __ */ + fallback: if (fallback >= 0) { const char *name; -- cgit v1.2.3-55-g7522