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

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