- int fd;
- int tried = 0;
- struct vt_stat vt;
- struct serial_struct sr;
- char *s;
-
- if ((s = getenv("CONSOLE")) != NULL || (s = getenv("console")) != NULL) {
- safe_strncpy(console, s, sizeof(console));
-#if #cpu(sparc)
- /* sparc kernel supports console=tty[ab] parameter which is also
- * passed to init, so catch it here */
- /* remap tty[ab] to /dev/ttyS[01] */
- if (strcmp(s, "ttya") == 0)
- safe_strncpy(console, SC_0, sizeof(console));
- else if (strcmp(s, "ttyb") == 0)
- safe_strncpy(console, SC_1, sizeof(console));
-#endif
- } else {
- /* 2.2 kernels: identify the real console backend and try to use it */
- if (ioctl(0, TIOCGSERIAL, &sr) == 0) {
- /* this is a serial console */
- snprintf(console, sizeof(console) - 1, SC_FORMAT, sr.line);
- } else if (ioctl(0, VT_GETSTATE, &vt) == 0) {
- /* this is linux virtual tty */
- snprintf(console, sizeof(console) - 1, VC_FORMAT, vt.v_active);
- } else {
- safe_strncpy(console, _PATH_CONSOLE, sizeof(console));
- tried++;
- }
- }
-
- while ((fd = open(console, O_RDONLY | O_NONBLOCK)) < 0 && tried < 2) {
- /* Can't open selected console -- try
- logical system console and VT_MASTER */
- safe_strncpy(console, (tried == 0 ? _PATH_CONSOLE : CURRENT_VC),
- sizeof(console));
- tried++;
- }
- if (fd < 0) {
- /* Perhaps we should panic here? */
-#ifndef CONFIG_SYSLOGD
- log =
-#endif
- safe_strncpy(console, "/dev/null", sizeof(console));
- } else {
- s = getenv("TERM");
- /* check for serial console */
- if (ioctl(fd, TIOCGSERIAL, &sr) == 0) {
- /* Force the TERM setting to vt102 for serial console --
- * if TERM is set to linux (the default) */
- if (s == NULL || strcmp(s, "linux") == 0)
- putenv("TERM=vt102");
-#ifndef CONFIG_SYSLOGD
- log = console;
-#endif
- } else {
- if (s == NULL)
- putenv("TERM=linux");
+ /* empty tty_name means "use init's tty", else... */
+ if (tty_name[0]) {
+ int fd;
+ close(0);
+ /* fd can be only < 0 or 0: */
+ fd = device_open(tty_name, O_RDWR);
+ if (fd) {
+ message(L_LOG | L_CONSOLE, "Can't open %s: %s",
+ tty_name, strerror(errno));
+ if (exit_on_failure)
+ _exit(1);
+ if (ENABLE_DEBUG_INIT)
+ _exit(2);
+ halt_reboot_pwoff(SIGUSR1); /* halt the system */