mirror of
git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
synced 2025-04-13 09:59:31 +00:00
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>
This commit is contained in:
parent
44b045e27c
commit
f0a1ffa6f9
4 changed files with 87 additions and 0 deletions
|
@ -1,4 +1,5 @@
|
|||
# SPDX-License-Identifier: GPL-2.0
|
||||
TARGETS += acct
|
||||
TARGETS += alsa
|
||||
TARGETS += amd-pstate
|
||||
TARGETS += arm64
|
||||
|
|
3
tools/testing/selftests/acct/.gitignore
vendored
Normal file
3
tools/testing/selftests/acct/.gitignore
vendored
Normal file
|
@ -0,0 +1,3 @@
|
|||
acct_syscall
|
||||
config
|
||||
process_log
|
5
tools/testing/selftests/acct/Makefile
Normal file
5
tools/testing/selftests/acct/Makefile
Normal file
|
@ -0,0 +1,5 @@
|
|||
# SPDX-License-Identifier: GPL-2.0
|
||||
TEST_GEN_PROGS := acct_syscall
|
||||
CFLAGS += -Wall
|
||||
|
||||
include ../lib.mk
|
78
tools/testing/selftests/acct/acct_syscall.c
Normal file
78
tools/testing/selftests/acct/acct_syscall.c
Normal file
|
@ -0,0 +1,78 @@
|
|||
// 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>
|
||||
|
||||
#include "../kselftest.h"
|
||||
|
||||
int main(void)
|
||||
{
|
||||
char filename[] = "process_log";
|
||||
FILE *fp;
|
||||
pid_t child_pid;
|
||||
int sz;
|
||||
|
||||
// Setting up kselftest framework
|
||||
ksft_print_header();
|
||||
ksft_set_plan(1);
|
||||
|
||||
// Check if test is run a root
|
||||
if (geteuid()) {
|
||||
ksft_test_result_skip("This test needs root to run!\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Create file to log closed processes
|
||||
fp = fopen(filename, "w");
|
||||
|
||||
if (!fp) {
|
||||
ksft_test_result_error("%s.\n", strerror(errno));
|
||||
ksft_finished();
|
||||
return 1;
|
||||
}
|
||||
|
||||
acct(filename);
|
||||
|
||||
// Handle error conditions
|
||||
if (errno) {
|
||||
ksft_test_result_error("%s.\n", strerror(errno));
|
||||
fclose(fp);
|
||||
ksft_finished();
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Create child process and wait for it to terminate.
|
||||
|
||||
child_pid = fork();
|
||||
|
||||
if (child_pid < 0) {
|
||||
ksft_test_result_error("Creating a child process to log failed\n");
|
||||
acct(NULL);
|
||||
return 1;
|
||||
} else if (child_pid > 0) {
|
||||
wait(NULL);
|
||||
fseek(fp, 0L, SEEK_END);
|
||||
sz = ftell(fp);
|
||||
|
||||
acct(NULL);
|
||||
|
||||
if (sz <= 0) {
|
||||
ksft_test_result_fail("Terminated child process not logged\n");
|
||||
ksft_exit_fail();
|
||||
return 1;
|
||||
}
|
||||
|
||||
ksft_test_result_pass("Successfully logged terminated process.\n");
|
||||
fclose(fp);
|
||||
ksft_exit_pass();
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
Loading…
Add table
Reference in a new issue