Skip to contentSkip to conversation

selftest: acct: Add selftest for the acct() syscall

PATCH Superseded Abdulrasaq Lawani 2 participants 2 messages Jun 30, 2024 – Jul 5, 2024 Kselftest

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 selftest: acct: Add selftest for the acct() syscall

Noticed that there was no selftest for the acct() syscall which enables the kernel to record terminated processes into a specified file.

The acct() system call enables or disables process accounting. If accounting is turned on, records for each terminating process are appended to a specified filename as it terminates. An argument of NULL causes accounting to be turned off.

This patch provides a test for the acct() syscall.

References:
https://man7.org/linux/man-pages/man2/acct.2.html

---
Changes in v2:
Add testcases to test error conditions.
Add kselftest function for reporting results.

- Link to v1: https://lore.kernel.org/r/[email protected]

---
 tools/testing/selftests/Makefile            |  1 +
 tools/testing/selftests/acct/.gitignore     |  2 +
 tools/testing/selftests/acct/Makefile       |  4 ++
 tools/testing/selftests/acct/acct_syscall.c | 89 +++++++++++++++++++++++++++++
 4 files changed, 96 insertions(+)
tools/testing/selftests/Makefile +1 -0
index 9039f3709aff..45a58ef5ad92 100644
@@ -1,4 +1,5 @@
 # SPDX-License-Identifier: GPL-2.0
+TARGETS += acct
 TARGETS += alsa
 TARGETS += amd-pstate
 TARGETS += arm64
tools/testing/selftests/acct/.gitignore new file +2 -0
new file mode 100644
index 000000000000..8ab358d81bd2
@@ -0,0 +1,2 @@
+acct_syscall
+config
\ No newline at end of file
tools/testing/selftests/acct/Makefile new file +4 -0
new file mode 100644
index 000000000000..ff3e238c5634
@@ -0,0 +1,4 @@
+TEST_GEN_PROGS := acct_syscall
+CFLAGS += -Wall
+
+include ../lib.mk
\ No newline at end of file
tools/testing/selftests/acct/acct_syscall.c new file +89 -0

89 lines not on this page · raw message

---
base-commit: 50736169ecc8387247fe6a00932852ce7b057083
change-id: 20240622-kselftest-acct-syscall-2d90f7666b1e

Best regards,

Signature · 1 line
Abdulrasaq Lawani <[email protected]>
maintainer
#1 lore raw
Quoted 9 lines · Abdulrasaq Lawani

Noticed that there was no selftest for the acct() syscall which enables the kernel to record terminated processes into a specified file.

The acct() system call enables or disables process accounting. If accounting is turned on, records for each terminating process are appended to a specified filename as it terminates. An argument of NULL causes accounting to be turned off.

Quoted 6 lines

This patch provides a test for the acct() syscall.

References:
https://man7.org/linux/man-pages/man2/acct.2.html

What does the test output look like?

Quoted 29 lines

---
Changes in v2:
Add testcases to test error conditions.
Add kselftest function for reporting results.

- Link to v1: https://lore.kernel.org/r/[email protected]

---
  tools/testing/selftests/Makefile            |  1 +
  tools/testing/selftests/acct/.gitignore     |  2 +
  tools/testing/selftests/acct/Makefile       |  4 ++
  tools/testing/selftests/acct/acct_syscall.c | 89 +++++++++++++++++++++++++++++
  4 files changed, 96 insertions(+)
tools/testing/selftests/Makefile +1 -0
index 9039f3709aff..45a58ef5ad92 100644
@@ -1,4 +1,5 @@
  # SPDX-License-Identifier: GPL-2.0
+TARGETS += acct
  TARGETS += alsa
  TARGETS += amd-pstate
  TARGETS += arm64
tools/testing/selftests/acct/.gitignore new file +0 -0
new file mode 100644
index 000000000000..8ab358d81bd2

tools/testing/selftests/acct/.gitignore line 2

Quoted 3 lines
tools/testing/selftests/acct/.gitignore new file +2 -0
@@ -0,0 +1,2 @@
+acct_syscall
+config
\ No newline at end of file

What is this?

