From bf0946fcf4ee4618d54d2f07c4ac1e3afe6b4f87 Mon Sep 17 00:00:00 2001 From: mpf Date: Tue, 13 Jan 2015 10:07:58 +0000 Subject: [PATCH] Add dmesg -s support, to view the output of rc(8) system startup messages. Help and feedback by Theo and Miod. OK deraadt@, manpage-ok jmc@ --- sbin/dmesg/dmesg.8 | 11 +++++++++-- sbin/dmesg/dmesg.c | 14 +++++++++----- sys/kern/kern_sysctl.c | 25 ++++++++++++++++++------- sys/kern/subr_log.c | 20 +++++++++++++------- sys/kern/subr_prf.c | 4 ++-- sys/kern/tty.c | 15 ++++++++++++++- sys/sys/msgbuf.h | 6 ++++-- sys/sys/sysctl.h | 6 ++++-- 8 files changed, 73 insertions(+), 28 deletions(-) diff --git a/sbin/dmesg/dmesg.8 b/sbin/dmesg/dmesg.8 index a85c8b31edd..2ece4d3a7d4 100644 --- a/sbin/dmesg/dmesg.8 +++ b/sbin/dmesg/dmesg.8 @@ -1,4 +1,4 @@ -.\" $OpenBSD: dmesg.8,v 1.14 2013/08/14 06:32:35 jmc Exp $ +.\" $OpenBSD: dmesg.8,v 1.15 2015/01/13 10:07:58 mpf Exp $ .\" $NetBSD: dmesg.8,v 1.9 1995/03/18 14:54:47 cgd Exp $ .\" .\" Copyright (c) 1980, 1991, 1993 @@ -30,7 +30,7 @@ .\" .\" @(#)dmesg.8 8.1 (Berkeley) 6/5/93 .\" -.Dd $Mdocdate: August 14 2013 $ +.Dd $Mdocdate: January 13 2015 $ .Dt DMESG 8 .Os .Sh NAME @@ -38,6 +38,7 @@ .Nd display the system message buffer .Sh SYNOPSIS .Nm dmesg +.Op Fl s .Op Fl M Ar core .Op Fl N Ar system .Sh DESCRIPTION @@ -57,6 +58,12 @@ Extract the name list from the specified .Ar system instead of the default .Pa /bsd . +.It Fl s +Display the contents of the console message buffer instead. +This can be used to review +.Xr rc 8 +system startup messages. +This option is limited to the superuser. .El .Sh FILES .Bl -tag -width /var/run/dmesg.boot -compact diff --git a/sbin/dmesg/dmesg.c b/sbin/dmesg/dmesg.c index 35fe2d81ef4..a83c9fe81a7 100644 --- a/sbin/dmesg/dmesg.c +++ b/sbin/dmesg/dmesg.c @@ -1,4 +1,4 @@ -/* $OpenBSD: dmesg.c,v 1.23 2014/04/22 20:43:12 tedu Exp $ */ +/* $OpenBSD: dmesg.c,v 1.24 2015/01/13 10:07:58 mpf Exp $ */ /* $NetBSD: dmesg.c,v 1.8 1995/03/18 14:54:49 cgd Exp $ */ /*- @@ -66,11 +66,15 @@ main(int argc, char *argv[]) char *p; struct msgbuf cur; char *memf, *nlistf, *bufdata = NULL; + int startupmsgs = 0; char buf[5]; memf = nlistf = NULL; - while ((ch = getopt(argc, argv, "M:N:")) != -1) + while ((ch = getopt(argc, argv, "sM:N:")) != -1) switch(ch) { + case 's': + startupmsgs = 1; + break; case 'M': memf = optarg; break; @@ -89,7 +93,7 @@ main(int argc, char *argv[]) size_t len; mib[0] = CTL_KERN; - mib[1] = KERN_MSGBUFSIZE; + mib[1] = startupmsgs ? KERN_CONSBUFSIZE : KERN_MSGBUFSIZE; len = sizeof(msgbufsize); if (sysctl(mib, 2, &msgbufsize, &len, NULL, 0)) err(1, "sysctl: KERN_MSGBUFSIZE"); @@ -99,7 +103,7 @@ main(int argc, char *argv[]) if (bufdata == NULL) errx(1, "couldn't allocate space for buffer data"); - mib[1] = KERN_MSGBUF; + mib[1] = startupmsgs ? KERN_CONSBUF : KERN_MSGBUF; len = msgbufsize; if (sysctl(mib, 2, bufdata, &len, NULL, 0)) err(1, "sysctl: KERN_MSGBUF"); @@ -179,6 +183,6 @@ usage(void) { extern char *__progname; - fprintf(stderr, "usage: %s [-M core] [-N system]\n", __progname); + fprintf(stderr, "usage: %s [-s] [-M core] [-N system]\n", __progname); exit(1); } diff --git a/sys/kern/kern_sysctl.c b/sys/kern/kern_sysctl.c index a3f807dc5be..04e08d21fbe 100644 --- a/sys/kern/kern_sysctl.c +++ b/sys/kern/kern_sysctl.c @@ -1,4 +1,4 @@ -/* $OpenBSD: kern_sysctl.c,v 1.277 2014/12/12 07:45:46 tedu Exp $ */ +/* $OpenBSD: kern_sysctl.c,v 1.278 2015/01/13 10:07:58 mpf Exp $ */ /* $NetBSD: kern_sysctl.c,v 1.17 1996/05/20 17:49:05 mrg Exp $ */ /*- @@ -444,19 +444,30 @@ kern_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp, void *newp, return (sysctl_rdint(oldp, oldlenp, newp, 0)); #endif case KERN_MSGBUFSIZE: + case KERN_CONSBUFSIZE: { + struct msgbuf *mp; + mp = (name[0] == KERN_MSGBUFSIZE) ? msgbufp : consbufp; /* * deal with cases where the message buffer has * become corrupted. */ - if (!msgbufp || msgbufp->msg_magic != MSG_MAGIC) + if (!mp || mp->msg_magic != MSG_MAGIC) return (ENXIO); - return (sysctl_rdint(oldp, oldlenp, newp, msgbufp->msg_bufs)); - case KERN_MSGBUF: + return (sysctl_rdint(oldp, oldlenp, newp, mp->msg_bufs)); + } + case KERN_CONSBUF: + if ((error = suser(p, 0))) + return (error); + /* FALLTHROUGH */ + case KERN_MSGBUF: { + struct msgbuf *mp; + mp = (name[0] == KERN_MSGBUF) ? msgbufp : consbufp; /* see note above */ - if (!msgbufp || msgbufp->msg_magic != MSG_MAGIC) + if (!mp || mp->msg_magic != MSG_MAGIC) return (ENXIO); - return (sysctl_rdstruct(oldp, oldlenp, newp, msgbufp, - msgbufp->msg_bufs + offsetof(struct msgbuf, msg_bufc))); + return (sysctl_rdstruct(oldp, oldlenp, newp, mp, + mp->msg_bufs + offsetof(struct msgbuf, msg_bufc))); + } case KERN_MALLOCSTATS: return (sysctl_malloc(name + 1, namelen - 1, oldp, oldlenp, newp, newlen, p)); diff --git a/sys/kern/subr_log.c b/sys/kern/subr_log.c index ec1e2ebb850..6720d5ede34 100644 --- a/sys/kern/subr_log.c +++ b/sys/kern/subr_log.c @@ -1,4 +1,4 @@ -/* $OpenBSD: subr_log.c,v 1.25 2014/12/13 21:05:33 doug Exp $ */ +/* $OpenBSD: subr_log.c,v 1.26 2015/01/13 10:07:58 mpf Exp $ */ /* $NetBSD: subr_log.c,v 1.11 1996/03/30 22:24:44 christos Exp $ */ /* @@ -74,8 +74,8 @@ struct logsoftc { int log_open; /* also used in log() */ int msgbufmapped; /* is the message buffer mapped */ -int msgbufenabled; /* is logging to the buffer enabled */ struct msgbuf *msgbufp; /* the mapped buffer, itself. */ +struct msgbuf *consbufp; /* console message buffer. */ struct file *syslogf; void filt_logrdetach(struct knote *kn); @@ -113,17 +113,23 @@ initmsgbuf(caddr_t buf, size_t bufsize) /* Always start new buffer data on a new line. */ if (mbp->msg_bufx > 0 && mbp->msg_bufc[mbp->msg_bufx - 1] != '\n') - msgbuf_putchar('\n'); + msgbuf_putchar(msgbufp, '\n'); /* mark it as ready for use. */ - msgbufmapped = msgbufenabled = 1; + msgbufmapped = 1; + + /* Set up a buffer to collect /dev/console output */ + consbufp = malloc(CONSBUFSIZE, M_TEMP, M_NOWAIT|M_ZERO); + if (consbufp) { + new_bufs = CONSBUFSIZE - offsetof(struct msgbuf, msg_bufc); + consbufp->msg_magic = MSG_MAGIC; + consbufp->msg_bufs = new_bufs; + } } void -msgbuf_putchar(const char c) +msgbuf_putchar(struct msgbuf *mbp, const char c) { - struct msgbuf *mbp = msgbufp; - if (mbp->msg_magic != MSG_MAGIC) /* Nothing we can do */ return; diff --git a/sys/kern/subr_prf.c b/sys/kern/subr_prf.c index 899fcb529d5..06f2cc60161 100644 --- a/sys/kern/subr_prf.c +++ b/sys/kern/subr_prf.c @@ -1,4 +1,4 @@ -/* $OpenBSD: subr_prf.c,v 1.83 2014/07/13 23:49:40 uebayasi Exp $ */ +/* $OpenBSD: subr_prf.c,v 1.84 2015/01/13 10:07:58 mpf Exp $ */ /* $NetBSD: subr_prf.c,v 1.45 1997/10/24 18:14:25 chuck Exp $ */ /*- @@ -349,7 +349,7 @@ kputchar(int c, int flags, struct tty *tp) constty = NULL; if ((flags & TOLOG) && c != '\0' && c != '\r' && c != 0177 && msgbufmapped) - msgbuf_putchar(c); + msgbuf_putchar(msgbufp, c); if ((flags & TOCONS) && (constty == NULL || ddb_active) && c != '\0') (*v_putc)(c); #ifdef DDB diff --git a/sys/kern/tty.c b/sys/kern/tty.c index c9b8cc6adbe..419f6e65d90 100644 --- a/sys/kern/tty.c +++ b/sys/kern/tty.c @@ -1,4 +1,4 @@ -/* $OpenBSD: tty.c,v 1.119 2014/12/17 19:42:15 tedu Exp $ */ +/* $OpenBSD: tty.c,v 1.120 2015/01/13 10:07:58 mpf Exp $ */ /* $NetBSD: tty.c,v 1.68.4.2 1996/06/06 16:04:52 thorpej Exp $ */ /*- @@ -52,6 +52,7 @@ #include #include #include +#include #include #include #include @@ -63,6 +64,7 @@ #include +#include #include #include "pty.h" @@ -1786,6 +1788,17 @@ loop: } if (cc > obufcc) obufcc = cc; + + /* duplicate /dev/console output into console buffer */ + if (consbufp && cn_tab && + cn_tab->cn_dev == tp->t_dev && tp->t_gen == 0) { + int i; + for (i = 0; i < cc; i++) { + char c = cp[i]; + if (c != '\0' && c != '\r' && c != 0177) + msgbuf_putchar(consbufp, c); + } + } } /* * If nothing fancy need be done, grab those characters we diff --git a/sys/sys/msgbuf.h b/sys/sys/msgbuf.h index bce761e3036..8ca1511b061 100644 --- a/sys/sys/msgbuf.h +++ b/sys/sys/msgbuf.h @@ -1,4 +1,4 @@ -/* $OpenBSD: msgbuf.h,v 1.8 2005/04/14 21:58:50 krw Exp $ */ +/* $OpenBSD: msgbuf.h,v 1.9 2015/01/13 10:07:58 mpf Exp $ */ /* $NetBSD: msgbuf.h,v 1.8 1995/03/26 20:24:27 jtc Exp $ */ /* @@ -42,8 +42,10 @@ struct msgbuf { char msg_bufc[1]; /* buffer */ }; #ifdef _KERNEL +#define CONSBUFSIZE (16 * 1024) /* console message buffer size */ extern struct msgbuf *msgbufp; +extern struct msgbuf *consbufp; void initmsgbuf(caddr_t buf, size_t bufsize); -void msgbuf_putchar(const char c); +void msgbuf_putchar(struct msgbuf *, const char c); #endif diff --git a/sys/sys/sysctl.h b/sys/sys/sysctl.h index 58d53eb1f8c..a8004cdedea 100644 --- a/sys/sys/sysctl.h +++ b/sys/sys/sysctl.h @@ -1,4 +1,4 @@ -/* $OpenBSD: sysctl.h,v 1.153 2014/12/12 07:45:46 tedu Exp $ */ +/* $OpenBSD: sysctl.h,v 1.154 2015/01/13 10:07:58 mpf Exp $ */ /* $NetBSD: sysctl.h,v 1.16 1996/04/09 20:55:36 cgd Exp $ */ /* @@ -182,7 +182,9 @@ struct ctlname { #define KERN_PROC_NOBROADCASTKILL 79 /* node: proc no broadcast kill */ #define KERN_PROC_VMMAP 80 /* node: proc vmmap */ #define KERN_GLOBAL_PTRACE 81 /* allow ptrace globally */ -#define KERN_MAXID 82 /* number of valid kern ids */ +#define KERN_CONSBUFSIZE 82 /* int: console message buffer size */ +#define KERN_CONSBUF 83 /* console message buffer */ +#define KERN_MAXID 84 /* number of valid kern ids */ #define CTL_KERN_NAMES { \ { 0, 0 }, \ -- 2.20.1