linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Rasmus Villemoes <linux@rasmusvillemoes.dk>
To: "Pavel Machek" <pavel@ucw.cz>,
	"Uwe Kleine-König" <u.kleine-koenig@pengutronix.de>,
	"Jacek Anaszewski" <jacek.anaszewski@gmail.com>
Cc: LKML <linux-kernel@vger.kernel.org>,
	Rasmus Villemoes <linux@rasmusvillemoes.dk>,
	linux-leds@vger.kernel.org
Subject: [PATCH v2 3/6] leds: netdev trigger: move newline handling back to device_name_store
Date: Thu, 14 Mar 2019 15:06:16 +0100	[thread overview]
Message-ID: <20190314140619.3309-4-linux@rasmusvillemoes.dk> (raw)
In-Reply-To: <20190314140619.3309-1-linux@rasmusvillemoes.dk>

Currently, setting device_name to a 15-character name requires
avoiding a trailing newline (e.g. by using 'echo -n' or 'printf'),
which is inconsistent and potentially surprising. Instead of
potentially including a newline in the copy and then overwriting it,
move the newline detection logic to the sysfs handler itself, and
handle it by passing a smaller buffer size to
netdev_trig_set_device().

This also makes netdev_trig_set_device() a better fit for a future
user, which will not need the newline truncation logic.

We still have to tell userspace we consumed all the bytes it gave us,
so we have to stash the original value of size.

Signed-off-by: Rasmus Villemoes <linux@rasmusvillemoes.dk>
---
 drivers/leds/trigger/ledtrig-netdev.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/drivers/leds/trigger/ledtrig-netdev.c b/drivers/leds/trigger/ledtrig-netdev.c
index 21605033e322..c35439291424 100644
--- a/drivers/leds/trigger/ledtrig-netdev.c
+++ b/drivers/leds/trigger/ledtrig-netdev.c
@@ -114,8 +114,6 @@ static void netdev_trig_set_device(struct led_netdev_data *trigger_data,
 
 	memcpy(trigger_data->device_name, buf, size);
 	trigger_data->device_name[size] = 0;
-	if (size > 0 && trigger_data->device_name[size - 1] == '\n')
-		trigger_data->device_name[size - 1] = 0;
 
 	if (trigger_data->device_name[0] != 0)
 		trigger_data->net_dev =
@@ -134,7 +132,11 @@ static ssize_t device_name_store(struct device *dev,
 				 size_t size)
 {
 	struct led_netdev_data *trigger_data = led_trigger_get_drvdata(dev);
+	size_t orig_size = size;
 
+	/* Ignore trailing newline */
+	if (size > 0 && buf[size - 1] == '\n')
+		size--;
 	if (size >= IFNAMSIZ)
 		return -EINVAL;
 
@@ -147,7 +149,7 @@ static ssize_t device_name_store(struct device *dev,
 	set_baseline_state(trigger_data);
 	spin_unlock_bh(&trigger_data->lock);
 
-	return size;
+	return orig_size;
 }
 
 static DEVICE_ATTR_RW(device_name);
-- 
2.20.1


  parent reply	other threads:[~2019-03-14 14:06 UTC|newest]

Thread overview: 47+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-03-11 10:59 replacement for CAN_LEDS Rasmus Villemoes
2019-03-11 14:42 ` Pavel Machek
2019-03-13 20:26   ` [PATCH 0/4] leds: netdev trigger: allow setting initial values in device tree Rasmus Villemoes
2019-03-13 20:26     ` [PATCH 1/4] leds: netdev trigger: use memcpy in device_name_store Rasmus Villemoes
2019-03-14  9:29       ` Uwe Kleine-König
2019-03-14  9:57         ` Rasmus Villemoes
2019-03-14 10:04           ` Uwe Kleine-König
2019-03-14 10:14       ` Pavel Machek
2019-03-14 10:54         ` Rasmus Villemoes
2019-03-14 12:16           ` Pavel Machek
2019-03-13 20:26     ` [PATCH 2/4] leds: netdev trigger: factor out middle part of device_name_store Rasmus Villemoes
2019-03-14  9:31       ` Uwe Kleine-König
2019-03-14  9:57         ` Rasmus Villemoes
2019-03-14 10:15         ` Pavel Machek
2019-03-14 10:20           ` Uwe Kleine-König
2019-03-13 20:26     ` [PATCH 3/4] leds: netdev trigger: add documentation to leds/common.txt Rasmus Villemoes
2019-03-13 20:26     ` [PATCH 4/4] leds: netdev trigger: allow setting initial values in device tree Rasmus Villemoes
2019-03-14  9:36       ` Uwe Kleine-König
2019-03-14 10:28         ` Rasmus Villemoes
2019-03-14 10:29       ` Pavel Machek
2019-03-14 11:26         ` Rasmus Villemoes
2019-03-14 12:00           ` Pavel Machek
2019-03-14 13:19             ` Rasmus Villemoes
2019-03-17 19:11               ` Pavel Machek
2019-03-24 20:39                 ` Rasmus Villemoes
2019-03-14 10:32       ` Jacek Anaszewski
2019-03-14 14:06     ` [PATCH v2 0/6] " Rasmus Villemoes
2019-03-14 14:06       ` [PATCH v2 1/6] leds: netdev trigger: use memcpy in device_name_store Rasmus Villemoes
2019-03-18 11:20         ` Pavel Machek
2019-03-26 19:53         ` Jacek Anaszewski
2019-03-27 15:26           ` Rasmus Villemoes
2019-03-27 21:20             ` Jacek Anaszewski
2019-03-27 21:31               ` Rasmus Villemoes
2019-03-27 21:45                 ` Jacek Anaszewski
2019-03-14 14:06       ` [PATCH v2 2/6] leds: netdev trigger: factor out middle part of device_name_store Rasmus Villemoes
2019-03-18 11:24         ` Pavel Machek
2019-03-14 14:06       ` Rasmus Villemoes [this message]
2019-03-18 11:25         ` [PATCH v2 3/6] leds: netdev trigger: move newline handling back to device_name_store Pavel Machek
2019-03-14 14:06       ` [PATCH v2 4/6] leds: netdev trigger: move name length checking to netdev_trig_set_device Rasmus Villemoes
2019-03-18 11:26         ` Pavel Machek
2019-03-14 14:06       ` [PATCH v2 5/6] leds: netdev trigger: add documentation to leds/common.txt Rasmus Villemoes
2019-03-14 14:06       ` [PATCH v2 6/6] leds: netdev trigger: allow setting initial values in device tree Rasmus Villemoes
2019-03-14 14:24         ` Jacek Anaszewski
2019-03-14 15:05           ` Rasmus Villemoes
2019-03-14 15:36             ` Jacek Anaszewski
2019-03-18 11:54         ` Pavel Machek
2019-03-28 16:28         ` Rob Herring

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=20190314140619.3309-4-linux@rasmusvillemoes.dk \
    --to=linux@rasmusvillemoes.dk \
    --cc=jacek.anaszewski@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-leds@vger.kernel.org \
    --cc=pavel@ucw.cz \
    --cc=u.kleine-koenig@pengutronix.de \
    /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).