1
0
mirror of https://github.com/systemd/systemd.git synced 2024-12-22 17:35:35 +03:00

basic/pager: ignore ^C when piping to less and K is not set

Normally, we want to immediately quit on ^C. But when we are running under
less, people may set SYSTEMD_LESS without K, in which case they can use ^C to
communicate with less, and e.g. start and stop following input.

Fixes #6405.
This commit is contained in:
Zbigniew Jędrzejewski-Szmek 2018-11-11 19:04:00 +01:00
parent ce02b69ea8
commit 6432da6a69
2 changed files with 80 additions and 15 deletions

View File

@ -26,7 +26,13 @@
<term><varname>$SYSTEMD_LESS</varname></term>
<listitem><para>Override the options passed to <command>less</command> (by default
<literal>FRSXMK</literal>).</para></listitem>
<literal>FRSXMK</literal>).</para>
<para>If the value of <varname>$SYSTEMD_LESS</varname> does not include <literal>K</literal>,
and the pager that is invoked is <command>less</command>,
<keycombo><keycap>Ctrl</keycap><keycap>C</keycap></keycombo> will be ignored by the
executable. This allows <command>less</command> to handle
<keycombo><keycap>Ctrl</keycap><keycap>C</keycap></keycombo> itself.</para></listitem>
</varlistentry>
<varlistentry id='lesscharset'>

View File

@ -12,6 +12,8 @@
#include "copy.h"
#include "fd-util.h"
#include "fileio.h"
#include "io-util.h"
#include "locale-util.h"
#include "log.h"
#include "macro.h"
@ -41,10 +43,47 @@ _noreturn_ static void pager_fallback(void) {
_exit(EXIT_SUCCESS);
}
static int no_quit_on_interrupt(int exe_name_fd, const char *less_opts) {
_cleanup_fclose_ FILE *file = NULL;
_cleanup_free_ char *line = NULL;
int r;
assert(exe_name_fd >= 0);
assert(less_opts);
/* This takes ownership of exe_name_fd */
file = fdopen(exe_name_fd, "r");
if (!file) {
safe_close(exe_name_fd);
return log_debug_errno(errno, "Failed to create FILE object: %m");
}
/* Find the last line */
for (;;) {
_cleanup_free_ char *t = NULL;
r = read_line(file, LONG_LINE_MAX, &t);
if (r < 0)
return r;
if (r == 0)
break;
free_and_replace(line, t);
}
/* We only treat "less" specially.
* Return true whenever option K is *not* set. */
r = streq_ptr(line, "less") && !strchr(less_opts, 'K');
log_debug("Pager executable is \"%s\", options \"%s\", quit_on_interrupt: %s",
strnull(line), less_opts, yes_no(!r));
return r;
}
int pager_open(PagerFlags flags) {
_cleanup_close_pair_ int fd[2] = { -1, -1 };
_cleanup_close_pair_ int fd[2] = { -1, -1 }, exe_name_pipe[2] = { -1, -1 };
_cleanup_strv_free_ char **pager_args = NULL;
const char *pager;
const char *pager, *less_opts;
int r;
if (flags & PAGER_DISABLE)
@ -81,16 +120,9 @@ int pager_open(PagerFlags flags) {
if (pipe2(fd, O_CLOEXEC) < 0)
return log_error_errno(errno, "Failed to create pager pipe: %m");
r = safe_fork("(pager)", FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_LOG, &pager_pid);
if (r < 0)
return r;
if (r == 0) {
const char* less_opts, *less_charset;
/* In the child start the pager */
(void) dup2(fd[0], STDIN_FILENO);
safe_close_pair(fd);
/* This is a pipe to feed the name of the executed pager binary into the parent */
if (pipe2(exe_name_pipe, O_CLOEXEC) < 0)
return log_error_errno(errno, "Failed to create exe_name pipe: %m");
/* Initialize a good set of less options */
less_opts = getenv("SYSTEMD_LESS");
@ -98,6 +130,18 @@ int pager_open(PagerFlags flags) {
less_opts = "FRSXMK";
if (flags & PAGER_JUMP_TO_END)
less_opts = strjoina(less_opts, " +G");
r = safe_fork("(pager)", FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_LOG, &pager_pid);
if (r < 0)
return r;
if (r == 0) {
const char *less_charset, *exe;
/* In the child start the pager */
(void) dup2(fd[0], STDIN_FILENO);
safe_close_pair(fd);
if (setenv("LESS", less_opts, 1) < 0)
_exit(EXIT_FAILURE);
@ -111,8 +155,12 @@ int pager_open(PagerFlags flags) {
setenv("LESSCHARSET", less_charset, 1) < 0)
_exit(EXIT_FAILURE);
if (pager_args)
if (pager_args) {
if (loop_write(exe_name_pipe[1], pager_args[0], strlen(pager_args[0]) + 1, false) < 0)
_exit(EXIT_FAILURE);
execvp(pager_args[0], pager_args);
}
/* Debian's alternatives command for pagers is
* called 'pager'. Note that we do not call
@ -120,11 +168,14 @@ int pager_open(PagerFlags flags) {
* shell script that implements a logic that
* is similar to this one anyway, but is
* Debian-specific. */
execlp("pager", "pager", NULL);
execlp("less", "less", NULL);
execlp("more", "more", NULL);
FOREACH_STRING(exe, "pager", "less", "more") {
if (loop_write(exe_name_pipe[1], exe, strlen(exe) + 1, false) < 0)
_exit(EXIT_FAILURE);
execlp(exe, exe, NULL);
}
if (loop_write(exe_name_pipe[1], "(built-in)", strlen("(built-in") + 1, false) < 0)
_exit(EXIT_FAILURE);
pager_fallback();
/* not reached */
}
@ -144,6 +195,14 @@ int pager_open(PagerFlags flags) {
}
stderr_redirected = true;
exe_name_pipe[1] = safe_close(exe_name_pipe[1]);
r = no_quit_on_interrupt(TAKE_FD(exe_name_pipe[0]), less_opts);
if (r < 0)
return r;
if (r > 0)
(void) ignore_signals(SIGINT, -1);
return 1;
}