tools: Make use of the correct environment variables

Since commit 834c5720 which extracted the generic functionality out of virsh
and made it available for other clients like virt-admin to make use of it, it
also introduced a bug when it renamed the original VIRSH_ environment variables
to VSH_ variables. Virt-admin of course suffers from the same bug, so this
patch modifies the generic module vsh.c to construct the correct name for
environment variables of each client from information it has.

Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=1357363

Signed-off-by: Erik Skultety <eskultet@redhat.com>
This commit is contained in:
Erik Skultety 2016-07-28 12:54:16 +02:00
parent 0ef07e19c7
commit d02ef33451
4 changed files with 15 additions and 11 deletions

View File

@ -935,6 +935,7 @@ main(int argc, char **argv)
memset(ctl, 0, sizeof(vshControl)); memset(ctl, 0, sizeof(vshControl));
memset(&virshCtl, 0, sizeof(virshControl)); memset(&virshCtl, 0, sizeof(virshControl));
ctl->name = "virsh"; /* hardcoded name of the binary */ ctl->name = "virsh"; /* hardcoded name of the binary */
ctl->env_prefix = "VIRSH";
ctl->log_fd = -1; /* Initialize log file descriptor */ ctl->log_fd = -1; /* Initialize log file descriptor */
ctl->debug = VSH_DEBUG_DEFAULT; ctl->debug = VSH_DEBUG_DEFAULT;
ctl->hooks = &hooks; ctl->hooks = &hooks;

View File

@ -1340,6 +1340,7 @@ main(int argc, char **argv)
memset(ctl, 0, sizeof(vshControl)); memset(ctl, 0, sizeof(vshControl));
memset(&virtAdminCtl, 0, sizeof(vshAdmControl)); memset(&virtAdminCtl, 0, sizeof(vshAdmControl));
ctl->name = "virt-admin"; /* hardcoded name of the binary */ ctl->name = "virt-admin"; /* hardcoded name of the binary */
ctl->env_prefix = "VIRT_ADMIN";
ctl->log_fd = -1; /* Initialize log file descriptor */ ctl->log_fd = -1; /* Initialize log file descriptor */
ctl->debug = VSH_DEBUG_DEFAULT; ctl->debug = VSH_DEBUG_DEFAULT;
ctl->hooks = &hooks; ctl->hooks = &hooks;

View File

@ -2830,16 +2830,10 @@ static int
vshReadlineInit(vshControl *ctl) vshReadlineInit(vshControl *ctl)
{ {
char *userdir = NULL; char *userdir = NULL;
char *name_capitalized = NULL;
int max_history = 500; int max_history = 500;
int ret = -1; int ret = -1;
char *histsize_env = NULL; char *histsize_env = NULL;
const char *histsize_str = NULL; const char *histsize_str = NULL;
const char *strings[] = {
name_capitalized,
"HISTSIZE",
NULL
};
/* Allow conditional parsing of the ~/.inputrc file. /* Allow conditional parsing of the ~/.inputrc file.
* Work around ancient readline 4.1 (hello Mac OS X), * Work around ancient readline 4.1 (hello Mac OS X),
@ -2852,8 +2846,7 @@ vshReadlineInit(vshControl *ctl)
rl_basic_word_break_characters = " \t\n\\`@$><=;|&{("; rl_basic_word_break_characters = " \t\n\\`@$><=;|&{(";
if (virStringToUpper(&name_capitalized, ctl->name) < 0 || if (virAsprintf(&histsize_env, "%s_HISTSIZE", ctl->env_prefix) < 0)
!(histsize_env = virStringJoin(strings, "_")))
goto cleanup; goto cleanup;
/* Limit the total size of the history buffer */ /* Limit the total size of the history buffer */
@ -2895,7 +2888,6 @@ vshReadlineInit(vshControl *ctl)
cleanup: cleanup:
VIR_FREE(userdir); VIR_FREE(userdir);
VIR_FREE(name_capitalized);
VIR_FREE(histsize_env); VIR_FREE(histsize_env);
return ret; return ret;
} }
@ -2967,10 +2959,14 @@ static int
vshInitDebug(vshControl *ctl) vshInitDebug(vshControl *ctl)
{ {
const char *debugEnv; const char *debugEnv;
char *env = NULL;
if (ctl->debug == VSH_DEBUG_DEFAULT) { if (ctl->debug == VSH_DEBUG_DEFAULT) {
if (virAsprintf(&env, "%s_DEBUG", ctl->env_prefix) < 0)
return -1;
/* log level not set from commandline, check env variable */ /* log level not set from commandline, check env variable */
debugEnv = virGetEnvAllowSUID("VSH_DEBUG"); debugEnv = virGetEnvAllowSUID(env);
if (debugEnv) { if (debugEnv) {
int debug; int debug;
if (virStrToLong_i(debugEnv, NULL, 10, &debug) < 0 || if (virStrToLong_i(debugEnv, NULL, 10, &debug) < 0 ||
@ -2981,15 +2977,20 @@ vshInitDebug(vshControl *ctl)
ctl->debug = debug; ctl->debug = debug;
} }
} }
VIR_FREE(env);
} }
if (ctl->logfile == NULL) { if (ctl->logfile == NULL) {
if (virAsprintf(&env, "%s_LOG_FILE", ctl->env_prefix) < 0)
return -1;
/* log file not set from cmdline */ /* log file not set from cmdline */
debugEnv = virGetEnvBlockSUID("VSH_LOG_FILE"); debugEnv = virGetEnvBlockSUID(env);
if (debugEnv && *debugEnv) { if (debugEnv && *debugEnv) {
ctl->logfile = vshStrdup(ctl, debugEnv); ctl->logfile = vshStrdup(ctl, debugEnv);
vshOpenLogFile(ctl); vshOpenLogFile(ctl);
} }
VIR_FREE(env);
} }
return 0; return 0;

View File

@ -197,6 +197,7 @@ struct _vshControl {
const char *name; /* hardcoded name of the binary that cannot const char *name; /* hardcoded name of the binary that cannot
* be changed without recompilation compared * be changed without recompilation compared
* to program name */ * to program name */
const char *env_prefix; /* hardcoded environment variable prefix */
char *connname; /* connection name */ char *connname; /* connection name */
char *progname; /* program name */ char *progname; /* program name */
vshCmd *cmd; /* the current command */ vshCmd *cmd; /* the current command */