From 7622e100f5152673ae7ef6278fbb268bacd52667 Mon Sep 17 00:00:00 2001 From: Walid Boudebouda Date: Wed, 27 Dec 2023 17:45:26 +0100 Subject: [PATCH] varnishadm: Harmonize exit status --- bin/varnishadm/varnishadm.c | 21 +++++++++++++-------- bin/varnishtest/tests/m00000.vtc | 2 +- bin/varnishtest/tests/u00000.vtc | 2 +- bin/varnishtest/tests/u00012.vtc | 2 +- doc/sphinx/reference/varnishadm.rst | 19 +++++++++++++------ 5 files changed, 29 insertions(+), 17 deletions(-) diff --git a/bin/varnishadm/varnishadm.c b/bin/varnishadm/varnishadm.c index 69ed7d14932..d890f50ff94 100644 --- a/bin/varnishadm/varnishadm.c +++ b/bin/varnishadm/varnishadm.c @@ -87,6 +87,11 @@ static int p_arg = 0; static int e_arg = 0; static int line_sock; +static void v_noreturn_ +adm_exit(enum VCLI_status_e status) { + RL_EXIT(status / 100); +} + static void cli_write(int sock, const char *s) { @@ -97,7 +102,7 @@ cli_write(int sock, const char *s) if (i == l) return; perror("Write error CLI socket"); - RL_EXIT(1); + adm_exit(CLIS_COMMS); } /* @@ -196,11 +201,11 @@ pass_answer(int fd, enum pass_mode_e mode) if (u) { if (status == CLIS_COMMS) { fprintf(stderr, "%s\n", answer); - RL_EXIT(2); + adm_exit(CLIS_COMMS); } if (answer) fprintf(stderr, "%s\n", answer); - RL_EXIT(1); + adm_exit(status); } pass_print_answer(status, answer, mode); @@ -227,7 +232,7 @@ do_args(int sock, int argc, char * const *argv) exit(0); if (!p_arg) fprintf(stderr, "Command failed with error code %u\n", status); - exit(1); + adm_exit(status); } /* Callback for readline, doesn't take a private pointer, so we need @@ -366,7 +371,7 @@ vadm_cli_cb_after(const struct cli *cli) pass_print_answer(cli->result, VSB_data(cli->sb), pass_script); fprintf(stderr, "\nCommand \"%s\" failed with error code %u\n", cmd, cli->result); - RL_EXIT(cli->result/100); + adm_exit(cli->result); } /* @@ -504,7 +509,7 @@ main(int argc, char * const *argv) t_arg = optarg; break; default: - usage(1); + usage(2); } } @@ -513,11 +518,11 @@ main(int argc, char * const *argv) if (T_arg != NULL) { if (n_arg != NULL) - usage(1); + usage(2); sock = cli_sock(T_arg, S_arg); } else { if (S_arg != NULL) - usage(1); + usage(2); sock = n_arg_sock(n_arg, t_arg); } if (sock < 0) diff --git a/bin/varnishtest/tests/m00000.vtc b/bin/varnishtest/tests/m00000.vtc index 715f5fa7b7f..ab9bd2455fb 100644 --- a/bin/varnishtest/tests/m00000.vtc +++ b/bin/varnishtest/tests/m00000.vtc @@ -186,7 +186,7 @@ shell { varnish v1 -clierr 300 "vcl.load f1 ${tmpdir}/f1" -shell -exit 1 -expect {failing as requested} { +shell -exit 3 -expect {failing as requested} { varnishadm -t 10 -n ${tmpdir}/v1 vcl.load f1 ${tmpdir}/f1 } diff --git a/bin/varnishtest/tests/u00000.vtc b/bin/varnishtest/tests/u00000.vtc index d3baa09d69e..7b19aaa2e41 100644 --- a/bin/varnishtest/tests/u00000.vtc +++ b/bin/varnishtest/tests/u00000.vtc @@ -116,7 +116,7 @@ shell {varnishadm -n ${tmpdir}/v1 start} shell {varnishadm -n ${tmpdir}/v1 debug.listen_address} -shell -exit 1 -expect "Command failed with error code 500" { +shell -exit 5 -expect "Command failed with error code 500" { varnishadm -n ${tmpdir}/v1 quit } diff --git a/bin/varnishtest/tests/u00012.vtc b/bin/varnishtest/tests/u00012.vtc index 3f4a19ec6d2..10dc81332b9 100644 --- a/bin/varnishtest/tests/u00012.vtc +++ b/bin/varnishtest/tests/u00012.vtc @@ -32,7 +32,7 @@ process p1 -screen_dump -write "\x04" -wait process p1 -log {varnishadm -t foobar 2>&1} -expect-exit 2 -run process p1 -expect-text 0 0 "-t: Invalid argument:" -process p1 -log {varnishadm -Q 2>&1} -expect-exit 1 -run +process p1 -log {varnishadm -Q 2>&1} -expect-exit 2 -run process p1 -expect-text 0 0 "Usage: varnishadm" process p2 -log {varnishadm -h 2>&1} -expect-exit 0 -run diff --git a/doc/sphinx/reference/varnishadm.rst b/doc/sphinx/reference/varnishadm.rst index 46afd7553bc..a0bc50bb011 100644 --- a/doc/sphinx/reference/varnishadm.rst +++ b/doc/sphinx/reference/varnishadm.rst @@ -41,11 +41,12 @@ OPTIONS ======= -e - Exit immediately if a command fails in `pass` mode and return the CLI - status code of the failing command divided by 100. This has no effect - on `interactive` mode (except when `-p` is used). Similarly to `CLI - Command File` (see :ref:`varnishd(1)`), if a command is prefixed with - '-', its failure will be ignored and will not cause varnishadm to exit. + Exit immediately if a command fails in `pass` mode with an exit + status indicating the nature of the error (See EXIT STATUS for details). + This has no effect on `interactive` mode (except when `-p` is used). + Similarly to `CLI Command File` (see :ref:`varnishd(1)`), if a command + is prefixed with '-', its failure will be ignored and will not cause + varnishadm to exit. -h Print program usage and exit. @@ -82,7 +83,13 @@ EXIT STATUS =========== If a command is given, the exit status of the `varnishadm` utility is -zero if the command succeeded, and non-zero otherwise. +zero if the command succeeded, and one of the following otherwise: + +1: could not execute the command +2: bad usage +3: the command failed +4: the connection was lost +5: the CLI session was terminated EXAMPLES ========