Blame SOURCES/0180-pathtrace-util-do-not-print-deleted-as-part-of-the-p.patch

d0480e
From 676979fa9cc7920e5e4d547814f9c0edb597fa0d Mon Sep 17 00:00:00 2001
d0480e
From: Eugene Syromyatnikov <evgsyr@gmail.com>
d0480e
Date: Thu, 30 Jun 2022 16:01:05 +0200
d0480e
Subject: [PATCH] pathtrace, util: do not print " (deleted)" as part of the
d0480e
 path
d0480e
d0480e
In order to allow to discern the unlinked paths from the paths that
d0480e
do indeed end with " (deleted)".
d0480e
d0480e
* src/defs.h (getfdpath_pid): Add deleted parameter.
d0480e
(getfdpath): Pass NULL as deleted parameter to getfdpath_pid.
d0480e
* src/largefile_wrappers.h (lstat_file): New macro.
d0480e
* src/pathtrace.c: Include <sys/stat.h>, <sys/types.h>, <unistd.h>,
d0480e
and "largefile_wrappers.h".
d0480e
(getfdpath_pid): Add deleted parameter, check if path ends with
d0480e
" (deleted)", and if it is, try to figure out if it is a part
d0480e
of the path by comparing device/inode numbers of the file procfs
d0480e
link resolves into and the file pointed by the path read;  strip
d0480e
" (deleted)";  set deleted (if it is non-NULL) to true if the fd
d0480e
is turned out to be deleted and to false otherwise.
d0480e
* src/util.c (print_quoted_string_in_angle_brackets): Add deleted
d0480e
parameter, print "(deleted)" after the closing angle bracket if it is
d0480e
non-NULL.
d0480e
(printfd_pid): Add deleted local variable, pass it to getfdpath_pid
d0480e
and print_quoted_string_in_angle_brackets calls.
d0480e
* tests/fchmod.c: Add checks for a file with " (deleted)" in the path,
d0480e
update expected output.
d0480e
* NEWS: Mention the change.
d0480e
---
d0480e
 NEWS                     |  5 +++++
d0480e
 src/defs.h               |  5 +++--
d0480e
 src/largefile_wrappers.h |  2 ++
d0480e
 src/pathtrace.c          | 48 +++++++++++++++++++++++++++++++++++++++++++++---
d0480e
 src/util.c               | 10 +++++++---
d0480e
 tests/fchmod.c           | 47 +++++++++++++++++++++++++++++++++++++++++++----
d0480e
 6 files changed, 105 insertions(+), 12 deletions(-)
d0480e
d0480e
Index: strace-5.18/NEWS
d0480e
===================================================================
d0480e
--- strace-5.18.orig/NEWS	2022-07-13 12:52:48.219784860 +0200
d0480e
+++ strace-5.18/NEWS	2022-07-13 12:52:48.451782122 +0200
d0480e
@@ -1,6 +1,11 @@
d0480e
 Noteworthy changes in release 5.18 (2022-06-18)
d0480e
 ===============================================
d0480e
 
d0480e
+* Changes in behaviour
d0480e
+  * The "(deleted)" marker for unlinked paths of file descriptors is now printed
d0480e
+    outside angle brackets;  the matching of unlinked paths of file descriptors
d0480e
+    no longer includes the " (deleted)" part into consideration.
d0480e
+
d0480e
 * Improvements
d0480e
   * Added an interface of raising des Strausses awareness.
d0480e
   * Added --tips option to print strace tips, tricks, and tweaks
d0480e
Index: strace-5.18/src/defs.h
d0480e
===================================================================
d0480e
--- strace-5.18.orig/src/defs.h	2022-07-13 12:52:29.405006910 +0200
d0480e
+++ strace-5.18/src/defs.h	2022-07-13 12:52:54.532710356 +0200
d0480e
@@ -785,12 +785,13 @@
d0480e
 	return pathtrace_match_set(tcp, &global_path_set);
d0480e
 }
d0480e
 
