Skip to content

Commit 4cb4de6

Browse files
committed
conmon: Move OOM setup to helper function
Signed-off-by: Alexander Larsson <[email protected]>
1 parent 34b75c2 commit 4cb4de6

File tree

1 file changed

+29
-29
lines changed

1 file changed

+29
-29
lines changed

conmon/conmon.c

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -892,6 +892,33 @@ static void setup_terminal_control_fifo()
892892
ninfo("ctlfd: %d", ctlfd);
893893
}
894894

895+
static void setup_oom_handling(int cpid)
896+
{
897+
/* Setup OOM notification for container process */
898+
_cleanup_free_ char *memory_cgroup_path = process_cgroup_subsystem_path(cpid, "memory");
899+
if (!memory_cgroup_path) {
900+
nexit("Failed to get memory cgroup path");
901+
}
902+
903+
_cleanup_free_ char *memory_cgroup_file_path = g_build_filename(memory_cgroup_path, "cgroup.event_control", NULL);
904+
905+
if ((cfd = open(memory_cgroup_file_path, O_WRONLY | O_CLOEXEC)) == -1) {
906+
nwarn("Failed to open %s", memory_cgroup_file_path);
907+
return;
908+
}
909+
910+
_cleanup_free_ char *memory_cgroup_file_oom_path = g_build_filename(memory_cgroup_path, "memory.oom_control", NULL);
911+
if ((ofd = open(memory_cgroup_file_oom_path, O_RDONLY | O_CLOEXEC)) == -1)
912+
pexit("Failed to open %s", memory_cgroup_file_oom_path);
913+
914+
if ((oom_efd = eventfd(0, EFD_CLOEXEC)) == -1)
915+
pexit("Failed to create eventfd");
916+
917+
_cleanup_free_ char *data = g_strdup_printf("%d %d", oom_efd, ofd);
918+
if (write_all(cfd, data, strlen(data)) < 0)
919+
pexit("Failed to write to cgroup.event_control");
920+
}
921+
895922
int main(int argc, char *argv[])
896923
{
897924
int ret;
@@ -918,9 +945,6 @@ int main(int argc, char *argv[])
918945
_cleanup_close_ int dev_null_w = -1;
919946
int fds[2];
920947

921-
_cleanup_free_ char *memory_cgroup_path = NULL;
922-
int wb;
923-
924948
main_loop = g_main_loop_new (NULL, FALSE);
925949

926950
/* Command line parameters */
@@ -1194,31 +1218,7 @@ int main(int argc, char *argv[])
11941218
write_sync_fd(sync_pipe_fd, cpid, NULL);
11951219
}
11961220

1197-
/* Setup OOM notification for container process */
1198-
memory_cgroup_path = process_cgroup_subsystem_path(cpid, "memory");
1199-
if (!memory_cgroup_path) {
1200-
nexit("Failed to get memory cgroup path");
1201-
}
1202-
1203-
bool oom_handling_enabled = true;
1204-
_cleanup_free_ char *memory_cgroup_file_path = g_build_filename(memory_cgroup_path, "cgroup.event_control", NULL);
1205-
if ((cfd = open(memory_cgroup_file_path, O_WRONLY | O_CLOEXEC)) == -1) {
1206-
nwarn("Failed to open %s", memory_cgroup_file_path);
1207-
oom_handling_enabled = false;
1208-
}
1209-
1210-
if (oom_handling_enabled) {
1211-
_cleanup_free_ char *memory_cgroup_file_oom_path = g_build_filename(memory_cgroup_path, "memory.oom_control", NULL);
1212-
if ((ofd = open(memory_cgroup_file_oom_path, O_RDONLY | O_CLOEXEC)) == -1)
1213-
pexit("Failed to open %s", memory_cgroup_file_oom_path);
1214-
1215-
if ((oom_efd = eventfd(0, EFD_CLOEXEC)) == -1)
1216-
pexit("Failed to create eventfd");
1217-
1218-
wb = snprintf(buf, BUF_SIZE, "%d %d", oom_efd, ofd);
1219-
if (write_all(cfd, buf, wb) < 0)
1220-
pexit("Failed to write to cgroup.event_control");
1221-
}
1221+
setup_oom_handling(cpid);
12221222

12231223
if (masterfd_stdout >= 0) {
12241224
g_unix_fd_add (masterfd_stdout, G_IO_IN, stdio_cb, GINT_TO_POINTER(STDOUT_PIPE));
@@ -1230,7 +1230,7 @@ int main(int argc, char *argv[])
12301230
}
12311231

12321232
/* Add the OOM event fd to epoll */
1233-
if (oom_handling_enabled) {
1233+
if (oom_efd >= 0) {
12341234
g_unix_fd_add (oom_efd, G_IO_IN, oom_cb, NULL);
12351235
}
12361236

0 commit comments

Comments
 (0)