qemu_conf.c: use new function, virFileReadLimFD

* src/qemu_conf.c (qemudExtractVersionInfo): Use virFileReadLimFD
and VIR_FREE in place of an open-coded loop and a static buffer.
This commit is contained in:
Jim Meyering 2008-09-02 10:30:40 +00:00
parent d2ac46f2b0
commit 81cb09f9fe
2 changed files with 11 additions and 14 deletions

View File

@ -1,5 +1,9 @@
Tue Sep 2 12:28:54 CEST 2008 Jim Meyering <meyering@redhat.com> Tue Sep 2 12:28:54 CEST 2008 Jim Meyering <meyering@redhat.com>
qemu_conf.c: use new function, virFileReadLimFD
* src/qemu_conf.c (qemudExtractVersionInfo): Use virFileReadLimFD
and VIR_FREE in place of an open-coded loop and a static buffer.
util.c: add a file-descriptor-based wrapper for fread_file_lim util.c: add a file-descriptor-based wrapper for fread_file_lim
* src/util.c (virFileReadLimFP): New function. * src/util.c (virFileReadLimFP): New function.
(__virFileReadLimFD): New function. (__virFileReadLimFD): New function.

View File

@ -401,8 +401,7 @@ int qemudExtractVersionInfo(const char *qemu,
const char *const qemuenv[] = { "LC_ALL=C", NULL }; const char *const qemuenv[] = { "LC_ALL=C", NULL };
pid_t child; pid_t child;
int newstdout = -1; int newstdout = -1;
char help[8192]; /* Ought to be enough to hold QEMU help screen */ int ret = -1, status;
int got = 0, ret = -1, status;
unsigned int major, minor, micro; unsigned int major, minor, micro;
unsigned int version; unsigned int version;
unsigned int flags = 0; unsigned int flags = 0;
@ -416,16 +415,11 @@ int qemudExtractVersionInfo(const char *qemu,
&child, -1, &newstdout, NULL, VIR_EXEC_NONE) < 0) &child, -1, &newstdout, NULL, VIR_EXEC_NONE) < 0)
return -1; return -1;
char *help = NULL;
while (got < (sizeof(help)-1)) { enum { MAX_HELP_OUTPUT_SIZE = 8192 };
int len; int len = virFileReadLimFD(newstdout, MAX_HELP_OUTPUT_SIZE, &help);
if ((len = saferead(newstdout, help+got, sizeof(help)-got-1)) < 0) if (len < 0)
goto cleanup2; goto cleanup2;
if (!len)
break;
got += len;
}
help[got] = '\0';
if (sscanf(help, "QEMU PC emulator version %u.%u.%u", if (sscanf(help, "QEMU PC emulator version %u.%u.%u",
&major, &minor, &micro) != 3) { &major, &minor, &micro) != 3) {
@ -447,7 +441,6 @@ int qemudExtractVersionInfo(const char *qemu,
if (version >= 9000) if (version >= 9000)
flags |= QEMUD_CMD_FLAG_VNC_COLON; flags |= QEMUD_CMD_FLAG_VNC_COLON;
if (retversion) if (retversion)
*retversion = version; *retversion = version;
if (retflags) if (retflags)
@ -459,6 +452,7 @@ int qemudExtractVersionInfo(const char *qemu,
major, minor, micro, version, flags); major, minor, micro, version, flags);
cleanup2: cleanup2:
VIR_FREE(help);
if (close(newstdout) < 0) if (close(newstdout) < 0)
ret = -1; ret = -1;
@ -1229,4 +1223,3 @@ int qemudBuildCommandLine(virConnectPtr conn,
#undef ADD_ARG_LIT #undef ADD_ARG_LIT
#undef ADD_ARG_SPACE #undef ADD_ARG_SPACE
} }