This is an automated email from the git hooks/post-receive script.

guillem pushed a commit to branch master
in repository dpkg.

View the commit online:
https://git.dpkg.org/cgit/dpkg/dpkg.git/commit/?id=5d194d5d55d5a7ac62422b7c98aa23548c5d7349

commit 5d194d5d55d5a7ac62422b7c98aa23548c5d7349
Author: Guillem Jover <[email protected]>
AuthorDate: Tue Oct 2 04:57:47 2018 +0200

    libdpkg: Ignore SIGPIPE when setting up a pager
    
    If the pager quits early, the parent will receive a SIGPIPE as the write
    end of the pipe will not be available anymore. Instead we ignore SIGPIPE
    and also EPIPE errors when writing to stdout.
    
    Otherwise if we quit the pager early, the program will exit with an
    error code.
    
    Reported-by: Holger Levsen <[email protected]>
    Ref: #909754
---
 debian/changelog |  3 +++
 lib/dpkg/mlib.c  |  3 ++-
 lib/dpkg/pager.c | 12 ++++++++++++
 3 files changed, 17 insertions(+), 1 deletion(-)

diff --git a/debian/changelog b/debian/changelog
index c7e9fc8ef..359d23f6b 100644
--- a/debian/changelog
+++ b/debian/changelog
@@ -10,6 +10,9 @@ dpkg (1.19.2) UNRELEASED; urgency=medium
   * libdpkg: Honor DPKG_PAGER when spawning a pager.
     Suggested by Craig Sanders <[email protected]>.
   * libdpkg: Set LESS to “-FRSXMQ” if not already set, when spawning a pager.
+  * libdpkg: Ignore SIGPIPE when setting up a pager, and then ignore EPIPE
+    errors when writing to stdout, otherwise if we quit the pager early, the
+    program will exit with an error code.
   * Perl modules:
     - Dpkg::OpenPGP: Ignore Version field in enarmored output.
       Fixes CPAN#127217.
diff --git a/lib/dpkg/mlib.c b/lib/dpkg/mlib.c
index 8de77b7ea..4e5e5b1a8 100644
--- a/lib/dpkg/mlib.c
+++ b/lib/dpkg/mlib.c
@@ -24,6 +24,7 @@
 
 #include <sys/types.h>
 
+#include <errno.h>
 #include <string.h>
 #include <fcntl.h>
 #include <unistd.h>
@@ -143,7 +144,7 @@ void
 m_output(FILE *f, const char *name)
 {
   fflush(f);
-  if (ferror(f))
+  if (ferror(f) && errno != EPIPE)
     ohshite(_("error writing to '%s'"), name);
 }
 
diff --git a/lib/dpkg/pager.c b/lib/dpkg/pager.c
index 96cfbb0da..ed41daedb 100644
--- a/lib/dpkg/pager.c
+++ b/lib/dpkg/pager.c
@@ -25,6 +25,7 @@
 
 #include <stdbool.h>
 #include <stdlib.h>
+#include <signal.h>
 #include <unistd.h>
 
 #include <dpkg/dpkg.h>
@@ -60,6 +61,7 @@ struct pager {
        bool used;
        const char *desc;
        pid_t pid;
+       struct sigaction sigpipe;
        int stdout_old;
        int pipe[2];
 };
@@ -67,6 +69,7 @@ struct pager {
 struct pager *
 pager_spawn(const char *desc)
 {
+       struct sigaction sa;
        struct pager *pager;
        const char *exec;
 
@@ -83,6 +86,13 @@ pager_spawn(const char *desc)
 
        m_pipe(pager->pipe);
 
+       memset(&sa, 0, sizeof(sa));
+       sigemptyset(&sa.sa_mask);
+       sa.sa_handler = SIG_IGN;
+       sa.sa_flags = 0;
+
+       sigaction(SIGPIPE, &sa, &pager->sigpipe);
+
        pager->pid = subproc_fork();
        if (pager->pid == 0) {
                /* Set better defaults for less if not already set. */
@@ -112,5 +122,7 @@ pager_reap(struct pager *pager)
        m_dup2(pager->stdout_old, 1);
        subproc_reap(pager->pid, pager->desc, SUBPROC_NOPIPE);
 
+       sigaction(SIGPIPE, &pager->sigpipe, NULL);
+
        free(pager);
 }

-- 
Dpkg.Org's dpkg

Reply via email to