]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
openvz: Fix regression in config file parsing
authorTaisuke Yamada <tai@rakugaki.org>
Thu, 26 May 2011 17:28:54 +0000 (19:28 +0200)
committerMatthias Bolte <matthias.bolte@googlemail.com>
Thu, 26 May 2011 17:49:18 +0000 (19:49 +0200)
As reported by Diego Blanco in

  https://bugzilla.redhat.com/show_bug.cgi?id=702602

commit f0443765 which replaced openvz_readline to getline(3)
broke OpenVZ driver as it changed semantics of EOF-handling
when parsing OpenVZ configuration.

There're several other issues reported with current OpenVZ driver:

 #1: unclear error message when parsing "CPUS=" line
 #2: openvz driver goes into crashing loop
 #3: "NETIF=" line in configuration is not parsed correctly
 #4: aborts even when optional parameter is missing
 #5: there's a potential memory leak

This updated patch to fix #[145]. This patch does not fix #[23]
as I haven't verified these yet, but this at least got me to run
OpenVZ on libvirt once again.

AUTHORS
src/openvz/openvz_conf.c

diff --git a/AUTHORS b/AUTHORS
index 21ac53efecc97fff5113086d16fe0c2e88cb3d47..6e0276e34c964d3bc1b5650cdedb19915acaca9c 100644 (file)
--- a/AUTHORS
+++ b/AUTHORS
@@ -171,6 +171,7 @@ Patches have also been contributed by:
   Yufang Zhang         <yuzhang@redhat.com>
   Supriya Kannery      <supriyak@in.ibm.com>
   Dirk Herrendoerfer   <d.herrendoerfer@herrendoerfer.name>
+  Taisuke Yamada       <tai@rakugaki.org>
 
   [....send patches to get your name here....]
 
index cfb691745f36e5588c8ef85b07cce436ec630db2..c106b07f63098c889bcf112711b80e9513aadcef 100644 (file)
@@ -642,53 +642,45 @@ openvzWriteVPSConfigParam(int vpsid, const char *param, const char *value)
 /*
  * value will be freed before a new value is assigned to it, the caller is
  * responsible for freeing it afterwards.
+ *
+ * Returns <0 on error, 0 if not found, 1 if found.
  */
 static int
 openvzReadConfigParam(const char *conf_file, const char *param, char **value)
 {
     char *line = NULL;
     size_t line_size = 0;
-    ssize_t ret;
     FILE *fp;
-    int found = 0;
-    char *sf, *token;
-    char *saveptr = NULL;
-
-    value[0] = 0;
+    int err = 0;
+    char *sf, *token, *saveptr = NULL;
 
     fp = fopen(conf_file, "r");
     if (fp == NULL)
         return -1;
 
-    while (1) {
-        ret = getline(&line, &line_size, fp);
-        if (ret <= 0)
-            break;
+    VIR_FREE(*value);
+    while (getline(&line, &line_size, fp) >= 0) {
+        if (! STREQLEN(line, param, strlen(param)))
+            continue;
+
+        sf = line + strlen(param);
+        if (*sf++ != '=') continue;
+
         saveptr = NULL;
-        if (STREQLEN(line, param, strlen(param))) {
-            sf = line;
-            sf += strlen(param);
-            if (sf[0] == '=' && sf[1] != '\0' ) {
-                sf++;
-                if ((token = strtok_r(sf,"\"\t\n", &saveptr)) != NULL) {
-                    VIR_FREE(*value);
-                    *value = strdup(token);
-                    if (value == NULL) {
-                        ret = -1;
-                        break;
-                    }
-                    found = 1;
-                }
+        if ((token = strtok_r(sf, "\"\t\n", &saveptr)) != NULL) {
+            VIR_FREE(*value);
+            *value = strdup(token);
+            if (*value == NULL) {
+                err = 1;
+                break;
             }
-       }
+            /* keep going - last entry wins */
+        }
     }
     VIR_FREE(line);
     VIR_FORCE_FCLOSE(fp);
 
-    if (ret == 0 && found)
-        ret = 1;
-
-    return ret;
+    return err ? -1 : *value ? 1 : 0;
 }
 
 /*