[PATCH 3/7] Introduce nat/linux-namespaces.[ch]

Gary Benson
2015-04-16T12:34:59Z

This commit introduces new shared files nat/linux-namespaces.[ch]
containing code to support Linux namespaces that will be used by
both GDB and gdbserver.

gdb/ChangeLog:

* configure.ac (AC_CHECK_FUNCS): Add setns.
* config.in: Regenerate.
* configure: Likewise.
* nat/linux-namespaces.h: New file.
* nat/linux-namespaces.c: Likewise.
* Makefile.in (HFILES_NO_SRCDIR): Add nat/linux-namespaces.h.
(linux-namespaces.o): New rule.
* config/aarch64/linux.mh (NATDEPFILES): Add linux-namespaces.o.
* config/alpha/alpha-linux.mh (NATDEPFILES): Likewise.
* config/arm/linux.mh (NATDEPFILES): Likewise.
* config/i386/linux.mh (NATDEPFILES): Likewise.
* config/i386/linux64.mh (NATDEPFILES): Likewise.
* config/ia64/linux.mh (NATDEPFILES): Likewise.
* config/m32r/linux.mh (NATDEPFILES): Likewise.
* config/m68k/linux.mh (NATDEPFILES): Likewise.
* config/mips/linux.mh (NATDEPFILES): Likewise.
* config/pa/linux.mh (NATDEPFILES): Likewise.
* config/powerpc/linux.mh (NATDEPFILES): Likewise.
* config/powerpc/ppc64-linux.mh (NATDEPFILES): Likewise.
* config/powerpc/spu-linux.mh (NATDEPFILES): Likewise.
* config/s390/linux.mh (NATDEPFILES): Likewise.
* config/sparc/linux.mh (NATDEPFILES): Likewise.
* config/sparc/linux64.mh (NATDEPFILES): Likewise.
* config/tilegx/linux.mh (NATDEPFILES): Likewise.
* config/xtensa/linux.mh (NATDEPFILES): Likewise.

gdb/gdbserver/ChangeLog:

* configure.ac (AC_CHECK_FUNCS): Add setns.
* config.in: Regenerate.
* configure: Likewise.
* Makefile.in (SFILES): Add nat/linux-namespaces.c.
(linux-namespaces.o): New rule.
* configure.srv (srv_linux_obj): Add linux-namespaces.o.
---
gdb/ChangeLog | 28 +++++
gdb/Makefile.in | 6 +-
gdb/config.in | 3 +
gdb/config/aarch64/linux.mh | 2 +-
gdb/config/alpha/alpha-linux.mh | 2 +-
gdb/config/arm/linux.mh | 2 +-
gdb/config/i386/linux.mh | 2 +-
gdb/config/i386/linux64.mh | 2 +-
gdb/config/ia64/linux.mh | 3 +-
gdb/config/m32r/linux.mh | 2 +-
gdb/config/m68k/linux.mh | 2 +-
gdb/config/mips/linux.mh | 2 +-
gdb/config/pa/linux.mh | 2 +-
gdb/config/powerpc/linux.mh | 2 +-
gdb/config/powerpc/ppc64-linux.mh | 3 +-
gdb/config/powerpc/spu-linux.mh | 4 +-
gdb/config/s390/linux.mh | 2 +-
gdb/config/sparc/linux.mh | 2 +-
gdb/config/sparc/linux64.mh | 2 +-
gdb/config/tilegx/linux.mh | 2 +-
gdb/config/xtensa/linux.mh | 2 +-
gdb/configure | 2 +-
gdb/configure.ac | 2 +-
gdb/gdbserver/ChangeLog | 9 ++
gdb/gdbserver/Makefile.in | 6 +-
gdb/gdbserver/config.in | 3 +
gdb/gdbserver/configure | 2 +-
gdb/gdbserver/configure.ac | 2 +-
gdb/gdbserver/configure.srv | 2 +-
gdb/nat/linux-namespaces.c | 231 +++++++++++++++++++++++++++++++++++++
gdb/nat/linux-namespaces.h | 61 ++++++++++
31 files changed, 371 insertions(+), 26 deletions(-)
create mode 100644 gdb/nat/linux-namespaces.c
create mode 100644 gdb/nat/linux-namespaces.h

