• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

systemd / systemd / 15057632786

15 May 2025 09:01PM UTC coverage: 72.267% (+0.02%) from 72.244%
15057632786

push

github

bluca
man: document how to hook stuff into system wakeup

Fixes: #6364

298523 of 413084 relevant lines covered (72.27%)

738132.88 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

7.02
/src/shared/pager.c
1
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2

3
#include <errno.h>
4
#include <stddef.h>
5
#include <stdint.h>
6
#include <stdio.h>
7
#include <stdlib.h>
8
#include <sys/prctl.h>
9
#include <unistd.h>
10

11
#include "sd-login.h"
12

13
#include "copy.h"
14
#include "env-util.h"
15
#include "fd-util.h"
16
#include "fileio.h"
17
#include "io-util.h"
18
#include "locale-util.h"
19
#include "log.h"
20
#include "macro.h"
21
#include "pager.h"
22
#include "process-util.h"
23
#include "rlimit-util.h"
24
#include "signal-util.h"
25
#include "string-util.h"
26
#include "strv.h"
27
#include "terminal-util.h"
28

29
static pid_t pager_pid = 0;
30

31
static int stored_stdout = -1;
32
static int stored_stderr = -1;
33
static bool stdout_redirected = false;
34
static bool stderr_redirected = false;
35

36
_noreturn_ static void pager_fallback(void) {
×
37
        int r;
×
38

39
        r = copy_bytes(STDIN_FILENO, STDOUT_FILENO, UINT64_MAX, 0);
×
40
        if (r < 0) {
×
41
                log_error_errno(r, "Internal pager failed: %m");
×
42
                _exit(EXIT_FAILURE);
×
43
        }
44

45
        _exit(EXIT_SUCCESS);
×
46
}
47

48
static int no_quit_on_interrupt(int exe_name_fd, const char *less_opts) {
×
49
        _cleanup_fclose_ FILE *file = NULL;
×
50
        _cleanup_free_ char *line = NULL;
×
51
        int r;
×
52

53
        assert(exe_name_fd >= 0);
×
54
        assert(less_opts);
×
55

56
        /* This takes ownership of exe_name_fd */
57
        file = fdopen(exe_name_fd, "r");
×
58
        if (!file) {
×
59
                safe_close(exe_name_fd);
×
60
                return log_error_errno(errno, "Failed to create FILE object: %m");
×
61
        }
62

63
        /* Find the last line */
64
        for (;;) {
×
65
                _cleanup_free_ char *t = NULL;
×
66

67
                r = read_line(file, LONG_LINE_MAX, &t);
×
68
                if (r < 0)
×
69
                        return log_error_errno(r, "Failed to read from socket: %m");
×
70
                if (r == 0)
×
71
                        break;
72

73
                free_and_replace(line, t);
×
74
        }
75

76
        /* We only treat "less" specially.
77
         * Return true whenever option K is *not* set. */
78
        r = streq_ptr(line, "less") && !strchr(less_opts, 'K');
×
79

80
        log_debug("Pager executable is \"%s\", options \"%s\", quit_on_interrupt: %s",
×
81
                  strnull(line), less_opts, yes_no(!r));
82
        return r;
83
}
84

85
static bool running_with_escalated_privileges(void) {
×
86
        int r;
×
87

88
        if (getenv("SUDO_UID"))
×
89
                return true;
×
90

91
        uid_t uid;
×
92
        r = sd_pid_get_owner_uid(0, &uid);
×
93
        if (r < 0) {
×
94
                log_debug_errno(r, "sd_pid_get_owner_uid() failed, enabling pager secure mode: %m");
×
95
                return true;
×
96
        }
97

98
        return uid != geteuid();
×
99
}
100

