cgroup_util.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #define _GNU_SOURCE
  3. #include <errno.h>
  4. #include <fcntl.h>
  5. #include <linux/limits.h>
  6. #include <signal.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <sys/stat.h>
  11. #include <sys/types.h>
  12. #include <sys/wait.h>
  13. #include <unistd.h>
  14. #include "cgroup_util.h"
  15. static ssize_t read_text(const char *path, char *buf, size_t max_len)
  16. {
  17. ssize_t len;
  18. int fd;
  19. fd = open(path, O_RDONLY);
  20. if (fd < 0)
  21. return fd;
  22. len = read(fd, buf, max_len - 1);
  23. if (len < 0)
  24. goto out;
  25. buf[len] = 0;
  26. out:
  27. close(fd);
  28. return len;
  29. }
  30. static ssize_t write_text(const char *path, char *buf, ssize_t len)
  31. {
  32. int fd;
  33. fd = open(path, O_WRONLY | O_APPEND);
  34. if (fd < 0)
  35. return fd;
  36. len = write(fd, buf, len);
  37. if (len < 0) {
  38. close(fd);
  39. return len;
  40. }
  41. close(fd);
  42. return len;
  43. }
  44. char *cg_name(const char *root, const char *name)
  45. {
  46. size_t len = strlen(root) + strlen(name) + 2;
  47. char *ret = malloc(len);
  48. snprintf(ret, len, "%s/%s", root, name);
  49. return ret;
  50. }
  51. char *cg_name_indexed(const char *root, const char *name, int index)
  52. {
  53. size_t len = strlen(root) + strlen(name) + 10;
  54. char *ret = malloc(len);
  55. snprintf(ret, len, "%s/%s_%d", root, name, index);
  56. return ret;
  57. }
  58. int cg_read(const char *cgroup, const char *control, char *buf, size_t len)
  59. {
  60. char path[PATH_MAX];
  61. snprintf(path, sizeof(path), "%s/%s", cgroup, control);
  62. if (read_text(path, buf, len) >= 0)
  63. return 0;
  64. return -1;
  65. }
  66. int cg_read_strcmp(const char *cgroup, const char *control,
  67. const char *expected)
  68. {
  69. size_t size = strlen(expected) + 1;
  70. char *buf;
  71. buf = malloc(size);
  72. if (!buf)
  73. return -1;
  74. if (cg_read(cgroup, control, buf, size))
  75. return -1;
  76. return strcmp(expected, buf);
  77. }
  78. int cg_read_strstr(const char *cgroup, const char *control, const char *needle)
  79. {
  80. char buf[PAGE_SIZE];
  81. if (cg_read(cgroup, control, buf, sizeof(buf)))
  82. return -1;
  83. return strstr(buf, needle) ? 0 : -1;
  84. }
  85. long cg_read_long(const char *cgroup, const char *control)
  86. {
  87. char buf[128];
  88. if (cg_read(cgroup, control, buf, sizeof(buf)))
  89. return -1;
  90. return atol(buf);
  91. }
  92. long cg_read_key_long(const char *cgroup, const char *control, const char *key)
  93. {
  94. char buf[PAGE_SIZE];
  95. char *ptr;
  96. if (cg_read(cgroup, control, buf, sizeof(buf)))
  97. return -1;
  98. ptr = strstr(buf, key);
  99. if (!ptr)
  100. return -1;
  101. return atol(ptr + strlen(key));
  102. }
  103. int cg_write(const char *cgroup, const char *control, char *buf)
  104. {
  105. char path[PATH_MAX];
  106. ssize_t len = strlen(buf);
  107. snprintf(path, sizeof(path), "%s/%s", cgroup, control);
  108. if (write_text(path, buf, len) == len)
  109. return 0;
  110. return -1;
  111. }
  112. int cg_find_unified_root(char *root, size_t len)
  113. {
  114. char buf[10 * PAGE_SIZE];
  115. char *fs, *mount, *type;
  116. const char delim[] = "\n\t ";
  117. if (read_text("/proc/self/mounts", buf, sizeof(buf)) <= 0)
  118. return -1;
  119. /*
  120. * Example:
  121. * cgroup /sys/fs/cgroup cgroup2 rw,seclabel,noexec,relatime 0 0
  122. */
  123. for (fs = strtok(buf, delim); fs; fs = strtok(NULL, delim)) {
  124. mount = strtok(NULL, delim);
  125. type = strtok(NULL, delim);
  126. strtok(NULL, delim);
  127. strtok(NULL, delim);
  128. strtok(NULL, delim);
  129. if (strcmp(fs, "cgroup") == 0 &&
  130. strcmp(type, "cgroup2") == 0) {
  131. strncpy(root, mount, len);
  132. return 0;
  133. }
  134. }
  135. return -1;
  136. }
  137. int cg_create(const char *cgroup)
  138. {
  139. return mkdir(cgroup, 0644);
  140. }
  141. static int cg_killall(const char *cgroup)
  142. {
  143. char buf[PAGE_SIZE];
  144. char *ptr = buf;
  145. if (cg_read(cgroup, "cgroup.procs", buf, sizeof(buf)))
  146. return -1;
  147. while (ptr < buf + sizeof(buf)) {
  148. int pid = strtol(ptr, &ptr, 10);
  149. if (pid == 0)
  150. break;
  151. if (*ptr)
  152. ptr++;
  153. else
  154. break;
  155. if (kill(pid, SIGKILL))
  156. return -1;
  157. }
  158. return 0;
  159. }
  160. int cg_destroy(const char *cgroup)
  161. {
  162. int ret;
  163. retry:
  164. ret = rmdir(cgroup);
  165. if (ret && errno == EBUSY) {
  166. ret = cg_killall(cgroup);
  167. if (ret)
  168. return ret;
  169. usleep(100);
  170. goto retry;
  171. }
  172. if (ret && errno == ENOENT)
  173. ret = 0;
  174. return ret;
  175. }
  176. int cg_enter_current(const char *cgroup)
  177. {
  178. char pidbuf[64];
  179. snprintf(pidbuf, sizeof(pidbuf), "%d", getpid());
  180. return cg_write(cgroup, "cgroup.procs", pidbuf);
  181. }
  182. int cg_run(const char *cgroup,
  183. int (*fn)(const char *cgroup, void *arg),
  184. void *arg)
  185. {
  186. int pid, retcode;
  187. pid = fork();
  188. if (pid < 0) {
  189. return pid;
  190. } else if (pid == 0) {
  191. char buf[64];
  192. snprintf(buf, sizeof(buf), "%d", getpid());
  193. if (cg_write(cgroup, "cgroup.procs", buf))
  194. exit(EXIT_FAILURE);
  195. exit(fn(cgroup, arg));
  196. } else {
  197. waitpid(pid, &retcode, 0);
  198. if (WIFEXITED(retcode))
  199. return WEXITSTATUS(retcode);
  200. else
  201. return -1;
  202. }
  203. }
  204. int cg_run_nowait(const char *cgroup,
  205. int (*fn)(const char *cgroup, void *arg),
  206. void *arg)
  207. {
  208. int pid;
  209. pid = fork();
  210. if (pid == 0) {
  211. char buf[64];
  212. snprintf(buf, sizeof(buf), "%d", getpid());
  213. if (cg_write(cgroup, "cgroup.procs", buf))
  214. exit(EXIT_FAILURE);
  215. exit(fn(cgroup, arg));
  216. }
  217. return pid;
  218. }
  219. int get_temp_fd(void)
  220. {
  221. return open(".", O_TMPFILE | O_RDWR | O_EXCL);
  222. }
  223. int alloc_pagecache(int fd, size_t size)
  224. {
  225. char buf[PAGE_SIZE];
  226. struct stat st;
  227. int i;
  228. if (fstat(fd, &st))
  229. goto cleanup;
  230. size += st.st_size;
  231. if (ftruncate(fd, size))
  232. goto cleanup;
  233. for (i = 0; i < size; i += sizeof(buf))
  234. read(fd, buf, sizeof(buf));
  235. return 0;
  236. cleanup:
  237. return -1;
  238. }
  239. int alloc_anon(const char *cgroup, void *arg)
  240. {
  241. size_t size = (unsigned long)arg;
  242. char *buf, *ptr;
  243. buf = malloc(size);
  244. for (ptr = buf; ptr < buf + size; ptr += PAGE_SIZE)
  245. *ptr = 0;
  246. free(buf);
  247. return 0;
  248. }
  249. int is_swap_enabled(void)
  250. {
  251. char buf[PAGE_SIZE];
  252. const char delim[] = "\n";
  253. int cnt = 0;
  254. char *line;
  255. if (read_text("/proc/swaps", buf, sizeof(buf)) <= 0)
  256. return -1;
  257. for (line = strtok(buf, delim); line; line = strtok(NULL, delim))
  258. cnt++;
  259. return cnt > 1;
  260. }