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 <guanjunxiong@huawei.com>
Reviewed-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: Keith Busch <keith.busch@intel.com>
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;
}