101
void pager_open(PagerFlags flags) {
36,575✔
102
        _cleanup_close_pair_ int fd[2] = EBADF_PAIR, exe_name_pipe[2] = EBADF_PAIR;
36,575✔
103
        _cleanup_strv_free_ char **pager_args = NULL;
×
104
        _cleanup_free_ char *l = NULL;
36,575✔
105
        const char *pager, *less_opts;
36,575✔
106
        int r;
36,575✔
107

108
        if (flags & PAGER_DISABLE)
36,575✔
109
                return;
110

111
        if (pager_pid > 0)
36,430✔
112
                return;
113

114
        if (terminal_is_dumb())
36,430✔
115
                return;
116

117
        if (!is_main_thread())
×
118
                return (void) log_error_errno(SYNTHETIC_ERRNO(EPERM), "Pager invoked from wrong thread.");
×
119

120
        pager = getenv("SYSTEMD_PAGER");
×
121
        if (!pager)
×
122
                pager = getenv("PAGER");
×
123

124
        if (pager) {
×
125
                pager_args = strv_split(pager, WHITESPACE);
×
126
                if (!pager_args)
×
127
                        return (void) log_oom();
×
128

129
                /* If the pager is explicitly turned off, honour it */
130
                if (strv_isempty(pager_args) || strv_equal(pager_args, STRV_MAKE("cat")))
×
131
                        return;
×
132
        }
133

134
        /* Determine and cache number of columns/lines before we spawn the pager so that we get the value from the
135
         * actual tty */
136
        (void) columns();
×
137
        (void) lines();
×
138

139
        if (pipe2(fd, O_CLOEXEC) < 0)
×
140
                return (void) log_error_errno(errno, "Failed to create pager pipe: %m");
×
141

142
        /* This is a pipe to feed the name of the executed pager binary into the parent */
143
        if (pipe2(exe_name_pipe, O_CLOEXEC) < 0)
×
144
                return (void) log_error_errno(errno, "Failed to create exe_name pipe: %m");
×
145

146
        /* Initialize a good set of less options */
147
        less_opts = getenv("SYSTEMD_LESS");
×
148
        if (!less_opts)
×
149
                less_opts = "FRSXMK";
×
150
        if (flags & PAGER_JUMP_TO_END) {
×
151
                l = strjoin(less_opts, " +G");
×
152
                if (!l)
×
153
                        return (void) log_oom();
×
154
                less_opts = l;
155
        }
156

157
        /* We set SIGINT as PR_DEATHSIG signal here, to match the "K" parameter we set in $LESS, which enables SIGINT behaviour. */
158
        r = safe_fork("(pager)", FORK_RESET_SIGNALS|FORK_DEATHSIG_SIGINT|FORK_RLIMIT_NOFILE_SAFE|FORK_LOG, &pager_pid);
×
159
        if (r < 0)
×
160
                return;
161
        if (r == 0) {
×
162
                const char *less_charset;
×
163

164
                /* In the child start the pager */
165

166
                if (dup2(fd[0], STDIN_FILENO) < 0) {
×
167
                        log_error_errno(errno, "Failed to duplicate file descriptor to STDIN: %m");
×
168
                        _exit(EXIT_FAILURE);
×
169
                }
170

171
                safe_close_pair(fd);
×
172

173
                if (setenv("LESS", less_opts, 1) < 0) {
×
174
                        log_error_errno(errno, "Failed to set environment variable LESS: %m");
×
175
                        _exit(EXIT_FAILURE);
×
176
                }
177

178
                /* Initialize a good charset for less. This is particularly important if we output UTF-8
179
                 * characters. */
180
                less_charset = getenv("SYSTEMD_LESSCHARSET");
×
181
                if (!less_charset && is_locale_utf8())
×
182
                        less_charset = "utf-8";
183
                if (less_charset &&
×
184
                    setenv("LESSCHARSET", less_charset, 1) < 0) {
×
185
                        log_error_errno(errno, "Failed to set environment variable LESSCHARSET: %m");
×
186
                        _exit(EXIT_FAILURE);
×
187
                }
188

189
                /* People might invoke us from sudo, don't needlessly allow less to be a way to shell out
190
                 * privileged stuff. If the user set $SYSTEMD_PAGERSECURE, trust their configuration of the
191
                 * pager. If they didn't, use secure mode when under euid is changed. If $SYSTEMD_PAGERSECURE
192
                 * wasn't explicitly set, and we autodetect the need for secure mode, only use the pager we
193
                 * know to be good. */
194
                int use_secure_mode = secure_getenv_bool("SYSTEMD_PAGERSECURE");
×
195
                bool trust_pager = use_secure_mode >= 0;
×
196
                if (use_secure_mode == -ENXIO)
×
197
                        use_secure_mode = running_with_escalated_privileges();
×
198
                else if (use_secure_mode < 0) {
×
199
                        log_warning_errno(use_secure_mode, "Unable to parse $SYSTEMD_PAGERSECURE, assuming true: %m");
×
200
                        use_secure_mode = true;
201
                }
202

203
                /* We generally always set variables used by less, even if we end up using a different pager.
204
                 * They shouldn't hurt in any case, and ideally other pagers would look at them too. */
205
                r = set_unset_env("LESSSECURE", use_secure_mode ? "1" : NULL, true);
×
206
                if (r < 0) {
×
207
                        log_error_errno(r, "Failed to adjust environment variable LESSSECURE: %m");
×
208
                        _exit(EXIT_FAILURE);
×
209
                }
210

211
                if (trust_pager && pager_args) { /* The pager config might be set globally, and we cannot
×
212
                                                  * know if the user adjusted it to be appropriate for the
213
                                                  * secure mode. Thus, start the pager specified through
214
                                                  * envvars only when $SYSTEMD_PAGERSECURE was explicitly set
215
                                                  * as well. */
216
                        r = loop_write(exe_name_pipe[1], pager_args[0], strlen(pager_args[0]) + 1);
×
217
                        if (r < 0) {
×
218
                                log_error_errno(r, "Failed to write pager name to socket: %m");
×
219
                                _exit(EXIT_FAILURE);
×
220
                        }
221

222
                        execvp(pager_args[0], pager_args);
×
223
                        log_full_errno(errno == ENOENT ? LOG_DEBUG : LOG_WARNING, errno,
×
224
                                       "Failed to execute '%s', using fallback pagers: %m", pager_args[0]);
225
                }
226

227
                /* Debian's alternatives command for pagers is called 'pager'. Note that we do not call
228
                 * sensible-pagers here, since that is just a shell script that implements a logic that is
229
                 * similar to this one anyway, but is Debian-specific. */
230
                static const char* pagers[] = { "pager", "less", "more", "(built-in)" };
231

232
                for (unsigned i = 0; i < ELEMENTSOF(pagers); i++) {
×
233
                        /* Only less (and our trivial fallback) implement secure mode right now. */
234
                        if (use_secure_mode && !STR_IN_SET(pagers[i], "less", "(built-in)"))
×
235
                                continue;
×
236

237
                        r = loop_write(exe_name_pipe[1], pagers[i], strlen(pagers[i]) + 1);
×
238
                        if (r < 0) {
×
239
                                log_error_errno(r, "Failed to write pager name to socket: %m");
×
240
                                _exit(EXIT_FAILURE);
×
241
                        }
242

243
                        if (i < ELEMENTSOF(pagers) - 1) {
×
244
                                execlp(pagers[i], pagers[i], NULL);
×
245
                                log_full_errno(errno == ENOENT ? LOG_DEBUG : LOG_WARNING, errno,
×
246
                                               "Failed to execute '%s', will try '%s' next: %m", pagers[i], pagers[i+1]);
247
                        } else {
248
                                /* Close pipe to signal the parent to start sending data */
249
                                safe_close_pair(exe_name_pipe);
×
250
                                pager_fallback();
×
251
                                assert_not_reached();
252
                        }
253
                }
254
        }
255

256
        /* Return in the parent */
257
        stored_stdout = fcntl(STDOUT_FILENO, F_DUPFD_CLOEXEC, 3);
×
258
        if (dup2(fd[1], STDOUT_FILENO) < 0) {
×
259
                stored_stdout = safe_close(stored_stdout);
×
260
                return (void) log_error_errno(errno, "Failed to duplicate pager pipe: %m");
×
261
        }
262
        stdout_redirected = true;
×
263

264
        stored_stderr = fcntl(STDERR_FILENO, F_DUPFD_CLOEXEC, 3);
×
265
        if (dup2(fd[1], STDERR_FILENO) < 0) {
×
266
                stored_stderr = safe_close(stored_stderr);
×
267
                return (void) log_error_errno(errno, "Failed to duplicate pager pipe: %m");
×
268
        }
269
        stderr_redirected = true;
×
270

271
        exe_name_pipe[1] = safe_close(exe_name_pipe[1]);
×
272

273
        r = no_quit_on_interrupt(TAKE_FD(exe_name_pipe[0]), less_opts);
×
274
        if (r > 0)
×
275
                (void) ignore_signals(SIGINT);
×
276
}
277

