acct_syscall.c 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* kselftest for acct() system call
  3. * The acct() system call enables or disables process accounting.
  4. */
  5. #include <stdio.h>
  6. #include <errno.h>
  7. #include <string.h>
  8. #include <sys/wait.h>
  9. #include "kselftest.h"
  10. int main(void)
  11. {
  12. char filename[] = "process_log";
  13. FILE *fp;
  14. pid_t child_pid;
  15. int sz;
  16. // Setting up kselftest framework
  17. ksft_print_header();
  18. ksft_set_plan(1);
  19. // Check if test is run a root
  20. if (geteuid()) {
  21. ksft_exit_skip("This test needs root to run!\n");
  22. return 1;
  23. }
  24. // Create file to log closed processes
  25. fp = fopen(filename, "w");
  26. if (!fp) {
  27. ksft_test_result_error("%s.\n", strerror(errno));
  28. ksft_finished();
  29. return 1;
  30. }
  31. acct(filename);
  32. // Handle error conditions
  33. if (errno) {
  34. ksft_test_result_error("%s.\n", strerror(errno));
  35. fclose(fp);
  36. ksft_finished();
  37. return 1;
  38. }
  39. // Create child process and wait for it to terminate.
  40. child_pid = fork();
  41. if (child_pid < 0) {
  42. ksft_test_result_error("Creating a child process to log failed\n");
  43. acct(NULL);
  44. return 1;
  45. } else if (child_pid > 0) {
  46. wait(NULL);
  47. fseek(fp, 0L, SEEK_END);
  48. sz = ftell(fp);
  49. acct(NULL);
  50. if (sz <= 0) {
  51. ksft_test_result_fail("Terminated child process not logged\n");
  52. ksft_exit_fail();
  53. return 1;
  54. }
  55. ksft_test_result_pass("Successfully logged terminated process.\n");
  56. fclose(fp);
  57. ksft_exit_pass();
  58. return 0;
  59. }
  60. return 1;
  61. }