]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/pager.c
58b62fdccfab66cd430c5bb304ef4c7dbd3cc2fd
[thirdparty/systemd.git] / src / shared / pager.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4 This file is part of systemd.
5
6 Copyright 2010 Lennart Poettering
7
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
12
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <fcntl.h>
23 #include <stdlib.h>
24 #include <unistd.h>
25 #include <string.h>
26 #include <sys/prctl.h>
27
28 #include "pager.h"
29 #include "util.h"
30 #include "process-util.h"
31 #include "macro.h"
32 #include "terminal-util.h"
33
34 static pid_t pager_pid = 0;
35
36 noreturn static void pager_fallback(void) {
37 ssize_t n;
38
39 do {
40 n = splice(STDIN_FILENO, NULL, STDOUT_FILENO, NULL, 64*1024, 0);
41 } while (n > 0);
42
43 if (n < 0) {
44 log_error_errno(errno, "Internal pager failed: %m");
45 _exit(EXIT_FAILURE);
46 }
47
48 _exit(EXIT_SUCCESS);
49 }
50
51 int pager_open(bool jump_to_end) {
52 int fd[2];
53 const char *pager;
54 pid_t parent_pid;
55 int r;
56
57 if (pager_pid > 0)
58 return 1;
59
60 if ((pager = getenv("SYSTEMD_PAGER")) || (pager = getenv("PAGER")))
61 if (!*pager || streq(pager, "cat"))
62 return 0;
63
64 if (!on_tty())
65 return 0;
66
67 /* Determine and cache number of columns before we spawn the
68 * pager so that we get the value from the actual tty */
69 columns();
70
71 if (pipe(fd) < 0)
72 return log_error_errno(errno, "Failed to create pager pipe: %m");
73
74 parent_pid = getpid();
75
76 pager_pid = fork();
77 if (pager_pid < 0) {
78 r = -errno;
79 log_error_errno(errno, "Failed to fork pager: %m");
80 safe_close_pair(fd);
81 return r;
82 }
83
84 /* In the child start the pager */
85 if (pager_pid == 0) {
86 const char* less_opts;
87
88 dup2(fd[0], STDIN_FILENO);
89 safe_close_pair(fd);
90
91 less_opts = getenv("SYSTEMD_LESS");
92 if (!less_opts)
93 less_opts = "FRSXMK";
94 if (jump_to_end)
95 less_opts = strjoina(less_opts, " +G");
96 setenv("LESS", less_opts, 1);
97
98 /* Make sure the pager goes away when the parent dies */
99 if (prctl(PR_SET_PDEATHSIG, SIGTERM) < 0)
100 _exit(EXIT_FAILURE);
101
102 /* Check whether our parent died before we were able
103 * to set the death signal */
104 if (getppid() != parent_pid)
105 _exit(EXIT_SUCCESS);
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 if (dup2(fd[1], STDOUT_FILENO) < 0)
129 return log_error_errno(errno, "Failed to duplicate pager pipe: %m");
130
131 safe_close_pair(fd);
132 return 1;
133 }
134
135 void pager_close(void) {
136
137 if (pager_pid <= 0)
138 return;
139
140 /* Inform pager that we are done */
141 fclose(stdout);
142 kill(pager_pid, SIGCONT);
143 (void) wait_for_terminate(pager_pid, NULL);
144 pager_pid = 0;
145 }
146
147 bool pager_have(void) {
148 return pager_pid > 0;
149 }
150
151 int show_man_page(const char *desc, bool null_stdio) {
152 const char *args[4] = { "man", NULL, NULL, NULL };
153 char *e = NULL;
154 pid_t pid;
155 size_t k;
156 int r;
157 siginfo_t status;
158
159 k = strlen(desc);
160
161 if (desc[k-1] == ')')
162 e = strrchr(desc, '(');
163
164 if (e) {
165 char *page = NULL, *section = NULL;
166
167 page = strndupa(desc, e - desc);
168 section = strndupa(e + 1, desc + k - e - 2);
169
170 args[1] = section;
171 args[2] = page;
172 } else
173 args[1] = desc;
174
175 pid = fork();
176 if (pid < 0)
177 return log_error_errno(errno, "Failed to fork: %m");
178
179 if (pid == 0) {
180 /* Child */
181 if (null_stdio) {
182 r = make_null_stdio();
183 if (r < 0) {
184 log_error_errno(r, "Failed to kill stdio: %m");
185 _exit(EXIT_FAILURE);
186 }
187 }
188
189 execvp(args[0], (char**) args);
190 log_error_errno(errno, "Failed to execute man: %m");
191 _exit(EXIT_FAILURE);
192 }
193
194 r = wait_for_terminate(pid, &status);
195 if (r < 0)
196 return r;
197
198 log_debug("Exit code %i status %i", status.si_code, status.si_status);
199 return status.si_status;
200 }