]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/pager.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2010 Lennart Poettering
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.
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.
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/>.
28 #include <sys/prctl.h>
33 #include "locale-util.h"
37 #include "process-util.h"
38 #include "signal-util.h"
39 #include "string-util.h"
41 #include "terminal-util.h"
43 static pid_t pager_pid
= 0;
45 noreturn
static void pager_fallback(void) {
48 r
= copy_bytes(STDIN_FILENO
, STDOUT_FILENO
, (uint64_t) -1, 0);
50 log_error_errno(r
, "Internal pager failed: %m");
57 static int stored_stdout
= -1;
58 static int stored_stderr
= -1;
59 static bool stdout_redirected
= false;
60 static bool stderr_redirected
= false;
62 int pager_open(bool no_pager
, bool jump_to_end
) {
63 _cleanup_close_pair_
int fd
[2] = { -1, -1 };
73 if (terminal_is_dumb())
76 pager
= getenv("SYSTEMD_PAGER");
78 pager
= getenv("PAGER");
80 /* If the pager is explicitly turned off, honour it */
81 if (pager
&& STR_IN_SET(pager
, "", "cat"))
84 /* Determine and cache number of columns/lines before we spawn the pager so that we get the value from the
89 if (pipe2(fd
, O_CLOEXEC
) < 0)
90 return log_error_errno(errno
, "Failed to create pager pipe: %m");
92 parent_pid
= getpid_cached();
96 return log_error_errno(errno
, "Failed to fork pager: %m");
98 /* In the child start the pager */
100 const char* less_opts
, *less_charset
;
102 (void) reset_all_signal_handlers();
103 (void) reset_signal_mask();
105 (void) dup2(fd
[0], STDIN_FILENO
);
108 /* Initialize a good set of less options */
109 less_opts
= getenv("SYSTEMD_LESS");
111 less_opts
= "FRSXMK";
113 less_opts
= strjoina(less_opts
, " +G");
114 if (setenv("LESS", less_opts
, 1) < 0)
117 /* Initialize a good charset for less. This is
118 * particularly important if we output UTF-8
120 less_charset
= getenv("SYSTEMD_LESSCHARSET");
121 if (!less_charset
&& is_locale_utf8())
122 less_charset
= "utf-8";
124 setenv("LESSCHARSET", less_charset
, 1) < 0)
127 /* Make sure the pager goes away when the parent dies */
128 if (prctl(PR_SET_PDEATHSIG
, SIGTERM
) < 0)
131 /* Check whether our parent died before we were able
132 * to set the death signal */
133 if (getppid() != parent_pid
)
137 execlp(pager
, pager
, NULL
);
138 execl("/bin/sh", "sh", "-c", pager
, NULL
);
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
);
149 execlp("less", "less", NULL
);
150 execlp("more", "more", NULL
);
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");
162 stdout_redirected
= true;
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");
169 stderr_redirected
= true;
174 void pager_close(void) {
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;
192 (void) kill(pager_pid
, SIGCONT
);
193 (void) wait_for_terminate(pager_pid
, NULL
);
197 bool pager_have(void) {
198 return pager_pid
> 0;
201 int show_man_page(const char *desc
, bool null_stdio
) {
202 const char *args
[4] = { "man", NULL
, NULL
, NULL
};
211 if (desc
[k
-1] == ')')
212 e
= strrchr(desc
, '(');
215 char *page
= NULL
, *section
= NULL
;
217 page
= strndupa(desc
, e
- desc
);
218 section
= strndupa(e
+ 1, desc
+ k
- e
- 2);
227 return log_error_errno(errno
, "Failed to fork: %m");
232 (void) reset_all_signal_handlers();
233 (void) reset_signal_mask();
236 r
= make_null_stdio();
238 log_error_errno(r
, "Failed to kill stdio: %m");
243 execvp(args
[0], (char**) args
);
244 log_error_errno(errno
, "Failed to execute man: %m");
248 r
= wait_for_terminate(pid
, &status
);
252 log_debug("Exit code %i status %i", status
.si_code
, status
.si_status
);
253 return status
.si_status
;