Blame SOURCES/gdb-rhbz1473411-spawn-default-signal-handlers.patch

8f6b9e
commit 35fcb4fc81e51295d14125785765e0ea3e132cd9
8f6b9e
Author: Pedro Alves <palves@redhat.com>
8f6b9e
Date:   Tue Aug 9 20:21:08 2016 +0100
8f6b9e
8f6b9e
    Fix PR gdb/18653: gdb disturbs inferior's inherited signal dispositions
8f6b9e
    
8f6b9e
    gdb's (or gdbserver's) own signal handling should not interfere with
8f6b9e
    the signal dispositions their spawned children inherit.  However, it
8f6b9e
    currently does.  For example, some paths in gdb cause SIGPIPE to be
8f6b9e
    set to SIG_IGN, and as consequence, the child starts with SIGPIPE to
8f6b9e
    set to SIG_IGN too, even though gdb was started with SIGPIPE set to
8f6b9e
    SIG_DFL.
8f6b9e
    
8f6b9e
    This is because the exec family of functions does not reset the signal
8f6b9e
    disposition of signals that are set to SIG_IGN:
8f6b9e
    
8f6b9e
      http://pubs.opengroup.org/onlinepubs/7908799/xsh/execve.html
8f6b9e
    
8f6b9e
      Signals set to the default action (SIG_DFL) in the calling process
8f6b9e
      image are set to the default action in the new process
8f6b9e
      image. Signals set to be ignored (SIG_IGN) by the calling process
8f6b9e
      image are set to be ignored by the new process image. Signals set to
8f6b9e
      be caught by the calling process image are set to the default action
8f6b9e
      in the new process image (see <signal.h>).
8f6b9e
    
8f6b9e
    And neither does it reset signal masks or flags.
8f6b9e
    
8f6b9e
    In order to be transparent, when spawning new child processes to debug
8f6b9e
    (with "run", etc.), reset signal actions and mask back to what was
8f6b9e
    originally inherited from gdb/gdbserver's parent, just before execing
8f6b9e
    the target program to debug.
8f6b9e
    
8f6b9e
    gdb/ChangeLog:
8f6b9e
    2016-08-09  Pedro Alves  <palves@redhat.com>
8f6b9e
    
8f6b9e
            PR gdb/18653
8f6b9e
            * Makefile.in (SFILES): Add
8f6b9e
            common/signals-state-save-restore.c.
8f6b9e
            (HFILES_NO_SRCDIR): Add common/signals-state-save-restore.h.
8f6b9e
            (COMMON_OBS): Add signals-state-save-restore.o.
8f6b9e
            (signals-state-save-restore.o): New rule.
8f6b9e
            * configure: Regenerate.
8f6b9e
            * fork-child.c: Include "signals-state-save-restore.h".
8f6b9e
            (fork_inferior): Call restore_original_signals_state.
8f6b9e
            * main.c: Include "signals-state-save-restore.h".
8f6b9e
            (captured_main): Call save_original_signals_state.
8f6b9e
            * common/common.m4: Add sigaction to AC_CHECK_FUNCS checks.
8f6b9e
            * common/signals-state-save-restore.c: New file.
8f6b9e
            * common/signals-state-save-restore.h: New file.
8f6b9e
    
8f6b9e
    gdb/gdbserver/ChangeLog:
8f6b9e
    2016-08-09  Pedro Alves  <palves@redhat.com>
8f6b9e
    
8f6b9e
            PR gdb/18653
8f6b9e
            * Makefile.in (OBS): Add signals-state-save-restore.o.
8f6b9e
            (signals-state-save-restore.o): New rule.
8f6b9e
            * config.in: Regenerate.
8f6b9e
            * configure: Regenerate.
8f6b9e
            * linux-low.c: Include "signals-state-save-restore.h".
8f6b9e
            (linux_create_inferior): Call
8f6b9e
            restore_original_signals_state.
8f6b9e
            * server.c: Include "dispositions-save-restore.h".
8f6b9e
            (captured_main): Call save_original_signals_state.
8f6b9e
    
8f6b9e
    gdb/testsuite/ChangeLog:
8f6b9e
    2016-08-09  Pedro Alves  <palves@redhat.com>
8f6b9e
    
8f6b9e
            PR gdb/18653
8f6b9e
            * gdb.base/signals-state-child.c: New file.
8f6b9e
            * gdb.base/signals-state-child.exp: New file.
8f6b9e
            * gdb.gdb/selftest.exp (do_steps_and_nexts): Add new pattern.
8f6b9e
8f6b9e
### a/gdb/ChangeLog
8f6b9e
### b/gdb/ChangeLog
8f6b9e
## -1,5 +1,22 @@
8f6b9e
 2016-08-09  Pedro Alves  <palves@redhat.com>
8f6b9e
 
