2012-11-08 08:55:48 -06:00
|
|
|
/*
|
|
|
|
* consoles.c Routines to detect the system consoles
|
|
|
|
*
|
|
|
|
* Copyright (c) 2011 SuSE LINUX Products GmbH, All rights reserved.
|
2012-11-08 10:04:01 -06:00
|
|
|
* Copyright (C) 2012 Karel Zak <kzak@redhat.com>
|
2012-12-18 08:02:35 -06:00
|
|
|
* Copyright (C) 2012 Werner Fink <werner@suse.de>
|
2012-11-08 08:55:48 -06:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2, or (at your option)
|
|
|
|
* any later version.
|
2012-11-08 10:04:01 -06:00
|
|
|
*
|
2012-11-08 08:55:48 -06:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program (see the file COPYING); if not, write to the
|
|
|
|
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
|
|
|
|
* MA 02110-1301, USA.
|
|
|
|
*
|
|
|
|
* Author: Werner Fink <werner@suse.de>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <limits.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#ifdef __linux__
|
2013-03-20 08:40:19 -05:00
|
|
|
# include <sys/vt.h>
|
|
|
|
# include <sys/kd.h>
|
|
|
|
# include <linux/serial.h>
|
|
|
|
# include <linux/major.h>
|
2012-11-08 08:55:48 -06:00
|
|
|
#endif
|
|
|
|
#include <dirent.h>
|
2016-02-11 06:35:26 -06:00
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
2012-11-08 08:55:48 -06:00
|
|
|
#include <unistd.h>
|
2012-11-08 10:04:01 -06:00
|
|
|
|
2012-12-10 06:27:10 -06:00
|
|
|
#ifdef USE_SULOGIN_EMERGENCY_MOUNT
|
|
|
|
# include <sys/mount.h>
|
|
|
|
# include <linux/fs.h>
|
|
|
|
# include <linux/magic.h>
|
|
|
|
# ifndef MNT_DETACH
|
|
|
|
# define MNT_DETACH 2
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
2012-11-08 10:04:01 -06:00
|
|
|
#include "c.h"
|
|
|
|
#include "canonicalize.h"
|
2012-12-23 15:14:21 -06:00
|
|
|
#include "sulogin-consoles.h"
|
2012-11-08 08:55:48 -06:00
|
|
|
|
|
|
|
#if !defined(__STDC_VERSION__) || (__STDC_VERSION__ < 199901L)
|
|
|
|
# ifndef typeof
|
|
|
|
# define typeof __typeof__
|
|
|
|
# endif
|
|
|
|
# ifndef restrict
|
|
|
|
# define restrict __restrict__
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define alignof(type) ((sizeof(type)+(sizeof(void*)-1)) & ~(sizeof(void*)-1))
|
2012-12-18 08:02:35 -06:00
|
|
|
#define strsize(string) (strlen((string))+1)
|
2012-11-08 08:55:48 -06:00
|
|
|
|
2012-11-09 05:44:00 -06:00
|
|
|
static int consoles_debug;
|
|
|
|
#define DBG(x) do { \
|
|
|
|
if (consoles_debug) { \
|
|
|
|
fputs("consoles debug: ", stderr); \
|
|
|
|
x; \
|
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
static inline void __attribute__ ((__format__ (__printf__, 1, 2)))
|
2017-04-15 15:46:38 -05:00
|
|
|
dbgprint(const char * const mesg, ...)
|
2012-11-09 05:44:00 -06:00
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, mesg);
|
|
|
|
vfprintf(stderr, mesg, ap);
|
|
|
|
va_end(ap);
|
|
|
|
fputc('\n', stderr);
|
|
|
|
}
|
|
|
|
|
2012-12-10 06:27:10 -06:00
|
|
|
#ifdef USE_SULOGIN_EMERGENCY_MOUNT
|
|
|
|
/*
|
|
|
|
* Make C library standard calls such like ttyname(3) work
|
|
|
|
* even if the system does not show any of the standard
|
|
|
|
* directories.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static uint32_t emergency_flags;
|
|
|
|
# define MNT_PROCFS 0x0001
|
|
|
|
# define MNT_DEVTMPFS 0x0002
|
|
|
|
|
|
|
|
void emergency_do_umounts(void)
|
|
|
|
{
|
|
|
|
if (emergency_flags & MNT_DEVTMPFS)
|
|
|
|
umount2("/dev", MNT_DETACH);
|
|
|
|
if (emergency_flags & MNT_PROCFS)
|
|
|
|
umount2("/proc", MNT_DETACH);
|
|
|
|
}
|
|
|
|
|
|
|
|
void emergency_do_mounts(void)
|
|
|
|
{
|
|
|
|
struct stat rt, xt;
|
|
|
|
|
|
|
|
if (emergency_flags) {
|
|
|
|
emergency_flags = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stat("/", &rt) != 0) {
|
2016-11-01 13:40:33 -05:00
|
|
|
warn("cannot get file status of root file system\n");
|
2012-12-10 06:27:10 -06:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stat("/proc", &xt) == 0
|
|
|
|
&& rt.st_dev == xt.st_dev
|
|
|
|
&& mount("proc", "/proc", "proc", MS_RELATIME, NULL) == 0)
|
|
|
|
emergency_flags |= MNT_PROCFS;
|
|
|
|
|
|
|
|
if (stat("/dev", &xt) == 0
|
|
|
|
&& rt.st_dev == xt.st_dev
|
|
|
|
&& mount("devtmpfs", "/dev", "devtmpfs",
|
|
|
|
MS_RELATIME, "mode=0755,nr_inodes=0") == 0) {
|
|
|
|
|
|
|
|
emergency_flags |= MNT_DEVTMPFS;
|
|
|
|
mknod("/dev/console", S_IFCHR|S_IRUSR|S_IWUSR,
|
|
|
|
makedev(TTYAUX_MAJOR, 1));
|
|
|
|
|
|
|
|
if (symlink("/proc/self/fd", "/dev/fd") == 0) {
|
|
|
|
ignore_result( symlink("fd/0", "/dev/stdin") );
|
|
|
|
ignore_result( symlink("fd/1", "/dev/stdout") );
|
|
|
|
ignore_result( symlink("fd/2", "/dev/stderr") );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-12-23 15:44:05 -06:00
|
|
|
|
|
|
|
#else /* !USE_SULOGIN_EMERGENCY_MOUNT */
|
|
|
|
|
|
|
|
void emergency_do_umounts(void) { }
|
|
|
|
void emergency_do_mounts(void) { }
|
|
|
|
|
2012-12-10 06:27:10 -06:00
|
|
|
#endif /* USE_SULOGIN_EMERGENCY_MOUNT */
|
|
|
|
|
2012-11-08 08:55:48 -06:00
|
|
|
/*
|
|
|
|
* Read and allocate one line from file,
|
|
|
|
* the caller has to free the result
|
|
|
|
*/
|
2012-11-08 10:04:01 -06:00
|
|
|
static __attribute__((__nonnull__))
|
2017-04-15 15:46:38 -05:00
|
|
|
char *oneline(const char * const file)
|
2012-11-08 08:55:48 -06:00
|
|
|
{
|
|
|
|
FILE *fp;
|
2012-11-08 10:04:01 -06:00
|
|
|
char *ret = NULL;
|
2015-06-05 23:26:43 -05:00
|
|
|
size_t dummy = 0;
|
|
|
|
ssize_t len;
|
2012-11-08 08:55:48 -06:00
|
|
|
|
2012-11-09 05:44:00 -06:00
|
|
|
DBG(dbgprint("reading %s", file));
|
|
|
|
|
2016-04-16 09:43:17 -05:00
|
|
|
if (!(fp = fopen(file, "r" UL_CLOEXECSTR)))
|
2012-11-08 10:04:01 -06:00
|
|
|
return NULL;
|
2015-06-05 23:26:43 -05:00
|
|
|
len = getline(&ret, &dummy, fp);
|
|
|
|
if (len >= 0) {
|
2012-11-08 10:04:01 -06:00
|
|
|
char *nl;
|
|
|
|
|
|
|
|
if (len)
|
|
|
|
ret[len-1] = '\0';
|
|
|
|
if ((nl = strchr(ret, '\n')))
|
|
|
|
*nl = '\0';
|
|
|
|
}
|
|
|
|
|
2012-11-08 08:55:48 -06:00
|
|
|
fclose(fp);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef __linux__
|
|
|
|
/*
|
|
|
|
* Read and determine active attribute for tty below
|
|
|
|
* /sys/class/tty, the caller has to free the result.
|
|
|
|
*/
|
2012-11-08 10:04:01 -06:00
|
|
|
static __attribute__((__malloc__))
|
2017-04-15 15:46:38 -05:00
|
|
|
char *actattr(const char * const tty)
|
2012-11-08 08:55:48 -06:00
|
|
|
{
|
2012-11-08 10:04:01 -06:00
|
|
|
char *ret, *path;
|
2012-11-08 08:55:48 -06:00
|
|
|
|
2012-11-08 10:04:01 -06:00
|
|
|
if (!tty || !*tty)
|
|
|
|
return NULL;
|
2012-11-08 08:55:48 -06:00
|
|
|
if (asprintf(&path, "/sys/class/tty/%s/active", tty) < 0)
|
2012-11-08 10:04:01 -06:00
|
|
|
return NULL;
|
2012-11-08 08:55:48 -06:00
|
|
|
|
2012-11-08 10:04:01 -06:00
|
|
|
ret = oneline(path);
|
2012-11-08 08:55:48 -06:00
|
|
|
free(path);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Read and determine device attribute for tty below
|
|
|
|
* /sys/class/tty.
|
|
|
|
*/
|
|
|
|
static
|
2017-04-15 15:46:38 -05:00
|
|
|
dev_t devattr(const char * const tty)
|
2012-11-08 08:55:48 -06:00
|
|
|
{
|
|
|
|
dev_t dev = 0;
|
|
|
|
char *path, *value;
|
|
|
|
|
2012-11-08 10:04:01 -06:00
|
|
|
if (!tty || !*tty)
|
|
|
|
return 0;
|
2012-11-08 08:55:48 -06:00
|
|
|
if (asprintf(&path, "/sys/class/tty/%s/dev", tty) < 0)
|
2012-11-08 10:04:01 -06:00
|
|
|
return 0;
|
2012-11-08 08:55:48 -06:00
|
|
|
|
2012-11-08 10:04:01 -06:00
|
|
|
value = oneline(path);
|
|
|
|
if (value) {
|
|
|
|
unsigned int maj, min;
|
|
|
|
|
|
|
|
if (sscanf(value, "%u:%u", &maj, &min) == 2)
|
|
|
|
dev = makedev(maj, min);
|
|
|
|
free(value);
|
|
|
|
}
|
2012-11-08 08:55:48 -06:00
|
|
|
|
|
|
|
free(path);
|
|
|
|
return dev;
|
|
|
|
}
|
|
|
|
#endif /* __linux__ */
|
|
|
|
|
|
|
|
/*
|
2016-02-03 08:00:37 -06:00
|
|
|
* Search below /dev for the character device in `dev_t comparedev' variable.
|
2016-02-11 06:35:26 -06:00
|
|
|
* Note that realpath(3) is used here to avoid not existent devices due the
|
|
|
|
* strdup(3) used in our canonicalize_path()!
|
2012-11-08 08:55:48 -06:00
|
|
|
*/
|
|
|
|
static
|
|
|
|
#ifdef __GNUC__
|
|
|
|
__attribute__((__nonnull__,__malloc__,__hot__))
|
|
|
|
#endif
|
2017-04-15 15:46:38 -05:00
|
|
|
char* scandev(DIR *dir, const dev_t comparedev)
|
2012-11-08 08:55:48 -06:00
|
|
|
{
|
2016-02-11 06:35:26 -06:00
|
|
|
char path[PATH_MAX];
|
2012-11-08 10:04:01 -06:00
|
|
|
char *name = NULL;
|
2017-04-15 15:46:38 -05:00
|
|
|
const struct dirent *dent;
|
2016-02-11 06:35:26 -06:00
|
|
|
int len, fd;
|
2012-11-08 08:55:48 -06:00
|
|
|
|
2012-11-09 05:44:00 -06:00
|
|
|
DBG(dbgprint("scanning /dev for %u:%u", major(comparedev), minor(comparedev)));
|
|
|
|
|
2016-02-11 06:35:26 -06:00
|
|
|
/*
|
|
|
|
* Try udev links on character devices first.
|
|
|
|
*/
|
|
|
|
if ((len = snprintf(path, sizeof(path),
|
|
|
|
"/dev/char/%u:%u", major(comparedev), minor(comparedev))) > 0 &&
|
|
|
|
(size_t)len < sizeof(path)) {
|
|
|
|
|
|
|
|
name = realpath(path, NULL);
|
|
|
|
if (name)
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2012-11-08 08:55:48 -06:00
|
|
|
fd = dirfd(dir);
|
|
|
|
rewinddir(dir);
|
|
|
|
while ((dent = readdir(dir))) {
|
|
|
|
struct stat st;
|
2013-11-12 05:26:13 -06:00
|
|
|
|
|
|
|
#ifdef _DIRENT_HAVE_D_TYPE
|
|
|
|
if (dent->d_type != DT_UNKNOWN && dent->d_type != DT_CHR)
|
|
|
|
continue;
|
|
|
|
#endif
|
2012-11-08 08:55:48 -06:00
|
|
|
if (fstatat(fd, dent->d_name, &st, 0) < 0)
|
|
|
|
continue;
|
|
|
|
if (!S_ISCHR(st.st_mode))
|
|
|
|
continue;
|
|
|
|
if (comparedev != st.st_rdev)
|
|
|
|
continue;
|
2016-02-11 06:35:26 -06:00
|
|
|
if ((len = snprintf(path, sizeof(path), "/dev/%s", dent->d_name)) < 0 ||
|
|
|
|
(size_t)len >= sizeof(path))
|
2012-11-08 08:55:48 -06:00
|
|
|
continue;
|
2012-12-10 06:27:10 -06:00
|
|
|
|
2016-02-11 06:35:26 -06:00
|
|
|
name = realpath(path, NULL);
|
|
|
|
if (name)
|
|
|
|
goto out;
|
2012-11-08 08:55:48 -06:00
|
|
|
}
|
2012-11-08 10:04:01 -06:00
|
|
|
|
2016-02-11 06:35:26 -06:00
|
|
|
#ifdef USE_SULOGIN_EMERGENCY_MOUNT
|
|
|
|
/*
|
|
|
|
* There was no /dev mounted hence and no device was found hence we create our own.
|
|
|
|
*/
|
|
|
|
if (!name && (emergency_flags & MNT_DEVTMPFS)) {
|
|
|
|
|
|
|
|
if ((len = snprintf(path, sizeof(path),
|
|
|
|
"/dev/tmp-%u:%u", major(comparedev), minor(comparedev))) < 0 ||
|
|
|
|
(size_t)len >= sizeof(path))
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
if (mknod(path, S_IFCHR|S_IRUSR|S_IWUSR, comparedev) < 0 && errno != EEXIST)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
name = realpath(path, NULL);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
out:
|
2012-11-08 08:55:48 -06:00
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Default control characters for an unknown terminal line.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Allocate an aligned `struct console' memory area,
|
|
|
|
* initialize its default values, and append it to
|
|
|
|
* the global linked list.
|
|
|
|
*/
|
|
|
|
static
|
|
|
|
#ifdef __GNUC__
|
2016-07-02 04:28:55 -05:00
|
|
|
__attribute__((__hot__))
|
2012-11-08 08:55:48 -06:00
|
|
|
#endif
|
2017-04-15 15:46:38 -05:00
|
|
|
int append_console(struct list_head *consoles, const char * const name)
|
2012-11-08 08:55:48 -06:00
|
|
|
{
|
|
|
|
struct console *restrict tail;
|
2017-04-15 15:46:38 -05:00
|
|
|
const struct console *last = NULL;
|
2012-11-08 10:25:09 -06:00
|
|
|
|
2020-07-09 12:14:32 -05:00
|
|
|
DBG(dbgprint("appending %s", name));
|
2012-11-09 05:44:00 -06:00
|
|
|
|
2012-12-18 08:02:35 -06:00
|
|
|
if (!list_empty(consoles))
|
|
|
|
last = list_last_entry(consoles, struct console, entry);
|
|
|
|
|
|
|
|
if (posix_memalign((void *) &tail, sizeof(void *),
|
|
|
|
alignof(struct console) + strsize(name)) != 0)
|
2012-11-09 03:18:49 -06:00
|
|
|
return -ENOMEM;
|
2012-11-08 08:55:48 -06:00
|
|
|
|
2012-12-18 08:02:35 -06:00
|
|
|
INIT_LIST_HEAD(&tail->entry);
|
2013-01-08 08:38:40 -06:00
|
|
|
INIT_CHARDATA(&tail->cp);
|
2012-11-09 02:52:35 -06:00
|
|
|
|
2012-12-18 08:02:35 -06:00
|
|
|
list_add_tail(&tail->entry, consoles);
|
|
|
|
tail->tty = ((char *) tail) + alignof(struct console);
|
|
|
|
strcpy(tail->tty, name);
|
2012-11-08 08:55:48 -06:00
|
|
|
|
|
|
|
tail->file = (FILE*)0;
|
|
|
|
tail->flags = 0;
|
|
|
|
tail->fd = -1;
|
2012-11-09 02:52:35 -06:00
|
|
|
tail->id = last ? last->id + 1 : 0;
|
2016-02-11 06:35:26 -06:00
|
|
|
tail->pid = -1;
|
2012-11-08 08:55:48 -06:00
|
|
|
memset(&tail->tio, 0, sizeof(tail->tio));
|
|
|
|
|
2012-11-09 03:18:49 -06:00
|
|
|
return 0;
|
2012-11-08 08:55:48 -06:00
|
|
|
}
|
|
|
|
|
2012-11-09 03:49:56 -06:00
|
|
|
#ifdef __linux__
|
|
|
|
/*
|
|
|
|
* return codes:
|
|
|
|
* < 0 - fatal error (no mem or so... )
|
|
|
|
* 0 - success
|
|
|
|
* 1 - recoverable error
|
|
|
|
* 2 - detection not available
|
|
|
|
*/
|
2012-12-18 08:02:35 -06:00
|
|
|
static int detect_consoles_from_proc(struct list_head *consoles)
|
2012-11-09 03:49:56 -06:00
|
|
|
{
|
|
|
|
char fbuf[16 + 1];
|
|
|
|
DIR *dir = NULL;
|
|
|
|
FILE *fc = NULL;
|
2014-05-08 05:09:23 -05:00
|
|
|
int maj, min, rc = 1, matches;
|
2012-11-09 03:49:56 -06:00
|
|
|
|
2012-11-09 05:44:00 -06:00
|
|
|
DBG(dbgprint("trying /proc"));
|
2012-11-09 03:49:56 -06:00
|
|
|
|
2016-04-16 09:43:17 -05:00
|
|
|
fc = fopen("/proc/consoles", "r" UL_CLOEXECSTR);
|
2012-11-09 05:44:00 -06:00
|
|
|
if (!fc) {
|
|
|
|
rc = 2;
|
|
|
|
goto done;
|
|
|
|
}
|
2012-11-09 03:49:56 -06:00
|
|
|
dir = opendir("/dev");
|
|
|
|
if (!dir)
|
|
|
|
goto done;
|
|
|
|
|
2014-05-08 05:09:23 -05:00
|
|
|
while ((matches = fscanf(fc, "%*s %*s (%16[^)]) %d:%d", fbuf, &maj, &min)) >= 1) {
|
2012-11-09 03:49:56 -06:00
|
|
|
char *name;
|
|
|
|
dev_t comparedev;
|
|
|
|
|
2014-05-08 05:09:23 -05:00
|
|
|
if (matches != 3)
|
|
|
|
continue;
|
2012-11-09 03:49:56 -06:00
|
|
|
if (!strchr(fbuf, 'E'))
|
|
|
|
continue;
|
|
|
|
comparedev = makedev(maj, min);
|
|
|
|
name = scandev(dir, comparedev);
|
|
|
|
if (!name)
|
|
|
|
continue;
|
|
|
|
rc = append_console(consoles, name);
|
2012-12-18 08:02:35 -06:00
|
|
|
free(name);
|
2012-11-09 03:49:56 -06:00
|
|
|
if (rc < 0)
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2012-12-18 08:02:35 -06:00
|
|
|
rc = list_empty(consoles) ? 1 : 0;
|
2012-11-09 03:49:56 -06:00
|
|
|
done:
|
|
|
|
if (dir)
|
|
|
|
closedir(dir);
|
|
|
|
if (fc)
|
|
|
|
fclose(fc);
|
2012-11-09 05:44:00 -06:00
|
|
|
DBG(dbgprint("[/proc rc=%d]", rc));
|
2012-11-09 03:49:56 -06:00
|
|
|
return rc;
|
|
|
|
}
|
2012-11-09 04:17:23 -06:00
|
|
|
|
|
|
|
/*
|
|
|
|
* return codes:
|
|
|
|
* < 0 - fatal error (no mem or so... )
|
|
|
|
* 0 - success
|
|
|
|
* 1 - recoverable error
|
|
|
|
* 2 - detection not available
|
|
|
|
*/
|
2012-12-18 08:02:35 -06:00
|
|
|
static int detect_consoles_from_sysfs(struct list_head *consoles)
|
2012-11-09 04:17:23 -06:00
|
|
|
{
|
|
|
|
char *attrib = NULL, *words, *token;
|
|
|
|
DIR *dir = NULL;
|
|
|
|
int rc = 1;
|
|
|
|
|
2012-11-09 05:44:00 -06:00
|
|
|
DBG(dbgprint("trying /sys"));
|
|
|
|
|
2012-11-09 04:17:23 -06:00
|
|
|
attrib = actattr("console");
|
2012-11-09 05:44:00 -06:00
|
|
|
if (!attrib) {
|
|
|
|
rc = 2;
|
|
|
|
goto done;
|
|
|
|
}
|
2012-11-09 04:17:23 -06:00
|
|
|
|
|
|
|
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);
|
2012-12-18 08:02:35 -06:00
|
|
|
free(name);
|
2012-11-09 04:17:23 -06:00
|
|
|
if (rc < 0)
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2012-12-18 08:02:35 -06:00
|
|
|
rc = list_empty(consoles) ? 1 : 0;
|
2012-11-09 04:17:23 -06:00
|
|
|
done:
|
|
|
|
free(attrib);
|
|
|
|
if (dir)
|
|
|
|
closedir(dir);
|
2012-11-09 05:44:00 -06:00
|
|
|
DBG(dbgprint("[/sys rc=%d]", rc));
|
2012-11-09 04:17:23 -06:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2012-11-09 04:52:11 -06:00
|
|
|
|
2012-12-18 08:02:35 -06:00
|
|
|
static int detect_consoles_from_cmdline(struct list_head *consoles)
|
2012-11-09 04:52:11 -06:00
|
|
|
{
|
|
|
|
char *cmdline, *words, *token;
|
|
|
|
dev_t comparedev;
|
2012-11-09 05:44:00 -06:00
|
|
|
DIR *dir = NULL;
|
2012-11-09 04:52:11 -06:00
|
|
|
int rc = 1, fd;
|
|
|
|
|
2012-11-09 05:44:00 -06:00
|
|
|
DBG(dbgprint("trying kernel cmdline"));
|
|
|
|
|
2012-11-09 04:52:11 -06:00
|
|
|
cmdline = oneline("/proc/cmdline");
|
2012-11-09 05:44:00 -06:00
|
|
|
if (!cmdline) {
|
|
|
|
rc = 2;
|
|
|
|
goto done;
|
|
|
|
}
|
2012-11-09 04:52:11 -06:00
|
|
|
|
|
|
|
words= cmdline;
|
|
|
|
dir = opendir("/dev");
|
|
|
|
if (!dir)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
while ((token = strsep(&words, " \t\r\n"))) {
|
|
|
|
#ifdef TIOCGDEV
|
|
|
|
unsigned int devnum;
|
|
|
|
#else
|
|
|
|
struct vt_stat vt;
|
|
|
|
struct stat st;
|
|
|
|
#endif
|
|
|
|
char *colon, *name;
|
|
|
|
|
|
|
|
if (*token != 'c')
|
|
|
|
continue;
|
|
|
|
if (strncmp(token, "console=", 8) != 0)
|
|
|
|
continue;
|
|
|
|
token += 8;
|
|
|
|
|
|
|
|
if (strcmp(token, "brl") == 0)
|
|
|
|
token += 4;
|
|
|
|
if ((colon = strchr(token, ',')))
|
|
|
|
*colon = '\0';
|
|
|
|
|
|
|
|
if (asprintf(&name, "/dev/%s", token) < 0)
|
|
|
|
continue;
|
|
|
|
if ((fd = open(name, O_RDWR|O_NONBLOCK|O_NOCTTY|O_CLOEXEC)) < 0) {
|
|
|
|
free(name);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
free(name);
|
|
|
|
#ifdef TIOCGDEV
|
|
|
|
if (ioctl (fd, TIOCGDEV, &devnum) < 0) {
|
|
|
|
close(fd);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
comparedev = (dev_t) devnum;
|
|
|
|
#else
|
|
|
|
if (fstat(fd, &st) < 0) {
|
|
|
|
close(fd);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
comparedev = st.st_rdev;
|
|
|
|
if (comparedev == makedev(TTY_MAJOR, 0)) {
|
|
|
|
if (ioctl(fd, VT_GETSTATE, &vt) < 0) {
|
|
|
|
close(fd);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
comparedev = makedev(TTY_MAJOR, (int)vt.v_active);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
name = scandev(dir, comparedev);
|
|
|
|
if (!name)
|
|
|
|
continue;
|
|
|
|
rc = append_console(consoles, name);
|
2012-12-18 08:02:35 -06:00
|
|
|
free(name);
|
2012-11-09 04:52:11 -06:00
|
|
|
if (rc < 0)
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2012-12-18 08:02:35 -06:00
|
|
|
rc = list_empty(consoles) ? 1 : 0;
|
2012-11-09 04:52:11 -06:00
|
|
|
done:
|
|
|
|
if (dir)
|
|
|
|
closedir(dir);
|
|
|
|
free(cmdline);
|
2012-11-09 05:44:00 -06:00
|
|
|
DBG(dbgprint("[kernel cmdline rc=%d]", rc));
|
2012-11-09 04:52:11 -06:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2013-04-17 10:23:26 -05:00
|
|
|
#ifdef TIOCGDEV
|
2012-12-18 08:02:35 -06:00
|
|
|
static int detect_consoles_from_tiocgdev(struct list_head *consoles,
|
2017-04-15 15:46:38 -05:00
|
|
|
const int fallback,
|
2012-11-09 05:18:31 -06:00
|
|
|
const char *device)
|
|
|
|
{
|
|
|
|
unsigned int devnum;
|
|
|
|
char *name;
|
|
|
|
int rc = 1, fd = -1;
|
|
|
|
dev_t comparedev;
|
|
|
|
DIR *dir = NULL;
|
2012-12-18 08:02:35 -06:00
|
|
|
struct console *console;
|
2012-11-09 05:18:31 -06:00
|
|
|
|
2012-11-09 05:44:00 -06:00
|
|
|
DBG(dbgprint("trying tiocgdev"));
|
|
|
|
|
2012-11-09 05:18:31 -06:00
|
|
|
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);
|
2012-12-18 08:02:35 -06:00
|
|
|
free(name);
|
2012-11-09 05:18:31 -06:00
|
|
|
if (rc < 0)
|
|
|
|
goto done;
|
2012-12-18 08:02:35 -06:00
|
|
|
if (list_empty(consoles)) {
|
|
|
|
rc = 1;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
console = list_last_entry(consoles, struct console, entry);
|
|
|
|
if (console && (!device || !*device))
|
|
|
|
console->fd = fallback;
|
2012-11-09 05:18:31 -06:00
|
|
|
done:
|
|
|
|
if (fd >= 0)
|
|
|
|
close(fd);
|
2012-11-09 05:44:00 -06:00
|
|
|
DBG(dbgprint("[tiocgdev rc=%d]", rc));
|
2012-11-09 05:18:31 -06:00
|
|
|
return rc;
|
|
|
|
}
|
2013-04-17 10:23:26 -05:00
|
|
|
#endif /* TIOCGDEV */
|
2012-11-09 03:49:56 -06:00
|
|
|
#endif /* __linux__ */
|
|
|
|
|
2012-11-08 08:55:48 -06:00
|
|
|
/*
|
|
|
|
* Try to detect the real device(s) used for the system console
|
|
|
|
* /dev/console if but only if /dev/console is used. On Linux
|
|
|
|
* this can be more than one device, e.g. a serial line as well
|
|
|
|
* as a virtual console as well as a simple printer.
|
|
|
|
*
|
|
|
|
* Returns 1 if stdout and stderr should be reconnected and 0
|
2012-11-09 03:18:49 -06:00
|
|
|
* otherwise or less than zero on error.
|
2012-11-08 08:55:48 -06:00
|
|
|
*/
|
2017-04-15 15:46:38 -05:00
|
|
|
int detect_consoles(const char *device, const int fallback, struct list_head *consoles)
|
2012-11-08 08:55:48 -06:00
|
|
|
{
|
2012-11-09 03:30:14 -06:00
|
|
|
int fd, reconnect = 0, rc;
|
2012-11-08 10:22:45 -06:00
|
|
|
dev_t comparedev = 0;
|
2012-11-09 04:52:11 -06:00
|
|
|
|
2012-11-09 05:44:00 -06:00
|
|
|
consoles_debug = getenv("CONSOLES_DEBUG") ? 1 : 0;
|
|
|
|
|
2012-11-08 10:04:01 -06:00
|
|
|
if (!device || !*device)
|
2015-01-29 04:32:42 -06:00
|
|
|
fd = fallback >= 0 ? dup(fallback) : - 1;
|
2012-11-08 08:55:48 -06:00
|
|
|
else {
|
|
|
|
fd = open(device, O_RDWR|O_NONBLOCK|O_NOCTTY|O_CLOEXEC);
|
2012-11-09 03:30:14 -06:00
|
|
|
reconnect = 1;
|
2012-11-08 08:55:48 -06:00
|
|
|
}
|
|
|
|
|
2012-11-09 05:44:00 -06:00
|
|
|
DBG(dbgprint("detection started [device=%s, fallback=%d]",
|
|
|
|
device, fallback));
|
|
|
|
|
2012-11-08 08:55:48 -06:00
|
|
|
if (fd >= 0) {
|
|
|
|
DIR *dir;
|
|
|
|
char *name;
|
|
|
|
struct stat st;
|
|
|
|
#ifdef TIOCGDEV
|
|
|
|
unsigned int devnum;
|
2015-10-09 18:29:47 -05:00
|
|
|
#endif
|
|
|
|
#ifdef __GNU__
|
|
|
|
/*
|
|
|
|
* The Hurd always gives st_rdev as 0, which causes this
|
|
|
|
* method to select the first terminal it finds.
|
|
|
|
*/
|
|
|
|
close(fd);
|
|
|
|
goto fallback;
|
2012-11-08 08:55:48 -06:00
|
|
|
#endif
|
2012-11-09 05:44:00 -06:00
|
|
|
DBG(dbgprint("trying device/fallback file descriptor"));
|
2012-11-08 08:55:48 -06:00
|
|
|
|
|
|
|
if (fstat(fd, &st) < 0) {
|
|
|
|
close(fd);
|
|
|
|
goto fallback;
|
|
|
|
}
|
|
|
|
comparedev = st.st_rdev;
|
|
|
|
|
2012-11-09 03:30:14 -06:00
|
|
|
if (reconnect &&
|
|
|
|
(fstat(fallback, &st) < 0 || comparedev != st.st_rdev))
|
2012-11-08 08:55:48 -06:00
|
|
|
dup2(fd, fallback);
|
|
|
|
#ifdef __linux__
|
|
|
|
/*
|
|
|
|
* Check if the device detection for Linux system console should be used.
|
|
|
|
*/
|
|
|
|
if (comparedev == makedev(TTYAUX_MAJOR, 0)) { /* /dev/tty */
|
|
|
|
close(fd);
|
|
|
|
device = "/dev/tty";
|
|
|
|
goto fallback;
|
|
|
|
}
|
|
|
|
if (comparedev == makedev(TTYAUX_MAJOR, 1)) { /* /dev/console */
|
|
|
|
close(fd);
|
|
|
|
goto console;
|
|
|
|
}
|
|
|
|
if (comparedev == makedev(TTYAUX_MAJOR, 2)) { /* /dev/ptmx */
|
|
|
|
close(fd);
|
|
|
|
device = "/dev/tty";
|
|
|
|
goto fallback;
|
|
|
|
}
|
|
|
|
if (comparedev == makedev(TTY_MAJOR, 0)) { /* /dev/tty0 */
|
|
|
|
struct vt_stat vt;
|
|
|
|
if (ioctl(fd, VT_GETSTATE, &vt) < 0) {
|
|
|
|
close(fd);
|
|
|
|
goto fallback;
|
|
|
|
}
|
|
|
|
comparedev = makedev(TTY_MAJOR, (int)vt.v_active);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#ifdef TIOCGDEV
|
|
|
|
if (ioctl (fd, TIOCGDEV, &devnum) < 0) {
|
|
|
|
close(fd);
|
|
|
|
goto fallback;
|
|
|
|
}
|
|
|
|
comparedev = (dev_t)devnum;
|
|
|
|
#endif
|
|
|
|
close(fd);
|
|
|
|
dir = opendir("/dev");
|
|
|
|
if (!dir)
|
|
|
|
goto fallback;
|
2012-11-08 10:22:45 -06:00
|
|
|
name = scandev(dir, comparedev);
|
2012-11-09 05:59:33 -06:00
|
|
|
closedir(dir);
|
|
|
|
|
2012-11-09 03:18:49 -06:00
|
|
|
if (name) {
|
|
|
|
rc = append_console(consoles, name);
|
2012-12-18 08:02:35 -06:00
|
|
|
free(name);
|
2012-11-09 03:18:49 -06:00
|
|
|
if (rc < 0)
|
|
|
|
return rc;
|
|
|
|
}
|
2012-12-18 08:02:35 -06:00
|
|
|
if (list_empty(consoles))
|
2012-11-08 08:55:48 -06:00
|
|
|
goto fallback;
|
2012-11-09 05:59:33 -06:00
|
|
|
|
|
|
|
DBG(dbgprint("detection success [rc=%d]", reconnect));
|
2012-11-09 03:30:14 -06:00
|
|
|
return reconnect;
|
2012-11-08 08:55:48 -06:00
|
|
|
}
|
|
|
|
#ifdef __linux__
|
|
|
|
console:
|
|
|
|
/*
|
2016-05-29 16:11:53 -05:00
|
|
|
* Detection of devices used for Linux system console using
|
2012-11-08 08:55:48 -06:00
|
|
|
* the /proc/consoles API with kernel 2.6.38 and higher.
|
|
|
|
*/
|
2012-11-09 03:49:56 -06:00
|
|
|
rc = detect_consoles_from_proc(consoles);
|
|
|
|
if (rc == 0)
|
|
|
|
return reconnect; /* success */
|
|
|
|
if (rc < 0)
|
|
|
|
return rc; /* fatal error */
|
2012-11-08 08:55:48 -06:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Detection of devices used for Linux system console using
|
|
|
|
* the sysfs /sys/class/tty/ API with kernel 2.6.37 and higher.
|
|
|
|
*/
|
2012-11-09 04:17:23 -06:00
|
|
|
rc = detect_consoles_from_sysfs(consoles);
|
|
|
|
if (rc == 0)
|
|
|
|
return reconnect; /* success */
|
|
|
|
if (rc < 0)
|
|
|
|
return rc; /* fatal error */
|
2012-11-08 08:55:48 -06:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Detection of devices used for Linux system console using
|
|
|
|
* kernel parameter on the kernels command line.
|
|
|
|
*/
|
2012-11-09 04:52:11 -06:00
|
|
|
rc = detect_consoles_from_cmdline(consoles);
|
|
|
|
if (rc == 0)
|
|
|
|
return reconnect; /* success */
|
|
|
|
if (rc < 0)
|
|
|
|
return rc; /* fatal error */
|
2012-11-08 08:55:48 -06:00
|
|
|
|
2012-11-09 05:18:31 -06:00
|
|
|
/*
|
|
|
|
* Detection of the device used for Linux system console using
|
|
|
|
* the ioctl TIOCGDEV if available (e.g. official 2.6.38).
|
|
|
|
*/
|
2013-04-17 10:23:26 -05:00
|
|
|
#ifdef TIOCGDEV
|
2012-11-09 05:18:31 -06:00
|
|
|
rc = detect_consoles_from_tiocgdev(consoles, fallback, device);
|
|
|
|
if (rc == 0)
|
|
|
|
return reconnect; /* success */
|
|
|
|
if (rc < 0)
|
|
|
|
return rc; /* fatal error */
|
2013-04-17 10:23:26 -05:00
|
|
|
#endif
|
2012-12-18 08:02:35 -06:00
|
|
|
if (!list_empty(consoles)) {
|
2012-11-09 05:52:33 -06:00
|
|
|
DBG(dbgprint("detection success [rc=%d]", reconnect));
|
|
|
|
return reconnect;
|
|
|
|
}
|
2012-11-09 05:44:00 -06:00
|
|
|
|
2012-11-08 08:55:48 -06:00
|
|
|
#endif /* __linux __ */
|
2012-11-09 05:18:31 -06:00
|
|
|
|
2012-11-08 08:55:48 -06:00
|
|
|
fallback:
|
|
|
|
if (fallback >= 0) {
|
2013-07-09 07:32:05 -05:00
|
|
|
const char *name;
|
|
|
|
char *n;
|
2012-12-18 08:02:35 -06:00
|
|
|
struct console *console;
|
2012-11-08 10:04:01 -06:00
|
|
|
|
2012-11-08 08:55:48 -06:00
|
|
|
if (device && *device != '\0')
|
|
|
|
name = device;
|
|
|
|
else name = ttyname(fallback);
|
|
|
|
|
|
|
|
if (!name)
|
|
|
|
name = "/dev/tty";
|
|
|
|
|
2013-07-07 14:54:43 -05:00
|
|
|
n = strdup(name);
|
|
|
|
if (!n)
|
|
|
|
return -ENOMEM;
|
|
|
|
rc = append_console(consoles, n);
|
|
|
|
free(n);
|
2012-11-09 03:18:49 -06:00
|
|
|
if (rc < 0)
|
|
|
|
return rc;
|
2012-12-18 08:02:35 -06:00
|
|
|
if (list_empty(consoles))
|
|
|
|
return 1;
|
|
|
|
console = list_last_entry(consoles, struct console, entry);
|
|
|
|
if (console)
|
|
|
|
console->fd = fallback;
|
2012-11-08 08:55:48 -06:00
|
|
|
}
|
2012-11-09 05:44:00 -06:00
|
|
|
|
|
|
|
DBG(dbgprint("detection done by fallback [rc=%d]", reconnect));
|
2012-11-09 03:30:14 -06:00
|
|
|
return reconnect;
|
2012-11-08 08:55:48 -06:00
|
|
|
}
|
|
|
|
|
2012-11-09 03:12:35 -06:00
|
|
|
|
|
|
|
#ifdef TEST_PROGRAM
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
char *name = NULL;
|
|
|
|
int fd, re;
|
2017-01-05 04:54:59 -06:00
|
|
|
struct list_head *p, consoles;
|
2012-11-09 03:12:35 -06:00
|
|
|
|
|
|
|
if (argc == 2) {
|
|
|
|
name = argv[1];
|
|
|
|
fd = open(name, O_RDWR);
|
|
|
|
} else {
|
|
|
|
name = ttyname(STDIN_FILENO);
|
|
|
|
fd = STDIN_FILENO;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!name)
|
|
|
|
errx(EXIT_FAILURE, "usage: %s [<tty>]\n", program_invocation_short_name);
|
|
|
|
|
2017-01-05 04:54:59 -06:00
|
|
|
INIT_LIST_HEAD(&consoles);
|
2012-11-09 03:12:35 -06:00
|
|
|
re = detect_consoles(name, fd, &consoles);
|
|
|
|
|
2012-12-18 08:02:35 -06:00
|
|
|
list_for_each(p, &consoles) {
|
|
|
|
struct console *c = list_entry(p, struct console, entry);
|
|
|
|
printf("%s: id=%d %s\n", c->tty, c->id, re ? "(reconnect) " : "");
|
|
|
|
}
|
2012-11-09 03:12:35 -06:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|