All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] nvme-cli/list: fix when nvme device can not be open
@ 2017-04-10  2:33 Guan Junxiong
  2017-04-10  7:46 ` Christoph Hellwig
  0 siblings, 1 reply; 3+ messages in thread
From: Guan Junxiong @ 2017-04-10  2:33 UTC (permalink / raw)


This patch checks the validity of fd returned by open operation on
the nvme divices. For example, when a user who doesn't have the
permission to open the device trys to use nvme list command to list
nvme devices, the fd is returned as -1 and errno indicates permission
denied.
In addition,this patch closes the current open device before
processing the next device.
---
 nvme.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/nvme.c b/nvme.c
index 4cc5a96..d80883c 100644
--- a/nvme.c
+++ b/nvme.c
@@ -811,7 +811,8 @@ static int list(int argc, char **argv, struct command *cmd, struct plugin *plugi
 	char path[256];
 	struct dirent **devices;
 	struct list_item *list_items;
-	unsigned int i, n, fd, ret;
+	unsigned int i, n, ret;
+	int fd;
 	int fmt;
 	const char *desc = "Retrieve basic information for the given device";
 	struct config {
@@ -846,7 +847,13 @@ static int list(int argc, char **argv, struct command *cmd, struct plugin *plugi
 	for (i = 0; i < n; i++) {
 		snprintf(path, sizeof(path), "%s%s", dev, devices[i]->d_name);
 		fd = open(path, O_RDONLY);
+		if (fd < 0) {
+			fprintf(stderr, "can not open %s: %s\n", path,
+					strerror(errno));
+			return errno;
+		}
 		ret = get_nvme_info(fd, &list_items[i], path);
+		close(fd);
 		if (ret)
 			return ret;
 	}
-- 
2.7.4

^ permalink raw reply related	[flat|nested] 3+ messages in thread

* [PATCH] nvme-cli/list: fix when nvme device can not be open
  2017-04-10  2:33 [PATCH] nvme-cli/list: fix when nvme device can not be open Guan Junxiong
@ 2017-04-10  7:46 ` Christoph Hellwig
  2017-04-10 14:49   ` Keith Busch
  0 siblings, 1 reply; 3+ messages in thread
From: Christoph Hellwig @ 2017-04-10  7:46 UTC (permalink / raw)


Looks good,

Reviewed-by: Christoph Hellwig <hch at lst.de>

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH] nvme-cli/list: fix when nvme device can not be open
  2017-04-10  7:46 ` Christoph Hellwig
@ 2017-04-10 14:49   ` Keith Busch
  0 siblings, 0 replies; 3+ messages in thread
From: Keith Busch @ 2017-04-10 14:49 UTC (permalink / raw)


Applied, thanks!

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2017-04-10 14:49 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-04-10  2:33 [PATCH] nvme-cli/list: fix when nvme device can not be open Guan Junxiong
2017-04-10  7:46 ` Christoph Hellwig
2017-04-10 14:49   ` Keith Busch

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.