d0480e
-extern int getfdpath_pid(pid_t pid, int fd, char *buf, unsigned bufsize);
d0480e
+extern int getfdpath_pid(pid_t pid, int fd, char *buf, unsigned bufsize,
d0480e
+			 bool *deleted);
d0480e
 
d0480e
 static inline int
d0480e
 getfdpath(struct tcb *tcp, int fd, char *buf, unsigned bufsize)
d0480e
 {
d0480e
-	return getfdpath_pid(tcp->pid, fd, buf, bufsize);
d0480e
+	return getfdpath_pid(tcp->pid, fd, buf, bufsize, NULL);
d0480e
 }
d0480e
 
d0480e
 extern unsigned long getfdinode(struct tcb *, int);
d0480e
Index: strace-5.18/src/largefile_wrappers.h
d0480e
===================================================================
d0480e
--- strace-5.18.orig/src/largefile_wrappers.h	2022-07-13 12:52:29.405006910 +0200
d0480e
+++ strace-5.18/src/largefile_wrappers.h	2022-07-13 12:52:48.451782122 +0200
d0480e
@@ -31,6 +31,7 @@
d0480e
 #  endif
d0480e
 #  define fstat_fd fstat64
d0480e
 #  define strace_stat_t struct stat64
d0480e
+#  define lstat_file lstat64
d0480e
 #  define stat_file stat64
d0480e
 #  define struct_dirent struct dirent64
d0480e
 #  define read_dir readdir64
d0480e
@@ -42,6 +43,7 @@
d0480e
 #  define fcntl_fd fcntl
d0480e
 #  define fstat_fd fstat
d0480e
 #  define strace_stat_t struct stat
d0480e
+#  define lstat_file lstat
d0480e
 #  define stat_file stat
d0480e
 #  define struct_dirent struct dirent
d0480e
 #  define read_dir readdir
d0480e
Index: strace-5.18/src/pathtrace.c
d0480e
===================================================================
d0480e
--- strace-5.18.orig/src/pathtrace.c	2022-07-13 12:52:29.405006910 +0200
d0480e
+++ strace-5.18/src/pathtrace.c	2022-07-13 12:52:54.532710356 +0200
d0480e
@@ -10,7 +10,11 @@
d0480e
 #include "defs.h"
d0480e
 #include <limits.h>
d0480e
 #include <poll.h>
d0480e
+#include <sys/stat.h>
d0480e
+#include <sys/types.h>
d0480e
+#include <unistd.h>
d0480e
 
d0480e
+#include "largefile_wrappers.h"
d0480e
 #include "number_set.h"
d0480e
 #include "sen.h"
d0480e
 #include "xstring.h"
d0480e
@@ -77,7 +81,7 @@
d0480e
  * Get path associated with fd of a process with pid.
d0480e
  */
d0480e
 int
d0480e
-getfdpath_pid(pid_t pid, int fd, char *buf, unsigned bufsize)
d0480e
+getfdpath_pid(pid_t pid, int fd, char *buf, unsigned bufsize, bool *deleted)
d0480e
 {
d0480e
 	char linkpath[sizeof("/proc/%u/fd/%u") + 2 * sizeof(int)*3];
d0480e
 	ssize_t n;
d0480e
@@ -91,12 +95,50 @@
d0480e
 
d0480e
 	xsprintf(linkpath, "/proc/%u/fd/%u", proc_pid, fd);
d0480e
 	n = readlink(linkpath, buf, bufsize - 1);
d0480e
+	if (n < 0)
d0480e
+		goto end;
d0480e
+
d0480e
 	/*
d0480e
 	 * NB: if buf is too small, readlink doesn't fail,
d0480e
 	 * it returns truncated result (IOW: n == bufsize - 1).
d0480e
 	 */
d0480e
-	if (n >= 0)
d0480e
-		buf[n] = '\0';
d0480e
+	buf[n] = '\0';
d0480e
+	if (deleted)
d0480e
+		*deleted = false;
d0480e
+
d0480e
+	/*
d0480e
+	 * Try to figure out if the kernel has appended " (deleted)"
d0480e
+	 * to the end of a potentially unlinked path and set deleted
d0480e
+	 * if it is the case.
d0480e
+	 */
d0480e
+	static const char del_sfx[] = " (deleted)";
d0480e
+	if ((size_t) n <= sizeof(del_sfx))
d0480e
+		goto end;
d0480e
+
d0480e
+	char *del = buf + n + 1 - sizeof(del_sfx);
d0480e
+
d0480e
+	if (memcmp(del, del_sfx, sizeof(del_sfx)))
d0480e
+		goto end;
d0480e
+
d0480e
+	strace_stat_t st_link;
d0480e
+	strace_stat_t st_path;
d0480e
+	int rc = stat_file(linkpath, &st_link);
d0480e
+
d0480e
+	if (rc)
d0480e
+		goto end;
d0480e
+
d0480e
+	rc = lstat_file(buf, &st_path);
d0480e
+
d0480e
+	if (rc ||
d0480e
+	    (st_link.st_ino != st_path.st_ino) ||
d0480e
+	    (st_link.st_dev != st_path.st_dev)) {
d0480e
+		*del = '\0';
d0480e
+		n = del - buf + 1;
d0480e
+		if (deleted)
d0480e
+			*deleted = true;
d0480e
+	}
d0480e
+
d0480e
+end:
d0480e
 	return n;
d0480e
 }
