[libvirt] [PATCH 10/16] openvz: Convert virExec usage to virCommand

Daniel P. Berrange berrange at redhat.com
Wed May 11 10:33:05 UTC 2011


On Tue, May 10, 2011 at 04:07:49PM -0400, Cole Robinson wrote:
> 
> Signed-off-by: Cole Robinson <crobinso at redhat.com>
> ---
>  src/openvz/openvz_conf.c   |   37 ++++++++++---------------------
>  src/openvz/openvz_driver.c |   52 ++++++++++++++++++++++++-------------------
>  2 files changed, 41 insertions(+), 48 deletions(-)
> 
> diff --git a/src/openvz/openvz_conf.c b/src/openvz/openvz_conf.c
> index 88cd4c8..239cd48 100644
> --- a/src/openvz/openvz_conf.c
> +++ b/src/openvz/openvz_conf.c
> @@ -74,53 +74,40 @@ strtoI(const char *str)
>  
>  
>  static int
> -openvzExtractVersionInfo(const char *cmd, int *retversion)
> +openvzExtractVersionInfo(const char *cmdstr, int *retversion)
>  {
> -    const char *const vzarg[] = { cmd, "--help", NULL };
> -    const char *const vzenv[] = { "LC_ALL=C", NULL };
> -    pid_t child;
> -    int newstdout = -1;
> -    int ret = -1, status;
> +    int ret = -1;
>      unsigned long version;
> +    char *help = NULL;
>      char *tmp;
> +    virCommandPtr cmd = virCommandNewArgList(cmdstr, "--help", NULL);
>  
>      if (retversion)
>          *retversion = 0;
>  
> -    if (virExec(vzarg, vzenv, NULL,
> -                &child, -1, &newstdout, NULL, VIR_EXEC_NONE) < 0)
> -        return -1;
> +    virCommandAddEnvString(cmd, "LC_ALL=C");
> +    virCommandSetOutputBuffer(cmd, &help);
>  
> -    char *help = NULL;
> -    int len = virFileReadLimFD(newstdout, 4096, &help);
> -    if (len < 0)
> -        goto cleanup2;
> +    if (virCommandRun(cmd, NULL) < 0)
> +        goto cleanup;
>  
>      tmp = help;
>  
>      /* expected format: vzctl version <major>.<minor>.<micro> */
>      if ((tmp = STRSKIP(tmp, "vzctl version ")) == NULL)
> -        goto cleanup2;
> +        goto cleanup;
>  
>      if (virParseVersionString(tmp, &version) < 0)
> -        goto cleanup2;
> +        goto cleanup;
>  
>      if (retversion)
>          *retversion = version;
>  
>      ret = 0;
>  
> -cleanup2:
> +cleanup:
> +    virCommandFree(cmd);
>      VIR_FREE(help);
> -    if (VIR_CLOSE(newstdout) < 0)
> -        ret = -1;
> -
> -rewait:
> -    if (waitpid(child, &status, 0) != child) {
> -        if (errno == EINTR)
> -            goto rewait;
> -        ret = -1;
> -    }
>  
>      return ret;
>  }
> diff --git a/src/openvz/openvz_driver.c b/src/openvz/openvz_driver.c
> index f5fae2d..d65f618 100644
> --- a/src/openvz/openvz_driver.c
> +++ b/src/openvz/openvz_driver.c
> @@ -59,6 +59,7 @@
>  #include "bridge.h"
>  #include "files.h"
>  #include "logging.h"
> +#include "command.h"
>  
>  #define VIR_FROM_THIS VIR_FROM_OPENVZ
>  
> @@ -1379,19 +1380,15 @@ static int openvzListDomains(virConnectPtr conn ATTRIBUTE_UNUSED,
>      int veid;
>      pid_t pid;
>      int outfd = -1;
> +    int rc = -1;
>      int ret;
>      char buf[32];
>      char *endptr;
> -    const char *cmd[] = {VZLIST, "-ovpsid", "-H" , NULL};
> -
> -    ret = virExec(cmd, NULL, NULL,
> -                  &pid, -1, &outfd, NULL, VIR_EXEC_NONE);
> -    if (ret == -1) {
> -        openvzError(VIR_ERR_INTERNAL_ERROR,
> -                    _("Could not exec %s"), VZLIST);
> -        VIR_FORCE_CLOSE(outfd);
> -        return -1;
> -    }
> +    virCommandPtr cmd = virCommandNewArgList(VZLIST, "-ovpsid", "-H" , NULL);
> +
> +    virCommandSetOutputFD(cmd, &outfd);
> +    if (virCommandRunAsync(cmd, &pid) < 0)
> +        goto cleanup;
>  
>      while (got < nids) {
>          ret = openvz_readline(outfd, buf, 32);
> @@ -1405,13 +1402,20 @@ static int openvzListDomains(virConnectPtr conn ATTRIBUTE_UNUSED,
>          ids[got] = veid;
>          got ++;
>      }
> -    waitpid(pid, NULL, 0);
> +
> +    if (virCommandWait(cmd, NULL) < 0)
> +        goto cleanup;
>  
>      if (VIR_CLOSE(outfd) < 0) {
>          virReportSystemError(errno, "%s", _("failed to close file"));
> -        return -1;
> +        goto cleanup;
>      }
> -    return got;
> +
> +    rc = got;
> +cleanup:
> +    VIR_FORCE_CLOSE(outfd);
> +    virCommandFree(cmd);
> +    return rc;
>  }
>  
>  static int openvzNumDomains(virConnectPtr conn) {
> @@ -1429,20 +1433,18 @@ static int openvzListDefinedDomains(virConnectPtr conn ATTRIBUTE_UNUSED,
>                                      char **const names, int nnames) {
>      int got = 0;
>      int veid, outfd = -1, ret;
> +    int rc = -1;
>      pid_t pid;
>      char vpsname[32];
>      char buf[32];
>      char *endptr;
> -    const char *cmd[] = {VZLIST, "-ovpsid", "-H", "-S", NULL};
> +    virCommandPtr cmd = virCommandNewArgList(VZLIST,
> +                                             "-ovpsid", "-H", "-S", NULL);
>  
>      /* the -S options lists only stopped domains */
> -    ret = virExec(cmd, NULL, NULL,
> -                  &pid, -1, &outfd, NULL, VIR_EXEC_NONE);
> -    if (ret == -1) {
> -        openvzError(VIR_ERR_INTERNAL_ERROR,
> -                    _("Could not exec %s"), VZLIST);
> +    virCommandSetOutputFD(cmd, &outfd);
> +    if (virCommandRunAsync(cmd, &pid) < 0)
>          goto out;
> -    }
>  
>      while (got < nnames) {
>          ret = openvz_readline(outfd, buf, 32);
> @@ -1460,18 +1462,22 @@ static int openvzListDefinedDomains(virConnectPtr conn ATTRIBUTE_UNUSED,
>          }
>          got ++;
>      }
> -    waitpid(pid, NULL, 0);
> +
> +    if (virCommandWait(cmd, NULL) < 0)
> +        goto out;
> +
>      if (VIR_CLOSE(outfd) < 0) {
>          virReportSystemError(errno, "%s", _("failed to close file"));
>          goto out;
>      }
> -    return got;
>  
> +    rc = got;
>  out:
>      VIR_FORCE_CLOSE(outfd);
> +    virCommandFree(cmd);
>      for ( ; got >= 0 ; got--)
>          VIR_FREE(names[got]);
> -    return -1;
> +    return rc;
>  }
>  
>  static int openvzGetProcessInfo(unsigned long long *cpuTime, int vpsid)

ACK

Daniel
-- 
|: http://berrange.com      -o-    http://www.flickr.com/photos/dberrange/ :|
|: http://libvirt.org              -o-             http://virt-manager.org :|
|: http://autobuild.org       -o-         http://search.cpan.org/~danberr/ :|
|: http://entangle-photo.org       -o-       http://live.gnome.org/gtk-vnc :|




More information about the libvir-list mailing list