Merge pull request #10753 from keszybz/pager-no-interrupt
Add mode in journalctl where ^C is handled by the pager
This commit is contained in:
@@ -9,8 +9,6 @@
|
||||
#include <sys/types.h>
|
||||
|
||||
#include "def.h"
|
||||
#include "hashmap.h"
|
||||
#include "macro.h"
|
||||
#include "set.h"
|
||||
|
||||
#define SYSTEMD_CGROUP_CONTROLLER_LEGACY "name=systemd"
|
||||
|
||||
@@ -12,7 +12,6 @@
|
||||
|
||||
#include "alloc-util.h"
|
||||
#include "clock-util.h"
|
||||
#include "def.h"
|
||||
#include "fd-util.h"
|
||||
#include "fileio.h"
|
||||
#include "macro.h"
|
||||
|
||||
@@ -74,6 +74,4 @@
|
||||
"/usr/lib/" n \
|
||||
_CONF_PATHS_SPLIT_USR(n))
|
||||
|
||||
#define LONG_LINE_MAX (1U*1024U*1024U)
|
||||
|
||||
#define HIGH_RLIMIT_MEMLOCK (1024ULL*1024ULL*64ULL)
|
||||
|
||||
@@ -9,7 +9,6 @@
|
||||
|
||||
#include "alloc-util.h"
|
||||
#include "conf-files.h"
|
||||
#include "def.h"
|
||||
#include "env-util.h"
|
||||
#include "exec-util.h"
|
||||
#include "fd-util.h"
|
||||
|
||||
@@ -15,7 +15,6 @@
|
||||
|
||||
#include "alloc-util.h"
|
||||
#include "ctype.h"
|
||||
#include "def.h"
|
||||
#include "env-util.h"
|
||||
#include "escape.h"
|
||||
#include "fd-util.h"
|
||||
|
||||
@@ -10,6 +10,8 @@
|
||||
#include "macro.h"
|
||||
#include "time-util.h"
|
||||
|
||||
#define LONG_LINE_MAX (1U*1024U*1024U)
|
||||
|
||||
typedef enum {
|
||||
WRITE_STRING_FILE_CREATE = 1 << 0,
|
||||
WRITE_STRING_FILE_ATOMIC = 1 << 1,
|
||||
|
||||
@@ -8,7 +8,6 @@
|
||||
#include <unistd.h>
|
||||
|
||||
#include "alloc-util.h"
|
||||
#include "def.h"
|
||||
#include "fd-util.h"
|
||||
#include "fileio.h"
|
||||
#include "hostname-util.h"
|
||||
|
||||
@@ -13,7 +13,6 @@
|
||||
#include <libmount.h>
|
||||
|
||||
#include "alloc-util.h"
|
||||
#include "def.h"
|
||||
#include "escape.h"
|
||||
#include "extract-word.h"
|
||||
#include "fd-util.h"
|
||||
|
||||
+76
-17
@@ -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,13 +43,50 @@ _noreturn_ static void pager_fallback(void) {
|
||||
_exit(EXIT_SUCCESS);
|
||||
}
|
||||
|
||||
int pager_open(bool no_pager, bool jump_to_end) {
|
||||
_cleanup_close_pair_ int fd[2] = { -1, -1 };
|
||||
_cleanup_strv_free_ char **pager_args = NULL;
|
||||
const char *pager;
|
||||
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;
|
||||
|
||||
if (no_pager)
|
||||
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 }, exe_name_pipe[2] = { -1, -1 };
|
||||
_cleanup_strv_free_ char **pager_args = NULL;
|
||||
const char *pager, *less_opts;
|
||||
int r;
|
||||
|
||||
if (flags & PAGER_DISABLE)
|
||||
return 0;
|
||||
|
||||
if (pager_pid > 0)
|
||||
@@ -81,23 +120,28 @@ int pager_open(bool no_pager, bool jump_to_end) {
|
||||
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 (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(bool no_pager, bool jump_to_end) {
|
||||
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(bool no_pager, bool jump_to_end) {
|
||||
* 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(bool no_pager, bool jump_to_end) {
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
+6
-1
@@ -5,7 +5,12 @@
|
||||
|
||||
#include "macro.h"
|
||||
|
||||
int pager_open(bool no_pager, bool jump_to_end);
|
||||
typedef enum PagerFlags {
|
||||
PAGER_DISABLE = 1 << 0,
|
||||
PAGER_JUMP_TO_END = 1 << 1,
|
||||
} PagerFlags;
|
||||
|
||||
int pager_open(PagerFlags flags);
|
||||
void pager_close(void);
|
||||
bool pager_have(void) _pure_;
|
||||
|
||||
|
||||
@@ -25,7 +25,6 @@
|
||||
|
||||
#include "alloc-util.h"
|
||||
#include "architecture.h"
|
||||
#include "def.h"
|
||||
#include "escape.h"
|
||||
#include "fd-util.h"
|
||||
#include "fileio.h"
|
||||
|
||||
@@ -14,7 +14,6 @@
|
||||
#include <unistd.h>
|
||||
|
||||
#include "alloc-util.h"
|
||||
#include "def.h"
|
||||
#include "fd-util.h"
|
||||
#include "fileio.h"
|
||||
#include "fs-util.h"
|
||||
|
||||
@@ -11,7 +11,6 @@
|
||||
|
||||
#include "alloc-util.h"
|
||||
#include "dirent-util.h"
|
||||
#include "def.h"
|
||||
#include "env-util.h"
|
||||
#include "fd-util.h"
|
||||
#include "fileio.h"
|
||||
|
||||
Reference in New Issue
Block a user