d0480e
 
d0480e
Index: strace-5.18/src/util.c
d0480e
===================================================================
d0480e
--- strace-5.18.orig/src/util.c	2022-07-13 12:52:47.989787575 +0200
d0480e
+++ strace-5.18/src/util.c	2022-07-13 12:52:48.452782111 +0200
d0480e
@@ -735,12 +735,15 @@
d0480e
 }
d0480e
 
d0480e
 static void
d0480e
-print_quoted_string_in_angle_brackets(const char *str)
d0480e
+print_quoted_string_in_angle_brackets(const char *str, const bool deleted)
d0480e
 {
d0480e
 	tprints("<");
d0480e
 	print_quoted_string_ex(str, strlen(str),
d0480e
 			       QUOTE_OMIT_LEADING_TRAILING_QUOTES, "<>");
d0480e
 	tprints(">");
d0480e
+
d0480e
+	if (deleted)
d0480e
+		tprints("(deleted)");
d0480e
 }
d0480e
 
d0480e
 void
d0480e
@@ -749,8 +752,9 @@
d0480e
 	PRINT_VAL_D(fd);
d0480e
 
d0480e
 	char path[PATH_MAX + 1];
d0480e
+	bool deleted;
d0480e
 	if (pid > 0 && !number_set_array_is_empty(decode_fd_set, 0)
d0480e
-	    && getfdpath_pid(pid, fd, path, sizeof(path)) >= 0) {
d0480e
+	    && getfdpath_pid(pid, fd, path, sizeof(path), &deleted) >= 0) {
d0480e
 		if (is_number_in_set(DECODE_FD_SOCKET, decode_fd_set) &&
d0480e
 		    printsocket(tcp, fd, path))
d0480e
 			goto printed;
d0480e
@@ -761,7 +765,7 @@
d0480e
 		    printpidfd(pid, fd, path))
d0480e
 			goto printed;
d0480e
 		if (is_number_in_set(DECODE_FD_PATH, decode_fd_set))
d0480e
-			print_quoted_string_in_angle_brackets(path);
d0480e
+			print_quoted_string_in_angle_brackets(path, deleted);
d0480e
 printed:	;
d0480e
 	}
d0480e
 
d0480e
Index: strace-5.18/tests/fchmod.c
d0480e
===================================================================
d0480e
--- strace-5.18.orig/tests/fchmod.c	2022-07-13 12:52:29.405006910 +0200
d0480e
+++ strace-5.18/tests/fchmod.c	2022-07-13 12:52:48.452782111 +0200
d0480e
@@ -35,10 +35,17 @@
d0480e
 	(void) unlink(sample);
d0480e
 	int fd = open(sample, O_CREAT|O_RDONLY, 0400);
d0480e
 	if (fd == -1)
