[PATCH v2 1/6] Split printdev() into functions for printing and getting information about fd

Masatake YAMATO yamato at redhat.com
Fri Mar 11 18:02:04 UTC 2022


In preparation of using elsewhere.

printdev() did two things: getting information about fd and printing
it. Newly introduced get_finfo_for_dev() is for the "getting
information" part.

struct finfo a data struct for the information passed from
get_finfo_for_dev() to printdev().

* src/defs.h (struct finfo): New data structure.
* src/utils.c (get_finfo_for_dev): New fucntion derived from printdev.
(printdev): Use get_finfo_for_dev().

Signed-off-by: Masatake YAMATO <yamato at redhat.com>
---
 src/defs.h | 12 ++++++++++++
 src/util.c | 51 +++++++++++++++++++++++++++++++++++++++------------
 2 files changed, 51 insertions(+), 12 deletions(-)

diff --git a/src/defs.h b/src/defs.h
index addd85962..62868fa7e 100644
--- a/src/defs.h
+++ b/src/defs.h
@@ -653,6 +653,18 @@ static inline int set_tcb_priv_ulong(struct tcb *tcp, unsigned long val)
 	return set_tcb_priv_data(tcp, (void *) val, 0);
 }
 
+struct finfo {
+	enum {
+		finfo_unset,
+		finfo_dev_blk,
+		finfo_dev_chr,
+	} type;
+	const char *path;
+	struct {
+		unsigned int major, minor;
+	} dev;
+};
+
 /**
  * @return 0 on success, -1 on error.
  */
diff --git a/src/util.c b/src/util.c
index d0de13ada..6e61333f1 100644
--- a/src/util.c
+++ b/src/util.c
@@ -631,33 +631,60 @@ printsocket(struct tcb *tcp, int fd, const char *path)
 	return false;
 }
 
-static bool
-printdev(struct tcb *tcp, int fd, const char *path)
+static struct finfo *
+get_finfo_for_dev(const char *path, struct finfo *finfo)
 {
 	strace_stat_t st;
 
-	if (path[0] != '/')
-		return false;
+	finfo->type = finfo_unset;
+	finfo->path = path;
 
-	if (stat_file(path, &st)) {
-		debug_func_perror_msg("stat(\"%s\")", path);
-		return false;
+	if (finfo->path[0] != '/')
+		goto out;
+
+	if (stat_file(finfo->path, &st)) {
+		debug_func_perror_msg("stat(\"%s\")", finfo->path);
+		goto out;
 	}
 
 	switch (st.st_mode & S_IFMT) {
 	case S_IFBLK:
+		finfo->type = finfo_dev_blk;
+		break;
 	case S_IFCHR:
+		finfo->type = finfo_dev_chr;
+		break;
+	default:
+		goto out;
+	}
+
+	finfo->dev.major = major(st.st_rdev);
+	finfo->dev.minor = minor(st.st_rdev);
+
+ out:
+	return finfo;
+}
+
+static bool
+printdev(struct tcb *tcp, int fd, const char *path)
+{
+	struct finfo finforec;
+	struct finfo *finfo = get_finfo_for_dev(path, &finforec);
+
+	switch (finfo->type) {
+	case finfo_dev_blk:
+	case finfo_dev_chr:
 		tprints("<");
-		print_quoted_string_ex(path, strlen(path),
+		print_quoted_string_ex(finfo->path, strlen(finfo->path),
 				       QUOTE_OMIT_LEADING_TRAILING_QUOTES,
 				       "<>");
 		tprintf("<%s %u:%u>>",
-			S_ISBLK(st.st_mode)? "block" : "char",
-			major(st.st_rdev), minor(st.st_rdev));
+			(finfo->type == finfo_dev_blk)? "block": "char",
+			finfo->dev.major, finfo->dev.minor);
 		return true;
+	default:
+		return false;
 	}
-
-	return false;
 }
 
 pid_t
-- 
2.35.1



More information about the Strace-devel mailing list