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