From 6432da6a692268c52f90dd6d247c35a5c7da4e27 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Sun, 11 Nov 2018 19:04:00 +0100 Subject: [PATCH] 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. --- man/less-variables.xml | 8 +++- src/basic/pager.c | 87 +++++++++++++++++++++++++++++++++++------- 2 files changed, 80 insertions(+), 15 deletions(-) diff --git a/man/less-variables.xml b/man/less-variables.xml index a3faa389975..eb332b56b0b 100644 --- a/man/less-variables.xml +++ b/man/less-variables.xml @@ -26,7 +26,13 @@ $SYSTEMD_LESS Override the options passed to less (by default - FRSXMK). + FRSXMK). + + If the value of $SYSTEMD_LESS does not include K, + and the pager that is invoked is less, + CtrlC will be ignored by the + executable. This allows less to handle + CtrlC itself. diff --git a/src/basic/pager.c b/src/basic/pager.c index 0c17ed5d98a..88d9ef349e7 100644 --- a/src/basic/pager.c +++ b/src/basic/pager.c @@ -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,23 +120,28 @@ int pager_open(PagerFlags flags) { if (pipe2(fd, O_CLOEXEC) < 0) return log_error_errno(errno, "Failed to create pager pipe: %m"); + /* 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"); + if (!less_opts) + 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_opts, *less_charset; + const char *less_charset, *exe; /* In the child start the pager */ (void) dup2(fd[0], STDIN_FILENO); safe_close_pair(fd); - /* Initialize a good set of less options */ - less_opts = getenv("SYSTEMD_LESS"); - if (!less_opts) - less_opts = "FRSXMK"; - if (flags & PAGER_JUMP_TO_END) - less_opts = strjoina(less_opts, " +G"); 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; } -- 2.39.2