Quoted 28 lines
tools/testing/selftests/acct/Makefile new file +4 -0
new file mode 100644
index 000000000000..ff3e238c5634
@@ -0,0 +1,4 @@
+TEST_GEN_PROGS := acct_syscall
+CFLAGS += -Wall
+
+include ../lib.mk
\ No newline at end of file
tools/testing/selftests/acct/acct_syscall.c new file +11 -0
new file mode 100644
index 000000000000..4fa00a88a1bd
@@ -0,0 +1,89 @@
+// SPDX-License-Identifier: GPL-2.0
+
+/* kselftest for acct() system call
+ *  The acct() system call enables or disables process accounting.
+ */
+
+#include <stdio.h>
+#include <errno.h>
+#include <string.h>
+#include <sys/wait.h>
+

tools/testing/selftests/acct/acct_syscall.c line 21

Quoted 10 lines
tools/testing/selftests/acct/acct_syscall.c new file +10 -0
+#include "../kselftest.h"
+
+int main(void)
+{
+	// Setting up kselftest framework
+	ksft_print_header();
+	ksft_set_plan(1);
+
+	// Create file to log closed processes
+	char filename[] = "process_log";

Where does this file created?

tools/testing/selftests/acct/acct_syscall.c line 24

Quoted 3 lines
tools/testing/selftests/acct/acct_syscall.c new file +3 -0
+	FILE *fp;
+
+	fp = fopen(filename, "w");

What happens if file creation fails?

tools/testing/selftests/acct/acct_syscall.c line 26

Quoted 2 lines
tools/testing/selftests/acct/acct_syscall.c new file +2 -0
+
+	int i = acct(filename);

Chose a descriptive name for the variable e.g: ret

tools/testing/selftests/acct/acct_syscall.c line 29

Quoted 3 lines
tools/testing/selftests/acct/acct_syscall.c new file +3 -0
+
+	// Handle error conditions
+	if (i) {

Is this the failure case - ret < 0 case - complete conditional.

Quoted 26 lines
tools/testing/selftests/acct/acct_syscall.c new file +26 -0
+		switch (errno) {
+		case EPERM:
+			ksft_test_result_error("%s. Please run the test as root.\n",
+				strerror(errno));
+			break;
+
+		case EACCES:
+			ksft_test_result_error("Insufficient privilege.\n");
+			break;
+
+		case EIO:
+			ksft_test_result_error("Error writing to the file: %s.\n", filename);
+			break;
+
+		default:
+			ksft_test_result_error("%s.\n", strerror(errno));
+			break;
+		}
+
+		remove(filename);
+		fclose(fp);
+		ksft_finished();
+		return 1;
+	}
+
+	// Create child process and wait for it to terminate.

tools/testing/selftests/acct/acct_syscall.c line 65

Quoted 10 lines
tools/testing/selftests/acct/acct_syscall.c new file +10 -0
+	pid_t child_pid;
+
+	child_pid = fork();
+
+	if (child_pid < 0) {
+		ksft_test_result_error("Process failed\n");
+		ksft_finished();
+		return 1;
+	} else if (child_pid == 0) {
+		ksft_print_msg("Child process successfully created!\n");

You don't need braces here since it it a single statement after the conditional.

Quoted 29 lines
tools/testing/selftests/acct/acct_syscall.c new file +24 -1
+	} else {
+		wait(NULL);
+		fseek(fp, 0L, SEEK_END);
+		int sz = ftell(fp);
+
+		ksft_print_msg("Parent process successfully created!\n");
+
+		i = acct(NULL);
+
+		if (sz <= 0) {
+			ksft_test_result_fail("Terminated child process not logged");
+			ksft_exit_fail();
+			return 1;
+		}
+
+		ksft_test_result_pass("Successfully logged terminated process.\n");
+		remove(filename);
+		fclose(fp);
+		ksft_exit_pass();
+		return 0;
+	}
+
+	return 1;
+}
 
---

base-commit: 50736169ecc8387247fe6a00932852ce7b057083
change-id: 20240622-kselftest-acct-syscall-2d90f7666b1e

Quoted 1 line

Best regards,

Please check coding guidelines and ruun checkpatch on this.