d0480e
-		perror_msg_and_fail("open");
d0480e
+		perror_msg_and_fail("open(\"%s\")", sample);
d0480e
+
d0480e
+	static const char sample_del[] = "fchmod_sample_file (deleted)";
d0480e
+	(void) unlink(sample_del);
d0480e
+	int fd_del = open(sample_del, O_CREAT|O_RDONLY, 0400);
d0480e
+	if (fd_del == -1)
d0480e
+		perror_msg_and_fail("open(\"%s\")", sample);
d0480e
 
d0480e
 # ifdef YFLAG
d0480e
 	char *sample_realpath = get_fd_path(fd);
d0480e
+	char *sample_del_realpath = get_fd_path(fd_del);
d0480e
 # endif
d0480e
 
d0480e
 	const char *sample_secontext = SECONTEXT_FILE(sample);
d0480e
@@ -56,12 +63,27 @@
d0480e
 	       sample_secontext,
d0480e
 	       sprintrc(rc));
d0480e
 
d0480e
+	const char *sample_del_secontext = SECONTEXT_FILE(sample_del);
d0480e
+	rc = syscall(__NR_fchmod, fd_del, 0600);
d0480e
+# ifdef YFLAG
d0480e
+	printf("%s%s(%d<%s>%s, 0600) = %s\n",
d0480e
+# else
d0480e
+	printf("%s%s(%d%s, 0600) = %s\n",
d0480e
+# endif
d0480e
+	       my_secontext, "fchmod",
d0480e
+	       fd_del,
d0480e
+# ifdef YFLAG
d0480e
+	       sample_del_realpath,
d0480e
+# endif
d0480e
+	       sample_del_secontext,
d0480e
+	       sprintrc(rc));
d0480e
+
d0480e
 	if (unlink(sample))
d0480e
-		perror_msg_and_fail("unlink");
d0480e
+		perror_msg_and_fail("unlink(\"%s\")", sample);
d0480e
 
d0480e
 	rc = syscall(__NR_fchmod, fd, 051);
d0480e
 # ifdef YFLAG
d0480e
-	printf("%s%s(%d<%s (deleted)>%s, 051) = %s\n",
d0480e
+	printf("%s%s(%d<%s>(deleted)%s, 051) = %s\n",
d0480e
 # else
d0480e
 	printf("%s%s(%d%s, 051) = %s\n",
d0480e
 # endif
d0480e
@@ -73,9 +95,26 @@
d0480e
 	       sample_secontext,
d0480e
 	       sprintrc(rc));
d0480e
 
d0480e
+	if (unlink(sample_del))
d0480e
+		perror_msg_and_fail("unlink(\"%s\")", sample_del);
d0480e
+
d0480e
+	rc = syscall(__NR_fchmod, fd_del, 051);
d0480e
+# ifdef YFLAG
d0480e
+	printf("%s%s(%d<%s>(deleted)%s, 051) = %s\n",
d0480e
+# else
d0480e
+	printf("%s%s(%d%s, 051) = %s\n",
d0480e
+# endif
d0480e
+	       my_secontext, "fchmod",
d0480e
+	       fd_del,
d0480e
+# ifdef YFLAG
d0480e
+	       sample_del_realpath,
d0480e
+# endif
d0480e
+	       sample_del_secontext,
d0480e
+	       sprintrc(rc));
d0480e
+
d0480e
 	rc = syscall(__NR_fchmod, fd, 004);
d0480e
 # ifdef YFLAG
d0480e
-	printf("%s%s(%d<%s (deleted)>%s, 004) = %s\n",
d0480e
+	printf("%s%s(%d<%s>(deleted)%s, 004) = %s\n",
d0480e
 # else
d0480e
 	printf("%s%s(%d%s, 004) = %s\n",
d0480e
 # endif
d0480e
Index: strace-5.18/tests-m32/fchmod.c
d0480e
===================================================================
d0480e
--- strace-5.18.orig/tests-m32/fchmod.c	2022-07-13 12:52:29.405006910 +0200
d0480e
+++ strace-5.18/tests-m32/fchmod.c	2022-07-13 12:52:48.452782111 +0200
d0480e
@@ -35,10 +35,17 @@
d0480e
 	(void) unlink(sample);
d0480e
 	int fd = open(sample, O_CREAT|O_RDONLY, 0400);
d0480e
 	if (fd == -1)
d0480e
-		perror_msg_and_fail("open");
d0480e
+		perror_msg_and_fail("open(\"%s\")", sample);
d0480e
+
d0480e
+	static const char sample_del[] = "fchmod_sample_file (deleted)";
d0480e
+	(void) unlink(sample_del);
d0480e
+	int fd_del = open(sample_del, O_CREAT|O_RDONLY, 0400);
d0480e
+	if (fd_del == -1)
d0480e
+		perror_msg_and_fail("open(\"%s\")", sample);
d0480e
 
d0480e
 # ifdef YFLAG
d0480e
 	char *sample_realpath = get_fd_path(fd);
d0480e
+	char *sample_del_realpath = get_fd_path(fd_del);
d0480e
 # endif
d0480e
 
d0480e
 	const char *sample_secontext = SECONTEXT_FILE(sample);
d0480e
@@ -56,12 +63,27 @@
d0480e
 	       sample_secontext,
d0480e
 	       sprintrc(rc));
d0480e
 
d0480e
+	const char *sample_del_secontext = SECONTEXT_FILE(sample_del);
d0480e
+	rc = syscall(__NR_fchmod, fd_del, 0600);
d0480e
+# ifdef YFLAG
d0480e
+	printf("%s%s(%d<%s>%s, 0600) = %s\n",
d0480e
+# else
d0480e
+	printf("%s%s(%d%s, 0600) = %s\n",
d0480e
+# endif
d0480e
+	       my_secontext, "fchmod",
d0480e
+	       fd_del,
d0480e
+# ifdef YFLAG
d0480e
+	       sample_del_realpath,
d0480e
+# endif
d0480e
+	       sample_del_secontext,
d0480e
+	       sprintrc(rc));
d0480e
+
d0480e
 	if (unlink(sample))
d0480e
-		perror_msg_and_fail("unlink");
d0480e
+		perror_msg_and_fail("unlink(\"%s\")", sample);
d0480e
 
d0480e
 	rc = syscall(__NR_fchmod, fd, 051);
d0480e
 # ifdef YFLAG
d0480e
-	printf("%s%s(%d<%s (deleted)>%s, 051) = %s\n",
d0480e
+	printf("%s%s(%d<%s>(deleted)%s, 051) = %s\n",
d0480e
 # else
d0480e
 	printf("%s%s(%d%s, 051) = %s\n",
d0480e
 # endif
d0480e
@@ -73,9 +95,26 @@
d0480e
 	       sample_secontext,
d0480e
 	       sprintrc(rc));
d0480e
 
d0480e
+	if (unlink(sample_del))
d0480e
+		perror_msg_and_fail("unlink(\"%s\")", sample_del);
d0480e
+
d0480e
+	rc = syscall(__NR_fchmod, fd_del, 051);
d0480e
+# ifdef YFLAG
d0480e
+	printf("%s%s(%d<%s>(deleted)%s, 051) = %s\n",
d0480e
+# else
d0480e
+	printf("%s%s(%d%s, 051) = %s\n",
d0480e
+# endif
d0480e
+	       my_secontext, "fchmod",
d0480e
+	       fd_del,
d0480e
+# ifdef YFLAG
d0480e
+	       sample_del_realpath,
d0480e
+# endif
d0480e
+	       sample_del_secontext,
d0480e
+	       sprintrc(rc));
d0480e
+
d0480e
 	rc = syscall(__NR_fchmod, fd, 004);
d0480e
 # ifdef YFLAG
d0480e
-	printf("%s%s(%d<%s (deleted)>%s, 004) = %s\n",
d0480e
+	printf("%s%s(%d<%s>(deleted)%s, 004) = %s\n",
d0480e
 # else
d0480e
 	printf("%s%s(%d%s, 004) = %s\n",
d0480e
 # endif