From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 60510 invoked by alias); 17 Apr 2015 04:26:37 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Received: (qmail 60500 invoked by uid 89); 17 Apr 2015 04:26:37 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.6 required=5.0 tests=AWL,BAYES_00,RCVD_IN_DNSWL_LOW,SPF_PASS,T_RP_MATCHES_RCVD autolearn=ham version=3.3.2 X-HELO: mail-oi0-f47.google.com Received: from mail-oi0-f47.google.com (HELO mail-oi0-f47.google.com) (209.85.218.47) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES128-GCM-SHA256 encrypted) ESMTPS; Fri, 17 Apr 2015 04:26:34 +0000 Received: by oica37 with SMTP id a37so61640834oic.0 for ; Thu, 16 Apr 2015 21:26:33 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:to:cc:content-type; bh=/7LZ0ap4zYUyFloljhaOA8aDbZ81+BJSs1LI2vyJuCQ=; b=Cv1pNfifgMp2sweJaBCGzOZJjqa0dSytTAXLKDW0MhTwBbq+s5V4MJ57pOq4E5nZN8 zzPGB+KzSWQz2P5TxDSusElF84wu9JsAUHCVRrwlTHXp9lOZLtS+5aSN3JEdDRpNhkJ9 1a0C3FMwcnQM3dDRgRolmS0iBvxpapz2ino+M/6KcvDYk1IjUBj0DyvO4npabOGnVZ96 zruGzkuorZOK5GbfJh+G0y/gUo9y7v8u0wcdUz0llWrSXfGCfZDjg5xvcVFNnp+kngNQ 7qCrpj32HdyB7uTAcKqy3hNeB/0/PJ7hIqQq3/divdHrQ6sw+EMZIn2Bf+OWNX7dlEUs 2tCA== X-Gm-Message-State: ALoCoQmeAYtOgr+RbkgEa6dVl1g1aphAH26nALgk5+n7LYcHZ0XrCfd0pXv2mD0UNOqf0g29koPV MIME-Version: 1.0 X-Received: by 10.60.123.40 with SMTP id lx8mr1037362oeb.15.1429244792890; Thu, 16 Apr 2015 21:26:32 -0700 (PDT) Received: by 10.182.103.101 with HTTP; Thu, 16 Apr 2015 21:26:32 -0700 (PDT) In-Reply-To: <1429186791-6867-4-git-send-email-gbenson@redhat.com> References: <1429186791-6867-1-git-send-email-gbenson@redhat.com> <1429186791-6867-4-git-send-email-gbenson@redhat.com> Date: Fri, 17 Apr 2015 04:26:00 -0000 Message-ID: Subject: Re: [PATCH 3/7] Introduce nat/linux-namespaces.[ch] From: Doug Evans To: Gary Benson Cc: gdb-patches Content-Type: text/plain; charset=UTF-8 X-IsSubscribed: yes X-SW-Source: 2015-04/txt/msg00639.txt.bz2 On Thu, Apr 16, 2015 at 5:19 AM, Gary Benson 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 . */ > + > +#include "common-defs.h" > +#include "nat/linux-namespaces.h" > +#include "filestuff.h" > +#include > +#include > +#include > + > +/* 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 . */ > + > +#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]