]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/pager.c
Merge pull request #16112 from poettering/nss-systemd-block-fix
[thirdparty/systemd.git] / src / shared / pager.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <errno.h>
4 #include <stddef.h>
5 #include <stdint.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <sys/prctl.h>
9 #include <unistd.h>
10
11 #include "copy.h"
12 #include "fd-util.h"
13 #include "fileio.h"
14 #include "io-util.h"
15 #include "locale-util.h"
16 #include "log.h"
17 #include "macro.h"
18 #include "pager.h"
19 #include "process-util.h"
20 #include "rlimit-util.h"
21 #include "signal-util.h"
22 #include "string-util.h"
23 #include "strv.h"
24 #include "terminal-util.h"
25 #include "util.h"
26
27 static pid_t pager_pid = 0;
28
29 static int stored_stdout = -1;
30 static int stored_stderr = -1;
31 static bool stdout_redirected = false;
32 static bool stderr_redirected = false;
33
34 _noreturn_ static void pager_fallback(void) {
35 int r;
36
37 r = copy_bytes(STDIN_FILENO, STDOUT_FILENO, (uint64_t) -1, 0);
38 if (r < 0) {
39 log_error_errno(r, "Internal pager failed: %m");
40 _exit(EXIT_FAILURE);
41 }
42
43 _exit(EXIT_SUCCESS);
44 }
45
46 static int no_quit_on_interrupt(int exe_name_fd, const char *less_opts) {
47 _cleanup_fclose_ FILE *file = NULL;
48 _cleanup_free_ char *line = NULL;
49 int r;
50
51 assert(exe_name_fd >= 0);
52 assert(less_opts);
53
54 /* This takes ownership of exe_name_fd */
55 file = fdopen(exe_name_fd, "r");
56 if (!file) {
57 safe_close(exe_name_fd);
58 return log_error_errno(errno, "Failed to create FILE object: %m");
59 }
60
61 /* Find the last line */
62 for (;;) {
63 _cleanup_free_ char *t = NULL;
64
65 r = read_line(file, LONG_LINE_MAX, &t);
66 if (r < 0)
67 return log_error_errno(r, "Failed to read from socket: %m");
68 if (r == 0)
69 break;
70
71 free_and_replace(line, t);
72 }
73
74 /* We only treat "less" specially.
75 * Return true whenever option K is *not* set. */
76 r = streq_ptr(line, "less") && !strchr(less_opts, 'K');
77
78 log_debug("Pager executable is \"%s\", options \"%s\", quit_on_interrupt: %s",
79 strnull(line), less_opts, yes_no(!r));
80 return r;
81 }
82
83 int pager_open(PagerFlags flags) {
84 _cleanup_close_pair_ int fd[2] = { -1, -1 }, exe_name_pipe[2] = { -1, -1 };
85 _cleanup_strv_free_ char **pager_args = NULL;
86 const char *pager, *less_opts;
87 int r;
88
89 if (flags & PAGER_DISABLE)
90 return 0;
91
92 if (pager_pid > 0)
93 return 1;
94
95 if (terminal_is_dumb())
96 return 0;
97
98 if (!is_main_thread())
99 return log_error_errno(SYNTHETIC_ERRNO(EPERM), "Pager invoked from wrong thread.");
100
101 pager = getenv("SYSTEMD_PAGER");
102 if (!pager)
103 pager = getenv("PAGER");
104
105 if (pager) {
106 pager_args = strv_split(pager, WHITESPACE);
107 if (!pager_args)
108 return log_oom();
109
110 /* If the pager is explicitly turned off, honour it */
111 if (strv_isempty(pager_args) || strv_equal(pager_args, STRV_MAKE("cat")))
112 return 0;
113 }
114
115 /* Determine and cache number of columns/lines before we spawn the pager so that we get the value from the
116 * actual tty */
117 (void) columns();
118 (void) lines();
119
120 if (pipe2(fd, O_CLOEXEC) < 0)
121 return log_error_errno(errno, "Failed to create pager pipe: %m");
122
123 /* This is a pipe to feed the name of the executed pager binary into the parent */
124 if (pipe2(exe_name_pipe, O_CLOEXEC) < 0)
125 return log_error_errno(errno, "Failed to create exe_name pipe: %m");
126
127 /* Initialize a good set of less options */
128 less_opts = getenv("SYSTEMD_LESS");
129 if (!less_opts)
130 less_opts = "FRSXMK";
131 if (flags & PAGER_JUMP_TO_END)
132 less_opts = strjoina(less_opts, " +G");
133
134 /* We set SIGINT as PR_DEATHSIG signal here, to match the "K" parameter we set in $LESS, which enables SIGINT behaviour. */
135 r = safe_fork("(pager)", FORK_RESET_SIGNALS|FORK_DEATHSIG_SIGINT|FORK_RLIMIT_NOFILE_SAFE|FORK_LOG, &pager_pid);
136 if (r < 0)
137 return r;
138 if (r == 0) {
139 const char *less_charset, *exe;
140
141 /* In the child start the pager */
142
143 if (dup2(fd[0], STDIN_FILENO) < 0) {
144 log_error_errno(errno, "Failed to duplicate file descriptor to STDIN: %m");
145 _exit(EXIT_FAILURE);
146 }
147
148 safe_close_pair(fd);
149
150 if (setenv("LESS", less_opts, 1) < 0) {
151 log_error_errno(errno, "Failed to set environment variable LESS: %m");
152 _exit(EXIT_FAILURE);
153 }
154
155 /* Initialize a good charset for less. This is
156 * particularly important if we output UTF-8
157 * characters. */
158 less_charset = getenv("SYSTEMD_LESSCHARSET");
159 if (!less_charset && is_locale_utf8())
160 less_charset = "utf-8";
161 if (less_charset &&
162 setenv("LESSCHARSET", less_charset, 1) < 0) {
163 log_error_errno(errno, "Failed to set environment variable LESSCHARSET: %m");
164 _exit(EXIT_FAILURE);
165 }
166
167 if (pager_args) {
168 r = loop_write(exe_name_pipe[1], pager_args[0], strlen(pager_args[0]) + 1, false);
169 if (r < 0) {
170 log_error_errno(r, "Failed to write pager name to socket: %m");
171 _exit(EXIT_FAILURE);
172 }
173
174 execvp(pager_args[0], pager_args);
175 log_full_errno(errno == ENOENT ? LOG_DEBUG : LOG_WARNING, errno,
176 "Failed to execute '%s', using fallback pagers: %m", pager_args[0]);
177 }
178
179 /* Debian's alternatives command for pagers is
180 * called 'pager'. Note that we do not call
181 * sensible-pagers here, since that is just a
182 * shell script that implements a logic that
183 * is similar to this one anyway, but is
184 * Debian-specific. */
185 FOREACH_STRING(exe, "pager", "less", "more") {
186 r = loop_write(exe_name_pipe[1], exe, strlen(exe) + 1, false);
187 if (r < 0) {
188 log_error_errno(r, "Failed to write pager name to socket: %m");
189 _exit(EXIT_FAILURE);
190 }
191 execlp(exe, exe, NULL);
192 log_full_errno(errno == ENOENT ? LOG_DEBUG : LOG_WARNING, errno,
193 "Failed to execute '%s', using next fallback pager: %m", exe);
194 }
195
196 r = loop_write(exe_name_pipe[1], "(built-in)", strlen("(built-in)") + 1, false);
197 if (r < 0) {
198 log_error_errno(r, "Failed to write pager name to socket: %m");
199 _exit(EXIT_FAILURE);
200 }
201 /* Close pipe to signal the parent to start sending data */
202 safe_close_pair(exe_name_pipe);
203 pager_fallback();
204 /* not reached */
205 }
206
207 /* Return in the parent */
208 stored_stdout = fcntl(STDOUT_FILENO, F_DUPFD_CLOEXEC, 3);
209 if (dup2(fd[1], STDOUT_FILENO) < 0) {
210 stored_stdout = safe_close(stored_stdout);
211 return log_error_errno(errno, "Failed to duplicate pager pipe: %m");
212 }
213 stdout_redirected = true;
214
215 stored_stderr = fcntl(STDERR_FILENO, F_DUPFD_CLOEXEC, 3);
216 if (dup2(fd[1], STDERR_FILENO) < 0) {
217 stored_stderr = safe_close(stored_stderr);
218 return log_error_errno(errno, "Failed to duplicate pager pipe: %m");
219 }
220 stderr_redirected = true;
221
222 exe_name_pipe[1] = safe_close(exe_name_pipe[1]);
223
224 r = no_quit_on_interrupt(TAKE_FD(exe_name_pipe[0]), less_opts);
225 if (r < 0)
226 return r;
227 if (r > 0)
228 (void) ignore_signals(SIGINT, -1);
229
230 return 1;
231 }
232
233 void pager_close(void) {
234
235 if (pager_pid <= 0)
236 return;
237
238 /* Inform pager that we are done */
239 (void) fflush(stdout);
240 if (stdout_redirected)
241 if (stored_stdout < 0 || dup2(stored_stdout, STDOUT_FILENO) < 0)
242 (void) close(STDOUT_FILENO);
243 stored_stdout = safe_close(stored_stdout);
244 (void) fflush(stderr);
245 if (stderr_redirected)
246 if (stored_stderr < 0 || dup2(stored_stderr, STDERR_FILENO) < 0)
247 (void) close(STDERR_FILENO);
248 stored_stderr = safe_close(stored_stderr);
249 stdout_redirected = stderr_redirected = false;
250
251 (void) kill(pager_pid, SIGCONT);
252 (void) wait_for_terminate(pager_pid, NULL);
253 pager_pid = 0;
254 }
255
256 bool pager_have(void) {
257 return pager_pid > 0;
258 }
259
260 int show_man_page(const char *desc, bool null_stdio) {
261 const char *args[4] = { "man", NULL, NULL, NULL };
262 char *e = NULL;
263 pid_t pid;
264 size_t k;
265 int r;
266
267 k = strlen(desc);
268
269 if (desc[k-1] == ')')
270 e = strrchr(desc, '(');
271
272 if (e) {
273 char *page = NULL, *section = NULL;
274
275 page = strndupa(desc, e - desc);
276 section = strndupa(e + 1, desc + k - e - 2);
277
278 args[1] = section;
279 args[2] = page;
280 } else
281 args[1] = desc;
282
283 r = safe_fork("(man)", FORK_RESET_SIGNALS|FORK_DEATHSIG|(null_stdio ? FORK_NULL_STDIO : 0)|FORK_RLIMIT_NOFILE_SAFE|FORK_LOG, &pid);
284 if (r < 0)
285 return r;
286 if (r == 0) {
287 /* Child */
288 execvp(args[0], (char**) args);
289 log_error_errno(errno, "Failed to execute man: %m");
290 _exit(EXIT_FAILURE);
291 }
292
293 return wait_for_terminate_and_check(NULL, pid, 0);
294 }