mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2024-12-23 22:25:25 +00:00
storage: Covert regex helpers to virCommand
v2: Simplify command creation Add a missing virCommandFree Use virCommand auto-cleanup for async process
This commit is contained in:
parent
0c0ed675de
commit
be1965be66
@ -1404,8 +1404,7 @@ virStorageBackendRunProgRegex(virStoragePoolObjPtr pool,
|
|||||||
void *data,
|
void *data,
|
||||||
int *outexit)
|
int *outexit)
|
||||||
{
|
{
|
||||||
int fd = -1, exitstatus, err, failed = 1;
|
int fd = -1, err, ret = -1;
|
||||||
pid_t child = 0;
|
|
||||||
FILE *list = NULL;
|
FILE *list = NULL;
|
||||||
regex_t *reg;
|
regex_t *reg;
|
||||||
regmatch_t *vars = NULL;
|
regmatch_t *vars = NULL;
|
||||||
@ -1413,6 +1412,7 @@ virStorageBackendRunProgRegex(virStoragePoolObjPtr pool,
|
|||||||
int maxReg = 0, i, j;
|
int maxReg = 0, i, j;
|
||||||
int totgroups = 0, ngroup = 0, maxvars = 0;
|
int totgroups = 0, ngroup = 0, maxvars = 0;
|
||||||
char **groups;
|
char **groups;
|
||||||
|
virCommandPtr cmd = NULL;
|
||||||
|
|
||||||
/* Compile all regular expressions */
|
/* Compile all regular expressions */
|
||||||
if (VIR_ALLOC_N(reg, nregex) < 0) {
|
if (VIR_ALLOC_N(reg, nregex) < 0) {
|
||||||
@ -1449,10 +1449,9 @@ virStorageBackendRunProgRegex(virStoragePoolObjPtr pool,
|
|||||||
goto cleanup;
|
goto cleanup;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
cmd = virCommandNewArgs(prog);
|
||||||
/* Run the program and capture its output */
|
virCommandSetOutputFD(cmd, &fd);
|
||||||
if (virExec(prog, NULL, NULL,
|
if (virCommandRunAsync(cmd, NULL) < 0) {
|
||||||
&child, -1, &fd, NULL, VIR_EXEC_NONE) < 0) {
|
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1501,8 +1500,7 @@ virStorageBackendRunProgRegex(virStoragePoolObjPtr pool,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
failed = 0;
|
ret = virCommandWait(cmd, outexit);
|
||||||
|
|
||||||
cleanup:
|
cleanup:
|
||||||
if (groups) {
|
if (groups) {
|
||||||
for (j = 0 ; j < totgroups ; j++)
|
for (j = 0 ; j < totgroups ; j++)
|
||||||
@ -1515,33 +1513,12 @@ virStorageBackendRunProgRegex(virStoragePoolObjPtr pool,
|
|||||||
regfree(®[i]);
|
regfree(®[i]);
|
||||||
|
|
||||||
VIR_FREE(reg);
|
VIR_FREE(reg);
|
||||||
|
virCommandFree(cmd);
|
||||||
|
|
||||||
VIR_FORCE_FCLOSE(list);
|
VIR_FORCE_FCLOSE(list);
|
||||||
VIR_FORCE_CLOSE(fd);
|
VIR_FORCE_CLOSE(fd);
|
||||||
|
|
||||||
while ((err = waitpid(child, &exitstatus, 0) == -1) && errno == EINTR);
|
return ret;
|
||||||
|
|
||||||
/* 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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -1563,13 +1540,12 @@ virStorageBackendRunProgNul(virStoragePoolObjPtr pool,
|
|||||||
void *data)
|
void *data)
|
||||||
{
|
{
|
||||||
size_t n_tok = 0;
|
size_t n_tok = 0;
|
||||||
int fd = -1, exitstatus;
|
int fd = -1;
|
||||||
pid_t child = 0;
|
|
||||||
FILE *fp = NULL;
|
FILE *fp = NULL;
|
||||||
char **v;
|
char **v;
|
||||||
int err = -1;
|
int ret = -1;
|
||||||
int w_err;
|
|
||||||
int i;
|
int i;
|
||||||
|
virCommandPtr cmd = NULL;
|
||||||
|
|
||||||
if (n_columns == 0)
|
if (n_columns == 0)
|
||||||
return -1;
|
return -1;
|
||||||
@ -1581,9 +1557,9 @@ virStorageBackendRunProgNul(virStoragePoolObjPtr pool,
|
|||||||
for (i = 0; i < n_columns; i++)
|
for (i = 0; i < n_columns; i++)
|
||||||
v[i] = NULL;
|
v[i] = NULL;
|
||||||
|
|
||||||
/* Run the program and capture its output */
|
cmd = virCommandNewArgs(prog);
|
||||||
if (virExec(prog, NULL, NULL,
|
virCommandSetOutputFD(cmd, &fd);
|
||||||
&child, -1, &fd, NULL, VIR_EXEC_NONE) < 0) {
|
if (virCommandRunAsync(cmd, NULL) < 0) {
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1618,48 +1594,23 @@ virStorageBackendRunProgNul(virStoragePoolObjPtr pool,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (feof (fp))
|
if (feof (fp) < 0) {
|
||||||
err = 0;
|
|
||||||
else
|
|
||||||
virReportSystemError(errno,
|
virReportSystemError(errno,
|
||||||
_("read error on pipe to '%s'"), prog[0]);
|
_("read error on pipe to '%s'"), prog[0]);
|
||||||
|
goto cleanup;
|
||||||
|
}
|
||||||
|
|
||||||
|
ret = virCommandWait(cmd, NULL);
|
||||||
cleanup:
|
cleanup:
|
||||||
for (i = 0; i < n_columns; i++)
|
for (i = 0; i < n_columns; i++)
|
||||||
VIR_FREE(v[i]);
|
VIR_FREE(v[i]);
|
||||||
VIR_FREE(v);
|
VIR_FREE(v);
|
||||||
|
virCommandFree(cmd);
|
||||||
|
|
||||||
VIR_FORCE_FCLOSE(fp);
|
VIR_FORCE_FCLOSE(fp);
|
||||||
VIR_FORCE_CLOSE(fd);
|
VIR_FORCE_CLOSE(fd);
|
||||||
|
|
||||||
while ((w_err = waitpid (child, &exitstatus, 0) == -1) && errno == EINTR)
|
return ret;
|
||||||
/* 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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#else /* WIN32 */
|
#else /* WIN32 */
|
||||||
|
Loading…
Reference in New Issue
Block a user