X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=src%2Fshared%2Fpager.c;h=58b62fdccfab66cd430c5bb304ef4c7dbd3cc2fd;hp=002e3aa37356c7b0f556dd635bcacade31c7d502;hb=24166fdaeee2e0e48cfaf0a3228581c3f11f5627;hpb=3d94f76c99da13e5603831d0b278f8c8c21bcb02 diff --git a/src/shared/pager.c b/src/shared/pager.c index 002e3aa37..58b62fdcc 100644 --- a/src/shared/pager.c +++ b/src/shared/pager.c @@ -19,7 +19,6 @@ along with systemd; If not, see . ***/ -#include #include #include #include @@ -28,7 +27,9 @@ #include "pager.h" #include "util.h" +#include "process-util.h" #include "macro.h" +#include "terminal-util.h" static pid_t pager_pid = 0; @@ -40,7 +41,7 @@ noreturn static void pager_fallback(void) { } while (n > 0); if (n < 0) { - log_error("Internal pager failed: %m"); + log_error_errno(errno, "Internal pager failed: %m"); _exit(EXIT_FAILURE); } @@ -67,17 +68,15 @@ int pager_open(bool jump_to_end) { * pager so that we get the value from the actual tty */ columns(); - if (pipe(fd) < 0) { - log_error("Failed to create pager pipe: %m"); - return -errno; - } + if (pipe(fd) < 0) + return log_error_errno(errno, "Failed to create pager pipe: %m"); parent_pid = getpid(); pager_pid = fork(); if (pager_pid < 0) { r = -errno; - log_error("Failed to fork pager: %m"); + log_error_errno(errno, "Failed to fork pager: %m"); safe_close_pair(fd); return r; } @@ -93,7 +92,7 @@ int pager_open(bool jump_to_end) { if (!less_opts) less_opts = "FRSXMK"; if (jump_to_end) - less_opts = strappenda(less_opts, " +G"); + less_opts = strjoina(less_opts, " +G"); setenv("LESS", less_opts, 1); /* Make sure the pager goes away when the parent dies */ @@ -126,10 +125,8 @@ int pager_open(bool jump_to_end) { } /* Return in the parent */ - if (dup2(fd[1], STDOUT_FILENO) < 0) { - log_error("Failed to duplicate pager pipe: %m"); - return -errno; - } + if (dup2(fd[1], STDOUT_FILENO) < 0) + return log_error_errno(errno, "Failed to duplicate pager pipe: %m"); safe_close_pair(fd); return 1; @@ -143,10 +140,61 @@ void pager_close(void) { /* Inform pager that we are done */ fclose(stdout); kill(pager_pid, SIGCONT); - wait_for_terminate(pager_pid, NULL); + (void) wait_for_terminate(pager_pid, NULL); pager_pid = 0; } bool pager_have(void) { return pager_pid > 0; } + +int show_man_page(const char *desc, bool null_stdio) { + const char *args[4] = { "man", NULL, NULL, NULL }; + char *e = NULL; + pid_t pid; + size_t k; + int r; + siginfo_t status; + + k = strlen(desc); + + if (desc[k-1] == ')') + e = strrchr(desc, '('); + + if (e) { + char *page = NULL, *section = NULL; + + page = strndupa(desc, e - desc); + section = strndupa(e + 1, desc + k - e - 2); + + args[1] = section; + args[2] = page; + } else + args[1] = desc; + + pid = fork(); + if (pid < 0) + return log_error_errno(errno, "Failed to fork: %m"); + + if (pid == 0) { + /* Child */ + if (null_stdio) { + r = make_null_stdio(); + if (r < 0) { + log_error_errno(r, "Failed to kill stdio: %m"); + _exit(EXIT_FAILURE); + } + } + + execvp(args[0], (char**) args); + log_error_errno(errno, "Failed to execute man: %m"); + _exit(EXIT_FAILURE); + } + + r = wait_for_terminate(pid, &status); + if (r < 0) + return r; + + log_debug("Exit code %i status %i", status.si_code, status.si_status); + return status.si_status; +}