mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Wang Long <long.wanglong@huawei.com>
To: <stefani@seibold.net>
Cc: <long.wanglong@huawei.com>, <peifeiyue@huawei.com>,
	<linux-kernel@vger.kernel.org>
Subject: [PATCH] samples: Fix `echo 1 > /proc/int-fifo` never return error
Date: Tue, 3 Feb 2015 11:51:17 +0000	[thread overview]
Message-ID: <1422964277-140278-2-git-send-email-long.wanglong@huawei.com> (raw)
In-Reply-To: <1422964277-140278-1-git-send-email-long.wanglong@huawei.com>

echo 99 > /proc/int-fifo       ------------> Never return
echo 1000 > /proc/int-fifo     ------------> Never return

this patch fix it.

Signed-off-by: Wang Long <long.wanglong@huawei.com>
---
 samples/kfifo/inttype-example.c | 51 ++++++++++++++++++++++++++++++++---------
 1 file changed, 40 insertions(+), 11 deletions(-)

diff --git a/samples/kfifo/inttype-example.c b/samples/kfifo/inttype-example.c
index 8dc3c2e..cc0db5f 100644
--- a/samples/kfifo/inttype-example.c
+++ b/samples/kfifo/inttype-example.c
@@ -6,6 +6,7 @@
  * Released under the GPL version 2 only.
  *
  */
+#include <asm/uaccess.h>
 
 #include <linux/init.h>
 #include <linux/module.h>
@@ -23,6 +24,9 @@
 /* name of the proc entry */
 #define	PROC_FIFO	"int-fifo"
 
+/* Worst case buffer size needed for holding an integer. */
+#define PROC_NUMBUF 13
+
 /* lock for procfs read access */
 static DEFINE_MUTEX(read_lock);
 
@@ -108,33 +112,58 @@ static int __init testfunc(void)
 static ssize_t fifo_write(struct file *file, const char __user *buf,
 						size_t count, loff_t *ppos)
 {
-	int ret;
-	unsigned int copied;
+	char buffer[PROC_NUMBUF];
+	int value;
+	int err;
 
-	if (mutex_lock_interruptible(&write_lock))
-		return -ERESTARTSYS;
+	memset(buffer, 0, sizeof(buffer));
 
-	ret = kfifo_from_user(&test, buf, count, &copied);
+	if (count > sizeof(buffer) - 1)
+		count = sizeof(buffer) - 1;
+	if (copy_from_user(buffer, buf, count)) {
+		err = -EFAULT;
+		goto out;
+	}
 
-	mutex_unlock(&write_lock);
+	err = kstrtoint(strstrip(buffer), 0, &value);
+	if (err)
+		goto out;
+
+	if (kfifo_is_full(&test)) {
+		err = -EINVAL;
+		goto out;
+	}
 
-	return ret ? ret : copied;
+	if (mutex_lock_interruptible(&write_lock))
+		return -ERESTARTSYS;
+	kfifo_put(&test, value);
+	mutex_unlock(&write_lock);
+out:
+	return err < 0 ? err : count;
 }
 
 static ssize_t fifo_read(struct file *file, char __user *buf,
 						size_t count, loff_t *ppos)
 {
-	int ret;
-	unsigned int copied;
+	char buffer[PROC_NUMBUF * FIFO_SIZE];
+	int value;
+	size_t	len = 0;
+	ssize_t	ret = -1;
+
+	memset(buffer, 0, sizeof(buffer));
 
 	if (mutex_lock_interruptible(&read_lock))
 		return -ERESTARTSYS;
 
-	ret = kfifo_to_user(&test, buf, count, &copied);
+	while (!kfifo_is_empty(&test)){
+		ret = kfifo_get(&test, &value);
+		len = snprintf(buffer, sizeof(buffer), "%s%d\n", buffer, value);
+	}
 
 	mutex_unlock(&read_lock);
+	ret = copy_to_user(buf, buffer, len);
 
-	return ret ? ret : copied;
+	return ret ? ret : len;
 }
 
 static const struct file_operations fifo_fops = {
-- 
1.8.3.1


  reply	other threads:[~2015-02-03 11:55 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-02-03 11:51 Wang Long
2015-02-03 11:51 ` Wang Long [this message]
2015-02-04 10:18   ` Stefani Seibold
2015-02-04 10:35     ` long.wanglong

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=1422964277-140278-2-git-send-email-long.wanglong@huawei.com \
    --to=long.wanglong@huawei.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=peifeiyue@huawei.com \
    --cc=stefani@seibold.net \
    /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

all inboxes | Powered by JetHome®