If user doesn't supply console= in cmdline but instead for example specifies 'stdout-path' in device tree bootlogd fails to detect which tty to use. This patch brings back methods for detecting current console that were removed in commit 986bee6.
It also introduces chdir_int helper function used to suppress warnings of type '-Wunused-result' in findtty(). Signed-off-by: Alexander Vickberg <wickbergs...@gmail.com> --- src/bootlogd.c | 66 +++++++++++++++++++++++++++++++++++++------------- 1 file changed, 49 insertions(+), 17 deletions(-) diff --git a/src/bootlogd.c b/src/bootlogd.c index 10d7c0e..aae66ad 100644 --- a/src/bootlogd.c +++ b/src/bootlogd.c @@ -106,12 +106,25 @@ void handler(int sig) } /* - * Scan /dev and find the device name. + * chdir with error message on fail. */ -/* -This function does not appear to be called anymore. Commenting it -out for now, can probably be removed entirely in the future. +static int chdir_int(const char *path) +{ + int ret; + + if ((ret = chdir(path)) != 0) { + const char *msgprefix = "bootlogd: %s"; + char msg[PATH_MAX + sizeof(msgprefix)]; + snprintf(msg, sizeof(msg), msgprefix, path); + perror(msg); + } + + return ret; +} +/* + * Scan /dev and find the device name. + */ static int findtty(char *res, const char *startdir, int rlen, dev_t dev) { DIR *dir; @@ -120,13 +133,8 @@ static int findtty(char *res, const char *startdir, int rlen, dev_t dev) int r = -1; char *olddir = getcwd(NULL, 0); - if (chdir(startdir) < 0 || (dir = opendir(".")) == NULL) { - int msglen = strlen(startdir) + 11; - char *msg = malloc(msglen); - snprintf(msg, msglen, "bootlogd: %s", startdir); - perror(msg); - free(msg); - chdir(olddir); + if (chdir_int(startdir) < 0 || (dir = opendir(".")) == NULL) { + chdir_int(olddir); return -1; } while ((ent = readdir(dir)) != NULL) { @@ -141,7 +149,7 @@ static int findtty(char *res, const char *startdir, int rlen, dev_t dev) free(path); if (0 == r) { closedir(dir); - chdir(olddir); + chdir_int(olddir); return 0; } continue; @@ -152,22 +160,21 @@ static int findtty(char *res, const char *startdir, int rlen, dev_t dev) if ( (int) (strlen(ent->d_name) + strlen(startdir) + 1) >= rlen) { fprintf(stderr, "bootlogd: console device name too long\n"); closedir(dir); - chdir(olddir); + chdir_int(olddir); return -1; } else { snprintf(res, rlen, "%s/%s", startdir, ent->d_name); closedir(dir); - chdir(olddir); + chdir_int(olddir); return 0; } } } closedir(dir); - chdir(olddir); + chdir_int(olddir); return r; } -*/ /* * For some reason, openpty() in glibc sometimes doesn't @@ -269,7 +276,7 @@ int isconsole(char *s, char *res, int rlen) int consolenames(struct real_cons *cons, int max_consoles) { #ifdef TIOCGDEV - /* This appears to be unused. unsigned int kdev; */ + unsigned int kdev; #endif struct stat st, st2; char buf[KERNEL_COMMAND_LENGTH]; @@ -340,6 +347,31 @@ dontuse: p--; } + if (num_consoles > 0) return num_consoles; +#endif + + fstat(0, &st); + if (major(st.st_rdev) != 5 || minor(st.st_rdev) != 1) { + /* + * Old kernel, can find real device easily. + */ + int r = findtty(cons[num_consoles].name, "/dev", sizeof(cons[num_consoles].name), st.st_rdev); + if (!r) + num_consoles++; + } + + if (num_consoles > 0) return num_consoles; + +#ifdef TIOCGDEV +# ifndef ENOIOCTLCMD +# define ENOIOCTLCMD 515 +# endif + if (ioctl(0, TIOCGDEV, &kdev) == 0) { + int r = findtty(cons[num_consoles].name, "/dev", sizeof(cons[num_consoles].name), (dev_t)kdev); + if (!r) + num_consoles++; + } + if (num_consoles > 0) return num_consoles; #endif -- 2.25.1