Blame SOURCES/0136-Use-get_proc_pid-for-proc-paths.patch

c5cada
From bba566504901b2c07885ecf325829875a96381a7 Mon Sep 17 00:00:00 2001
c5cada
From: =?UTF-8?q?=C3=81kos=20Uzonyi?= <uzonyi.akos@gmail.com>
c5cada
Date: Tue, 30 Jun 2020 17:20:12 +0200
c5cada
Subject: [PATCH 136/138] Use get_proc_pid for /proc paths
c5cada
c5cada
* mmap_cache.c (mmap_cache_rebuild_if_invalid): Use proc pid instead of
c5cada
tcp->pid for /proc path.
c5cada
* util.c (getfdproto): Likewise.
c5cada
(pidfd_get_pid): Likewise.
c5cada
* pathtrace.c (getfdpath_pid): Likewise.
c5cada
* strace.c (attach_tcb): Likewise.
c5cada
---
c5cada
 mmap_cache.c | 2 +-
c5cada
 pathtrace.c  | 7 ++++++-
c5cada
 strace.c     | 2 +-
c5cada
 util.c       | 9 +++++++--
c5cada
 4 files changed, 15 insertions(+), 5 deletions(-)
c5cada
c5cada
diff --git a/mmap_cache.c b/mmap_cache.c
c5cada
index 89c6225..9825df2 100644
c5cada
--- a/mmap_cache.c
c5cada
+++ b/mmap_cache.c
c5cada
@@ -84,7 +84,7 @@ mmap_cache_rebuild_if_invalid(struct tcb *tcp, const char *caller)
c5cada
 		return MMAP_CACHE_REBUILD_READY;
c5cada
 
c5cada
 	char filename[sizeof("/proc/4294967296/maps")];
c5cada
-	xsprintf(filename, "/proc/%u/maps", tcp->pid);
c5cada
+	xsprintf(filename, "/proc/%u/maps", get_proc_pid(tcp));
c5cada
 
c5cada
 	FILE *fp = fopen_stream(filename, "r");
c5cada
 	if (!fp) {
c5cada
diff --git a/pathtrace.c b/pathtrace.c
c5cada
index f85cf14..87dc64b 100644
c5cada
--- a/pathtrace.c
c5cada
+++ b/pathtrace.c
c5cada
@@ -87,7 +87,12 @@ getfdpath_pid(pid_t pid, int fd, char *buf, unsigned bufsize)
c5cada
 	if (fd < 0)
c5cada
 		return -1;
c5cada
 
c5cada
-	xsprintf(linkpath, "/proc/%u/fd/%u", pid, fd);
c5cada
+	int proc_pid = 0;
c5cada
+	translate_pid(NULL, pid, PT_TID, &proc_pid);
c5cada
+	if (!proc_pid)
c5cada
+		return -1;
c5cada
+
c5cada
+	xsprintf(linkpath, "/proc/%u/fd/%u", proc_pid, fd);
c5cada
 	n = readlink(linkpath, buf, bufsize - 1);
c5cada
 	/*
c5cada
 	 * NB: if buf is too small, readlink doesn't fail,
c5cada
diff --git a/strace.c b/strace.c
c5cada
index 249533e..ef23f08 100644
c5cada
--- a/strace.c
c5cada
+++ b/strace.c
c5cada
@@ -1196,7 +1196,7 @@ attach_tcb(struct tcb *const tcp)
c5cada
 	unsigned int ntid = 0, nerr = 0;
c5cada
 
c5cada
 	if (followfork && tcp->pid != strace_child &&
c5cada
-	    xsprintf(procdir, task_path, tcp->pid) > 0 &&
c5cada
+	    xsprintf(procdir, task_path, get_proc_pid(tcp)) > 0 &&
c5cada
 	    (dir = opendir(procdir)) != NULL) {
c5cada
 		struct_dirent *de;
c5cada
 
c5cada
diff --git a/util.c b/util.c
c5cada
index 2568021..481144b 100644
c5cada
--- a/util.c
c5cada
+++ b/util.c
c5cada
@@ -501,7 +501,7 @@ getfdproto(struct tcb *tcp, int fd)
c5cada
 	if (fd < 0)
c5cada
 		return SOCK_PROTO_UNKNOWN;
c5cada
 
c5cada
-	xsprintf(path, "/proc/%u/fd/%u", tcp->pid, fd);
c5cada
+	xsprintf(path, "/proc/%u/fd/%u", get_proc_pid(tcp), fd);
c5cada
 	r = getxattr(path, "system.sockprotoname", buf, bufsize - 1);
c5cada
 	if (r <= 0)
c5cada
 		return SOCK_PROTO_UNKNOWN;
c5cada
@@ -582,8 +582,13 @@ printdev(struct tcb *tcp, int fd, const char *path)
c5cada
 pid_t
c5cada
 pidfd_get_pid(pid_t pid_of_fd, int fd)
c5cada
 {
c5cada
+	int proc_pid = 0;
c5cada
+	translate_pid(NULL, pid_of_fd, PT_TID, &proc_pid);
c5cada
+	if (!proc_pid)
c5cada
+		return -1;
c5cada
+
c5cada
 	char fdi_path[sizeof("/proc/%u/fdinfo/%u") + 2 * sizeof(int) * 3];
c5cada
-	xsprintf(fdi_path, "/proc/%u/fdinfo/%u", pid_of_fd, fd);
c5cada
+	xsprintf(fdi_path, "/proc/%u/fdinfo/%u", proc_pid, fd);
c5cada
 
c5cada
 	FILE *f = fopen_stream(fdi_path, "r");
c5cada
 	if (!f)
c5cada
-- 
c5cada
2.1.4
c5cada