[PATCH 5/7] commands: devlookup: use cdev_open_by_name()

Sascha Hauer s.hauer at pengutronix.de
Thu May 16 00:08:20 PDT 2024


cdev_by_name() returns a cdev without increasing its reference count. In
order to maintain a proper reference counting use cdev_open_by_name()
instead and make sure it's closed afterwards when no longer needed.

Signed-off-by: Sascha Hauer <s.hauer at pengutronix.de>
---
 commands/devlookup.c | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)

diff --git a/commands/devlookup.c b/commands/devlookup.c
index ffd6afbaba..bc9bd94614 100644
--- a/commands/devlookup.c
+++ b/commands/devlookup.c
@@ -25,7 +25,7 @@ static int do_devlookup(int argc, char *argv[])
 {
 	const char *variable = NULL, *devicefile, *paramname;
 	struct cdev *cdev;
-	int opt;
+	int opt, ret;
 
 	while ((opt = getopt(argc, argv, "v:")) > 0) {
 		switch(opt) {
@@ -43,7 +43,7 @@ static int do_devlookup(int argc, char *argv[])
 
 	devicefile = devpath_to_name(devicefile);
 
-	cdev = cdev_by_name(devicefile);
+	cdev = cdev_open_by_name(devicefile, O_RDONLY);
 	if (!cdev) {
 		printf("devlookup: cdev %s not found\n", devicefile);
 		return -ENOENT;
@@ -51,13 +51,18 @@ static int do_devlookup(int argc, char *argv[])
 
 	if (!cdev->dev) {
 		printf("devlookup: cdev %s not associated with a device\n", devicefile);
-		return -ENODEV;
+		ret = -ENODEV;
+		goto out;
 	}
 
 	if (paramname)
-		return report(variable, dev_get_param(cdev->dev, paramname));
+		ret = report(variable, dev_get_param(cdev->dev, paramname));
+	else
+		ret = report(variable, dev_name(cdev->dev));
+out:
+	cdev_close(cdev);
 
-	return report(variable, dev_name(cdev->dev));
+	return ret;
 }
 
 BAREBOX_CMD_HELP_START(devlookup)
-- 
2.39.2




More information about the barebox mailing list