--- blogd.8 +++ blogd.8 2011-04-19 11:59:52.704425678 +0000 @@ -65,7 +65,7 @@ says that it should stop writing to disk but continue to repeat messages to the old devices of the system console. -\." +.\" .SH BUGS .B blogd needs a mounted --- blogd.c +++ blogd.c 2011-04-19 11:59:52.736425469 +0000 @@ -41,6 +41,7 @@ # define _POSIX_MAX_CANON 255 #endif extern volatile sig_atomic_t nsigsys; +extern volatile sig_atomic_t signaled; static const char console[] = "/dev/console"; @@ -66,7 +67,7 @@ void error (const char *fmt, ...) _logger(fmt, ap); va_end(ap); popd(); - exit (1); + exit(EXIT_FAILURE); } /* @@ -122,7 +123,6 @@ static struct sigaction saved_sigint; static struct sigaction saved_sigquit; static struct sigaction saved_sigterm; static struct sigaction saved_sigsys; -static volatile sig_atomic_t signaled = 0; static void sighandle(int sig) { @@ -336,7 +336,7 @@ int main(int argc, char *argv[]) dup2(0, 2); secondtty(cons, st.st_rdev); -exit(0); + (void)ioctl(0, TIOCCONS, NULL); /* Undo any current map if any */ close(0); @@ -351,22 +351,34 @@ exit(0); #endif c->max_canon = _POSIX_MAX_CANON; c->tlock = 0; - if (tcgetattr(cons->fd, &cons->otio) < 0) + if (tcgetattr(c->fd, &c->otio) < 0) continue; c->tlock = 1; -#if 1 + iflag = c->otio.c_iflag; oflag = c->otio.c_oflag; - c->otio.c_iflag |= (ICRNL | IGNBRK); - c->otio.c_iflag &= ~(INLCR | IGNCR | BRKINT); - c->otio.c_oflag |= (ONLCR | OPOST); - c->otio.c_oflag &= ~(OCRNL | ONLRET); - (void)tcsetattr(cons->fd, TCSADRAIN, &cons->otio); + if (ioctl(c->fd, TIOCMGET, &flags) == 0) { + ispeed = cfgetispeed(&c->otio); + ospeed = cfgetospeed(&c->otio); + + c->otio.c_iflag = c->otio.c_lflag = 0; + c->otio.c_oflag = (ONLCR | OPOST); + c->otio.c_cflag = CREAD | CS8 | HUPCL | (c->otio.c_cflag & CLOCAL); + + cfsetispeed(&c->otio, ispeed); + cfsetospeed(&c->otio, ospeed); + } else { + c->otio.c_iflag |= (ICRNL | IGNBRK); + c->otio.c_iflag &= ~(INLCR | IGNCR | BRKINT); + c->otio.c_oflag |= (ONLCR | OPOST); + c->otio.c_oflag &= ~(OCRNL | ONLRET); + } + (void)tcsetattr(c->fd, TCSADRAIN, &c->otio); c->otio.c_iflag = iflag; c->otio.c_oflag = oflag; -#endif + if ((c->otio.c_lflag & ICANON) == 0) { c->otio.c_lflag |= ICANON | IEXTEN | ISIG | ECHO|ECHOE|ECHOK|ECHOKE; c->otio.c_oflag |= OPOST; @@ -384,7 +396,7 @@ exit(0); } } - nsigsys = 0; + signaled = nsigsys = 0; set_signal(SIGTTIN, &saved_sigttin, SIG_IGN); set_signal(SIGTTOU, &saved_sigttou, SIG_IGN); set_signal(SIGTSTP, &saved_sigtstp, SIG_IGN); @@ -427,7 +439,7 @@ exit(0); } fprintf(stdout, "\rBoot logging started on %s(%s) at %.24s\n", tty, console, stt); fflush(stdout); - _exit(0); /* NEVER rise exit handlers here */ + _exit(EXIT_SUCCESS); /* NEVER rise exit handlers here */ } atexit(flush_handler); /* Register flush exit handler */ @@ -436,7 +448,7 @@ exit(0); while (!signaled) safeIO(); - exit(0); /* Raise exit handlers */ + exit(EXIT_SUCCESS); /* Raise exit handlers */ } static void flush_handler (void) --- isserial.8 +++ isserial.8 2011-04-19 11:59:52.772425964 +0000 @@ -32,7 +32,7 @@ is if .B stdin is a serial line otherwise not. -\." +.\" .SH FILES .TP .I /dev/console --- libconsole.c +++ libconsole.c 2011-04-19 11:59:52.812425484 +0000 @@ -59,6 +59,22 @@ #include "listing.h" /* + * Remember if we're signaled. + */ +volatile sig_atomic_t signaled; + +/* + * Error raised in exit handler should not call exit(3) its self + */ +#define lerror(fmt, args...) \ + do { \ + if (signaled) { \ + warn(fmt, args); \ + goto out; \ + } error(fmt, args); \ + } while (0) + +/* * push and popd direcotry changes */ @@ -182,7 +198,9 @@ static inline void safeout (int fd, cons if (p < 0) { if (errno == EPIPE) { warn("error on writing to fd %d: %m\n", fd); - exit (0); + if (signaled) + goto out; + exit(EXIT_SUCCESS); } if (errno == EINTR) { errno = 0; @@ -192,8 +210,9 @@ static inline void safeout (int fd, cons int ret; fd_set check; - if (repeated++ > 1000) - error("repeated error on writing to fd %d: %m\n", fd); + if (repeated++ > 1000) { + lerror("repeated error on writing to fd %d: %m\n", fd); + } FD_ZERO (&check); FD_SET (fd, &check); @@ -206,25 +225,28 @@ static inline void safeout (int fd, cons } while ((ret < 0) && (errno == EINTR)); - if (ret < 0) - error("can not write to fd %d: %m\n", fd); + if (ret < 0) { + lerror("can not write to fd %d: %m\n", fd); + } errno = 0; continue; } if (errno == EIO) { - if ((eiocount++ > 10) || !vc_reconnect) - error("can not write to fd %d: %m\n", fd); + if ((eiocount++ > 10) || !vc_reconnect) { + lerror("can not write to fd %d: %m\n", fd); + } (*vc_reconnect)(fd); errno = 0; continue; } - error("can not write to fd %d: %m\n", fd); + lerror("can not write to fd %d: %m\n", fd); } repeated = 0; ptr += p; s -= p; } +out: errno = saveerr; } @@ -260,11 +282,11 @@ static inline ssize_t safein (int fd, ch /* Do not exit on a broken FIFO */ if (r < 0 && errno != EPIPE) { - if (noerr) + if (noerr || signaled) goto out; if (fd == 0 && errno == EIO) warn("\e[31m\e[1msystem console stolen at line %d!\e[m\n", __LINE__); - error("Can not read from fd %d: %m\n", fd); + lerror("Can not read from fd %d: %m\n", fd); } goto out; @@ -277,17 +299,18 @@ static inline ssize_t safein (int fd, ch while (t > 0) { ssize_t p = read (fd, ptr, t); if (p < 0) { - if (repeated++ > 1000) - error("Repeated error on reading from fd %d: %m\n", fd); + if (repeated++ > 1000) { + lerror("Repeated error on reading from fd %d: %m\n", fd); + } if (errno == EINTR || errno == EAGAIN) { errno = 0; continue; } - if (noerr) + if (noerr || signaled) goto out; if (fd == 0 && errno == EIO) warn("\e[31m\e[1msystem console stolen at line %d!\e[m\n", __LINE__); - error("Can not read from fd %d: %m\n", fd); + lerror("Can not read from fd %d: %m\n", fd); } repeated = 0; ptr += p; @@ -302,7 +325,7 @@ out: /* * The stdio file pointer for our log file */ -struct console *restrict cons; +struct console *cons; static FILE * flog = NULL; static int fdread = -1; static int fdfifo = -1; @@ -310,7 +333,7 @@ static int fdfifo = -1; /* * Signal control for writing on log file */ -volatile sig_atomic_t nsigsys = 0; +volatile sig_atomic_t nsigsys; static volatile sig_atomic_t nsigio = -1; static sigset_t save_oldset; @@ -402,7 +425,7 @@ xout: static inline void writelog(void) { if (!flog) - goto xout; + return; clearerr(flog); lock(&llock); while (avail > 0) { @@ -412,7 +435,7 @@ static inline void writelog(void) ret = TRANS_BUFFER_SIZE; if (!flog) - goto xout;; + break; ret = fwrite(head, sizeof(unsigned char), ret, flog); if (!ret && ferror(flog)) break; @@ -429,12 +452,10 @@ static inline void writelog(void) } } unlock(&llock); - if (!flog) - goto xout;; - fflush(flog); - fdatasync(fileno(flog)); -xout: - return; + if (flog) { + fflush(flog); + fdatasync(fileno(flog)); + } } static inline void flushlog(void) @@ -808,7 +829,7 @@ static void more_input (struct timeval * if (nfds < 0) { timeout->tv_sec = 0; timeout->tv_usec = 0; - if (errno != EINTR) + if (errno != EINTR && !signaled) error ("select(): %m\n"); goto nothing; } @@ -998,20 +1019,14 @@ void closeIO(void) pthread_cancel(lthread); } - if (!flog) - goto xout; - - writelog(); - - if (!nl) - fputc('\n', flog); - - if (!flog) - goto xout; - - (void)fclose(flog); + if (flog) { + writelog(); + if (!nl) + fputc('\n', flog); + (void)fclose(flog); + } flog = NULL; -xout: + if (fdfifo >= 0) { close(fdfifo); fdfifo = -1; @@ -1101,7 +1116,7 @@ static dev_t fallback(const pid_t pid, c printf("|%u|%u|", tty, ttypgrp); /* stdout to pipe synchronize ... */ - exit(0); + exit(EXIT_SUCCESS); } break; case -1: error("can not execute: %m\n"); @@ -1463,6 +1478,10 @@ static void consalloc(struct console *re return; } tail->next = newc; + tail->tlock = 0; + tail->max_canon = _POSIX_MAX_CANON; + memset(&tail->ltio, 0, sizeof(tail->ltio)); + memset(&tail->otio, 0, sizeof(tail->otio)); tail = tail->next; if ((flags = fcntl(newc->fd, F_GETFL)) < 0) --- libconsole.h +++ libconsole.h 2011-03-31 14:47:22.755926508 +0000 @@ -39,7 +39,7 @@ struct console { int fd, tlock; ssize_t max_canon; struct termios ltio, otio; - struct console *restrict next; + struct console * next; }; extern void error (const char *fmt, ...) attribute((__noreturn__, __format__(__printf__, 1, 2))); extern void warn (const char *fmt, ...) attribute((__format__(__printf__, 1, 2))); @@ -50,6 +50,6 @@ extern void secondtty(struct console *re extern void prepareIO(void (*rfunc)(int), void (*pfunc)(void), const pid_t, const int); extern void safeIO (void); extern void closeIO(void); -extern struct console *restrict cons; +extern struct console *cons; #define alignof(type) ((sizeof(type)+(sizeof(void*)-1)) & ~(sizeof(void*)-1)) #define strsize(string) ((strlen(string)+1)*sizeof(char)) --- showconsole.8 +++ showconsole.8 2011-04-19 11:59:52.844425538 +0000 @@ -41,7 +41,7 @@ Without any argument or the option the .B setconsole undo any redirection. -\." +.\" .SH OPTIONS .TP .B \-n @@ -50,7 +50,7 @@ the device file name. This can be used kernel for the major and minor device numbers of a not existing device file in .IR /dev . -\." +.\" .SH BUGS .B showconsole needs a mounted