All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] selftests/proc/proc-empty-vm.c: Test for unmapped process
@ 2024-09-30 16:09 Siddharth Menon
  2024-09-30 22:07 ` Shuah Khan
  0 siblings, 1 reply; 2+ messages in thread
From: Siddharth Menon @ 2024-09-30 16:09 UTC (permalink / raw)
  To: shuah; +Cc: Siddharth Menon, linux-fsdevel, linux-kselftest

Check if VMsize is 0 to determine whether the process has been unmapped.
The child process cannot signal the parent that it has unmapped itself,
as it no longer exists. This includes unmapping the text segment,
preventing the child from proceeding to the next instruction.

Signed-off-by: Siddharth Menon <simeddon@gmail.com>
---
 tools/testing/selftests/proc/proc-empty-vm.c | 50 ++++++++++++++++++++
 1 file changed, 50 insertions(+)

diff --git a/tools/testing/selftests/proc/proc-empty-vm.c b/tools/testing/selftests/proc/proc-empty-vm.c
index b3f898aab4ab..8ee000b0ddd7 100644
--- a/tools/testing/selftests/proc/proc-empty-vm.c
+++ b/tools/testing/selftests/proc/proc-empty-vm.c
@@ -213,6 +213,53 @@ static void vsyscall(void)
 }
 #endif
 
+static int test_proc_pid_mem(pid_t pid)
+{
+	char buf[4096];
+	char *line;
+	int vm_size = -1;
+
+	snprintf(buf, sizeof(buf), "/proc/%d/status", pid);
+	int fd = open(buf, O_RDONLY);
+
+	if (fd == -1) {
+		if (errno == ENOENT) {
+			// Process does not exist
+			return EXIT_SUCCESS;
+		}
+	perror("open /proc/[pid]/status");
+	return EXIT_FAILURE;
+	}
+
+	ssize_t rv = read(fd, buf, sizeof(buf) - 1);
+
+	if (rv == -1) {
+		perror("read");
+		close(fd);
+		return EXIT_FAILURE;
+	}
+	buf[rv] = '\0';
+
+	line = strtok(buf, "\n");
+	while (line != NULL) {
+		// Check for VmSize
+		if (strncmp(line, "VmSize:", 7) == 0) {
+			sscanf(line, "VmSize: %d", &vm_size);
+			break;
+		}
+		line = strtok(NULL, "\n");
+	}
+
+	close(fd);
+
+	// Check if VmSize is 0
+	if (vm_size == 0) {
+		return EXIT_SUCCESS;
+	}
+
+	return EXIT_FAILURE;
+}
+
 static int test_proc_pid_maps(pid_t pid)
 {
 	char buf[4096];
@@ -508,6 +555,9 @@ int main(void)
 		 */
 		sleep(1);
 
+		if (rv == EXIT_SUCCESS) {
+			rv = test_proc_pid_mem(pid);
+		}
 		if (rv == EXIT_SUCCESS) {
 			rv = test_proc_pid_maps(pid);
 		}
-- 
2.39.5


^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2024-09-30 22:07 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-09-30 16:09 [PATCH] selftests/proc/proc-empty-vm.c: Test for unmapped process Siddharth Menon
2024-09-30 22:07 ` Shuah Khan

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.