]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/shared/pager.c
network: warn if Address= is specified without prefixlen
[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"
1968a360
LP
27
28static pid_t pager_pid = 0;
29
3f603952
LP
30static int stored_stdout = -1;
31static int stored_stderr = -1;
32static bool stdout_redirected = false;
33static bool stderr_redirected = false;
34
848e863a 35_noreturn_ static void pager_fallback(void) {
6a7c676c 36 int r;
46e65dcc 37
1c876927 38 r = copy_bytes(STDIN_FILENO, STDOUT_FILENO, (uint64_t) -1, 0);
6a7c676c
LP
39 if (r < 0) {
40 log_error_errno(r, "Internal pager failed: %m");
4a8e40eb
MS
41 _exit(EXIT_FAILURE);
42 }
46e65dcc 43
4a8e40eb
MS
44 _exit(EXIT_SUCCESS);
45}
46
6432da6a
ZJS
47static 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);
1d788908 59 return log_error_errno(errno, "Failed to create FILE object: %m");
6432da6a
ZJS
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)
1d788908 68 return log_error_errno(r, "Failed to read from socket: %m");
6432da6a
ZJS
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
0221d68a 84int pager_open(PagerFlags flags) {
6432da6a 85 _cleanup_close_pair_ int fd[2] = { -1, -1 }, exe_name_pipe[2] = { -1, -1 };
43942e80 86 _cleanup_strv_free_ char **pager_args = NULL;
6432da6a 87 const char *pager, *less_opts;
4c253ed1 88 int r;
1968a360 89
0221d68a 90 if (flags & PAGER_DISABLE)
ea4b98e6
AK
91 return 0;
92
1968a360 93 if (pager_pid > 0)
f89a3b6f 94 return 1;
1968a360 95
ac96418b 96 if (terminal_is_dumb())
f89a3b6f 97 return 0;
729e3769 98
85afeae8 99 if (!is_main_thread())
1d788908 100 return log_error_errno(SYNTHETIC_ERRNO(EPERM), "Pager invoked from wrong thread.");
85afeae8 101
bcbd61db
LP
102 pager = getenv("SYSTEMD_PAGER");
103 if (!pager)
104 pager = getenv("PAGER");
105
43942e80
YW
106 if (pager) {
107 pager_args = strv_split(pager, WHITESPACE);
108 if (!pager_args)
1d788908 109 return log_oom();
43942e80
YW
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 }
bcbd61db 115
d13b5227
LP
116 /* Determine and cache number of columns/lines before we spawn the pager so that we get the value from the
117 * actual tty */
bcbd61db 118 (void) columns();
d13b5227 119 (void) lines();
1968a360 120
d262e99e 121 if (pipe2(fd, O_CLOEXEC) < 0)
4a62c710 122 return log_error_errno(errno, "Failed to create pager pipe: %m");
1968a360 123
6432da6a
ZJS
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
a81921e4 135 r = safe_fork("(pager)", FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_RLIMIT_NOFILE_SAFE|FORK_LOG, &pager_pid);
4c253ed1 136 if (r < 0)
b6e1fff1 137 return r;
4c253ed1 138 if (r == 0) {
6432da6a 139 const char *less_charset, *exe;
1968a360 140
4c253ed1 141 /* In the child start the pager */
ce30c8dc 142
1d788908
LP
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
3d94f76c 148 safe_close_pair(fd);
1968a360 149
1d788908
LP
150 if (setenv("LESS", less_opts, 1) < 0) {
151 log_error_errno(errno, "Failed to set environment variable LESS: %m");
0357fa0d 152 _exit(EXIT_FAILURE);
1d788908 153 }
1968a360 154
a1b4e6e9
LP
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";
0357fa0d 161 if (less_charset &&
1d788908
LP
162 setenv("LESSCHARSET", less_charset, 1) < 0) {
163 log_error_errno(errno, "Failed to set environment variable LESSCHARSET: %m");
0357fa0d 164 _exit(EXIT_FAILURE);
1d788908 165 }
a1b4e6e9 166
6432da6a 167 if (pager_args) {
1d788908
LP
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");
6432da6a 171 _exit(EXIT_FAILURE);
1d788908 172 }
6432da6a 173
43942e80 174 execvp(pager_args[0], pager_args);
1d788908
LP
175 log_full_errno(errno == ENOENT ? LOG_DEBUG : LOG_WARNING, errno,
176 "Failed execute %s, using fallback pagers: %m", pager_args[0]);
6432da6a 177 }
1968a360
LP
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. */
6432da6a 185 FOREACH_STRING(exe, "pager", "less", "more") {
1d788908
LP
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");
6432da6a 189 _exit(EXIT_FAILURE);
1d788908 190 }
6432da6a 191 execlp(exe, exe, NULL);
1d788908
LP
192 log_full_errno(errno == ENOENT ? LOG_DEBUG : LOG_WARNING, errno,
193 "Failed execute %s, using next fallback pager: %m", exe);
6432da6a 194 }
1968a360 195
1d788908
LP
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");
6432da6a 199 _exit(EXIT_FAILURE);
1d788908 200 }
4a8e40eb
MS
201 pager_fallback();
202 /* not reached */
1968a360
LP
203 }
204
205 /* Return in the parent */
a45e7bb4
MS
206 stored_stdout = fcntl(STDOUT_FILENO, F_DUPFD_CLOEXEC, 3);
207 if (dup2(fd[1], STDOUT_FILENO) < 0) {
208 stored_stdout = safe_close(stored_stdout);
4a62c710 209 return log_error_errno(errno, "Failed to duplicate pager pipe: %m");
a45e7bb4
MS
210 }
211 stdout_redirected = true;
212
213 stored_stderr = fcntl(STDERR_FILENO, F_DUPFD_CLOEXEC, 3);
214 if (dup2(fd[1], STDERR_FILENO) < 0) {
215 stored_stderr = safe_close(stored_stderr);
8b5264aa 216 return log_error_errno(errno, "Failed to duplicate pager pipe: %m");
a45e7bb4
MS
217 }
218 stderr_redirected = true;
1968a360 219
6432da6a
ZJS
220 exe_name_pipe[1] = safe_close(exe_name_pipe[1]);
221
222 r = no_quit_on_interrupt(TAKE_FD(exe_name_pipe[0]), less_opts);
223 if (r < 0)
224 return r;
225 if (r > 0)
226 (void) ignore_signals(SIGINT, -1);
227
f89a3b6f 228 return 1;
1968a360
LP
229}
230
231void pager_close(void) {
232
233 if (pager_pid <= 0)
234 return;
235
236 /* Inform pager that we are done */
a45e7bb4 237 (void) fflush(stdout);
77018a8c
MS
238 if (stdout_redirected)
239 if (stored_stdout < 0 || dup2(stored_stdout, STDOUT_FILENO) < 0)
240 (void) close(STDOUT_FILENO);
a45e7bb4
MS
241 stored_stdout = safe_close(stored_stdout);
242 (void) fflush(stderr);
77018a8c
MS
243 if (stderr_redirected)
244 if (stored_stderr < 0 || dup2(stored_stderr, STDERR_FILENO) < 0)
245 (void) close(STDERR_FILENO);
a45e7bb4
MS
246 stored_stderr = safe_close(stored_stderr);
247 stdout_redirected = stderr_redirected = false;
8b5264aa 248
74ca738f 249 (void) kill(pager_pid, SIGCONT);
c73d180d 250 (void) wait_for_terminate(pager_pid, NULL);
1968a360
LP
251 pager_pid = 0;
252}
f89a3b6f
LP
253
254bool pager_have(void) {
255 return pager_pid > 0;
256}
78002a67
ZJS
257
258int show_man_page(const char *desc, bool null_stdio) {
259 const char *args[4] = { "man", NULL, NULL, NULL };
260 char *e = NULL;
261 pid_t pid;
262 size_t k;
263 int r;
78002a67
ZJS
264
265 k = strlen(desc);
266
267 if (desc[k-1] == ')')
268 e = strrchr(desc, '(');
269
270 if (e) {
271 char *page = NULL, *section = NULL;
272
273 page = strndupa(desc, e - desc);
274 section = strndupa(e + 1, desc + k - e - 2);
275
276 args[1] = section;
277 args[2] = page;
278 } else
279 args[1] = desc;
280
0672e2c6 281 r = safe_fork("(man)", FORK_RESET_SIGNALS|FORK_DEATHSIG|(null_stdio ? FORK_NULL_STDIO : 0)|FORK_RLIMIT_NOFILE_SAFE|FORK_LOG, &pid);
4c253ed1 282 if (r < 0)
b6e1fff1 283 return r;
4c253ed1 284 if (r == 0) {
78002a67 285 /* Child */
78002a67 286 execvp(args[0], (char**) args);
56f64d95 287 log_error_errno(errno, "Failed to execute man: %m");
78002a67
ZJS
288 _exit(EXIT_FAILURE);
289 }
290
2e87a1fd 291 return wait_for_terminate_and_check(NULL, pid, 0);
78002a67 292}