]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/pager.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
10 #include <sys/prctl.h>
17 #include "locale-util.h"
21 #include "process-util.h"
22 #include "rlimit-util.h"
23 #include "signal-util.h"
24 #include "string-util.h"
26 #include "terminal-util.h"
29 static pid_t pager_pid
= 0;
31 static int stored_stdout
= -1;
32 static int stored_stderr
= -1;
33 static bool stdout_redirected
= false;
34 static bool stderr_redirected
= false;
36 _noreturn_
static void pager_fallback(void) {
39 r
= copy_bytes(STDIN_FILENO
, STDOUT_FILENO
, (uint64_t) -1, 0);
41 log_error_errno(r
, "Internal pager failed: %m");
48 static int no_quit_on_interrupt(int exe_name_fd
, const char *less_opts
) {
49 _cleanup_fclose_
FILE *file
= NULL
;
50 _cleanup_free_
char *line
= NULL
;
53 assert(exe_name_fd
>= 0);
56 /* This takes ownership of exe_name_fd */
57 file
= fdopen(exe_name_fd
, "r");
59 safe_close(exe_name_fd
);
60 return log_error_errno(errno
, "Failed to create FILE object: %m");
63 /* Find the last line */
65 _cleanup_free_
char *t
= NULL
;
67 r
= read_line(file
, LONG_LINE_MAX
, &t
);
69 return log_error_errno(r
, "Failed to read from socket: %m");
73 free_and_replace(line
, t
);
76 /* We only treat "less" specially.
77 * Return true whenever option K is *not* set. */
78 r
= streq_ptr(line
, "less") && !strchr(less_opts
, 'K');
80 log_debug("Pager executable is \"%s\", options \"%s\", quit_on_interrupt: %s",
81 strnull(line
), less_opts
, yes_no(!r
));
85 int pager_open(PagerFlags flags
) {
86 _cleanup_close_pair_
int fd
[2] = { -1, -1 }, exe_name_pipe
[2] = { -1, -1 };
87 _cleanup_strv_free_
char **pager_args
= NULL
;
88 const char *pager
, *less_opts
;
91 if (flags
& PAGER_DISABLE
)
97 if (terminal_is_dumb())
100 if (!is_main_thread())
101 return log_error_errno(SYNTHETIC_ERRNO(EPERM
), "Pager invoked from wrong thread.");
103 pager
= getenv("SYSTEMD_PAGER");
105 pager
= getenv("PAGER");
108 pager_args
= strv_split(pager
, WHITESPACE
);
112 /* If the pager is explicitly turned off, honour it */
113 if (strv_isempty(pager_args
) || strv_equal(pager_args
, STRV_MAKE("cat")))
117 /* Determine and cache number of columns/lines before we spawn the pager so that we get the value from the
122 if (pipe2(fd
, O_CLOEXEC
) < 0)
123 return log_error_errno(errno
, "Failed to create pager pipe: %m");
125 /* This is a pipe to feed the name of the executed pager binary into the parent */
126 if (pipe2(exe_name_pipe
, O_CLOEXEC
) < 0)
127 return log_error_errno(errno
, "Failed to create exe_name pipe: %m");
129 /* Initialize a good set of less options */
130 less_opts
= getenv("SYSTEMD_LESS");
132 less_opts
= "FRSXMK";
133 if (flags
& PAGER_JUMP_TO_END
)
134 less_opts
= strjoina(less_opts
, " +G");
136 r
= safe_fork("(pager)", FORK_RESET_SIGNALS
|FORK_DEATHSIG
|FORK_RLIMIT_NOFILE_SAFE
|FORK_LOG
, &pager_pid
);
140 const char *less_charset
, *exe
;
142 /* In the child start the pager */
144 if (dup2(fd
[0], STDIN_FILENO
) < 0) {
145 log_error_errno(errno
, "Failed to duplicate file descriptor to STDIN: %m");
151 if (setenv("LESS", less_opts
, 1) < 0) {
152 log_error_errno(errno
, "Failed to set environment variable LESS: %m");
156 /* Initialize a good charset for less. This is
157 * particularly important if we output UTF-8
159 less_charset
= getenv("SYSTEMD_LESSCHARSET");
160 if (!less_charset
&& is_locale_utf8())
161 less_charset
= "utf-8";
163 setenv("LESSCHARSET", less_charset
, 1) < 0) {
164 log_error_errno(errno
, "Failed to set environment variable LESSCHARSET: %m");
169 r
= loop_write(exe_name_pipe
[1], pager_args
[0], strlen(pager_args
[0]) + 1, false);
171 log_error_errno(r
, "Failed to write pager name to socket: %m");
175 execvp(pager_args
[0], pager_args
);
176 log_full_errno(errno
== ENOENT
? LOG_DEBUG
: LOG_WARNING
, errno
,
177 "Failed to execute '%s', using fallback pagers: %m", pager_args
[0]);
180 /* Debian's alternatives command for pagers is
181 * called 'pager'. Note that we do not call
182 * sensible-pagers here, since that is just a
183 * shell script that implements a logic that
184 * is similar to this one anyway, but is
185 * Debian-specific. */
186 FOREACH_STRING(exe
, "pager", "less", "more") {
187 r
= loop_write(exe_name_pipe
[1], exe
, strlen(exe
) + 1, false);
189 log_error_errno(r
, "Failed to write pager name to socket: %m");
192 execlp(exe
, exe
, NULL
);
193 log_full_errno(errno
== ENOENT
? LOG_DEBUG
: LOG_WARNING
, errno
,
194 "Failed to execute '%s', using next fallback pager: %m", exe
);
197 r
= loop_write(exe_name_pipe
[1], "(built-in)", strlen("(built-in") + 1, false);
199 log_error_errno(r
, "Failed to write pager name to socket: %m");
206 /* Return in the parent */
207 stored_stdout
= fcntl(STDOUT_FILENO
, F_DUPFD_CLOEXEC
, 3);
208 if (dup2(fd
[1], STDOUT_FILENO
) < 0) {
209 stored_stdout
= safe_close(stored_stdout
);
210 return log_error_errno(errno
, "Failed to duplicate pager pipe: %m");
212 stdout_redirected
= true;
214 stored_stderr
= fcntl(STDERR_FILENO
, F_DUPFD_CLOEXEC
, 3);
215 if (dup2(fd
[1], STDERR_FILENO
) < 0) {
216 stored_stderr
= safe_close(stored_stderr
);
217 return log_error_errno(errno
, "Failed to duplicate pager pipe: %m");
219 stderr_redirected
= true;
221 exe_name_pipe
[1] = safe_close(exe_name_pipe
[1]);
223 r
= no_quit_on_interrupt(TAKE_FD(exe_name_pipe
[0]), less_opts
);
227 (void) ignore_signals(SIGINT
, -1);
232 void pager_close(void) {
237 /* Inform pager that we are done */
238 (void) fflush(stdout
);
239 if (stdout_redirected
)
240 if (stored_stdout
< 0 || dup2(stored_stdout
, STDOUT_FILENO
) < 0)
241 (void) close(STDOUT_FILENO
);
242 stored_stdout
= safe_close(stored_stdout
);
243 (void) fflush(stderr
);
244 if (stderr_redirected
)
245 if (stored_stderr
< 0 || dup2(stored_stderr
, STDERR_FILENO
) < 0)
246 (void) close(STDERR_FILENO
);
247 stored_stderr
= safe_close(stored_stderr
);
248 stdout_redirected
= stderr_redirected
= false;
250 (void) kill(pager_pid
, SIGCONT
);
251 (void) wait_for_terminate(pager_pid
, NULL
);
255 bool pager_have(void) {
256 return pager_pid
> 0;
259 int show_man_page(const char *desc
, bool null_stdio
) {
260 const char *args
[4] = { "man", NULL
, NULL
, NULL
};
268 if (desc
[k
-1] == ')')
269 e
= strrchr(desc
, '(');
272 char *page
= NULL
, *section
= NULL
;
274 page
= strndupa(desc
, e
- desc
);
275 section
= strndupa(e
+ 1, desc
+ k
- e
- 2);
282 r
= safe_fork("(man)", FORK_RESET_SIGNALS
|FORK_DEATHSIG
|(null_stdio
? FORK_NULL_STDIO
: 0)|FORK_RLIMIT_NOFILE_SAFE
|FORK_LOG
, &pid
);
287 execvp(args
[0], (char**) args
);
288 log_error_errno(errno
, "Failed to execute man: %m");
292 return wait_for_terminate_and_check(NULL
, pid
, 0);