From 1ea832d65bc80e5d3591db9a4750fbb659eba620 Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Thu, 6 Apr 2006 10:33:06 +0000 Subject: [PATCH] use stdout rather than stderr, improve allocation checks --- ChangeLog | 5 +++ src/virsh.c | 102 +++++++++++++++++++++++++++++----------------------- 2 files changed, 62 insertions(+), 45 deletions(-) diff --git a/ChangeLog b/ChangeLog index 10a297d8e2..6d80cbedd7 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +Thu Apr 6 11:32:46 CEST 2006 Karel Zak + + * src/virsh.c: use stdout for standard outputs, improve + allocation checks + Wed Apr 5 09:32:54 EDT 2006 Daniel Veillard * src/hash.c: tiny fix diff --git a/src/virsh.c b/src/virsh.c index 1047bc9767..c33e11d284 100644 --- a/src/virsh.c +++ b/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 const char *vshDomainStateToString(int state); static int vshConnectionUsability(vshControl * ctl, virConnectPtr conn, 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 * --------------- @@ -311,12 +319,8 @@ cmdList(vshControl * ctl, vshCmd * cmd ATTRIBUTE_UNUSED) vshError(ctl, FALSE, "failed to list active domains."); return FALSE; } - ids = malloc(sizeof(int) * maxid); - if (ids == NULL) { - fprintf(stderr, "Failed to allocate %d bytes\n", - (int) sizeof(int) * maxid); - exit(1); - } + ids = vshMalloc(ctl, sizeof(int) * maxid); + virConnectListDomains(ctl->conn, &ids[0], maxid); 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) return VSH_TK_END; - *res = malloc(sz + 1); - if (*res == NULL) { - fprintf(stderr, "Failed to allocate %d bytes\n", sz + 1); - exit(1); - } + *res = vshMalloc(ctl, sz + 1); memcpy(*res, tkstr, sz); *(*res + sz) = '\0'; @@ -1519,12 +1519,7 @@ vshCommandParse(vshControl * ctl, char *cmdstr) } if (opt) { /* save option */ - vshCmdOpt *arg = malloc(sizeof(vshCmdOpt)); - if (arg == NULL) { - fprintf(stderr, "Failed to allocate %d bytes\n", - (int) sizeof(vshCmdOpt)); - exit(1); - } + vshCmdOpt *arg = vshMalloc(ctl, sizeof(vshCmdOpt)); arg->def = opt; arg->data = tkdata; @@ -1549,13 +1544,7 @@ vshCommandParse(vshControl * ctl, char *cmdstr) /* commad parsed -- allocate new struct for the command */ if (cmd) { - vshCmd *c = malloc(sizeof(vshCmd)); - - if (c == NULL) { - fprintf(stderr, "Failed to allocate %d bytes\n", - (int) sizeof(vshCmd)); - exit(1); - } + vshCmd *c = vshMalloc(ctl, sizeof(vshCmd)); c->opts = first; c->def = cmd; @@ -1666,7 +1655,7 @@ vshPrint(vshControl * ctl, vshOutType type, const char *format, ...) return; va_start(ap, format); - vfprintf(stderr, format, ap); + vfprintf(stdout, format, ap); va_end(ap); } @@ -1687,11 +1676,48 @@ vshError(vshControl * ctl, int doexit, const char *format, ...) fputc('\n', stderr); if (doexit) { - vshDeinit(ctl); + if (ctl) + vshDeinit(ctl); 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 */ @@ -1749,7 +1775,7 @@ vshReadlineCommandGenerator(const char *text, int state) while ((name = commands[list_index].name)) { list_index++; if (strncmp(name, text, len) == 0) - return strdup(name); + return vshStrdup(NULL, name); } /* If no names matched, then return NULL. */ @@ -1771,12 +1797,7 @@ vshReadlineOptionsGenerator(const char *text, int state) if (!(p = strchr(rl_line_buffer, ' '))) return NULL; - cmdname = calloc((p - rl_line_buffer) + 1, 1); - if (cmdname == NULL) { - fprintf(stderr, "Failed to allocate %d bytes\n", - (p - rl_line_buffer) + 1); - exit(1); - } + cmdname = vshCalloc(NULL, (p - rl_line_buffer) + 1, 1); memcpy(cmdname, rl_line_buffer, p - rl_line_buffer); cmd = vshCmddefSearch(cmdname); @@ -1802,12 +1823,7 @@ vshReadlineOptionsGenerator(const char *text, int state) if (strncmp(name, text + 2, len - 2)) continue; } - res = malloc(strlen(name) + 3); - if (res == NULL) { - fprintf(stderr, "Failed to allocate %d bytes\n", - (int) strlen(name) + 3); - exit(1); - } + res = vshMalloc(NULL, strlen(name) + 3); sprintf(res, "--%s", name); return res; } @@ -1986,11 +2002,7 @@ vshParseArgv(vshControl * ctl, int argc, char **argv) for (i = end; i < argc; i++) sz += strlen(argv[i]) + 1; /* +1 is for blank space between items */ - cmdstr = calloc(sz + 1, 1); - if (cmdstr == NULL) { - fprintf(stderr, "Failed to allocate %d bytes\n", sz + 1); - exit(1); - } + cmdstr = vshCalloc(ctl, sz + 1, 1); for (i = end; i < argc; i++) { strncat(cmdstr, argv[i], sz);