diff --git a/gdb/Makefile.in b/gdb/Makefile.in
index 47b216a..9921dcb 100644
--- a/gdb/Makefile.in
+++ b/gdb/Makefile.in
@@ -980,7 +980,7 @@ common/common-debug.h common/cleanups.h common/gdb_setjmp.h \
common/common-exceptions.h target/target.h common/symbol.h \
common/common-regcache.h fbsd-tdep.h nat/linux-personality.h \
common/fileio.h nat/x86-linux.h nat/x86-linux-dregs.h \
-common/cleanup-utils.h
+common/cleanup-utils.h nat/linux-namespaces.h

# Header files that already have srcdir in them, or which are in objdir.

@@ -2325,6 +2325,10 @@ x86-linux-dregs.o: ${srcdir}/nat/x86-linux-dregs.c
$(COMPILE) $(srcdir)/nat/x86-linux-dregs.c
$(POSTCOMPILE)

+linux-namespaces.o: ${srcdir}/nat/linux-namespaces.c
+ $(COMPILE) $(srcdir)/nat/linux-namespaces.c
+ $(POSTCOMPILE)
+
#
# gdb/tui/ dependencies
#
diff --git a/gdb/config.in b/gdb/config.in
index d41e6cf..5b6dbbb 100644
--- a/gdb/config.in
+++ b/gdb/config.in
@@ -390,6 +390,9 @@
/* Define to 1 if you have the `setlocale' function. */
#undef HAVE_SETLOCALE

+/* Define to 1 if you have the `setns' function. */
+#undef HAVE_SETNS
+
/* Define to 1 if you have the `setpgid' function. */
#undef HAVE_SETPGID

diff --git a/gdb/config/aarch64/linux.mh b/gdb/config/aarch64/linux.mh
index 7f96e4d..6a8aa7d 100644
--- a/gdb/config/aarch64/linux.mh
+++ b/gdb/config/aarch64/linux.mh
@@ -22,7 +22,7 @@ NAT_FILE= config/nm-linux.h
NATDEPFILES= inf-ptrace.o fork-child.o aarch64-linux-nat.o \
proc-service.o linux-thread-db.o linux-nat.o linux-fork.o \
linux-procfs.o linux-ptrace.o linux-osdata.o linux-waitpid.o \
- linux-personality.o
+ linux-personality.o linux-namespaces.o
NAT_CDEPS = $(srcdir)/proc-service.list

LOADLIBES= -ldl $(RDYNAMIC)
diff --git a/gdb/config/alpha/alpha-linux.mh b/gdb/config/alpha/alpha-linux.mh
index 2ea02a1..4991dd2 100644
--- a/gdb/config/alpha/alpha-linux.mh
+++ b/gdb/config/alpha/alpha-linux.mh
@@ -3,7 +3,7 @@ NAT_FILE= config/nm-linux.h
NATDEPFILES= inf-ptrace.o alpha-linux-nat.o \
fork-child.o proc-service.o linux-thread-db.o \
linux-nat.o linux-osdata.o linux-fork.o linux-procfs.o linux-ptrace.o \
- linux-waitpid.o linux-personality.o
+ linux-waitpid.o linux-personality.o linux-namespaces.o
NAT_CDEPS = $(srcdir)/proc-service.list

# The dynamically loaded libthread_db needs access to symbols in the
diff --git a/gdb/config/arm/linux.mh b/gdb/config/arm/linux.mh
index 549bf42..f0c5967 100644
--- a/gdb/config/arm/linux.mh
+++ b/gdb/config/arm/linux.mh
@@ -4,7 +4,7 @@ NAT_FILE= config/nm-linux.h
NATDEPFILES= inf-ptrace.o fork-child.o arm-linux-nat.o \
proc-service.o linux-thread-db.o \
linux-nat.o linux-osdata.o linux-fork.o linux-procfs.o linux-ptrace.o \
- linux-waitpid.o linux-personality.o
+ linux-waitpid.o linux-personality.o linux-namespaces.o
NAT_CDEPS = $(srcdir)/proc-service.list

