Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

tests/cgroup: move cg_wait_for(), cg_prepare_for_wait()

as they will be used by the tests for cgroup killing.

Link: https://lore.kernel.org/r/20210503143922.3093755-4-brauner@kernel.org
Cc: Tejun Heo <tj@kernel.org>
Cc: cgroups@vger.kernel.org
Reviewed-by: Shakeel Butt <shakeelb@google.com>
Acked-by: Roman Gushchin <guro@fb.com>
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
Signed-off-by: Tejun Heo <tj@kernel.org>

authored by

Christian Brauner and committed by
Tejun Heo
8075e4f6 0de3103f

+49 -57
+47
tools/testing/selftests/cgroup/cgroup_util.c
··· 5 5 #include <errno.h> 6 6 #include <fcntl.h> 7 7 #include <linux/limits.h> 8 + #include <poll.h> 8 9 #include <signal.h> 9 10 #include <stdio.h> 10 11 #include <stdlib.h> 11 12 #include <string.h> 13 + #include <sys/inotify.h> 12 14 #include <sys/stat.h> 13 15 #include <sys/types.h> 14 16 #include <sys/wait.h> ··· 581 579 */ 582 580 (void)clone_reap(pid, WEXITED); 583 581 return 0; 582 + } 583 + 584 + int cg_prepare_for_wait(const char *cgroup) 585 + { 586 + int fd, ret = -1; 587 + 588 + fd = inotify_init1(0); 589 + if (fd == -1) 590 + return fd; 591 + 592 + ret = inotify_add_watch(fd, cg_control(cgroup, "cgroup.events"), 593 + IN_MODIFY); 594 + if (ret == -1) { 595 + close(fd); 596 + fd = -1; 597 + } 598 + 599 + return fd; 600 + } 601 + 602 + int cg_wait_for(int fd) 603 + { 604 + int ret = -1; 605 + struct pollfd fds = { 606 + .fd = fd, 607 + .events = POLLIN, 608 + }; 609 + 610 + while (true) { 611 + ret = poll(&fds, 1, 10000); 612 + 613 + if (ret == -1) { 614 + if (errno == EINTR) 615 + continue; 616 + 617 + break; 618 + } 619 + 620 + if (ret > 0 && fds.revents & POLLIN) { 621 + ret = 0; 622 + break; 623 + } 624 + } 625 + 626 + return ret; 584 627 }
+2
tools/testing/selftests/cgroup/cgroup_util.h
··· 54 54 extern int clone_reap(pid_t pid, int options); 55 55 extern int clone_into_cgroup_run_wait(const char *cgroup); 56 56 extern int dirfd_open_opath(const char *dir); 57 + extern int cg_prepare_for_wait(const char *cgroup); 58 + extern int cg_wait_for(int fd);
-57
tools/testing/selftests/cgroup/test_freezer.c
··· 7 7 #include <unistd.h> 8 8 #include <stdio.h> 9 9 #include <errno.h> 10 - #include <poll.h> 11 10 #include <stdlib.h> 12 - #include <sys/inotify.h> 13 11 #include <string.h> 14 12 #include <sys/wait.h> 15 13 ··· 50 52 static int cg_freeze_nowait(const char *cgroup, bool freeze) 51 53 { 52 54 return cg_write(cgroup, "cgroup.freeze", freeze ? "1" : "0"); 53 - } 54 - 55 - /* 56 - * Prepare for waiting on cgroup.events file. 57 - */ 58 - static int cg_prepare_for_wait(const char *cgroup) 59 - { 60 - int fd, ret = -1; 61 - 62 - fd = inotify_init1(0); 63 - if (fd == -1) { 64 - debug("Error: inotify_init1() failed\n"); 65 - return fd; 66 - } 67 - 68 - ret = inotify_add_watch(fd, cg_control(cgroup, "cgroup.events"), 69 - IN_MODIFY); 70 - if (ret == -1) { 71 - debug("Error: inotify_add_watch() failed\n"); 72 - close(fd); 73 - fd = -1; 74 - } 75 - 76 - return fd; 77 - } 78 - 79 - /* 80 - * Wait for an event. If there are no events for 10 seconds, 81 - * treat this an error. 82 - */ 83 - static int cg_wait_for(int fd) 84 - { 85 - int ret = -1; 86 - struct pollfd fds = { 87 - .fd = fd, 88 - .events = POLLIN, 89 - }; 90 - 91 - while (true) { 92 - ret = poll(&fds, 1, 10000); 93 - 94 - if (ret == -1) { 95 - if (errno == EINTR) 96 - continue; 97 - debug("Error: poll() failed\n"); 98 - break; 99 - } 100 - 101 - if (ret > 0 && fds.revents & POLLIN) { 102 - ret = 0; 103 - break; 104 - } 105 - } 106 - 107 - return ret; 108 55 } 109 56 110 57 /*