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