]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
Rewrite openvzSetUUID.
authorJim Meyering <meyering@redhat.com>
Thu, 21 Feb 2008 18:48:06 +0000 (18:48 +0000)
committerJim Meyering <meyering@redhat.com>
Thu, 21 Feb 2008 18:48:06 +0000 (18:48 +0000)
* src/openvz_conf.c (openvzSetUUID): Rewrite to avoid unchecked
lseek, write, and close as well as a potential file descriptor leak.

ChangeLog
src/openvz_conf.c

index 54ac292ee4a95a3ab44b52978ea5cb68d8ae9e03..bc0cdec4c0db649450f1e038094216c4b0f9b653 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,9 @@
 Thu Feb 21 19:22:10 CET 2008 Jim Meyering <meyering@redhat.com>
 
+       Rewrite openvzSetUUID.
+       * src/openvz_conf.c (openvzSetUUID): Rewrite to avoid unchecked
+       lseek, write, and close as well as a potential file descriptor leak.
+
        Handle failed openvzLocateConfDir.
        * src/openvz_conf.c (openvzLocateConfDir, openvzGetVPSUUID):
        (openvzSetUUID): Don't dereference NULL upon failure.
index 79d1e90f564b6bb7dfca2ba4abf0bf26008fd544..364cb2505dc1d0f6c743d1a4eac93dc53c472e49 100644 (file)
@@ -677,7 +677,6 @@ openvzSetUUID(int vpsid)
     char uuidstr[VIR_UUID_STRING_BUFLEN];
     unsigned char uuid[VIR_UUID_BUFLEN];
     char *conf_dir;
-    int fd, ret;
 
     conf_dir = openvzLocateConfDir();
     if (conf_dir == NULL)
@@ -685,23 +684,22 @@ openvzSetUUID(int vpsid)
     sprintf(conf_file, "%s/%d.conf", conf_dir, vpsid);
     free(conf_dir);
 
-    fd = open(conf_file, O_RDWR);
-    if(fd == -1)
+    if (openvzGetVPSUUID(vpsid, uuidstr))
         return -1;
 
-    ret = openvzGetVPSUUID(vpsid, uuidstr);
-    if(ret == -1)
-        return -1;
+    if (uuidstr[0] == 0) {
+       FILE *fp = fopen(conf_file, "a"); /* append */
+       if (fp == NULL)
+         return -1;
 
-    if(uuidstr[0] == 0) {
         virUUIDGenerate(uuid);
         virUUIDFormat(uuid, uuidstr);
 
-        lseek(fd, 0, SEEK_END);
-        write(fd, "\n#UUID: ", 8);
-        write(fd, uuidstr, strlen(uuidstr));
-        write(fd, "\n", 1);
-        close(fd);
+       /* Record failure if fprintf or fclose fails,
+          and be careful always to close the stream.  */
+       if ((fprintf(fp, "\n#UUID: %s\n", uuidstr) < 0)
+           + (fclose(fp) == EOF))
+           return -1;
     }
 
     return 0;