]>
Commit | Line | Data |
---|---|---|
1 | /* SPDX-License-Identifier: LGPL-2.1+ */ | |
2 | ||
3 | #include <errno.h> | |
4 | #include <signal.h> | |
5 | #include <stddef.h> | |
6 | #include <stdint.h> | |
7 | #include <stdio.h> | |
8 | #include <stdlib.h> | |
9 | #include <string.h> | |
10 | #include <sys/prctl.h> | |
11 | #include <unistd.h> | |
12 | ||
13 | #include "copy.h" | |
14 | #include "fd-util.h" | |
15 | #include "locale-util.h" | |
16 | #include "log.h" | |
17 | #include "macro.h" | |
18 | #include "pager.h" | |
19 | #include "process-util.h" | |
20 | #include "signal-util.h" | |
21 | #include "string-util.h" | |
22 | #include "strv.h" | |
23 | #include "terminal-util.h" | |
24 | ||
25 | static pid_t pager_pid = 0; | |
26 | ||
27 | static int stored_stdout = -1; | |
28 | static int stored_stderr = -1; | |
29 | static bool stdout_redirected = false; | |
30 | static bool stderr_redirected = false; | |
31 | ||
32 | _noreturn_ static void pager_fallback(void) { | |
33 | int r; | |
34 | ||
35 | r = copy_bytes(STDIN_FILENO, STDOUT_FILENO, (uint64_t) -1, 0); | |
36 | if (r < 0) { | |
37 | log_error_errno(r, "Internal pager failed: %m"); | |
38 | _exit(EXIT_FAILURE); | |
39 | } | |
40 | ||
41 | _exit(EXIT_SUCCESS); | |
42 | } | |
43 | ||
44 | int pager_open(bool no_pager, bool jump_to_end) { | |
45 | _cleanup_close_pair_ int fd[2] = { -1, -1 }; | |
46 | const char *pager; | |
47 | int r; | |
48 | ||
49 | if (no_pager) | |
50 | return 0; | |
51 | ||
52 | if (pager_pid > 0) | |
53 | return 1; | |
54 | ||
55 | if (terminal_is_dumb()) | |
56 | return 0; | |
57 | ||
58 | if (!is_main_thread()) | |
59 | return -EPERM; | |
60 | ||
61 | pager = getenv("SYSTEMD_PAGER"); | |
62 | if (!pager) | |
63 | pager = getenv("PAGER"); | |
64 | ||
65 | /* If the pager is explicitly turned off, honour it */ | |
66 | if (pager && STR_IN_SET(pager, "", "cat")) | |
67 | return 0; | |
68 | ||
69 | /* Determine and cache number of columns/lines before we spawn the pager so that we get the value from the | |
70 | * actual tty */ | |
71 | (void) columns(); | |
72 | (void) lines(); | |
73 | ||
74 | if (pipe2(fd, O_CLOEXEC) < 0) | |
75 | return log_error_errno(errno, "Failed to create pager pipe: %m"); | |
76 | ||
77 | r = safe_fork("(pager)", FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_LOG, &pager_pid); | |
78 | if (r < 0) | |
79 | return r; | |
80 | if (r == 0) { | |
81 | const char* less_opts, *less_charset; | |
82 | ||
83 | /* In the child start the pager */ | |
84 | ||
85 | (void) dup2(fd[0], STDIN_FILENO); | |
86 | safe_close_pair(fd); | |
87 | ||
88 | /* Initialize a good set of less options */ | |
89 | less_opts = getenv("SYSTEMD_LESS"); | |
90 | if (!less_opts) | |
91 | less_opts = "FRSXMK"; | |
92 | if (jump_to_end) | |
93 | less_opts = strjoina(less_opts, " +G"); | |
94 | if (setenv("LESS", less_opts, 1) < 0) | |
95 | _exit(EXIT_FAILURE); | |
96 | ||
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"; | |
103 | if (less_charset && | |
104 | setenv("LESSCHARSET", less_charset, 1) < 0) | |
105 | _exit(EXIT_FAILURE); | |
106 | ||
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); | |
122 | ||
123 | pager_fallback(); | |
124 | /* not reached */ | |
125 | } | |
126 | ||
127 | /* Return in the parent */ | |
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); | |
131 | return log_error_errno(errno, "Failed to duplicate pager pipe: %m"); | |
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); | |
138 | return log_error_errno(errno, "Failed to duplicate pager pipe: %m"); | |
139 | } | |
140 | stderr_redirected = true; | |
141 | ||
142 | return 1; | |
143 | } | |
144 | ||
145 | void pager_close(void) { | |
146 | ||
147 | if (pager_pid <= 0) | |
148 | return; | |
149 | ||
150 | /* Inform pager that we are done */ | |
151 | (void) fflush(stdout); | |
152 | if (stdout_redirected) | |
153 | if (stored_stdout < 0 || dup2(stored_stdout, STDOUT_FILENO) < 0) | |
154 | (void) close(STDOUT_FILENO); | |
155 | stored_stdout = safe_close(stored_stdout); | |
156 | (void) fflush(stderr); | |
157 | if (stderr_redirected) | |
158 | if (stored_stderr < 0 || dup2(stored_stderr, STDERR_FILENO) < 0) | |
159 | (void) close(STDERR_FILENO); | |
160 | stored_stderr = safe_close(stored_stderr); | |
161 | stdout_redirected = stderr_redirected = false; | |
162 | ||
163 | (void) kill(pager_pid, SIGCONT); | |
164 | (void) wait_for_terminate(pager_pid, NULL); | |
165 | pager_pid = 0; | |
166 | } | |
167 | ||
168 | bool pager_have(void) { | |
169 | return pager_pid > 0; | |
170 | } | |
171 | ||
172 | int 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; | |
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 | ||
195 | r = safe_fork("(man)", FORK_RESET_SIGNALS|FORK_DEATHSIG|(null_stdio ? FORK_NULL_STDIO : 0)|FORK_LOG, &pid); | |
196 | if (r < 0) | |
197 | return r; | |
198 | if (r == 0) { | |
199 | /* Child */ | |
200 | execvp(args[0], (char**) args); | |
201 | log_error_errno(errno, "Failed to execute man: %m"); | |
202 | _exit(EXIT_FAILURE); | |
203 | } | |
204 | ||
205 | return wait_for_terminate_and_check(NULL, pid, 0); | |
206 | } |