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 Dec 27, 2023
1 parent 6b9d73d commit 5bab39e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 16 deletions.
25 changes: 15 additions & 10 deletions bin/varnishadm/varnishadm.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ struct vsb *cli_buf;
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 @@ -101,7 +106,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 @@ -200,11 +205,11 @@ pass_answer(int fd, enum pass_mode_e mode, struct cli *cli)
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);
}
if (cli) {
VCLI_SetResult(cli, status);
Expand Down Expand Up @@ -237,7 +242,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 @@ -377,7 +382,7 @@ vadm_cli_cb_after(const struct cli *cli)

fprintf(stderr, "\nCommand \"%s\" failed with error code %u\n",
cmd, cli->result);
RL_EXIT(cli->result/100);
adm_exit(cli->result);
}

static char *
Expand Down Expand Up @@ -478,7 +483,7 @@ pass(int sock)
j = poll(fds, 1, 0);
if (j == 1 && recv(sock, buf, 1, MSG_PEEK) == 0) {
fprintf(stderr, "CLI communication error\n");
RL_EXIT(1);
adm_exit(CLIS_COMMS);
}

} while (i == 0);
Expand Down Expand Up @@ -601,7 +606,7 @@ main(int argc, char * const *argv)
t_arg = optarg;
break;
default:
usage(1);
usage(2);
}
}

Expand All @@ -610,15 +615,15 @@ 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)
exit(2);
exit(4);

if (t_arg != NULL && t_arg_timeout(t_arg) < 0)
exit(2);
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 5bab39e

Please sign in to comment.