Skip to content

Commit

Permalink
Rename symbols
Browse files Browse the repository at this point in the history
  • Loading branch information
Moe-hacker committed Nov 21, 2024
1 parent 65fb5c6 commit 61fa27e
Show file tree
Hide file tree
Showing 22 changed files with 641 additions and 643 deletions.
16 changes: 8 additions & 8 deletions src/caplist.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
*/
#include "include/ruri.h"
// Add a cap to caplist.
void add_to_caplist(cap_value_t *_Nonnull list, cap_value_t cap)
void ruri_add_to_caplist(cap_value_t *_Nonnull list, cap_value_t cap)
{
/*
* If cap is already in list, just do nothing and quit.
Expand All @@ -39,7 +39,7 @@ void add_to_caplist(cap_value_t *_Nonnull list, cap_value_t cap)
if (!CAP_IS_SUPPORTED(cap)) {
return;
}
if (!is_in_caplist(list, cap)) {
if (!ruri_is_in_caplist(list, cap)) {
for (int k = 0; true; k++) {
if (list[k] == INIT_VALUE) {
list[k] = cap;
Expand All @@ -50,7 +50,7 @@ void add_to_caplist(cap_value_t *_Nonnull list, cap_value_t cap)
}
}
// Check if the cap is in the list.
bool is_in_caplist(const cap_value_t *_Nonnull list, cap_value_t cap)
bool ruri_is_in_caplist(const cap_value_t *_Nonnull list, cap_value_t cap)
{
/*
* If cap is in list, return true,
Expand All @@ -68,7 +68,7 @@ bool is_in_caplist(const cap_value_t *_Nonnull list, cap_value_t cap)
return false;
}
// Del a cap from caplist.
void del_from_caplist(cap_value_t *_Nonnull list, cap_value_t cap)
void ruri_del_from_caplist(cap_value_t *_Nonnull list, cap_value_t cap)
{
/*
* If the cap is not in list, just do nothing and quit.
Expand All @@ -88,7 +88,7 @@ void del_from_caplist(cap_value_t *_Nonnull list, cap_value_t cap)
}
}
}
void build_caplist(cap_value_t caplist[], bool privileged, cap_value_t drop_caplist_extra[], cap_value_t keep_caplist_extra[])
void ruri_build_caplist(cap_value_t caplist[], bool privileged, cap_value_t drop_caplist_extra[], cap_value_t keep_caplist_extra[])
{
/*
* If privileged is true, we setup a full list of all capabilities,
Expand All @@ -114,7 +114,7 @@ void build_caplist(cap_value_t caplist[], bool privileged, cap_value_t drop_capl
if (keep_caplist_common[i] == INIT_VALUE) {
break;
}
del_from_caplist(caplist, keep_caplist_common[i]);
ruri_del_from_caplist(caplist, keep_caplist_common[i]);
}
}
// Add drop_caplist_extra[] to caplist.
Expand All @@ -123,7 +123,7 @@ void build_caplist(cap_value_t caplist[], bool privileged, cap_value_t drop_capl
if (drop_caplist_extra[i] == INIT_VALUE) {
break;
}
add_to_caplist(caplist, drop_caplist_extra[i]);
ruri_add_to_caplist(caplist, drop_caplist_extra[i]);
}
}
// Del keep_caplist_extra[] from caplist.
Expand All @@ -132,7 +132,7 @@ void build_caplist(cap_value_t caplist[], bool privileged, cap_value_t drop_capl
if (keep_caplist_extra[i] == INIT_VALUE) {
break;
}
del_from_caplist(caplist, keep_caplist_extra[i]);
ruri_del_from_caplist(caplist, keep_caplist_extra[i]);
}
}
}
76 changes: 38 additions & 38 deletions src/cgroup.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ static void mount_cgroup_v1_memory(void)
// Mount memory controller.
mkdir("/sys/fs/cgroup/memory", S_IRUSR | S_IWUSR);
mount("none", "/sys/fs/cgroup/memory", "cgroup", MS_NOSUID | MS_NODEV | MS_NOEXEC | MS_RELATIME, "memory");
log("{base}Tried to mount cgroup v1 memory\n");
ruri_log("{base}Tried to mount cgroup v1 memory\n");
}
static void mount_cgroup_v1_cpu(void)
{
Expand All @@ -60,7 +60,7 @@ static void mount_cgroup_v1_cpu(void)
// Mount cpu controller.
mkdir("/sys/fs/cgroup/cpu", S_IRUSR | S_IWUSR);
mount("none", "/sys/fs/cgroup/cpu", "cgroup", MS_NOSUID | MS_NODEV | MS_NOEXEC | MS_RELATIME, "cpu");
log("{base}Tried to mount cgroup v1 cpu\n");
ruri_log("{base}Tried to mount cgroup v1 cpu\n");
}
static void mount_cgroup_v1_cpuset(void)
{
Expand All @@ -77,7 +77,7 @@ static void mount_cgroup_v1_cpuset(void)
// Mount cpuset controller.
mkdir("/sys/fs/cgroup/cpuset", S_IRUSR | S_IWUSR);
mount("none", "/sys/fs/cgroup/cpuset", "cgroup", MS_NOSUID | MS_NODEV | MS_NOEXEC | MS_RELATIME, "cpuset");
log("{base}Tried to mount cgroup v1 cpuset\n");
ruri_log("{base}Tried to mount cgroup v1 cpuset\n");
}
static bool is_cgroupv2_support(const char *_Nonnull type)
{
Expand All @@ -104,33 +104,33 @@ static bool is_cgroupv2_support(const char *_Nonnull type)
int fd = open("/sys/fs/cgroup/cgroup.controllers", O_RDONLY | O_CLOEXEC);
if (fd < 0) {
umount2("/sys/fs/cgroup", MNT_DETACH | MNT_FORCE);
log("{base}Cgroup v2 does not support %s\n", type);
log("{base}cgroup.controllers does not exist\n");
ruri_log("{base}Cgroup v2 does not support %s\n", type);
ruri_log("{base}cgroup.controllers does not exist\n");
return false;
}
char buf[256] = { '\0' };
ssize_t len = read(fd, buf, 255);
if (len <= 0) {
umount2("/sys/fs/cgroup", MNT_DETACH | MNT_FORCE);
log("{base}Cgroup v2 does not support %s\n", type);
log("{base}cgroup.controllers read failed\n");
ruri_log("{base}Cgroup v2 does not support %s\n", type);
ruri_log("{base}cgroup.controllers read failed\n");
return false;
}
buf[len] = '\0';
char str_to_find[32] = { '\0' };
log("{base}cgroup.controllers: %s\n", buf);
ruri_log("{base}cgroup.controllers: %s\n", buf);
sprintf(str_to_find, "%s ", type);
log("{base}str_to_find: %s\n", str_to_find);
ruri_log("{base}str_to_find: %s\n", str_to_find);
if (strstr(buf, str_to_find) != NULL) {
umount2("/sys/fs/cgroup", MNT_DETACH | MNT_FORCE);
log("{base}Cgroup v2 supports %s\n", type);
ruri_log("{base}Cgroup v2 supports %s\n", type);
return true;
}
umount2("/sys/fs/cgroup", MNT_DETACH | MNT_FORCE);
log("{base}Cgroup v2 does not support %s\n", type);
ruri_log("{base}Cgroup v2 does not support %s\n", type);
return false;
}
static void set_cgroup_v1_memory(const struct CONTAINER *_Nonnull container)
static void set_cgroup_v1_memory(const struct RURI_CONTAINER *_Nonnull container)
{
mount_cgroup_v1_memory();
pid_t pid = getpid();
Expand All @@ -146,13 +146,13 @@ static void set_cgroup_v1_memory(const struct CONTAINER *_Nonnull container)
sprintf(memory_cgroup_limit_path, "/sys/fs/cgroup/memory/%d/memory.limit_in_bytes", container->container_id);
fd = open(memory_cgroup_limit_path, O_RDWR | O_CLOEXEC);
if (fd < 0 && !container->no_warnings) {
warning("{yellow}Set memory limit failed{clear}\n");
ruri_warning("{yellow}Set memory limit failed{clear}\n");
umount2("/sys/fs/cgroup", MNT_DETACH | MNT_FORCE);
return;
}
sprintf(buf, "%s\n", container->memory);
if (write(fd, buf, strlen(buf)) < 0 && !container->no_warnings) {
warning("{yellow}Set memory limit failed{clear}\n");
ruri_warning("{yellow}Set memory limit failed{clear}\n");
}
close(fd);
}
Expand All @@ -169,7 +169,7 @@ static void set_cgroup_v1_memory(const struct CONTAINER *_Nonnull container)
close(fd);
umount2("/sys/fs/cgroup", MNT_DETACH | MNT_FORCE);
}
static void set_cgroup_v1_cpu(const struct CONTAINER *_Nonnull container)
static void set_cgroup_v1_cpu(const struct RURI_CONTAINER *_Nonnull container)
{
mount_cgroup_v1_cpu();
char cpu_cgroup_path[PATH_MAX] = { '\0' };
Expand All @@ -185,26 +185,26 @@ static void set_cgroup_v1_cpu(const struct CONTAINER *_Nonnull container)
sprintf(cpu_cgroup_quota_path, "/sys/fs/cgroup/cpu/%d/cpu.cfs_quota_us", container->container_id);
fd = open(cpu_cgroup_quota_path, O_RDWR | O_CLOEXEC);
if (fd < 0 && !container->no_warnings) {
warning("{yellow}Set memory limit failed{clear}\n");
ruri_warning("{yellow}Set memory limit failed{clear}\n");
umount2("/sys/fs/cgroup", MNT_DETACH | MNT_FORCE);
return;
}
sprintf(buf, "%d\n", container->cpupercent * 1000);
if (write(fd, buf, strlen(buf)) < 0 && !container->no_warnings) {
warning("{yellow}Set cpupercent limit failed{clear}\n");
ruri_warning("{yellow}Set cpupercent limit failed{clear}\n");
}
close(fd);
char cpu_cgroup_period_path[PATH_MAX] = { '\0' };
sprintf(cpu_cgroup_period_path, "/sys/fs/cgroup/cpu/%d/cpu.cfs_period_us", container->container_id);
fd = open(cpu_cgroup_period_path, O_RDWR | O_CLOEXEC);
if (fd < 0 && !container->no_warnings) {
warning("{yellow}Set cpupercent limit failed{clear}\n");
ruri_warning("{yellow}Set cpupercent limit failed{clear}\n");
umount2("/sys/fs/cgroup", MNT_DETACH | MNT_FORCE);
return;
}
sprintf(buf, "%d\n", 100000);
if (write(fd, buf, strlen(buf)) < 0 && !container->no_warnings) {
warning("{yellow}Set cpupercent limit failed{clear}\n");
ruri_warning("{yellow}Set cpupercent limit failed{clear}\n");
}
close(fd);
}
Expand All @@ -221,7 +221,7 @@ static void set_cgroup_v1_cpu(const struct CONTAINER *_Nonnull container)
close(fd);
umount2("/sys/fs/cgroup", MNT_DETACH | MNT_FORCE);
}
static void set_cgroup_v1_cpuset(const struct CONTAINER *_Nonnull container)
static void set_cgroup_v1_cpuset(const struct RURI_CONTAINER *_Nonnull container)
{
mount_cgroup_v1_cpuset();
char cpuset_cgroup_path[PATH_MAX] = { '\0' };
Expand All @@ -236,7 +236,7 @@ static void set_cgroup_v1_cpuset(const struct CONTAINER *_Nonnull container)
sprintf(cpuset_cgroup_mems_path, "/sys/fs/cgroup/cpuset/%d/cpuset.mems", container->container_id);
fd = open(cpuset_cgroup_mems_path, O_RDWR | O_CLOEXEC);
if (fd < 0 && !container->no_warnings) {
warning("{yellow}Set cpuset limit failed{clear}\n");
ruri_warning("{yellow}Set cpuset limit failed{clear}\n");
// Do not keep the apifs mounted.
umount2("/sys/fs/cgroup", MNT_DETACH | MNT_FORCE);
return;
Expand All @@ -247,14 +247,14 @@ static void set_cgroup_v1_cpuset(const struct CONTAINER *_Nonnull container)
sprintf(cpuset_cgroup_cpus_path, "/sys/fs/cgroup/cpuset/%d/cpuset.cpus", container->container_id);
fd = open(cpuset_cgroup_cpus_path, O_RDWR | O_CLOEXEC);
if (fd < 0 && !container->no_warnings) {
warning("{yellow}Set cpuset limit failed{clear}\n");
ruri_warning("{yellow}Set cpuset limit failed{clear}\n");
// Do not keep the apifs mounted.
umount2("/sys/fs/cgroup", MNT_DETACH | MNT_FORCE);
return;
}
sprintf(buf, "%s\n", container->cpuset);
if (write(fd, buf, strlen(buf)) < 0 && !container->no_warnings) {
warning("{yellow}Set cpu limit failed{clear}\n");
ruri_warning("{yellow}Set cpu limit failed{clear}\n");
}
close(fd);
}
Expand All @@ -273,7 +273,7 @@ static void set_cgroup_v1_cpuset(const struct CONTAINER *_Nonnull container)
// Do not keep the apifs mounted.
umount2("/sys/fs/cgroup", MNT_DETACH | MNT_FORCE);
}
static void set_cgroup_v2_memory(const struct CONTAINER *_Nonnull container)
static void set_cgroup_v2_memory(const struct RURI_CONTAINER *_Nonnull container)
{
// Mount cgroup v2.
mkdir("/sys/fs/cgroup", S_IRUSR | S_IWUSR);
Expand All @@ -292,7 +292,7 @@ static void set_cgroup_v2_memory(const struct CONTAINER *_Nonnull container)
// Add pid to container_id cgroup.
int fd = open(cgroup_procs_path, O_RDWR | O_CLOEXEC);
if (fd < 0 && !container->no_warnings) {
warning("{yellow}Set cgroup.procs failed{clear}\n");
ruri_warning("{yellow}Set cgroup.procs failed{clear}\n");
// Do not keep the apifs mounted.
umount2("/sys/fs/cgroup", MNT_DETACH | MNT_FORCE);
return;
Expand All @@ -306,20 +306,20 @@ static void set_cgroup_v2_memory(const struct CONTAINER *_Nonnull container)
sprintf(cgroup_memlimit_path, "/sys/fs/cgroup/%d/memory.high", container->container_id);
fd = open(cgroup_memlimit_path, O_RDWR | O_CLOEXEC);
if (fd < 0 && !container->no_warnings) {
warning("{yellow}Set memory limit failed{clear}\n");
ruri_warning("{yellow}Set memory limit failed{clear}\n");
// Do not keep the apifs mounted.
umount2("/sys/fs/cgroup", MNT_DETACH | MNT_FORCE);
return;
}
sprintf(buf, "%s\n", container->memory);
if (write(fd, buf, strlen(buf)) < 0 && !container->no_warnings) {
warning("{yellow}Set memory limit failed{clear}\n");
ruri_warning("{yellow}Set memory limit failed{clear}\n");
}
close(fd);
}
umount2("/sys/fs/cgroup", MNT_DETACH | MNT_FORCE);
}
static void set_cgroup_v2_cpuset(const struct CONTAINER *_Nonnull container)
static void set_cgroup_v2_cpuset(const struct RURI_CONTAINER *_Nonnull container)
{
// Mount cgroup v2.
mkdir("/sys/fs/cgroup", S_IRUSR | S_IWUSR);
Expand All @@ -338,14 +338,14 @@ static void set_cgroup_v2_cpuset(const struct CONTAINER *_Nonnull container)
// Add pid to container_id cgroup.
int fd = open(cgroup_procs_path, O_RDWR | O_CLOEXEC);
if (fd < 0 && !container->no_warnings) {
warning("{yellow}Set cgroup.procs failed{clear}\n");
ruri_warning("{yellow}Set cgroup.procs failed{clear}\n");
// Do not keep the apifs mounted.
umount2("/sys/fs/cgroup", MNT_DETACH | MNT_FORCE);
return;
}
sprintf(buf, "%d\n", pid);
if (write(fd, buf, strlen(buf)) < 0 && !container->no_warnings) {
warning("{yellow}Set cgroup.procs failed{clear}\n");
ruri_warning("{yellow}Set cgroup.procs failed{clear}\n");
}
close(fd);
if (container->cpuset != NULL) {
Expand All @@ -354,21 +354,21 @@ static void set_cgroup_v2_cpuset(const struct CONTAINER *_Nonnull container)
sprintf(cgroup_cpuset_path, "/sys/fs/cgroup/%d/cpuset.cpus", container->container_id);
fd = open(cgroup_cpuset_path, O_RDWR | O_CLOEXEC);
if (fd < 0 && !container->no_warnings) {
warning("{yellow}Set cpuset limit failed{clear}\n");
ruri_warning("{yellow}Set cpuset limit failed{clear}\n");
// Do not keep the apifs mounted.
umount2("/sys/fs/cgroup", MNT_DETACH | MNT_FORCE);
return;
}
sprintf(buf, "%s\n", container->cpuset);
if (write(fd, buf, strlen(buf)) < 0 && !container->no_warnings) {
warning("{yellow}Set cpuset limit failed{clear}\n");
ruri_warning("{yellow}Set cpuset limit failed{clear}\n");
}
close(fd);
}
// Do not keep the apifs mounted.
umount2("/sys/fs/cgroup", MNT_DETACH | MNT_FORCE);
}
static void set_cgroup_v2_cpu(const struct CONTAINER *_Nonnull container)
static void set_cgroup_v2_cpu(const struct RURI_CONTAINER *_Nonnull container)
{
// Mount cgroup v2.
mkdir("/sys/fs/cgroup", S_IRUSR | S_IWUSR);
Expand All @@ -387,14 +387,14 @@ static void set_cgroup_v2_cpu(const struct CONTAINER *_Nonnull container)
// Add pid to container_id cgroup.
int fd = open(cgroup_procs_path, O_RDWR | O_CLOEXEC);
if (fd < 0 && !container->no_warnings) {
warning("{yellow}Set cgroup.procs failed{clear}\n");
ruri_warning("{yellow}Set cgroup.procs failed{clear}\n");
// Do not keep the apifs mounted.
umount2("/sys/fs/cgroup", MNT_DETACH | MNT_FORCE);
return;
}
sprintf(buf, "%d\n", pid);
if (write(fd, buf, strlen(buf)) < 0 && !container->no_warnings) {
warning("{yellow}Set cgroup.procs failed{clear}\n");
ruri_warning("{yellow}Set cgroup.procs failed{clear}\n");
}
close(fd);
if (container->cpupercent > 0) {
Expand All @@ -403,21 +403,21 @@ static void set_cgroup_v2_cpu(const struct CONTAINER *_Nonnull container)
sprintf(cgroup_cpu_path, "/sys/fs/cgroup/%d/cpu.max", container->container_id);
fd = open(cgroup_cpu_path, O_RDWR | O_CLOEXEC);
if (fd < 0 && !container->no_warnings) {
warning("{yellow}Set cpupercent limit failed{clear}\n");
ruri_warning("{yellow}Set cpupercent limit failed{clear}\n");
// Do not keep the apifs mounted.
umount2("/sys/fs/cgroup", MNT_DETACH | MNT_FORCE);
return;
}
sprintf(buf, "%d 100000\n", container->cpupercent * 1000);
if (write(fd, buf, strlen(buf)) < 0 && !container->no_warnings) {
warning("{yellow}Set cpupercent limit failed{clear}\n");
ruri_warning("{yellow}Set cpupercent limit failed{clear}\n");
}
close(fd);
}
// Do not keep the apifs mounted.
umount2("/sys/fs/cgroup", MNT_DETACH | MNT_FORCE);
}
void set_limit(const struct CONTAINER *_Nonnull container)
void ruri_set_limit(const struct RURI_CONTAINER *_Nonnull container)
{
/*
* Mount cgroup controller and set limit.
Expand Down
Loading

0 comments on commit 61fa27e

Please sign in to comment.