1 /* SPDX-License-Identifier: LGPL-2.1+ */
10 #include <sys/prctl.h>
15 #include "locale-util.h"
19 #include "process-util.h"
20 #include "signal-util.h"
21 #include "string-util.h"
23 #include "terminal-util.h"
25 static pid_t pager_pid = 0;
27 static int stored_stdout = -1;
28 static int stored_stderr = -1;
29 static bool stdout_redirected = false;
30 static bool stderr_redirected = false;
32 _noreturn_ static void pager_fallback(void) {
35 r = copy_bytes(STDIN_FILENO, STDOUT_FILENO, (uint64_t) -1, 0);
37 log_error_errno(r, "Internal pager failed: %m");
44 int pager_open(bool no_pager, bool jump_to_end) {
45 _cleanup_close_pair_ int fd[2] = { -1, -1 };
55 if (terminal_is_dumb())
58 if (!is_main_thread())
61 pager = getenv("SYSTEMD_PAGER");
63 pager = getenv("PAGER");
65 /* If the pager is explicitly turned off, honour it */
66 if (pager && STR_IN_SET(pager, "", "cat"))
69 /* Determine and cache number of columns/lines before we spawn the pager so that we get the value from the
74 if (pipe2(fd, O_CLOEXEC) < 0)
75 return log_error_errno(errno, "Failed to create pager pipe: %m");
77 r = safe_fork("(pager)", FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_LOG, &pager_pid);
81 const char* less_opts, *less_charset;
83 /* In the child start the pager */
85 (void) dup2(fd[0], STDIN_FILENO);
88 /* Initialize a good set of less options */
89 less_opts = getenv("SYSTEMD_LESS");
93 less_opts = strjoina(less_opts, " +G");
94 if (setenv("LESS", less_opts, 1) < 0)
97 /* Initialize a good charset for less. This is
98 * particularly important if we output UTF-8
100 less_charset = getenv("SYSTEMD_LESSCHARSET");
101 if (!less_charset && is_locale_utf8())
102 less_charset = "utf-8";
104 setenv("LESSCHARSET", less_charset, 1) < 0)
108 execlp(pager, pager, NULL);
109 execl("/bin/sh", "sh", "-c", pager, NULL);
112 /* Debian's alternatives command for pagers is
113 * called 'pager'. Note that we do not call
114 * sensible-pagers here, since that is just a
115 * shell script that implements a logic that
116 * is similar to this one anyway, but is
117 * Debian-specific. */
118 execlp("pager", "pager", NULL);
120 execlp("less", "less", NULL);
121 execlp("more", "more", NULL);
127 /* Return in the parent */
128 stored_stdout = fcntl(STDOUT_FILENO, F_DUPFD_CLOEXEC, 3);
129 if (dup2(fd[1], STDOUT_FILENO) < 0) {
130 stored_stdout = safe_close(stored_stdout);
131 return log_error_errno(errno, "Failed to duplicate pager pipe: %m");
133 stdout_redirected = true;
135 stored_stderr = fcntl(STDERR_FILENO, F_DUPFD_CLOEXEC, 3);
136 if (dup2(fd[1], STDERR_FILENO) < 0) {
137 stored_stderr = safe_close(stored_stderr);
138 return log_error_errno(errno, "Failed to duplicate pager pipe: %m");
140 stderr_redirected = true;
145 void pager_close(void) {
150 /* Inform pager that we are done */
151 (void) fflush(stdout);
152 if (stdout_redirected)
153 if (stored_stdout < 0 || dup2(stored_stdout, STDOUT_FILENO) < 0)
154 (void) close(STDOUT_FILENO);
155 stored_stdout = safe_close(stored_stdout);
156 (void) fflush(stderr);
157 if (stderr_redirected)
158 if (stored_stderr < 0 || dup2(stored_stderr, STDERR_FILENO) < 0)
159 (void) close(STDERR_FILENO);
160 stored_stderr = safe_close(stored_stderr);
161 stdout_redirected = stderr_redirected = false;
163 (void) kill(pager_pid, SIGCONT);
164 (void) wait_for_terminate(pager_pid, NULL);
168 bool pager_have(void) {
169 return pager_pid > 0;
172 #if 0 /// UNNEEDED by elogind
173 int show_man_page(const char *desc, bool null_stdio) {
174 const char *args[4] = { "man", NULL, NULL, NULL };
182 if (desc[k-1] == ')')
183 e = strrchr(desc, '(');
186 char *page = NULL, *section = NULL;
188 page = strndupa(desc, e - desc);
189 section = strndupa(e + 1, desc + k - e - 2);
196 r = safe_fork("(man)", FORK_RESET_SIGNALS|FORK_DEATHSIG|(null_stdio ? FORK_NULL_STDIO : 0)|FORK_LOG, &pid);
201 execvp(args[0], (char**) args);
202 log_error_errno(errno, "Failed to execute man: %m");
206 return wait_for_terminate_and_check(NULL, pid, 0);