[RFC PATCH v2 3/4] Add test for --decode-fds=eventfd

Sahil Siddiq icegambit91 at gmail.com
Wed May 1 17:28:00 UTC 2024


Add test for --decode-fds=eventfd.

* tests/.gitignore: Add eventfd-yy.
* tests/Makefile.am: Add eventfd-yy.
* tests/eventfd-yy.c: New file.
* tests/gen_tests.in: Add rule to generate test.

Signed-off-by: Sahil Siddiq <icegambit91 at gmail.com>
---
Changes v1 -> v2:
- tests/eventfd-yy.c
  (parse_fdinfo_efd_id): New function.
  (procfs_get_efd_id): Likewise.
  (main): Remove "poll". Test "eventfd-id" retrieval.
- tests/gen_tests.in: Modify rule.

 tests/.gitignore   |   1 +
 tests/Makefile.am  |   1 +
 tests/eventfd-yy.c | 118 +++++++++++++++++++++++++++++++++++++++++++++
 tests/gen_tests.in |   1 +
 4 files changed, 121 insertions(+)
 create mode 100644 tests/eventfd-yy.c

diff --git a/tests/.gitignore b/tests/.gitignore
index 26274e727..8252d4d4f 100644
--- a/tests/.gitignore
+++ b/tests/.gitignore
@@ -129,6 +129,7 @@ epoll_pwait2-y
 epoll_wait
 erestartsys
 eventfd
+eventfd-yy
 execve
 execve-v
 execveat
diff --git a/tests/Makefile.am b/tests/Makefile.am
index ef6889862..64f32edd7 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -142,6 +142,7 @@ check_PROGRAMS = $(PURE_EXECUTABLES) \
 	count-f \
 	delay \
 	detach-vfork \
+	eventfd-yy \
 	execve-v \
 	execveat-v \
 	fcntl--pidns-translation \
diff --git a/tests/eventfd-yy.c b/tests/eventfd-yy.c
new file mode 100644
index 000000000..ab62ef545
--- /dev/null
+++ b/tests/eventfd-yy.c
@@ -0,0 +1,118 @@
+/*
+ * Test --decode-fds=eventfd option.
+ *
+ * Copyright (c) 2024 The strace developers.
+ * All rights reserved.
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+#include "tests.h"
+
+#include <inttypes.h>
+#include <poll.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/eventfd.h>
+
+#define SKIP_IF_PROC_IS_UNAVAILABLE skip_if_unavailable("/proc/self/fd/")
+
+static bool
+parse_fdinfo_efd_id(const char *value, void *data)
+{
+	int *efd_id = data;
+	*efd_id = atoi(value);
+
+	if (*efd_id == 0)
+		return false;
+
+	return true;
+}
+
+static bool
+procfs_get_efd_id(pid_t pid, int fd, const char *search_pfx,
+                  size_t search_pfx_len, void *data)
+{
+	char fdinfo_path[sizeof("/proc/%u/fdinfo/%u") + 2 * sizeof(int) * 3];
+	sprintf(fdinfo_path, "/proc/%u/fdinfo/%u", pid, fd);
+
+	FILE *f = fopen(fdinfo_path, "r");
+	if (!f)
+		perror_msg_and_fail("fopen");
+
+	char *line = NULL;
+	size_t sz = 0;
+	bool result = false;
+
+	while (getline(&line, &sz, f) > 0) {
+		const char *value = NULL;
+		if (!strncmp(line, search_pfx, search_pfx_len)) {
+			value = line + search_pfx_len;
+			if (parse_fdinfo_efd_id(value, data)) {
+				result = true;
+				break;
+			}
+		}
+	}
+
+	free(line);
+	fclose(f);
+
+	return result;
+}
+
+int
+main(void)
+{
+	SKIP_IF_PROC_IS_UNAVAILABLE;
+
+	uint64_t u = 5;
+	uint64_t efd_counter = u;
+	pid_t pid = getpid();
+
+	int efd = eventfd((unsigned int)u, 0);
+	if (efd == -1)
+		perror_msg_and_fail("eventfd");
+
+	static const char efd_id_pfx[] = "eventfd-id:";
+	int efd_id = -1;
+
+	procfs_get_efd_id(getpid(), efd, efd_id_pfx, sizeof(efd_id_pfx) - 1, &efd_id);
+
+	printf("eventfd2(%d, 0) = %d<eventfd-count:%"PRIu64", eventfd-id:%d>\n",
+	       (unsigned int)u, efd, efd_counter, efd_id);
+
+	efd_counter += u;
+	if (write(efd, &u, sizeof(uint64_t)) == -1)
+		perror_msg_and_fail("write");
+
+	int efd2 = dup(efd);
+
+	printf("dup(%d<eventfd-count:%"PRIu64", eventfd-id:%d>) = %d<eventfd-count:%"PRIu64", eventfd-id:%d>\n",
+	       efd, efd_counter, efd_id, efd2, efd_counter, efd_id);
+
+	uint64_t efd3_counter = u;
+	int efd3 = eventfd((unsigned int)u, 0);
+	if (efd3 == -1)
+		return -1;
+
+    int efd3_id = -1;
+	procfs_get_efd_id(pid, efd3, efd_id_pfx, sizeof(efd_id_pfx) - 1, &efd3_id);
+
+	printf("eventfd2(%d, 0) = %d<eventfd-count:%"PRIu64", eventfd-id:%d>\n",
+           (unsigned int)u, efd3, efd3_counter, efd3_id);
+
+	int efd4 = dup(efd3);
+
+	printf("dup(%d<eventfd-count:%"PRIu64", eventfd-id:%d>) = %d<eventfd-count:%"PRIu64", eventfd-id:%d>\n",
+	       efd3, efd3_counter, efd3_id, efd4, efd3_counter, efd3_id);
+
+
+	close(efd);
+	close(efd2);
+	close(efd3);
+	close(efd4);
+	return 0;
+}
diff --git a/tests/gen_tests.in b/tests/gen_tests.in
index e0e079727..b10c44104 100644
--- a/tests/gen_tests.in
+++ b/tests/gen_tests.in
@@ -113,6 +113,7 @@ epoll_pwait2-P	--trace=epoll_pwait2 -P /dev/full
 epoll_pwait2-y	--trace=epoll_pwait2 -y
 epoll_wait	-a26
 erestartsys	-a34 -e signal=none -e trace=recvfrom
+eventfd-yy -a10 --trace=dup,eventfd2 -yy -qq
 execve--secontext	+execve.test --secontext
 execve--secontext_full	+execve.test --secontext=full
 execve--secontext_full_mismatch	+execve.test --secontext=full,mismatch
-- 
2.44.0



More information about the Strace-devel mailing list