mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2025-03-07 17:28:15 +00:00
use stdout rather than stderr, improve allocation checks
This commit is contained in:
parent
6bd95bf2a3
commit
1ea832d65b
@ -1,3 +1,8 @@
|
|||||||
|
Thu Apr 6 11:32:46 CEST 2006 Karel Zak <kzak@redhat.com>
|
||||||
|
|
||||||
|
* src/virsh.c: use stdout for standard outputs, improve
|
||||||
|
allocation checks
|
||||||
|
|
||||||
Wed Apr 5 09:32:54 EDT 2006 Daniel Veillard <veillard@redhat.com>
|
Wed Apr 5 09:32:54 EDT 2006 Daniel Veillard <veillard@redhat.com>
|
||||||
|
|
||||||
* src/hash.c: tiny fix
|
* src/hash.c: tiny fix
|
||||||
|
100
src/virsh.c
100
src/virsh.c
@ -202,11 +202,19 @@ static virDomainPtr vshCommandOptDomain(vshControl * ctl, vshCmd * cmd,
|
|||||||
static void vshPrint(vshControl * ctl, vshOutType out, const char *format,
|
static void vshPrint(vshControl * ctl, vshOutType out, const char *format,
|
||||||
...);
|
...);
|
||||||
|
|
||||||
|
|
||||||
static const char *vshDomainStateToString(int state);
|
static const char *vshDomainStateToString(int state);
|
||||||
static int vshConnectionUsability(vshControl * ctl, virConnectPtr conn,
|
static int vshConnectionUsability(vshControl * ctl, virConnectPtr conn,
|
||||||
int showerror);
|
int showerror);
|
||||||
|
|
||||||
|
static void *_vshMalloc(vshControl * ctl, size_t sz, const char *filename, int line);
|
||||||
|
#define vshMalloc(_ctl, _sz) _vshMalloc(_ctl, _sz, __FILE__, __LINE__)
|
||||||
|
|
||||||
|
static void *_vshCalloc(vshControl * ctl, size_t nmemb, size_t sz, const char *filename, int line);
|
||||||
|
#define vshCalloc(_ctl, _nmemb, _sz) _vshCalloc(_ctl, _nmemb, _sz, __FILE__, __LINE__)
|
||||||
|
|
||||||
|
static char *_vshStrdup(vshControl * ctl, const char *s, const char *filename, int line);
|
||||||
|
#define vshStrdup(_ctl, _s) _vshStrdup(_ctl, _s, __FILE__, __LINE__)
|
||||||
|
|
||||||
/* ---------------
|
/* ---------------
|
||||||
* Commands
|
* Commands
|
||||||
* ---------------
|
* ---------------
|
||||||
@ -311,12 +319,8 @@ cmdList(vshControl * ctl, vshCmd * cmd ATTRIBUTE_UNUSED)
|
|||||||
vshError(ctl, FALSE, "failed to list active domains.");
|
vshError(ctl, FALSE, "failed to list active domains.");
|
||||||
return FALSE;
|
return FALSE;
|
||||||
}
|
}
|
||||||
ids = malloc(sizeof(int) * maxid);
|
ids = vshMalloc(ctl, sizeof(int) * maxid);
|
||||||
if (ids == NULL) {
|
|
||||||
fprintf(stderr, "Failed to allocate %d bytes\n",
|
|
||||||
(int) sizeof(int) * maxid);
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
virConnectListDomains(ctl->conn, &ids[0], maxid);
|
virConnectListDomains(ctl->conn, &ids[0], maxid);
|
||||||
|
|
||||||
vshPrint(ctl, VSH_HEADER, "%3s %-20s %s\n", "Id", "Name", "State");
|
vshPrint(ctl, VSH_HEADER, "%3s %-20s %s\n", "Id", "Name", "State");
|
||||||
@ -1420,11 +1424,7 @@ vshCommandGetToken(vshControl * ctl, char *str, char **end, char **res)
|
|||||||
if (sz == 0)
|
if (sz == 0)
|
||||||
return VSH_TK_END;
|
return VSH_TK_END;
|
||||||
|
|
||||||
*res = malloc(sz + 1);
|
*res = vshMalloc(ctl, sz + 1);
|
||||||
if (*res == NULL) {
|
|
||||||
fprintf(stderr, "Failed to allocate %d bytes\n", sz + 1);
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
memcpy(*res, tkstr, sz);
|
memcpy(*res, tkstr, sz);
|
||||||
*(*res + sz) = '\0';
|
*(*res + sz) = '\0';
|
||||||
|
|
||||||
@ -1519,12 +1519,7 @@ vshCommandParse(vshControl * ctl, char *cmdstr)
|
|||||||
}
|
}
|
||||||
if (opt) {
|
if (opt) {
|
||||||
/* save option */
|
/* save option */
|
||||||
vshCmdOpt *arg = malloc(sizeof(vshCmdOpt));
|
vshCmdOpt *arg = vshMalloc(ctl, sizeof(vshCmdOpt));
|
||||||
if (arg == NULL) {
|
|
||||||
fprintf(stderr, "Failed to allocate %d bytes\n",
|
|
||||||
(int) sizeof(vshCmdOpt));
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
arg->def = opt;
|
arg->def = opt;
|
||||||
arg->data = tkdata;
|
arg->data = tkdata;
|
||||||
@ -1549,13 +1544,7 @@ vshCommandParse(vshControl * ctl, char *cmdstr)
|
|||||||
|
|
||||||
/* commad parsed -- allocate new struct for the command */
|
/* commad parsed -- allocate new struct for the command */
|
||||||
if (cmd) {
|
if (cmd) {
|
||||||
vshCmd *c = malloc(sizeof(vshCmd));
|
vshCmd *c = vshMalloc(ctl, sizeof(vshCmd));
|
||||||
|
|
||||||
if (c == NULL) {
|
|
||||||
fprintf(stderr, "Failed to allocate %d bytes\n",
|
|
||||||
(int) sizeof(vshCmd));
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
c->opts = first;
|
c->opts = first;
|
||||||
c->def = cmd;
|
c->def = cmd;
|
||||||
@ -1666,7 +1655,7 @@ vshPrint(vshControl * ctl, vshOutType type, const char *format, ...)
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
va_start(ap, format);
|
va_start(ap, format);
|
||||||
vfprintf(stderr, format, ap);
|
vfprintf(stdout, format, ap);
|
||||||
va_end(ap);
|
va_end(ap);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1687,11 +1676,48 @@ vshError(vshControl * ctl, int doexit, const char *format, ...)
|
|||||||
fputc('\n', stderr);
|
fputc('\n', stderr);
|
||||||
|
|
||||||
if (doexit) {
|
if (doexit) {
|
||||||
|
if (ctl)
|
||||||
vshDeinit(ctl);
|
vshDeinit(ctl);
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void *
|
||||||
|
_vshMalloc(vshControl * ctl, size_t size, const char *filename, int line)
|
||||||
|
{
|
||||||
|
void *x;
|
||||||
|
|
||||||
|
if ((x = malloc(size)))
|
||||||
|
return x;
|
||||||
|
vshError(ctl, TRUE, "%s: %d: failed to allocate %d bytes\n",
|
||||||
|
filename, line, (int) size);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void *
|
||||||
|
_vshCalloc(vshControl * ctl, size_t nmemb, size_t size, const char *filename, int line)
|
||||||
|
{
|
||||||
|
void *x;
|
||||||
|
|
||||||
|
if ((x = calloc(nmemb, size)))
|
||||||
|
return x;
|
||||||
|
vshError(ctl, TRUE, "%s: %d: failed to allocate %d bytes\n",
|
||||||
|
filename, line, (int) (size*nmemb));
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
static char *
|
||||||
|
_vshStrdup(vshControl * ctl, const char *s, const char *filename, int line)
|
||||||
|
{
|
||||||
|
char *x;
|
||||||
|
|
||||||
|
if ((x = strdup(s)))
|
||||||
|
return x;
|
||||||
|
vshError(ctl, TRUE, "%s: %d: failed to allocate %d bytes\n",
|
||||||
|
filename, line, strlen(s));
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Initialize vistsh
|
* Initialize vistsh
|
||||||
*/
|
*/
|
||||||
@ -1749,7 +1775,7 @@ vshReadlineCommandGenerator(const char *text, int state)
|
|||||||
while ((name = commands[list_index].name)) {
|
while ((name = commands[list_index].name)) {
|
||||||
list_index++;
|
list_index++;
|
||||||
if (strncmp(name, text, len) == 0)
|
if (strncmp(name, text, len) == 0)
|
||||||
return strdup(name);
|
return vshStrdup(NULL, name);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* If no names matched, then return NULL. */
|
/* If no names matched, then return NULL. */
|
||||||
@ -1771,12 +1797,7 @@ vshReadlineOptionsGenerator(const char *text, int state)
|
|||||||
if (!(p = strchr(rl_line_buffer, ' ')))
|
if (!(p = strchr(rl_line_buffer, ' ')))
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
cmdname = calloc((p - rl_line_buffer) + 1, 1);
|
cmdname = vshCalloc(NULL, (p - rl_line_buffer) + 1, 1);
|
||||||
if (cmdname == NULL) {
|
|
||||||
fprintf(stderr, "Failed to allocate %d bytes\n",
|
|
||||||
(p - rl_line_buffer) + 1);
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
memcpy(cmdname, rl_line_buffer, p - rl_line_buffer);
|
memcpy(cmdname, rl_line_buffer, p - rl_line_buffer);
|
||||||
|
|
||||||
cmd = vshCmddefSearch(cmdname);
|
cmd = vshCmddefSearch(cmdname);
|
||||||
@ -1802,12 +1823,7 @@ vshReadlineOptionsGenerator(const char *text, int state)
|
|||||||
if (strncmp(name, text + 2, len - 2))
|
if (strncmp(name, text + 2, len - 2))
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
res = malloc(strlen(name) + 3);
|
res = vshMalloc(NULL, strlen(name) + 3);
|
||||||
if (res == NULL) {
|
|
||||||
fprintf(stderr, "Failed to allocate %d bytes\n",
|
|
||||||
(int) strlen(name) + 3);
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
sprintf(res, "--%s", name);
|
sprintf(res, "--%s", name);
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
@ -1986,11 +2002,7 @@ vshParseArgv(vshControl * ctl, int argc, char **argv)
|
|||||||
for (i = end; i < argc; i++)
|
for (i = end; i < argc; i++)
|
||||||
sz += strlen(argv[i]) + 1; /* +1 is for blank space between items */
|
sz += strlen(argv[i]) + 1; /* +1 is for blank space between items */
|
||||||
|
|
||||||
cmdstr = calloc(sz + 1, 1);
|
cmdstr = vshCalloc(ctl, sz + 1, 1);
|
||||||
if (cmdstr == NULL) {
|
|
||||||
fprintf(stderr, "Failed to allocate %d bytes\n", sz + 1);
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
for (i = end; i < argc; i++) {
|
for (i = end; i < argc; i++) {
|
||||||
strncat(cmdstr, argv[i], sz);
|
strncat(cmdstr, argv[i], sz);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user