[PATCH] selftest: acct: Add selftest for the acct() syscall

Abdulrasaq Lawani posted 1 patch 1 year, 5 months ago
There is a newer version of this series
tools/testing/selftests/Makefile            |  1 +
tools/testing/selftests/acct/.gitignore     |  2 +
tools/testing/selftests/acct/Makefile       |  4 ++
tools/testing/selftests/acct/acct_syscall.c | 60 +++++++++++++++++++++++++++++
4 files changed, 67 insertions(+)
[PATCH] selftest: acct: Add selftest for the acct() syscall
Posted by Abdulrasaq Lawani 1 year, 5 months ago
Noticed that there was no selftest for the acct() syscall
which enables the kernel to record terminated processes
into a specified file.

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

Signed-off-by: Abdulrasaq Lawani <abdulrasaqolawani@gmail.com>
---
 tools/testing/selftests/Makefile            |  1 +
 tools/testing/selftests/acct/.gitignore     |  2 +
 tools/testing/selftests/acct/Makefile       |  4 ++
 tools/testing/selftests/acct/acct_syscall.c | 60 +++++++++++++++++++++++++++++
 4 files changed, 67 insertions(+)

diff --git a/tools/testing/selftests/Makefile b/tools/testing/selftests/Makefile
index 9039f3709aff..45a58ef5ad92 100644
--- a/tools/testing/selftests/Makefile
+++ b/tools/testing/selftests/Makefile
@@ -1,4 +1,5 @@
 # SPDX-License-Identifier: GPL-2.0
+TARGETS += acct
 TARGETS += alsa
 TARGETS += amd-pstate
 TARGETS += arm64
diff --git a/tools/testing/selftests/acct/.gitignore b/tools/testing/selftests/acct/.gitignore
new file mode 100644
index 000000000000..8ab358d81bd2
--- /dev/null
+++ b/tools/testing/selftests/acct/.gitignore
@@ -0,0 +1,2 @@
+acct_syscall
+config
\ No newline at end of file
diff --git a/tools/testing/selftests/acct/Makefile b/tools/testing/selftests/acct/Makefile
new file mode 100644
index 000000000000..ff3e238c5634
--- /dev/null
+++ b/tools/testing/selftests/acct/Makefile
@@ -0,0 +1,4 @@
+TEST_GEN_PROGS := acct_syscall
+CFLAGS += -Wall
+
+include ../lib.mk
\ No newline at end of file
diff --git a/tools/testing/selftests/acct/acct_syscall.c b/tools/testing/selftests/acct/acct_syscall.c
new file mode 100644
index 000000000000..457b83937cac
--- /dev/null
+++ b/tools/testing/selftests/acct/acct_syscall.c
@@ -0,0 +1,60 @@
+// SPDX-License-Identifier: GPL-2.0
+
+// kselftest for acct() syscall
+
+// This tests the acct() syscall, which logs closed processes
+// until deactivated or when criteria in /proc/sys/kernel/acct is met
+
+#include <stdio.h>
+#include <unistd.h>
+#include <errno.h>
+#include <string.h>
+#include <sys/wait.h>
+
+int main(void)
+{
+// Create file to log closed processes
+char filename[] = "process_log";
+FILE *fp;
+fp = fopen(filename, "w");
+
+int i = acct(filename);
+
+if (i) {
+    printf("Test Result: %s\n", strerror(errno));
+    remove(filename);
+    fclose(fp);
+    return 1;
+}
+
+    // Create child process and wait to close
+pid_t child_pid;
+
+child_pid = fork();
+
+if (child_pid < 0) {
+    printf("Process failed\n");
+    return 1;
+} else if (child_pid == 0) {
+    printf("Child process successfully created!\n");
+} else {
+    wait(NULL);
+    fseek(fp, 0L, SEEK_END);
+    int sz = ftell(fp);
+    printf("Parent process successfully created!\n");
+
+    i = acct(NULL);
+
+    if (sz <= 0) {
+        printf("Child process not logged");
+        return 1;
+    }
+
+    printf("Test Result: Successfully logged closed process.\n");
+    remove(filename);
+    fclose(fp);
+    return 0;
+}
+
+return 1;
+}

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

