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

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