Add the ability to query configuration variables to tincctl.
This commit is contained in:
parent
a9caa2a6ea
commit
b0f3a76e9b
3 changed files with 55 additions and 22 deletions
|
@ -2044,6 +2044,11 @@ the value of this environment variable is used.
|
||||||
Create initial configuration files and RSA and ECDSA keypairs with default length.
|
Create initial configuration files and RSA and ECDSA keypairs with default length.
|
||||||
If no @var{name} for this node is given, it will be asked for.
|
If no @var{name} for this node is given, it will be asked for.
|
||||||
|
|
||||||
|
@item config [get] @var{variable}
|
||||||
|
Print the current value of configuration variable @var{variable}.
|
||||||
|
If more than one variable with the same name exists,
|
||||||
|
the value of each of them will be printed on a separate line.
|
||||||
|
|
||||||
@item config [set] @var{variable} @var{value}
|
@item config [set] @var{variable} @var{value}
|
||||||
Set configuration variable @var{variable} to the given @var{value}.
|
Set configuration variable @var{variable} to the given @var{value}.
|
||||||
All previously existing configuration variables with the same name are removed.
|
All previously existing configuration variables with the same name are removed.
|
||||||
|
|
|
@ -52,6 +52,11 @@ Create initial configuration files and RSA and ECDSA keypairs with default lengt
|
||||||
If no
|
If no
|
||||||
.Ar name
|
.Ar name
|
||||||
for this node is given, it will be asked for.
|
for this node is given, it will be asked for.
|
||||||
|
.It config Oo get Oc Ar variable
|
||||||
|
Print the current value of configuration variable
|
||||||
|
.Ar variable .
|
||||||
|
If more than one variable with the same name exists,
|
||||||
|
the value of each of them will be printed on a separate line.
|
||||||
.It config Oo set Oc Ar variable Ar value
|
.It config Oo set Oc Ar variable Ar value
|
||||||
Set configuration variable
|
Set configuration variable
|
||||||
.Ar variable
|
.Ar variable
|
||||||
|
|
|
@ -111,6 +111,7 @@ static void usage(bool status) {
|
||||||
"Valid commands are:\n"
|
"Valid commands are:\n"
|
||||||
" init [name] Create initial configuration files.\n"
|
" init [name] Create initial configuration files.\n"
|
||||||
" config Change configuration:\n"
|
" config Change configuration:\n"
|
||||||
|
" [get] VARIABLE - print current value of VARIABLE\n"
|
||||||
" [set] VARIABLE VALUE - set VARIABLE to VALUE\n"
|
" [set] VARIABLE VALUE - set VARIABLE to VALUE\n"
|
||||||
" add VARIABLE VALUE - add VARIABLE with the given VALUE\n"
|
" add VARIABLE VALUE - add VARIABLE with the given VALUE\n"
|
||||||
" del VARIABLE [VALUE] - remove VARIABLE [only ones with watching VALUE]\n"
|
" del VARIABLE [VALUE] - remove VARIABLE [only ones with watching VALUE]\n"
|
||||||
|
@ -942,7 +943,7 @@ static int cmd_log(int argc, char *argv[]) {
|
||||||
}
|
}
|
||||||
|
|
||||||
static int cmd_pid(int argc, char *argv[]) {
|
static int cmd_pid(int argc, char *argv[]) {
|
||||||
if(!connect_tincd())
|
if(!connect_tincd() && !pid)
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
printf("%d\n", pid);
|
printf("%d\n", pid);
|
||||||
|
@ -1056,8 +1057,10 @@ static int cmd_config(int argc, char *argv[]) {
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int action = 0;
|
int action = -2;
|
||||||
if(!strcasecmp(argv[1], "add")) {
|
if(!strcasecmp(argv[1], "get")) {
|
||||||
|
argv++, argc--;
|
||||||
|
} else if(!strcasecmp(argv[1], "add")) {
|
||||||
argv++, argc--, action = 1;
|
argv++, argc--, action = 1;
|
||||||
} else if(!strcasecmp(argv[1], "del")) {
|
} else if(!strcasecmp(argv[1], "del")) {
|
||||||
argv++, argc--, action = -1;
|
argv++, argc--, action = -1;
|
||||||
|
@ -1109,6 +1112,9 @@ static int cmd_config(int argc, char *argv[]) {
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(action < -1 && *value)
|
||||||
|
action = 0;
|
||||||
|
|
||||||
/* Some simple checks. */
|
/* Some simple checks. */
|
||||||
bool found = false;
|
bool found = false;
|
||||||
|
|
||||||
|
@ -1157,8 +1163,8 @@ static int cmd_config(int argc, char *argv[]) {
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!found && action >= 0) {
|
if(!found) {
|
||||||
if(force) {
|
if(force || action < 0) {
|
||||||
fprintf(stderr, "Warning: %s is not a known configuration variable!\n", variable);
|
fprintf(stderr, "Warning: %s is not a known configuration variable!\n", variable);
|
||||||
} else {
|
} else {
|
||||||
fprintf(stderr, "%s: is not a known configuration variable! Use --force to use it anyway.\n", variable);
|
fprintf(stderr, "%s: is not a known configuration variable! Use --force to use it anyway.\n", variable);
|
||||||
|
@ -1190,19 +1196,24 @@ static int cmd_config(int argc, char *argv[]) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
char *tmpfile;
|
char *tmpfile = NULL;
|
||||||
xasprintf(&tmpfile, "%s.config.tmp", filename);
|
FILE *tf = NULL;
|
||||||
FILE *tf = fopen(tmpfile, "w");
|
|
||||||
if(!tf) {
|
if(action >= -1) {
|
||||||
fprintf(stderr, "Could not open temporary file %s: %s\n", tmpfile, strerror(errno));
|
xasprintf(&tmpfile, "%s.config.tmp", filename);
|
||||||
return 1;
|
tf = fopen(tmpfile, "w");
|
||||||
|
if(!tf) {
|
||||||
|
fprintf(stderr, "Could not open temporary file %s: %s\n", tmpfile, strerror(errno));
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Copy the file, making modifications on the fly.
|
// Copy the file, making modifications on the fly, unless we are just getting a value.
|
||||||
char buf1[4096];
|
char buf1[4096];
|
||||||
char buf2[4096];
|
char buf2[4096];
|
||||||
bool set = false;
|
bool set = false;
|
||||||
bool removed = false;
|
bool removed = false;
|
||||||
|
found = false;
|
||||||
|
|
||||||
while(fgets(buf1, sizeof buf1, f)) {
|
while(fgets(buf1, sizeof buf1, f)) {
|
||||||
buf1[sizeof buf1 - 1] = 0;
|
buf1[sizeof buf1 - 1] = 0;
|
||||||
|
@ -1224,8 +1235,12 @@ static int cmd_config(int argc, char *argv[]) {
|
||||||
|
|
||||||
// Did it match?
|
// Did it match?
|
||||||
if(!strcasecmp(buf2, variable)) {
|
if(!strcasecmp(buf2, variable)) {
|
||||||
|
// Get
|
||||||
|
if(action < -1) {
|
||||||
|
found = true;
|
||||||
|
printf("%s\n", bvalue);
|
||||||
// Del
|
// Del
|
||||||
if(action < 0) {
|
} else if(action == -1) {
|
||||||
if(!*value || !strcasecmp(bvalue, value)) {
|
if(!*value || !strcasecmp(bvalue, value)) {
|
||||||
removed = true;
|
removed = true;
|
||||||
continue;
|
continue;
|
||||||
|
@ -1245,18 +1260,20 @@ static int cmd_config(int argc, char *argv[]) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Copy original line...
|
if(action >= -1) {
|
||||||
if(fputs(buf1, tf) < 0) {
|
// Copy original line...
|
||||||
fprintf(stderr, "Error writing to temporary file %s: %s\n", tmpfile, strerror(errno));
|
if(fputs(buf1, tf) < 0) {
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Add newline if it is missing...
|
|
||||||
if(*buf1 && buf1[strlen(buf1) - 1] != '\n') {
|
|
||||||
if(fputc('\n', tf) < 0) {
|
|
||||||
fprintf(stderr, "Error writing to temporary file %s: %s\n", tmpfile, strerror(errno));
|
fprintf(stderr, "Error writing to temporary file %s: %s\n", tmpfile, strerror(errno));
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Add newline if it is missing...
|
||||||
|
if(*buf1 && buf1[strlen(buf1) - 1] != '\n') {
|
||||||
|
if(fputc('\n', tf) < 0) {
|
||||||
|
fprintf(stderr, "Error writing to temporary file %s: %s\n", tmpfile, strerror(errno));
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1279,6 +1296,12 @@ static int cmd_config(int argc, char *argv[]) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(action < -1) {
|
||||||
|
if(!found)
|
||||||
|
fprintf(stderr, "No matching configuration variables found.\n");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
// Make sure we wrote everything...
|
// Make sure we wrote everything...
|
||||||
if(fclose(tf)) {
|
if(fclose(tf)) {
|
||||||
fprintf(stderr, "Error closing temporary file %s: %s\n", tmpfile, strerror(errno));
|
fprintf(stderr, "Error closing temporary file %s: %s\n", tmpfile, strerror(errno));
|
||||||
|
|
Loading…
Reference in a new issue