]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/basic/pager.c
basic/pager: ignore ^C when piping to less and K is not set
[thirdparty/systemd.git] / src / basic / 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"
ce30c8dc 22#include "signal-util.h"
07630cea 23#include "string-util.h"
57c9e047 24#include "strv.h"
07630cea 25#include "terminal-util.h"
1968a360
LP
26
27static pid_t pager_pid = 0;
28
3f603952
LP
29static int stored_stdout = -1;
30static int stored_stderr = -1;
31static bool stdout_redirected = false;
32static bool stderr_redirected = false;
33
848e863a 34_noreturn_ static void pager_fallback(void) {
6a7c676c 35 int r;
46e65dcc 36
1c876927 37 r = copy_bytes(STDIN_FILENO, STDOUT_FILENO, (uint64_t) -1, 0);
6a7c676c
LP
38 if (r < 0) {
39 log_error_errno(r, "Internal pager failed: %m");
4a8e40eb
MS
40 _exit(EXIT_FAILURE);
41 }
46e65dcc 42
4a8e40eb
MS
43 _exit(EXIT_SUCCESS);
44}
45
6432da6a
ZJS
46static 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_debug_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 r;
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
0221d68a 83int pager_open(PagerFlags flags) {
6432da6a 84 _cleanup_close_pair_ int fd[2] = { -1, -1 }, exe_name_pipe[2] = { -1, -1 };
43942e80 85 _cleanup_strv_free_ char **pager_args = NULL;
6432da6a 86 const char *pager, *less_opts;
4c253ed1 87 int r;
1968a360 88
0221d68a 89 if (flags & PAGER_DISABLE)
ea4b98e6
AK
90 return 0;
91
1968a360 92 if (pager_pid > 0)
f89a3b6f 93 return 1;
1968a360 94
ac96418b 95 if (terminal_is_dumb())
f89a3b6f 96 return 0;
729e3769 97
85afeae8
LP
98 if (!is_main_thread())
99 return -EPERM;
100
bcbd61db
LP
101 pager = getenv("SYSTEMD_PAGER");
102 if (!pager)
103 pager = getenv("PAGER");
104
43942e80
YW
105 if (pager) {
106 pager_args = strv_split(pager, WHITESPACE);
107 if (!pager_args)
108 return -ENOMEM;
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 }
bcbd61db 114
d13b5227
LP
115 /* Determine and cache number of columns/lines before we spawn the pager so that we get the value from the
116 * actual tty */
bcbd61db 117 (void) columns();
d13b5227 118 (void) lines();
1968a360 119
d262e99e 120 if (pipe2(fd, O_CLOEXEC) < 0)
4a62c710 121 return log_error_errno(errno, "Failed to create pager pipe: %m");
1968a360 122
6432da6a
ZJS
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
b6e1fff1 134 r = safe_fork("(pager)", FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_LOG, &pager_pid);
4c253ed1 135 if (r < 0)
b6e1fff1 136 return r;
4c253ed1 137 if (r == 0) {
6432da6a 138 const char *less_charset, *exe;
1968a360 139
4c253ed1 140 /* In the child start the pager */
ce30c8dc 141
bcbd61db 142 (void) dup2(fd[0], STDIN_FILENO);
3d94f76c 143 safe_close_pair(fd);
1968a360 144
0357fa0d
ZJS
145 if (setenv("LESS", less_opts, 1) < 0)
146 _exit(EXIT_FAILURE);
1968a360 147
a1b4e6e9
LP
148 /* Initialize a good charset for less. This is
149 * particularly important if we output UTF-8
150 * characters. */
151 less_charset = getenv("SYSTEMD_LESSCHARSET");
152 if (!less_charset && is_locale_utf8())
153 less_charset = "utf-8";
0357fa0d
ZJS
154 if (less_charset &&
155 setenv("LESSCHARSET", less_charset, 1) < 0)
156 _exit(EXIT_FAILURE);
a1b4e6e9 157
6432da6a
ZJS
158 if (pager_args) {
159 if (loop_write(exe_name_pipe[1], pager_args[0], strlen(pager_args[0]) + 1, false) < 0)
160 _exit(EXIT_FAILURE);
161
43942e80 162 execvp(pager_args[0], pager_args);
6432da6a 163 }
1968a360
LP
164
165 /* Debian's alternatives command for pagers is
166 * called 'pager'. Note that we do not call
167 * sensible-pagers here, since that is just a
168 * shell script that implements a logic that
169 * is similar to this one anyway, but is
170 * Debian-specific. */
6432da6a
ZJS
171 FOREACH_STRING(exe, "pager", "less", "more") {
172 if (loop_write(exe_name_pipe[1], exe, strlen(exe) + 1, false) < 0)
173 _exit(EXIT_FAILURE);
174 execlp(exe, exe, NULL);
175 }
1968a360 176
6432da6a
ZJS
177 if (loop_write(exe_name_pipe[1], "(built-in)", strlen("(built-in") + 1, false) < 0)
178 _exit(EXIT_FAILURE);
4a8e40eb
MS
179 pager_fallback();
180 /* not reached */
1968a360
LP
181 }
182
183 /* Return in the parent */
a45e7bb4
MS
184 stored_stdout = fcntl(STDOUT_FILENO, F_DUPFD_CLOEXEC, 3);
185 if (dup2(fd[1], STDOUT_FILENO) < 0) {
186 stored_stdout = safe_close(stored_stdout);
4a62c710 187 return log_error_errno(errno, "Failed to duplicate pager pipe: %m");
a45e7bb4
MS
188 }
189 stdout_redirected = true;
190
191 stored_stderr = fcntl(STDERR_FILENO, F_DUPFD_CLOEXEC, 3);
192 if (dup2(fd[1], STDERR_FILENO) < 0) {
193 stored_stderr = safe_close(stored_stderr);
8b5264aa 194 return log_error_errno(errno, "Failed to duplicate pager pipe: %m");
a45e7bb4
MS
195 }
196 stderr_redirected = true;
1968a360 197
6432da6a
ZJS
198 exe_name_pipe[1] = safe_close(exe_name_pipe[1]);
199
200 r = no_quit_on_interrupt(TAKE_FD(exe_name_pipe[0]), less_opts);
201 if (r < 0)
202 return r;
203 if (r > 0)
204 (void) ignore_signals(SIGINT, -1);
205
f89a3b6f 206 return 1;
1968a360
LP
207}
208
209void pager_close(void) {
210
211 if (pager_pid <= 0)
212 return;
213
214 /* Inform pager that we are done */
a45e7bb4 215 (void) fflush(stdout);
77018a8c
MS
216 if (stdout_redirected)
217 if (stored_stdout < 0 || dup2(stored_stdout, STDOUT_FILENO) < 0)
218 (void) close(STDOUT_FILENO);
a45e7bb4
MS
219 stored_stdout = safe_close(stored_stdout);
220 (void) fflush(stderr);
77018a8c
MS
221 if (stderr_redirected)
222 if (stored_stderr < 0 || dup2(stored_stderr, STDERR_FILENO) < 0)
223 (void) close(STDERR_FILENO);
a45e7bb4
MS
224 stored_stderr = safe_close(stored_stderr);
225 stdout_redirected = stderr_redirected = false;
8b5264aa 226
74ca738f 227 (void) kill(pager_pid, SIGCONT);
c73d180d 228 (void) wait_for_terminate(pager_pid, NULL);
1968a360
LP
229 pager_pid = 0;
230}
f89a3b6f
LP
231
232bool pager_have(void) {
233 return pager_pid > 0;
234}
78002a67
ZJS
235
236int show_man_page(const char *desc, bool null_stdio) {
237 const char *args[4] = { "man", NULL, NULL, NULL };
238 char *e = NULL;
239 pid_t pid;
240 size_t k;
241 int r;
78002a67
ZJS
242
243 k = strlen(desc);
244
245 if (desc[k-1] == ')')
246 e = strrchr(desc, '(');
247
248 if (e) {
249 char *page = NULL, *section = NULL;
250
251 page = strndupa(desc, e - desc);
252 section = strndupa(e + 1, desc + k - e - 2);
253
254 args[1] = section;
255 args[2] = page;
256 } else
257 args[1] = desc;
258
b6e1fff1 259 r = safe_fork("(man)", FORK_RESET_SIGNALS|FORK_DEATHSIG|(null_stdio ? FORK_NULL_STDIO : 0)|FORK_LOG, &pid);
4c253ed1 260 if (r < 0)
b6e1fff1 261 return r;
4c253ed1 262 if (r == 0) {
78002a67 263 /* Child */
78002a67 264 execvp(args[0], (char**) args);
56f64d95 265 log_error_errno(errno, "Failed to execute man: %m");
78002a67
ZJS
266 _exit(EXIT_FAILURE);
267 }
268
2e87a1fd 269 return wait_for_terminate_and_check(NULL, pid, 0);
78002a67 270}