]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/shared/pager.c
core: fix event source annotations
[thirdparty/systemd.git] / src / shared / pager.c
CommitLineData
1968a360
LP
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
5430f7f2
LP
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
1968a360
LP
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
5430f7f2 16 Lesser General Public License for more details.
1968a360 17
5430f7f2 18 You should have received a copy of the GNU Lesser General Public License
1968a360
LP
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20***/
21
4a8e40eb 22#include <fcntl.h>
1968a360
LP
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"
0b452006 30#include "process-util.h"
1968a360 31#include "macro.h"
288a74cc 32#include "terminal-util.h"
1968a360
LP
33
34static pid_t pager_pid = 0;
35
919ce0b7 36noreturn static void pager_fallback(void) {
4a8e40eb 37 ssize_t n;
46e65dcc 38
4a8e40eb
MS
39 do {
40 n = splice(STDIN_FILENO, NULL, STDOUT_FILENO, NULL, 64*1024, 0);
41 } while (n > 0);
46e65dcc 42
4a8e40eb 43 if (n < 0) {
56f64d95 44 log_error_errno(errno, "Internal pager failed: %m");
4a8e40eb
MS
45 _exit(EXIT_FAILURE);
46 }
46e65dcc 47
4a8e40eb
MS
48 _exit(EXIT_SUCCESS);
49}
50
1b12a7b5 51int pager_open(bool jump_to_end) {
1968a360
LP
52 int fd[2];
53 const char *pager;
54 pid_t parent_pid;
f89a3b6f 55 int r;
1968a360
LP
56
57 if (pager_pid > 0)
f89a3b6f 58 return 1;
1968a360 59
1968a360
LP
60 if ((pager = getenv("SYSTEMD_PAGER")) || (pager = getenv("PAGER")))
61 if (!*pager || streq(pager, "cat"))
f89a3b6f 62 return 0;
1968a360 63
8481248b 64 if (!on_tty())
f89a3b6f 65 return 0;
729e3769 66
1968a360
LP
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
4a62c710
MS
71 if (pipe(fd) < 0)
72 return log_error_errno(errno, "Failed to create pager pipe: %m");
1968a360
LP
73
74 parent_pid = getpid();
75
76 pager_pid = fork();
77 if (pager_pid < 0) {
f89a3b6f 78 r = -errno;
56f64d95 79 log_error_errno(errno, "Failed to fork pager: %m");
3d94f76c 80 safe_close_pair(fd);
f89a3b6f 81 return r;
1968a360
LP
82 }
83
84 /* In the child start the pager */
85 if (pager_pid == 0) {
f366d58d 86 const char* less_opts;
1968a360
LP
87
88 dup2(fd[0], STDIN_FILENO);
3d94f76c 89 safe_close_pair(fd);
1968a360 90
f366d58d
JD
91 less_opts = getenv("SYSTEMD_LESS");
92 if (!less_opts)
93 less_opts = "FRSXMK";
1b12a7b5 94 if (jump_to_end)
63c372cb 95 less_opts = strjoina(less_opts, " +G");
f366d58d 96 setenv("LESS", less_opts, 1);
1968a360
LP
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);
1968a360 122
4a8e40eb
MS
123 pager_fallback();
124 /* not reached */
1968a360
LP
125 }
126
127 /* Return in the parent */
4a62c710
MS
128 if (dup2(fd[1], STDOUT_FILENO) < 0)
129 return log_error_errno(errno, "Failed to duplicate pager pipe: %m");
1968a360 130
3d94f76c 131 safe_close_pair(fd);
f89a3b6f 132 return 1;
1968a360
LP
133}
134
135void 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);
c73d180d 143 (void) wait_for_terminate(pager_pid, NULL);
1968a360
LP
144 pager_pid = 0;
145}
f89a3b6f
LP
146
147bool pager_have(void) {
148 return pager_pid > 0;
149}
78002a67
ZJS
150
151int 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();
4a62c710
MS
176 if (pid < 0)
177 return log_error_errno(errno, "Failed to fork: %m");
78002a67
ZJS
178
179 if (pid == 0) {
180 /* Child */
181 if (null_stdio) {
182 r = make_null_stdio();
183 if (r < 0) {
da927ba9 184 log_error_errno(r, "Failed to kill stdio: %m");
78002a67
ZJS
185 _exit(EXIT_FAILURE);
186 }
187 }
188
189 execvp(args[0], (char**) args);
56f64d95 190 log_error_errno(errno, "Failed to execute man: %m");
78002a67
ZJS
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}