Skip to content

Commit 4934df9

Browse files
authored
Merge pull request #1015 from giuseppe/honor-check-before-update
cgroup: honor checkBeforeUpdate
2 parents 90825ee + fdb26d0 commit 4934df9

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

src/libcrun/cgroup-resources.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -661,6 +661,25 @@ write_memory (int dirfd, bool cgroup2, runtime_spec_schema_config_linux_resource
661661

662662
limit_buf_len = cg_itoa (limit_buf, memory->limit, cgroup2);
663663

664+
if (cgroup2 && memory->check_before_update_present && memory->check_before_update)
665+
{
666+
cleanup_free char *current = NULL;
667+
long long val;
668+
int ret;
669+
670+
ret = read_all_file_at (dirfd, "memory.current", &current, NULL, err);
671+
if (UNLIKELY (ret < 0))
672+
return ret;
673+
674+
errno = 0;
675+
val = strtoll (current, NULL, 10);
676+
if (UNLIKELY (errno))
677+
return crun_make_error (err, errno, "parse memory.current");
678+
679+
if (memory->limit <= val)
680+
return crun_make_error (err, 0, "cannot set the memory limit lower than its current usage");
681+
}
682+
664683
return write_cgroup_file (dirfd, cgroup2 ? "memory.max" : "memory.limit_in_bytes", limit_buf, limit_buf_len, err);
665684
}
666685

0 commit comments

Comments
 (0)