File: acct_syscall.c

package info (click to toggle)
linux 6.12.8-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,673,568 kB
  • sloc: ansic: 25,888,630; asm: 268,782; sh: 136,481; python: 64,809; makefile: 55,668; perl: 38,052; xml: 19,270; cpp: 5,893; yacc: 4,923; lex: 2,939; awk: 1,592; sed: 28; ruby: 25
file content (78 lines) | stat: -rw-r--r-- 1,430 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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;
}