LOADLIBES= -ldl $(RDYNAMIC)
diff --git a/gdb/config/i386/linux.mh b/gdb/config/i386/linux.mh
index 9be2c5f..421c56f 100644
--- a/gdb/config/i386/linux.mh
+++ b/gdb/config/i386/linux.mh
@@ -6,7 +6,7 @@ NATDEPFILES= inf-ptrace.o fork-child.o \
proc-service.o linux-thread-db.o \
linux-nat.o linux-osdata.o linux-fork.o linux-procfs.o linux-ptrace.o \
linux-btrace.o linux-waitpid.o linux-personality.o x86-linux.o \
- x86-linux-dregs.o
+ x86-linux-dregs.o linux-namespaces.o
NAT_CDEPS = $(srcdir)/proc-service.list

# The dynamically loaded libthread_db needs access to symbols in the
diff --git a/gdb/config/i386/linux64.mh b/gdb/config/i386/linux64.mh
index 224a2a9..04cbb95 100644
--- a/gdb/config/i386/linux64.mh
+++ b/gdb/config/i386/linux64.mh
@@ -6,7 +6,7 @@ NATDEPFILES= inf-ptrace.o fork-child.o \
proc-service.o linux-thread-db.o linux-fork.o \
linux-procfs.o linux-ptrace.o linux-btrace.o \
linux-waitpid.o linux-personality.o x86-linux.o \
- x86-linux-dregs.o
+ x86-linux-dregs.o linux-namespaces.o
NAT_FILE= config/nm-linux.h
NAT_CDEPS = $(srcdir)/proc-service.list

diff --git a/gdb/config/ia64/linux.mh b/gdb/config/ia64/linux.mh
index 9dce22b..b05f834 100644
--- a/gdb/config/ia64/linux.mh
+++ b/gdb/config/ia64/linux.mh
@@ -6,7 +6,8 @@ NATDEPFILES= inf-ptrace.o fork-child.o \
proc-service.o linux-thread-db.o \
linux-nat.o linux-osdata.o linux-fork.o \
linux-personality.o \
- linux-procfs.o linux-ptrace.o linux-waitpid.o
+ linux-procfs.o linux-ptrace.o linux-waitpid.o \
+ linux-namespaces.o
NAT_CDEPS = $(srcdir)/proc-service.list

LOADLIBES = -ldl $(RDYNAMIC)
diff --git a/gdb/config/m32r/linux.mh b/gdb/config/m32r/linux.mh
index 6b810e6..277d8bd 100644
--- a/gdb/config/m32r/linux.mh
+++ b/gdb/config/m32r/linux.mh
@@ -4,7 +4,7 @@ NAT_FILE= config/nm-linux.h
NATDEPFILES= inf-ptrace.o fork-child.o \
m32r-linux-nat.o proc-service.o linux-thread-db.o \
linux-nat.o linux-osdata.o linux-fork.o linux-procfs.o linux-ptrace.o \
- linux-waitpid.o linux-personality.o
+ linux-waitpid.o linux-personality.o linux-namespaces.o
NAT_CDEPS = $(srcdir)/proc-service.list

LOADLIBES= -ldl $(RDYNAMIC)
diff --git a/gdb/config/m68k/linux.mh b/gdb/config/m68k/linux.mh
index f3b3baa..60d5ce4 100644
--- a/gdb/config/m68k/linux.mh
+++ b/gdb/config/m68k/linux.mh
@@ -6,7 +6,7 @@ NATDEPFILES= inf-ptrace.o fork-child.o \
proc-service.o linux-thread-db.o \
linux-nat.o linux-osdata.o linux-fork.o linux-procfs.o linux-ptrace.o \
linux-personality.o \
- linux-waitpid.o
+ linux-waitpid.o linux-namespaces.o
NAT_CDEPS = $(srcdir)/proc-service.list

# The dynamically loaded libthread_db needs access to symbols in the
diff --git a/gdb/config/mips/linux.mh b/gdb/config/mips/linux.mh
index d6a802f..11ff903 100644
--- a/gdb/config/mips/linux.mh
+++ b/gdb/config/mips/linux.mh
@@ -5,7 +5,7 @@ NATDEPFILES= inf-ptrace.o fork-child.o mips-linux-nat.o \
linux-nat.o linux-osdata.o linux-fork.o \
linux-procfs.o linux-ptrace.o linux-waitpid.o \
linux-personality.o \
- mips-linux-watch.o
+ mips-linux-watch.o linux-namespaces.o
NAT_CDEPS = $(srcdir)/proc-service.list

