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

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