]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/pager.c
Add SPDX license identifiers to source files under the LGPL
[thirdparty/systemd.git] / src / shared / pager.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 This file is part of systemd.
4
5 Copyright 2010 Lennart Poettering
6
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
11
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
19 ***/
20
21 #include <errno.h>
22 #include <signal.h>
23 #include <stddef.h>
24 #include <stdint.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <sys/prctl.h>
29 #include <unistd.h>
30
31 #include "copy.h"
32 #include "fd-util.h"
33 #include "locale-util.h"
34 #include "log.h"
35 #include "macro.h"
36 #include "pager.h"
37 #include "process-util.h"
38 #include "signal-util.h"
39 #include "string-util.h"
40 #include "strv.h"
41 #include "terminal-util.h"
42
43 static pid_t pager_pid = 0;
44
45 noreturn static void pager_fallback(void) {
46 int r;
47
48 r = copy_bytes(STDIN_FILENO, STDOUT_FILENO, (uint64_t) -1, 0);
49 if (r < 0) {
50 log_error_errno(r, "Internal pager failed: %m");
51 _exit(EXIT_FAILURE);
52 }
53
54 _exit(EXIT_SUCCESS);
55 }
56
57 static int stored_stdout = -1;
58 static int stored_stderr = -1;
59 static bool stdout_redirected = false;
60 static bool stderr_redirected = false;
61
62 int pager_open(bool no_pager, bool jump_to_end) {
63 _cleanup_close_pair_ int fd[2] = { -1, -1 };
64 const char *pager;
65 pid_t parent_pid;
66
67 if (no_pager)
68 return 0;
69
70 if (pager_pid > 0)
71 return 1;
72
73 if (terminal_is_dumb())
74 return 0;
75
76 pager = getenv("SYSTEMD_PAGER");
77 if (!pager)
78 pager = getenv("PAGER");
79
80 /* If the pager is explicitly turned off, honour it */
81 if (pager && STR_IN_SET(pager, "", "cat"))
82 return 0;
83
84 /* Determine and cache number of columns/lines before we spawn the pager so that we get the value from the
85 * actual tty */
86 (void) columns();
87 (void) lines();
88
89 if (pipe2(fd, O_CLOEXEC) < 0)
90 return log_error_errno(errno, "Failed to create pager pipe: %m");
91
92 parent_pid = getpid_cached();
93
94 pager_pid = fork();
95 if (pager_pid < 0)
96 return log_error_errno(errno, "Failed to fork pager: %m");
97
98 /* In the child start the pager */
99 if (pager_pid == 0) {
100 const char* less_opts, *less_charset;
101
102 (void) reset_all_signal_handlers();
103 (void) reset_signal_mask();
104
105 (void) dup2(fd[0], STDIN_FILENO);
106 safe_close_pair(fd);
107
108 /* Initialize a good set of less options */
109 less_opts = getenv("SYSTEMD_LESS");
110 if (!less_opts)
111 less_opts = "FRSXMK";
112 if (jump_to_end)
113 less_opts = strjoina(less_opts, " +G");
114 if (setenv("LESS", less_opts, 1) < 0)
115 _exit(EXIT_FAILURE);
116
117 /* Initialize a good charset for less. This is
118 * particularly important if we output UTF-8
119 * characters. */
120 less_charset = getenv("SYSTEMD_LESSCHARSET");
121 if (!less_charset && is_locale_utf8())
122 less_charset = "utf-8";
123 if (less_charset &&
124 setenv("LESSCHARSET", less_charset, 1) < 0)
125 _exit(EXIT_FAILURE);
126
127 /* Make sure the pager goes away when the parent dies */
128 if (prctl(PR_SET_PDEATHSIG, SIGTERM) < 0)
129 _exit(EXIT_FAILURE);
130
131 /* Check whether our parent died before we were able
132 * to set the death signal */
133 if (getppid() != parent_pid)
134 _exit(EXIT_SUCCESS);
135
136 if (pager) {
137 execlp(pager, pager, NULL);
138 execl("/bin/sh", "sh", "-c", pager, NULL);
139 }
140
141 /* Debian's alternatives command for pagers is
142 * called 'pager'. Note that we do not call
143 * sensible-pagers here, since that is just a
144 * shell script that implements a logic that
145 * is similar to this one anyway, but is
146 * Debian-specific. */
147 execlp("pager", "pager", NULL);
148
149 execlp("less", "less", NULL);
150 execlp("more", "more", NULL);
151
152 pager_fallback();
153 /* not reached */
154 }
155
156 /* Return in the parent */
157 stored_stdout = fcntl(STDOUT_FILENO, F_DUPFD_CLOEXEC, 3);
158 if (dup2(fd[1], STDOUT_FILENO) < 0) {
159 stored_stdout = safe_close(stored_stdout);
160 return log_error_errno(errno, "Failed to duplicate pager pipe: %m");
161 }
162 stdout_redirected = true;
163
164 stored_stderr = fcntl(STDERR_FILENO, F_DUPFD_CLOEXEC, 3);
165 if (dup2(fd[1], STDERR_FILENO) < 0) {
166 stored_stderr = safe_close(stored_stderr);
167 return log_error_errno(errno, "Failed to duplicate pager pipe: %m");
168 }
169 stderr_redirected = true;
170
171 return 1;
172 }
173
174 void pager_close(void) {
175
176 if (pager_pid <= 0)
177 return;
178
179 /* Inform pager that we are done */
180 (void) fflush(stdout);
181 if (stdout_redirected)
182 if (stored_stdout < 0 || dup2(stored_stdout, STDOUT_FILENO) < 0)
183 (void) close(STDOUT_FILENO);
184 stored_stdout = safe_close(stored_stdout);
185 (void) fflush(stderr);
186 if (stderr_redirected)
187 if (stored_stderr < 0 || dup2(stored_stderr, STDERR_FILENO) < 0)
188 (void) close(STDERR_FILENO);
189 stored_stderr = safe_close(stored_stderr);
190 stdout_redirected = stderr_redirected = false;
191
192 (void) kill(pager_pid, SIGCONT);
193 (void) wait_for_terminate(pager_pid, NULL);
194 pager_pid = 0;
195 }
196
197 bool pager_have(void) {
198 return pager_pid > 0;
199 }
200
201 int show_man_page(const char *desc, bool null_stdio) {
202 const char *args[4] = { "man", NULL, NULL, NULL };
203 char *e = NULL;
204 pid_t pid;
205 size_t k;
206 int r;
207 siginfo_t status;
208
209 k = strlen(desc);
210
211 if (desc[k-1] == ')')
212 e = strrchr(desc, '(');
213
214 if (e) {
215 char *page = NULL, *section = NULL;
216
217 page = strndupa(desc, e - desc);
218 section = strndupa(e + 1, desc + k - e - 2);
219
220 args[1] = section;
221 args[2] = page;
222 } else
223 args[1] = desc;
224
225 pid = fork();
226 if (pid < 0)
227 return log_error_errno(errno, "Failed to fork: %m");
228
229 if (pid == 0) {
230 /* Child */
231
232 (void) reset_all_signal_handlers();
233 (void) reset_signal_mask();
234
235 if (null_stdio) {
236 r = make_null_stdio();
237 if (r < 0) {
238 log_error_errno(r, "Failed to kill stdio: %m");
239 _exit(EXIT_FAILURE);
240 }
241 }
242
243 execvp(args[0], (char**) args);
244 log_error_errno(errno, "Failed to execute man: %m");
245 _exit(EXIT_FAILURE);
246 }
247
248 r = wait_for_terminate(pid, &status);
249 if (r < 0)
250 return r;
251
252 log_debug("Exit code %i status %i", status.si_code, status.si_status);
253 return status.si_status;
254 }