Blame SOURCES/0147-file_handle-print-f_handle-as-a-hexadecimal-string.patch

242c1e
From 5a8d3bab6f492b3932299d9e80fb247ab00b8102 Mon Sep 17 00:00:00 2001
242c1e
From: "Dmitry V. Levin" <ldv@strace.io>
242c1e
Date: Sun, 7 Mar 2021 08:00:00 +0000
242c1e
Subject: [PATCH 147/149] file_handle: print f_handle as a hexadecimal string
242c1e
242c1e
Printing the sequence of bytes as a hexadecimal number is misleading
242c1e
because the latter depends on endianness.
242c1e
242c1e
* src/file_handle.c (print_f_handle): New function.
242c1e
(SYS_FUNC(name_to_handle_at), SYS_FUNC(open_by_handle_at)): Use it
242c1e
to print struct file_handle.f_handle.
242c1e
* tests/file_handle.c (print_handle_data, do_open_by_handle_at, main):
242c1e
Update expected output.
242c1e
---
242c1e
 file_handle.c       | 47 ++++++++++++++++++++++-------------------------
242c1e
 tests/file_handle.c | 26 +++++++++++---------------
242c1e
 2 files changed, 33 insertions(+), 40 deletions(-)
242c1e
242c1e
diff --git a/file_handle.c b/file_handle.c
242c1e
index d82a1bc..343111f 100644
242c1e
--- a/file_handle.c
242c1e
+++ b/file_handle.c
242c1e
@@ -19,6 +19,22 @@ typedef struct {
242c1e
 	int handle_type;
242c1e
 } file_handle_header;
242c1e
 
242c1e
+static void
242c1e
+print_f_handle(struct tcb *tcp, kernel_ulong_t addr, unsigned int handle_bytes)
242c1e
+{
242c1e
+	unsigned int len = MIN(handle_bytes, MAX_HANDLE_SZ);
242c1e
+	char f_handle[MAX_HANDLE_SZ];
242c1e
+	addr += sizeof(file_handle_header);
242c1e
+	if (addr > sizeof(file_handle_header) &&
242c1e
+	    !umoven(tcp, addr, len, f_handle)) {
242c1e
+		print_quoted_string(f_handle, len, QUOTE_FORCE_HEX);
242c1e
+		if (handle_bytes > len)
242c1e
+			tprints("...");
242c1e
+	} else {
242c1e
+		tprints("???");
242c1e
+	}
242c1e
+}
242c1e
+
242c1e
 SYS_FUNC(name_to_handle_at)
