Blame SOURCES/gdb-rhbz1186918-gdbserver-in-container-4of8.patch

01917d
Introduce nat/linux-namespaces.[ch]
01917d
01917d
From: Gary Benson <gbenson@redhat.com>
01917d
01917d
This commit introduces new shared files nat/linux-namespaces.[ch]
01917d
containing code to support Linux namespaces that will be used by
01917d
both GDB and gdbserver.
01917d
01917d
gdb/ChangeLog:
01917d
01917d
	* configure.ac (AC_CHECK_FUNCS): Add setns.
01917d
	* config.in: Regenerate.
01917d
	* configure: Likewise.
01917d
	* nat/linux-namespaces.h: New file.
01917d
	* nat/linux-namespaces.c: Likewise.
01917d
	* Makefile.in (HFILES_NO_SRCDIR): Add nat/linux-namespaces.h.
01917d
	(linux-namespaces.o): New rule.
01917d
	* config/aarch64/linux.mh (NATDEPFILES): Add linux-namespaces.o.
01917d
	* config/alpha/alpha-linux.mh (NATDEPFILES): Likewise.
01917d
	* config/arm/linux.mh (NATDEPFILES): Likewise.
01917d
	* config/i386/linux.mh (NATDEPFILES): Likewise.
01917d
	* config/i386/linux64.mh (NATDEPFILES): Likewise.
01917d
	* config/ia64/linux.mh (NATDEPFILES): Likewise.
01917d
	* config/m32r/linux.mh (NATDEPFILES): Likewise.
01917d
	* config/m68k/linux.mh (NATDEPFILES): Likewise.
01917d
	* config/mips/linux.mh (NATDEPFILES): Likewise.
01917d
	* config/pa/linux.mh (NATDEPFILES): Likewise.
01917d
	* config/powerpc/linux.mh (NATDEPFILES): Likewise.
01917d
	* config/powerpc/ppc64-linux.mh (NATDEPFILES): Likewise.
01917d
	* config/powerpc/spu-linux.mh (NATDEPFILES): Likewise.
01917d
	* config/s390/linux.mh (NATDEPFILES): Likewise.
01917d
	* config/sparc/linux.mh (NATDEPFILES): Likewise.
01917d
	* config/sparc/linux64.mh (NATDEPFILES): Likewise.
01917d
	* config/tilegx/linux.mh (NATDEPFILES): Likewise.
01917d
	* config/xtensa/linux.mh (NATDEPFILES): Likewise.
01917d
01917d
gdb/gdbserver/ChangeLog:
01917d
01917d
	* configure.ac (AC_CHECK_FUNCS): Add setns.
01917d
	* config.in: Regenerate.
01917d
	* configure: Likewise.
01917d
	* Makefile.in (SFILES): Add nat/linux-namespaces.c.
01917d
	(linux-namespaces.o): New rule.
01917d
	* configure.srv (srv_linux_obj): Add linux-namespaces.o.
01917d
---
01917d
 gdb/Makefile.in             |    7 
01917d
 gdb/gdbserver/Makefile.in   |    6 
01917d
 gdb/gdbserver/config.in     |    3 
01917d
 gdb/gdbserver/configure     |    2 
01917d
 gdb/gdbserver/configure.ac  |    2 
01917d
 gdb/gdbserver/configure.srv |   38 +
01917d
 gdb/nat/linux-namespaces.c  | 1093 +++++++++++++++++++++++++++++++++++++++++++
01917d
 gdb/nat/linux-namespaces.h  |   78 +++
01917d
 8 files changed, 1206 insertions(+), 23 deletions(-)
01917d
 create mode 100644 gdb/nat/linux-namespaces.c
01917d
 create mode 100644 gdb/nat/linux-namespaces.h
01917d
01917d
Index: gdb-7.6.1/gdb/Makefile.in
01917d
===================================================================
120562
--- gdb-7.6.1.orig/gdb/Makefile.in	2018-05-18 10:55:27.630976775 +0200
120562
+++ gdb-7.6.1/gdb/Makefile.in	2018-05-18 10:55:28.422983192 +0200
01917d
@@ -840,7 +840,8 @@
01917d
 common/common-utils.h common/xml-utils.h common/buffer.h common/ptid.h \
01917d
 common/format.h common/host-defs.h utils.h common/queue.h common/gdb_string.h \
01917d
 common/linux-osdata.h gdb-dlfcn.h auto-load.h probe.h stap-probe.h \
01917d
-gdb_bfd.h sparc-ravenscar-thread.h ppc-ravenscar-thread.h common/linux-btrace.h
01917d
+gdb_bfd.h sparc-ravenscar-thread.h ppc-ravenscar-thread.h common/linux-btrace.h \
01917d
+nat/linux-namespaces.h
01917d
 
01917d
 # Header files that already have srcdir in them, or which are in objdir.
01917d
 
01917d
@@ -2037,6 +2038,10 @@
01917d
 	$(COMPILE) $(srcdir)/common/linux-btrace.c
01917d
 	$(POSTCOMPILE)
01917d
 
01917d
+linux-namespaces.o: ${srcdir}/nat/linux-namespaces.c
01917d
+	$(COMPILE) $(srcdir)/nat/linux-namespaces.c
01917d
+	$(POSTCOMPILE)
01917d
+
01917d
 #
01917d
 # gdb/tui/ dependencies
01917d
 #
01917d
Index: gdb-7.6.1/gdb/gdbserver/Makefile.in
01917d
===================================================================
120562
--- gdb-7.6.1.orig/gdb/gdbserver/Makefile.in	2018-05-18 10:55:28.251981806 +0200
120562
+++ gdb-7.6.1/gdb/gdbserver/Makefile.in	2018-05-18 10:55:28.422983192 +0200
01917d
@@ -155,7 +155,8 @@
01917d
 	$(srcdir)/common/vec.c $(srcdir)/common/gdb_vecs.c \
01917d
 	$(srcdir)/common/common-utils.c $(srcdir)/common/xml-utils.c \
01917d
 	$(srcdir)/common/linux-osdata.c $(srcdir)/common/ptid.c \
01917d
-	$(srcdir)/common/buffer.c $(srcdir)/common/linux-btrace.c
01917d
+	$(srcdir)/common/buffer.c $(srcdir)/common/linux-btrace.c \
01917d
+	$(srcdir)/nat/linux-namespaces.c
01917d
 
01917d
 DEPFILES = @GDBSERVER_DEPFILES@
01917d
 
01917d
@@ -568,6 +569,9 @@
01917d
 linux-low.o: linux-low.c
01917d
 	$(COMPILE) $< @USE_THREAD_DB@
01917d
 	$(POSTCOMPILE)
01917d
+linux-namespaces.o: ../nat/linux-namespaces.c
01917d
+	$(COMPILE) $<
01917d
+	$(POSTCOMPILE)
01917d
 
01917d
 win32_low_h = $(srcdir)/win32-low.h
01917d
 
01917d
Index: gdb-7.6.1/gdb/gdbserver/config.in
01917d
===================================================================
120562
--- gdb-7.6.1.orig/gdb/gdbserver/config.in	2018-05-18 10:55:26.840970375 +0200
120562
+++ gdb-7.6.1/gdb/gdbserver/config.in	2018-05-18 11:00:34.000000000 +0200
120562
@@ -146,12 +146,18 @@
01917d
 /* Define to 1 if you have the <selinux/selinux.h> header file. */
01917d
 #undef HAVE_SELINUX_SELINUX_H
01917d
 
01917d
+/* Define to 1 if you have the `setns' function. */
01917d
+#undef HAVE_SETNS
01917d
+
01917d
 /* Define to 1 if you have the <sgtty.h> header file. */
01917d
 #undef HAVE_SGTTY_H
01917d
 
120562
 /* Define to 1 if you have the <signal.h> header file. */
120562
 #undef HAVE_SIGNAL_H
120562
 
120562
+/* Define to 1 if you have the `socketpair' function. */
120562
+#undef HAVE_SOCKETPAIR
120562
+
120562
 /* Define to 1 if the system has the type `socklen_t'. */
120562
 #undef HAVE_SOCKLEN_T
120562
 
01917d
Index: gdb-7.6.1/gdb/gdbserver/configure
01917d
===================================================================
120562
--- gdb-7.6.1.orig/gdb/gdbserver/configure	2018-05-18 10:55:26.841970383 +0200
120562
+++ gdb-7.6.1/gdb/gdbserver/configure	2018-05-18 11:00:35.421470381 +0200
01917d
@@ -4796,7 +4796,7 @@
01917d
 
01917d
 done
01917d
 
01917d
-for ac_func in pread pwrite pread64 readlink
120562
+for ac_func in pread pwrite pread64 readlink socketpair setns
01917d
 do :
01917d
   as_ac_var=`$as_echo "ac_cv_func_$ac_func" | $as_tr_sh`
01917d
 ac_fn_c_check_func "$LINENO" "$ac_func" "$as_ac_var"
01917d
Index: gdb-7.6.1/gdb/gdbserver/configure.ac
01917d
===================================================================
120562
--- gdb-7.6.1.orig/gdb/gdbserver/configure.ac	2018-05-18 10:55:26.828970278 +0200
120562
+++ gdb-7.6.1/gdb/gdbserver/configure.ac	2018-05-18 11:00:13.703294429 +0200
01917d
@@ -70,7 +70,7 @@
01917d
 		 sys/ioctl.h netinet/in.h sys/socket.h netdb.h dnl
