[libvirt] [PATCHv2 2/2] sysinfo: convert to virCommand

Matthias Bolte matthias.bolte at googlemail.com
Mon Dec 13 23:10:00 UTC 2010


2010/12/7 Eric Blake <eblake at redhat.com>:
> * src/util/sysinfo.c (virSysinfoRead): Use virCommand instead of
> virExec.
> ---
> v2: remove unused variable, rebase on top of whitespace cleanups.
>
> This patch is awaiting review on these prerequisites:
> https://www.redhat.com/archives/libvir-list/2010-December/msg00318.html
> https://www.redhat.com/archives/libvir-list/2010-December/msg00321.html
>

>     path = virFindFileInPath(SYSINFO_SMBIOS_DECODER);
>     if (path == NULL) {
> @@ -108,43 +106,13 @@ virSysinfoRead(void) {
>                              SYSINFO_SMBIOS_DECODER);
>         return NULL;
>     }
> -    argv[0] = path;
>
> -    res = virExec(argv, NULL, NULL, &pid, -1, &outfd, &errfd,
> -                  VIR_EXEC_NONE | VIR_EXEC_NONBLOCK);
> -    if (res < 0) {
> +    cmd = virCommandNewArgList(path, "-q", "-t", "0,1", NULL);
> +    virCommandSetOutputBuffer(cmd, &outbuf);
> +    if (virCommandRun(cmd, NULL) < 0) {
>         virSmbiosReportError(VIR_ERR_INTERNAL_ERROR,
>                              _("Failed to execute command %s"),
>                              path);
> -        res = 1;
> -        goto cleanup;
> -    }
> -
> -    /*
> -     * we are interested in the output, capture it and errors too
> -     */
> -    if (virPipeReadUntilEOF(outfd, errfd, &outbuf, &errbuf) < 0) {
> -        virReportSystemError(errno, _("cannot wait for '%s'"), path);
> -        while (waitpid(pid, &exitstatus, 0) == -1 && errno == EINTR)
> -            ;
> -        goto cleanup;
> -    }
> -
> -    if (outbuf)
> -        VIR_DEBUG("Command stdout: %s", outbuf);
> -    if (errbuf)
> -        VIR_DEBUG("Command stderr: %s", errbuf);
> -
> -    while ((waitret = waitpid(pid, &exitstatus, 0) == -1) &&
> -           (errno == EINTR));
> -    if (waitret == -1) {
> -        virReportSystemError(errno, _("Failed to wait for '%s'"), path);
> -        goto cleanup;
> -    }
> -    if (exitstatus != 0) {
> -        virSmbiosReportError(VIR_ERR_INTERNAL_ERROR,
> -                             _("command %s failed with error code %d:%s"),
> -                             path, exitstatus, errbuf);
>         goto cleanup;
>     }
>
> @@ -227,8 +195,7 @@ virSysinfoRead(void) {
>
>  cleanup:
>     VIR_FREE(outbuf);
> -    VIR_FREE(errbuf);
> -    VIR_FREE(path);
> +    virCommandFree(cmd);

Why not free path anymore, doesn't it leak now?

ACK, with that possible leak addressed.

Matthias




More information about the libvir-list mailing list