Skip to content

Commit

Permalink
varnishadm: Harmonize exit status
Browse files Browse the repository at this point in the history
  • Loading branch information
walid-git committed Jan 23, 2024
1 parent 93c6910 commit 7622e10
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 17 deletions.
21 changes: 13 additions & 8 deletions bin/varnishadm/varnishadm.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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);
}

/*
Expand Down Expand Up @@ -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);
Expand All @@ -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
Expand Down Expand Up @@ -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);
}

/*
Expand Down Expand Up @@ -504,7 +509,7 @@ main(int argc, char * const *argv)
t_arg = optarg;
break;
default:
usage(1);
usage(2);
}
}

Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion bin/varnishtest/tests/m00000.vtc
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion bin/varnishtest/tests/u00000.vtc
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion bin/varnishtest/tests/u00012.vtc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 13 additions & 6 deletions doc/sphinx/reference/varnishadm.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
========
Expand Down

0 comments on commit 7622e10

Please sign in to comment.