mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2024-12-23 14:15:28 +00:00
util: command: use VIR_AUTOFREE instead of VIR_FREE for scalar types
By making use of GNU C's cleanup attribute handled by the VIR_AUTOFREE macro for declaring scalar variables, majority of the VIR_FREE calls can be dropped, which in turn leads to getting rid of most of our cleanup sections. Signed-off-by: Sukrit Bhatnagar <skrtbhtngr@gmail.com> Reviewed-by: Erik Skultety <eskultet@redhat.com>
This commit is contained in:
parent
7e34375892
commit
46a1f0bb64
@ -507,11 +507,11 @@ virExec(virCommandPtr cmd)
|
|||||||
int childout = -1;
|
int childout = -1;
|
||||||
int childerr = -1;
|
int childerr = -1;
|
||||||
int tmpfd;
|
int tmpfd;
|
||||||
char *binarystr = NULL;
|
VIR_AUTOFREE(char *) binarystr = NULL;
|
||||||
const char *binary = NULL;
|
const char *binary = NULL;
|
||||||
int ret;
|
int ret;
|
||||||
struct sigaction waxon, waxoff;
|
struct sigaction waxon, waxoff;
|
||||||
gid_t *groups = NULL;
|
VIR_AUTOFREE(gid_t *) groups = NULL;
|
||||||
int ngroups;
|
int ngroups;
|
||||||
|
|
||||||
if (cmd->args[0][0] != '/') {
|
if (cmd->args[0][0] != '/') {
|
||||||
@ -604,9 +604,6 @@ virExec(virCommandPtr cmd)
|
|||||||
|
|
||||||
cmd->pid = pid;
|
cmd->pid = pid;
|
||||||
|
|
||||||
VIR_FREE(groups);
|
|
||||||
VIR_FREE(binarystr);
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -796,9 +793,6 @@ virExec(virCommandPtr cmd)
|
|||||||
/* This is cleanup of parent process only - child
|
/* This is cleanup of parent process only - child
|
||||||
should never jump here on error */
|
should never jump here on error */
|
||||||
|
|
||||||
VIR_FREE(binarystr);
|
|
||||||
VIR_FREE(groups);
|
|
||||||
|
|
||||||
/* NB we don't virReportError() on any failures here
|
/* NB we don't virReportError() on any failures here
|
||||||
because the code which jumped here already raised
|
because the code which jumped here already raised
|
||||||
an error condition which we must not overwrite */
|
an error condition which we must not overwrite */
|
||||||
@ -2386,7 +2380,7 @@ int
|
|||||||
virCommandRunAsync(virCommandPtr cmd, pid_t *pid)
|
virCommandRunAsync(virCommandPtr cmd, pid_t *pid)
|
||||||
{
|
{
|
||||||
int ret = -1;
|
int ret = -1;
|
||||||
char *str = NULL;
|
VIR_AUTOFREE(char *) str = NULL;
|
||||||
size_t i;
|
size_t i;
|
||||||
bool synchronous = false;
|
bool synchronous = false;
|
||||||
int infd[2] = {-1, -1};
|
int infd[2] = {-1, -1};
|
||||||
@ -2511,7 +2505,6 @@ virCommandRunAsync(virCommandPtr cmd, pid_t *pid)
|
|||||||
VIR_FORCE_CLOSE(cmd->infd);
|
VIR_FORCE_CLOSE(cmd->infd);
|
||||||
VIR_FORCE_CLOSE(cmd->inpipe);
|
VIR_FORCE_CLOSE(cmd->inpipe);
|
||||||
}
|
}
|
||||||
VIR_FREE(str);
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -2588,8 +2581,8 @@ virCommandWait(virCommandPtr cmd, int *exitstatus)
|
|||||||
if (exitstatus && (cmd->rawStatus || WIFEXITED(status))) {
|
if (exitstatus && (cmd->rawStatus || WIFEXITED(status))) {
|
||||||
*exitstatus = cmd->rawStatus ? status : WEXITSTATUS(status);
|
*exitstatus = cmd->rawStatus ? status : WEXITSTATUS(status);
|
||||||
} else if (status) {
|
} else if (status) {
|
||||||
char *str = virCommandToString(cmd);
|
VIR_AUTOFREE(char *) str = virCommandToString(cmd);
|
||||||
char *st = virProcessTranslateStatus(status);
|
VIR_AUTOFREE(char *) st = virProcessTranslateStatus(status);
|
||||||
bool haveErrMsg = cmd->errbuf && *cmd->errbuf && (*cmd->errbuf)[0];
|
bool haveErrMsg = cmd->errbuf && *cmd->errbuf && (*cmd->errbuf)[0];
|
||||||
|
|
||||||
virReportError(VIR_ERR_INTERNAL_ERROR,
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
||||||
@ -2597,8 +2590,6 @@ virCommandWait(virCommandPtr cmd, int *exitstatus)
|
|||||||
str ? str : cmd->args[0], NULLSTR(st),
|
str ? str : cmd->args[0], NULLSTR(st),
|
||||||
haveErrMsg ? ": " : "",
|
haveErrMsg ? ": " : "",
|
||||||
haveErrMsg ? *cmd->errbuf : "");
|
haveErrMsg ? *cmd->errbuf : "");
|
||||||
VIR_FREE(str);
|
|
||||||
VIR_FREE(st);
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -2718,7 +2709,7 @@ int virCommandHandshakeWait(virCommandPtr cmd)
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
if (c != '1') {
|
if (c != '1') {
|
||||||
char *msg;
|
VIR_AUTOFREE(char *) msg = NULL;
|
||||||
ssize_t len;
|
ssize_t len;
|
||||||
if (VIR_ALLOC_N(msg, 1024) < 0) {
|
if (VIR_ALLOC_N(msg, 1024) < 0) {
|
||||||
VIR_FORCE_CLOSE(cmd->handshakeWait[0]);
|
VIR_FORCE_CLOSE(cmd->handshakeWait[0]);
|
||||||
@ -2731,7 +2722,6 @@ int virCommandHandshakeWait(virCommandPtr cmd)
|
|||||||
|
|
||||||
if ((len = saferead(cmd->handshakeWait[0], msg, 1024)) < 0) {
|
if ((len = saferead(cmd->handshakeWait[0], msg, 1024)) < 0) {
|
||||||
VIR_FORCE_CLOSE(cmd->handshakeWait[0]);
|
VIR_FORCE_CLOSE(cmd->handshakeWait[0]);
|
||||||
VIR_FREE(msg);
|
|
||||||
virReportSystemError(errno, "%s",
|
virReportSystemError(errno, "%s",
|
||||||
_("No error message from child failure"));
|
_("No error message from child failure"));
|
||||||
return -1;
|
return -1;
|
||||||
@ -2739,7 +2729,6 @@ int virCommandHandshakeWait(virCommandPtr cmd)
|
|||||||
VIR_FORCE_CLOSE(cmd->handshakeWait[0]);
|
VIR_FORCE_CLOSE(cmd->handshakeWait[0]);
|
||||||
msg[len-1] = '\0';
|
msg[len-1] = '\0';
|
||||||
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", msg);
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", msg);
|
||||||
VIR_FREE(msg);
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
VIR_FORCE_CLOSE(cmd->handshakeWait[0]);
|
VIR_FORCE_CLOSE(cmd->handshakeWait[0]);
|
||||||
@ -2853,8 +2842,8 @@ virCommandFree(virCommandPtr cmd)
|
|||||||
* This requests asynchronous string IO on @cmd. It is useful in
|
* This requests asynchronous string IO on @cmd. It is useful in
|
||||||
* combination with virCommandRunAsync():
|
* combination with virCommandRunAsync():
|
||||||
*
|
*
|
||||||
* virCommandPtr cmd = virCommandNew*(...);
|
* VIR_AUTOPTR(virCommand) cmd = virCommandNew*(...);
|
||||||
* char *buf = NULL;
|
* VIR_AUTOFREE(char *) buf = NULL;
|
||||||
*
|
*
|
||||||
* ...
|
* ...
|
||||||
*
|
*
|
||||||
@ -2862,21 +2851,18 @@ virCommandFree(virCommandPtr cmd)
|
|||||||
* virCommandDoAsyncIO(cmd);
|
* virCommandDoAsyncIO(cmd);
|
||||||
*
|
*
|
||||||
* if (virCommandRunAsync(cmd, NULL) < 0)
|
* if (virCommandRunAsync(cmd, NULL) < 0)
|
||||||
* goto cleanup;
|
* return;
|
||||||
*
|
*
|
||||||
* ...
|
* ...
|
||||||
*
|
*
|
||||||
* if (virCommandWait(cmd, NULL) < 0)
|
* if (virCommandWait(cmd, NULL) < 0)
|
||||||
* goto cleanup;
|
* return;
|
||||||
*
|
*
|
||||||
* // @buf now contains @cmd's stdout
|
* // @buf now contains @cmd's stdout
|
||||||
* VIR_DEBUG("STDOUT: %s", NULLSTR(buf));
|
* VIR_DEBUG("STDOUT: %s", NULLSTR(buf));
|
||||||
*
|
*
|
||||||
* ...
|
* ...
|
||||||
*
|
*
|
||||||
* cleanup:
|
|
||||||
* VIR_FREE(buf);
|
|
||||||
* virCommandFree(cmd);
|
|
||||||
*
|
*
|
||||||
* The libvirt's event loop is used for handling stdios of @cmd.
|
* The libvirt's event loop is used for handling stdios of @cmd.
|
||||||
* Since current implementation uses strlen to determine length
|
* Since current implementation uses strlen to determine length
|
||||||
@ -2969,11 +2955,11 @@ virCommandRunRegex(virCommandPtr cmd,
|
|||||||
{
|
{
|
||||||
int err;
|
int err;
|
||||||
regex_t *reg;
|
regex_t *reg;
|
||||||
regmatch_t *vars = NULL;
|
VIR_AUTOFREE(regmatch_t *) vars = NULL;
|
||||||
size_t i, j, k;
|
size_t i, j, k;
|
||||||
int totgroups = 0, ngroup = 0, maxvars = 0;
|
int totgroups = 0, ngroup = 0, maxvars = 0;
|
||||||
char **groups;
|
char **groups;
|
||||||
char *outbuf = NULL;
|
VIR_AUTOFREE(char *) outbuf = NULL;
|
||||||
char **lines = NULL;
|
char **lines = NULL;
|
||||||
int ret = -1;
|
int ret = -1;
|
||||||
|
|
||||||
@ -3054,13 +3040,11 @@ virCommandRunRegex(virCommandPtr cmd,
|
|||||||
ret = 0;
|
ret = 0;
|
||||||
cleanup:
|
cleanup:
|
||||||
virStringListFree(lines);
|
virStringListFree(lines);
|
||||||
VIR_FREE(outbuf);
|
|
||||||
if (groups) {
|
if (groups) {
|
||||||
for (j = 0; j < totgroups; j++)
|
for (j = 0; j < totgroups; j++)
|
||||||
VIR_FREE(groups[j]);
|
VIR_FREE(groups[j]);
|
||||||
VIR_FREE(groups);
|
VIR_FREE(groups);
|
||||||
}
|
}
|
||||||
VIR_FREE(vars);
|
|
||||||
|
|
||||||
for (i = 0; i < nregex; i++)
|
for (i = 0; i < nregex; i++)
|
||||||
regfree(®[i]);
|
regfree(®[i]);
|
||||||
|
Loading…
Reference in New Issue
Block a user