]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
cleanup OpenVZ config file accesses
authorDaniel Veillard <veillard@redhat.com>
Fri, 11 Jul 2008 08:56:16 +0000 (08:56 +0000)
committerDaniel Veillard <veillard@redhat.com>
Fri, 11 Jul 2008 08:56:16 +0000 (08:56 +0000)
* src/openvz_conf.c: cleanup OpenVZ config file accesses, patch
  from Evgeniy Sokolov
Daniel

ChangeLog
src/openvz_conf.c

index 1514e2196c4ce13ed321785b8cd92a6ddbb418f0..eede5f37d47eedb5484e7d91831079afd7c876ce 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+Fri Jul 11 10:53:59 CEST 2008 Daniel Veillard <veillard@redhat.com>
+
+       * src/openvz_conf.c: cleanup OpenVZ config file accesses, patch
+         from Evgeniy Sokolov
+
 Thu Jul 10 17:28:19 CEST 2008 Daniel Veillard <veillard@redhat.com>
 
        * src/libvirt.c: Evgeniy Sokolov found a driver open bug
index c0e9d9ef4cf25320fd1229f329a828419f2e3471..8b920e00e4fff08b17593b32af06c38afdc83d9e 100644 (file)
@@ -60,6 +60,7 @@ static char *openvzLocateConfDir(void);
 static struct openvz_vm_def *openvzParseXML(virConnectPtr conn, xmlDocPtr xml);
 static int openvzGetVPSUUID(int vpsid, char *uuidstr);
 static int openvzSetUUID(int vpsid);
+static int openvzLocateConfFile(int vpsid, char *conffile, int maxlen);
 
 void
 openvzError (virConnectPtr conn, virErrorNumber code, const char *fmt, ...)
@@ -595,20 +596,13 @@ openvzReadConfigParam(int vpsid ,const char * param, char *value, int maxlen)
     char conf_file[PATH_MAX] ;
     char line[PATH_MAX] ;
     int ret, found = 0;
-    char * conf_dir;
     int fd ;
     char * sf, * token;
     char *saveptr = NULL;
 
-    conf_dir = openvzLocateConfDir();
-    if (conf_dir == NULL)
-        return -1;
-
-    if (snprintf(conf_file, PATH_MAX, "%s/%d.conf", conf_dir,vpsid) >= PATH_MAX)
+    if (openvzLocateConfFile(vpsid, conf_file, PATH_MAX)<0)
         return -1;
 
-    VIR_FREE(conf_dir);
-
     value[0] = 0;
 
     fd = open(conf_file, O_RDONLY);
@@ -638,6 +632,27 @@ openvzReadConfigParam(int vpsid ,const char * param, char *value, int maxlen)
     return ret ;
 }
 
+/* Locate config file of container
+* return -1 - error
+*         0 - OK
+*/
+static int
+openvzLocateConfFile(int vpsid, char *conffile, int maxlen)
+{
+    char * confdir;
+    int ret = 0;
+
+    confdir = openvzLocateConfDir();
+    if (confdir == NULL)
+        return -1;
+
+    if (snprintf(conffile, maxlen, "%s/%d.conf", confdir, vpsid) >= maxlen)
+        ret = -1;
+
+    VIR_FREE(confdir);
+    return ret;
+}
+
 static char
 *openvzLocateConfDir(void)
 {
@@ -686,16 +701,12 @@ openvzGetVPSUUID(int vpsid, char *uuidstr)
     char line[1024];
     char uuidbuf[1024];
     char iden[1024];
-    char *conf_dir;
     int fd, ret;
 
-    conf_dir = openvzLocateConfDir();
-    if (conf_dir == NULL)
-        return -1;
-    sprintf(conf_file, "%s/%d.conf", conf_dir, vpsid);
-    VIR_FREE(conf_dir);
+   if (openvzLocateConfFile(vpsid, conf_file, PATH_MAX)<0)
+       return -1;
 
-    fd = open(conf_file, O_RDWR);
+    fd = open(conf_file, O_RDONLY);
     if(fd == -1)
         return -1;
 
@@ -730,13 +741,9 @@ openvzSetUUID(int vpsid)
     char conf_file[PATH_MAX];
     char uuidstr[VIR_UUID_STRING_BUFLEN];
     unsigned char uuid[VIR_UUID_BUFLEN];
-    char *conf_dir;
 
-    conf_dir = openvzLocateConfDir();
-    if (conf_dir == NULL)
-        return -1;
-    sprintf(conf_file, "%s/%d.conf", conf_dir, vpsid);
-    VIR_FREE(conf_dir);
+   if (openvzLocateConfFile(vpsid, conf_file, PATH_MAX)<0)
+       return -1;
 
     if (openvzGetVPSUUID(vpsid, uuidstr))
         return -1;