util-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Milan Broz <mbroz@redhat.com>
To: util-linux@vger.kernel.org
Cc: Milan Broz <mbroz@redhat.com>
Subject: [PATCH] dmesg: fix kmsg read if read returns EPIPE
Date: Mon, 13 Aug 2012 13:55:30 +0200	[thread overview]
Message-ID: <1344858930-21075-1-git-send-email-mbroz@redhat.com> (raw)

The /dev/kmsg can return EPIPE if current record has beed modified
while reading.

For init_kmsg, it cause switch to DMESG_METHOD_SYSLOG
(which is not expected) and later it can truncate output.

Signed-off-by: Milan Broz <mbroz@redhat.com>
---
 sys-utils/dmesg.c |   18 +++++++++++++++---
 1 file changed, 15 insertions(+), 3 deletions(-)

diff --git a/sys-utils/dmesg.c b/sys-utils/dmesg.c
index e92904f..0ee03ee 100644
--- a/sys-utils/dmesg.c
+++ b/sys-utils/dmesg.c
@@ -851,6 +851,19 @@ static void print_buffer(struct dmesg_control *ctl,
 		print_record(ctl, &rec);
 }
 
+static ssize_t read_kmsg_one(struct dmesg_control *ctl)
+{
+	ssize_t size;
+
+	/* kmsg returns EPIPE if record was modified while reading */
+	do {
+		size = read(ctl->kmsg, ctl->kmsg_buf,
+			    sizeof(ctl->kmsg_buf) - 1);
+	} while (size < 0 && errno == EPIPE);
+
+	return size;
+}
+
 static int init_kmsg(struct dmesg_control *ctl)
 {
 	int mode = O_RDONLY;
@@ -877,8 +890,7 @@ static int init_kmsg(struct dmesg_control *ctl)
 	 * Let's try to read the first record. The record is later processed in
 	 * read_kmsg().
 	 */
-	ctl->kmsg_first_read = read(ctl->kmsg, ctl->kmsg_buf,
-				  sizeof(ctl->kmsg_buf) - 1);
+	ctl->kmsg_first_read = read_kmsg_one(ctl);
 	if (ctl->kmsg_first_read < 0) {
 		close(ctl->kmsg);
 		ctl->kmsg = -1;
@@ -996,7 +1008,7 @@ static int read_kmsg(struct dmesg_control *ctl)
 				      ctl->kmsg_buf, (size_t) sz) == 0)
 			print_record(ctl, &rec);
 
-		sz = read(ctl->kmsg, ctl->kmsg_buf, sizeof(ctl->kmsg_buf) - 1);
+		sz = read_kmsg_one(ctl);
 	}
 
 	return 0;
-- 
1.7.10.4


             reply	other threads:[~2012-08-13 11:55 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-08-13 11:55 Milan Broz [this message]
2012-08-13 13:55 ` [PATCH] dmesg: fix kmsg read if read returns EPIPE Karel Zak

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1344858930-21075-1-git-send-email-mbroz@redhat.com \
    --to=mbroz@redhat.com \
    --cc=util-linux@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).