LOADLIBES = -ldl $(RDYNAMIC)
diff --git a/gdb/config/pa/linux.mh b/gdb/config/pa/linux.mh
index 9539b64..1b73ecd 100644
--- a/gdb/config/pa/linux.mh
+++ b/gdb/config/pa/linux.mh
@@ -4,7 +4,7 @@ NATDEPFILES= inf-ptrace.o fork-child.o \
hppa-linux-nat.o proc-service.o linux-thread-db.o \
linux-nat.o linux-osdata.o linux-fork.o \
linux-procfs.o linux-ptrace.o linux-waitpid.o \
- linux-personality.o
+ linux-personality.o linux-namespaces.o
NAT_CDEPS = $(srcdir)/proc-service.list

LOADLIBES = -ldl $(RDYNAMIC)
diff --git a/gdb/config/powerpc/linux.mh b/gdb/config/powerpc/linux.mh
index 76e62c0..f4a52c3 100644
--- a/gdb/config/powerpc/linux.mh
+++ b/gdb/config/powerpc/linux.mh
@@ -6,7 +6,7 @@ NAT_FILE= config/nm-linux.h
NATDEPFILES= inf-ptrace.o fork-child.o \
ppc-linux-nat.o proc-service.o linux-thread-db.o \
linux-nat.o linux-osdata.o linux-fork.o linux-procfs.o linux-ptrace.o \
- linux-waitpid.o linux-personality.o
+ linux-waitpid.o linux-personality.o linux-namespaces.o
NAT_CDEPS = $(srcdir)/proc-service.list

LOADLIBES = -ldl $(RDYNAMIC)
diff --git a/gdb/config/powerpc/ppc64-linux.mh b/gdb/config/powerpc/ppc64-linux.mh
index 7eb6507..580eb1f 100644
--- a/gdb/config/powerpc/ppc64-linux.mh
+++ b/gdb/config/powerpc/ppc64-linux.mh
@@ -6,7 +6,8 @@ NAT_FILE= config/nm-linux.h
NATDEPFILES= inf-ptrace.o fork-child.o \
ppc-linux-nat.o proc-service.o linux-thread-db.o \
linux-nat.o linux-osdata.o linux-fork.o linux-procfs.o linux-ptrace.o \
- linux-waitpid.o ppc-linux.o linux-personality.o
+ linux-waitpid.o ppc-linux.o linux-personality.o \
+ linux-namespaces.o
NAT_CDEPS = $(srcdir)/proc-service.list

# The PowerPC has severe limitations on TOC size, and uses them even
diff --git a/gdb/config/powerpc/spu-linux.mh b/gdb/config/powerpc/spu-linux.mh
index d44aeeb..9205b62 100644
--- a/gdb/config/powerpc/spu-linux.mh
+++ b/gdb/config/powerpc/spu-linux.mh
@@ -4,5 +4,5 @@
# PPU side of the Cell BE and debugging the SPU side.

NATDEPFILES = spu-linux-nat.o fork-child.o inf-ptrace.o \
- linux-procfs.o linux-ptrace.o linux-waitpid.o linux-personality.o
-
+ linux-procfs.o linux-ptrace.o linux-waitpid.o \
+ linux-personality.o linux-namespaces.o
diff --git a/gdb/config/s390/linux.mh b/gdb/config/s390/linux.mh
index e1ad899..4a137cd 100644
--- a/gdb/config/s390/linux.mh
+++ b/gdb/config/s390/linux.mh
@@ -4,6 +4,6 @@ NATDEPFILES= inf-ptrace.o fork-child.o s390-linux-nat.o \
linux-thread-db.o proc-service.o \
linux-nat.o linux-osdata.o linux-fork.o linux-procfs.o linux-ptrace.o \
linux-personality.o \
- linux-waitpid.o
+ linux-waitpid.o linux-namespaces.o
NAT_CDEPS = $(srcdir)/proc-service.list
LOADLIBES = -ldl $(RDYNAMIC)
diff --git a/gdb/config/sparc/linux.mh b/gdb/config/sparc/linux.mh
index bd7fc86..385f640 100644
--- a/gdb/config/sparc/linux.mh
+++ b/gdb/config/sparc/linux.mh
@@ -5,7 +5,7 @@ NATDEPFILES= sparc-nat.o sparc-linux-nat.o \
proc-service.o linux-thread-db.o \
linux-nat.o linux-osdata.o linux-fork.o \
linux-procfs.o linux-ptrace.o linux-waitpid.o \
- linux-personality.o
+ linux-personality.o linux-namespaces.o
NAT_CDEPS = $(srcdir)/proc-service.list

