Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S965135AbbBDKgP (ORCPT ); Wed, 4 Feb 2015 05:36:15 -0500 Received: from www84.your-server.de ([213.133.104.84]:60487 "EHLO www84.your-server.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S933613AbbBDKgK (ORCPT ); Wed, 4 Feb 2015 05:36:10 -0500 X-Greylist: delayed 1064 seconds by postgrey-1.27 at vger.kernel.org; Wed, 04 Feb 2015 05:36:10 EST Message-ID: <1423045101.8226.5.camel@seibold.net> Subject: Re: [PATCH] samples: Fix `echo 1 > /proc/int-fifo` never return error From: Stefani Seibold To: Wang Long Cc: peifeiyue@huawei.com, linux-kernel@vger.kernel.org Date: Wed, 04 Feb 2015 11:18:21 +0100 In-Reply-To: <1422964277-140278-2-git-send-email-long.wanglong@huawei.com> References: <1422964277-140278-1-git-send-email-long.wanglong@huawei.com> <1422964277-140278-2-git-send-email-long.wanglong@huawei.com> Content-Type: text/plain; charset="UTF-8" X-Mailer: Evolution 3.12.10 Mime-Version: 1.0 Content-Transfer-Encoding: 7bit X-Authenticated-Sender: stefani@seibold.net Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 3209 Lines: 114 The example is intended for int types, not for strings. So it is not a bug, it's a feature ;-) But anyway, if you prefer to handle with strings your are okay by me. Am Dienstag, den 03.02.2015, 11:51 +0000 schrieb Wang Long: > echo 99 > /proc/int-fifo ------------> Never return > echo 1000 > /proc/int-fifo ------------> Never return > > this patch fix it. > > Signed-off-by: Wang Long > --- > 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 > > #include > #include > @@ -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 = { -- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/