cgroup_util.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  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 <poll.h>
  7. #include <signal.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <sys/inotify.h>
  12. #include <sys/stat.h>
  13. #include <sys/types.h>
  14. #include <sys/wait.h>
  15. #include <unistd.h>
  16. #include "cgroup_util.h"
  17. #include "../clone3/clone3_selftests.h"
  18. /* Returns read len on success, or -errno on failure. */
  19. static ssize_t read_text(const char *path, char *buf, size_t max_len)
  20. {
  21. ssize_t len;
  22. int fd;
  23. fd = open(path, O_RDONLY);
  24. if (fd < 0)
  25. return -errno;
  26. len = read(fd, buf, max_len - 1);
  27. if (len >= 0)
  28. buf[len] = 0;
  29. close(fd);
  30. return len < 0 ? -errno : len;
  31. }
  32. /* Returns written len on success, or -errno on failure. */
  33. static ssize_t write_text(const char *path, char *buf, ssize_t len)
  34. {
  35. int fd;
  36. fd = open(path, O_WRONLY | O_APPEND);
  37. if (fd < 0)
  38. return -errno;
  39. len = write(fd, buf, len);
  40. close(fd);
  41. return len < 0 ? -errno : len;
  42. }
  43. char *cg_name(const char *root, const char *name)
  44. {
  45. size_t len = strlen(root) + strlen(name) + 2;
  46. char *ret = malloc(len);
  47. snprintf(ret, len, "%s/%s", root, name);
  48. return ret;
  49. }
  50. char *cg_name_indexed(const char *root, const char *name, int index)
  51. {
  52. size_t len = strlen(root) + strlen(name) + 10;
  53. char *ret = malloc(len);
  54. snprintf(ret, len, "%s/%s_%d", root, name, index);
  55. return ret;
  56. }
  57. char *cg_control(const char *cgroup, const char *control)
  58. {
  59. size_t len = strlen(cgroup) + strlen(control) + 2;
  60. char *ret = malloc(len);
  61. snprintf(ret, len, "%s/%s", cgroup, control);
  62. return ret;
  63. }
  64. /* Returns 0 on success, or -errno on failure. */
  65. int cg_read(const char *cgroup, const char *control, char *buf, size_t len)
  66. {
  67. char path[PATH_MAX];
  68. ssize_t ret;
  69. snprintf(path, sizeof(path), "%s/%s", cgroup, control);
  70. ret = read_text(path, buf, len);
  71. return ret >= 0 ? 0 : ret;
  72. }
  73. int cg_read_strcmp(const char *cgroup, const char *control,
  74. const char *expected)
  75. {
  76. size_t size;
  77. char *buf;
  78. int ret;
  79. /* Handle the case of comparing against empty string */
  80. if (!expected)
  81. return -1;
  82. else
  83. size = strlen(expected) + 1;
  84. buf = malloc(size);
  85. if (!buf)
  86. return -1;
  87. if (cg_read(cgroup, control, buf, size)) {
  88. free(buf);
  89. return -1;
  90. }
  91. ret = strcmp(expected, buf);
  92. free(buf);
  93. return ret;
  94. }
  95. int cg_read_strstr(const char *cgroup, const char *control, const char *needle)
  96. {
  97. char buf[PAGE_SIZE];
  98. if (cg_read(cgroup, control, buf, sizeof(buf)))
  99. return -1;
  100. return strstr(buf, needle) ? 0 : -1;
  101. }
  102. long cg_read_long(const char *cgroup, const char *control)
  103. {
  104. char buf[128];
  105. if (cg_read(cgroup, control, buf, sizeof(buf)))
  106. return -1;
  107. return atol(buf);
  108. }
  109. long cg_read_long_fd(int fd)
  110. {
  111. char buf[128];
  112. if (pread(fd, buf, sizeof(buf), 0) <= 0)
  113. return -1;
  114. return atol(buf);
  115. }
  116. long cg_read_key_long(const char *cgroup, const char *control, const char *key)
  117. {
  118. char buf[PAGE_SIZE];
  119. char *ptr;
  120. if (cg_read(cgroup, control, buf, sizeof(buf)))
  121. return -1;
  122. ptr = strstr(buf, key);
  123. if (!ptr)
  124. return -1;
  125. return atol(ptr + strlen(key));
  126. }
  127. long cg_read_lc(const char *cgroup, const char *control)
  128. {
  129. char buf[PAGE_SIZE];
  130. const char delim[] = "\n";
  131. char *line;
  132. long cnt = 0;
  133. if (cg_read(cgroup, control, buf, sizeof(buf)))
  134. return -1;
  135. for (line = strtok(buf, delim); line; line = strtok(NULL, delim))
  136. cnt++;
  137. return cnt;
  138. }
  139. /* Returns 0 on success, or -errno on failure. */
  140. int cg_write(const char *cgroup, const char *control, char *buf)
  141. {
  142. char path[PATH_MAX];
  143. ssize_t len = strlen(buf), ret;
  144. snprintf(path, sizeof(path), "%s/%s", cgroup, control);
  145. ret = write_text(path, buf, len);
  146. return ret == len ? 0 : ret;
  147. }
  148. /*
  149. * Returns fd on success, or -1 on failure.
  150. * (fd should be closed with close() as usual)
  151. */
  152. int cg_open(const char *cgroup, const char *control, int flags)
  153. {
  154. char path[PATH_MAX];
  155. snprintf(path, sizeof(path), "%s/%s", cgroup, control);
  156. return open(path, flags);
  157. }
  158. int cg_write_numeric(const char *cgroup, const char *control, long value)
  159. {
  160. char buf[64];
  161. int ret;
  162. ret = sprintf(buf, "%lu", value);
  163. if (ret < 0)
  164. return ret;
  165. return cg_write(cgroup, control, buf);
  166. }
  167. int cg_find_unified_root(char *root, size_t len, bool *nsdelegate)
  168. {
  169. char buf[10 * PAGE_SIZE];
  170. char *fs, *mount, *type, *options;
  171. const char delim[] = "\n\t ";
  172. if (read_text("/proc/self/mounts", buf, sizeof(buf)) <= 0)
  173. return -1;
  174. /*
  175. * Example:
  176. * cgroup /sys/fs/cgroup cgroup2 rw,seclabel,noexec,relatime 0 0
  177. */
  178. for (fs = strtok(buf, delim); fs; fs = strtok(NULL, delim)) {
  179. mount = strtok(NULL, delim);
  180. type = strtok(NULL, delim);
  181. options = strtok(NULL, delim);
  182. strtok(NULL, delim);
  183. strtok(NULL, delim);
  184. if (strcmp(type, "cgroup2") == 0) {
  185. strncpy(root, mount, len);
  186. if (nsdelegate)
  187. *nsdelegate = !!strstr(options, "nsdelegate");
  188. return 0;
  189. }
  190. }
  191. return -1;
  192. }
  193. int cg_create(const char *cgroup)
  194. {
  195. return mkdir(cgroup, 0755);
  196. }
  197. int cg_wait_for_proc_count(const char *cgroup, int count)
  198. {
  199. char buf[10 * PAGE_SIZE] = {0};
  200. int attempts;
  201. char *ptr;
  202. for (attempts = 10; attempts >= 0; attempts--) {
  203. int nr = 0;
  204. if (cg_read(cgroup, "cgroup.procs", buf, sizeof(buf)))
  205. break;
  206. for (ptr = buf; *ptr; ptr++)
  207. if (*ptr == '\n')
  208. nr++;
  209. if (nr >= count)
  210. return 0;
  211. usleep(100000);
  212. }
  213. return -1;
  214. }
  215. int cg_killall(const char *cgroup)
  216. {
  217. char buf[PAGE_SIZE];
  218. char *ptr = buf;
  219. /* If cgroup.kill exists use it. */
  220. if (!cg_write(cgroup, "cgroup.kill", "1"))
  221. return 0;
  222. if (cg_read(cgroup, "cgroup.procs", buf, sizeof(buf)))
  223. return -1;
  224. while (ptr < buf + sizeof(buf)) {
  225. int pid = strtol(ptr, &ptr, 10);
  226. if (pid == 0)
  227. break;
  228. if (*ptr)
  229. ptr++;
  230. else
  231. break;
  232. if (kill(pid, SIGKILL))
  233. return -1;
  234. }
  235. return 0;
  236. }
  237. int cg_destroy(const char *cgroup)
  238. {
  239. int ret;
  240. if (!cgroup)
  241. return 0;
  242. retry:
  243. ret = rmdir(cgroup);
  244. if (ret && errno == EBUSY) {
  245. cg_killall(cgroup);
  246. usleep(100);
  247. goto retry;
  248. }
  249. if (ret && errno == ENOENT)
  250. ret = 0;
  251. return ret;
  252. }
  253. int cg_enter(const char *cgroup, int pid)
  254. {
  255. char pidbuf[64];
  256. snprintf(pidbuf, sizeof(pidbuf), "%d", pid);
  257. return cg_write(cgroup, "cgroup.procs", pidbuf);
  258. }
  259. int cg_enter_current(const char *cgroup)
  260. {
  261. return cg_write(cgroup, "cgroup.procs", "0");
  262. }
  263. int cg_enter_current_thread(const char *cgroup)
  264. {
  265. return cg_write(cgroup, "cgroup.threads", "0");
  266. }
  267. int cg_run(const char *cgroup,
  268. int (*fn)(const char *cgroup, void *arg),
  269. void *arg)
  270. {
  271. int pid, retcode;
  272. pid = fork();
  273. if (pid < 0) {
  274. return pid;
  275. } else if (pid == 0) {
  276. char buf[64];
  277. snprintf(buf, sizeof(buf), "%d", getpid());
  278. if (cg_write(cgroup, "cgroup.procs", buf))
  279. exit(EXIT_FAILURE);
  280. exit(fn(cgroup, arg));
  281. } else {
  282. waitpid(pid, &retcode, 0);
  283. if (WIFEXITED(retcode))
  284. return WEXITSTATUS(retcode);
  285. else
  286. return -1;
  287. }
  288. }
  289. pid_t clone_into_cgroup(int cgroup_fd)
  290. {
  291. #ifdef CLONE_ARGS_SIZE_VER2
  292. pid_t pid;
  293. struct __clone_args args = {
  294. .flags = CLONE_INTO_CGROUP,
  295. .exit_signal = SIGCHLD,
  296. .cgroup = cgroup_fd,
  297. };
  298. pid = sys_clone3(&args, sizeof(struct __clone_args));
  299. /*
  300. * Verify that this is a genuine test failure:
  301. * ENOSYS -> clone3() not available
  302. * E2BIG -> CLONE_INTO_CGROUP not available
  303. */
  304. if (pid < 0 && (errno == ENOSYS || errno == E2BIG))
  305. goto pretend_enosys;
  306. return pid;
  307. pretend_enosys:
  308. #endif
  309. errno = ENOSYS;
  310. return -ENOSYS;
  311. }
  312. int clone_reap(pid_t pid, int options)
  313. {
  314. int ret;
  315. siginfo_t info = {
  316. .si_signo = 0,
  317. };
  318. again:
  319. ret = waitid(P_PID, pid, &info, options | __WALL | __WNOTHREAD);
  320. if (ret < 0) {
  321. if (errno == EINTR)
  322. goto again;
  323. return -1;
  324. }
  325. if (options & WEXITED) {
  326. if (WIFEXITED(info.si_status))
  327. return WEXITSTATUS(info.si_status);
  328. }
  329. if (options & WSTOPPED) {
  330. if (WIFSTOPPED(info.si_status))
  331. return WSTOPSIG(info.si_status);
  332. }
  333. if (options & WCONTINUED) {
  334. if (WIFCONTINUED(info.si_status))
  335. return 0;
  336. }
  337. return -1;
  338. }
  339. int dirfd_open_opath(const char *dir)
  340. {
  341. return open(dir, O_DIRECTORY | O_CLOEXEC | O_NOFOLLOW | O_PATH);
  342. }
  343. #define close_prot_errno(fd) \
  344. if (fd >= 0) { \
  345. int _e_ = errno; \
  346. close(fd); \
  347. errno = _e_; \
  348. }
  349. static int clone_into_cgroup_run_nowait(const char *cgroup,
  350. int (*fn)(const char *cgroup, void *arg),
  351. void *arg)
  352. {
  353. int cgroup_fd;
  354. pid_t pid;
  355. cgroup_fd = dirfd_open_opath(cgroup);
  356. if (cgroup_fd < 0)
  357. return -1;
  358. pid = clone_into_cgroup(cgroup_fd);
  359. close_prot_errno(cgroup_fd);
  360. if (pid == 0)
  361. exit(fn(cgroup, arg));
  362. return pid;
  363. }
  364. int cg_run_nowait(const char *cgroup,
  365. int (*fn)(const char *cgroup, void *arg),
  366. void *arg)
  367. {
  368. int pid;
  369. pid = clone_into_cgroup_run_nowait(cgroup, fn, arg);
  370. if (pid > 0)
  371. return pid;
  372. /* Genuine test failure. */
  373. if (pid < 0 && errno != ENOSYS)
  374. return -1;
  375. pid = fork();
  376. if (pid == 0) {
  377. char buf[64];
  378. snprintf(buf, sizeof(buf), "%d", getpid());
  379. if (cg_write(cgroup, "cgroup.procs", buf))
  380. exit(EXIT_FAILURE);
  381. exit(fn(cgroup, arg));
  382. }
  383. return pid;
  384. }
  385. int get_temp_fd(void)
  386. {
  387. return open(".", O_TMPFILE | O_RDWR | O_EXCL);
  388. }
  389. int alloc_pagecache(int fd, size_t size)
  390. {
  391. char buf[PAGE_SIZE];
  392. struct stat st;
  393. int i;
  394. if (fstat(fd, &st))
  395. goto cleanup;
  396. size += st.st_size;
  397. if (ftruncate(fd, size))
  398. goto cleanup;
  399. for (i = 0; i < size; i += sizeof(buf))
  400. read(fd, buf, sizeof(buf));
  401. return 0;
  402. cleanup:
  403. return -1;
  404. }
  405. int alloc_anon(const char *cgroup, void *arg)
  406. {
  407. size_t size = (unsigned long)arg;
  408. char *buf, *ptr;
  409. buf = malloc(size);
  410. for (ptr = buf; ptr < buf + size; ptr += PAGE_SIZE)
  411. *ptr = 0;
  412. free(buf);
  413. return 0;
  414. }
  415. int is_swap_enabled(void)
  416. {
  417. char buf[PAGE_SIZE];
  418. const char delim[] = "\n";
  419. int cnt = 0;
  420. char *line;
  421. if (read_text("/proc/swaps", buf, sizeof(buf)) <= 0)
  422. return -1;
  423. for (line = strtok(buf, delim); line; line = strtok(NULL, delim))
  424. cnt++;
  425. return cnt > 1;
  426. }
  427. int set_oom_adj_score(int pid, int score)
  428. {
  429. char path[PATH_MAX];
  430. int fd, len;
  431. sprintf(path, "/proc/%d/oom_score_adj", pid);
  432. fd = open(path, O_WRONLY | O_APPEND);
  433. if (fd < 0)
  434. return fd;
  435. len = dprintf(fd, "%d", score);
  436. if (len < 0) {
  437. close(fd);
  438. return len;
  439. }
  440. close(fd);
  441. return 0;
  442. }
  443. int proc_mount_contains(const char *option)
  444. {
  445. char buf[4 * PAGE_SIZE];
  446. ssize_t read;
  447. read = read_text("/proc/mounts", buf, sizeof(buf));
  448. if (read < 0)
  449. return read;
  450. return strstr(buf, option) != NULL;
  451. }
  452. ssize_t proc_read_text(int pid, bool thread, const char *item, char *buf, size_t size)
  453. {
  454. char path[PATH_MAX];
  455. ssize_t ret;
  456. if (!pid)
  457. snprintf(path, sizeof(path), "/proc/%s/%s",
  458. thread ? "thread-self" : "self", item);
  459. else
  460. snprintf(path, sizeof(path), "/proc/%d/%s", pid, item);
  461. ret = read_text(path, buf, size);
  462. return ret < 0 ? -1 : ret;
  463. }
  464. int proc_read_strstr(int pid, bool thread, const char *item, const char *needle)
  465. {
  466. char buf[PAGE_SIZE];
  467. if (proc_read_text(pid, thread, item, buf, sizeof(buf)) < 0)
  468. return -1;
  469. return strstr(buf, needle) ? 0 : -1;
  470. }
  471. int clone_into_cgroup_run_wait(const char *cgroup)
  472. {
  473. int cgroup_fd;
  474. pid_t pid;
  475. cgroup_fd = dirfd_open_opath(cgroup);
  476. if (cgroup_fd < 0)
  477. return -1;
  478. pid = clone_into_cgroup(cgroup_fd);
  479. close_prot_errno(cgroup_fd);
  480. if (pid < 0)
  481. return -1;
  482. if (pid == 0)
  483. exit(EXIT_SUCCESS);
  484. /*
  485. * We don't care whether this fails. We only care whether the initial
  486. * clone succeeded.
  487. */
  488. (void)clone_reap(pid, WEXITED);
  489. return 0;
  490. }
  491. static int __prepare_for_wait(const char *cgroup, const char *filename)
  492. {
  493. int fd, ret = -1;
  494. fd = inotify_init1(0);
  495. if (fd == -1)
  496. return fd;
  497. ret = inotify_add_watch(fd, cg_control(cgroup, filename), IN_MODIFY);
  498. if (ret == -1) {
  499. close(fd);
  500. fd = -1;
  501. }
  502. return fd;
  503. }
  504. int cg_prepare_for_wait(const char *cgroup)
  505. {
  506. return __prepare_for_wait(cgroup, "cgroup.events");
  507. }
  508. int memcg_prepare_for_wait(const char *cgroup)
  509. {
  510. return __prepare_for_wait(cgroup, "memory.events");
  511. }
  512. int cg_wait_for(int fd)
  513. {
  514. int ret = -1;
  515. struct pollfd fds = {
  516. .fd = fd,
  517. .events = POLLIN,
  518. };
  519. while (true) {
  520. ret = poll(&fds, 1, 10000);
  521. if (ret == -1) {
  522. if (errno == EINTR)
  523. continue;
  524. break;
  525. }
  526. if (ret > 0 && fds.revents & POLLIN) {
  527. ret = 0;
  528. break;
  529. }
  530. }
  531. return ret;
  532. }