summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorchristos <christos@NetBSD.org>2018-03-31 23:12:01 +0000
committerchristos <christos@NetBSD.org>2018-03-31 23:12:01 +0000
commitec462995c03cdebd26f79e319ecfe5b86cdd0796 (patch)
tree6c826c11c4be964d537187c6d26b2297fdfb4a16
parentf37b296df7bc2fc583b1e54264ae86ed2c74f211 (diff)
factor out some repeated code and simplify the logputchar function.
-rw-r--r--sys/kern/subr_log.c97
-rw-r--r--sys/kern/subr_prf.c8
-rw-r--r--sys/miscfs/kernfs/kernfs_vnops.c9
-rw-r--r--sys/sys/msgbuf.h8
4 files changed, 68 insertions, 54 deletions
diff --git a/sys/kern/subr_log.c b/sys/kern/subr_log.c
index fd84dd93210..5e0077d63ec 100644
--- a/sys/kern/subr_log.c
+++ b/sys/kern/subr_log.c
@@ -1,4 +1,4 @@
-/* $NetBSD: subr_log.c,v 1.56 2017/10/25 08:12:39 maya Exp $ */
+/* $NetBSD: subr_log.c,v 1.57 2018/03/31 23:12:01 christos Exp $ */
/*-
* Copyright (c) 2007, 2008 The NetBSD Foundation, Inc.
@@ -65,7 +65,7 @@
*/
#include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: subr_log.c,v 1.56 2017/10/25 08:12:39 maya Exp $");
+__KERNEL_RCSID(0, "$NetBSD: subr_log.c,v 1.57 2018/03/31 23:12:01 christos Exp $");
#include <sys/param.h>
#include <sys/systm.h>
@@ -382,6 +382,28 @@ logioctl(dev_t dev, u_long com, void *data, int flag, struct lwp *lwp)
return (0);
}
+static void
+logskip(struct kern_msgbuf *mbp)
+{
+ /*
+ * Move forward read pointer to the next line
+ * in the buffer. Note that the buffer is
+ * a ring buffer so we should reset msg_bufr
+ * to 0 when msg_bufr exceeds msg_bufs.
+ *
+ * To prevent to loop forever, give up if we
+ * cannot find a newline in mbp->msg_bufs
+ * characters (the max size of the buffer).
+ */
+ for (int i = 0; i < mbp->msg_bufs; i++) {
+ char c0 = mbp->msg_bufc[mbp->msg_bufr];
+ if (++mbp->msg_bufr >= mbp->msg_bufs)
+ mbp->msg_bufr = 0;
+ if (c0 == '\n')
+ break;
+ }
+}
+
void
logputchar(int c)
{
@@ -389,48 +411,35 @@ logputchar(int c)
if (!cold)
mutex_spin_enter(&log_lock);
- if (msgbufenabled) {
- mbp = msgbufp;
- if (mbp->msg_magic != MSG_MAGIC) {
- /*
- * Arguably should panic or somehow notify the
- * user... but how? Panic may be too drastic,
- * and would obliterate the message being kicked
- * out (maybe a panic itself), and printf
- * would invoke us recursively. Silently punt
- * for now. If syslog is running, it should
- * notice.
- */
- msgbufenabled = 0;
- } else {
- mbp->msg_bufc[mbp->msg_bufx++] = c;
- if (mbp->msg_bufx < 0 || mbp->msg_bufx >= mbp->msg_bufs)
- mbp->msg_bufx = 0;
- /* If the buffer is full, keep the most recent data. */
- if (mbp->msg_bufr == mbp->msg_bufx) {
- char c0;
- int i;
-
- /*
- * Move forward read pointer to the next line
- * in the buffer. Note that the buffer is
- * a ring buffer so we should reset msg_bufr
- * to 0 when msg_bufr exceeds msg_bufs.
- *
- * To prevent to loop forever, give up if we
- * cannot find a newline in mbp->msg_bufs
- * characters (the max size of the buffer).
- */
- for (i = 0; i < mbp->msg_bufs; i++) {
- c0 = mbp->msg_bufc[mbp->msg_bufr];
- if (++mbp->msg_bufr >= mbp->msg_bufs)
- mbp->msg_bufr = 0;
- if (c0 == '\n')
- break;
- }
- }
- }
+
+ if (!msgbufenabled)
+ goto out;
+
+ mbp = msgbufp;
+ if (mbp->msg_magic != MSG_MAGIC) {
+ /*
+ * Arguably should panic or somehow notify the
+ * user... but how? Panic may be too drastic,
+ * and would obliterate the message being kicked
+ * out (maybe a panic itself), and printf
+ * would invoke us recursively. Silently punt
+ * for now. If syslog is running, it should
+ * notice.
+ */
+ msgbufenabled = 0;
+ goto out;
+
}
+
+ mbp->msg_bufc[mbp->msg_bufx++] = c;
+ if (mbp->msg_bufx < 0 || mbp->msg_bufx >= mbp->msg_bufs)
+ mbp->msg_bufx = 0;
+
+ /* If the buffer is full, keep the most recent data. */
+ if (mbp->msg_bufr == mbp->msg_bufx)
+ logskip(mbp);
+
+out:
if (!cold)
mutex_spin_exit(&log_lock);
}
@@ -449,7 +458,7 @@ sysctl_msgbuf(SYSCTLFN_ARGS)
extern kmutex_t log_lock;
int error;
- if (!msgbufenabled || msgbufp->msg_magic != MSG_MAGIC) {
+ if (!logenabled(msgbufp)) {
msgbufenabled = 0;
return (ENXIO);
}
diff --git a/sys/kern/subr_prf.c b/sys/kern/subr_prf.c
index 873d2095da1..5147ae18a20 100644
--- a/sys/kern/subr_prf.c
+++ b/sys/kern/subr_prf.c
@@ -1,4 +1,4 @@
-/* $NetBSD: subr_prf.c,v 1.162 2017/10/27 12:25:15 joerg Exp $ */
+/* $NetBSD: subr_prf.c,v 1.163 2018/03/31 23:12:01 christos Exp $ */
/*-
* Copyright (c) 1986, 1988, 1991, 1993
@@ -37,7 +37,7 @@
*/
#include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: subr_prf.c,v 1.162 2017/10/27 12:25:15 joerg Exp $");
+__KERNEL_RCSID(0, "$NetBSD: subr_prf.c,v 1.163 2018/03/31 23:12:01 christos Exp $");
#ifdef _KERNEL_OPT
#include "opt_ddb.h"
@@ -307,7 +307,7 @@ vpanic(const char *fmt, va_list ap)
doing_shutdown = 1;
- if (msgbufenabled && msgbufp->msg_magic == MSG_MAGIC)
+ if (logenabled(msgbufp))
panicstart = msgbufp->msg_bufx;
printf("panic: ");
@@ -323,7 +323,7 @@ vpanic(const char *fmt, va_list ap)
}
printf("\n");
- if (msgbufenabled && msgbufp->msg_magic == MSG_MAGIC)
+ if (logenabled(msgbufp))
panicend = msgbufp->msg_bufx;
#ifdef IPKDB
diff --git a/sys/miscfs/kernfs/kernfs_vnops.c b/sys/miscfs/kernfs/kernfs_vnops.c
index e01918718aa..ba1c0def180 100644
--- a/sys/miscfs/kernfs/kernfs_vnops.c
+++ b/sys/miscfs/kernfs/kernfs_vnops.c
@@ -1,4 +1,4 @@
-/* $NetBSD: kernfs_vnops.c,v 1.158 2017/05/26 14:21:01 riastradh Exp $ */
+/* $NetBSD: kernfs_vnops.c,v 1.159 2018/03/31 23:12:01 christos Exp $ */
/*
* Copyright (c) 1992, 1993
@@ -39,7 +39,7 @@
*/
#include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: kernfs_vnops.c,v 1.158 2017/05/26 14:21:01 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kernfs_vnops.c,v 1.159 2018/03/31 23:12:01 christos Exp $");
#include <sys/param.h>
#include <sys/systm.h>
@@ -381,7 +381,7 @@ kernfs_xread(struct kernfs_node *kfs, int off, char **bufp, size_t len, size_t *
* deal with cases where the message buffer has
* become corrupted.
*/
- if (!msgbufenabled || msgbufp->msg_magic != MSG_MAGIC) {
+ if (!logenabled(msgbufp)) {
msgbufenabled = 0;
return (ENXIO);
}
@@ -942,8 +942,7 @@ kernfs_readdir(void *v)
vrele(fvp);
}
if (kt->kt_tag == KFSmsgbuf) {
- if (!msgbufenabled
- || msgbufp->msg_magic != MSG_MAGIC) {
+ if (!logenabled(msgbufp)) {
continue;
}
}
diff --git a/sys/sys/msgbuf.h b/sys/sys/msgbuf.h
index 5cac8db6015..19628a18c5a 100644
--- a/sys/sys/msgbuf.h
+++ b/sys/sys/msgbuf.h
@@ -1,4 +1,4 @@
-/* $NetBSD: msgbuf.h,v 1.15 2007/11/07 00:19:08 ad Exp $ */
+/* $NetBSD: msgbuf.h,v 1.16 2018/03/31 23:12:01 christos Exp $ */
/*
* Copyright (c) 1981, 1984, 1993
@@ -51,6 +51,12 @@ extern struct kern_msgbuf *msgbufp; /* the mapped buffer, itself. */
void initmsgbuf(void *, size_t);
void loginit(void);
void logputchar(int);
+
+static inline int
+logenabled(const struct kern_msgbuf *mbp)
+{
+ return msgbufenabled && mbp->msg_magic == MSG_MAGIC;
+}
#endif
#endif /* !_SYS_MSGBUF_H_ */