Source-Changes-HG archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
[src/trunk]: src/sys factor out some repeated code and simplify the logputcha...
details: https://anonhg.NetBSD.org/src/rev/5a479108250b
branches: trunk
changeset: 321697:5a479108250b
user: christos <christos%NetBSD.org@localhost>
date: Sat Mar 31 23:12:01 2018 +0000
description:
factor out some repeated code and simplify the logputchar function.
diffstat:
sys/kern/subr_log.c | 95 +++++++++++++++++++++------------------
sys/kern/subr_prf.c | 8 +-
sys/miscfs/kernfs/kernfs_vnops.c | 9 +--
sys/sys/msgbuf.h | 8 ++-
4 files changed, 67 insertions(+), 53 deletions(-)
diffs (227 lines):
diff -r c336ccdfe291 -r 5a479108250b sys/kern/subr_log.c
--- a/sys/kern/subr_log.c Sat Mar 31 19:27:14 2018 +0000
+++ b/sys/kern/subr_log.c Sat Mar 31 23:12:01 2018 +0000
@@ -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 @@
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 @@
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;
+
+ if (!msgbufenabled)
+ goto out;
- /*
- * 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;
- }
- }
- }
+ 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 @@
extern kmutex_t log_lock;
int error;
- if (!msgbufenabled || msgbufp->msg_magic != MSG_MAGIC) {
+ if (!logenabled(msgbufp)) {
msgbufenabled = 0;
return (ENXIO);
}
diff -r c336ccdfe291 -r 5a479108250b sys/kern/subr_prf.c
--- a/sys/kern/subr_prf.c Sat Mar 31 19:27:14 2018 +0000
+++ b/sys/kern/subr_prf.c Sat Mar 31 23:12:01 2018 +0000
@@ -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 @@
doing_shutdown = 1;
- if (msgbufenabled && msgbufp->msg_magic == MSG_MAGIC)
+ if (logenabled(msgbufp))
panicstart = msgbufp->msg_bufx;
printf("panic: ");
@@ -323,7 +323,7 @@
}
printf("\n");
- if (msgbufenabled && msgbufp->msg_magic == MSG_MAGIC)
+ if (logenabled(msgbufp))
panicend = msgbufp->msg_bufx;
#ifdef IPKDB
diff -r c336ccdfe291 -r 5a479108250b sys/miscfs/kernfs/kernfs_vnops.c
--- a/sys/miscfs/kernfs/kernfs_vnops.c Sat Mar 31 19:27:14 2018 +0000
+++ b/sys/miscfs/kernfs/kernfs_vnops.c Sat Mar 31 23:12:01 2018 +0000
@@ -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 @@
* 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 @@
vrele(fvp);
}
if (kt->kt_tag == KFSmsgbuf) {
- if (!msgbufenabled
- || msgbufp->msg_magic != MSG_MAGIC) {
+ if (!logenabled(msgbufp)) {
continue;
}
}
diff -r c336ccdfe291 -r 5a479108250b sys/sys/msgbuf.h
--- a/sys/sys/msgbuf.h Sat Mar 31 19:27:14 2018 +0000
+++ b/sys/sys/msgbuf.h Sat Mar 31 23:12:01 2018 +0000
@@ -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 @@
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_ */
Home |
Main Index |
Thread Index |
Old Index