From: Guan Junxiong Date: Wed, 2 Aug 2017 07:51:09 +0000 (+0800) Subject: nvme-cli/list: fix when nvme device can not be open X-Git-Tag: v1.4~19 X-Git-Url: https://www.infradead.org/git/?a=commitdiff_plain;h=e2c83fdd416f1f22ddbb6dce51056351222338c4;p=users%2Fsagi%2Fnvme-cli.git nvme-cli/list: fix when nvme device can not be open 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. Signed-off-by: Guan Junxiong Reviewed-by: Christoph Hellwig Signed-off-by: Keith Busch --- diff --git a/nvme.c b/nvme.c index 235a1bbf..b4a039ad 100644 --- a/nvme.c +++ b/nvme.c @@ -911,7 +911,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; }