Skip to content

Commit f0a1ffa

Browse files
sheunlshuahkh
authored andcommitted
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 <[email protected]> Signed-off-by: Shuah Khan <[email protected]>
1 parent 44b045e commit f0a1ffa

File tree

4 files changed

+87
-0
lines changed

4 files changed

+87
-0
lines changed

tools/testing/selftests/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# SPDX-License-Identifier: GPL-2.0
2+
TARGETS += acct
23
TARGETS += alsa
34
TARGETS += amd-pstate
45
TARGETS += arm64
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
acct_syscall
2+
config
3+
process_log

tools/testing/selftests/acct/Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# SPDX-License-Identifier: GPL-2.0
2+
TEST_GEN_PROGS := acct_syscall
3+
CFLAGS += -Wall
4+
5+
include ../lib.mk
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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+
}

0 commit comments

Comments
 (0)