8f6b9e
+	PR gdb/18653
8f6b9e
+	* Makefile.in (SFILES): Add
8f6b9e
+	common/signals-state-save-restore.c.
8f6b9e
+	(HFILES_NO_SRCDIR): Add common/signals-state-save-restore.h.
8f6b9e
+	(COMMON_OBS): Add signals-state-save-restore.o.
8f6b9e
+	(signals-state-save-restore.o): New rule.
8f6b9e
+	* configure: Regenerate.
8f6b9e
+	* fork-child.c: Include "signals-state-save-restore.h".
8f6b9e
+	(fork_inferior): Call restore_original_signals_state.
8f6b9e
+	* main.c: Include "signals-state-save-restore.h".
8f6b9e
+	(captured_main): Call save_original_signals_state.
8f6b9e
+	* common/common.m4: Add sigaction to AC_CHECK_FUNCS checks.
8f6b9e
+	* common/signals-state-save-restore.c: New file.
8f6b9e
+	* common/signals-state-save-restore.h: New file.
8f6b9e
+
8f6b9e
+2016-08-09  Pedro Alves  <palves@redhat.com>
8f6b9e
+
8f6b9e
 	* value.c (unpack_value_bitfield): Skip unpacking if the parent
8f6b9e
 	has no contents buffer to begin with.
8f6b9e
 
8f6b9e
Index: gdb-7.6.1/gdb/Makefile.in
8f6b9e
===================================================================
8f6b9e
--- gdb-7.6.1.orig/gdb/Makefile.in	2017-11-03 19:22:10.364013263 +0100
8f6b9e
+++ gdb-7.6.1/gdb/Makefile.in	2017-11-03 19:22:11.452021206 +0100
8f6b9e
@@ -763,7 +763,8 @@
8f6b9e
 	regset.c sol-thread.c windows-termcap.c \
8f6b9e
 	common/gdb_vecs.c common/common-utils.c common/xml-utils.c \
8f6b9e
 	common/ptid.c common/buffer.c gdb-dlfcn.c common/agent.c \
8f6b9e
-	common/format.c btrace.c record-btrace.c
8f6b9e
+	common/format.c btrace.c record-btrace.c \
8f6b9e
+	common/signals-state-save-restore.c \
8f6b9e
 
8f6b9e
 LINTFILES = $(SFILES) $(YYFILES) $(CONFIG_SRCS) init.c
8f6b9e
 
8f6b9e
@@ -841,7 +842,8 @@
8f6b9e
 common/format.h common/host-defs.h utils.h common/queue.h common/gdb_string.h \
8f6b9e
 common/linux-osdata.h gdb-dlfcn.h auto-load.h probe.h stap-probe.h \
8f6b9e
 gdb_bfd.h sparc-ravenscar-thread.h ppc-ravenscar-thread.h common/linux-btrace.h \
8f6b9e
-nat/linux-namespaces.h
8f6b9e
+nat/linux-namespaces.h \
8f6b9e
+common/signals-state-save-restore.h
8f6b9e
 
8f6b9e
 # Header files that already have srcdir in them, or which are in objdir.
8f6b9e
 
8f6b9e
@@ -898,6 +900,7 @@
8f6b9e
 	memattr.o mem-break.o target.o parse.o language.o buildsym.o \
8f6b9e
 	findcmd.o \
8f6b9e
 	std-regs.o \
8f6b9e
+	signals-state-save-restore.o \
8f6b9e
 	signals.o \
8f6b9e
 	exec.o reverse.o \
8f6b9e
 	bcache.o objfiles.o observer.o minsyms.o maint.o demangle.o \
8f6b9e
@@ -2116,6 +2119,10 @@
8f6b9e
 	$(COMPILE) $(srcdir)/tui/tui-winsource.c
8f6b9e
 	$(POSTCOMPILE)
8f6b9e
 
8f6b9e
+signals-state-save-restore.o: $(srcdir)/common/signals-state-save-restore.c
8f6b9e
+	$(COMPILE) $(srcdir)/common/signals-state-save-restore.c
8f6b9e
+	$(POSTCOMPILE)
8f6b9e
+
8f6b9e
 #
8f6b9e
 # gdb/python/ dependencies
8f6b9e
 #
