From e42a2c9c85e200a59aa82b47768bb8cbbc740565 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Myl=C3=A8ne=20Josserand?= Date: Fri, 16 Oct 2020 10:38:11 +0200 Subject: [PATCH 1/2] console: Add 'k' option to exit on console-down MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This commit is adding an option 'k' to make the console application exits when the console is down. This can be useful in some cases such as an integration to LAVA infrastructure. Console can be used to open a terminal on different boards to be able to run some tests. In some cases, the console is down and without this commit, the tests will be timed-out because it can't talk to the device. Adding this option will allow us to exit the console directly, without waiting for a timeout. The benefit will be time saving. Signed-off-by: Mylène Josserand --- console/console.c | 22 +++++++++++++++++++--- console/readconf.h | 1 + 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/console/console.c b/console/console.c index 224f1a8..905eba6 100644 --- a/console/console.c +++ b/console/console.c @@ -1471,8 +1471,13 @@ CallUp(CONSFILE *pcf, char *pcMaster, char *pcMach, char *pcHow, /* try to grok the state of the console */ FilePrint(pcf, FLAGFALSE, "%c%c=", chAttn, chEsc); r = ReadReply(pcf, FLAGFALSE); - if (strncmp(r, "[unknown", 8) != 0 && strncmp(r, "[up]", 4) != 0) - FileWrite(cfstdout, FLAGFALSE, r, -1); + if (strncmp(r, "[unknown", 8) != 0 && strncmp(r, "[up]", 4) != 0) { + FileWrite(cfstdout, FLAGFALSE, r, -1); + if (config->exitdown == FLAGTRUE) { + printf("Device is down. Will exit with option 'k'\n"); + kill(thepid, SIGTERM); + } + } /* try to grok the version of the server */ FilePrint(pcf, FLAGFALSE, "%c%c%c", chAttn, chEsc, 0xD6); @@ -1905,7 +1910,7 @@ main(int argc, char **argv) int fLocal; static STRING *acPorts = (STRING *)0; static char acOpts[] = - "7aAb:B:c:C:d:De:EfFhiIl:M:np:PqQrRsSt:uUvVwWxz:Z:"; + "7aAb:B:c:C:d:De:EfFhikIl:M:np:PqQrRsSt:uUvVwWxz:Z:"; extern int optind; extern int optopt; extern char *optarg; @@ -2035,6 +2040,10 @@ main(int argc, char **argv) pcCmd = "info"; break; + case 'k': + optConf->exitdown = FLAGTRUE; + break; + case 'l': if ((optConf->username = StrDup(optarg)) == (char *)0) OutOfMem(); @@ -2232,6 +2241,13 @@ main(int argc, char **argv) else config->striphigh = FLAGFALSE; + if (optConf->exitdown != FLAGUNKNOWN) + config->exitdown = optConf->exitdown; + else if (pConfig->exitdown != FLAGUNKNOWN) + config->exitdown = pConfig->exitdown; + else + config->exitdown = FLAGFALSE; + if (optConf->escape != (char *)0) ParseEsc(optConf->escape); else if (pConfig->escape != (char *)0) diff --git a/console/readconf.h b/console/readconf.h index 1e9d65d..81f0550 100644 --- a/console/readconf.h +++ b/console/readconf.h @@ -12,6 +12,7 @@ typedef struct config { char *port; char *escape; FLAG striphigh; + FLAG exitdown; unsigned short replay; unsigned short playback; #if HAVE_OPENSSL From 3bf686ed39c7695edf3548551caab6d1bbca73db Mon Sep 17 00:00:00 2001 From: Bryan Stansell Date: Sat, 17 Oct 2020 17:07:01 -0700 Subject: [PATCH 2/2] adding docs/help for new -k and exiting like other code paths --- console/console.c | 7 ++++--- console/console.man.in | 6 +++++- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/console/console.c b/console/console.c index 905eba6..52e4cc6 100644 --- a/console/console.c +++ b/console/console.c @@ -285,6 +285,7 @@ Usage(int wantfull) "f(F) force read/write connection (and replay)", "h output this message", "i(I) display status info in machine-parseable form (on master)", + "k abort connection if the console is not 'up'", "l user use username instead of current username", "M master master server to poll first", "n do not read system-wide config file", @@ -312,7 +313,7 @@ Usage(int wantfull) %s [generic-args] [-iIuwWx] [console]\n\ %s [generic-args] [-hPqQrRV] [-[bB] message] [-d [user][@console]]\n\ [-t [user][@console] message] [-[zZ] cmd]\n\n\ - generic-args: [-7DEnUv] [-c cred] [-C config] [-M master]\n\ + generic-args: [-7DEknUv] [-c cred] [-C config] [-M master]\n\ [-p port] [-l username]\n", progname, progname, progname); if (wantfull) { @@ -1474,8 +1475,8 @@ CallUp(CONSFILE *pcf, char *pcMaster, char *pcMach, char *pcHow, if (strncmp(r, "[unknown", 8) != 0 && strncmp(r, "[up]", 4) != 0) { FileWrite(cfstdout, FLAGFALSE, r, -1); if (config->exitdown == FLAGTRUE) { - printf("Device is down. Will exit with option 'k'\n"); - kill(thepid, SIGTERM); + Error("Console is not 'up'. Exiting. (-k)"); + Bye(EX_UNAVAILABLE); } } diff --git a/console/console.man.in b/console/console.man.in index 9c5e9cf..678794c 100644 --- a/console/console.man.in +++ b/console/console.man.in @@ -28,7 +28,7 @@ console \- console server client program .IR cmd ] .PP .IR generic-args : -.RB [ \-7DEnUv ] +.RB [ \-7DEknUv ] .RB [ \-c .IR cred ] .RB [ \-C @@ -178,6 +178,10 @@ Same as .B \-i but just acts on the primary server. .TP +.B \-k +Abort the connection if the console is not in the `up' state immediately +upon connecting. +.TP .BI \-l user Set the login name used for authentication to .IR user .