[4/7] Remove linux_proc_pid_get_ns

Message ID 1429186791-6867-5-git-send-email-gbenson@redhat.com
State Superseded
Headers

Commit Message

Gary Benson April 16, 2015, 12:19 p.m. UTC
  This commit removes linux_proc_pid_get_ns, and updates its only
use to use linux_ns_same instead.

gdb/ChangeLog:

	* linux-thread-db.c (nat/linux-namespaces.h): New include.
	(check_pid_namespace_match): Use linux_ns_same rather than
	linux_proc_pid_get_ns to spot PID namespace mismatches.
	* nat/linux-procfs.h (linux_proc_pid_get_ns): Remove.
	* nat/linux-procfs.c (linux_proc_pid_get_ns): Likewise.
---
 gdb/ChangeLog          |    8 ++++++++
 gdb/linux-thread-db.c  |   13 +++----------
 gdb/nat/linux-procfs.c |   19 -------------------
 gdb/nat/linux-procfs.h |    6 ------
 4 files changed, 11 insertions(+), 35 deletions(-)
  

Comments

Doug Evans April 17, 2015, 4:35 a.m. UTC | #1
On Thu, Apr 16, 2015 at 5:19 AM, Gary Benson <gbenson@redhat.com> wrote:
> This commit removes linux_proc_pid_get_ns, and updates its only
> use to use linux_ns_same instead.
>
> gdb/ChangeLog:
>
>         * linux-thread-db.c (nat/linux-namespaces.h): New include.
>         (check_pid_namespace_match): Use linux_ns_same rather than
>         linux_proc_pid_get_ns to spot PID namespace mismatches.
>         * nat/linux-procfs.h (linux_proc_pid_get_ns): Remove.
>         * nat/linux-procfs.c (linux_proc_pid_get_ns): Likewise.

LGTM.
one nit below

> diff --git a/gdb/nat/linux-procfs.c b/gdb/nat/linux-procfs.c
> index 44364c5..24bcb01 100644
> --- a/gdb/nat/linux-procfs.c
> +++ b/gdb/nat/linux-procfs.c
> @@ -185,25 +185,6 @@ linux_proc_pid_is_zombie (pid_t pid)
>    return linux_proc_pid_is_zombie_maybe_warn (pid, 1);
>  }
>
> -/* See linux-procfs.h declaration.  */
> -
> -char *
> -linux_proc_pid_get_ns (pid_t pid, const char *ns)
> -{
> -  char buf[100];
> -  char nsval[64];
> -  int ret;
> -  xsnprintf (buf, sizeof (buf), "/proc/%d/ns/%s", (int) pid, ns);
> -  ret = readlink (buf, nsval, sizeof (nsval));
> -  if (0 < ret && ret < sizeof (nsval))
> -    {
> -      nsval[ret] = '\0';
> -      return xstrdup (nsval);
> -    }
> -
> -  return NULL;
> -}

I see we're using pid_t here.
I think we talked before about a type for pids.
Since this is all linux specific code maybe use pid_t more in this patch set?
  
Gary Benson April 17, 2015, 1:44 p.m. UTC | #2
Doug Evans wrote:
> On Thu, Apr 16, 2015 at 5:19 AM, Gary Benson <gbenson@redhat.com> wrote:
> > This commit removes linux_proc_pid_get_ns, and updates its only
> > use to use linux_ns_same instead.
> >
> > gdb/ChangeLog:
> >
> >         * linux-thread-db.c (nat/linux-namespaces.h): New include.
> >         (check_pid_namespace_match): Use linux_ns_same rather than
> >         linux_proc_pid_get_ns to spot PID namespace mismatches.
> >         * nat/linux-procfs.h (linux_proc_pid_get_ns): Remove.
> >         * nat/linux-procfs.c (linux_proc_pid_get_ns): Likewise.
> 
> LGTM.
> one nit below
[snip]
> I see we're using pid_t here.
> I think we talked before about a type for pids.
> Since this is all linux specific code maybe use pid_t more in this
> patch set?

I could do that.  It would likely only be in linux-namespaces.[ch]
and maybe one or two places in linux-nat.c, all the other stuff is
in the target vectors.

Cheers,
Gary
  

Patch

diff --git a/gdb/linux-thread-db.c b/gdb/linux-thread-db.c
index 886d8ac..74db74d 100644
--- a/gdb/linux-thread-db.c
+++ b/gdb/linux-thread-db.c
@@ -42,9 +42,9 @@ 
 #include "nat/linux-osdata.h"
 #include "auto-load.h"
 #include "cli/cli-utils.h"
-
 #include <signal.h>
 #include <ctype.h>
+#include "nat/linux-namespaces.h"
 
 /* GNU/Linux libthread_db support.
 
@@ -1211,20 +1211,13 @@  check_pid_namespace_match (void)
 	 child's thread list, we'll mistakenly think it has no threads
 	 since the thread PID fields won't match the PID we give to
 	 libthread_db.  */
-      char *our_pid_ns = linux_proc_pid_get_ns (getpid (), "pid");
-      char *inferior_pid_ns = linux_proc_pid_get_ns (
-	ptid_get_pid (inferior_ptid), "pid");
-
-      if (our_pid_ns != NULL && inferior_pid_ns != NULL
-	  && strcmp (our_pid_ns, inferior_pid_ns) != 0)
+      if (!linux_ns_same (getpid (), ptid_get_pid (inferior_ptid),
+			  LINUX_NS_PID))
 	{
 	  warning (_ ("Target and debugger are in different PID "
 		      "namespaces; thread lists and other data are "
 		      "likely unreliable"));
 	}
-
-      xfree (our_pid_ns);
-      xfree (inferior_pid_ns);
     }
 }
 
diff --git a/gdb/nat/linux-procfs.c b/gdb/nat/linux-procfs.c
index 44364c5..24bcb01 100644
--- a/gdb/nat/linux-procfs.c
+++ b/gdb/nat/linux-procfs.c
@@ -185,25 +185,6 @@  linux_proc_pid_is_zombie (pid_t pid)
   return linux_proc_pid_is_zombie_maybe_warn (pid, 1);
 }
 
-/* See linux-procfs.h declaration.  */
-
-char *
-linux_proc_pid_get_ns (pid_t pid, const char *ns)
-{
-  char buf[100];
-  char nsval[64];
-  int ret;
-  xsnprintf (buf, sizeof (buf), "/proc/%d/ns/%s", (int) pid, ns);
-  ret = readlink (buf, nsval, sizeof (nsval));
-  if (0 < ret && ret < sizeof (nsval))
-    {
-      nsval[ret] = '\0';
-      return xstrdup (nsval);
-    }
-
-  return NULL;
-}
-
 /* See linux-procfs.h.  */
 
 void
diff --git a/gdb/nat/linux-procfs.h b/gdb/nat/linux-procfs.h
index fdbf383..f9cad39 100644
--- a/gdb/nat/linux-procfs.h
+++ b/gdb/nat/linux-procfs.h
@@ -54,12 +54,6 @@  extern int linux_proc_pid_is_zombie_nowarn (pid_t pid);
 
 extern int linux_proc_pid_is_gone (pid_t pid);
 
-/* Return an opaque string identifying PID's NS namespace or NULL if
- * the information is unavailable.  The returned string must be
- * released with xfree.  */
-
-extern char *linux_proc_pid_get_ns (pid_t pid, const char *ns);
-
 /* Callback function for linux_proc_attach_tgid_threads.  If the PTID
    thread is not yet known, try to attach to it and return true,
    otherwise return false.  */