# The dynamically loaded libthread_db needs access to symbols in the
diff --git a/gdb/config/sparc/linux64.mh b/gdb/config/sparc/linux64.mh
index 86f984f..8df0de1 100644
--- a/gdb/config/sparc/linux64.mh
+++ b/gdb/config/sparc/linux64.mh
@@ -5,7 +5,7 @@ NATDEPFILES= sparc-nat.o sparc64-nat.o sparc64-linux-nat.o \
proc-service.o linux-thread-db.o \
linux-nat.o linux-osdata.o linux-fork.o \
linux-procfs.o linux-ptrace.o linux-waitpid.o \
- linux-personality.o
+ linux-personality.o linux-namespaces.o
NAT_CDEPS = $(srcdir)/proc-service.list

# The dynamically loaded libthread_db needs access to symbols in the
diff --git a/gdb/config/tilegx/linux.mh b/gdb/config/tilegx/linux.mh
index b5edcd4..ec648d3 100644
--- a/gdb/config/tilegx/linux.mh
+++ b/gdb/config/tilegx/linux.mh
@@ -6,7 +6,7 @@ NATDEPFILES= inf-ptrace.o fork-child.o \
proc-service.o linux-thread-db.o \
linux-nat.o linux-osdata.o linux-fork.o \
linux-procfs.o linux-ptrace.o linux-waitpid.o \
- linux-personality.o
+ linux-personality.o linux-namespaces.o

# The dynamically loaded libthread_db needs access to symbols in the
# gdb executable.
diff --git a/gdb/config/xtensa/linux.mh b/gdb/config/xtensa/linux.mh
index b4e59b3..d5b8f91 100644
--- a/gdb/config/xtensa/linux.mh
+++ b/gdb/config/xtensa/linux.mh
@@ -5,7 +5,7 @@ NAT_FILE= config/nm-linux.h
NATDEPFILES= inf-ptrace.o fork-child.o xtensa-linux-nat.o \
linux-thread-db.o proc-service.o \
linux-nat.o linux-osdata.o linux-fork.o linux-procfs.o linux-ptrace.o \
- linux-waitpid.o linux-personality.o
+ linux-waitpid.o linux-personality.o linux-namespaces.o
NAT_CDEPS = $(srcdir)/proc-service.list

LOADLIBES = -ldl $(RDYNAMIC)
diff --git a/gdb/configure b/gdb/configure
index 2baf6dd..5100921 100755
--- a/gdb/configure
+++ b/gdb/configure
@@ -10957,7 +10957,7 @@ for ac_func in getauxval getrusage getuid getgid \
sigaction sigprocmask sigsetmask socketpair \
ttrace wborder wresize setlocale iconvlist libiconvlist btowc \
setrlimit getrlimit posix_madvise waitpid \
- ptrace64 sigaltstack mkdtemp
+ ptrace64 sigaltstack mkdtemp setns
do :
as_ac_var=`$as_echo "ac_cv_func_$ac_func" | $as_tr_sh`
ac_fn_c_check_func "$LINENO" "$ac_func" "$as_ac_var"
diff --git a/gdb/configure.ac b/gdb/configure.ac
index c703e35..2035a37 100644
--- a/gdb/configure.ac
+++ b/gdb/configure.ac
@@ -1331,7 +1331,7 @@ AC_CHECK_FUNCS([getauxval getrusage getuid getgid \
sigaction sigprocmask sigsetmask socketpair \
ttrace wborder wresize setlocale iconvlist libiconvlist btowc \
setrlimit getrlimit posix_madvise waitpid \
- ptrace64 sigaltstack mkdtemp])
+ ptrace64 sigaltstack mkdtemp setns])
AM_LANGINFO_CODESET
GDB_AC_COMMON