242c1e
 {
242c1e
 	file_handle_header h;
242c1e
@@ -53,24 +69,15 @@ SYS_FUNC(name_to_handle_at)
242c1e
 
242c1e
 		return 0;
242c1e
 	} else {
242c1e
-		unsigned int i = get_tcb_priv_ulong(tcp);
242c1e
-
242c1e
 		if ((!syserror(tcp) || EOVERFLOW == tcp->u_error)
242c1e
 		    && !umove(tcp, addr, &h)) {
242c1e
-			unsigned char f_handle[MAX_HANDLE_SZ];
242c1e
 
242c1e
-			if (i != h.handle_bytes)
242c1e
+			if (h.handle_bytes != get_tcb_priv_ulong(tcp))
242c1e
 				tprintf(" => %u", h.handle_bytes);
242c1e
 			if (!syserror(tcp)) {
242c1e
-				tprintf(", handle_type=%d", h.handle_type);
242c1e
-				if (h.handle_bytes > MAX_HANDLE_SZ)
242c1e
-					h.handle_bytes = MAX_HANDLE_SZ;
242c1e
-				if (!umoven(tcp, addr + sizeof(h), h.handle_bytes,
242c1e
-					    f_handle)) {
242c1e
-					tprints(", f_handle=0x");
242c1e
-					for (i = 0; i < h.handle_bytes; ++i)
242c1e
-						tprintf("%02x", f_handle[i]);
242c1e
-				}
242c1e
+				tprintf(", handle_type=%d, f_handle=",
242c1e
+					h.handle_type);
242c1e
+				print_f_handle(tcp, addr, h.handle_bytes);
242c1e
 			}
242c1e
 		}
242c1e
 		tprints("}, ");
242c1e
@@ -96,19 +103,9 @@ SYS_FUNC(open_by_handle_at)
242c1e
 
242c1e
 	/* handle */
242c1e
 	if (!umove_or_printaddr(tcp, addr, &h)) {
242c1e
-		unsigned char f_handle[MAX_HANDLE_SZ];
242c1e
-
242c1e
-		tprintf("{handle_bytes=%u, handle_type=%d",
242c1e
+		tprintf("{handle_bytes=%u, handle_type=%d, f_handle=",
242c1e
 			h.handle_bytes, h.handle_type);
242c1e
-		if (h.handle_bytes > MAX_HANDLE_SZ)
242c1e
-			h.handle_bytes = MAX_HANDLE_SZ;
242c1e
-		if (!umoven(tcp, addr + sizeof(h), h.handle_bytes, &f_handle)) {
242c1e
-			unsigned int i;
242c1e
-
242c1e
-			tprints(", f_handle=0x");
242c1e
-			for (i = 0; i < h.handle_bytes; ++i)
242c1e
-				tprintf("%02x", f_handle[i]);
242c1e
-		}
242c1e
+		print_f_handle(tcp, addr, h.handle_bytes);
242c1e
 		tprints("}");
242c1e
 	}
242c1e
 	tprints(", ");
242c1e
diff --git a/tests/file_handle.c b/tests/file_handle.c
242c1e
index edabde6..07af7ba 100644
242c1e
--- a/tests/file_handle.c
242c1e
+++ b/tests/file_handle.c
242c1e
@@ -42,14 +42,10 @@ struct file_handle {
242c1e
 void
242c1e
 print_handle_data(unsigned char *bytes, unsigned int size)
242c1e
 {
242c1e
-	unsigned int i;
242c1e
-
242c1e
-	if (size > MAX_HANDLE_SZ)
242c1e
-		size = MAX_HANDLE_SZ;
242c1e
-
242c1e
-	printf("0x");
242c1e
-	for (i = 0; i < size; ++i)
242c1e
-		printf("%02x", bytes[i]);
242c1e
+	unsigned int len = MIN(size, MAX_HANDLE_SZ);
242c1e
+	print_quoted_hex(bytes, len);
242c1e
+	if (size > len)
242c1e
+		printf("...");
242c1e
 }
242c1e
 
242c1e
 void
242c1e
@@ -111,11 +107,13 @@ do_open_by_handle_at(kernel_ulong_t mount_fd,
242c1e
 		printf("{handle_bytes=%u, handle_type=%d", fh->handle_bytes,
242c1e
 		       fh->handle_type);
242c1e
 
242c1e
+		printf(", f_handle=");
242c1e
 		if (valid_data) {
242c1e
-			printf(", f_handle=");
242c1e
 			print_handle_data((unsigned char *) fh +
242c1e
 					  sizeof(struct file_handle),
242c1e
 					  fh->handle_bytes);
242c1e
+		} else {
242c1e
+			printf("???");
242c1e
 		}
242c1e
 
242c1e
 		printf("}");
242c1e
@@ -275,16 +273,14 @@ main(void)
242c1e
 	assert(syscall(__NR_name_to_handle_at, fdcwd, ".", handle, &mount_id,
242c1e
 		flags) == 0);
242c1e
 	printf("name_to_handle_at(AT_FDCWD, \".\", {handle_bytes=%u"
242c1e
-	       ", handle_type=%d, f_handle=0x",
242c1e
+	       ", handle_type=%d, f_handle=",
242c1e
 	       handle->handle_bytes, handle->handle_type);
242c1e
-	for (i = 0; i < handle->handle_bytes; ++i)
242c1e
-		printf("%02x", handle->f_handle[i]);
242c1e
+	print_handle_data(handle->f_handle, handle->handle_bytes);
242c1e
 	printf("}, [%d], AT_SYMLINK_FOLLOW) = 0\n", mount_id);
242c1e
 
242c1e
 	printf("open_by_handle_at(-1, {handle_bytes=%u, handle_type=%d"
242c1e
-	       ", f_handle=0x", handle->handle_bytes, handle->handle_type);
242c1e
-	for (i = 0; i < handle->handle_bytes; ++i)
242c1e
-		printf("%02x", handle->f_handle[i]);
242c1e
+	       ", f_handle=", handle->handle_bytes, handle->handle_type);
242c1e
+	print_handle_data(handle->f_handle, handle->handle_bytes);
242c1e
 	int rc = syscall(__NR_open_by_handle_at, -1, handle,
242c1e
 		O_RDONLY | O_DIRECTORY);
242c1e
 	printf("}, O_RDONLY|O_DIRECTORY) = %d %s (%m)\n", rc, errno2name());
242c1e
diff --git a/tests-m32/file_handle.c b/tests-m32/file_handle.c
242c1e
index edabde6..07af7ba 100644
242c1e
--- a/tests-m32/file_handle.c
242c1e
+++ b/tests-m32/file_handle.c
242c1e
@@ -42,14 +42,10 @@ struct file_handle {
242c1e
 void
242c1e
 print_handle_data(unsigned char *bytes, unsigned int size)
242c1e
 {
242c1e
-	unsigned int i;
242c1e
-
242c1e
-	if (size > MAX_HANDLE_SZ)
242c1e
-		size = MAX_HANDLE_SZ;
242c1e
-
242c1e
-	printf("0x");
242c1e
-	for (i = 0; i < size; ++i)
242c1e
-		printf("%02x", bytes[i]);
242c1e
+	unsigned int len = MIN(size, MAX_HANDLE_SZ);
242c1e
+	print_quoted_hex(bytes, len);
242c1e
+	if (size > len)
242c1e
+		printf("...");
242c1e
 }
242c1e
 
242c1e
 void
242c1e
@@ -111,11 +107,13 @@ do_open_by_handle_at(kernel_ulong_t mount_fd,
242c1e
 		printf("{handle_bytes=%u, handle_type=%d", fh->handle_bytes,
242c1e
 		       fh->handle_type);
242c1e
 
242c1e
+		printf(", f_handle=");
242c1e
 		if (valid_data) {
242c1e
-			printf(", f_handle=");
242c1e
 			print_handle_data((unsigned char *) fh +
242c1e
 					  sizeof(struct file_handle),
242c1e
 					  fh->handle_bytes);
242c1e
+		} else {
242c1e
+			printf("???");
242c1e
 		}
242c1e
 
242c1e
 		printf("}");
242c1e
@@ -275,16 +273,14 @@ main(void)
242c1e
 	assert(syscall(__NR_name_to_handle_at, fdcwd, ".", handle, &mount_id,
242c1e
 		flags) == 0);
242c1e
 	printf("name_to_handle_at(AT_FDCWD, \".\", {handle_bytes=%u"
242c1e
-	       ", handle_type=%d, f_handle=0x",
242c1e
+	       ", handle_type=%d, f_handle=",
242c1e
 	       handle->handle_bytes, handle->handle_type);
242c1e
-	for (i = 0; i < handle->handle_bytes; ++i)
242c1e
-		printf("%02x", handle->f_handle[i]);
242c1e
+	print_handle_data(handle->f_handle, handle->handle_bytes);
242c1e
 	printf("}, [%d], AT_SYMLINK_FOLLOW) = 0\n", mount_id);
242c1e
 
242c1e
 	printf("open_by_handle_at(-1, {handle_bytes=%u, handle_type=%d"
242c1e
-	       ", f_handle=0x", handle->handle_bytes, handle->handle_type);
242c1e
-	for (i = 0; i < handle->handle_bytes; ++i)
242c1e
-		printf("%02x", handle->f_handle[i]);
242c1e
+	       ", f_handle=", handle->handle_bytes, handle->handle_type);
242c1e
+	print_handle_data(handle->f_handle, handle->handle_bytes);
242c1e
 	int rc = syscall(__NR_open_by_handle_at, -1, handle,
242c1e
 		O_RDONLY | O_DIRECTORY);
242c1e
 	printf("}, O_RDONLY|O_DIRECTORY) = %d %s (%m)\n", rc, errno2name());
242c1e
diff --git a/tests-mx32/file_handle.c b/tests-mx32/file_handle.c
242c1e
index edabde6..07af7ba 100644
242c1e
--- a/tests-mx32/file_handle.c
242c1e
+++ b/tests-mx32/file_handle.c
242c1e
@@ -42,14 +42,10 @@ struct file_handle {
242c1e
 void
242c1e
 print_handle_data(unsigned char *bytes, unsigned int size)
242c1e
 {
242c1e
-	unsigned int i;
242c1e
-
242c1e
-	if (size > MAX_HANDLE_SZ)
242c1e
-		size = MAX_HANDLE_SZ;
242c1e
-
242c1e
-	printf("0x");
242c1e
-	for (i = 0; i < size; ++i)
242c1e
-		printf("%02x", bytes[i]);
242c1e
+	unsigned int len = MIN(size, MAX_HANDLE_SZ);
242c1e
+	print_quoted_hex(bytes, len);
242c1e
+	if (size > len)
242c1e
+		printf("...");
242c1e
 }
242c1e
 
242c1e
 void
242c1e
@@ -111,11 +107,13 @@ do_open_by_handle_at(kernel_ulong_t mount_fd,
242c1e
 		printf("{handle_bytes=%u, handle_type=%d", fh->handle_bytes,
242c1e
 		       fh->handle_type);
242c1e
 
242c1e
+		printf(", f_handle=");
242c1e
 		if (valid_data) {
242c1e
-			printf(", f_handle=");
242c1e
 			print_handle_data((unsigned char *) fh +
242c1e
 					  sizeof(struct file_handle),
242c1e
 					  fh->handle_bytes);
242c1e
+		} else {
242c1e
+			printf("???");
242c1e
 		}
242c1e
 
242c1e
 		printf("}");
242c1e
@@ -275,16 +273,14 @@ main(void)
242c1e
 	assert(syscall(__NR_name_to_handle_at, fdcwd, ".", handle, &mount_id,
242c1e
 		flags) == 0);
242c1e
 	printf("name_to_handle_at(AT_FDCWD, \".\", {handle_bytes=%u"
242c1e
-	       ", handle_type=%d, f_handle=0x",
242c1e
+	       ", handle_type=%d, f_handle=",
242c1e
 	       handle->handle_bytes, handle->handle_type);
242c1e
-	for (i = 0; i < handle->handle_bytes; ++i)
242c1e
-		printf("%02x", handle->f_handle[i]);
242c1e
+	print_handle_data(handle->f_handle, handle->handle_bytes);
242c1e
 	printf("}, [%d], AT_SYMLINK_FOLLOW) = 0\n", mount_id);
242c1e
 
242c1e
 	printf("open_by_handle_at(-1, {handle_bytes=%u, handle_type=%d"
242c1e
-	       ", f_handle=0x", handle->handle_bytes, handle->handle_type);
242c1e
-	for (i = 0; i < handle->handle_bytes; ++i)
242c1e
-		printf("%02x", handle->f_handle[i]);
242c1e
+	       ", f_handle=", handle->handle_bytes, handle->handle_type);
242c1e
+	print_handle_data(handle->f_handle, handle->handle_bytes);
242c1e
 	int rc = syscall(__NR_open_by_handle_at, -1, handle,
242c1e
 		O_RDONLY | O_DIRECTORY);
242c1e
 	printf("}, O_RDONLY|O_DIRECTORY) = %d %s (%m)\n", rc, errno2name());
242c1e
-- 
242c1e
2.1.4
242c1e