278
void pager_close(void) {
73,753✔
279

280
        if (pager_pid <= 0)
73,753✔
281
                return;
282

283
        /* Inform pager that we are done */
284
        (void) fflush(stdout);
×
285
        if (stdout_redirected)
×
286
                if (stored_stdout < 0 || dup2(stored_stdout, STDOUT_FILENO) < 0)
×
287
                        (void) close(STDOUT_FILENO);
×
288
        stored_stdout = safe_close(stored_stdout);
×
289
        (void) fflush(stderr);
×
290
        if (stderr_redirected)
×
291
                if (stored_stderr < 0 || dup2(stored_stderr, STDERR_FILENO) < 0)
×
292
                        (void) close(STDERR_FILENO);
×
293
        stored_stderr = safe_close(stored_stderr);
×
294
        stdout_redirected = stderr_redirected = false;
×
295

296
        (void) kill(pager_pid, SIGCONT);
×
297
        (void) wait_for_terminate(TAKE_PID(pager_pid), NULL);
×
298
        pager_pid = 0;
×
299
}
300

301
bool pager_have(void) {
3,044✔
302
        return pager_pid > 0;
3,044✔
303
}
304

305
int show_man_page(const char *desc, bool null_stdio) {
×
306
        const char *args[4] = { "man", NULL, NULL, NULL };
×
307
        char *e = NULL;
×
308
        pid_t pid;
×
309
        size_t k;
×
310
        int r;
×
311

312
        k = strlen(desc);
×
313

314
        if (desc[k-1] == ')')
×
315
                e = strrchr(desc, '(');
×
316

317
        if (e) {
×
318
                char *page = NULL, *section = NULL;
×
319

320
                page = strndupa_safe(desc, e - desc);
×
321
                section = strndupa_safe(e + 1, desc + k - e - 2);
×
322

323
                args[1] = section;
×
324
                args[2] = page;
×
325
        } else
326
                args[1] = desc;
×
327

328
        r = safe_fork("(man)", FORK_RESET_SIGNALS|FORK_DEATHSIG_SIGTERM|(null_stdio ? FORK_REARRANGE_STDIO : 0)|FORK_RLIMIT_NOFILE_SAFE|FORK_LOG, &pid);
×
329
        if (r < 0)
×
330
                return r;
×
331
        if (r == 0) {
×
332
                /* Child */
333
                execvp(args[0], (char**) args);
×
334
                log_error_errno(errno, "Failed to execute man: %m");
×
335
                _exit(EXIT_FAILURE);
×
336
        }
337

338
        return wait_for_terminate_and_check(NULL, pid, 0);
×
339
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc