diff options
author | eadler <eadler@FreeBSD.org> | 2012-03-19 00:36:32 +0000 |
---|---|---|
committer | eadler <eadler@FreeBSD.org> | 2012-03-19 00:36:32 +0000 |
commit | 169b46c915342cb0b6ad052546ca37d596e4e43b (patch) | |
tree | 0ebe1e7cd76486bc4de15eb1a59c179ab50128dd | |
parent | 6cf6bbda47f7179440b7e1e8aa8a6a8fea6af1bf (diff) | |
download | FreeBSD-src-169b46c915342cb0b6ad052546ca37d596e4e43b.zip FreeBSD-src-169b46c915342cb0b6ad052546ca37d596e4e43b.tar.gz |
- Clean up timestamps in msgbuf code. The timestamps should now be
inserted after the priority token thus cleaning up the output.
- Remove the needless double internal do_add_char function.
- Resolve a possible deadlock if interrupts are
disabled and getnanotime is called
Reviewed by: bde kmacy, avg, sbruno (various versions)
Approved by: cperciva
MFC after: 2 weeks
-rw-r--r-- | sys/kern/subr_msgbuf.c | 54 | ||||
-rw-r--r-- | sys/sys/msgbuf.h | 5 |
2 files changed, 21 insertions, 38 deletions
diff --git a/sys/kern/subr_msgbuf.c b/sys/kern/subr_msgbuf.c index 2d141e3..ecdbe72 100644 --- a/sys/kern/subr_msgbuf.c +++ b/sys/kern/subr_msgbuf.c @@ -49,7 +49,8 @@ static u_int msgbuf_cksum(struct msgbuf *mbp); /* - * + * Timestamps in msgbuf are useful when trying to diagnose when core dumps + * or other actions occured. */ static int msgbuf_show_timestamp = 0; SYSCTL_INT(_kern, OID_AUTO, msgbuf_show_timestamp, CTLFLAG_RW | CTLFLAG_TUN, @@ -143,49 +144,20 @@ msgbuf_getcount(struct msgbuf *mbp) * * The caller should hold the message buffer spinlock. */ -static inline void -__msgbuf_do_addchar(struct msgbuf * const mbp, u_int * const seq, const int c) + +static void +msgbuf_do_addchar(struct msgbuf * const mbp, u_int * const seq, const int c) { u_int pos; /* Make sure we properly wrap the sequence number. */ pos = MSGBUF_SEQ_TO_POS(mbp, *seq); - - mbp->msg_cksum += (u_int)c - + mbp->msg_cksum += (u_int)(u_char)c - (u_int)(u_char)mbp->msg_ptr[pos]; - mbp->msg_ptr[pos] = c; - *seq = MSGBUF_SEQNORM(mbp, *seq + 1); } -static inline void -msgbuf_do_addchar(struct msgbuf * const mbp, u_int * const seq, const int c) -{ - - if (msgbuf_show_timestamp && - (mbp->msg_flags & MSGBUF_NEXT_NEW_LINE) != 0) { - char buf[32]; - char const *bufp; - struct timespec ts; - int err; - - getnanouptime(&ts); - err = snprintf(buf, sizeof (buf), "[%jd.%ld] ", - (intmax_t)ts.tv_sec, ts.tv_nsec / 1000); - - for (bufp = buf; *bufp != '\0'; bufp++) - __msgbuf_do_addchar(mbp, seq, *bufp); - - mbp->msg_flags &= ~MSGBUF_NEXT_NEW_LINE; - } - - __msgbuf_do_addchar(mbp, seq, c); - - if (c == '\n') - mbp->msg_flags |= MSGBUF_NEXT_NEW_LINE; -} - /* * Append a character to a message buffer. */ @@ -213,7 +185,8 @@ msgbuf_addstr(struct msgbuf *mbp, int pri, char *str, int filter_cr) u_int seq; size_t len, prefix_len; char prefix[MAXPRIBUF]; - int nl, i; + char buf[32]; + int nl, i, j, needtime; len = strlen(str); prefix_len = 0; @@ -250,6 +223,7 @@ msgbuf_addstr(struct msgbuf *mbp, int pri, char *str, int filter_cr) mbp->msg_flags &= ~MSGBUF_NEEDNL; } + needtime = 1; for (i = 0; i < len; i++) { /* * If we just had a newline, and the priority is not -1 @@ -263,6 +237,16 @@ msgbuf_addstr(struct msgbuf *mbp, int pri, char *str, int filter_cr) msgbuf_do_addchar(mbp, &seq, prefix[j]); } + if (msgbuf_show_timestamp && needtime == 1 && + (mbp->msg_flags & MSGBUF_NEEDNL) == 0) { + + snprintf(buf, sizeof(buf), "[%jd] ", + (intmax_t)time_uptime); + for (j = 0; buf[j] != '\0'; j++) + msgbuf_do_addchar(mbp, &seq, buf[j]); + needtime = 0; + } + /* * Don't copy carriage returns if the caller requested * filtering. diff --git a/sys/sys/msgbuf.h b/sys/sys/msgbuf.h index 0e25a1e..996fa20 100644 --- a/sys/sys/msgbuf.h +++ b/sys/sys/msgbuf.h @@ -46,9 +46,8 @@ struct msgbuf { u_int msg_cksum; /* checksum of contents */ u_int msg_seqmod; /* range for sequence numbers */ int msg_lastpri; /* saved priority value */ - u_int msg_flags; -#define MSGBUF_NEEDNL 0x01 /* set when newline needed */ -#define MSGBUF_NEXT_NEW_LINE 0x02 + u_int msg_flags; +#define MSGBUF_NEEDNL 0x01 /* set when newline needed */ struct mtx msg_lock; /* mutex to protect the buffer */ }; |