Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Varnishadm: Add -e argument #4018

Open
wants to merge 11 commits into
base: master
Choose a base branch
from

Conversation

walid-git
Copy link
Member

-e
Exit immediately if a command fails in pass mode and return the CLI
status code of the failing command. 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.

Fixes: #4012

@walid-git
Copy link
Member Author

Bugwash:

[15:09:38] <phk> I dont see any problems
[15:09:49] <phk> but I guess we should give @nigoroll a chance

doc/sphinx/reference/varnishadm.rst Outdated Show resolved Hide resolved
doc/sphinx/reference/varnishadm.rst Outdated Show resolved Hide resolved
bin/varnishadm/varnishadm.c Outdated Show resolved Hide resolved
bin/varnishadm/varnishadm.c Outdated Show resolved Hide resolved
bin/varnishadm/varnishadm.c Outdated Show resolved Hide resolved
bin/varnishtest/tests/u00001.vtc Outdated Show resolved Hide resolved
lib/libvarnish/vcli_serve.c Outdated Show resolved Hide resolved
bin/varnishadm/varnishadm.c Outdated Show resolved Hide resolved
bin/varnishadm/varnishadm.c Outdated Show resolved Hide resolved
Copy link
Member

@dridi dridi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we are in the neighborhood...

Comment on lines 354 to 434
for (i = 1; av[i] != NULL; i++) {
VSB_quote(cli_buf, av[i], strlen(av[i]), 0);
VSB_putc(cli_buf, ' ');
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think now is a good time to tackle the lack of SP quoting and share the logic with the do_args() function.

This could mean a new VSB_QUOTE_CLI flag. I'll let you think about our options here. One could be to always write double-quoted arguments with such a flag, except of course when we have a here-document.

@nigoroll
Copy link
Member

I am not sure if what I am asking for justifies the effort, but I am having a hard time following the first commit. If it is not too much work, could it be split into smaller commits or could the commit message explain better what it does?

@dridi
Copy link
Member

dridi commented Nov 13, 2023

If it is not too much work, could it be split into smaller commits or could the commit message explain better what it does?

Not sure how the github web UI would render it, but if the discarded comment was preserved as I suggested we would likely have a cleaner diff.

@walid-git
Copy link
Member Author

I brought back the comment mentioned by Dridi, hopefully this will make the diff a bit more readable. Otherwise, I don't see any way to break the commit into smaller ones.

@walid-git
Copy link
Member Author

walid-git commented Nov 16, 2023

  • Changed the return value of varnishadm -e to cli status / 100 after phk's suggestion during bugwash. That is because CLI status can go beyond 255 and we might have a status code that is N*256 that will return 0.
  • Added polling to the remote cli socket so that we are notified if the socket hangs up when we are not writing anything.
  • Added quoting to the CLI command arguments so that the VCLS server we are forwarding commands to sees the arguments grouped in the same way they were received.

Note that tests will not pass until the bugfix in f354ca9 is merged.

Copy link
Member

@nigoroll nigoroll left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the improved diff. I think I understand the patch better now, but still I am not that experienced in this area. Looks OK to my eyes...

lib/libvarnish/vsb.c Outdated Show resolved Hide resolved
bin/varnishadm/varnishadm.c Outdated Show resolved Hide resolved
bin/varnishadm/varnishadm.c Outdated Show resolved Hide resolved
Comment on lines 43 to 45
-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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should point to the existing section in the manual and detail things there:

Exit immediately if a command fails in pass mode, see EXIT STATUS for more details.

We can even make EXIT STATUS a reference for the HTML rendering with sphinx.

Then in that section we don't document it as "divided by 100" but rather as a list of status codes with their meanings:

  • 1: could not execute the command
  • 3: the command failed
  • 4: the connection was lost
  • 5: the connection was closed

Help to better word this would be appreciated.

This should probably be done first in argv mode, at the beginning of this patch series.

Not sure about the interactive mode.

Comment on lines 440 to 487
i = VCLS_Poll(vcls, cli, 0);
j = poll(fds, 1, 0);
if (j == 1 && recv(sock, buf, 1, MSG_PEEK) == 0) {
fprintf(stderr, "CLI communication error\n");
RL_EXIT(1);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-RL_EXIT(1);
+adm_exit(CLIS_COMMS);

Where the adm_exit() function does the division by 100.

Also, I'm curious, why peek instead of checking the result for poll(2)?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used peek, because, on my system at least, poll would report POLLIN event when the remote socket is closed, and that could not be differentiated from when we have actual data to read (which should never be the case but still..), so I found using recv with MSG_PEEK to be the most reliable solution here as it will always return 0 on EOF

Comment on lines +126 to +129
#define VSB_QUOTE_CLI 64
/*
* Add quotes ".." around arguments when needed
*/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the rule for "when needed" is the presence of either space, tabulation, or double quote in the argument value, so in its current state VSB_QUOTE_CLI is insufficient.

@walid-git walid-git force-pushed the varnishadm_fix branch 2 times, most recently from 5bab39e to cb825bc Compare December 27, 2023 18:33
@walid-git
Copy link
Member Author

Addressed dridi's comments:

  • Harmonized the exit status and documented them in EXIT STATUS section of the man page.
  • We don't forward the parsed heredoc now, we reconstruct it and forward it to be parsed in mgt instead.

Ready for review

Copy link
Member

@dridi dridi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We looked at some of the issues brought up in this pull request with @walid-git and we are considering adding small building blocks to vcli_serve to offer a "CLI proxy" feature. @walid-git is working on it, and meanwhile I forgot to submit the small nitpicks we found while reviewing the commits.

printf("%s\n", answer);
if (status == CLIS_TRUNCATED)
printf("[response was truncated]\n");
if (cli) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Explicit null check please.

Comment on lines 362 to 364
pass_print_answer(cli->result, VSB_data(cli->sb), pass_script);

}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove blank line after pass_print_answer() call.

Comment on lines 360 to 359
vadm_cli_cb_after(const struct cli *cli)
{


const char *cmd;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove blank lines before cmd declaration.

@walid-git walid-git force-pushed the varnishadm_fix branch 2 times, most recently from 7622e10 to d4835cf Compare January 23, 2024 13:49
VCLP is a VCLS used as a proxy, it accepts connections similary
to a VCLS and has a single wildcard command that forwards the
input to another remote VCLS. VCLP_poll is different from
VCLS_poll in that it also polls the target VCLS socket and returns
an error in case the connection is broken.
From POLL(2) man page's POLLHUP section:
    Note that when reading from a channel such as a pipe or a stream socket,
    this event merely indicates that the peer closed its end of the channel.
    Subsequent reads from the channel will return 0 (end of file) only after
    all outstanding data in the channel has been consumed.

This means that receiving a POLLHUP event should not prevent us from
trying a read since data might still be available to consume. Thus, we can
simply ignore POLLHUP events and rely entirely on read return value.
Put arguments between quotes ".." when needed
This will make sure that the VCLS server that we are forwarding
commands to will see the commands in the same way they were received.
Let mgt parse the heredocs as received instead of forwarding the
parsed version. This keeps the previous logging behvior where
only the first line of the heredoc command gets logged to syslog
instead of the whole thing.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

varnishadm always returns 0 (success) when commands sent via STDIN
3 participants