8f6b9e
Index: gdb-7.6.1/gdb/common/signals-state-save-restore.c
8f6b9e
===================================================================
8f6b9e
--- /dev/null	1970-01-01 00:00:00.000000000 +0000
8f6b9e
+++ gdb-7.6.1/gdb/common/signals-state-save-restore.c	2017-11-03 19:33:46.346074718 +0100
8f6b9e
@@ -0,0 +1,99 @@
8f6b9e
+/* Copyright (C) 2016 Free Software Foundation, Inc.
8f6b9e
+
8f6b9e
+   This file is part of GDB.
8f6b9e
+
8f6b9e
+   This program is free software; you can redistribute it and/or modify
8f6b9e
+   it under the terms of the GNU General Public License as published by
8f6b9e
+   the Free Software Foundation; either version 3 of the License, or
8f6b9e
+   (at your option) any later version.
8f6b9e
+
8f6b9e
+   This program is distributed in the hope that it will be useful,
8f6b9e
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
8f6b9e
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
8f6b9e
+   GNU General Public License for more details.
8f6b9e
+
8f6b9e
+   You should have received a copy of the GNU General Public License
8f6b9e
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
8f6b9e
+
8f6b9e
+#ifdef GDBSERVER
8f6b9e
+#include "server.h"
8f6b9e
+#else
8f6b9e
+#include "defs.h"
8f6b9e
+#endif
8f6b9e
+
8f6b9e
+#include "signals-state-save-restore.h"
8f6b9e
+
8f6b9e
+#include <signal.h>
8f6b9e
+
8f6b9e
+/* The original signal actions and mask.  */
8f6b9e
+
8f6b9e
+#ifdef HAVE_SIGACTION
8f6b9e
+static struct sigaction original_signal_actions[NSIG];
8f6b9e
+
8f6b9e
+/* Note that we use sigprocmask without worrying about threads because
8f6b9e
+   the save/restore functions are called either from main, or after a
8f6b9e
+   fork.  In both cases, we know the calling process is single
8f6b9e
+   threaded.  */
8f6b9e
+static sigset_t original_signal_mask;
8f6b9e
+#endif
8f6b9e
+
8f6b9e
+/* See signals-state-save-restore.h.   */
8f6b9e
+
8f6b9e
+void
8f6b9e
+save_original_signals_state (void)
8f6b9e
+{
8f6b9e
+#ifdef HAVE_SIGACTION
8f6b9e
+  int i;
8f6b9e
+  int res;
8f6b9e
+
8f6b9e
+  res = sigprocmask (0,  NULL, &original_signal_mask);
8f6b9e
+  if (res == -1)
8f6b9e
+    perror_with_name ("sigprocmask");
8f6b9e
+
8f6b9e
+  for (i = 1; i < NSIG; i++)
8f6b9e
+    {
8f6b9e
+      struct sigaction *oldact = &original_signal_actions[i];
8f6b9e
+
8f6b9e
+      res = sigaction (i, NULL, oldact);
8f6b9e
+      if (res == -1 && errno == EINVAL)
8f6b9e
+	{
8f6b9e
+	  /* Some signal numbers in the range are invalid.  */
8f6b9e
+	  continue;
8f6b9e
+	}
8f6b9e
+      else if (res == -1)
8f6b9e
+	perror_with_name ("sigaction");
8f6b9e
+
8f6b9e
+      /* If we find a custom signal handler already installed, then
8f6b9e
+	 this function was called too late.  */
8f6b9e
+      if (oldact->sa_handler != SIG_DFL && oldact->sa_handler != SIG_IGN)
8f6b9e
+	internal_error (__FILE__, __LINE__, _("unexpected signal handler"));
8f6b9e
+    }
8f6b9e
+#endif
8f6b9e
+}
8f6b9e
+
8f6b9e
+/* See signals-state-save-restore.h.   */
8f6b9e
+
8f6b9e
+void
8f6b9e
+restore_original_signals_state (void)
8f6b9e
+{
8f6b9e
+#ifdef HAVE_SIGACTION
8f6b9e
+  int i;
8f6b9e
+  int res;
8f6b9e
+
8f6b9e
+  for (i = 1; i < NSIG; i++)
8f6b9e
+    {
8f6b9e
+      res = sigaction (i, &original_signal_actions[i], NULL);
8f6b9e
+      if (res == -1 && errno == EINVAL)
8f6b9e
+	{
8f6b9e
+	  /* Some signal numbers in the range are invalid.  */
8f6b9e
+	  continue;
8f6b9e
+	}
8f6b9e
+      else if (res == -1)
8f6b9e
+	perror_with_name ("sigaction");
8f6b9e
+    }
8f6b9e
+
8f6b9e
+  res = sigprocmask (SIG_SETMASK,  &original_signal_mask, NULL);
8f6b9e
+  if (res == -1)
8f6b9e
+    perror_with_name ("sigprocmask");
8f6b9e
+#endif
8f6b9e
+}
8f6b9e
Index: gdb-7.6.1/gdb/common/signals-state-save-restore.h
8f6b9e
===================================================================
8f6b9e
--- /dev/null	1970-01-01 00:00:00.000000000 +0000
8f6b9e
+++ gdb-7.6.1/gdb/common/signals-state-save-restore.h	2017-11-03 19:22:11.453021213 +0100
8f6b9e
@@ -0,0 +1,39 @@
8f6b9e
+/* Copyright (C) 2016 Free Software Foundation, Inc.
8f6b9e
+
8f6b9e
+   This file is part of GDB.
8f6b9e
+
8f6b9e
+   This program is free software; you can redistribute it and/or modify
8f6b9e
+   it under the terms of the GNU General Public License as published by
8f6b9e
+   the Free Software Foundation; either version 3 of the License, or
8f6b9e
+   (at your option) any later version.
8f6b9e
+
8f6b9e
+   This program is distributed in the hope that it will be useful,
8f6b9e
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
8f6b9e
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
8f6b9e
+   GNU General Public License for more details.
8f6b9e
+
8f6b9e
+   You should have received a copy of the GNU General Public License
8f6b9e
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
8f6b9e
+
8f6b9e
+#ifndef COMMON_SIGNALS_STATE_SAVE_RESTORE_H
8f6b9e
+#define COMMON_SIGNALS_STATE_SAVE_RESTORE_H
8f6b9e
+
8f6b9e
+/* Save/restore the signal actions of all signals, and the signal
8f6b9e
+   mask.
8f6b9e
+
8f6b9e
+   Since the exec family of functions does not reset the signal
8f6b9e
+   disposition of signals set to SIG_IGN, nor does it reset the signal
8f6b9e
+   mask, in order to be transparent, when spawning new child processes
8f6b9e
+   to debug (with "run", etc.), we must reset signal actions and mask
8f6b9e
+   back to what was originally inherited from gdb/gdbserver's parent,
8f6b9e
+   just before execing the target program to debug.  */
8f6b9e
+
8f6b9e
+/* Save the signal state of all signals.  */
8f6b9e
+
8f6b9e
+extern void save_original_signals_state (void);
8f6b9e
+
8f6b9e
+/* Restore the signal state of all signals.  */
8f6b9e
+
8f6b9e
+extern void restore_original_signals_state (void);
8f6b9e
+
8f6b9e
+#endif /* COMMON_SIGNALS_STATE_SAVE_RESTORE_H */
8f6b9e
Index: gdb-7.6.1/gdb/configure
8f6b9e
===================================================================
8f6b9e
--- gdb-7.6.1.orig/gdb/configure	2017-11-03 19:22:08.181997334 +0100
8f6b9e
+++ gdb-7.6.1/gdb/configure	2017-11-03 19:22:11.455021228 +0100
8f6b9e
@@ -10667,7 +10667,7 @@
8f6b9e
 		sbrk setpgid setpgrp setsid \
