]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/shared/pager.c
zsh: remove _files prefixes
[thirdparty/systemd.git] / src / shared / pager.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
1968a360 2
a8fbdf54 3#include <stdio.h>
1968a360 4#include <stdlib.h>
07630cea 5#include <unistd.h>
1968a360 6
0a42426d
ZJS
7#include "sd-login.h"
8
07630cea 9#include "copy.h"
612ebf6c 10#include "env-util.h"
3ffd4af2 11#include "fd-util.h"
6432da6a
ZJS
12#include "fileio.h"
13#include "io-util.h"
8752c575 14#include "locale-util.h"
a8fbdf54 15#include "log.h"
3ffd4af2 16#include "pager.h"
07630cea 17#include "process-util.h"
ce30c8dc 18#include "signal-util.h"
07630cea 19#include "string-util.h"
57c9e047 20#include "strv.h"
07630cea 21#include "terminal-util.h"
1968a360
LP
22
23static pid_t pager_pid = 0;
24
3f603952
LP
25static int stored_stdout = -1;
26static int stored_stderr = -1;
27static bool stdout_redirected = false;
28static bool stderr_redirected = false;
29
848e863a 30_noreturn_ static void pager_fallback(void) {
6a7c676c 31 int r;
46e65dcc 32
f5fbe71d 33 r = copy_bytes(STDIN_FILENO, STDOUT_FILENO, UINT64_MAX, 0);
6a7c676c
LP
34 if (r < 0) {
35 log_error_errno(r, "Internal pager failed: %m");
4a8e40eb
MS
36 _exit(EXIT_FAILURE);
37 }
46e65dcc 38
4a8e40eb
MS
39 _exit(EXIT_SUCCESS);
40}
41
6432da6a
ZJS
42static int no_quit_on_interrupt(int exe_name_fd, const char *less_opts) {
43 _cleanup_fclose_ FILE *file = NULL;
44 _cleanup_free_ char *line = NULL;
45 int r;
46
47 assert(exe_name_fd >= 0);
48 assert(less_opts);
49
50 /* This takes ownership of exe_name_fd */
51 file = fdopen(exe_name_fd, "r");
52 if (!file) {
53 safe_close(exe_name_fd);
1d788908 54 return log_error_errno(errno, "Failed to create FILE object: %m");
6432da6a
ZJS
55 }
56
57 /* Find the last line */
58 for (;;) {
59 _cleanup_free_ char *t = NULL;
60
61 r = read_line(file, LONG_LINE_MAX, &t);
62 if (r < 0)
1d788908 63 return log_error_errno(r, "Failed to read from socket: %m");
6432da6a
ZJS
64 if (r == 0)
65 break;
66
67 free_and_replace(line, t);
68 }
69
70 /* We only treat "less" specially.
71 * Return true whenever option K is *not* set. */
72 r = streq_ptr(line, "less") && !strchr(less_opts, 'K');
73
74 log_debug("Pager executable is \"%s\", options \"%s\", quit_on_interrupt: %s",
75 strnull(line), less_opts, yes_no(!r));
76 return r;
77}
78
cd93478a
ZJS
79static bool running_with_escalated_privileges(void) {
80 int r;
81
82 if (getenv("SUDO_UID"))
83 return true;
84
85 uid_t uid;
86 r = sd_pid_get_owner_uid(0, &uid);
87 if (r < 0) {
88 log_debug_errno(r, "sd_pid_get_owner_uid() failed, enabling pager secure mode: %m");
89 return true;
90 }
91
92 return uid != geteuid();
93}
94
384c2c32 95void pager_open(PagerFlags flags) {
71136404 96 _cleanup_close_pair_ int fd[2] = EBADF_PAIR, exe_name_pipe[2] = EBADF_PAIR;
43942e80 97 _cleanup_strv_free_ char **pager_args = NULL;
1d3b68f6 98 _cleanup_free_ char *l = NULL;
6432da6a 99 const char *pager, *less_opts;
4c253ed1 100 int r;
1968a360 101
0221d68a 102 if (flags & PAGER_DISABLE)
384c2c32 103 return;
ea4b98e6 104
1968a360 105 if (pager_pid > 0)
384c2c32 106 return;
1968a360 107
ac96418b 108 if (terminal_is_dumb())
384c2c32 109 return;
729e3769 110
85afeae8 111 if (!is_main_thread())
384c2c32 112 return (void) log_error_errno(SYNTHETIC_ERRNO(EPERM), "Pager invoked from wrong thread.");
85afeae8 113
bcbd61db
LP
114 pager = getenv("SYSTEMD_PAGER");
115 if (!pager)
116 pager = getenv("PAGER");
117
43942e80
YW
118 if (pager) {
119 pager_args = strv_split(pager, WHITESPACE);
120 if (!pager_args)
384c2c32 121 return (void) log_oom();
43942e80
YW
122
123 /* If the pager is explicitly turned off, honour it */
124 if (strv_isempty(pager_args) || strv_equal(pager_args, STRV_MAKE("cat")))
384c2c32 125 return;
43942e80 126 }
bcbd61db 127
d13b5227
LP
128 /* Determine and cache number of columns/lines before we spawn the pager so that we get the value from the
129 * actual tty */
bcbd61db 130 (void) columns();
d13b5227 131 (void) lines();
1968a360 132
d262e99e 133 if (pipe2(fd, O_CLOEXEC) < 0)
384c2c32 134 return (void) log_error_errno(errno, "Failed to create pager pipe: %m");
1968a360 135
6432da6a
ZJS
136 /* This is a pipe to feed the name of the executed pager binary into the parent */
137 if (pipe2(exe_name_pipe, O_CLOEXEC) < 0)
384c2c32 138 return (void) log_error_errno(errno, "Failed to create exe_name pipe: %m");
6432da6a
ZJS
139
140 /* Initialize a good set of less options */
141 less_opts = getenv("SYSTEMD_LESS");
142 if (!less_opts)
143 less_opts = "FRSXMK";
1d3b68f6
AZ
144 if (flags & PAGER_JUMP_TO_END) {
145 l = strjoin(less_opts, " +G");
146 if (!l)
147 return (void) log_oom();
148 less_opts = l;
149 }
6432da6a 150
97033ba4
LP
151 /* We set SIGINT as PR_DEATHSIG signal here, to match the "K" parameter we set in $LESS, which enables SIGINT behaviour. */
152 r = safe_fork("(pager)", FORK_RESET_SIGNALS|FORK_DEATHSIG_SIGINT|FORK_RLIMIT_NOFILE_SAFE|FORK_LOG, &pager_pid);
4c253ed1 153 if (r < 0)
384c2c32 154 return;
4c253ed1 155 if (r == 0) {
5980d463 156 const char *less_charset;
1968a360 157
4c253ed1 158 /* In the child start the pager */
ce30c8dc 159
1d788908
LP
160 if (dup2(fd[0], STDIN_FILENO) < 0) {
161 log_error_errno(errno, "Failed to duplicate file descriptor to STDIN: %m");
162 _exit(EXIT_FAILURE);
163 }
164
3d94f76c 165 safe_close_pair(fd);
1968a360 166
1d788908
LP
167 if (setenv("LESS", less_opts, 1) < 0) {
168 log_error_errno(errno, "Failed to set environment variable LESS: %m");
0357fa0d 169 _exit(EXIT_FAILURE);
1d788908 170 }
1968a360 171
612ebf6c 172 /* Initialize a good charset for less. This is particularly important if we output UTF-8
a1b4e6e9
LP
173 * characters. */
174 less_charset = getenv("SYSTEMD_LESSCHARSET");
175 if (!less_charset && is_locale_utf8())
176 less_charset = "utf-8";
0357fa0d 177 if (less_charset &&
1d788908
LP
178 setenv("LESSCHARSET", less_charset, 1) < 0) {
179 log_error_errno(errno, "Failed to set environment variable LESSCHARSET: %m");
0357fa0d 180 _exit(EXIT_FAILURE);
1d788908 181 }
a1b4e6e9 182
612ebf6c 183 /* People might invoke us from sudo, don't needlessly allow less to be a way to shell out
0a42426d
ZJS
184 * privileged stuff. If the user set $SYSTEMD_PAGERSECURE, trust their configuration of the
185 * pager. If they didn't, use secure mode when under euid is changed. If $SYSTEMD_PAGERSECURE
186 * wasn't explicitly set, and we autodetect the need for secure mode, only use the pager we
187 * know to be good. */
efb9b3ba 188 int use_secure_mode = secure_getenv_bool("SYSTEMD_PAGERSECURE");
0a42426d 189 bool trust_pager = use_secure_mode >= 0;
cd93478a
ZJS
190 if (use_secure_mode == -ENXIO)
191 use_secure_mode = running_with_escalated_privileges();
192 else if (use_secure_mode < 0) {
0a42426d
ZJS
193 log_warning_errno(use_secure_mode, "Unable to parse $SYSTEMD_PAGERSECURE, assuming true: %m");
194 use_secure_mode = true;
612ebf6c
LP
195 }
196
0a42426d
ZJS
197 /* We generally always set variables used by less, even if we end up using a different pager.
198 * They shouldn't hurt in any case, and ideally other pagers would look at them too. */
063f9f0d 199 r = set_unset_env("LESSSECURE", use_secure_mode ? "1" : NULL, true);
0a42426d 200 if (r < 0) {
063f9f0d 201 log_error_errno(r, "Failed to adjust environment variable LESSSECURE: %m");
0a42426d
ZJS
202 _exit(EXIT_FAILURE);
203 }
204
205 if (trust_pager && pager_args) { /* The pager config might be set globally, and we cannot
206 * know if the user adjusted it to be appropriate for the
207 * secure mode. Thus, start the pager specified through
208 * envvars only when $SYSTEMD_PAGERSECURE was explicitly set
209 * as well. */
e22c60a9 210 r = loop_write(exe_name_pipe[1], pager_args[0], strlen(pager_args[0]) + 1);
1d788908
LP
211 if (r < 0) {
212 log_error_errno(r, "Failed to write pager name to socket: %m");
6432da6a 213 _exit(EXIT_FAILURE);
1d788908 214 }
6432da6a 215
43942e80 216 execvp(pager_args[0], pager_args);
1d788908 217 log_full_errno(errno == ENOENT ? LOG_DEBUG : LOG_WARNING, errno,
118dccc9 218 "Failed to execute '%s', using fallback pagers: %m", pager_args[0]);
6432da6a 219 }
1968a360 220
0a42426d
ZJS
221 /* Debian's alternatives command for pagers is called 'pager'. Note that we do not call
222 * sensible-pagers here, since that is just a shell script that implements a logic that is
223 * similar to this one anyway, but is Debian-specific. */
b622d2f7
ZJS
224 static const char* pagers[] = { "pager", "less", "more", "(built-in)" };
225
226 for (unsigned i = 0; i < ELEMENTSOF(pagers); i++) {
227 /* Only less (and our trivial fallback) implement secure mode right now. */
228 if (use_secure_mode && !STR_IN_SET(pagers[i], "less", "(built-in)"))
0a42426d
ZJS
229 continue;
230
e22c60a9 231 r = loop_write(exe_name_pipe[1], pagers[i], strlen(pagers[i]) + 1);
b622d2f7 232 if (r < 0) {
1d788908 233 log_error_errno(r, "Failed to write pager name to socket: %m");
6432da6a 234 _exit(EXIT_FAILURE);
1d788908 235 }
1968a360 236
b622d2f7
ZJS
237 if (i < ELEMENTSOF(pagers) - 1) {
238 execlp(pagers[i], pagers[i], NULL);
239 log_full_errno(errno == ENOENT ? LOG_DEBUG : LOG_WARNING, errno,
240 "Failed to execute '%s', will try '%s' next: %m", pagers[i], pagers[i+1]);
241 } else {
242 /* Close pipe to signal the parent to start sending data */
243 safe_close_pair(exe_name_pipe);
244 pager_fallback();
245 assert_not_reached();
246 }
1d788908 247 }
1968a360
LP
248 }
249
250 /* Return in the parent */
a45e7bb4
MS
251 stored_stdout = fcntl(STDOUT_FILENO, F_DUPFD_CLOEXEC, 3);
252 if (dup2(fd[1], STDOUT_FILENO) < 0) {
253 stored_stdout = safe_close(stored_stdout);
384c2c32 254 return (void) log_error_errno(errno, "Failed to duplicate pager pipe: %m");
a45e7bb4
MS
255 }
256 stdout_redirected = true;
257
258 stored_stderr = fcntl(STDERR_FILENO, F_DUPFD_CLOEXEC, 3);
259 if (dup2(fd[1], STDERR_FILENO) < 0) {
260 stored_stderr = safe_close(stored_stderr);
384c2c32 261 return (void) log_error_errno(errno, "Failed to duplicate pager pipe: %m");
a45e7bb4
MS
262 }
263 stderr_redirected = true;
1968a360 264
6432da6a
ZJS
265 exe_name_pipe[1] = safe_close(exe_name_pipe[1]);
266
267 r = no_quit_on_interrupt(TAKE_FD(exe_name_pipe[0]), less_opts);
6432da6a 268 if (r > 0)
9c274488 269 (void) ignore_signals(SIGINT);
1968a360
LP
270}
271
272void pager_close(void) {
273
274 if (pager_pid <= 0)
275 return;
276
277 /* Inform pager that we are done */
a45e7bb4 278 (void) fflush(stdout);
77018a8c
MS
279 if (stdout_redirected)
280 if (stored_stdout < 0 || dup2(stored_stdout, STDOUT_FILENO) < 0)
281 (void) close(STDOUT_FILENO);
a45e7bb4
MS
282 stored_stdout = safe_close(stored_stdout);
283 (void) fflush(stderr);
77018a8c
MS
284 if (stderr_redirected)
285 if (stored_stderr < 0 || dup2(stored_stderr, STDERR_FILENO) < 0)
286 (void) close(STDERR_FILENO);
a45e7bb4
MS
287 stored_stderr = safe_close(stored_stderr);
288 stdout_redirected = stderr_redirected = false;
8b5264aa 289
74ca738f 290 (void) kill(pager_pid, SIGCONT);
8f03de53 291 (void) wait_for_terminate(TAKE_PID(pager_pid), NULL);
1968a360
LP
292 pager_pid = 0;
293}
f89a3b6f
LP
294
295bool pager_have(void) {
296 return pager_pid > 0;
297}
78002a67
ZJS
298
299int show_man_page(const char *desc, bool null_stdio) {
300 const char *args[4] = { "man", NULL, NULL, NULL };
301 char *e = NULL;
302 pid_t pid;
303 size_t k;
304 int r;
78002a67
ZJS
305
306 k = strlen(desc);
307
308 if (desc[k-1] == ')')
309 e = strrchr(desc, '(');
310
311 if (e) {
312 char *page = NULL, *section = NULL;
313
2f82562b
LP
314 page = strndupa_safe(desc, e - desc);
315 section = strndupa_safe(e + 1, desc + k - e - 2);
78002a67
ZJS
316
317 args[1] = section;
318 args[2] = page;
319 } else
320 args[1] = desc;
321
e9ccae31 322 r = safe_fork("(man)", FORK_RESET_SIGNALS|FORK_DEATHSIG_SIGTERM|(null_stdio ? FORK_REARRANGE_STDIO : 0)|FORK_RLIMIT_NOFILE_SAFE|FORK_LOG, &pid);
4c253ed1 323 if (r < 0)
b6e1fff1 324 return r;
4c253ed1 325 if (r == 0) {
78002a67 326 /* Child */
78002a67 327 execvp(args[0], (char**) args);
56f64d95 328 log_error_errno(errno, "Failed to execute man: %m");
78002a67
ZJS
329 _exit(EXIT_FAILURE);
330 }
331
2e87a1fd 332 return wait_for_terminate_and_check(NULL, pid, 0);
78002a67 333}