]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/basic/pager.c
tree-wide: remove Lennart's copyright lines
[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"
8752c575 15#include "locale-util.h"
a8fbdf54 16#include "log.h"
1968a360 17#include "macro.h"
3ffd4af2 18#include "pager.h"
07630cea 19#include "process-util.h"
ce30c8dc 20#include "signal-util.h"
07630cea 21#include "string-util.h"
57c9e047 22#include "strv.h"
07630cea 23#include "terminal-util.h"
1968a360
LP
24
25static pid_t pager_pid = 0;
26
3f603952
LP
27static int stored_stdout = -1;
28static int stored_stderr = -1;
29static bool stdout_redirected = false;
30static bool stderr_redirected = false;
31
848e863a 32_noreturn_ static void pager_fallback(void) {
6a7c676c 33 int r;
46e65dcc 34
1c876927 35 r = copy_bytes(STDIN_FILENO, STDOUT_FILENO, (uint64_t) -1, 0);
6a7c676c
LP
36 if (r < 0) {
37 log_error_errno(r, "Internal pager failed: %m");
4a8e40eb
MS
38 _exit(EXIT_FAILURE);
39 }
46e65dcc 40
4a8e40eb
MS
41 _exit(EXIT_SUCCESS);
42}
43
ea4b98e6 44int pager_open(bool no_pager, bool jump_to_end) {
bcbd61db 45 _cleanup_close_pair_ int fd[2] = { -1, -1 };
1968a360 46 const char *pager;
4c253ed1 47 int r;
1968a360 48
ea4b98e6
AK
49 if (no_pager)
50 return 0;
51
1968a360 52 if (pager_pid > 0)
f89a3b6f 53 return 1;
1968a360 54
ac96418b 55 if (terminal_is_dumb())
f89a3b6f 56 return 0;
729e3769 57
85afeae8
LP
58 if (!is_main_thread())
59 return -EPERM;
60
bcbd61db
LP
61 pager = getenv("SYSTEMD_PAGER");
62 if (!pager)
63 pager = getenv("PAGER");
64
65 /* If the pager is explicitly turned off, honour it */
57c9e047 66 if (pager && STR_IN_SET(pager, "", "cat"))
bcbd61db
LP
67 return 0;
68
d13b5227
LP
69 /* Determine and cache number of columns/lines before we spawn the pager so that we get the value from the
70 * actual tty */
bcbd61db 71 (void) columns();
d13b5227 72 (void) lines();
1968a360 73
d262e99e 74 if (pipe2(fd, O_CLOEXEC) < 0)
4a62c710 75 return log_error_errno(errno, "Failed to create pager pipe: %m");
1968a360 76
b6e1fff1 77 r = safe_fork("(pager)", FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_LOG, &pager_pid);
4c253ed1 78 if (r < 0)
b6e1fff1 79 return r;
4c253ed1 80 if (r == 0) {
a1b4e6e9 81 const char* less_opts, *less_charset;
1968a360 82
4c253ed1 83 /* In the child start the pager */
ce30c8dc 84
bcbd61db 85 (void) dup2(fd[0], STDIN_FILENO);
3d94f76c 86 safe_close_pair(fd);
1968a360 87
a1b4e6e9 88 /* Initialize a good set of less options */
f366d58d
JD
89 less_opts = getenv("SYSTEMD_LESS");
90 if (!less_opts)
91 less_opts = "FRSXMK";
1b12a7b5 92 if (jump_to_end)
63c372cb 93 less_opts = strjoina(less_opts, " +G");
0357fa0d
ZJS
94 if (setenv("LESS", less_opts, 1) < 0)
95 _exit(EXIT_FAILURE);
1968a360 96
a1b4e6e9
LP
97 /* Initialize a good charset for less. This is
98 * particularly important if we output UTF-8
99 * characters. */
100 less_charset = getenv("SYSTEMD_LESSCHARSET");
101 if (!less_charset && is_locale_utf8())
102 less_charset = "utf-8";
0357fa0d
ZJS
103 if (less_charset &&
104 setenv("LESSCHARSET", less_charset, 1) < 0)
105 _exit(EXIT_FAILURE);
a1b4e6e9 106
1968a360
LP
107 if (pager) {
108 execlp(pager, pager, NULL);
109 execl("/bin/sh", "sh", "-c", pager, NULL);
110 }
111
112 /* Debian's alternatives command for pagers is
113 * called 'pager'. Note that we do not call
114 * sensible-pagers here, since that is just a
115 * shell script that implements a logic that
116 * is similar to this one anyway, but is
117 * Debian-specific. */
118 execlp("pager", "pager", NULL);
119
120 execlp("less", "less", NULL);
121 execlp("more", "more", NULL);
1968a360 122
4a8e40eb
MS
123 pager_fallback();
124 /* not reached */
1968a360
LP
125 }
126
127 /* Return in the parent */
a45e7bb4
MS
128 stored_stdout = fcntl(STDOUT_FILENO, F_DUPFD_CLOEXEC, 3);
129 if (dup2(fd[1], STDOUT_FILENO) < 0) {
130 stored_stdout = safe_close(stored_stdout);
4a62c710 131 return log_error_errno(errno, "Failed to duplicate pager pipe: %m");
a45e7bb4
MS
132 }
133 stdout_redirected = true;
134
135 stored_stderr = fcntl(STDERR_FILENO, F_DUPFD_CLOEXEC, 3);
136 if (dup2(fd[1], STDERR_FILENO) < 0) {
137 stored_stderr = safe_close(stored_stderr);
8b5264aa 138 return log_error_errno(errno, "Failed to duplicate pager pipe: %m");
a45e7bb4
MS
139 }
140 stderr_redirected = true;
1968a360 141
f89a3b6f 142 return 1;
1968a360
LP
143}
144
145void pager_close(void) {
146
147 if (pager_pid <= 0)
148 return;
149
150 /* Inform pager that we are done */
a45e7bb4 151 (void) fflush(stdout);
77018a8c
MS
152 if (stdout_redirected)
153 if (stored_stdout < 0 || dup2(stored_stdout, STDOUT_FILENO) < 0)
154 (void) close(STDOUT_FILENO);
a45e7bb4
MS
155 stored_stdout = safe_close(stored_stdout);
156 (void) fflush(stderr);
77018a8c
MS
157 if (stderr_redirected)
158 if (stored_stderr < 0 || dup2(stored_stderr, STDERR_FILENO) < 0)
159 (void) close(STDERR_FILENO);
a45e7bb4
MS
160 stored_stderr = safe_close(stored_stderr);
161 stdout_redirected = stderr_redirected = false;
8b5264aa 162
74ca738f 163 (void) kill(pager_pid, SIGCONT);
c73d180d 164 (void) wait_for_terminate(pager_pid, NULL);
1968a360
LP
165 pager_pid = 0;
166}
f89a3b6f
LP
167
168bool pager_have(void) {
169 return pager_pid > 0;
170}
78002a67
ZJS
171
172int show_man_page(const char *desc, bool null_stdio) {
173 const char *args[4] = { "man", NULL, NULL, NULL };
174 char *e = NULL;
175 pid_t pid;
176 size_t k;
177 int r;
78002a67
ZJS
178
179 k = strlen(desc);
180
181 if (desc[k-1] == ')')
182 e = strrchr(desc, '(');
183
184 if (e) {
185 char *page = NULL, *section = NULL;
186
187 page = strndupa(desc, e - desc);
188 section = strndupa(e + 1, desc + k - e - 2);
189
190 args[1] = section;
191 args[2] = page;
192 } else
193 args[1] = desc;
194
b6e1fff1 195 r = safe_fork("(man)", FORK_RESET_SIGNALS|FORK_DEATHSIG|(null_stdio ? FORK_NULL_STDIO : 0)|FORK_LOG, &pid);
4c253ed1 196 if (r < 0)
b6e1fff1 197 return r;
4c253ed1 198 if (r == 0) {
78002a67 199 /* Child */
78002a67 200 execvp(args[0], (char**) args);
56f64d95 201 log_error_errno(errno, "Failed to execute man: %m");
78002a67
ZJS
202 _exit(EXIT_FAILURE);
203 }
204
2e87a1fd 205 return wait_for_terminate_and_check(NULL, pid, 0);
78002a67 206}