01917d
 		 netinet/tcp.h arpa/inet.h sys/wait.h wait.h sys/un.h dnl
01917d
 		 linux/perf_event.h)
01917d
-AC_CHECK_FUNCS(pread pwrite pread64 readlink)
120562
+AC_CHECK_FUNCS(pread pwrite pread64 readlink socketpair setns)
01917d
 AC_REPLACE_FUNCS(vasprintf vsnprintf)
01917d
 
01917d
 # Check for UST
01917d
Index: gdb-7.6.1/gdb/gdbserver/configure.srv
01917d
===================================================================
120562
--- gdb-7.6.1.orig/gdb/gdbserver/configure.srv	2018-05-18 10:55:28.252981814 +0200
120562
+++ gdb-7.6.1/gdb/gdbserver/configure.srv	2018-05-18 10:55:28.424983208 +0200
01917d
@@ -45,7 +45,7 @@
01917d
   aarch64*-*-linux*)
01917d
 			srv_regobj="aarch64.o aarch64-without-fpu.o"
01917d
 			srv_tgtobj="linux-aarch64-low.o"
01917d
-			srv_tgtobj="${srv_tgtobj} linux-low.o"
01917d
+			srv_tgtobj="${srv_tgtobj} linux-low.o linux-namespaces.o"
01917d
 			srv_tgtobj="${srv_tgtobj} linux-osdata.o"
01917d
 			srv_tgtobj="${srv_tgtobj} linux-procfs.o"
01917d
 			srv_tgtobj="${srv_tgtobj} linux-ptrace.o"
01917d
@@ -61,7 +61,7 @@
01917d
 			srv_regobj="${srv_regobj} arm-with-vfpv2.o"
01917d
 			srv_regobj="${srv_regobj} arm-with-vfpv3.o"
01917d
 			srv_regobj="${srv_regobj} arm-with-neon.o"
01917d
-			srv_tgtobj="linux-low.o linux-osdata.o linux-arm-low.o linux-procfs.o"
01917d
+			srv_tgtobj="linux-low.o linux-namespaces.o linux-osdata.o linux-arm-low.o linux-procfs.o"
01917d
 			srv_tgtobj="${srv_tgtobj} linux-ptrace.o"
01917d
 			srv_xmlfiles="arm-with-iwmmxt.xml"
01917d
 			srv_xmlfiles="${srv_xmlfiles} arm-with-vfpv2.xml"
01917d
@@ -84,19 +84,19 @@
01917d
 			srv_mingwce=yes
01917d
 			;;
01917d
   bfin-*-*linux*)	srv_regobj=reg-bfin.o
01917d
-			srv_tgtobj="linux-low.o linux-osdata.o linux-bfin-low.o linux-procfs.o"
01917d
+			srv_tgtobj="linux-low.o linux-namespaces.o linux-osdata.o linux-bfin-low.o linux-procfs.o"
01917d
 			srv_tgtobj="${srv_tgtobj} linux-ptrace.o"
01917d
 			srv_linux_usrregs=yes
01917d
 			srv_linux_thread_db=yes
01917d
 			;;
01917d
   crisv32-*-linux*)	srv_regobj=reg-crisv32.o
01917d
-			srv_tgtobj="linux-low.o linux-osdata.o linux-crisv32-low.o linux-procfs.o"
01917d
+			srv_tgtobj="linux-low.o linux-namespaces.o linux-osdata.o linux-crisv32-low.o linux-procfs.o"
01917d
 			srv_tgtobj="${srv_tgtobj} linux-ptrace.o"
01917d
 			srv_linux_regsets=yes
01917d
 			srv_linux_thread_db=yes
01917d
 			;;
01917d
   cris-*-linux*)	srv_regobj=reg-cris.o
01917d
-			srv_tgtobj="linux-low.o linux-osdata.o linux-cris-low.o linux-procfs.o"
01917d
+			srv_tgtobj="linux-low.o linux-namespaces.o linux-osdata.o linux-cris-low.o linux-procfs.o"
01917d
 			srv_tgtobj="${srv_tgtobj} linux-ptrace.o"
01917d
 			srv_linux_usrregs=yes
01917d
 			srv_linux_thread_db=yes
01917d
@@ -111,7 +111,7 @@
01917d
 			    srv_regobj="$srv_regobj $srv_amd64_linux_regobj"
01917d
 			    srv_xmlfiles="${srv_xmlfiles} $srv_amd64_linux_xmlfiles"
01917d
 			fi
01917d
-			srv_tgtobj="linux-low.o linux-osdata.o linux-x86-low.o i386-low.o i387-fp.o linux-procfs.o"
01917d
+			srv_tgtobj="linux-low.o linux-namespaces.o linux-osdata.o linux-x86-low.o i386-low.o i387-fp.o linux-procfs.o"
01917d
 			srv_tgtobj="${srv_tgtobj} linux-ptrace.o linux-btrace.o"
01917d
 			srv_linux_usrregs=yes
01917d
 			srv_linux_regsets=yes
01917d
@@ -147,12 +147,12 @@
01917d
 			srv_qnx="yes"
01917d
 			;;
01917d
   ia64-*-linux*)	srv_regobj=reg-ia64.o
01917d
-			srv_tgtobj="linux-low.o linux-osdata.o linux-ia64-low.o linux-procfs.o"
01917d
+			srv_tgtobj="linux-low.o linux-namespaces.o linux-osdata.o linux-ia64-low.o linux-procfs.o"
01917d
 			srv_tgtobj="${srv_tgtobj} linux-ptrace.o"
01917d
 			srv_linux_usrregs=yes
01917d
 			;;
01917d
   m32r*-*-linux*)	srv_regobj=reg-m32r.o
01917d
-			srv_tgtobj="linux-low.o linux-osdata.o linux-m32r-low.o linux-procfs.o"
01917d
+			srv_tgtobj="linux-low.o linux-namespaces.o linux-osdata.o linux-m32r-low.o linux-procfs.o"
01917d
 			srv_tgtobj="${srv_tgtobj} linux-ptrace.o"
01917d
 			srv_linux_usrregs=yes
01917d
  			srv_linux_thread_db=yes
01917d
@@ -162,7 +162,7 @@
01917d
                         else
01917d
                           srv_regobj=reg-m68k.o
01917d
                         fi
01917d
-			srv_tgtobj="linux-low.o linux-osdata.o linux-m68k-low.o linux-procfs.o"
01917d
+			srv_tgtobj="linux-low.o linux-namespaces.o linux-osdata.o linux-m68k-low.o linux-procfs.o"
01917d
 			srv_tgtobj="${srv_tgtobj} linux-ptrace.o"
01917d
 			srv_linux_usrregs=yes
01917d
 			srv_linux_regsets=yes
01917d
@@ -173,7 +173,7 @@
01917d
                         else
01917d
                           srv_regobj=reg-m68k.o
01917d
                         fi
01917d
-			srv_tgtobj="linux-low.o linux-osdata.o linux-m68k-low.o linux-procfs.o"
01917d
+			srv_tgtobj="linux-low.o linux-namespaces.o linux-osdata.o linux-m68k-low.o linux-procfs.o"
01917d
 			srv_tgtobj="${srv_tgtobj} linux-ptrace.o"
01917d
 			srv_linux_usrregs=yes
01917d
 			srv_linux_regsets=yes
01917d
@@ -183,7 +183,7 @@
01917d
 			srv_regobj="${srv_regobj} mips-dsp-linux.o"
01917d
 			srv_regobj="${srv_regobj} mips64-linux.o"
01917d
 			srv_regobj="${srv_regobj} mips64-dsp-linux.o"
01917d
-			srv_tgtobj="linux-low.o linux-osdata.o linux-mips-low.o linux-procfs.o"
01917d
+			srv_tgtobj="linux-low.o linux-namespaces.o linux-osdata.o linux-mips-low.o linux-procfs.o"
01917d
 			srv_tgtobj="${srv_tgtobj} linux-ptrace.o"
01917d
 			srv_xmlfiles="mips-linux.xml"
01917d
 			srv_xmlfiles="${srv_xmlfiles} mips-dsp-linux.xml"
01917d
@@ -216,7 +216,7 @@
01917d
 			srv_regobj="${srv_regobj} powerpc-isa205-64l.o"
01917d
 			srv_regobj="${srv_regobj} powerpc-isa205-altivec64l.o"
01917d
 			srv_regobj="${srv_regobj} powerpc-isa205-vsx64l.o"
01917d
-			srv_tgtobj="linux-low.o linux-osdata.o linux-ppc-low.o linux-procfs.o"
01917d
+			srv_tgtobj="linux-low.o linux-namespaces.o linux-osdata.o linux-ppc-low.o linux-procfs.o"
01917d
 			srv_tgtobj="${srv_tgtobj} linux-ptrace.o"
01917d
 			srv_xmlfiles="rs6000/powerpc-32l.xml"
01917d
 			srv_xmlfiles="${srv_xmlfiles} rs6000/powerpc-altivec32l.xml"
01917d
@@ -268,7 +268,7 @@
01917d
 			srv_regobj="${srv_regobj} s390x-te-linux64.o"
01917d
 			srv_regobj="${srv_regobj} s390x-vx-linux64.o"