8f6b9e
 		sigaction sigprocmask sigsetmask socketpair syscall \
8f6b9e
 		ttrace wborder wresize setlocale iconvlist libiconvlist btowc \
8f6b9e
-		setrlimit getrlimit posix_madvise waitpid lstat
8f6b9e
+		setrlimit getrlimit posix_madvise waitpid lstat sigaction
8f6b9e
 do :
8f6b9e
   as_ac_var=`$as_echo "ac_cv_func_$ac_func" | $as_tr_sh`
8f6b9e
 ac_fn_c_check_func "$LINENO" "$ac_func" "$as_ac_var"
8f6b9e
Index: gdb-7.6.1/gdb/fork-child.c
8f6b9e
===================================================================
8f6b9e
--- gdb-7.6.1.orig/gdb/fork-child.c	2013-01-01 07:32:42.000000000 +0100
8f6b9e
+++ gdb-7.6.1/gdb/fork-child.c	2017-11-03 19:22:11.455021228 +0100
8f6b9e
@@ -32,7 +32,7 @@
8f6b9e
 #include "command.h" /* for dont_repeat () */
8f6b9e
 #include "gdbcmd.h"
8f6b9e
 #include "solib.h"
8f6b9e
-
8f6b9e
+#include "signals-state-save-restore.h"
8f6b9e
 #include <signal.h>
8f6b9e
 
8f6b9e
 /* This just gets used as a default if we can't find SHELL.  */
8f6b9e
@@ -350,6 +350,8 @@
8f6b9e
         saying "not parent".  Sorry; you'll have to use print
8f6b9e
         statements!  */
8f6b9e
 
