Blame SOURCES/xsane-0.999-signal-handling.patch

ae567d
From 3b5d3b7e1f320b0bfbe48024a586c0a22375aa2d Mon Sep 17 00:00:00 2001
ae567d
From: Nils Philippsen <nils@redhat.com>
ae567d
Date: Thu, 3 Jul 2014 10:38:03 +0200
ae567d
Subject: [PATCH] patch: signal-handling
ae567d
ae567d
Squashed commit of the following:
ae567d
ae567d
commit 1e9e8cf5edc469114c8eadf46817cd5c1261b35c
ae567d
Author: Nils Philippsen <nils@redhat.com>
ae567d
Date:   Thu Jul 3 10:14:52 2014 +0200
ae567d
ae567d
    don't use g_unix_open_pipe(), g_unix_fd_add()
ae567d
ae567d
    These functions have only recently been added to glib. Use pipe()/
ae567d
    fcntl() and g_io_channel_unix_new()/g_io_add_watch() instead which are
ae567d
    available in the minimum glib version needed for gtk+-2.x.
ae567d
ae567d
commit acbdf3f693d3d2a78ee7490ca1bf76957daf00cf
ae567d
Author: Nils Philippsen <nils@redhat.com>
ae567d
Date:   Thu Mar 13 13:38:12 2014 +0100
ae567d
ae567d
    separate signal handlers in top and bottom half
ae567d
ae567d
    This is to avoid race-conditions occurring when a signal is received
ae567d
    while the signal handler is not yet finished. It also avoids calling
ae567d
    non-reentrant functions from a signal handler. The top half (the real
ae567d
    signal handler) just writes a character into a pipe which gets picked up
ae567d
    and serviced by the bottom half from the normal event loop, this
ae567d
    serializes things and makes using non-reentrant functions safe.
ae567d
---
ae567d
 src/xsane.c | 151 ++++++++++++++++++++++++++++++++++++++++++++++++++++++------
ae567d
 1 file changed, 136 insertions(+), 15 deletions(-)
ae567d
ae567d
diff --git a/src/xsane.c b/src/xsane.c
ae567d
index 2b9211b..fc2ebbe 100644
ae567d
--- a/src/xsane.c
ae567d
+++ b/src/xsane.c
ae567d
@@ -47,6 +47,7 @@
ae567d
 #endif
ae567d
 
ae567d
 #include <sys/wait.h>
ae567d
+#include <glib-unix.h>
ae567d
 
ae567d
 #include <stdarg.h>
ae567d
 
ae567d
@@ -121,6 +122,7 @@ static const Preferences_medium_t pref_default_medium[]=
ae567d
 
ae567d
 int DBG_LEVEL = 0;
ae567d
 static guint xsane_resolution_timer = 0;
ae567d
+static int xsane_signal_pipe[2];
ae567d
 
ae567d
 /* ---------------------------------------------------------------------------------------------------------------------- */
ae567d
 
ae567d
@@ -161,8 +163,11 @@ void xsane_pref_save(void);
ae567d
 static int xsane_pref_restore(void);
ae567d
 static void xsane_pref_save_media(void);
ae567d
 static void xsane_pref_restore_media(void);
ae567d
-static RETSIGTYPE xsane_quit_handler(int signal);
ae567d
-static RETSIGTYPE xsane_sigchld_handler(int signal);
ae567d
+static RETSIGTYPE xsane_signal_handler_top_half(int signal);
ae567d
+static gboolean xsane_signal_handler_bottom_half(GIOChannel *source,
ae567d
+                                                 GIOCondition condition,
ae567d
+                                                 gpointer user_data);
ae567d
+static void xsane_sigchld_handler(void);
ae567d
 static void xsane_quit(void);
ae567d
 static void xsane_exit(void);
ae567d
 static gint xsane_standard_option_win_delete(GtkWidget *widget, gpointer data);
ae567d
@@ -2296,16 +2301,119 @@ static void xsane_pref_restore_media(void)
ae567d
 
ae567d
 /* ---------------------------------------------------------------------------------------------------------------------- */
