From 8f9a2c4395bc8d974ef8fd5d2f0bb0fd92b043f8 Mon Sep 17 00:00:00 2001 From: Ankit Kumar Date: Thu, 20 Jul 2017 17:20:03 +0530 Subject: [PATCH] Unique name for temp Device file given and file closed before deleting it. lsvpd: Unique name for temp Device file given and file closed before deleting it. This piece of code used mkstemp to get unique name of file in that particular directory. As file name is not hardcoded, so parameter for device_close and device_open is changed to pass device location path. This patch also fixes the issue of opened file which is not closed by closing file descriptor. Signed-off-by: Ankit Kumar [Removed redundant variable - Vasant] Signed-off-by: Vasant Hegde --- src/internal/sys_interface/sysfs_SCSI_Fill.cpp | 54 ++++++++++++++------------ 1 file changed, 30 insertions(+), 24 deletions(-) diff --git a/src/internal/sys_interface/sysfs_SCSI_Fill.cpp b/src/internal/sys_interface/sysfs_SCSI_Fill.cpp index 836d6cc..1a31822 100644 --- a/src/internal/sys_interface/sysfs_SCSI_Fill.cpp +++ b/src/internal/sys_interface/sysfs_SCSI_Fill.cpp @@ -67,6 +67,7 @@ #include #include #include +#include extern "C" { @@ -840,43 +841,49 @@ namespace lsvpd /** * Close a device that may have been opened for reading */ - void device_close(int device_fd, int major, int minor, int mode) + void device_close(int device_fd, string& dev_path) { - char name[256]; struct stat statbuf; - if (-1 != sprintf(name, "/tmp/node-%d-%d-%d", - mode, major, minor)) { - if (stat(name, &statbuf) == 0) { - unlink(name); - } - } + if (stat(dev_path.c_str(), &statbuf) == 0) { + if(device_fd >= 0) + close(device_fd); + unlink(dev_path.c_str()); + } } // Open a temp file through which we can ioctl() to device for reading - int device_open(int major, int minor, int mode) + int device_open(int major, int minor, int mode, string& dev_path) { - char name[256]; + char name[PATH_MAX]; + /* Var is used to store unique name */ + char template_d[] = "/tmp/node-XXXXXX"; int device_fd = 0; int ret; struct stat statbuf; - if (sprintf(name, "/tmp/node-%d-%d-%d", mode, major, minor) < 0) { - return -ERROR_ACCESSING_DEVICE; - } + device_fd = mkstemp(template_d); + if (device_fd == -1) + return -1; + close(device_fd); - if (stat(name, &statbuf) == 0) { - unlink(name); - } + if (stat(template_d, &statbuf) == 0) + unlink(template_d); + + if (snprintf(name, PATH_MAX - 1, "%s-%d-%d-%d", + template_d, mode, major, minor) < 0) + return -ERROR_ACCESSING_DEVICE; ret = mknod(name, 0760 | mode, makedev(major, minor)); - if (ret != 0){ + if (ret != 0) return -UNABLE_TO_MKNOD_FILE; - } + + /* Store temp device filename. It will be used in device_close */ + dev_path = string(name); device_fd = open(name, 0); if (device_fd < 0) { - device_close(device_fd, major, minor, mode); + device_close(device_fd, dev_path); return -UNABLE_TO_OPEN_FILE; } @@ -1488,6 +1495,7 @@ out: ostringstream err; Logger logger; string msg; + string dev_path; /* Not a SCSI device */ if (fillMe->devBus.getValue() == "usb") return; @@ -1501,7 +1509,8 @@ out: // Open Device for reading device_fd = device_open(fillMe->devMajor, fillMe->devMinor, - fillMe->devAccessMode); + fillMe->devAccessMode, + dev_path); if (device_fd < 0) { msg = string("vpdupdate: Failed opening device: ") + fillMe->idNode.getValue(); @@ -1511,10 +1520,7 @@ out: collectVpd(fillMe, device_fd, limitSCSISize); - device_close(device_fd, - fillMe->devMajor, - fillMe->devMinor, - fillMe->devAccessMode); + device_close(device_fd, dev_path); } fillIPRData( fillMe ); -- 2.7.4