Best regards,
-- 
Abdulrasaq Lawani <abdulrasaqolawani@gmail.com>
Re: [PATCH] selftest: acct: Add selftest for the acct() syscall
Posted by Shuah Khan 1 year, 5 months ago
On 6/22/24 11:24, Abdulrasaq Lawani wrote:
> Noticed that there was no selftest for the acct() syscall
> which enables the kernel to record terminated processes
> into a specified file.
> 
> This patch provides a test for the acct() syscall.

Describe the functionality being tested. Add testcases to
test error conditions.
The code doesn't meet coding guidelines for C and also kernel.

Please check other kernel source files and coding standards document.

Use ksft_* function for reporting results. Refer to an existing test
on how to.

> 
> Signed-off-by: Abdulrasaq Lawani <abdulrasaqolawani@gmail.com>
> ---
>   tools/testing/selftests/Makefile            |  1 +
>   tools/testing/selftests/acct/.gitignore     |  2 +
>   tools/testing/selftests/acct/Makefile       |  4 ++
>   tools/testing/selftests/acct/acct_syscall.c | 60 +++++++++++++++++++++++++++++
>   4 files changed, 67 insertions(+)
> 
> diff --git a/tools/testing/selftests/Makefile b/tools/testing/selftests/Makefile
> index 9039f3709aff..45a58ef5ad92 100644
> --- a/tools/testing/selftests/Makefile
> +++ b/tools/testing/selftests/Makefile
> @@ -1,4 +1,5 @@
>   # SPDX-License-Identifier: GPL-2.0
> +TARGETS += acct
>   TARGETS += alsa
>   TARGETS += amd-pstate
>   TARGETS += arm64
> diff --git a/tools/testing/selftests/acct/.gitignore b/tools/testing/selftests/acct/.gitignore
> new file mode 100644
> index 000000000000..8ab358d81bd2
> --- /dev/null
> +++ b/tools/testing/selftests/acct/.gitignore
> @@ -0,0 +1,2 @@
> +acct_syscall
> +config
> \ No newline at end of file
> diff --git a/tools/testing/selftests/acct/Makefile b/tools/testing/selftests/acct/Makefile
> new file mode 100644
> index 000000000000..ff3e238c5634
> --- /dev/null
> +++ b/tools/testing/selftests/acct/Makefile
> @@ -0,0 +1,4 @@
> +TEST_GEN_PROGS := acct_syscall
> +CFLAGS += -Wall
> +
> +include ../lib.mk
> \ No newline at end of file
> diff --git a/tools/testing/selftests/acct/acct_syscall.c b/tools/testing/selftests/acct/acct_syscall.c
> new file mode 100644
> index 000000000000..457b83937cac
> --- /dev/null
> +++ b/tools/testing/selftests/acct/acct_syscall.c
> @@ -0,0 +1,60 @@
> +// SPDX-License-Identifier: GPL-2.0
> +
> +// kselftest for acct() syscall
> +
> +// This tests the acct() syscall, which logs closed processes
> +// until deactivated or when criteria in /proc/sys/kernel/acct is met
> +
> +#include <stdio.h>
> +#include <unistd.h>
> +#include <errno.h>
> +#include <string.h>
> +#include <sys/wait.h>
> +
> +int main(void)
> +{
> +// Create file to log closed processes

Missing tabs for the body of the function.

> +char filename[] = "process_log";
> +FILE *fp;
> +fp = fopen(filename, "w");
> +
> +int i = acct(filename);
> +
> +if (i) {
> +    printf("Test Result: %s\n", strerror(errno));
> +    remove(filename);
> +    fclose(fp);
> +    return 1;
> +}

> +
> +    // Create child process and wait to close
> +pid_t child_pid;
> +
> +child_pid = fork();
> +
> +if (child_pid < 0) {
> +    printf("Process failed\n");
> +    return 1;
> +} else if (child_pid == 0) {
> +    printf("Child process successfully created!\n");
> +} else {
> +    wait(NULL);
> +    fseek(fp, 0L, SEEK_END);
> +    int sz = ftell(fp);
> +    printf("Parent process successfully created!\n");
> +
> +    i = acct(NULL);
> +
> +    if (sz <= 0) {
> +        printf("Child process not logged");
> +        return 1;
> +    }
> +
> +    printf("Test Result: Successfully logged closed process.\n");
> +    remove(filename);
> +    fclose(fp);
> +    return 0;
> +}
> +
> +return 1;
> +}
> thanks,
-- Shuah