8f6b9e
+      restore_original_signals_state ();
8f6b9e
+
8f6b9e
       /* There is no execlpe call, so we have to set the environment
8f6b9e
          for our child in the global variable.  If we've vforked, this
8f6b9e
          clobbers the parent, but environ is restored a few lines down
8f6b9e
Index: gdb-7.6.1/gdb/gdbserver/Makefile.in
8f6b9e
===================================================================
8f6b9e
--- gdb-7.6.1.orig/gdb/gdbserver/Makefile.in	2017-11-03 19:22:10.364013263 +0100
8f6b9e
+++ gdb-7.6.1/gdb/gdbserver/Makefile.in	2017-11-03 19:22:11.455021228 +0100
8f6b9e
@@ -170,6 +170,7 @@
8f6b9e
 	mem-break.o hostio.o event-loop.o tracepoint.o \
8f6b9e
 	xml-utils.o common-utils.o ptid.o buffer.o format.o \
8f6b9e
 	dll.o notif.o \
8f6b9e
+	signals-state-save-restore.o \
8f6b9e
 	$(XML_BUILTIN) \
8f6b9e
 	$(DEPFILES) $(LIBOBJS)
8f6b9e
 GDBREPLAY_OBS = gdbreplay.o version.o
8f6b9e
@@ -572,6 +573,9 @@
8f6b9e
 linux-namespaces.o: ../nat/linux-namespaces.c
8f6b9e
 	$(COMPILE) $<
8f6b9e
 	$(POSTCOMPILE)
8f6b9e
+signals-state-save-restore.o: ../common/signals-state-save-restore.c
8f6b9e
+	$(COMPILE) $<
8f6b9e
+	$(POSTCOMPILE)
8f6b9e
 
8f6b9e
 win32_low_h = $(srcdir)/win32-low.h
8f6b9e
 
8f6b9e
Index: gdb-7.6.1/gdb/gdbserver/config.in
8f6b9e
===================================================================
8f6b9e
--- gdb-7.6.1.orig/gdb/gdbserver/config.in	2017-11-03 19:22:10.364013263 +0100
8f6b9e
+++ gdb-7.6.1/gdb/gdbserver/config.in	2017-11-03 19:22:11.455021228 +0100
8f6b9e
@@ -152,6 +152,9 @@
8f6b9e
 /* Define to 1 if you have the <sgtty.h> header file. */
8f6b9e
 #undef HAVE_SGTTY_H
8f6b9e
 
8f6b9e
+/* Define to 1 if you have the `sigaction' function. */
8f6b9e
+#undef HAVE_SIGACTION
8f6b9e
+
8f6b9e
 /* Define to 1 if you have the <signal.h> header file. */
8f6b9e
 #undef HAVE_SIGNAL_H
8f6b9e
 
8f6b9e
Index: gdb-7.6.1/gdb/gdbserver/configure
8f6b9e
===================================================================
8f6b9e
--- gdb-7.6.1.orig/gdb/gdbserver/configure	2017-11-03 19:22:10.366013278 +0100
8f6b9e
+++ gdb-7.6.1/gdb/gdbserver/configure	2017-11-03 19:22:11.456021235 +0100
8f6b9e
@@ -4796,7 +4796,7 @@
8f6b9e
 
8f6b9e
 done
8f6b9e
 
8f6b9e
-for ac_func in pread pwrite pread64 readlink setns
8f6b9e
+for ac_func in pread pwrite pread64 readlink setns sigaction
8f6b9e
 do :
8f6b9e
   as_ac_var=`$as_echo "ac_cv_func_$ac_func" | $as_tr_sh`
8f6b9e
 ac_fn_c_check_func "$LINENO" "$ac_func" "$as_ac_var"
8f6b9e
Index: gdb-7.6.1/gdb/gdbserver/linux-low.c
8f6b9e
===================================================================
8f6b9e
--- gdb-7.6.1.orig/gdb/gdbserver/linux-low.c	2017-11-03 19:22:10.380013380 +0100
8f6b9e
+++ gdb-7.6.1/gdb/gdbserver/linux-low.c	2017-11-03 19:22:11.457021243 +0100
8f6b9e
@@ -20,7 +20,7 @@
8f6b9e
 #include "linux-low.h"
8f6b9e
 #include "linux-osdata.h"
8f6b9e
 #include "agent.h"
8f6b9e
-
8f6b9e
+#include "signals-state-save-restore.h"
8f6b9e
 #include "gdb_wait.h"
8f6b9e
 #include <stdio.h>
8f6b9e
 #include <sys/param.h>
8f6b9e
@@ -699,6 +699,8 @@
8f6b9e
 	    close (remote_desc);
8f6b9e
 	}
8f6b9e
 
8f6b9e
+      restore_original_signals_state ();
8f6b9e
+
8f6b9e
       execv (program, allargs);
8f6b9e
       if (errno == ENOENT)
8f6b9e
 	execvp (program, allargs);
8f6b9e
Index: gdb-7.6.1/gdb/gdbserver/server.c
8f6b9e
===================================================================
8f6b9e
--- gdb-7.6.1.orig/gdb/gdbserver/server.c	2017-11-03 19:22:10.380013380 +0100
8f6b9e
+++ gdb-7.6.1/gdb/gdbserver/server.c	2017-11-03 19:22:11.457021243 +0100
8f6b9e
@@ -20,7 +20,7 @@
8f6b9e
 #include "gdbthread.h"
8f6b9e
 #include "agent.h"
8f6b9e
 #include "notif.h"
8f6b9e
-
8f6b9e
+#include "signals-state-save-restore.h"
8f6b9e
 #if HAVE_UNISTD_H
8f6b9e
 #include <unistd.h>
8f6b9e
 #endif
