]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
basic/pager: ignore ^C when piping to less and K is not set
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Sun, 11 Nov 2018 18:04:00 +0000 (19:04 +0100)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Wed, 14 Nov 2018 15:53:50 +0000 (16:53 +0100)
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
src/basic/pager.c

index a3faa3899754ebbe5502846a988e3896e8e75f5f..eb332b56b0b46a071998e4eaeda1417e7da0620d 100644 (file)
       <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'>
index 0c17ed5d98ac0f05cbe5140c02c22116910dbc02..88d9ef349e7ee00d213d35ed60204094ddea4afc 100644 (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,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;
 }