From 6a9feaf50079e7f15288c52a27ab0d381482e388 Mon Sep 17 00:00:00 2001 From: Enrico Scholz Date: Tue, 25 Oct 2005 16:22:33 +0000 Subject: [PATCH] getCtxFromFile(): fixed resource leaks in errorcase vc_getVserverCtx(): validate result by calling getvserverbyctx() git-svn-id: http://svn.linux-vserver.org/svn/util-vserver/trunk@2184 94cd875c-1c1d-0410-91d2-eb244daf1a30 --- util-vserver/lib/getvserverctx.c | 45 +++++++++++++++++++++++++++++++++------- 1 file changed, 37 insertions(+), 8 deletions(-) diff --git a/util-vserver/lib/getvserverctx.c b/util-vserver/lib/getvserverctx.c index cf7a4f9..6a0e2c1 100644 --- a/util-vserver/lib/getvserverctx.c +++ b/util-vserver/lib/getvserverctx.c @@ -92,23 +92,28 @@ getCtxFromFile(char const *pathname) fd = open(pathname, O_RDONLY); - if (fd==-1 || - (len=lseek(fd, 0, SEEK_END))==-1 || + if (fd==-1) return VC_NOCTX; + if ((len=lseek(fd, 0, SEEK_END))==-1 || (len>50) || - (lseek(fd, 0, SEEK_SET)==-1)) + (lseek(fd, 0, SEEK_SET)==-1)) { + close(fd); return VC_NOCTX; + } { char buf[len+1]; char *errptr; xid_t res; - if (TEMP_FAILURE_RETRY(read(fd, buf, len+1))!=len) - return VC_NOCTX; + if (TEMP_FAILURE_RETRY(read(fd, buf, len+1))!=len) res = VC_NOCTX; + else { + buf[len] = '\0'; - res = strtol(buf, &errptr, 10); - if (*errptr!='\0' && *errptr!='\n') return VC_NOCTX; + res = strtol(buf, &errptr, 10); + if (*errptr!='\0' && *errptr!='\n') res = VC_NOCTX; + } + close(fd); return res; } } @@ -141,7 +146,31 @@ vc_getVserverCtx(char const *id, vcCfgStyle style, bool honor_static, bool *is_r memcpy(buf+idx, "/run", 5); // appends '\0' too res = getCtxFromFile(buf); - if (is_running) *is_running = res!=VC_NOCTX; + + // when context information could be read, we have to verify that + // it belongs to a running vserver and the both vservers are + // identically + if (res!=VC_NOCTX) { + char *cur_name; + struct vc_vx_info info; + + // determine the vserver which is associated with the xid + // resp. skip this step when the context does not exist + cur_name = (vc_get_vx_info(res, &info)!=-1 ? + vc_getVserverByCtx_Internal(res, &style, 0, false) : 0); + + buf[idx] = '\0'; // cut off the '/run' from the vserver name + res = ((cur_name!=0 && + vc_compareVserverById(buf, vcCFG_RECENT_FULL, + cur_name, vcCFG_RECENT_FULL)==0) + ? res + : VC_NOCTX); // correct the value of 'res' + + free(cur_name); + } + + if (is_running) // fill 'is_running' information... + *is_running = res!=VC_NOCTX; if (res==VC_NOCTX && honor_static) { memcpy(buf+idx, "/context", 9); // appends '\0' too -- 1.8.1.5