From 78cb7709c70f73b0afd5410c8a4e3bac8bb5d312 Mon Sep 17 00:00:00 2001 From: Mrunal Patel Date: Mon, 22 May 2017 19:02:21 -0700 Subject: [PATCH] conmon: Setup cgroups for container pid OOM notification Signed-off-by: Mrunal Patel --- conmon/conmon.c | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/conmon/conmon.c b/conmon/conmon.c index 308c3598..f4ca8f48 100644 --- a/conmon/conmon.c +++ b/conmon/conmon.c @@ -13,6 +13,7 @@ #include #include #include +#include #include #include @@ -326,6 +327,14 @@ int main(int argc, char *argv[]) GOptionContext *context; _cleanup_gstring_ GString *cmd = NULL; + /* Used for OOM notification API */ + _cleanup_close_ int efd = -1; + _cleanup_close_ int cfd = -1; + _cleanup_close_ int ofd = -1; + _cleanup_free_ char *memory_cgroup_path = NULL; + int wb; + uint64_t oom_event; + /* Command line parameters */ context = g_option_context_new("- conmon utility"); g_option_context_add_main_entries(context, entries, "conmon"); @@ -614,6 +623,28 @@ int main(int argc, char *argv[]) } } + /* Setup OOM notification for container process */ + memory_cgroup_path = process_cgroup_subsystem_path(cpid, "memory"); + if (!memory_cgroup_path) { + nexit("Failed to get memory cgroup path"); + } + + char memory_cgroup_file_path[PATH_MAX]; + snprintf(memory_cgroup_file_path, PATH_MAX, "%s/cgroup.event_control", memory_cgroup_path); + if ((cfd = open(memory_cgroup_file_path, O_WRONLY)) == -1) + pexit("Failed to open %s", memory_cgroup_file_path); + + snprintf(memory_cgroup_file_path, PATH_MAX, "%s/memory.oom_control", memory_cgroup_path); + if ((ofd = open(memory_cgroup_file_path, O_RDONLY)) == -1) + pexit("Failed to open %s", memory_cgroup_file_path); + + if ((efd = eventfd(0, 0)) == -1) + pexit("Failed to create eventfd"); + + wb = snprintf(buf, BUF_SIZE, "%d %d", efd, ofd); + if (write(cfd, buf, wb) < 0) + pexit("Failed to write to cgroup.event_control"); + /* Create epoll_ctl so that we can handle read/write events. */ /* * TODO: Switch to libuv so that we can also implement exec as well as