diff --git a/gdb/gdbserver/Makefile.in b/gdb/gdbserver/Makefile.in
index 8ba88d4..4179cd6 100644
--- a/gdb/gdbserver/Makefile.in
+++ b/gdb/gdbserver/Makefile.in
@@ -180,7 +180,8 @@ SFILES= $(srcdir)/gdbreplay.c $(srcdir)/inferiors.c $(srcdir)/dll.c \
$(srcdir)/common/common-debug.c $(srcdir)/common/cleanups.c \
$(srcdir)/common/common-exceptions.c $(srcdir)/symbol.c \
$(srcdir)/common/btrace-common.c \
- $(srcdir)/common/fileio.c $(srcdir)/common/cleanup-utils.c
+ $(srcdir)/common/fileio.c $(srcdir)/common/cleanup-utils.c \
+ $(srcdir)/nat/linux-namespaces.c

DEPFILES = @GDBSERVER_DEPFILES@

@@ -619,6 +620,9 @@ x86-linux-dregs.o: ../nat/x86-linux-dregs.c
cleanup-utils.o: ../common/cleanup-utils.c
$(COMPILE) $<
$(POSTCOMPILE)
+linux-namespaces.o: ../nat/linux-namespaces.c
+ $(COMPILE) $<
+ $(POSTCOMPILE)

aarch64.c : $(srcdir)/../regformats/aarch64.dat $(regdat_sh)
$(SHELL) $(regdat_sh) $(srcdir)/../regformats/aarch64.dat aarch64.c
diff --git a/gdb/gdbserver/config.in b/gdb/gdbserver/config.in
index 3c3bfca..893aaab 100644
--- a/gdb/gdbserver/config.in
+++ b/gdb/gdbserver/config.in
@@ -183,6 +183,9 @@
/* Define to 1 if you have the `pwrite' function. */
#undef HAVE_PWRITE

+/* Define to 1 if you have the `setns' function. */
+#undef HAVE_SETNS
+
/* Define to 1 if you have the <sgtty.h> header file. */
#undef HAVE_SGTTY_H

diff --git a/gdb/gdbserver/configure b/gdb/gdbserver/configure
index 75860dd..bcb3f2e 100755
--- a/gdb/gdbserver/configure
+++ b/gdb/gdbserver/configure
@@ -5310,7 +5310,7 @@ fi

done

-for ac_func in getauxval pread pwrite pread64
+for ac_func in getauxval pread pwrite pread64 setns
do :
as_ac_var=`$as_echo "ac_cv_func_$ac_func" | $as_tr_sh`
ac_fn_c_check_func "$LINENO" "$ac_func" "$as_ac_var"
diff --git a/gdb/gdbserver/configure.ac b/gdb/gdbserver/configure.ac
index 34af269..2aef2b4 100644
--- a/gdb/gdbserver/configure.ac
+++ b/gdb/gdbserver/configure.ac
@@ -94,7 +94,7 @@ AC_CHECK_HEADERS(sgtty.h termio.h termios.h sys/reg.h string.h dnl
fcntl.h signal.h sys/file.h dnl
sys/ioctl.h netinet/in.h sys/socket.h netdb.h dnl
netinet/tcp.h arpa/inet.h)
-AC_CHECK_FUNCS(getauxval pread pwrite pread64)
+AC_CHECK_FUNCS(getauxval pread pwrite pread64 setns)

GDB_AC_COMMON

diff --git a/gdb/gdbserver/configure.srv b/gdb/gdbserver/configure.srv
index 81dd235..7f89f2f 100644
--- a/gdb/gdbserver/configure.srv
+++ b/gdb/gdbserver/configure.srv
@@ -42,7 +42,7 @@ srv_amd64_linux_xmlfiles="i386/amd64-linux.xml i386/amd64-avx-linux.xml i386/amd

# Linux object files. This is so we don't have to repeat
# these files over and over again.
-srv_linux_obj="linux-low.o linux-osdata.o linux-procfs.o linux-ptrace.o linux-waitpid.o linux-personality.o"
+srv_linux_obj="linux-low.o linux-osdata.o linux-procfs.o linux-ptrace.o linux-waitpid.o linux-personality.o linux-namespaces.o"

# Input is taken from the "${target}" variable.

