mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2025-03-07 17:28:15 +00:00
openvz: convert popen to virCommand
popen must be matched with pclose (not fclose), or it will leak resources. Furthermore, it is a lousy interface when it comes to signal handling. We're much better off using our decent command wrapper. Note that virCommand guarantees that VIR_FREE(outbuf) is both required and safe to call, whether virCommandRun succeeded or failed. * src/openvz/openvz_conf.c (openvzLoadDomains, openvzGetVEID): Replace popen with virCommand usage.
This commit is contained in:
parent
cc5e2a849c
commit
bfd6267bcd
@ -51,6 +51,7 @@
|
|||||||
#include "util.h"
|
#include "util.h"
|
||||||
#include "nodeinfo.h"
|
#include "nodeinfo.h"
|
||||||
#include "files.h"
|
#include "files.h"
|
||||||
|
#include "command.h"
|
||||||
|
|
||||||
#define VIR_FROM_THIS VIR_FROM_OPENVZ
|
#define VIR_FROM_THIS VIR_FROM_OPENVZ
|
||||||
|
|
||||||
@ -433,26 +434,26 @@ openvzFreeDriver(struct openvz_driver *driver)
|
|||||||
|
|
||||||
|
|
||||||
int openvzLoadDomains(struct openvz_driver *driver) {
|
int openvzLoadDomains(struct openvz_driver *driver) {
|
||||||
FILE *fp;
|
|
||||||
int veid, ret;
|
int veid, ret;
|
||||||
char status[16];
|
char status[16];
|
||||||
char uuidstr[VIR_UUID_STRING_BUFLEN];
|
char uuidstr[VIR_UUID_STRING_BUFLEN];
|
||||||
virDomainObjPtr dom = NULL;
|
virDomainObjPtr dom = NULL;
|
||||||
char temp[50];
|
char temp[50];
|
||||||
|
char *outbuf = NULL;
|
||||||
|
char *line;
|
||||||
|
virCommandPtr cmd = NULL;
|
||||||
|
|
||||||
if (openvzAssignUUIDs() < 0)
|
if (openvzAssignUUIDs() < 0)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
if ((fp = popen(VZLIST " -a -ovpsid,status -H 2>/dev/null", "r")) == NULL) {
|
cmd = virCommandNewArgList(VZLIST, "-a", "-ovpsid,status", "-H", NULL);
|
||||||
openvzError(VIR_ERR_INTERNAL_ERROR, "%s", _("popen failed"));
|
virCommandSetOutputBuffer(cmd, &outbuf);
|
||||||
return -1;
|
if (virCommandRun(cmd, NULL) < 0)
|
||||||
}
|
goto cleanup;
|
||||||
|
|
||||||
while (!feof(fp)) {
|
|
||||||
if (fscanf(fp, "%d %s\n", &veid, status) != 2) {
|
|
||||||
if (feof(fp))
|
|
||||||
break;
|
|
||||||
|
|
||||||
|
line = *outbuf ? outbuf : NULL;
|
||||||
|
while (line) {
|
||||||
|
if (sscanf(line, "%d %s\n", &veid, status) != 2) {
|
||||||
openvzError(VIR_ERR_INTERNAL_ERROR, "%s",
|
openvzError(VIR_ERR_INTERNAL_ERROR, "%s",
|
||||||
_("Failed to parse vzlist output"));
|
_("Failed to parse vzlist output"));
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
@ -526,9 +527,14 @@ int openvzLoadDomains(struct openvz_driver *driver) {
|
|||||||
|
|
||||||
virDomainObjUnlock(dom);
|
virDomainObjUnlock(dom);
|
||||||
dom = NULL;
|
dom = NULL;
|
||||||
|
|
||||||
|
line = strchr(line, '\n');
|
||||||
|
if (line)
|
||||||
|
line++;
|
||||||
}
|
}
|
||||||
|
|
||||||
VIR_FORCE_FCLOSE(fp);
|
virCommandFree(cmd);
|
||||||
|
VIR_FREE(outbuf);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
@ -536,7 +542,8 @@ int openvzLoadDomains(struct openvz_driver *driver) {
|
|||||||
virReportOOMError();
|
virReportOOMError();
|
||||||
|
|
||||||
cleanup:
|
cleanup:
|
||||||
VIR_FORCE_FCLOSE(fp);
|
virCommandFree(cmd);
|
||||||
|
VIR_FREE(outbuf);
|
||||||
if (dom)
|
if (dom)
|
||||||
virDomainObjUnref(dom);
|
virDomainObjUnref(dom);
|
||||||
return -1;
|
return -1;
|
||||||
@ -978,27 +985,23 @@ static int openvzAssignUUIDs(void)
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
int openvzGetVEID(const char *name) {
|
int openvzGetVEID(const char *name) {
|
||||||
char *cmd;
|
virCommandPtr cmd;
|
||||||
|
char *outbuf;
|
||||||
int veid;
|
int veid;
|
||||||
FILE *fp;
|
|
||||||
bool ok;
|
bool ok;
|
||||||
|
|
||||||
if (virAsprintf(&cmd, "%s %s -ovpsid -H", VZLIST, name) < 0) {
|
cmd = virCommandNewArgList(VZLIST, name, "-ovpsid", "-H", NULL);
|
||||||
openvzError(VIR_ERR_INTERNAL_ERROR, "%s",
|
virCommandSetOutputBuffer(cmd, &outbuf);
|
||||||
_("virAsprintf failed"));
|
if (virCommandRun(cmd, NULL) < 0) {
|
||||||
|
virCommandFree(cmd);
|
||||||
|
VIR_FREE(outbuf);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
fp = popen(cmd, "r");
|
virCommandFree(cmd);
|
||||||
VIR_FREE(cmd);
|
ok = sscanf(outbuf, "%d\n", &veid) == 1;
|
||||||
|
VIR_FREE(outbuf);
|
||||||
|
|
||||||
if (fp == NULL) {
|
|
||||||
openvzError(VIR_ERR_INTERNAL_ERROR, "%s", _("popen failed"));
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
ok = fscanf(fp, "%d\n", &veid ) == 1;
|
|
||||||
VIR_FORCE_FCLOSE(fp);
|
|
||||||
if (ok && veid >= 0)
|
if (ok && veid >= 0)
|
||||||
return veid;
|
return veid;
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user