Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

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

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 will add a test for the acct() syscall.

Signed-off-by: Abdulrasaq Lawani <abdulrasaqolawani@gmail.com>
Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>

authored by

Abdulrasaq Lawani and committed by
Shuah Khan
f0a1ffa6 44b045e2

+87
+1
tools/testing/selftests/Makefile
··· 1 1 # SPDX-License-Identifier: GPL-2.0 2 + TARGETS += acct 2 3 TARGETS += alsa 3 4 TARGETS += amd-pstate 4 5 TARGETS += arm64
+3
tools/testing/selftests/acct/.gitignore
··· 1 + acct_syscall 2 + config 3 + process_log
+5
tools/testing/selftests/acct/Makefile
··· 1 + # SPDX-License-Identifier: GPL-2.0 2 + TEST_GEN_PROGS := acct_syscall 3 + CFLAGS += -Wall 4 + 5 + include ../lib.mk
+78
tools/testing/selftests/acct/acct_syscall.c
··· 1 + // SPDX-License-Identifier: GPL-2.0 2 + 3 + /* kselftest for acct() system call 4 + * The acct() system call enables or disables process accounting. 5 + */ 6 + 7 + #include <stdio.h> 8 + #include <errno.h> 9 + #include <string.h> 10 + #include <sys/wait.h> 11 + 12 + #include "../kselftest.h" 13 + 14 + int main(void) 15 + { 16 + char filename[] = "process_log"; 17 + FILE *fp; 18 + pid_t child_pid; 19 + int sz; 20 + 21 + // Setting up kselftest framework 22 + ksft_print_header(); 23 + ksft_set_plan(1); 24 + 25 + // Check if test is run a root 26 + if (geteuid()) { 27 + ksft_test_result_skip("This test needs root to run!\n"); 28 + return 1; 29 + } 30 + 31 + // Create file to log closed processes 32 + fp = fopen(filename, "w"); 33 + 34 + if (!fp) { 35 + ksft_test_result_error("%s.\n", strerror(errno)); 36 + ksft_finished(); 37 + return 1; 38 + } 39 + 40 + acct(filename); 41 + 42 + // Handle error conditions 43 + if (errno) { 44 + ksft_test_result_error("%s.\n", strerror(errno)); 45 + fclose(fp); 46 + ksft_finished(); 47 + return 1; 48 + } 49 + 50 + // Create child process and wait for it to terminate. 51 + 52 + child_pid = fork(); 53 + 54 + if (child_pid < 0) { 55 + ksft_test_result_error("Creating a child process to log failed\n"); 56 + acct(NULL); 57 + return 1; 58 + } else if (child_pid > 0) { 59 + wait(NULL); 60 + fseek(fp, 0L, SEEK_END); 61 + sz = ftell(fp); 62 + 63 + acct(NULL); 64 + 65 + if (sz <= 0) { 66 + ksft_test_result_fail("Terminated child process not logged\n"); 67 + ksft_exit_fail(); 68 + return 1; 69 + } 70 + 71 + ksft_test_result_pass("Successfully logged terminated process.\n"); 72 + fclose(fp); 73 + ksft_exit_pass(); 74 + return 0; 75 + } 76 + 77 + return 1; 78 + }