[RFC PATCH v4 1/4] Allow scanning of fdinfo fields in one pass

Sahil Siddiq icegambit91 at gmail.com
Thu May 16 04:20:53 UTC 2024


Implement constructs to scan /proc/<pid>/fdinfo/<fd>
in one pass and track which fields are available for
display.

* src/util.c
  (struct scan_fdinfo): New struct.
  (scan_fdinfo_lines): Use this struct.
  (scan_fdinfo): Wrapper around scan_fdinfo_lines.

Signed-off-by: Sahil Siddiq <icegambit91 at gmail.com>
---
Changes v3 -> v4:
- src/util.c:
  (struct scan_fdinfo): Rename struct.
  (scan_fdinfo_lines):
  - Rename from "scan_fdinfo".
  - (prefix_matches): Use ++ instead of +=1.
  (scan_fdinfo): New wrapper function.
  (get_finfo_for_dev): Revert to use scan_fdinfo wrapper.
  (pidfd_get_pid): Likewise.
  (printsignalfd): Likewise.

 src/util.c | 48 ++++++++++++++++++++++++++++++++++++++----------
 1 file changed, 38 insertions(+), 10 deletions(-)

diff --git a/src/util.c b/src/util.c
index efd390577..77c7d7c70 100644
--- a/src/util.c
+++ b/src/util.c
@@ -647,9 +647,16 @@ printsocket(struct tcb *tcp, int fd, const char *path)
 
 typedef bool (*scan_fdinfo_fn)(const char *value, void *data);
 
-static bool
-scan_fdinfo(pid_t pid_of_fd, int fd, const char *search_pfx,
-	    size_t search_pfx_len, scan_fdinfo_fn fn, void *data)
+struct scan_fdinfo {
+	const char *search_pfx;
+	size_t search_pfx_len;
+	scan_fdinfo_fn fn;
+	void *data;
+};
+
+static size_t
+scan_fdinfo_lines(pid_t pid_of_fd, int fd, struct scan_fdinfo *fdinfo_search_array,
+            size_t fdinfo_array_size)
 {
 	int proc_pid = 0;
 	translate_pid(NULL, pid_of_fd, PT_TID, &proc_pid);
@@ -665,21 +672,42 @@ scan_fdinfo(pid_t pid_of_fd, int fd, const char *search_pfx,
 
 	char *line = NULL;
 	size_t sz = 0;
-	bool result = false;
+	size_t prefix_matches = 0;
 
 	while (getline(&line, &sz, f) > 0) {
-		const char *value =
-			str_strip_prefix_len(line, search_pfx, search_pfx_len);
-		if (value != line && fn(value, data)) {
-			result = true;
-			break;
+		for (size_t i = 0; i < fdinfo_array_size ; i++) {
+			scan_fdinfo_fn fn = fdinfo_search_array[i].fn;
+			void *data = fdinfo_search_array[i].data;
+			const char *value =
+				str_strip_prefix_len(line, fdinfo_search_array[i].search_pfx,
+				                     fdinfo_search_array[i].search_pfx_len);
+			if (value != line && fn(value, data)) {
+				prefix_matches++;
+				break;
+			}
 		}
 	}
 
 	free(line);
 	fclose(f);
 
-	return result;
+	return prefix_matches;
+}
+
+static inline bool
+scan_fdinfo(pid_t pid_of_fd, int fd, const char *search_pfx,
+	        size_t search_pfx_len, scan_fdinfo_fn fn, void *data)
+{
+	struct scan_fdinfo fdinfo_lines[] = {
+		{
+			.search_pfx = search_pfx,
+			.search_pfx_len = search_pfx_len,
+			.fn = fn,
+			.data = data
+		}
+	};
+
+	return scan_fdinfo_lines(pid_of_fd, fd, fdinfo_lines, 1);
 }
 
 static bool
-- 
2.45.0



More information about the Strace-devel mailing list