8f6b9e
@@ -2896,6 +2896,8 @@
8f6b9e
       exit (1);
8f6b9e
     }
8f6b9e
 
8f6b9e
+  save_original_signals_state ();
8f6b9e
+
8f6b9e
   /* We need to know whether the remote connection is stdio before
8f6b9e
      starting the inferior.  Inferiors created in this scenario have
8f6b9e
      stdin,stdout redirected.  So do this here before we call
8f6b9e
Index: gdb-7.6.1/gdb/main.c
8f6b9e
===================================================================
8f6b9e
--- gdb-7.6.1.orig/gdb/main.c	2017-11-03 19:22:10.319012935 +0100
8f6b9e
+++ gdb-7.6.1/gdb/main.c	2017-11-03 19:22:11.458021250 +0100
8f6b9e
@@ -45,6 +45,7 @@
8f6b9e
 #include "auto-load.h"
8f6b9e
 
8f6b9e
 #include "filenames.h"
8f6b9e
+#include "signals-state-save-restore.h"
8f6b9e
 
8f6b9e
 /* The selected interpreter.  This will be used as a set command
8f6b9e
    variable, so it should always be malloc'ed - since
8f6b9e
@@ -393,6 +394,7 @@
8f6b9e
   textdomain (PACKAGE);
8f6b9e
 
8f6b9e
   bfd_init ();
8f6b9e
+  save_original_signals_state ();
8f6b9e
 
8f6b9e
   make_cleanup (VEC_cleanup (cmdarg_s), &cmdarg_vec);
8f6b9e
   dirsize = 1;
8f6b9e
Index: gdb-7.6.1/gdb/testsuite/gdb.base/signals-state-child.c
8f6b9e
===================================================================
8f6b9e
--- /dev/null	1970-01-01 00:00:00.000000000 +0000
8f6b9e
+++ gdb-7.6.1/gdb/testsuite/gdb.base/signals-state-child.c	2017-11-03 19:22:11.458021250 +0100
8f6b9e
@@ -0,0 +1,101 @@
8f6b9e
+/* Copyright 2016 Free Software Foundation, Inc.
8f6b9e
+
8f6b9e
+   This file is part of GDB.
8f6b9e
+
8f6b9e
+   This program is free software; you can redistribute it and/or modify
8f6b9e
+   it under the terms of the GNU General Public License as published by
8f6b9e
+   the Free Software Foundation; either version 3 of the License, or
8f6b9e
+   (at your option) any later version.
8f6b9e
+
8f6b9e
+   This program is distributed in the hope that it will be useful,
8f6b9e
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
8f6b9e
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
8f6b9e
+   GNU General Public License for more details.
8f6b9e
+
8f6b9e
+   You should have received a copy of the GNU General Public License
8f6b9e
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
8f6b9e
+
8f6b9e
+#include <stdlib.h>
8f6b9e
+#include <stdio.h>
8f6b9e
+#include <signal.h>
8f6b9e
+#include <assert.h>
8f6b9e
+#include <errno.h>
8f6b9e
+
8f6b9e
+#ifndef OUTPUT_TXT
8f6b9e
+# define OUTPUT_TXT "output.txt"
8f6b9e
+#endif
8f6b9e
+
8f6b9e
+static void
8f6b9e
+perror_and_exit (const char *s)
8f6b9e
+{
8f6b9e
+  perror (s);
8f6b9e
+  exit (1);
8f6b9e
+}
8f6b9e
+
8f6b9e
+int
8f6b9e
+main (int argc, char **argv)
8f6b9e
+{
8f6b9e
+  int i;
8f6b9e
+  FILE *out;
8f6b9e
+  sigset_t sigset;
8f6b9e
+  int res;
8f6b9e
+
8f6b9e
+  res = sigprocmask (0,  NULL, &sigset);
8f6b9e
+  if (res != 0)
8f6b9e
+    perror_and_exit ("sigprocmask");
8f6b9e
+
8f6b9e
+  if (argc > 1)
8f6b9e
+    out = stdout;
8f6b9e
+  else
8f6b9e
+    {
8f6b9e
+      out = fopen (OUTPUT_TXT, "w");
8f6b9e
+      if (out == NULL)
8f6b9e
+	perror_and_exit ("fopen");
8f6b9e
+    }
8f6b9e
+
8f6b9e
+  for (i = 1; i < NSIG; i++)
8f6b9e
+    {
8f6b9e
+      struct sigaction oldact;
8f6b9e
+
8f6b9e
+      fprintf (out, "signal %d: ", i);
8f6b9e
+
8f6b9e
+      res = sigaction (i, NULL, &oldact);
8f6b9e
+      if (res == -1 && errno == EINVAL)
8f6b9e
+	{
8f6b9e
+	  /* Some signal numbers in the range are invalid.  E.g.,
8f6b9e
+	     signals 32 and 33 on GNU/Linux.  */
8f6b9e
+	  fprintf (out, "invalid");
8f6b9e
+	}
8f6b9e
+      else if (res == -1)
8f6b9e
+	{
8f6b9e
+	  perror_and_exit ("sigaction");
8f6b9e
+	}
8f6b9e
+      else
8f6b9e
+	{
8f6b9e
+	  int m;
8f6b9e
+
8f6b9e
+	  fprintf (out, "sigaction={sa_handler=", i);
8f6b9e
+
8f6b9e
+	  if (oldact.sa_handler == SIG_DFL)
8f6b9e
+	    fprintf (out, "SIG_DFL");
8f6b9e
+	  else if (oldact.sa_handler == SIG_IGN)
8f6b9e
+	    fprintf (out, "SIG_IGN");
8f6b9e
+	  else
8f6b9e
+	    abort ();
8f6b9e
+
8f6b9e
+	  fprintf (out, ", sa_mask=");
8f6b9e
+	  for (m = 1; m < NSIG; m++)
8f6b9e
+	    fprintf (out, "%c", sigismember (&oldact.sa_mask, m) ? '1' : '0');
8f6b9e
+
8f6b9e
+	  fprintf (out, ", sa_flags=%d", oldact.sa_flags);
8f6b9e
+
8f6b9e
+	  fprintf (out, "}, masked=%d", sigismember (&sigset, i));
8f6b9e
+	}
8f6b9e
+      fprintf (out, "\n");
8f6b9e
+    }
8f6b9e
+
8f6b9e
+  if (out != stdout)
8f6b9e
+    fclose (out);
8f6b9e
+
8f6b9e
+  return 0;
8f6b9e
+}
8f6b9e
Index: gdb-7.6.1/gdb/testsuite/gdb.base/signals-state-child.exp
8f6b9e
===================================================================
8f6b9e
--- /dev/null	1970-01-01 00:00:00.000000000 +0000
8f6b9e
+++ gdb-7.6.1/gdb/testsuite/gdb.base/signals-state-child.exp	2017-11-03 19:22:11.458021250 +0100
8f6b9e
@@ -0,0 +1,82 @@
8f6b9e
+# Copyright 2016 Free Software Foundation, Inc.
8f6b9e
+
8f6b9e
+# This program is free software; you can redistribute it and/or modify
8f6b9e
+# it under the terms of the GNU General Public License as published by
8f6b9e
+# the Free Software Foundation; either version 3 of the License, or
8f6b9e
+# (at your option) any later version.
8f6b9e
+#
8f6b9e
+# This program is distributed in the hope that it will be useful,
8f6b9e
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
8f6b9e
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
8f6b9e
+# GNU General Public License for more details.
8f6b9e
+#
8f6b9e
+# You should have received a copy of the GNU General Public License
8f6b9e
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
8f6b9e
+
8f6b9e
+# Test that gdb's (or gdbserver's) own signal handling does not
8f6b9e
+# interfere with the signal actions (dispositions, etc.) and mask
8f6b9e
+# their spawned children inherit.
8f6b9e
+#
8f6b9e
+# - If gdb inherits some signal set to SIG_IGN, so should the
8f6b9e
+#   inferior, even if gdb itself chooses not to ignore the signal.
8f6b9e
+#
8f6b9e
+# - If gdb inherits some signal set to SIG_DFL, so should the inferior
8f6b9e
+#   even if gdb itself ignores that signal.
8f6b9e
+#
8f6b9e
+# This requires special support in gdb/gdbserver because the exec
8f6b9e
+# family of functions does not reset the signal disposition of signals
8f6b9e
+# that are set to SIG_IGN, nor signal masks and flags.
8f6b9e
+
8f6b9e
+standard_testfile
8f6b9e
+
8f6b9e
+set gdb_txt [standard_output_file gdb.txt]
8f6b9e
+set standalone_txt [standard_output_file standalone.txt]
8f6b9e
+remote_exec host "rm -f $gdb_txt"
8f6b9e
+remote_exec host "rm -f $standalone_txt"
8f6b9e
+
8f6b9e
+set options [list debug "additional_flags=-DOUTPUT_TXT=\"$gdb_txt\""]
8f6b9e
+if {[build_executable $testfile.exp $testfile $srcfile $options]} {
8f6b9e
+    untested $testfile.exp
8f6b9e
+    return -1
8f6b9e
+}
8f6b9e
+
8f6b9e
+set options [list debug "additional_flags=-DOUTPUT_TXT=\"$standalone_txt\""]
8f6b9e
+if {[build_executable $testfile.exp $testfile-standalone $srcfile $options]} {
8f6b9e
+    untested $testfile.exp
8f6b9e
+    return -1
8f6b9e
+}
8f6b9e
+
8f6b9e
+# Run the program directly, and dump its initial signal actions and
8f6b9e
+# mask in "standalone.txt".
8f6b9e
+
8f6b9e
+# Use remote_spawn instead of remote_exec, like how we spawn gdb.
8f6b9e
+# This is in order to take the same code code paths in dejagnu
8f6b9e
+# compared to when running the program through gdb.  E.g., because
8f6b9e
+# local_exec uses -ignore SIGHUP, while remote_spawn does not, if we
8f6b9e
+# used remote_exec, the test program would start with SIGHUP ignored
8f6b9e
+# when run standalone, but not when run through gdb.
8f6b9e
+set res [remote_spawn host "$binfile-standalone"]
8f6b9e
+if { $res < 0 || $res == "" } {
8f6b9e
+    untested "spawning $binfile-standalone failed"
8f6b9e
+    return 1
8f6b9e
+} else {
8f6b9e
+    pass "collect standalone signals state"
8f6b9e
+}
8f6b9e
+remote_close host
8f6b9e
+
8f6b9e
+# Now run the program through gdb, and dump its initial signal actions
8f6b9e
+# and mask in "gdb.txt".
8f6b9e
+
8f6b9e
+clean_restart $binfile
8f6b9e
+
8f6b9e
+if { ! [ runto_main ] } then {
8f6b9e
+    untested $testfile.exp
8f6b9e
+    return -1
8f6b9e
+}
8f6b9e
+
8f6b9e
+gdb_continue_to_end "collect signals state under gdb"
8f6b9e
+
8f6b9e
+# Diff the .txt files.  They should be identical.
8f6b9e
+gdb_test "shell diff -s $standalone_txt $gdb_txt" \
8f6b9e
+    "Files .* are identical.*" \
8f6b9e
+    "signals states are identical"
8f6b9e
Index: gdb-7.6.1/gdb/testsuite/gdb.gdb/selftest.exp
8f6b9e
===================================================================
8f6b9e
--- gdb-7.6.1.orig/gdb/testsuite/gdb.gdb/selftest.exp	2017-11-03 19:22:06.904988012 +0100
8f6b9e
+++ gdb-7.6.1/gdb/testsuite/gdb.gdb/selftest.exp	2017-11-03 19:22:11.458021250 +0100
8f6b9e
@@ -158,6 +158,10 @@
8f6b9e
 		set description "next over bfd_init"
