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