1 From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
2 From: Wolfgang Bumiller <w.bumiller@proxmox.com>
3 Date: Fri, 15 May 2020 15:07:07 +0200
4 Subject: [PATCH] cgfsng: deduplicate freeze code
6 Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
8 src/lxc/cgroups/cgfsng.c | 65 ++++++++++++----------------------------
9 1 file changed, 19 insertions(+), 46 deletions(-)
11 diff --git a/src/lxc/cgroups/cgfsng.c b/src/lxc/cgroups/cgfsng.c
12 index 892fd915b..7136d27a8 100644
13 --- a/src/lxc/cgroups/cgfsng.c
14 +++ b/src/lxc/cgroups/cgfsng.c
15 @@ -2042,7 +2042,11 @@ static int freezer_cgroup_events_cb(int fd, uint32_t events, void *cbdata,
16 return LXC_MAINLOOP_CONTINUE;
19 -static int cg_unified_freeze(struct cgroup_ops *ops, int timeout)
20 +static int cg_unified_freeze_do(struct cgroup_ops *ops, int timeout,
21 + const char *state_string,
23 + const char *epoll_error,
24 + const char *wait_error)
26 __do_close int fd = -EBADF;
27 call_cleaner(lxc_mainloop_close) struct lxc_epoll_descr *descr_ptr = NULL;
28 @@ -2067,26 +2071,33 @@ static int cg_unified_freeze(struct cgroup_ops *ops, int timeout)
30 ret = lxc_mainloop_open(&descr);
32 - return log_error_errno(-1, errno, "Failed to create epoll instance to wait for container freeze");
33 + return log_error_errno(-1, errno, "%s", epoll_error);
35 /* automatically cleaned up now */
38 - ret = lxc_mainloop_add_handler(&descr, fd, freezer_cgroup_events_cb, INT_TO_PTR((int){1}));
39 + ret = lxc_mainloop_add_handler(&descr, fd, freezer_cgroup_events_cb, INT_TO_PTR(state_num));
41 return log_error_errno(-1, errno, "Failed to add cgroup.events fd handler to mainloop");
44 - ret = lxc_write_openat(h->container_full_path, "cgroup.freeze", "1", 1);
45 + ret = lxc_write_openat(h->container_full_path, "cgroup.freeze", state_string, 1);
47 return log_error_errno(-1, errno, "Failed to open cgroup.freeze file");
49 if (timeout != 0 && lxc_mainloop(&descr, timeout))
50 - return log_error_errno(-1, errno, "Failed to wait for container to be frozen");
51 + return log_error_errno(-1, errno, "%s", wait_error);
56 +static int cg_unified_freeze(struct cgroup_ops *ops, int timeout)
58 + return cg_unified_freeze_do(ops, timeout, "1", 1,
59 + "Failed to create epoll instance to wait for container freeze",
60 + "Failed to wait for container to be frozen");
63 __cgfsng_ops static int cgfsng_freeze(struct cgroup_ops *ops, int timeout)
65 if (!ops->hierarchies)
66 @@ -2112,47 +2123,9 @@ static int cg_legacy_unfreeze(struct cgroup_ops *ops)
68 static int cg_unified_unfreeze(struct cgroup_ops *ops, int timeout)
70 - __do_close int fd = -EBADF;
71 - call_cleaner(lxc_mainloop_close)struct lxc_epoll_descr *descr_ptr = NULL;
73 - struct lxc_epoll_descr descr;
74 - struct hierarchy *h;
78 - return ret_set_errno(-1, ENOENT);
80 - if (!h->container_full_path)
81 - return ret_set_errno(-1, EEXIST);
84 - __do_free char *events_file = NULL;
86 - events_file = must_make_path(h->container_full_path, "cgroup.events", NULL);
87 - fd = open(events_file, O_RDONLY | O_CLOEXEC);
89 - return log_error_errno(-1, errno, "Failed to open cgroup.events file");
91 - ret = lxc_mainloop_open(&descr);
93 - return log_error_errno(-1, errno, "Failed to create epoll instance to wait for container unfreeze");
95 - /* automatically cleaned up now */
98 - ret = lxc_mainloop_add_handler(&descr, fd, freezer_cgroup_events_cb, INT_TO_PTR((int){0}));
100 - return log_error_errno(-1, errno, "Failed to add cgroup.events fd handler to mainloop");
103 - ret = lxc_write_openat(h->container_full_path, "cgroup.freeze", "0", 1);
105 - return log_error_errno(-1, errno, "Failed to open cgroup.freeze file");
107 - if (timeout != 0 && lxc_mainloop(&descr, timeout))
108 - return log_error_errno(-1, errno, "Failed to wait for container to be unfrozen");
111 + return cg_unified_freeze_do(ops, timeout, "0", 0,
112 + "Failed to create epoll instance to wait for container unfreeze",
113 + "Failed to wait for container to be unfrozen");
116 __cgfsng_ops static int cgfsng_unfreeze(struct cgroup_ops *ops, int timeout)