[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

Re: [libvirt] [PATCH 07/16] storage: Covert regex helpers to virCommand



On Tue, May 10, 2011 at 04:07:46PM -0400, Cole Robinson wrote:
> 
> Signed-off-by: Cole Robinson <crobinso redhat com>
> ---
>  src/storage/storage_backend.c |  101 ++++++++++++++---------------------------
>  1 files changed, 35 insertions(+), 66 deletions(-)
> 
> diff --git a/src/storage/storage_backend.c b/src/storage/storage_backend.c
> index 63ba686..5f9ff8f 100644
> --- a/src/storage/storage_backend.c
> +++ b/src/storage/storage_backend.c
> @@ -1363,8 +1363,7 @@ virStorageBackendRunProgRegex(virStoragePoolObjPtr pool,
>                                void *data,
>                                int *outexit)
>  {
> -    int fd = -1, exitstatus, err, failed = 1;
> -    pid_t child = 0;
> +    int fd = -1, err, ret = -1;
>      FILE *list = NULL;
>      regex_t *reg;
>      regmatch_t *vars = NULL;
> @@ -1372,6 +1371,9 @@ virStorageBackendRunProgRegex(virStoragePoolObjPtr pool,
>      int maxReg = 0, i, j;
>      int totgroups = 0, ngroup = 0, maxvars = 0;
>      char **groups;
> +    virCommandPtr cmd = NULL;
> +    const char * const *tmp;
> +    pid_t pid = -1;
>  
>      /* Compile all regular expressions */
>      if (VIR_ALLOC_N(reg, nregex) < 0) {
> @@ -1408,10 +1410,14 @@ virStorageBackendRunProgRegex(virStoragePoolObjPtr pool,
>          goto cleanup;
>      }
>  
> +    cmd = virCommandNew(prog[0]);
> +    tmp = prog;
> +    while (*(++tmp)) {
> +        virCommandAddArg(cmd, *tmp);
> +    }
>  
> -    /* Run the program and capture its output */
> -    if (virExec(prog, NULL, NULL,
> -                &child, -1, &fd, NULL, VIR_EXEC_NONE) < 0) {
> +    virCommandSetOutputFD(cmd, &fd);
> +    if (virCommandRunAsync(cmd, &pid) < 0) {
>          goto cleanup;
>      }
>  
> @@ -1460,9 +1466,11 @@ virStorageBackendRunProgRegex(virStoragePoolObjPtr pool,
>          }
>      }
>  
> -    failed = 0;
> +    ret = 0;
> +cleanup:
> +    if (pid > 0 && virCommandWait(cmd, outexit) < 0)
> +        ret = -1;
>  
> - cleanup:
>      if (groups) {
>          for (j = 0 ; j < totgroups ; j++)
>              VIR_FREE(groups[j]);
> @@ -1478,29 +1486,7 @@ virStorageBackendRunProgRegex(virStoragePoolObjPtr pool,
>      VIR_FORCE_FCLOSE(list);
>      VIR_FORCE_CLOSE(fd);
>  
> -    while ((err = waitpid(child, &exitstatus, 0) == -1) && errno == EINTR);
> -
> -    /* Don't bother checking exit status if we already failed */
> -    if (failed)
> -        return -1;
> -
> -    if (err == -1) {
> -        virReportSystemError(errno,
> -                             _("failed to wait for command '%s'"),
> -                             prog[0]);
> -        return -1;
> -    } else {
> -        if (WIFEXITED(exitstatus)) {
> -            if (outexit != NULL)
> -                *outexit = WEXITSTATUS(exitstatus);
> -        } else {
> -            virStorageReportError(VIR_ERR_INTERNAL_ERROR,
> -                                  "%s", _("command did not exit cleanly"));
> -            return -1;
> -        }
> -    }
> -
> -    return 0;
> +    return ret;
>  }
>  
>  /*
> @@ -1522,13 +1508,14 @@ virStorageBackendRunProgNul(virStoragePoolObjPtr pool,
>                              void *data)
>  {
>      size_t n_tok = 0;
> -    int fd = -1, exitstatus;
> -    pid_t child = 0;
> +    int fd = -1;
>      FILE *fp = NULL;
>      char **v;
> -    int err = -1;
> -    int w_err;
> +    int ret = -1;
>      int i;
> +    const char * const *tmp;
> +    virCommandPtr cmd = NULL;
> +    pid_t pid = -1;
>  
>      if (n_columns == 0)
>          return -1;
> @@ -1540,9 +1527,14 @@ virStorageBackendRunProgNul(virStoragePoolObjPtr pool,
>      for (i = 0; i < n_columns; i++)
>          v[i] = NULL;
>  
> -    /* Run the program and capture its output */
> -    if (virExec(prog, NULL, NULL,
> -                &child, -1, &fd, NULL, VIR_EXEC_NONE) < 0) {
> +    cmd = virCommandNew(prog[0]);
> +    tmp = prog;
> +    while (*(++tmp)) {
> +        virCommandAddArg(cmd, *tmp);
> +    }
> +
> +    virCommandSetOutputFD(cmd, &fd);
> +    if (virCommandRunAsync(cmd, &pid) < 0) {
>          goto cleanup;
>      }
>  
> @@ -1578,47 +1570,24 @@ virStorageBackendRunProgNul(virStoragePoolObjPtr pool,
>      }
>  
>      if (feof (fp))
> -        err = 0;
> +        ret = 0;
>      else
>          virReportSystemError(errno,
>                               _("read error on pipe to '%s'"), prog[0]);
>  
>   cleanup:
> +    if (pid > 0 && virCommandWait(cmd, NULL) < 0)
> +        ret = -1;
> +
>      for (i = 0; i < n_columns; i++)
>          VIR_FREE(v[i]);
>      VIR_FREE(v);
> +    virCommandFree(cmd);
>  
>      VIR_FORCE_FCLOSE(fp);
>      VIR_FORCE_CLOSE(fd);
>  
> -    while ((w_err = waitpid (child, &exitstatus, 0) == -1) && errno == EINTR)
> -        /* empty */ ;
> -
> -    /* Don't bother checking exit status if we already failed */
> -    if (err < 0)
> -        return -1;
> -
> -    if (w_err == -1) {
> -        virReportSystemError(errno,
> -                             _("failed to wait for command '%s'"),
> -                             prog[0]);
> -        return -1;
> -    } else {
> -        if (WIFEXITED(exitstatus)) {
> -            if (WEXITSTATUS(exitstatus) != 0) {
> -                virStorageReportError(VIR_ERR_INTERNAL_ERROR,
> -                                      _("non-zero exit status from command %d"),
> -                                      WEXITSTATUS(exitstatus));
> -                return -1;
> -            }
> -        } else {
> -            virStorageReportError(VIR_ERR_INTERNAL_ERROR,
> -                                  "%s", _("command did not exit cleanly"));
> -            return -1;
> -        }
> -    }
> -
> -    return 0;
> +    return ret;
>  }

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 :|


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]