ae567d
 
ae567d
-static RETSIGTYPE xsane_quit_handler(int signal)
ae567d
+static RETSIGTYPE xsane_signal_handler_top_half(int signal)
ae567d
 {
ae567d
-  DBG(DBG_proc, "xsane_quit_handler\n");
ae567d
+  const char *msg_func = "xsane_signal_handler_top_half(): ";
ae567d
+  const char *msg_short_write = "Short write() while processing signal.\n";
ae567d
+  const char *msg_err = "Error during write().\n";
ae567d
+  char sig_char;
ae567d
+  ssize_t written;
ae567d
+  int errno_saved = errno;
ae567d
 
ae567d
-  xsane_quit();
ae567d
+  switch (signal)
ae567d
+  {
ae567d
+    case SIGTERM:
ae567d
+      sig_char = 't';
ae567d
+      break;
ae567d
+    case SIGINT:
ae567d
+      sig_char = 'i';
ae567d
+      break;
ae567d
+    case SIGHUP:
ae567d
+      sig_char = 'h';
ae567d
+      break;
ae567d
+    case SIGCHLD:
ae567d
+      sig_char = 'c';
ae567d
+      break;
ae567d
+    default:
ae567d
+      sig_char = '?';
ae567d
+      break;
ae567d
+  }
ae567d
+
ae567d
+  if ((written = write(xsane_signal_pipe[1], &sig_char, 1)) <= 0)
ae567d
+  {
ae567d
+    /* At this point, all bets are off. Salvage what we can. */
ae567d
+
ae567d
+    const char *msg = (written == 0) ? msg_short_write : msg_err;
ae567d
+
ae567d
+    if ((write(STDERR_FILENO, msg_func, strlen(msg_func)) < 0) ||
ae567d
+        (write(STDERR_FILENO, msg, strlen(msg)) < 0))
ae567d
+    {
ae567d
+      /* This is really a no-op, but at this point it doesn't really matter
ae567d
+       * anymore if the writes succeeded or not. */
ae567d
+      goto bail_out;
ae567d
+    }
ae567d
+
ae567d
+bail_out:
ae567d
+    /* Ignore SIGCHLD errors, zombie processes don't hurt that much. */
ae567d
+    if (signal != SIGCHLD)
ae567d
+    {
ae567d
+      struct SIGACTION act;
ae567d
+      memset(&act, 0, sizeof(act));
ae567d
+      act.sa_handler = SIG_DFL;
ae567d
+      sigaction(signal, &act, NULL);
ae567d
+      raise(signal);
ae567d
+    }
ae567d
+  }
ae567d
+
ae567d
+  errno = errno_saved;
ae567d
+}
ae567d
+
ae567d
+static gboolean xsane_signal_handler_bottom_half(GIOChannel *source,
ae567d
+                                                 GIOCondition condition,
ae567d
+                                                 gpointer user_data)
ae567d
+{
ae567d
+  char sig_char;
ae567d
+  ssize_t readlen;
ae567d
+
ae567d
+  DBG(DBG_proc, "xsane_signal_handler_bottom_half\n");
ae567d
+
ae567d
+  while ((readlen = read(xsane_signal_pipe[0], &sig_char, 1)) != 0)
ae567d
+  {
ae567d
+    if (readlen < 0)
ae567d
+    {
ae567d
+      if (errno == EINTR)
ae567d
+      {
ae567d
+        /* if interrupted by signal, just repeat reading */
ae567d
+        continue;
ae567d
+      }
ae567d
+      else
ae567d
+      {
ae567d
+        break;
ae567d
+      }
ae567d
+    }
ae567d
+
ae567d
+    switch (sig_char)
ae567d
+    {
ae567d
+      case 't':
ae567d
+      case 'i':
ae567d
+      case 'h':
ae567d
+        xsane_quit();
ae567d
+        break;
ae567d
+      case 'c':
ae567d
+        xsane_sigchld_handler();
ae567d
+        break;
ae567d
+      default:
ae567d
+        DBG(DBG_error,
ae567d
+            "Don't know how to cope with character-encoded signal: '%c'\n",
ae567d
+            sig_char);
ae567d
+        break;
ae567d
+    }
ae567d
+  }
ae567d
+
ae567d
+  /* previous invocation might have read more than it should, so ignore
ae567d
+   * EAGAIN/EWOULDBLOCK */
ae567d
+  if (readlen < 0 && errno != EAGAIN && errno != EWOULDBLOCK)
ae567d
+  {
ae567d
+    DBG(DBG_error, "Error while reading from pipe: %d '%s'\n", errno,
ae567d
+        strerror(errno));
ae567d
+  }
ae567d
+
ae567d
+  return TRUE;
ae567d
 }