8f6b9e
 		set command "next"
8f6b9e
 	    }
8f6b9e
+	    -re ".*save_original_signals_state ..;.*$gdb_prompt $" {
8f6b9e
+		set description "next over save_original_signals_state"
8f6b9e
+		set command "next"
8f6b9e
+	    }
8f6b9e
 	    -re ".*VEC_cleanup .cmdarg_s.*$gdb_prompt $" {
8f6b9e
 		set description "next over cmdarg_s VEC_cleanup"
8f6b9e
 		set command "next"
8f6b9e
Index: gdb-7.6.1/gdb/configure.ac
8f6b9e
===================================================================
8f6b9e
--- gdb-7.6.1.orig/gdb/configure.ac	2017-11-03 19:22:08.164997210 +0100
8f6b9e
+++ gdb-7.6.1/gdb/configure.ac	2017-11-03 19:22:11.459021257 +0100
8f6b9e
@@ -1365,7 +1365,7 @@
8f6b9e
 		sbrk setpgid setpgrp setsid \
8f6b9e
 		sigaction sigprocmask sigsetmask socketpair syscall \
8f6b9e
 		ttrace wborder wresize setlocale iconvlist libiconvlist btowc \
8f6b9e
-		setrlimit getrlimit posix_madvise waitpid lstat])
8f6b9e
+		setrlimit getrlimit posix_madvise waitpid lstat sigaction])
8f6b9e
 AM_LANGINFO_CODESET
8f6b9e
 
8f6b9e
 # Check the return and argument types of ptrace.  No canned test for
8f6b9e
Index: gdb-7.6.1/gdb/gdbserver/configure.ac
8f6b9e
===================================================================
8f6b9e
--- gdb-7.6.1.orig/gdb/gdbserver/configure.ac	2017-11-03 19:22:10.366013278 +0100
8f6b9e
+++ gdb-7.6.1/gdb/gdbserver/configure.ac	2017-11-03 19:22:11.459021257 +0100
8f6b9e
@@ -70,7 +70,7 @@
8f6b9e
 		 sys/ioctl.h netinet/in.h sys/socket.h netdb.h dnl
8f6b9e
 		 netinet/tcp.h arpa/inet.h sys/wait.h wait.h sys/un.h dnl
8f6b9e
 		 linux/perf_event.h)
8f6b9e
-AC_CHECK_FUNCS(pread pwrite pread64 readlink setns)
8f6b9e
+AC_CHECK_FUNCS(pread pwrite pread64 readlink setns sigaction)
8f6b9e
 AC_REPLACE_FUNCS(vasprintf vsnprintf)
8f6b9e
 
8f6b9e
 # Check for UST