Skip to content

Commit 8075e4f

Browse files
Christian Braunerhtejun
authored andcommitted
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/[email protected] Cc: Tejun Heo <[email protected]> Cc: [email protected] Reviewed-by: Shakeel Butt <[email protected]> Acked-by: Roman Gushchin <[email protected]> Signed-off-by: Christian Brauner <[email protected]> Signed-off-by: Tejun Heo <[email protected]>
1 parent 0de3103 commit 8075e4f

File tree

3 files changed

+49
-57
lines changed

3 files changed

+49
-57
lines changed

tools/testing/selftests/cgroup/cgroup_util.c

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
#include <errno.h>
66
#include <fcntl.h>
77
#include <linux/limits.h>
8+
#include <poll.h>
89
#include <signal.h>
910
#include <stdio.h>
1011
#include <stdlib.h>
1112
#include <string.h>
13+
#include <sys/inotify.h>
1214
#include <sys/stat.h>
1315
#include <sys/types.h>
1416
#include <sys/wait.h>
@@ -580,3 +582,48 @@ int clone_into_cgroup_run_wait(const char *cgroup)
580582
(void)clone_reap(pid, WEXITED);
581583
return 0;
582584
}
585+
586+
int cg_prepare_for_wait(const char *cgroup)
587+
{
588+
int fd, ret = -1;
589+
590+
fd = inotify_init1(0);
591+
if (fd == -1)
592+
return fd;
593+
594+
ret = inotify_add_watch(fd, cg_control(cgroup, "cgroup.events"),
595+
IN_MODIFY);
596+
if (ret == -1) {
597+
close(fd);
598+
fd = -1;
599+
}
600+
601+
return fd;
602+
}
603+
604+
int cg_wait_for(int fd)
605+
{
606+
int ret = -1;
607+
struct pollfd fds = {
608+
.fd = fd,
609+
.events = POLLIN,
610+
};
611+
612+
while (true) {
613+
ret = poll(&fds, 1, 10000);
614+
615+
if (ret == -1) {
616+
if (errno == EINTR)
617+
continue;
618+
619+
break;
620+
}
621+
622+
if (ret > 0 && fds.revents & POLLIN) {
623+
ret = 0;
624+
break;
625+
}
626+
}
627+
628+
return ret;
629+
}

tools/testing/selftests/cgroup/cgroup_util.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,5 @@ extern pid_t clone_into_cgroup(int cgroup_fd);
5454
extern int clone_reap(pid_t pid, int options);
5555
extern int clone_into_cgroup_run_wait(const char *cgroup);
5656
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);

tools/testing/selftests/cgroup/test_freezer.c

Lines changed: 0 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@
77
#include <unistd.h>
88
#include <stdio.h>
99
#include <errno.h>
10-
#include <poll.h>
1110
#include <stdlib.h>
12-
#include <sys/inotify.h>
1311
#include <string.h>
1412
#include <sys/wait.h>
1513

@@ -54,61 +52,6 @@ static int cg_freeze_nowait(const char *cgroup, bool freeze)
5452
return cg_write(cgroup, "cgroup.freeze", freeze ? "1" : "0");
5553
}
5654

57-
/*
58-
* Prepare for waiting on cgroup.events file.
59-
*/
60-
static int cg_prepare_for_wait(const char *cgroup)
61-
{
62-
int fd, ret = -1;
63-
64-
fd = inotify_init1(0);
65-
if (fd == -1) {
66-
debug("Error: inotify_init1() failed\n");
67-
return fd;
68-
}
69-
70-
ret = inotify_add_watch(fd, cg_control(cgroup, "cgroup.events"),
71-
IN_MODIFY);
72-
if (ret == -1) {
73-
debug("Error: inotify_add_watch() failed\n");
74-
close(fd);
75-
fd = -1;
76-
}
77-
78-
return fd;
79-
}
80-
81-
/*
82-
* Wait for an event. If there are no events for 10 seconds,
83-
* treat this an error.
84-
*/
85-
static int cg_wait_for(int fd)
86-
{
87-
int ret = -1;
88-
struct pollfd fds = {
89-
.fd = fd,
90-
.events = POLLIN,
91-
};
92-
93-
while (true) {
94-
ret = poll(&fds, 1, 10000);
95-
96-
if (ret == -1) {
97-
if (errno == EINTR)
98-
continue;
99-
debug("Error: poll() failed\n");
100-
break;
101-
}
102-
103-
if (ret > 0 && fds.revents & POLLIN) {
104-
ret = 0;
105-
break;
106-
}
107-
}
108-
109-
return ret;
110-
}
111-
11255
/*
11356
* Attach a task to the given cgroup and wait for a cgroup frozen event.
11457
* All transient events (e.g. populated) are ignored.

0 commit comments

Comments
 (0)