]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/pager.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
18 #include "locale-util.h"
22 #include "process-util.h"
23 #include "rlimit-util.h"
24 #include "signal-util.h"
25 #include "string-util.h"
27 #include "terminal-util.h"
30 static pid_t pager_pid
= 0;
32 static int stored_stdout
= -1;
33 static int stored_stderr
= -1;
34 static bool stdout_redirected
= false;
35 static bool stderr_redirected
= false;
37 _noreturn_
static void pager_fallback(void) {
40 r
= copy_bytes(STDIN_FILENO
, STDOUT_FILENO
, UINT64_MAX
, 0);
42 log_error_errno(r
, "Internal pager failed: %m");
49 static int no_quit_on_interrupt(int exe_name_fd
, const char *less_opts
) {
50 _cleanup_fclose_
FILE *file
= NULL
;
51 _cleanup_free_
char *line
= NULL
;
54 assert(exe_name_fd
>= 0);
57 /* This takes ownership of exe_name_fd */
58 file
= fdopen(exe_name_fd
, "r");
60 safe_close(exe_name_fd
);
61 return log_error_errno(errno
, "Failed to create FILE object: %m");
64 /* Find the last line */
66 _cleanup_free_
char *t
= NULL
;
68 r
= read_line(file
, LONG_LINE_MAX
, &t
);
70 return log_error_errno(r
, "Failed to read from socket: %m");
74 free_and_replace(line
, t
);
77 /* We only treat "less" specially.
78 * Return true whenever option K is *not* set. */
79 r
= streq_ptr(line
, "less") && !strchr(less_opts
, 'K');
81 log_debug("Pager executable is \"%s\", options \"%s\", quit_on_interrupt: %s",
82 strnull(line
), less_opts
, yes_no(!r
));
86 void pager_open(PagerFlags flags
) {
87 _cleanup_close_pair_
int fd
[2] = { -1, -1 }, exe_name_pipe
[2] = { -1, -1 };
88 _cleanup_strv_free_
char **pager_args
= NULL
;
89 _cleanup_free_
char *l
= NULL
;
90 const char *pager
, *less_opts
;
93 if (flags
& PAGER_DISABLE
)
99 if (terminal_is_dumb())
102 if (!is_main_thread())
103 return (void) log_error_errno(SYNTHETIC_ERRNO(EPERM
), "Pager invoked from wrong thread.");
105 pager
= getenv("SYSTEMD_PAGER");
107 pager
= getenv("PAGER");
110 pager_args
= strv_split(pager
, WHITESPACE
);
112 return (void) log_oom();
114 /* If the pager is explicitly turned off, honour it */
115 if (strv_isempty(pager_args
) || strv_equal(pager_args
, STRV_MAKE("cat")))
119 /* Determine and cache number of columns/lines before we spawn the pager so that we get the value from the
124 if (pipe2(fd
, O_CLOEXEC
) < 0)
125 return (void) log_error_errno(errno
, "Failed to create pager pipe: %m");
127 /* This is a pipe to feed the name of the executed pager binary into the parent */
128 if (pipe2(exe_name_pipe
, O_CLOEXEC
) < 0)
129 return (void) log_error_errno(errno
, "Failed to create exe_name pipe: %m");
131 /* Initialize a good set of less options */
132 less_opts
= getenv("SYSTEMD_LESS");
134 less_opts
= "FRSXMK";
135 if (flags
& PAGER_JUMP_TO_END
) {
136 l
= strjoin(less_opts
, " +G");
138 return (void) log_oom();
142 /* We set SIGINT as PR_DEATHSIG signal here, to match the "K" parameter we set in $LESS, which enables SIGINT behaviour. */
143 r
= safe_fork("(pager)", FORK_RESET_SIGNALS
|FORK_DEATHSIG_SIGINT
|FORK_RLIMIT_NOFILE_SAFE
|FORK_LOG
, &pager_pid
);
147 const char *less_charset
, *exe
;
149 /* In the child start the pager */
151 if (dup2(fd
[0], STDIN_FILENO
) < 0) {
152 log_error_errno(errno
, "Failed to duplicate file descriptor to STDIN: %m");
158 if (setenv("LESS", less_opts
, 1) < 0) {
159 log_error_errno(errno
, "Failed to set environment variable LESS: %m");
163 /* Initialize a good charset for less. This is particularly important if we output UTF-8
165 less_charset
= getenv("SYSTEMD_LESSCHARSET");
166 if (!less_charset
&& is_locale_utf8())
167 less_charset
= "utf-8";
169 setenv("LESSCHARSET", less_charset
, 1) < 0) {
170 log_error_errno(errno
, "Failed to set environment variable LESSCHARSET: %m");
174 /* People might invoke us from sudo, don't needlessly allow less to be a way to shell out
175 * privileged stuff. If the user set $SYSTEMD_PAGERSECURE, trust their configuration of the
176 * pager. If they didn't, use secure mode when under euid is changed. If $SYSTEMD_PAGERSECURE
177 * wasn't explicitly set, and we autodetect the need for secure mode, only use the pager we
178 * know to be good. */
179 int use_secure_mode
= getenv_bool_secure("SYSTEMD_PAGERSECURE");
180 bool trust_pager
= use_secure_mode
>= 0;
181 if (use_secure_mode
== -ENXIO
) {
184 r
= sd_pid_get_owner_uid(0, &uid
);
186 log_debug_errno(r
, "sd_pid_get_owner_uid() failed, enabling pager secure mode: %m");
188 use_secure_mode
= r
< 0 || uid
!= geteuid();
190 } else if (use_secure_mode
< 0) {
191 log_warning_errno(use_secure_mode
, "Unable to parse $SYSTEMD_PAGERSECURE, assuming true: %m");
192 use_secure_mode
= true;
195 /* We generally always set variables used by less, even if we end up using a different pager.
196 * They shouldn't hurt in any case, and ideally other pagers would look at them too. */
197 r
= set_unset_env("LESSSECURE", use_secure_mode
? "1" : NULL
, true);
199 log_error_errno(r
, "Failed to adjust environment variable LESSSECURE: %m");
203 if (trust_pager
&& pager_args
) { /* The pager config might be set globally, and we cannot
204 * know if the user adjusted it to be appropriate for the
205 * secure mode. Thus, start the pager specified through
206 * envvars only when $SYSTEMD_PAGERSECURE was explicitly set
208 r
= loop_write(exe_name_pipe
[1], pager_args
[0], strlen(pager_args
[0]) + 1, false);
210 log_error_errno(r
, "Failed to write pager name to socket: %m");
214 execvp(pager_args
[0], pager_args
);
215 log_full_errno(errno
== ENOENT
? LOG_DEBUG
: LOG_WARNING
, errno
,
216 "Failed to execute '%s', using fallback pagers: %m", pager_args
[0]);
219 /* Debian's alternatives command for pagers is called 'pager'. Note that we do not call
220 * sensible-pagers here, since that is just a shell script that implements a logic that is
221 * similar to this one anyway, but is Debian-specific. */
222 FOREACH_STRING(exe
, "pager", "less", "more") {
223 /* Only less implements secure mode right now. */
224 if (use_secure_mode
&& !streq(exe
, "less"))
227 r
= loop_write(exe_name_pipe
[1], exe
, strlen(exe
) + 1, false);
229 log_error_errno(r
, "Failed to write pager name to socket: %m");
232 execlp(exe
, exe
, NULL
);
233 log_full_errno(errno
== ENOENT
? LOG_DEBUG
: LOG_WARNING
, errno
,
234 "Failed to execute '%s', using next fallback pager: %m", exe
);
237 /* Our builtin is also very secure. */
238 r
= loop_write(exe_name_pipe
[1], "(built-in)", strlen("(built-in)") + 1, false);
240 log_error_errno(r
, "Failed to write pager name to socket: %m");
243 /* Close pipe to signal the parent to start sending data */
244 safe_close_pair(exe_name_pipe
);
249 /* Return in the parent */
250 stored_stdout
= fcntl(STDOUT_FILENO
, F_DUPFD_CLOEXEC
, 3);
251 if (dup2(fd
[1], STDOUT_FILENO
) < 0) {
252 stored_stdout
= safe_close(stored_stdout
);
253 return (void) log_error_errno(errno
, "Failed to duplicate pager pipe: %m");
255 stdout_redirected
= true;
257 stored_stderr
= fcntl(STDERR_FILENO
, F_DUPFD_CLOEXEC
, 3);
258 if (dup2(fd
[1], STDERR_FILENO
) < 0) {
259 stored_stderr
= safe_close(stored_stderr
);
260 return (void) log_error_errno(errno
, "Failed to duplicate pager pipe: %m");
262 stderr_redirected
= true;
264 exe_name_pipe
[1] = safe_close(exe_name_pipe
[1]);
266 r
= no_quit_on_interrupt(TAKE_FD(exe_name_pipe
[0]), less_opts
);
268 (void) ignore_signals(SIGINT
);
271 void pager_close(void) {
276 /* Inform pager that we are done */
277 (void) fflush(stdout
);
278 if (stdout_redirected
)
279 if (stored_stdout
< 0 || dup2(stored_stdout
, STDOUT_FILENO
) < 0)
280 (void) close(STDOUT_FILENO
);
281 stored_stdout
= safe_close(stored_stdout
);
282 (void) fflush(stderr
);
283 if (stderr_redirected
)
284 if (stored_stderr
< 0 || dup2(stored_stderr
, STDERR_FILENO
) < 0)
285 (void) close(STDERR_FILENO
);
286 stored_stderr
= safe_close(stored_stderr
);
287 stdout_redirected
= stderr_redirected
= false;
289 (void) kill(pager_pid
, SIGCONT
);
290 (void) wait_for_terminate(TAKE_PID(pager_pid
), NULL
);
294 bool pager_have(void) {
295 return pager_pid
> 0;
298 int show_man_page(const char *desc
, bool null_stdio
) {
299 const char *args
[4] = { "man", NULL
, NULL
, NULL
};
307 if (desc
[k
-1] == ')')
308 e
= strrchr(desc
, '(');
311 char *page
= NULL
, *section
= NULL
;
313 page
= strndupa_safe(desc
, e
- desc
);
314 section
= strndupa_safe(e
+ 1, desc
+ k
- e
- 2);
321 r
= safe_fork("(man)", FORK_RESET_SIGNALS
|FORK_DEATHSIG
|(null_stdio
? FORK_NULL_STDIO
: 0)|FORK_RLIMIT_NOFILE_SAFE
|FORK_LOG
, &pid
);
326 execvp(args
[0], (char**) args
);
327 log_error_errno(errno
, "Failed to execute man: %m");
331 return wait_for_terminate_and_check(NULL
, pid
, 0);