drm/bridge: adv7511: Switch to atomic operations
[drm/drm-misc.git] / tools / testing / selftests / acct / acct_syscall.c
blobe44e8fe1f4a3c9acde7dc1132e9e4c2773958580
1 // SPDX-License-Identifier: GPL-2.0
3 /* kselftest for acct() system call
4 * The acct() system call enables or disables process accounting.
5 */
7 #include <stdio.h>
8 #include <errno.h>
9 #include <string.h>
10 #include <sys/wait.h>
12 #include "../kselftest.h"
14 int main(void)
16 char filename[] = "process_log";
17 FILE *fp;
18 pid_t child_pid;
19 int sz;
21 // Setting up kselftest framework
22 ksft_print_header();
23 ksft_set_plan(1);
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;
31 // Create file to log closed processes
32 fp = fopen(filename, "w");
34 if (!fp) {
35 ksft_test_result_error("%s.\n", strerror(errno));
36 ksft_finished();
37 return 1;
40 acct(filename);
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;
50 // Create child process and wait for it to terminate.
52 child_pid = fork();
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);
63 acct(NULL);
65 if (sz <= 0) {
66 ksft_test_result_fail("Terminated child process not logged\n");
67 ksft_exit_fail();
68 return 1;
71 ksft_test_result_pass("Successfully logged terminated process.\n");
72 fclose(fp);
73 ksft_exit_pass();
74 return 0;
77 return 1;