|
| 1 | +/* |
| 2 | + This file is part of the Hydrogen OJ Judger. |
| 3 | +
|
| 4 | + This program is free software: you can redistribute it and/or modify |
| 5 | + it under the terms of the GNU Affero General Public License as published |
| 6 | + by the Free Software Foundation, either version 3 of the License, or |
| 7 | + (at your option) any later version. |
| 8 | +
|
| 9 | + This program is distributed in the hope that it will be useful, |
| 10 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + GNU Affero General Public License for more details. |
| 13 | +
|
| 14 | + You should have received a copy of the GNU Affero General Public License |
| 15 | + along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 16 | +*/ |
| 17 | + |
| 18 | +/* Copyright (C) 2020-2021 Alex Cui */ |
| 19 | + |
| 20 | +#include "cgroup.h" |
| 21 | + |
| 22 | +#include <cstdint> |
| 23 | +#include <cstdio> |
| 24 | +#include <cstdlib> |
| 25 | +#include <cstring> |
| 26 | +#include <filesystem> |
| 27 | +#include <fstream> |
| 28 | +#include <iostream> |
| 29 | +#include <map> |
| 30 | +#include <vector> |
| 31 | + |
| 32 | +#include <mntent.h> |
| 33 | + |
| 34 | +namespace fs = std::filesystem; |
| 35 | + |
| 36 | +static std::map<std::string, std::vector<fs::path>> mount_table; |
| 37 | +static bool initialized = false; |
| 38 | + |
| 39 | +int hoj::init_cgroup() { |
| 40 | + mount_table.clear(); |
| 41 | + |
| 42 | + std::ifstream proc_cgroup("/proc/cgroups"); |
| 43 | + if (!proc_cgroup.is_open()) { |
| 44 | + // TODO: error |
| 45 | + } |
| 46 | + |
| 47 | + std::string subsys_name; |
| 48 | + int hierarchy, num_cgroups, enabled; |
| 49 | + std::vector<std::string> controller; |
| 50 | + |
| 51 | + proc_cgroup.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); |
| 52 | + while (!proc_cgroup.eof()) { |
| 53 | + proc_cgroup >> subsys_name >> hierarchy >> num_cgroups >> enabled; |
| 54 | + controller.push_back(subsys_name); |
| 55 | + } |
| 56 | + |
| 57 | + FILE *proc_mount = fopen("/proc/mounts", "re"); |
| 58 | + if (proc_mount == nullptr) { |
| 59 | + // TODO: error |
| 60 | + } |
| 61 | + |
| 62 | + mntent *ent = nullptr; |
| 63 | + mntent *temp_ent = new mntent; |
| 64 | + char mntent_buffer[4 * FILENAME_MAX]; |
| 65 | + while ( |
| 66 | + (ent = getmntent_r( |
| 67 | + proc_mount, temp_ent, mntent_buffer, sizeof(mntent_buffer)) |
| 68 | + ) != nullptr |
| 69 | + ) { |
| 70 | + if (strcmp(ent->mnt_type, "cgroup")) { |
| 71 | + continue; |
| 72 | + } |
| 73 | + |
| 74 | + for (const auto &name : controller) { |
| 75 | + char *mntopt = hasmntopt(ent, name.c_str()); |
| 76 | + if (!mntopt) { |
| 77 | + continue; |
| 78 | + } |
| 79 | + |
| 80 | + mount_table[name].push_back(ent->mnt_dir); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + delete temp_ent; |
| 85 | + fclose(proc_mount); |
| 86 | + proc_cgroup.close(); |
| 87 | + |
| 88 | + initialized = true; |
| 89 | + |
| 90 | + return 0; |
| 91 | +} |
| 92 | + |
| 93 | +bool hoj::cgroup_initialized() { |
| 94 | + return initialized; |
| 95 | +} |
| 96 | + |
| 97 | +static fs::path get_cgroup_path(const std::string &controller) { |
| 98 | + auto iter = mount_table.find(controller); |
| 99 | + if (iter == mount_table.end()) { |
| 100 | + // TODO: error |
| 101 | + return ""; |
| 102 | + } |
| 103 | + |
| 104 | + return iter->second[0]; |
| 105 | +} |
| 106 | + |
| 107 | +int hoj::create_cgroup(const hoj::cgroup_info &info) { |
| 108 | + for (const auto &controller : info.controller) { |
| 109 | + fs::create_directories(get_cgroup_path(controller) / info.name); |
| 110 | + } |
| 111 | + return 0; |
| 112 | +} |
| 113 | + |
| 114 | +int hoj::remove_cgroup(const cgroup_info &info) { |
| 115 | + for (const auto &controller : info.controller) { |
| 116 | + auto target = get_cgroup_path(controller) / info.name; |
| 117 | + if (fs::exists(target)) { |
| 118 | + fs::remove(target); |
| 119 | + } |
| 120 | + } |
| 121 | + return 0; |
| 122 | +} |
| 123 | + |
| 124 | +int hoj::check_cgroup(const cgroup_info &info) { |
| 125 | + for (const auto &controller : info.controller) { |
| 126 | + auto target = get_cgroup_path(controller) / info.name; |
| 127 | + if (fs::exists(target)) { |
| 128 | + return 1; |
| 129 | + } |
| 130 | + } |
| 131 | + return 0; |
| 132 | +} |
| 133 | + |
| 134 | +template <typename value_type> |
| 135 | +static int write_file( |
| 136 | + const fs::path &path, const value_type &value, |
| 137 | + bool overwrite = false |
| 138 | +) { |
| 139 | + std::ofstream file( |
| 140 | + path, |
| 141 | + overwrite ? std::ofstream::trunc : std::ofstream::app |
| 142 | + ); |
| 143 | + if (!file.is_open()) { |
| 144 | + // TODO: error |
| 145 | + return 1; |
| 146 | + } |
| 147 | + file << value << std::endl; |
| 148 | + file.close(); |
| 149 | + return 0; |
| 150 | +} |
| 151 | + |
| 152 | +template <typename value_type> |
| 153 | +static value_type read_file(const fs::path &path) { |
| 154 | + std::ifstream file(path); |
| 155 | + if (!file.is_open()) { |
| 156 | + // TODO: error |
| 157 | + return value_type(); |
| 158 | + } |
| 159 | + value_type value; |
| 160 | + file >> value; |
| 161 | + file.close(); |
| 162 | + return value; |
| 163 | +} |
| 164 | + |
| 165 | +template <typename value_type> |
| 166 | +static std::vector<value_type> read_file_array(const fs::path &path) { |
| 167 | + std::ifstream file(path); |
| 168 | + if (!file.is_open()) { |
| 169 | + // TODO: error |
| 170 | + return std::vector<value_type>(); |
| 171 | + } |
| 172 | + value_type temp; |
| 173 | + std::vector<value_type> value; |
| 174 | + while (!file.eof()) { |
| 175 | + file >> temp; |
| 176 | + value.push_back(temp); |
| 177 | + } |
| 178 | + return value; |
| 179 | +} |
| 180 | + |
| 181 | +template <typename value_type> |
| 182 | +int hoj::write_cgroup_property( |
| 183 | + const hoj::cgroup_info &info, const std::string &controller, |
| 184 | + const std::string &property, const value_type &value, bool overwrite |
| 185 | +) { |
| 186 | + return write_file( |
| 187 | + get_cgroup_path(controller) / info.name / property, |
| 188 | + value, overwrite |
| 189 | + ); |
| 190 | +} |
| 191 | + |
| 192 | +template int hoj::write_cgroup_property<std::int32_t>( |
| 193 | + const hoj::cgroup_info &info, const std::string &controller, |
| 194 | + const std::string &property, const std::int32_t &value, bool overwrite |
| 195 | +); |
| 196 | + |
| 197 | +template int hoj::write_cgroup_property<std::int64_t>( |
| 198 | + const hoj::cgroup_info &info, const std::string &controller, |
| 199 | + const std::string &property, const std::int64_t &value, bool overwrite |
| 200 | +); |
| 201 | + |
| 202 | +template int hoj::write_cgroup_property<std::string>( |
| 203 | + const hoj::cgroup_info &info, const std::string &controller, |
| 204 | + const std::string &property, const std::string &value, bool overwrite |
| 205 | +); |
| 206 | + |
| 207 | +template <typename value_type> |
| 208 | +value_type hoj::read_cgroup_property( |
| 209 | + const hoj::cgroup_info &info, const std::string &controller, |
| 210 | + const std::string &property |
| 211 | +) { |
| 212 | + return read_file<value_type>( |
| 213 | + get_cgroup_path(controller) / info.name / property |
| 214 | + ); |
| 215 | +} |
| 216 | + |
| 217 | +template std::int32_t hoj::read_cgroup_property<std::int32_t>( |
| 218 | + const hoj::cgroup_info &info, const std::string &controller, |
| 219 | + const std::string &property |
| 220 | +); |
| 221 | + |
| 222 | +template std::int64_t hoj::read_cgroup_property<std::int64_t>( |
| 223 | + const hoj::cgroup_info &info, const std::string &controller, |
| 224 | + const std::string &property |
| 225 | +); |
0 commit comments