Path: blob/master/tools/testing/selftests/acct/acct_syscall.c
26302 views
// SPDX-License-Identifier: GPL-2.012/* kselftest for acct() system call3* The acct() system call enables or disables process accounting.4*/56#include <stdio.h>7#include <errno.h>8#include <string.h>9#include <sys/wait.h>1011#include "../kselftest.h"1213int main(void)14{15char filename[] = "process_log";16FILE *fp;17pid_t child_pid;18int sz;1920// Setting up kselftest framework21ksft_print_header();22ksft_set_plan(1);2324// Check if test is run a root25if (geteuid()) {26ksft_exit_skip("This test needs root to run!\n");27return 1;28}2930// Create file to log closed processes31fp = fopen(filename, "w");3233if (!fp) {34ksft_test_result_error("%s.\n", strerror(errno));35ksft_finished();36return 1;37}3839acct(filename);4041// Handle error conditions42if (errno) {43ksft_test_result_error("%s.\n", strerror(errno));44fclose(fp);45ksft_finished();46return 1;47}4849// Create child process and wait for it to terminate.5051child_pid = fork();5253if (child_pid < 0) {54ksft_test_result_error("Creating a child process to log failed\n");55acct(NULL);56return 1;57} else if (child_pid > 0) {58wait(NULL);59fseek(fp, 0L, SEEK_END);60sz = ftell(fp);6162acct(NULL);6364if (sz <= 0) {65ksft_test_result_fail("Terminated child process not logged\n");66ksft_exit_fail();67return 1;68}6970ksft_test_result_pass("Successfully logged terminated process.\n");71fclose(fp);72ksft_exit_pass();73return 0;74}7576return 1;77}787980