From 915b86d29bcd7b4c4eb0cfb1297551b4be4f23ea Mon Sep 17 00:00:00 2001 From: Daniel Veillard Date: Fri, 11 Jul 2008 08:56:16 +0000 Subject: [PATCH] cleanup OpenVZ config file accesses * src/openvz_conf.c: cleanup OpenVZ config file accesses, patch from Evgeniy Sokolov Daniel --- ChangeLog | 5 +++++ src/openvz_conf.c | 49 +++++++++++++++++++++++++++-------------------- 2 files changed, 33 insertions(+), 21 deletions(-) diff --git a/ChangeLog b/ChangeLog index 1514e2196c..eede5f37d4 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +Fri Jul 11 10:53:59 CEST 2008 Daniel Veillard + + * src/openvz_conf.c: cleanup OpenVZ config file accesses, patch + from Evgeniy Sokolov + Thu Jul 10 17:28:19 CEST 2008 Daniel Veillard * src/libvirt.c: Evgeniy Sokolov found a driver open bug diff --git a/src/openvz_conf.c b/src/openvz_conf.c index c0e9d9ef4c..8b920e00e4 100644 --- a/src/openvz_conf.c +++ b/src/openvz_conf.c @@ -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) + if (openvzLocateConfFile(vpsid, conf_file, PATH_MAX)<0) return -1; - if (snprintf(conf_file, PATH_MAX, "%s/%d.conf", conf_dir,vpsid) >= PATH_MAX) - 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;