diff --git a/gdb/nat/linux-namespaces.c b/gdb/nat/linux-namespaces.c
new file mode 100644
index 0000000..f6fe863
--- /dev/null
+++ b/gdb/nat/linux-namespaces.c
@@ -0,0 +1,231 @@
+/* Linux namespaces(7) support.
+
+ Copyright (C) 2015 Free Software Foundation, Inc.
+
+ This file is part of GDB.
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>. */
+
+#include "common-defs.h"
+#include "nat/linux-namespaces.h"
+#include "filestuff.h"
+#include <fcntl.h>
+#include <sys/syscall.h>
+#include <sys/stat.h>
+
+/* Handle systems with __NR_setns but no setns. */
+#if defined(__NR_setns) && !defined(HAVE_SETNS)
+static int
+setns (int fd, int nstype)
+{
+ return syscall (__NR_setns, fd, nstype);
+}
+#define HAVE_SETNS 1
+#endif /* defined(__NR_setns) && !defined(HAVE_SETNS) */
+
+#ifdef HAVE_SETNS
+
+/* Return the path of the TYPE namespace entry for process PID.
+ The returned value persists until this function is next called. */
+
+static const char *
+linux_ns_path_for (int pid, const char *type)
+{
+ static char path[PATH_MAX];
+
+ xsnprintf (path, sizeof (path), "/proc/%d/ns/%s", pid, type);
+
+ return path;
+}
+
+/* See nat/linux-namespaces.h. */
+
+int
+linux_ns_same (int pid1, int pid2, const char *type)
+{
+ const char *path;
+ struct stat sb;
+ ino_t pid1_id;
+
+ if (pid1 == pid2)
+ return 1;
+
+ /* Lack of kernel support for TYPE namespaces is detected by
+ attempting to open our own. Try to make PID1 be our own
+ PID so we don't have to do extra checking if the the first
+ stat fails. */
+ if (pid2 == getpid ())
+ {
+ int swap = pid1;
+
+ pid1 = pid2;
+ pid2 = swap;
+ }
+
+ path = linux_ns_path_for (pid1, type);
+ if (stat (path, &sb) != 0)
+ {
+ int saved_errno;
+
+ if (pid1 == getpid ())
+ {
+ /* Assume the kernel does not support TYPE namespaces. */
+ return 1;
+ }
+
+ saved_errno = errno;
+ path = linux_ns_path_for (getpid (), type);
+ if (stat (path, &sb) != 0)
+ {
+ /* Assume the kernel does not support TYPE namespaces. */
+ return 1;
+ }
+
+ /* We can open our own TYPE namespace but not that for process
+ PID. The process might have died, or we might not have the
+ right permissions (though we should be attached by this time
+ so this seems unlikely). In any event, we cannot make any
+ decisions and must throw. */
+ errno = saved_errno;
+ perror_with_name (linux_ns_path_for (pid1, type));
+ }
+ pid1_id = sb.st_ino;
+
+ /* The kernel definitely supports TYPE namespaces so we cannot
+ make any decisions if this stat fails. */
+ path = linux_ns_path_for (pid2, type);
+ if (stat (path, &sb) != 0)
+ perror_with_name (path);
+
+ return sb.st_ino == pid1_id;
+}
+
+/* Helper function which does the work for make_cleanup_setns. */
+
+static void
+do_setns_cleanup (void *arg)
+{
+ int *fd = arg;
+
+ if (setns (*fd, 0) != 0)
+ internal_error (__FILE__, __LINE__,
+ _("unable to restore namespace: %s"),
+ safe_strerror (errno));
+}
+
+/* Return a new cleanup that calls setns on FD. */
+
+static struct cleanup *
+make_cleanup_setns (int fd)
+{
+ int *saved_fd = xmalloc (sizeof (fd));
+
+ *saved_fd = fd;
+ return make_cleanup_dtor (do_setns_cleanup, saved_fd, xfree);
+}
+
+
+/* See nat/linux-namespaces.h. */
+
+int
+linux_ns_enter (int pid, const char *type,
+ void (*func) (void *), void *arg)
+{
+ struct cleanup *old_chain, *close_pidfd;
+ int our_fd, pid_fd;
+ struct stat sb;
+ ino_t our_id;
+
+ /* Open our TYPE namespace. On failure, assume the kernel does
+ not support TYPE namespaces (so every process is in the same
+ TYPE namespace) and just call FUNC with ARG and return. */
+ our_fd = gdb_open_cloexec (linux_ns_path_for (getpid(), type),
+ O_RDONLY, 0);
+ if (our_fd < 0)
+ {
+ func (arg);
+ return 1;
+ }
+ old_chain = make_cleanup_close (our_fd);
+
+ /* We've successfully opened our own TYPE namespace. The kernel
+ obviously supports TYPE namespaces and any subsequent errors
+ cannot be continued over. */
+
+ if (fstat (our_fd, &sb) != 0)
+ {
+ do_cleanups (old_chain);
+ return 0;
+ }
+ our_id = sb.st_ino;
+
+ pid_fd = gdb_open_cloexec (linux_ns_path_for (pid, type),
+ O_RDONLY, 0);
+ if (pid_fd < 0)
+ {
+ do_cleanups (old_chain);
+ return 0;
+ }
+ close_pidfd = make_cleanup_close (pid_fd);
+
+ if (fstat (pid_fd, &sb) != 0)
+ {
+ do_cleanups (old_chain);
+ return 0;
+ }
+
+ if (sb.st_ino == our_id)
+ {
+ /* The other process has the same TYPE namespace as us. */
+ do_cleanups (old_chain);
+ func (arg);
+ return 1;
+ }
+
+ if (setns (pid_fd, 0) != 0)
+ {
+ do_cleanups (old_chain);
+ return 0;
+ }
+ do_cleanups (close_pidfd);
+ make_cleanup_setns (our_fd);
+
+ func (arg);
+
+ do_cleanups (old_chain);
+ return 1;
+}
+
+#else /* HAVE_SETNS */
+
+/* See nat/linux-namespaces.h. */
+
+int
+linux_ns_same (int pid1, int pid2, const char *type)
+{
+ return 1;
+}
+
+/* See nat/linux-namespaces.h. */
+
+int
+linux_ns_enter (int pid, const char *type,
+ void (*func) (void *), void *arg)
+{
+ func (arg);
+
+ return 1;
+}
+
+#endif /* HAVE_SETNS */
diff --git a/gdb/nat/linux-namespaces.h b/gdb/nat/linux-namespaces.h
new file mode 100644
index 0000000..0e94c07
--- /dev/null
+++ b/gdb/nat/linux-namespaces.h
@@ -0,0 +1,61 @@
+/* Linux namespaces(7) support.
+
+ Copyright (C) 2015 Free Software Foundation, Inc.
+
+ This file is part of GDB.
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>. */
+
+#ifndef LINUX_NAMESPACES_H
+#define LINUX_NAMESPACES_H
+
+/* Filenames of Linux namespaces in /proc/PID/ns. */
+
+/* IPC namespace: System V IPC, POSIX message queues. */
+#define LINUX_NS_IPC "ipc"
+
+/* Mount namespace: mount points. */
+#define LINUX_NS_MNT "mnt"
+
+/* Network namespace: network devices, stacks, ports, etc. */
+#define LINUX_NS_NET "net"
+
+/* PID namespace: process IDs. */
+#define LINUX_NS_PID "pid"
+
+/* User namespace: user and group IDs. */
+#define LINUX_NS_USER "user"
+
+/* UTS namespace: hostname and NIS domain name. */
+#define LINUX_NS_UTS "uts"
+
+
+/* Return nonzero if processes PID1 and PID2 have the same TYPE
+ namespace, or if the kernel does not support TYPE namespaces
+ (in which case there is only one TYPE namespace). Return zero
+ if the kernel supports TYPE namespaces and the two processes
+ have different TYPE namespaces. */
+
+extern int linux_ns_same (int pid1, int pid2, const char *type);
+
+/* Enter the TYPE namespace of process PID and call FUNC with the
+ argument ARG, returning to the original TYPE namespace afterwards.
+ If process PID has the same TYPE namespace as the current process,
+ or if TYPE namespaces are not supported, just call FUNC with ARG.
+ Return nonzero if FUNC was called, zero otherwise (and set ERRNO). */
+
+extern int linux_ns_enter (int pid, const char *type,
+ void (*func) (void *), void *arg);
+
+#endif /* LINUX_NAMESPACES_H */
--
1.7.1