ae567d
 
ae567d
 /* ---------------------------------------------------------------------------------------------------------------------- */
ae567d
 
ae567d
-static RETSIGTYPE xsane_sigchld_handler(int signal)
ae567d
+static void xsane_sigchld_handler(void)
ae567d
 {
ae567d
  int status;
ae567d
  XsaneChildprocess **childprocess_listptr = &xsane.childprocess_list;
ae567d
@@ -6026,6 +6134,8 @@ void xsane_interface(int argc, char **argv)
ae567d
 {
ae567d
  struct SIGACTION act;
ae567d
 
ae567d
+  GIOChannel *gio_pipe_read;
ae567d
+
ae567d
   DBG(DBG_proc, "xsane_interface\n");
ae567d
 
ae567d
   xsane.info_label = NULL;
ae567d
@@ -6069,18 +6179,29 @@ void xsane_interface(int argc, char **argv)
ae567d
     }
ae567d
   }
ae567d
 
ae567d
+  if ((pipe(xsane_signal_pipe) == -1) ||
ae567d
+      (fcntl(xsane_signal_pipe[0], F_SETFD, FD_CLOEXEC) == -1) ||
ae567d
+      (fcntl(xsane_signal_pipe[0], F_SETFL, O_NONBLOCK) == -1) ||
ae567d
+      (fcntl(xsane_signal_pipe[1], F_SETFD, FD_CLOEXEC) == -1) ||
ae567d
+      (fcntl(xsane_signal_pipe[1], F_SETFL, O_NONBLOCK) == -1) ||
ae567d
+      !(gio_pipe_read = g_io_channel_unix_new(xsane_signal_pipe[0])) ||
ae567d
+      !g_io_add_watch(gio_pipe_read, G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_PRI,
ae567d
+          xsane_signal_handler_bottom_half, NULL))
ae567d
+  {
ae567d
+    DBG(DBG_error,
ae567d
+        "Couldn't create signal handling pipe, set flags on it or install\n"
ae567d
+        "bottom half of handler.\n");
ae567d
+    exit(1);
ae567d
+  }
ae567d
+
ae567d
   /* define SIGTERM, SIGINT, SIGHUP-handler to make sure that e.g. all temporary files are deleted */
ae567d
   /* when xsane gets such a signal */
ae567d
   memset(&act, 0, sizeof(act));
ae567d
-  act.sa_handler = xsane_quit_handler;
ae567d
-  sigaction(SIGTERM, &act, 0);
ae567d
-  sigaction(SIGINT,  &act, 0);
ae567d
-  sigaction(SIGHUP,  &act, 0);
ae567d
-
ae567d
-  /* add a signal handler that cleans up zombie child processes */
ae567d
-  memset(&act, 0, sizeof(act));
ae567d
-  act.sa_handler = xsane_sigchld_handler;
ae567d
-  sigaction(SIGCHLD, &act, 0);
ae567d
+  act.sa_handler = xsane_signal_handler_top_half;
ae567d
+  sigaction(SIGTERM, &act, NULL);
ae567d
+  sigaction(SIGINT,  &act, NULL);
ae567d
+  sigaction(SIGHUP,  &act, NULL);
ae567d
+  sigaction(SIGCHLD, &act, NULL);
ae567d
 
ae567d
   gtk_main();
ae567d
   sane_exit();
ae567d
-- 
ae567d
1.9.3
ae567d