test_pids.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. // SPDX-License-Identifier: GPL-2.0
  2. #define _GNU_SOURCE
  3. #include <errno.h>
  4. #include <linux/limits.h>
  5. #include <signal.h>
  6. #include <string.h>
  7. #include <sys/stat.h>
  8. #include <sys/types.h>
  9. #include <unistd.h>
  10. #include "kselftest.h"
  11. #include "cgroup_util.h"
  12. static int run_success(const char *cgroup, void *arg)
  13. {
  14. return 0;
  15. }
  16. static int run_pause(const char *cgroup, void *arg)
  17. {
  18. return pause();
  19. }
  20. /*
  21. * This test checks that pids.max prevents forking new children above the
  22. * specified limit in the cgroup.
  23. */
  24. static int test_pids_max(const char *root)
  25. {
  26. int ret = KSFT_FAIL;
  27. char *cg_pids;
  28. int pid;
  29. cg_pids = cg_name(root, "pids_test");
  30. if (!cg_pids)
  31. goto cleanup;
  32. if (cg_create(cg_pids))
  33. goto cleanup;
  34. if (cg_read_strcmp(cg_pids, "pids.max", "max\n"))
  35. goto cleanup;
  36. if (cg_write(cg_pids, "pids.max", "2"))
  37. goto cleanup;
  38. if (cg_enter_current(cg_pids))
  39. goto cleanup;
  40. pid = cg_run_nowait(cg_pids, run_pause, NULL);
  41. if (pid < 0)
  42. goto cleanup;
  43. if (cg_run_nowait(cg_pids, run_success, NULL) != -1 || errno != EAGAIN)
  44. goto cleanup;
  45. if (kill(pid, SIGINT))
  46. goto cleanup;
  47. ret = KSFT_PASS;
  48. cleanup:
  49. cg_enter_current(root);
  50. cg_destroy(cg_pids);
  51. free(cg_pids);
  52. return ret;
  53. }
  54. /*
  55. * This test checks that pids.events are counted in cgroup associated with pids.max
  56. */
  57. static int test_pids_events(const char *root)
  58. {
  59. int ret = KSFT_FAIL;
  60. char *cg_parent = NULL, *cg_child = NULL;
  61. int pid;
  62. if (cgroup_feature("pids_localevents") <= 0)
  63. return KSFT_SKIP;
  64. cg_parent = cg_name(root, "pids_parent");
  65. cg_child = cg_name(cg_parent, "pids_child");
  66. if (!cg_parent || !cg_child)
  67. goto cleanup;
  68. if (cg_create(cg_parent))
  69. goto cleanup;
  70. if (cg_write(cg_parent, "cgroup.subtree_control", "+pids"))
  71. goto cleanup;
  72. if (cg_create(cg_child))
  73. goto cleanup;
  74. if (cg_write(cg_parent, "pids.max", "2"))
  75. goto cleanup;
  76. if (cg_read_strcmp(cg_child, "pids.max", "max\n"))
  77. goto cleanup;
  78. if (cg_enter_current(cg_child))
  79. goto cleanup;
  80. pid = cg_run_nowait(cg_child, run_pause, NULL);
  81. if (pid < 0)
  82. goto cleanup;
  83. if (cg_run_nowait(cg_child, run_success, NULL) != -1 || errno != EAGAIN)
  84. goto cleanup;
  85. if (kill(pid, SIGINT))
  86. goto cleanup;
  87. if (cg_read_key_long(cg_child, "pids.events", "max ") != 0)
  88. goto cleanup;
  89. if (cg_read_key_long(cg_parent, "pids.events", "max ") != 1)
  90. goto cleanup;
  91. ret = KSFT_PASS;
  92. cleanup:
  93. cg_enter_current(root);
  94. if (cg_child)
  95. cg_destroy(cg_child);
  96. if (cg_parent)
  97. cg_destroy(cg_parent);
  98. free(cg_child);
  99. free(cg_parent);
  100. return ret;
  101. }
  102. #define T(x) { x, #x }
  103. struct pids_test {
  104. int (*fn)(const char *root);
  105. const char *name;
  106. } tests[] = {
  107. T(test_pids_max),
  108. T(test_pids_events),
  109. };
  110. #undef T
  111. int main(int argc, char **argv)
  112. {
  113. char root[PATH_MAX];
  114. ksft_print_header();
  115. ksft_set_plan(ARRAY_SIZE(tests));
  116. if (cg_find_unified_root(root, sizeof(root), NULL))
  117. ksft_exit_skip("cgroup v2 isn't mounted\n");
  118. /*
  119. * Check that pids controller is available:
  120. * pids is listed in cgroup.controllers
  121. */
  122. if (cg_read_strstr(root, "cgroup.controllers", "pids"))
  123. ksft_exit_skip("pids controller isn't available\n");
  124. if (cg_read_strstr(root, "cgroup.subtree_control", "pids"))
  125. if (cg_write(root, "cgroup.subtree_control", "+pids"))
  126. ksft_exit_skip("Failed to set pids controller\n");
  127. for (int i = 0; i < ARRAY_SIZE(tests); i++) {
  128. switch (tests[i].fn(root)) {
  129. case KSFT_PASS:
  130. ksft_test_result_pass("%s\n", tests[i].name);
  131. break;
  132. case KSFT_SKIP:
  133. ksft_test_result_skip("%s\n", tests[i].name);
  134. break;
  135. default:
  136. ksft_test_result_fail("%s\n", tests[i].name);
  137. break;
  138. }
  139. }
  140. ksft_finished();
  141. }