01917d
 			srv_regobj="${srv_regobj} s390x-tevx-linux64.o"
01917d
-			srv_tgtobj="linux-low.o linux-osdata.o linux-s390-low.o linux-procfs.o"
01917d
+			srv_tgtobj="linux-low.o linux-namespaces.o linux-osdata.o linux-s390-low.o linux-procfs.o"
01917d
 			srv_tgtobj="${srv_tgtobj} linux-ptrace.o"
01917d
 			srv_xmlfiles="s390-linux32.xml"
01917d
 			srv_xmlfiles="${srv_xmlfiles} s390-linux32v1.xml"
01917d
@@ -297,14 +297,14 @@
01917d
 			srv_linux_thread_db=yes
01917d
 			;;
01917d
   sh*-*-linux*)		srv_regobj=reg-sh.o
01917d
-			srv_tgtobj="linux-low.o linux-osdata.o linux-sh-low.o linux-procfs.o"
01917d
+			srv_tgtobj="linux-low.o linux-namespaces.o linux-osdata.o linux-sh-low.o linux-procfs.o"
01917d
 			srv_tgtobj="${srv_tgtobj} linux-ptrace.o"
01917d
 			srv_linux_usrregs=yes
01917d
 			srv_linux_regsets=yes
01917d
 			srv_linux_thread_db=yes
01917d
 			;;
01917d
   sparc*-*-linux*)	srv_regobj=reg-sparc64.o
01917d
-			srv_tgtobj="linux-low.o linux-osdata.o linux-sparc-low.o linux-procfs.o"
01917d
+			srv_tgtobj="linux-low.o linux-namespaces.o linux-osdata.o linux-sparc-low.o linux-procfs.o"
01917d
 			srv_tgtobj="${srv_tgtobj} linux-ptrace.o"
01917d
 			srv_linux_regsets=yes
01917d
 			srv_linux_thread_db=yes
01917d
@@ -321,14 +321,14 @@
01917d
 			srv_xmlfiles="${srv_xmlfiles} tic6x-core.xml"
01917d
 			srv_xmlfiles="${srv_xmlfiles} tic6x-gp.xml"
01917d
 			srv_xmlfiles="${srv_xmlfiles} tic6x-c6xp.xml"
01917d
-  			srv_tgtobj="linux-low.o linux-osdata.o linux-tic6x-low.o linux-procfs.o"
01917d
+  			srv_tgtobj="linux-low.o linux-namespaces.o linux-osdata.o linux-tic6x-low.o linux-procfs.o"
01917d
 			srv_tgtobj="${srv_tgtobj} linux-ptrace.o"
01917d
 			srv_linux_regsets=yes
01917d
 			srv_linux_usrregs=yes
01917d
 			srv_linux_thread_db=yes
01917d
 			;;
01917d
   x86_64-*-linux*)	srv_regobj="$srv_amd64_linux_regobj $srv_i386_linux_regobj"
01917d
-			srv_tgtobj="linux-low.o linux-osdata.o linux-x86-low.o i386-low.o i387-fp.o linux-procfs.o"
01917d
+			srv_tgtobj="linux-low.o linux-namespaces.o linux-osdata.o linux-x86-low.o i386-low.o i387-fp.o linux-procfs.o"
01917d
 			srv_tgtobj="${srv_tgtobj} linux-ptrace.o linux-btrace.o"
01917d
 			srv_xmlfiles="$srv_i386_linux_xmlfiles $srv_amd64_linux_xmlfiles"
01917d
 			srv_linux_usrregs=yes # This is for i386 progs.
01917d
@@ -348,13 +348,13 @@
01917d
 			;;
01917d
 
01917d
   xtensa*-*-linux*)	srv_regobj=reg-xtensa.o
01917d
-			srv_tgtobj="linux-low.o linux-osdata.o linux-xtensa-low.o linux-procfs.o"
01917d
+			srv_tgtobj="linux-low.o linux-namespaces.o linux-osdata.o linux-xtensa-low.o linux-procfs.o"
01917d
 			srv_tgtobj="${srv_tgtobj} linux-ptrace.o"
01917d
 			srv_linux_regsets=yes
01917d
 			;;
01917d
   tilegx-*-linux*)	srv_regobj=reg-tilegx.o
01917d
 			srv_regobj="${srv_regobj} reg-tilegx32.o"
01917d
-			srv_tgtobj="linux-low.o linux-tile-low.o linux-osdata.o linux-procfs.o"
01917d
+			srv_tgtobj="linux-low.o linux-namespaces.o linux-tile-low.o linux-osdata.o linux-procfs.o"
01917d
 			srv_tgtobj="${srv_tgtobj} linux-ptrace.o"
01917d
 			srv_linux_regsets=yes
01917d
 			srv_linux_thread_db=yes
