cgroup_helpers.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. // SPDX-License-Identifier: GPL-2.0
  2. #define _GNU_SOURCE
  3. #include <sched.h>
  4. #include <sys/mount.h>
  5. #include <sys/stat.h>
  6. #include <sys/types.h>
  7. #include <linux/limits.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <linux/sched.h>
  11. #include <fcntl.h>
  12. #include <unistd.h>
  13. #include <ftw.h>
  14. #include "cgroup_helpers.h"
  15. /*
  16. * To avoid relying on the system setup, when setup_cgroup_env is called
  17. * we create a new mount namespace, and cgroup namespace. The cgroup2
  18. * root is mounted at CGROUP_MOUNT_PATH
  19. *
  20. * Unfortunately, most people don't have cgroupv2 enabled at this point in time.
  21. * It's easier to create our own mount namespace and manage it ourselves.
  22. *
  23. * We assume /mnt exists.
  24. */
  25. #define WALK_FD_LIMIT 16
  26. #define CGROUP_MOUNT_PATH "/mnt"
  27. #define CGROUP_WORK_DIR "/cgroup-test-work-dir"
  28. #define format_cgroup_path(buf, path) \
  29. snprintf(buf, sizeof(buf), "%s%s%s", CGROUP_MOUNT_PATH, \
  30. CGROUP_WORK_DIR, path)
  31. /**
  32. * setup_cgroup_environment() - Setup the cgroup environment
  33. *
  34. * After calling this function, cleanup_cgroup_environment should be called
  35. * once testing is complete.
  36. *
  37. * This function will print an error to stderr and return 1 if it is unable
  38. * to setup the cgroup environment. If setup is successful, 0 is returned.
  39. */
  40. int setup_cgroup_environment(void)
  41. {
  42. char cgroup_workdir[PATH_MAX - 24];
  43. format_cgroup_path(cgroup_workdir, "");
  44. if (unshare(CLONE_NEWNS)) {
  45. log_err("unshare");
  46. return 1;
  47. }
  48. if (mount("none", "/", NULL, MS_REC | MS_PRIVATE, NULL)) {
  49. log_err("mount fakeroot");
  50. return 1;
  51. }
  52. if (mount("none", CGROUP_MOUNT_PATH, "cgroup2", 0, NULL) && errno != EBUSY) {
  53. log_err("mount cgroup2");
  54. return 1;
  55. }
  56. /* Cleanup existing failed runs, now that the environment is setup */
  57. cleanup_cgroup_environment();
  58. if (mkdir(cgroup_workdir, 0777) && errno != EEXIST) {
  59. log_err("mkdir cgroup work dir");
  60. return 1;
  61. }
  62. return 0;
  63. }
  64. static int nftwfunc(const char *filename, const struct stat *statptr,
  65. int fileflags, struct FTW *pfwt)
  66. {
  67. if ((fileflags & FTW_D) && rmdir(filename))
  68. log_err("Removing cgroup: %s", filename);
  69. return 0;
  70. }
  71. static int join_cgroup_from_top(char *cgroup_path)
  72. {
  73. char cgroup_procs_path[PATH_MAX + 1];
  74. pid_t pid = getpid();
  75. int fd, rc = 0;
  76. snprintf(cgroup_procs_path, sizeof(cgroup_procs_path),
  77. "%s/cgroup.procs", cgroup_path);
  78. fd = open(cgroup_procs_path, O_WRONLY);
  79. if (fd < 0) {
  80. log_err("Opening Cgroup Procs: %s", cgroup_procs_path);
  81. return 1;
  82. }
  83. if (dprintf(fd, "%d\n", pid) < 0) {
  84. log_err("Joining Cgroup");
  85. rc = 1;
  86. }
  87. close(fd);
  88. return rc;
  89. }
  90. /**
  91. * join_cgroup() - Join a cgroup
  92. * @path: The cgroup path, relative to the workdir, to join
  93. *
  94. * This function expects a cgroup to already be created, relative to the cgroup
  95. * work dir, and it joins it. For example, passing "/my-cgroup" as the path
  96. * would actually put the calling process into the cgroup
  97. * "/cgroup-test-work-dir/my-cgroup"
  98. *
  99. * On success, it returns 0, otherwise on failure it returns 1.
  100. */
  101. int join_cgroup(const char *path)
  102. {
  103. char cgroup_path[PATH_MAX + 1];
  104. format_cgroup_path(cgroup_path, path);
  105. return join_cgroup_from_top(cgroup_path);
  106. }
  107. /**
  108. * cleanup_cgroup_environment() - Cleanup Cgroup Testing Environment
  109. *
  110. * This is an idempotent function to delete all temporary cgroups that
  111. * have been created during the test, including the cgroup testing work
  112. * directory.
  113. *
  114. * At call time, it moves the calling process to the root cgroup, and then
  115. * runs the deletion process. It is idempotent, and should not fail, unless
  116. * a process is lingering.
  117. *
  118. * On failure, it will print an error to stderr, and try to continue.
  119. */
  120. void cleanup_cgroup_environment(void)
  121. {
  122. char cgroup_workdir[PATH_MAX + 1];
  123. format_cgroup_path(cgroup_workdir, "");
  124. join_cgroup_from_top(CGROUP_MOUNT_PATH);
  125. nftw(cgroup_workdir, nftwfunc, WALK_FD_LIMIT, FTW_DEPTH | FTW_MOUNT);
  126. }
  127. /**
  128. * create_and_get_cgroup() - Create a cgroup, relative to workdir, and get the FD
  129. * @path: The cgroup path, relative to the workdir, to join
  130. *
  131. * This function creates a cgroup under the top level workdir and returns the
  132. * file descriptor. It is idempotent.
  133. *
  134. * On success, it returns the file descriptor. On failure it returns 0.
  135. * If there is a failure, it prints the error to stderr.
  136. */
  137. int create_and_get_cgroup(const char *path)
  138. {
  139. char cgroup_path[PATH_MAX + 1];
  140. int fd;
  141. format_cgroup_path(cgroup_path, path);
  142. if (mkdir(cgroup_path, 0777) && errno != EEXIST) {
  143. log_err("mkdiring cgroup %s .. %s", path, cgroup_path);
  144. return 0;
  145. }
  146. fd = open(cgroup_path, O_RDONLY);
  147. if (fd < 0) {
  148. log_err("Opening Cgroup");
  149. return 0;
  150. }
  151. return fd;
  152. }
  153. /**
  154. * get_cgroup_id() - Get cgroup id for a particular cgroup path
  155. * @path: The cgroup path, relative to the workdir, to join
  156. *
  157. * On success, it returns the cgroup id. On failure it returns 0,
  158. * which is an invalid cgroup id.
  159. * If there is a failure, it prints the error to stderr.
  160. */
  161. unsigned long long get_cgroup_id(const char *path)
  162. {
  163. int dirfd, err, flags, mount_id, fhsize;
  164. union {
  165. unsigned long long cgid;
  166. unsigned char raw_bytes[8];
  167. } id;
  168. char cgroup_workdir[PATH_MAX + 1];
  169. struct file_handle *fhp, *fhp2;
  170. unsigned long long ret = 0;
  171. format_cgroup_path(cgroup_workdir, path);
  172. dirfd = AT_FDCWD;
  173. flags = 0;
  174. fhsize = sizeof(*fhp);
  175. fhp = calloc(1, fhsize);
  176. if (!fhp) {
  177. log_err("calloc");
  178. return 0;
  179. }
  180. err = name_to_handle_at(dirfd, cgroup_workdir, fhp, &mount_id, flags);
  181. if (err >= 0 || fhp->handle_bytes != 8) {
  182. log_err("name_to_handle_at");
  183. goto free_mem;
  184. }
  185. fhsize = sizeof(struct file_handle) + fhp->handle_bytes;
  186. fhp2 = realloc(fhp, fhsize);
  187. if (!fhp2) {
  188. log_err("realloc");
  189. goto free_mem;
  190. }
  191. err = name_to_handle_at(dirfd, cgroup_workdir, fhp2, &mount_id, flags);
  192. fhp = fhp2;
  193. if (err < 0) {
  194. log_err("name_to_handle_at");
  195. goto free_mem;
  196. }
  197. memcpy(id.raw_bytes, fhp->f_handle, 8);
  198. ret = id.cgid;
  199. free_mem:
  200. free(fhp);
  201. return ret;
  202. }