cgroup_util.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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_run(const char *cgroup,
  177. int (*fn)(const char *cgroup, void *arg),
  178. void *arg)
  179. {
  180. int pid, retcode;
  181. pid = fork();
  182. if (pid < 0) {
  183. return pid;
  184. } else if (pid == 0) {
  185. char buf[64];
  186. snprintf(buf, sizeof(buf), "%d", getpid());
  187. if (cg_write(cgroup, "cgroup.procs", buf))
  188. exit(EXIT_FAILURE);
  189. exit(fn(cgroup, arg));
  190. } else {
  191. waitpid(pid, &retcode, 0);
  192. if (WIFEXITED(retcode))
  193. return WEXITSTATUS(retcode);
  194. else
  195. return -1;
  196. }
  197. }
  198. int cg_run_nowait(const char *cgroup,
  199. int (*fn)(const char *cgroup, void *arg),
  200. void *arg)
  201. {
  202. int pid;
  203. pid = fork();
  204. if (pid == 0) {
  205. char buf[64];
  206. snprintf(buf, sizeof(buf), "%d", getpid());
  207. if (cg_write(cgroup, "cgroup.procs", buf))
  208. exit(EXIT_FAILURE);
  209. exit(fn(cgroup, arg));
  210. }
  211. return pid;
  212. }
  213. int get_temp_fd(void)
  214. {
  215. return open(".", O_TMPFILE | O_RDWR | O_EXCL);
  216. }
  217. int alloc_pagecache(int fd, size_t size)
  218. {
  219. char buf[PAGE_SIZE];
  220. struct stat st;
  221. int i;
  222. if (fstat(fd, &st))
  223. goto cleanup;
  224. size += st.st_size;
  225. if (ftruncate(fd, size))
  226. goto cleanup;
  227. for (i = 0; i < size; i += sizeof(buf))
  228. read(fd, buf, sizeof(buf));
  229. return 0;
  230. cleanup:
  231. return -1;
  232. }
  233. int alloc_anon(const char *cgroup, void *arg)
  234. {
  235. size_t size = (unsigned long)arg;
  236. char *buf, *ptr;
  237. buf = malloc(size);
  238. for (ptr = buf; ptr < buf + size; ptr += PAGE_SIZE)
  239. *ptr = 0;
  240. free(buf);
  241. return 0;
  242. }
  243. int is_swap_enabled(void)
  244. {
  245. char buf[PAGE_SIZE];
  246. const char delim[] = "\n";
  247. int cnt = 0;
  248. char *line;
  249. if (read_text("/proc/swaps", buf, sizeof(buf)) <= 0)
  250. return -1;
  251. for (line = strtok(buf, delim); line; line = strtok(NULL, delim))
  252. cnt++;
  253. return cnt > 1;
  254. }