01917d
Index: gdb-7.6.1/gdb/nat/linux-namespaces.c
01917d
===================================================================
01917d
--- /dev/null	1970-01-01 00:00:00.000000000 +0000
120562
+++ gdb-7.6.1/gdb/nat/linux-namespaces.c	2018-05-18 10:55:28.424983208 +0200
01917d
@@ -0,0 +1,1093 @@
01917d
+/* Linux namespaces(7) support.
01917d
+
01917d
+   Copyright (C) 2015 Free Software Foundation, Inc.
01917d
+
01917d
+   This file is part of GDB.
01917d
+
01917d
+   This program is free software; you can redistribute it and/or modify
01917d
+   it under the terms of the GNU General Public License as published by
01917d
+   the Free Software Foundation; either version 3 of the License, or
01917d
+   (at your option) any later version.
01917d
+
01917d
+   This program is distributed in the hope that it will be useful,
01917d
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
01917d
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
01917d
+   GNU General Public License for more details.
01917d
+
01917d
+   You should have received a copy of the GNU General Public License
01917d
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
01917d
+
01917d
+#include "server.h"
01917d
+//#include "common-defs.h"
01917d
+#include "../nat/linux-namespaces.h"
01917d
+//#include "filestuff.h"
01917d
+#include <limits.h>
01917d
+#define debug_printf(x...) do { } while (0)
01917d
+#define internal_warning(file, line, x...) warning(x)
01917d
+#include <unistd.h>
01917d
+#include <fcntl.h>
01917d
+#include <sys/syscall.h>
01917d
+#include <sys/types.h>
01917d
+#include <sys/stat.h>
01917d
+#include <sys/socket.h>
01917d
+#include <sys/wait.h>
01917d
+#include <signal.h>
01917d
+#include <sched.h>
01917d
+
01917d
+/* See nat/linux-namespaces.h.  */
01917d
+int debug_linux_namespaces;
01917d
+
01917d
+/* Handle systems without setns.  */
01917d
+
01917d
+#ifndef HAVE_SETNS
01917d
+static int
01917d
+setns (int fd, int nstype)
01917d
+{
01917d
+#ifdef __NR_setns
01917d
+  return syscall (__NR_setns, fd, nstype);
01917d
+#else
01917d
+  errno = ENOSYS;
01917d
+  return -1;
01917d
+#endif
01917d
+}
01917d
+#endif
01917d
+
01917d
+/* A Linux namespace.  */
01917d
+
01917d
+struct linux_ns
01917d
+{
01917d
+  /* Filename of this namespace's entries in /proc/PID/ns.  */
01917d
+  const char *filename;
01917d
+
01917d
+  /* Nonzero if this object has been initialized.  */
01917d
+  int initialized;
01917d
+
01917d
+  /* Nonzero if this namespace is supported on this system.  */
01917d
+  int supported;
01917d
+
01917d
+  /* ID of the namespace the calling process is in, used to
01917d
+     see if other processes share the namespace.  The code in
01917d
+     this file assumes that the calling process never changes
01917d
+     namespace.  */
01917d
+  ino_t id;
01917d
+};
01917d
+
01917d
+/* Return the absolute filename of process PID's /proc/PID/ns
01917d
+   entry for namespace NS.  The returned value persists until
01917d
+   this function is next called.  */
01917d
+
01917d
+static const char *
01917d
+linux_ns_filename (struct linux_ns *ns, int pid)
01917d
+{
01917d
+  static char filename[PATH_MAX];
01917d
+
01917d
+  gdb_assert (pid > 0);
01917d
+  xsnprintf (filename, sizeof (filename), "/proc/%d/ns/%s", pid,
01917d
+	     ns->filename);
01917d
+
01917d
+  return filename;
01917d
+}
01917d
+
01917d
+/* Return a representation of the caller's TYPE namespace, or
01917d
+   NULL if TYPE namespaces are not supported on this system.  */
01917d
+
01917d
+static struct linux_ns *
01917d
+linux_ns_get_namespace (enum linux_ns_type type)
01917d
+{
01917d
+  static struct linux_ns namespaces[NUM_LINUX_NS_TYPES] =
01917d
+    {
01917d
+      { "ipc" },
01917d
+      { "mnt" },
01917d
+      { "net" },
01917d
+      { "pid" },
01917d
+      { "user" },
01917d
+      { "uts" },
01917d
+    };
01917d
+  struct linux_ns *ns;
01917d
+
01917d
+  gdb_assert (type >= 0 && type < NUM_LINUX_NS_TYPES);
01917d
+  ns = &namespaces[type];
01917d
+
01917d
+  if (!ns->initialized)
01917d
+    {
01917d
+      struct stat sb;
01917d
+
01917d
+      if (stat (linux_ns_filename (ns, getpid ()), &sb) == 0)
01917d
+	{
01917d
+	  ns->id = sb.st_ino;
01917d
+
01917d
+	  ns->supported = 1;
01917d
+	}
01917d
+
01917d
+      ns->initialized = 1;
01917d
+    }
01917d
+
01917d
+  return ns->supported ? ns : NULL;
01917d
+}
01917d
+
01917d
+/* See nat/linux-namespaces.h.  */
01917d
+
01917d
+int
01917d
+linux_ns_same (pid_t pid, enum linux_ns_type type)
01917d
+{
01917d
+  struct linux_ns *ns = linux_ns_get_namespace (type);
01917d
+  const char *filename;
01917d
+  struct stat sb;
01917d
+
01917d
+  /* If the kernel does not support TYPE namespaces then there's
01917d
+     effectively only one TYPE namespace that all processes on
01917d
+     the system share.  */
01917d
+  if (ns == NULL)
01917d
+    return 1;
01917d
+
01917d
+  /* Stat PID's TYPE namespace entry to get the namespace ID.  This
01917d
+     might fail if the process died, or if we don't have the right
01917d
+     permissions (though we should be attached by this time so this
01917d
+     seems unlikely).  In any event, we can't make any decisions and
01917d
+     must throw.  */
01917d
+  filename = linux_ns_filename (ns, pid);
01917d
+  if (stat (filename, &sb) != 0)
01917d
+    perror_with_name (filename);
01917d
+
01917d
+  return sb.st_ino == ns->id;
01917d
+}
01917d
+
01917d
+/* We need to use setns(2) to handle filesystem access in mount
01917d
+   namespaces other than our own, but this isn't permitted for
01917d
+   multithreaded processes.  GDB is multithreaded when compiled
01917d
+   with Guile support, and may become multithreaded if compiled
01917d
+   with Python support.  We deal with this by spawning a single-
01917d
+   threaded helper process to access mount namespaces other than
01917d
+   our own.
01917d
+
01917d
+   The helper process is started the first time a call to setns
01917d
+   is required.  The main process (GDB or gdbserver) communicates
01917d
+   with the helper via sockets, passing file descriptors where
01917d
+   necessary using SCM_RIGHTS.  Once started the helper process
01917d
+   runs until the main process terminates; when this happens the
01917d
+   helper will receive socket errors, notice that its parent died,
01917d
+   and exit accordingly (see mnsh_maybe_mourn_peer).
01917d
+
01917d
+   The protocol is that the main process sends a request in a
01917d
+   single message, and the helper replies to every message it
01917d
+   receives with a single-message response.  If the helper
01917d
+   receives a message it does not understand it will reply with
01917d
+   a MNSH_MSG_ERROR message.  The main process checks all
01917d
+   responses it receives with gdb_assert, so if the main process
01917d
+   receives something unexpected (which includes MNSH_MSG_ERROR)
01917d
+   the main process will call internal_error.
01917d
+
01917d
+   For avoidance of doubt, if the helper process receives a
01917d
+   message it doesn't handle it will reply with MNSH_MSG_ERROR.
01917d
+   If the main process receives MNSH_MSG_ERROR at any time then
01917d
+   it will call internal_error.  If internal_error causes the
01917d
+   main process to exit, the helper will notice this and also
01917d
+   exit.  The helper will not exit until the main process
01917d
+   terminates, so if the user continues through internal_error
01917d
+   the helper will still be there awaiting requests from the
01917d
+   main process.
01917d
+
01917d
+   Messages in both directions have the following payload:
01917d
+
01917d
+   - TYPE (enum mnsh_msg_type, always sent) - the message type.
01917d
+   - INT1 and
01917d
+   - INT2 (int, always sent, though not always used) - two
01917d
+           values whose meaning is message-type-dependent.
01917d
+	   See enum mnsh_msg_type documentation below.
01917d
+   - FD (int, optional, sent using SCM_RIGHTS) - an open file
01917d
+         descriptor.
01917d
+   - BUF (unstructured data, optional) - some data with message-
01917d
+          type-dependent meaning.
01917d
+
01917d
+   Note that the helper process is the child of a call to fork,
01917d
+   so all code in the helper must be async-signal-safe.  */
01917d
+
01917d
+/* Mount namespace helper message types.  */
01917d
+
01917d
+enum mnsh_msg_type
01917d
+  {
01917d
+    /* A communication error occurred.  Receipt of this message
01917d
+       by either end will cause an assertion failure in the main
01917d
+       process.  */
01917d
+    MNSH_MSG_ERROR,
01917d
+
01917d
+    /* Requests, sent from the main process to the helper.  */
01917d
+
01917d
+    /* A request that the helper call setns.  Arguments should
01917d
+       be passed in FD and INT1.  Helper should respond with a
01917d
+       MNSH_RET_INT.  */
01917d
+    MNSH_REQ_SETNS,
01917d
+
01917d
+    /* A request that the helper call open.  Arguments should
01917d
+       be passed in BUF, INT1 and INT2.  The filename (in BUF)
01917d
+       should include a terminating NUL character.  The helper
01917d
+       should respond with a MNSH_RET_FD.  */
01917d
+    MNSH_REQ_OPEN,
01917d
+
01917d
+    /* A request that the helper call unlink.  The single
01917d
+       argument (the filename) should be passed in BUF, and
01917d
+       should include a terminating NUL character.  The helper
01917d
+       should respond with a MNSH_RET_INT.  */
01917d
+    MNSH_REQ_UNLINK,
01917d
+
01917d
+    /* A request that the helper call readlink.  The single
01917d
+       argument (the filename) should be passed in BUF, and
01917d
+       should include a terminating NUL character.  The helper
01917d
+       should respond with a MNSH_RET_INTSTR.  */
01917d
+    MNSH_REQ_READLINK,
01917d
+
01917d
+    /* Responses, sent to the main process from the helper.  */
01917d
+
01917d
+    /* Return an integer in INT1 and errno in INT2.  */
01917d
+    MNSH_RET_INT,
01917d
+
01917d
+    /* Return a file descriptor in FD if one was opened or an
01917d
+       integer in INT1 otherwise.  Return errno in INT2.  */
01917d
+    MNSH_RET_FD,
01917d
+
01917d
+    /* Return an integer in INT1, errno in INT2, and optionally
01917d
+       some data in BUF.  */
01917d
+    MNSH_RET_INTSTR,
01917d
+  };
01917d
+
01917d
+/* Print a string representation of a message using debug_printf.
01917d
+   This function is not async-signal-safe so should never be
01917d
+   called from the helper.  */
01917d
+
01917d
+static void
01917d
+mnsh_debug_print_message (enum mnsh_msg_type type,
01917d
+			  int fd, int int1, int int2,
01917d
+			  const void *buf, int bufsiz)
01917d
+{
01917d
+  gdb_byte *c = (gdb_byte *) buf;
01917d
+  gdb_byte *cl = c + bufsiz;
01917d
+
01917d
+  switch (type)
01917d
+    {
01917d
+    case MNSH_MSG_ERROR:
01917d
+      debug_printf ("ERROR");
01917d
+      break;
01917d
+
01917d
+    case MNSH_REQ_SETNS:
01917d
+      debug_printf ("SETNS");
01917d
+      break;
01917d
+
01917d
+    case MNSH_REQ_OPEN:
01917d
+      debug_printf ("OPEN");
01917d
+      break;
01917d
+
01917d
+    case MNSH_REQ_UNLINK:
01917d
+      debug_printf ("UNLINK");
01917d
+      break;
01917d
+
01917d
+    case MNSH_REQ_READLINK:
01917d
+      debug_printf ("READLINK");
01917d
+      break;
01917d
+
01917d
+    case MNSH_RET_INT:
01917d
+      debug_printf ("INT");
01917d
+      break;
01917d
+
01917d
+    case MNSH_RET_FD:
01917d
+      debug_printf ("FD");
01917d
+      break;
01917d
+
01917d
+    case MNSH_RET_INTSTR:
01917d
+      debug_printf ("INTSTR");
01917d
+      break;
01917d
+
01917d
+    default:
01917d
+      debug_printf ("unknown-packet-%d", type);
01917d
+    }
01917d
+
01917d
+  debug_printf (" %d %d %d \"", fd, int1, int2);
01917d
+
01917d
+  for (; c < cl; c++)
01917d
+    debug_printf (*c >= ' ' && *c <= '~' ? "%c" : "\\%o", *c);
01917d
+
01917d
+  debug_printf ("\"");
01917d
+}
01917d
+
01917d
+/* Forward declaration.  */
01917d
+
01917d
+static void mnsh_maybe_mourn_peer (void);
01917d
+
01917d
+/* Send a message.  The argument SOCK is the file descriptor of the
01917d
+   sending socket, the other arguments are the payload to send.
01917d
+   Return the number of bytes sent on success.  Return -1 on failure
01917d
+   and set errno appropriately.  This function is called by both the
01917d
+   main process and the helper so must be async-signal-safe.  */
01917d
+
01917d
+static ssize_t
01917d
+mnsh_send_message (int sock, enum mnsh_msg_type type,
01917d
+		   int fd, int int1, int int2,
01917d
+		   const void *buf, int bufsiz)
01917d
+{
01917d
+  struct msghdr msg;
01917d
+  struct iovec iov[4];
01917d
+  char fdbuf[CMSG_SPACE (sizeof (fd))];
01917d
+  ssize_t size;
01917d
+
01917d
+  /* Build the basic TYPE, INT1, INT2 message.  */
01917d
+  memset (&msg, 0, sizeof (msg));
01917d
+  msg.msg_iov = iov;
01917d
+
01917d
+  iov[0].iov_base = &type;
01917d
+  iov[0].iov_len = sizeof (type);
01917d
+  iov[1].iov_base = &int1;
01917d
+  iov[1].iov_len = sizeof (int1);
01917d
+  iov[2].iov_base = &int2;
01917d
+  iov[2].iov_len = sizeof (int2);
01917d
+
01917d
+  msg.msg_iovlen = 3;
01917d
+
01917d
+  /* Append BUF if supplied.  */
01917d
+  if (buf != NULL && bufsiz > 0)
01917d
+    {
01917d
+      iov[3].iov_base = alloca (bufsiz);
01917d
+      memcpy (iov[3].iov_base, buf, bufsiz);
01917d
+      iov[3].iov_len = bufsiz;
01917d
+
01917d
+      msg.msg_iovlen ++;
01917d
+    }
01917d
+
01917d
+  /* Attach FD if supplied.  */
01917d
+  if (fd >= 0)
01917d
+    {
01917d
+      struct cmsghdr *cmsg;
01917d
+
01917d
+      msg.msg_control = fdbuf;
01917d
+      msg.msg_controllen = sizeof (fdbuf);
01917d
+
01917d
+      cmsg = CMSG_FIRSTHDR (&msg;;
01917d
+      cmsg->cmsg_level = SOL_SOCKET;
01917d
+      cmsg->cmsg_type = SCM_RIGHTS;
01917d
+      cmsg->cmsg_len = CMSG_LEN (sizeof (int));
01917d
+
01917d
+      memcpy (CMSG_DATA (cmsg), &fd, sizeof (int));
01917d
+
01917d
+      msg.msg_controllen = cmsg->cmsg_len;
01917d
+    }
01917d
+
01917d
+  /* Send the message.  */
01917d
+  size = sendmsg (sock, &msg, 0);
01917d
+
01917d
+  if (size < 0)
01917d
+    mnsh_maybe_mourn_peer ();
01917d
+
01917d
+  if (debug_linux_namespaces)
01917d
+    {
01917d
+      debug_printf ("mnsh: send: ");
01917d
+      mnsh_debug_print_message (type, fd, int1, int2, buf, bufsiz);
01917d
+      debug_printf (" -> %ld\n", size);
01917d
+    }
01917d
+
01917d
+  return size;
01917d
+}
01917d
+
01917d
+/* Receive a message.  The argument SOCK is the file descriptor of
01917d
+   the receiving socket, the other arguments point to storage for
01917d
+   the received payload.  Returns the number of bytes stored into
01917d
+   BUF on success, which may be zero in the event no BUF was sent.
01917d
+   Return -1 on failure and set errno appropriately.  This function
01917d
+   is called from both the main process and the helper and must be
01917d
+   async-signal-safe.  */
01917d
+
01917d
+static ssize_t
01917d
+mnsh_recv_message (int sock, enum mnsh_msg_type *type,
01917d
+		   int *fd, int *int1, int *int2,
01917d
+		   void *buf, int bufsiz)
01917d
+{
01917d
+  struct msghdr msg;
01917d
+  struct iovec iov[4];
01917d
+  char fdbuf[CMSG_SPACE (sizeof (*fd))];
01917d
+  struct cmsghdr *cmsg;
01917d
+  ssize_t size, fixed_size;
01917d
+  int i;
01917d
+
01917d
+  /* Build the message to receive data into.  */
01917d
+  memset (&msg, 0, sizeof (msg));
01917d
+  msg.msg_iov = iov;
01917d
+
01917d
+  iov[0].iov_base = type;
01917d
+  iov[0].iov_len = sizeof (*type);
01917d
+  iov[1].iov_base = int1;
01917d
+  iov[1].iov_len = sizeof (*int1);
01917d
+  iov[2].iov_base = int2;
01917d
+  iov[2].iov_len = sizeof (*int2);
01917d
+  iov[3].iov_base = buf;
01917d
+  iov[3].iov_len = bufsiz;
01917d
+
01917d
+  msg.msg_iovlen = 4;
01917d
+
01917d
+  for (fixed_size = i = 0; i < msg.msg_iovlen - 1; i++)
01917d
+    fixed_size += iov[i].iov_len;
01917d
+
01917d
+  msg.msg_control = fdbuf;
01917d
+  msg.msg_controllen = sizeof (fdbuf);
01917d
+
01917d
+  /* Receive the message.  */
01917d
+  size = recvmsg (sock, &msg, MSG_CMSG_CLOEXEC);
01917d
+  if (size < 0)
01917d
+    {
01917d
+      if (debug_linux_namespaces)
01917d
+	debug_printf ("namespace-helper: recv failed (%ld)\n", size);
01917d
+
01917d
+      mnsh_maybe_mourn_peer ();
01917d
+
01917d
+      return size;
01917d
+    }
01917d
+
01917d
+  /* Check for truncation.  */
01917d
+  if (size < fixed_size || (msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC)))
01917d
+    {
01917d
+      if (debug_linux_namespaces)
01917d
+	debug_printf ("namespace-helper: recv truncated (%ld 0x%x)\n",
01917d
+		      size, msg.msg_flags);
01917d
+
01917d
+      mnsh_maybe_mourn_peer ();
01917d
+
01917d
+      errno = EBADMSG;
01917d
+      return -1;
01917d
+    }
01917d
+
01917d
+  /* Unpack the file descriptor if supplied.  */
01917d
+  cmsg = CMSG_FIRSTHDR (&msg;;
01917d
+  if (cmsg != NULL
01917d
+      && cmsg->cmsg_len == CMSG_LEN (sizeof (int))
01917d
+      && cmsg->cmsg_level == SOL_SOCKET
01917d
+      && cmsg->cmsg_type == SCM_RIGHTS)
01917d
+    memcpy (fd, CMSG_DATA (cmsg), sizeof (int));
01917d
+  else
01917d
+    *fd = -1;
01917d
+
01917d
+  if (debug_linux_namespaces)
01917d
+    {
01917d
+      debug_printf ("mnsh: recv: ");
01917d
+      mnsh_debug_print_message (*type, *fd, *int1, *int2, buf,
01917d
+				size - fixed_size);
01917d
+      debug_printf ("\n");
01917d
+    }
01917d
+
01917d
+  /* Return the number of bytes of data in BUF.  */
01917d
+  return size - fixed_size;
01917d
+}
01917d
+
01917d
+/* Shortcuts for returning results from the helper.  */
01917d
+
01917d
+#define mnsh_return_int(sock, result, error) \
01917d
+  mnsh_send_message (sock, MNSH_RET_INT, -1, result, error, NULL, 0)
01917d
+
01917d
+#define mnsh_return_fd(sock, fd, error) \
01917d
+  mnsh_send_message (sock, MNSH_RET_FD, \
01917d
+		     (fd) < 0 ? -1 : (fd), \
01917d
+		     (fd) < 0 ? (fd) : 0, \
01917d
+		     error, NULL, 0)
01917d
+
01917d
+#define mnsh_return_intstr(sock, result, buf, bufsiz, error) \
01917d
+  mnsh_send_message (sock, MNSH_RET_INTSTR, -1, result, error, \
01917d
+		     buf, bufsiz)
01917d
+
01917d
+/* Handle a MNSH_REQ_SETNS message.  Must be async-signal-safe.  */
01917d
+
01917d
+static ssize_t
01917d
+mnsh_handle_setns (int sock, int fd, int nstype)
01917d
+{
01917d
+  int result = setns (fd, nstype);
01917d
+
01917d
+  return mnsh_return_int (sock, result, errno);
01917d
+}
01917d
+
01917d
+/* See filestuff.h.  */
01917d
+
01917d
+int
01917d
+gdb_open_cloexec (const char *filename, int flags, unsigned long mode)
01917d
+{
01917d
+  int fd = open (filename, flags | O_CLOEXEC, mode);
01917d
+
01917d
+//  if (fd >= 0)
01917d
+//    maybe_mark_cloexec (fd);
01917d
+
01917d
+  return fd;
01917d
+}
01917d
+
01917d
+/* Handle a MNSH_REQ_OPEN message.  Must be async-signal-safe.  */
01917d
+
01917d
+static ssize_t
01917d
+mnsh_handle_open (int sock, const char *filename,
01917d
+		  int flags, mode_t mode)
01917d
+{
01917d
+  int fd = gdb_open_cloexec (filename, flags, mode);
01917d
+  ssize_t result = mnsh_return_fd (sock, fd, errno);
01917d
+
01917d
+  if (fd >= 0)
01917d
+    close (fd);
01917d
+
01917d
+  return result;
01917d
+}
01917d
+
01917d
+/* Handle a MNSH_REQ_UNLINK message.  Must be async-signal-safe.  */
01917d
+
01917d
+static ssize_t
01917d
+mnsh_handle_unlink (int sock, const char *filename)
01917d
+{
01917d
+  int result = unlink (filename);
01917d
+
01917d
+  return mnsh_return_int (sock, result, errno);
01917d
+}
01917d
+
01917d
+/* Handle a MNSH_REQ_READLINK message.  Must be async-signal-safe.  */
01917d
+
01917d
+static ssize_t
01917d
+mnsh_handle_readlink (int sock, const char *filename)
01917d
+{
01917d
+  char buf[PATH_MAX];
01917d
+  int len = readlink (filename, buf, sizeof (buf));
01917d
+
01917d
+  return mnsh_return_intstr (sock, len,
01917d
+			     buf, len < 0 ? 0 : len,
01917d
+			     errno);
01917d
+}
01917d
+
01917d
+/* The helper process.  Never returns.  Must be async-signal-safe.  */
01917d
+
01917d
+static void mnsh_main (int sock) ATTRIBUTE_NORETURN;
01917d
+
01917d
+static void
01917d
+mnsh_main (int sock)
01917d
+{
01917d
+  while (1)
01917d
+    {
01917d
+      enum mnsh_msg_type type;
01917d
+      int fd, int1, int2;
01917d
+      char buf[PATH_MAX];
01917d
+      ssize_t size, response = -1;
01917d
+
01917d
+      size = mnsh_recv_message (sock, &type,
01917d
+				&fd, &int1, &int2,
01917d
+				buf, sizeof (buf));
01917d
+
01917d
+      if (size >= 0 && size < sizeof (buf))
01917d
+	{
01917d
+	  switch (type)
01917d
+	    {
01917d
+	    case MNSH_REQ_SETNS:
01917d
+	      if (fd > 0)
01917d
+		response = mnsh_handle_setns (sock, fd, int1);
01917d
+	      break;
01917d
+
01917d
+	    case MNSH_REQ_OPEN:
01917d
+	      if (size > 0 && buf[size - 1] == '\0')
01917d
+		response = mnsh_handle_open (sock, buf, int1, int2);
01917d
+	      break;
01917d
+
01917d
+	    case MNSH_REQ_UNLINK:
01917d
+	      if (size > 0 && buf[size - 1] == '\0')
01917d
+		response = mnsh_handle_unlink (sock, buf);
01917d
+	      break;
01917d
+
01917d
+	    case MNSH_REQ_READLINK:
01917d
+	      if (size > 0 && buf[size - 1] == '\0')
01917d
+		response = mnsh_handle_readlink (sock, buf);
01917d
+	      break;
01917d
+
01917d
+	    default:
01917d
+	      break; /* Handled below.  */
01917d
+	    }
01917d
+	}
01917d
+
01917d
+      /* Close any file descriptors we were passed.  */
01917d
+      if (fd >= 0)
01917d
+	close (fd);
01917d
+
01917d
+      /* Can't handle this message, bounce it back.  */
01917d
+      if (response < 0)
01917d
+	{
01917d
+	  if (size < 0)
01917d
+	    size = 0;
01917d
+
01917d
+	  mnsh_send_message (sock, MNSH_MSG_ERROR,
01917d
+			     -1, int1, int2, buf, size);
01917d
+	}
01917d
+    }
01917d
+}
01917d
+
01917d
+/* The mount namespace helper process.  */
01917d
+
01917d
+struct linux_mnsh
01917d
+{
01917d
+  /* PID of helper.  */
01917d
+  pid_t pid;
01917d
+
01917d
+  /* Socket for communication.  */
01917d
+  int sock;
01917d
+
01917d
+  /* ID of the mount namespace the helper is currently in.  */
01917d
+  ino_t nsid;
01917d
+};
01917d
+
01917d
+/* In the helper process this is set to the PID of the process that
01917d
+   created the helper (i.e. GDB or gdbserver).  In the main process
01917d
+   this is set to zero.  Used by mnsh_maybe_mourn_peer.  */
01917d
+static int mnsh_creator_pid = 0;
01917d
+
01917d
+/* See filestuff.h.  */
01917d
+
01917d
+int
01917d
+gdb_socketpair_cloexec (int domain, int style, int protocol,
01917d
+                        int filedes[2])
01917d
+{
01917d
+#ifdef HAVE_SOCKETPAIR
01917d
+  int result = socketpair (domain, style | SOCK_CLOEXEC, protocol, filedes);
01917d
+
01917d
+  if (result != -1)
01917d
+    {
01917d
+//      socket_mark_cloexec (filedes[0]);
01917d
+//      socket_mark_cloexec (filedes[1]);
01917d
+    }
01917d
+
01917d
+  return result;
01917d
+#else
01917d
+  gdb_assert_not_reached (_("socketpair not available on this host"));
01917d
+#endif
01917d
+}
01917d
+
01917d
+/* Return an object representing the mount namespace helper process.
01917d
+   If no mount namespace helper process has been started then start
01917d
+   one.  Return NULL if no mount namespace helper process could be
01917d
+   started.  */
01917d
+
01917d
+static struct linux_mnsh *
01917d
+linux_mntns_get_helper (void)
01917d
+{
01917d
+  static struct linux_mnsh *helper = NULL;
01917d
+
01917d
+  if (helper == NULL)
01917d
+    {
01917d
+      static struct linux_mnsh h;
01917d
+      struct linux_ns *ns;
01917d
+      pid_t helper_creator = getpid ();
01917d
+      int sv[2];
01917d
+
01917d
+      ns = linux_ns_get_namespace (LINUX_NS_MNT);
01917d
+      if (ns == NULL)
01917d
+	return NULL;
01917d
+
01917d
+      if (gdb_socketpair_cloexec (AF_UNIX, SOCK_STREAM, 0, sv) < 0)
01917d
+	return NULL;
01917d
+
01917d
+      h.pid = fork ();
01917d
+      if (h.pid < 0)
01917d
+	{
01917d
+	  int saved_errno = errno;
01917d
+
01917d
+	  close (sv[0]);
01917d
+	  close (sv[1]);
01917d
+
01917d
+	  errno = saved_errno;
01917d
+	  return NULL;
01917d
+	}
01917d
+
01917d
+      if (h.pid == 0)
01917d
+	{
01917d
+	  /* Child process.  */
01917d
+	  close (sv[0]);
01917d
+
01917d
+	  mnsh_creator_pid = helper_creator;
01917d
+
01917d
+	  /* Debug printing isn't async-signal-safe.  */
01917d
+	  debug_linux_namespaces = 0;
01917d
+
01917d
+	  mnsh_main (sv[1]);
01917d
+	}
01917d
+
01917d
+      /* Parent process.  */
01917d
+      close (sv[1]);
01917d
+
01917d
+      helper = &h;
01917d
+      helper->sock = sv[0];
01917d
+      helper->nsid = ns->id;
01917d
+
01917d
+      if (debug_linux_namespaces)
01917d
+	debug_printf ("Started mount namespace helper process %d\n",
01917d
+		      helper->pid);
01917d
+    }
01917d
+
01917d
+  return helper;
01917d
+}
01917d
+
01917d
+/* Check whether the other process died and act accordingly.  Called
01917d
+   whenever a socket error occurs, from both the main process and the
01917d
+   helper.  Must be async-signal-safe when called from the helper.  */
01917d
+
01917d
+static void
01917d
+mnsh_maybe_mourn_peer (void)
01917d
+{
01917d
+  if (mnsh_creator_pid != 0)
01917d
+    {
01917d
+      /* We're in the helper.  Check if our current parent is the
01917d
+	 process that started us.  If it isn't, then our original
01917d
+	 parent died and we've been reparented.  Exit immediately
01917d
+	 if that's the case.  */
01917d
+      if (getppid () != mnsh_creator_pid)
01917d
+	_exit (0);
01917d
+    }
01917d
+  else
01917d
+    {
01917d
+      /* We're in the main process.  */
01917d
+
01917d
+      struct linux_mnsh *helper = linux_mntns_get_helper ();
01917d
+      int status;
01917d
+      pid_t pid;
01917d
+
01917d
+      if (helper->pid < 0)
01917d
+	{
01917d
+	  /* We already mourned it.  */
01917d
+	  return;
01917d
+	}
01917d
+
01917d
+      pid = waitpid (helper->pid, &status, WNOHANG);
01917d
+      if (pid == 0)
01917d
+	{
01917d
+	  /* The helper is still alive.  */
01917d
+	  return;
01917d
+	}
01917d
+      else if (pid == -1)
01917d
+	{
01917d
+	  if (errno == ECHILD)
01917d
+	    warning (_("mount namespace helper vanished?"));
01917d
+	  else
01917d
+	    internal_warning (__FILE__, __LINE__,
01917d
+			      _("unhandled error %d"), errno);
01917d
+	}
01917d
+      else if (pid == helper->pid)
01917d
+	{
01917d
+	  if (WIFEXITED (status))
01917d
+	    warning (_("mount namespace helper exited with status %d"),
01917d
+		     WEXITSTATUS (status));
01917d
+	  else if (WIFSIGNALED (status))
01917d
+	    warning (_("mount namespace helper killed by signal %d"),
01917d
+		     WTERMSIG (status));
01917d
+	  else
01917d
+	    internal_warning (__FILE__, __LINE__,
01917d
+			      _("unhandled status %d"), status);
01917d
+	}
01917d
+      else
01917d
+	internal_warning (__FILE__, __LINE__,
01917d
+			  _("unknown pid %d"), pid);
01917d
+
01917d
+      /* Something unrecoverable happened.  */
01917d
+      helper->pid = -1;
01917d
+    }
01917d
+}
01917d
+
01917d
+/* Shortcuts for sending messages to the helper.  */
01917d
+
01917d
+#define mnsh_send_setns(helper, fd, nstype) \
01917d
+  mnsh_send_message (helper->sock, MNSH_REQ_SETNS, fd, nstype, 0, \
01917d
+		     NULL, 0)
01917d
+
01917d
+#define mnsh_send_open(helper, filename, flags, mode) \
01917d
+  mnsh_send_message (helper->sock, MNSH_REQ_OPEN, -1, flags, mode, \
01917d
+    		     filename, strlen (filename) + 1)
01917d
+
01917d
+#define mnsh_send_unlink(helper, filename) \
01917d
+  mnsh_send_message (helper->sock, MNSH_REQ_UNLINK, -1, 0, 0, \
01917d
+    		     filename, strlen (filename) + 1)
01917d
+
01917d
+#define mnsh_send_readlink(helper, filename) \
01917d
+  mnsh_send_message (helper->sock, MNSH_REQ_READLINK, -1, 0, 0, \
01917d
+    		     filename, strlen (filename) + 1)
01917d
+
01917d
+/* Receive a message from the helper.  Issue an assertion failure if
01917d
+   the message isn't a correctly-formatted MNSH_RET_INT.  Set RESULT
01917d
+   and ERROR and return 0 on success.  Set errno and return -1 on
01917d
+   failure.  */
01917d
+
01917d
+static int
01917d
+mnsh_recv_int (struct linux_mnsh *helper, int *result, int *error)
01917d
+{
01917d
+  enum mnsh_msg_type type;
01917d
+  char buf[PATH_MAX];
01917d
+  ssize_t size;
01917d
+  int fd;
01917d
+
01917d
+  size = mnsh_recv_message (helper->sock, &type, &fd,
01917d
+			    result, error,
01917d
+			    buf, sizeof (buf));
01917d
+  if (size < 0)
01917d
+    return -1;
01917d
+
01917d
+  gdb_assert (type == MNSH_RET_INT);
01917d
+  gdb_assert (fd == -1);
01917d
+  gdb_assert (size == 0);
01917d
+
01917d
+  return 0;
01917d
+}
01917d
+
01917d
+/* Receive a message from the helper.  Issue an assertion failure if
01917d
+   the message isn't a correctly-formatted MNSH_RET_FD.  Set FD and
01917d
+   ERROR and return 0 on success.  Set errno and return -1 on
01917d
+   failure.  */
01917d
+
01917d
+static int
01917d
+mnsh_recv_fd (struct linux_mnsh *helper, int *fd, int *error)
01917d
+{
01917d
+  enum mnsh_msg_type type;
01917d
+  char buf[PATH_MAX];
01917d
+  ssize_t size;
01917d
+  int result;
01917d
+
01917d
+  size = mnsh_recv_message (helper->sock, &type, fd,
01917d
+			    &result, error,
01917d
+			    buf, sizeof (buf));
01917d
+  if (size < 0)
01917d
+    return -1;
01917d
+
01917d
+  gdb_assert (type == MNSH_RET_FD);
01917d
+  gdb_assert (size == 0);
01917d
+
01917d
+  if (*fd < 0)
01917d
+    {
01917d
+      gdb_assert (result < 0);
01917d
+      *fd = result;
01917d
+    }
01917d
+
01917d
+  return 0;
01917d
+}
01917d
+
01917d
+/* Receive a message from the helper.  Issue an assertion failure if
01917d
+   the message isn't a correctly-formatted MNSH_RET_INTSTR.  Set
01917d
+   RESULT and ERROR and optionally store data in BUF, then return
01917d
+   the number of bytes stored in BUF on success (this may be zero).
01917d
+   Set errno and return -1 on error.  */
01917d
+
01917d
+static ssize_t
01917d
+mnsh_recv_intstr (struct linux_mnsh *helper,
01917d
+		  int *result, int *error,
01917d
+		  void *buf, int bufsiz)
01917d
+{
01917d
+  enum mnsh_msg_type type;
01917d
+  ssize_t size;
01917d
+  int fd;
01917d
+
01917d
+  size = mnsh_recv_message (helper->sock, &type, &fd,
01917d
+			    result, error,
01917d
+			    buf, bufsiz);
01917d
+
01917d
+  if (size < 0)
01917d
+    return -1;
01917d
+
01917d
+  gdb_assert (type == MNSH_RET_INTSTR);
01917d
+  gdb_assert (fd == -1);
01917d
+
01917d
+  return size;
01917d
+}
01917d
+
01917d
+/* Return values for linux_mntns_access_fs.  */
01917d
+
01917d
+enum mnsh_fs_code
01917d
+  {
01917d
+    /* Something went wrong, errno is set.  */
01917d
+    MNSH_FS_ERROR = -1,
01917d
+
01917d
+    /* The main process is in the correct mount namespace.
01917d
+       The caller should access the filesystem directly.  */
01917d
+    MNSH_FS_DIRECT,
01917d
+
01917d
+    /* The helper is in the correct mount namespace.
01917d
+       The caller should access the filesystem via the helper.  */
01917d
+    MNSH_FS_HELPER
01917d
+  };
01917d
+
01917d
+/* Return a value indicating how the caller should access the
01917d
+   mount namespace of process PID.  */
01917d
+
01917d
+static enum mnsh_fs_code
01917d
+linux_mntns_access_fs (pid_t pid)
01917d
+{
01917d
+//  struct cleanup *old_chain;
01917d
+  struct linux_ns *ns;
01917d
+  struct stat sb;
01917d
+  struct linux_mnsh *helper;
01917d
+  ssize_t size;
01917d
+  int fd, saved_errno;
01917d
+
01917d
+  if (pid == getpid ())
01917d
+    return MNSH_FS_DIRECT;
01917d
+
01917d
+  ns = linux_ns_get_namespace (LINUX_NS_MNT);
01917d
+  if (ns == NULL)
01917d
+    return MNSH_FS_DIRECT;
01917d
+
01917d
+//  old_chain = make_cleanup (null_cleanup, NULL);
01917d
+
01917d
+  fd = gdb_open_cloexec (linux_ns_filename (ns, pid), O_RDONLY, 0);
01917d
+  if (fd < 0)
01917d
+    goto error_nofd;
01917d
+
01917d
+//  old_chain = make_cleanup_close (fd);
01917d
+
01917d
+  if (fstat (fd, &sb) != 0)
01917d
+    goto error;
01917d
+
01917d
+  if (sb.st_ino == ns->id)
01917d
+    {
01917d
+//      do_cleanups (old_chain);
01917d
+close (fd);
01917d
+
01917d
+      return MNSH_FS_DIRECT;
01917d
+    }
01917d
+
01917d
+  helper = linux_mntns_get_helper ();
01917d
+  if (helper == NULL)
01917d
+    goto error;
01917d
+
01917d
+  if (sb.st_ino != helper->nsid)
01917d
+    {
01917d
+      int result, error;
01917d
+
01917d
+      size = mnsh_send_setns (helper, fd, 0);
01917d
+      if (size < 0)
01917d
+	goto error;
01917d
+
01917d
+      if (mnsh_recv_int (helper, &result, &error) != 0)
01917d
+	goto error;
01917d
+
01917d
+      if (result != 0)
01917d
+	{
01917d
+	  /* ENOSYS indicates that an entire function is unsupported
01917d
+	     (it's not appropriate for our versions of open/unlink/
01917d
+	     readlink to sometimes return with ENOSYS depending on how
01917d
+	     they're called) so we convert ENOSYS to ENOTSUP if setns
01917d
+	     fails.  */
01917d
+	  if (error == ENOSYS)
01917d
+	    error = ENOTSUP;
01917d
+
01917d
+	  errno = error;
01917d
+	  goto error;
01917d
+	}
01917d
+
01917d
+      helper->nsid = sb.st_ino;
01917d
+    }
01917d
+
01917d
+//  do_cleanups (old_chain);
01917d
+close (fd);
01917d
+
01917d
+  return MNSH_FS_HELPER;
01917d
+
01917d
+error:
01917d
+  saved_errno = errno;
01917d
+
01917d
+//  do_cleanups (old_chain);
01917d
+close (fd);
01917d
+
01917d
+  errno = saved_errno;
01917d
+error_nofd:
01917d
+  return MNSH_FS_ERROR;
01917d
+}
01917d
+
01917d
+/* See nat/linux-namespaces.h.  */
01917d
+
01917d
+int
01917d
+linux_mntns_open_cloexec (pid_t pid, const char *filename,
01917d
+			  int flags, mode_t mode)
01917d
+{
01917d
+  enum mnsh_fs_code access = linux_mntns_access_fs (pid);
01917d
+  struct linux_mnsh *helper;
01917d
+  int fd, error;
01917d
+  ssize_t size;
01917d
+
01917d
+  if (access == MNSH_FS_ERROR)
01917d
+    return -1;
01917d
+
01917d
+  if (access == MNSH_FS_DIRECT)
01917d
+    return gdb_open_cloexec (filename, flags, mode);
01917d
+
01917d
+  gdb_assert (access == MNSH_FS_HELPER);
01917d
+
01917d
+  helper = linux_mntns_get_helper ();
01917d
+
01917d
+  size = mnsh_send_open (helper, filename, flags, mode);
01917d
+  if (size < 0)
01917d
+    return -1;
01917d
+
01917d
+  if (mnsh_recv_fd (helper, &fd, &error) != 0)
01917d
+    return -1;
01917d
+
01917d
+  if (fd < 0)
01917d
+    errno = error;
01917d
+
01917d
+  return fd;
01917d
+}
01917d
+
01917d
+/* See nat/linux-namespaces.h.  */
01917d
+
01917d
+int
01917d
+linux_mntns_unlink (pid_t pid, const char *filename)
01917d
+{
01917d
+  enum mnsh_fs_code access = linux_mntns_access_fs (pid);
01917d
+  struct linux_mnsh *helper;
01917d
+  int ret, error;
01917d
+  ssize_t size;
01917d
+
01917d
+  if (access == MNSH_FS_ERROR)
01917d
+    return -1;
01917d
+
01917d
+  if (access == MNSH_FS_DIRECT)
01917d
+    return unlink (filename);
01917d
+
01917d
+  gdb_assert (access == MNSH_FS_HELPER);
01917d
+
01917d
+  helper = linux_mntns_get_helper ();
01917d
+
01917d
+  size = mnsh_send_unlink (helper, filename);
01917d
+  if (size < 0)
01917d
+    return -1;
01917d
+
01917d
+  if (mnsh_recv_int (helper, &ret, &error) != 0)
01917d
+    return -1;
01917d
+
01917d
+  if (ret != 0)
01917d
+    errno = error;
01917d
+
01917d
+  return ret;
01917d
+}
01917d
+
01917d
+/* See nat/linux-namespaces.h.  */
01917d
+
01917d
+ssize_t
01917d
+linux_mntns_readlink (pid_t pid, const char *filename,
01917d
+		      char *buf, size_t bufsiz)
01917d
+{
01917d
+  enum mnsh_fs_code access = linux_mntns_access_fs (pid);
01917d
+  struct linux_mnsh *helper;
01917d
+  int ret, error;
01917d
+  ssize_t size;
01917d
+
01917d
+  if (access == MNSH_FS_ERROR)
01917d
+    return -1;
01917d
+
01917d
+  if (access == MNSH_FS_DIRECT)
01917d
+    return readlink (filename, buf, bufsiz);
01917d
+
01917d
+  gdb_assert (access == MNSH_FS_HELPER);
01917d
+
01917d
+  helper = linux_mntns_get_helper ();
01917d
+
01917d
+  size = mnsh_send_readlink (helper, filename);
01917d
+  if (size < 0)
01917d
+    return -1;
01917d
+
01917d
+  size = mnsh_recv_intstr (helper, &ret, &error, buf, bufsiz);
01917d
+
01917d
+  if (size < 0)
01917d
+    {
01917d
+      ret = -1;
01917d
+      errno = error;
01917d
+    }
01917d
+  else
01917d
+    gdb_assert (size == ret);
01917d
+
01917d
+  return ret;
01917d
+}
01917d
Index: gdb-7.6.1/gdb/nat/linux-namespaces.h
01917d
===================================================================
01917d
--- /dev/null	1970-01-01 00:00:00.000000000 +0000
120562
+++ gdb-7.6.1/gdb/nat/linux-namespaces.h	2018-05-18 10:55:28.424983208 +0200
01917d
@@ -0,0 +1,78 @@
01917d
+/* Linux namespaces(7) support.
01917d
+
01917d
+   Copyright (C) 2015 Free Software Foundation, Inc.
01917d
+
01917d
+   This file is part of GDB.
01917d
+
01917d
+   This program is free software; you can redistribute it and/or modify
01917d
+   it under the terms of the GNU General Public License as published by
01917d
+   the Free Software Foundation; either version 3 of the License, or
01917d
+   (at your option) any later version.
01917d
+
01917d
+   This program is distributed in the hope that it will be useful,
01917d
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
01917d
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
01917d
+   GNU General Public License for more details.
01917d
+
01917d
+   You should have received a copy of the GNU General Public License
01917d
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
01917d
+
01917d
+#ifndef LINUX_NAMESPACES_H
01917d
+#define LINUX_NAMESPACES_H
01917d
+
01917d
+#include <sys/types.h>
01917d
+
01917d
+/* Set to nonzero to enable debugging of Linux namespaces code.  */
01917d
+
01917d
+extern int debug_linux_namespaces;
01917d
+
01917d
+/* Enumeration of Linux namespace types.  */
01917d
+
01917d
+enum linux_ns_type
01917d
+  {
01917d
+    /* IPC namespace: System V IPC, POSIX message queues.  */
01917d
+    LINUX_NS_IPC,
01917d
+
01917d
+    /* Mount namespace: mount points.  */
01917d
+    LINUX_NS_MNT,
01917d
+
01917d
+    /* Network namespace: network devices, stacks, ports, etc.  */
01917d
+    LINUX_NS_NET,
01917d
+
01917d
+    /* PID namespace: process IDs.  */
01917d
+    LINUX_NS_PID,
01917d
+
01917d
+    /* User namespace: user and group IDs.  */
01917d
+    LINUX_NS_USER,
01917d
+
01917d
+    /* UTS namespace: hostname and NIS domain name.  */
01917d
+    LINUX_NS_UTS,
01917d
+
01917d
+    /* Number of Linux namespaces.  */
01917d
+    NUM_LINUX_NS_TYPES
01917d
+  };
01917d
+
01917d
+/* Return nonzero if process PID has the same TYPE namespace as the
01917d
+   calling process, or if the kernel does not support TYPE namespaces
01917d
+   (in which case there is only one TYPE namespace).  Return zero if
01917d
+   the kernel supports TYPE namespaces and the two processes have
01917d
+   different TYPE namespaces.  */
01917d
+
01917d
+extern int linux_ns_same (pid_t pid, enum linux_ns_type type);
01917d
+
01917d
+/* Like gdb_open_cloexec, but in the mount namespace of process
01917d
+   PID.  */
01917d
+
01917d
+extern int linux_mntns_open_cloexec (pid_t pid, const char *filename,
01917d
+				     int flags, mode_t mode);
01917d
+
01917d
+/* Like unlink(2), but in the mount namespace of process PID.  */
01917d
+
01917d
+extern int linux_mntns_unlink (pid_t pid, const char *filename);
01917d
+
01917d
+/* Like readlink(2), but in the mount namespace of process PID.  */
01917d
+
01917d
+extern ssize_t linux_mntns_readlink (pid_t pid, const char *filename,
01917d
+				     char *buf, size_t bufsiz);
01917d
+
01917d
+#endif /* LINUX_NAMESPACES_H */
120562
Index: gdb-7.6.1/gdb/config.in
120562
===================================================================
120562
--- gdb-7.6.1.orig/gdb/config.in	2018-05-18 10:55:26.838970359 +0200
120562
+++ gdb-7.6.1/gdb/config.in	2018-05-18 11:00:28.000000000 +0200
120562
@@ -369,6 +369,9 @@
120562
 /* Define to 1 if you have the `setlocale' function. */
120562
 #undef HAVE_SETLOCALE
120562
 
120562
+/* Define to 1 if you have the `setns' function. */
120562
+#undef HAVE_SETNS
120562
+
120562
 /* Define to 1 if you have the `setpgid' function. */
120562
 #undef HAVE_SETPGID
120562
 
120562
Index: gdb-7.6.1/gdb/configure
120562
===================================================================
120562
--- gdb-7.6.1.orig/gdb/configure	2018-05-18 10:55:26.840970375 +0200
120562
+++ gdb-7.6.1/gdb/configure	2018-05-18 11:00:30.074427061 +0200
120562
@@ -10667,7 +10667,7 @@
120562
 		sbrk setpgid setpgrp setsid \
120562
 		sigaction sigprocmask sigsetmask socketpair syscall \
120562
 		ttrace wborder wresize setlocale iconvlist libiconvlist btowc \
120562
-		setrlimit getrlimit posix_madvise waitpid lstat
120562
+		setrlimit getrlimit posix_madvise waitpid lstat setns
120562
 do :
120562
   as_ac_var=`$as_echo "ac_cv_func_$ac_func" | $as_tr_sh`
120562
 ac_fn_c_check_func "$LINENO" "$ac_func" "$as_ac_var"
120562
Index: gdb-7.6.1/gdb/configure.ac
120562
===================================================================
120562
--- gdb-7.6.1.orig/gdb/configure.ac	2018-05-18 10:55:26.828970278 +0200
120562
+++ gdb-7.6.1/gdb/configure.ac	2018-05-18 11:00:02.912207003 +0200
120562
@@ -1365,7 +1365,7 @@
120562
 		sbrk setpgid setpgrp setsid \
120562
 		sigaction sigprocmask sigsetmask socketpair syscall \
120562
 		ttrace wborder wresize setlocale iconvlist libiconvlist btowc \
120562
-		setrlimit getrlimit posix_madvise waitpid lstat])
120562
+		setrlimit getrlimit posix_madvise waitpid lstat setns])
120562
 AM_LANGINFO_CODESET
120562
 
120562
 # Check the return and argument types of ptrace.  No canned test for