]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/shared/pager.c
cryptsetup: Add dependency for detached header
[thirdparty/systemd.git] / src / shared / pager.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
1968a360 2
a8fbdf54
TA
3#include <errno.h>
4#include <signal.h>
5#include <stddef.h>
6#include <stdint.h>
7#include <stdio.h>
1968a360 8#include <stdlib.h>
1968a360
LP
9#include <string.h>
10#include <sys/prctl.h>
07630cea 11#include <unistd.h>
1968a360 12
07630cea 13#include "copy.h"
3ffd4af2 14#include "fd-util.h"
6432da6a
ZJS
15#include "fileio.h"
16#include "io-util.h"
8752c575 17#include "locale-util.h"
a8fbdf54 18#include "log.h"
1968a360 19#include "macro.h"
3ffd4af2 20#include "pager.h"
07630cea 21#include "process-util.h"
595225af 22#include "rlimit-util.h"
ce30c8dc 23#include "signal-util.h"
07630cea 24#include "string-util.h"
57c9e047 25#include "strv.h"
07630cea 26#include "terminal-util.h"
ca78ad1d 27#include "util.h"
1968a360
LP
28
29static pid_t pager_pid = 0;
30
3f603952
LP
31static int stored_stdout = -1;
32static int stored_stderr = -1;
33static bool stdout_redirected = false;
34static bool stderr_redirected = false;
35
848e863a 36_noreturn_ static void pager_fallback(void) {
6a7c676c 37 int r;
46e65dcc 38
1c876927 39 r = copy_bytes(STDIN_FILENO, STDOUT_FILENO, (uint64_t) -1, 0);
6a7c676c
LP
40 if (r < 0) {
41 log_error_errno(r, "Internal pager failed: %m");
4a8e40eb
MS
42 _exit(EXIT_FAILURE);
43 }
46e65dcc 44
4a8e40eb
MS
45 _exit(EXIT_SUCCESS);
46}
47
6432da6a
ZJS
48static 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;
51 int r;
52
53 assert(exe_name_fd >= 0);
54 assert(less_opts);
55
56 /* This takes ownership of exe_name_fd */
57 file = fdopen(exe_name_fd, "r");
58 if (!file) {
59 safe_close(exe_name_fd);
1d788908 60 return log_error_errno(errno, "Failed to create FILE object: %m");
6432da6a
ZJS
61 }
62
63 /* Find the last line */
64 for (;;) {
65 _cleanup_free_ char *t = NULL;
66
67 r = read_line(file, LONG_LINE_MAX, &t);
68 if (r < 0)
1d788908 69 return log_error_errno(r, "Failed to read from socket: %m");
6432da6a
ZJS
70 if (r == 0)
71 break;
72
73 free_and_replace(line, t);
74 }
75
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');
79
80 log_debug("Pager executable is \"%s\", options \"%s\", quit_on_interrupt: %s",
81 strnull(line), less_opts, yes_no(!r));
82 return r;
83}
84
0221d68a 85int pager_open(PagerFlags flags) {
6432da6a 86 _cleanup_close_pair_ int fd[2] = { -1, -1 }, exe_name_pipe[2] = { -1, -1 };
43942e80 87 _cleanup_strv_free_ char **pager_args = NULL;
6432da6a 88 const char *pager, *less_opts;
4c253ed1 89 int r;
1968a360 90
0221d68a 91 if (flags & PAGER_DISABLE)
ea4b98e6
AK
92 return 0;
93
1968a360 94 if (pager_pid > 0)
f89a3b6f 95 return 1;
1968a360 96
ac96418b 97 if (terminal_is_dumb())
f89a3b6f 98 return 0;
729e3769 99
85afeae8 100 if (!is_main_thread())
1d788908 101 return log_error_errno(SYNTHETIC_ERRNO(EPERM), "Pager invoked from wrong thread.");
85afeae8 102
bcbd61db
LP
103 pager = getenv("SYSTEMD_PAGER");
104 if (!pager)
105 pager = getenv("PAGER");
106
43942e80
YW
107 if (pager) {
108 pager_args = strv_split(pager, WHITESPACE);
109 if (!pager_args)
1d788908 110 return log_oom();
43942e80
YW
111
112 /* If the pager is explicitly turned off, honour it */
113 if (strv_isempty(pager_args) || strv_equal(pager_args, STRV_MAKE("cat")))
114 return 0;
115 }
bcbd61db 116
d13b5227
LP
117 /* Determine and cache number of columns/lines before we spawn the pager so that we get the value from the
118 * actual tty */
bcbd61db 119 (void) columns();
d13b5227 120 (void) lines();
1968a360 121
d262e99e 122 if (pipe2(fd, O_CLOEXEC) < 0)
4a62c710 123 return log_error_errno(errno, "Failed to create pager pipe: %m");
1968a360 124
6432da6a
ZJS
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");
128
129 /* Initialize a good set of less options */
130 less_opts = getenv("SYSTEMD_LESS");
131 if (!less_opts)
132 less_opts = "FRSXMK";
133 if (flags & PAGER_JUMP_TO_END)
134 less_opts = strjoina(less_opts, " +G");
135
a81921e4 136 r = safe_fork("(pager)", FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_RLIMIT_NOFILE_SAFE|FORK_LOG, &pager_pid);
4c253ed1 137 if (r < 0)
b6e1fff1 138 return r;
4c253ed1 139 if (r == 0) {
6432da6a 140 const char *less_charset, *exe;
1968a360 141
4c253ed1 142 /* In the child start the pager */
ce30c8dc 143
1d788908
LP
144 if (dup2(fd[0], STDIN_FILENO) < 0) {
145 log_error_errno(errno, "Failed to duplicate file descriptor to STDIN: %m");
146 _exit(EXIT_FAILURE);
147 }
148
3d94f76c 149 safe_close_pair(fd);
1968a360 150
1d788908
LP
151 if (setenv("LESS", less_opts, 1) < 0) {
152 log_error_errno(errno, "Failed to set environment variable LESS: %m");
0357fa0d 153 _exit(EXIT_FAILURE);
1d788908 154 }
1968a360 155
a1b4e6e9
LP
156 /* Initialize a good charset for less. This is
157 * particularly important if we output UTF-8
158 * characters. */
159 less_charset = getenv("SYSTEMD_LESSCHARSET");
160 if (!less_charset && is_locale_utf8())
161 less_charset = "utf-8";
0357fa0d 162 if (less_charset &&
1d788908
LP
163 setenv("LESSCHARSET", less_charset, 1) < 0) {
164 log_error_errno(errno, "Failed to set environment variable LESSCHARSET: %m");
0357fa0d 165 _exit(EXIT_FAILURE);
1d788908 166 }
a1b4e6e9 167
6432da6a 168 if (pager_args) {
1d788908
LP
169 r = loop_write(exe_name_pipe[1], pager_args[0], strlen(pager_args[0]) + 1, false);
170 if (r < 0) {
171 log_error_errno(r, "Failed to write pager name to socket: %m");
6432da6a 172 _exit(EXIT_FAILURE);
1d788908 173 }
6432da6a 174
43942e80 175 execvp(pager_args[0], pager_args);
1d788908 176 log_full_errno(errno == ENOENT ? LOG_DEBUG : LOG_WARNING, errno,
118dccc9 177 "Failed to execute '%s', using fallback pagers: %m", pager_args[0]);
6432da6a 178 }
1968a360
LP
179
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. */
6432da6a 186 FOREACH_STRING(exe, "pager", "less", "more") {
1d788908
LP
187 r = loop_write(exe_name_pipe[1], exe, strlen(exe) + 1, false);
188 if (r < 0) {
189 log_error_errno(r, "Failed to write pager name to socket: %m");
6432da6a 190 _exit(EXIT_FAILURE);
1d788908 191 }
6432da6a 192 execlp(exe, exe, NULL);
1d788908 193 log_full_errno(errno == ENOENT ? LOG_DEBUG : LOG_WARNING, errno,
118dccc9 194 "Failed to execute '%s', using next fallback pager: %m", exe);
6432da6a 195 }
1968a360 196
1d788908
LP
197 r = loop_write(exe_name_pipe[1], "(built-in)", strlen("(built-in") + 1, false);
198 if (r < 0) {
199 log_error_errno(r, "Failed to write pager name to socket: %m");
6432da6a 200 _exit(EXIT_FAILURE);
1d788908 201 }
4a8e40eb
MS
202 pager_fallback();
203 /* not reached */
1968a360
LP
204 }
205
206 /* Return in the parent */
a45e7bb4
MS
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);
4a62c710 210 return log_error_errno(errno, "Failed to duplicate pager pipe: %m");
a45e7bb4
MS
211 }
212 stdout_redirected = true;
213
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);
8b5264aa 217 return log_error_errno(errno, "Failed to duplicate pager pipe: %m");
a45e7bb4
MS
218 }
219 stderr_redirected = true;
1968a360 220
6432da6a
ZJS
221 exe_name_pipe[1] = safe_close(exe_name_pipe[1]);
222
223 r = no_quit_on_interrupt(TAKE_FD(exe_name_pipe[0]), less_opts);
224 if (r < 0)
225 return r;
226 if (r > 0)
227 (void) ignore_signals(SIGINT, -1);
228
f89a3b6f 229 return 1;
1968a360
LP
230}
231
232void pager_close(void) {
233
234 if (pager_pid <= 0)
235 return;
236
237 /* Inform pager that we are done */
a45e7bb4 238 (void) fflush(stdout);
77018a8c
MS
239 if (stdout_redirected)
240 if (stored_stdout < 0 || dup2(stored_stdout, STDOUT_FILENO) < 0)
241 (void) close(STDOUT_FILENO);
a45e7bb4
MS
242 stored_stdout = safe_close(stored_stdout);
243 (void) fflush(stderr);
77018a8c
MS
244 if (stderr_redirected)
245 if (stored_stderr < 0 || dup2(stored_stderr, STDERR_FILENO) < 0)
246 (void) close(STDERR_FILENO);
a45e7bb4
MS
247 stored_stderr = safe_close(stored_stderr);
248 stdout_redirected = stderr_redirected = false;
8b5264aa 249
74ca738f 250 (void) kill(pager_pid, SIGCONT);
c73d180d 251 (void) wait_for_terminate(pager_pid, NULL);
1968a360
LP
252 pager_pid = 0;
253}
f89a3b6f
LP
254
255bool pager_have(void) {
256 return pager_pid > 0;
257}
78002a67
ZJS
258
259int show_man_page(const char *desc, bool null_stdio) {
260 const char *args[4] = { "man", NULL, NULL, NULL };
261 char *e = NULL;
262 pid_t pid;
263 size_t k;
264 int r;
78002a67
ZJS
265
266 k = strlen(desc);
267
268 if (desc[k-1] == ')')
269 e = strrchr(desc, '(');
270
271 if (e) {
272 char *page = NULL, *section = NULL;
273
274 page = strndupa(desc, e - desc);
275 section = strndupa(e + 1, desc + k - e - 2);
276
277 args[1] = section;
278 args[2] = page;
279 } else
280 args[1] = desc;
281
0672e2c6 282 r = safe_fork("(man)", FORK_RESET_SIGNALS|FORK_DEATHSIG|(null_stdio ? FORK_NULL_STDIO : 0)|FORK_RLIMIT_NOFILE_SAFE|FORK_LOG, &pid);
4c253ed1 283 if (r < 0)
b6e1fff1 284 return r;
4c253ed1 285 if (r == 0) {
78002a67 286 /* Child */
78002a67 287 execvp(args[0], (char**) args);
56f64d95 288 log_error_errno(errno, "Failed to execute man: %m");
78002a67
ZJS
289 _exit(EXIT_FAILURE);
290 }
291
2e87a1fd 292 return wait_for_terminate_and_check(NULL, pid, 0);
78002a67 293}