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