This is the mail archive of the
gdb-patches@sourceware.org
mailing list for the GDB project.
Re: [PATCH 3/7] Introduce nat/linux-namespaces.[ch]
- From: Doug Evans <dje at google dot com>
- To: Gary Benson <gbenson at redhat dot com>
- Cc: gdb-patches <gdb-patches at sourceware dot org>
- Date: Thu, 16 Apr 2015 21:26:32 -0700
- Subject: Re: [PATCH 3/7] Introduce nat/linux-namespaces.[ch]
- Authentication-results: sourceware.org; auth=none
- References: <1429186791-6867-1-git-send-email-gbenson at redhat dot com> <1429186791-6867-4-git-send-email-gbenson at redhat dot com>
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]