[libvirt] [PATCHv2] qemu: use virAsprintf instead of PATH_MAX

Eric Blake eblake at redhat.com
Wed Dec 8 21:57:51 UTC 2010


* src/qemu/qemu_driver.c (qemudLogFD, qemudLogReadFD)
(qemudStartup, qemudGetProcessInfo): Use heap instead of stack.
(qemudDomainDetachPciDiskDevice)
(qemudDomainDetachSCSIDiskDevice): Minor optimization.
---

v2: rebased on top of Ryan's v5 patch

 src/qemu/qemu_driver.c |   62 ++++++++++++++++++++++++-----------------------
 1 files changed, 32 insertions(+), 30 deletions(-)

diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index 4e65f3b..997d333 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -744,13 +744,11 @@ static int qemuCgroupControllerActive(struct qemud_driver *driver,
 static int
 qemudLogFD(struct qemud_driver *driver, const char* name, bool append)
 {
-    char logfile[PATH_MAX];
+    char *logfile;
     mode_t logmode;
-    int ret, fd = -1;
+    int fd = -1;

-    if ((ret = snprintf(logfile, sizeof(logfile), "%s/%s.log",
-                        driver->logDir, name))
-        < 0 || ret >= sizeof(logfile)) {
+    if (virAsprintf(&logfile, "%s/%s.log", driver->logDir, name) < 0) {
         virReportOOMError();
         return -1;
     }
@@ -766,8 +764,10 @@ qemudLogFD(struct qemud_driver *driver, const char* name, bool append)
         virReportSystemError(errno,
                              _("failed to create logfile %s"),
                              logfile);
+        VIR_FREE(logfile);
         return -1;
     }
+    VIR_FREE(logfile);
     if (virSetCloseExec(fd) < 0) {
         virReportSystemError(errno, "%s",
                              _("Unable to set VM logfile close-on-exec flag"));
@@ -781,30 +781,28 @@ qemudLogFD(struct qemud_driver *driver, const char* name, bool append)
 static int
 qemudLogReadFD(const char* logDir, const char* name, off_t pos)
 {
-    char logfile[PATH_MAX];
+    char *logfile;
     mode_t logmode = O_RDONLY;
-    int ret, fd = -1;
+    int fd = -1;

-    if ((ret = snprintf(logfile, sizeof(logfile), "%s/%s.log", logDir, name))
-        < 0 || ret >= sizeof(logfile)) {
+    if (virAsprintf(&logfile, "%s/%s.log", logDir, name) < 0) {
         qemuReportError(VIR_ERR_INTERNAL_ERROR,
                         _("failed to build logfile name %s/%s.log"),
                         logDir, name);
         return -1;
     }

-
     if ((fd = open(logfile, logmode)) < 0) {
         virReportSystemError(errno,
                              _("failed to create logfile %s"),
                              logfile);
-        return -1;
+        goto cleanup;
     }
     if (virSetCloseExec(fd) < 0) {
         virReportSystemError(errno, "%s",
                              _("Unable to set VM logfile close-on-exec flag"));
         VIR_FORCE_CLOSE(fd);
-        return -1;
+        goto cleanup;
     }
     if (pos < 0 || lseek(fd, pos, SEEK_SET) < 0) {
         virReportSystemError(pos < 0 ? 0 : errno,
@@ -812,6 +810,9 @@ qemudLogReadFD(const char* logDir, const char* name, off_t pos)
                              (long long) pos, logfile);
         VIR_FORCE_CLOSE(fd);
     }
+
+cleanup:
+    VIR_FREE(logfile);
     return fd;
 }

@@ -1721,7 +1722,7 @@ cleanup:
 static int
 qemudStartup(int privileged) {
     char *base = NULL;
-    char driverConf[PATH_MAX];
+    char *driverConf = NULL;
     int rc;
     virConnectPtr conn = NULL;

@@ -1850,14 +1851,9 @@ qemudStartup(int privileged) {
     /* Configuration paths are either ~/.libvirt/qemu/... (session) or
      * /etc/libvirt/qemu/... (system).
      */
-    if (snprintf (driverConf, sizeof(driverConf), "%s/qemu.conf", base) == -1)
-        goto out_of_memory;
-    driverConf[sizeof(driverConf)-1] = '\0';
-
-    if (virAsprintf(&qemu_driver->configDir, "%s/qemu", base) == -1)
-        goto out_of_memory;
-
-    if (virAsprintf(&qemu_driver->autostartDir, "%s/qemu/autostart", base) == -1)
+    if (virAsprintf(&driverConf, "%s/qemu.conf", base) < 0 ||
+        virAsprintf(&qemu_driver->configDir, "%s/qemu", base) < 0 ||
+        virAsprintf(&qemu_driver->autostartDir, "%s/qemu/autostart", base) < 0)
         goto out_of_memory;

     VIR_FREE(base);
@@ -1872,6 +1868,7 @@ qemudStartup(int privileged) {
     if (qemudLoadDriverConfig(qemu_driver, driverConf) < 0) {
         goto error;
     }
+    VIR_FREE(driverConf);

     if (qemudSecurityInit(qemu_driver) < 0)
         goto error;
@@ -1984,6 +1981,7 @@ error:
     if (conn)
         virConnectClose(conn);
     VIR_FREE(base);
+    VIR_FREE(driverConf);
     qemudShutdown();
     return -1;
 }
@@ -4552,18 +4550,20 @@ cleanup:
 }


-static int qemudGetProcessInfo(unsigned long long *cpuTime, int *lastCpu, int pid, int tid) {
-    char proc[PATH_MAX];
+static int
+qemudGetProcessInfo(unsigned long long *cpuTime, int *lastCpu, int pid,
+                    int tid) {
+    char *proc;
     FILE *pidinfo;
     unsigned long long usertime, systime;
     int cpu;
     int ret;

     if (tid)
-        ret = snprintf(proc, sizeof(proc), "/proc/%d/task/%d/stat", pid, tid);
+        ret = virAsprintf(&proc, "/proc/%d/task/%d/stat", pid, tid);
     else
-        ret = snprintf(proc, sizeof(proc), "/proc/%d/stat", pid);
-    if (ret >= (int)sizeof(proc)) {
+        ret = virAsprintf(&proc, "/proc/%d/stat", pid);
+    if (ret < 0) {
         errno = E2BIG;
         return -1;
     }
@@ -4574,8 +4574,10 @@ static int qemudGetProcessInfo(unsigned long long *cpuTime, int *lastCpu, int pi
             *cpuTime = 0;
         if (lastCpu)
             *lastCpu = 0;
+        VIR_FREE(proc);
         return 0;
     }
+    VIR_FREE(proc);

     /* See 'man proc' for information about what all these fields are. We're
      * only interested in a very few of them */
@@ -9063,8 +9065,8 @@ static int qemudDomainDetachPciDiskDevice(struct qemud_driver *driver,

     /* build the actual drive id string as the disk->info.alias doesn't
      * contain the QEMU_DRIVE_HOST_PREFIX that is passed to qemu */
-    if (virAsprintf(&drivestr, "%s%s",
-                    QEMU_DRIVE_HOST_PREFIX, detach->info.alias) < 0) {
+    if (virAsprintf(&drivestr, QEMU_DRIVE_HOST_PREFIX "%s",
+                    detach->info.alias) < 0) {
         virReportOOMError();
         goto cleanup;
     }
@@ -9155,8 +9157,8 @@ static int qemudDomainDetachSCSIDiskDevice(struct qemud_driver *driver,

     /* build the actual drive id string as the disk->info.alias doesn't
      * contain the QEMU_DRIVE_HOST_PREFIX that is passed to qemu */
-    if (virAsprintf(&drivestr, "%s%s",
-                    QEMU_DRIVE_HOST_PREFIX, detach->info.alias) < 0) {
+    if (virAsprintf(&drivestr, QEMU_DRIVE_HOST_PREFIX "%s",
+                    detach->info.alias) < 0) {
         virReportOOMError();
         goto cleanup;
     }
-- 
1.7.3.2




More information about the libvir-list mailing list