[3/7] Introduce nat/linux-namespaces.[ch]
Commit Message
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
Comments
On Thu, Apr 16, 2015 at 5:19 AM, Gary Benson <gbenson@redhat.com> wrote:
> 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.
> ...
> 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) */
Nit:
I understand the goal here, but I think it would be better to do this
slightly differently.
The value of HAVE_SETNS is potentially different in this file than for
the rest of gdb,
and I generally think of config.h as being consistent across the app.
I don't have a strong preference on how to change this, other than I
think values
defined in config.h should be left alone.
E.g., one could just just define another macro locally here.
#if defined(__NR_setns) || defined(HAVE_SETNS)
#define REALLY_HAVE_SETNS 1
#endif
#ifdef REALLY_HAVE_SETNS
... the rest of the file ...
or some such.
> +
> +#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 */
GNU coding convention says to write this as /* not HAVE_SETNS */
though I think /* !HAVE_SETNS */ is ok too.
http://www.gnu.org/prep/standards/standards.html#Comments
[I like this convention.]
> +
> +/* 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 */
/* !HAVE_SETNS */ or /* not 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"
> +
extra blank line
> +
> +/* 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 */
Otherwise LGTM.
[still reading patches in order though]
Doug Evans wrote:
> On Thu, Apr 16, 2015 at 5:19 AM, Gary Benson <gbenson@redhat.com> wrote:
> > 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 @@
[snip]
> > +#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) */
>
> Nit:
> I understand the goal here, but I think it would be better to do
> this slightly differently.
> The value of HAVE_SETNS is potentially different in this file than
> for the rest of gdb,
> and I generally think of config.h as being consistent across the app.
> I don't have a strong preference on how to change this, other than I
> think values defined in config.h should be left alone.
The value of HAVE_SETNS is potentially different in this file than for
the rest of gdb, but so is the existence of a setns function. That's
why I did it that way. I certainly didn't want to make our own setns
available to any other file, as really anything accessing namespaces
should go through this file.
> E.g., one could just just define another macro locally here.
>
> #if defined(__NR_setns) || defined(HAVE_SETNS)
> #define REALLY_HAVE_SETNS 1
> #endif
>
> #ifdef REALLY_HAVE_SETNS
> ... the rest of the file ...
>
> or some such.
I could do that (I'd probably call it HAVE_LINUX_NAMESPACES).
I prefer the way I did it but I won't fight this.
Cheers,
Gary
On 04/16/2015 01:19 PM, Gary Benson wrote:
> + 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;
Don't assume any function preserves errno. Save this right after
stat.
> + 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));
And here
if (setns (*fd, 0) != 0)
{
int saved_errno = errno;
internal_error (__FILE__, __LINE__,
_("unable to restore namespace: %s"),
safe_strerror (saved_errno));
}
because _() is a function call, and you can't rely on
whether safe_strerror is called before or after.
> +/* 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);
So the function:
#1 - enters the namespace
#2 - calls func
#3 - exits the namespace.
IMO, "linux_ns_ENTER" isn't a good name for that. I'd expect that a function
called "enter" do just #1 above. Something like "linux_ns_do",
"linux_do_in_ns", "linux_in_ns", etc., would be clearer, IMO.
Thanks,
Pedro Alves
On Fri, Apr 17, 2015 at 7:52 AM, Pedro Alves <palves@redhat.com> wrote:
>> +/* 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);
>
>
> So the function:
>
> #1 - enters the namespace
> #2 - calls func
> #3 - exits the namespace.
>
> IMO, "linux_ns_ENTER" isn't a good name for that. I'd expect that a function
> called "enter" do just #1 above. Something like "linux_ns_do",
> "linux_do_in_ns", "linux_in_ns", etc., would be clearer, IMO.
with_linux_ns ?
full disclosure: borrowed from Scheme
/ducks
Doug Evans wrote:
> On Fri, Apr 17, 2015 at 7:52 AM, Pedro Alves <palves@redhat.com> wrote:
> > > +/* 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);
> >
> >
> > So the function:
> >
> > #1 - enters the namespace
> > #2 - calls func
> > #3 - exits the namespace.
> >
> > IMO, "linux_ns_ENTER" isn't a good name for that. I'd expect that a function
> > called "enter" do just #1 above. Something like "linux_ns_do",
> > "linux_do_in_ns", "linux_in_ns", etc., would be clearer, IMO.
>
> with_linux_ns ?
>
> full disclosure: borrowed from Scheme
> /ducks
Works for me.
Cheers,
Gary
@@ -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
#
@@ -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
@@ -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)
@@ -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
@@ -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)
@@ -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
@@ -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
@@ -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)
@@ -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)
@@ -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
@@ -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)
@@ -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)
@@ -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)
@@ -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
@@ -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
@@ -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)
@@ -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
@@ -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
@@ -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.
@@ -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)
@@ -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"
@@ -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
@@ -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
@@ -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
@@ -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"
@@ -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
@@ -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.
new file mode 100644
@@ -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 */
new file mode 100644
@@ -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 */