diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index a9a51424..c64da818 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -63,6 +63,9 @@ jobs: run: cargo test - name: Run doc tests run: cargo test --doc + - name: Run cgroup tests + working-directory: cgroups + run: cargo test integration_tests: runs-on: ubuntu-latest strategy: diff --git a/Cargo.lock b/Cargo.lock index a332a863..ee9ea8f5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -76,6 +76,20 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "cgroups" +version = "0.1.0" +dependencies = [ + "anyhow", + "dbus", + "log", + "nix 0.22.0", + "oci_spec", + "procfs", + "serde", + "systemd", +] + [[package]] name = "chrono" version = "0.4.19" @@ -1036,6 +1050,7 @@ version = "0.0.1" dependencies = [ "anyhow", "caps", + "cgroups", "chrono", "clap", "dbus", diff --git a/Cargo.toml b/Cargo.toml index 802558d3..41783bff 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -30,6 +30,7 @@ chrono = { version="0.4", features = ["serde"] } once_cell = "1.6.0" futures = { version = "0.3", features = ["thread-pool"] } oci_spec = { version = "0.1.0", path = "./oci_spec" } +cgroups = { version = "0.1.0", path = "./cgroups" } systemd = { version = "0.8", default-features = false, optional = true } dbus = "0.9.2" tabwriter = "1" diff --git a/cgroups/.gitignore b/cgroups/.gitignore new file mode 100644 index 00000000..70d28edf --- /dev/null +++ b/cgroups/.gitignore @@ -0,0 +1,4 @@ +debug/ +target/ +**/*.rs.bk + diff --git a/cgroups/Cargo.lock b/cgroups/Cargo.lock new file mode 100644 index 00000000..dc31f2a3 --- /dev/null +++ b/cgroups/Cargo.lock @@ -0,0 +1,621 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "adler" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" + +[[package]] +name = "aho-corasick" +version = "0.7.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e37cfd5e7657ada45f742d6e99ca5788580b5c529dc78faf11ece6dc702656f" +dependencies = [ + "memchr", +] + +[[package]] +name = "anyhow" +version = "1.0.42" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "595d3cfa7a60d4555cb5067b99f07142a08ea778de5cf993f7b75c7d8fabc486" + +[[package]] +name = "autocfg" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" + +[[package]] +name = "bitflags" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" + +[[package]] +name = "build-env" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1522ac6ee801a11bf9ef3f80403f4ede6eb41291fac3dde3de09989679305f25" + +[[package]] +name = "byteorder" +version = "1.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610" + +[[package]] +name = "caps" +version = "0.5.3-alpha.0" +source = "git+https://github.com/lucab/caps-rs?rev=cb54844#cb54844125d9dd6de51d6c8c8a951aefbd0d3904" +dependencies = [ + "errno", + "libc", + "serde", + "thiserror", +] + +[[package]] +name = "cc" +version = "1.0.69" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e70cc2f62c6ce1868963827bd677764c62d07c3d9a3e1fb1177ee1a9ab199eb2" + +[[package]] +name = "cfg-if" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "cgroups" +version = "0.1.0" +dependencies = [ + "anyhow", + "dbus", + "log", + "nix", + "oci_spec", + "procfs", + "quickcheck", + "serde", + "systemd", +] + +[[package]] +name = "chrono" +version = "0.4.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "670ad68c9088c2a963aaa298cb369688cf3f9465ce5e2d4ca10e6e0098a1ce73" +dependencies = [ + "libc", + "num-integer", + "num-traits", + "time", + "winapi", +] + +[[package]] +name = "crc32fast" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "81156fece84ab6a9f2afdb109ce3ae577e42b1228441eded99bd77f627953b1a" +dependencies = [ + "cfg-if 1.0.0", +] + +[[package]] +name = "cstr-argument" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "20bd4e8067c20c7c3a4dea759ef91d4b18418ddb5bd8837ef6e2f2f93ca7ccbb" +dependencies = [ + "cfg-if 0.1.10", + "memchr", +] + +[[package]] +name = "dbus" +version = "0.9.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c8862bb50aa3b2a2db5bfd2c875c73b3038aa931c411087e335ca8ca0ed430b9" +dependencies = [ + "libc", + "libdbus-sys", + "winapi", +] + +[[package]] +name = "env_logger" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a19187fea3ac7e84da7dacf48de0c45d63c6a76f9490dae389aead16c243fce3" +dependencies = [ + "log", + "regex", +] + +[[package]] +name = "errno" +version = "0.2.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fa68f2fb9cae9d37c9b2b3584aba698a2e97f72d7aef7b9f7aa71d8b54ce46fe" +dependencies = [ + "errno-dragonfly", + "libc", + "winapi", +] + +[[package]] +name = "errno-dragonfly" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "14ca354e36190500e1e1fb267c647932382b54053c50b14970856c0b00a35067" +dependencies = [ + "gcc", + "libc", +] + +[[package]] +name = "flate2" +version = "1.0.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cd3aec53de10fe96d7d8c565eb17f2c687bb5518a2ec453b5b1252964526abe0" +dependencies = [ + "cfg-if 1.0.0", + "crc32fast", + "libc", + "miniz_oxide", +] + +[[package]] +name = "foreign-types" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d737d9aa519fb7b749cbc3b962edcf310a8dd1f4b67c91c4f83975dbdd17d965" +dependencies = [ + "foreign-types-macros", + "foreign-types-shared", +] + +[[package]] +name = "foreign-types-macros" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "63f713f8b2aa9e24fec85b0e290c56caee12e3b6ae0aeeda238a75b28251afd6" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "foreign-types-shared" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7684cf33bb7f28497939e8c7cf17e3e4e3b8d9a0080ffa4f8ae2f515442ee855" + +[[package]] +name = "gcc" +version = "0.3.55" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f5f3913fa0bfe7ee1fd8248b6b9f42a5af4b9d65ec2dd2c3c26132b950ecfc2" + +[[package]] +name = "getrandom" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7fcd999463524c52659517fe2cea98493cfe485d10565e7b0fb07dbba7ad2753" +dependencies = [ + "cfg-if 1.0.0", + "libc", + "wasi", +] + +[[package]] +name = "hex" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70" + +[[package]] +name = "itoa" +version = "0.4.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dd25036021b0de88a0aff6b850051563c6516d0bf53f8638938edbb9de732736" + +[[package]] +name = "lazy_static" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" + +[[package]] +name = "libc" +version = "0.2.98" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "320cfe77175da3a483efed4bc0adc1968ca050b098ce4f2f1c13a56626128790" + +[[package]] +name = "libdbus-sys" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc12a3bc971424edbbf7edaf6e5740483444db63aa8e23d3751ff12a30f306f0" +dependencies = [ + "pkg-config", +] + +[[package]] +name = "libsystemd-sys" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6e03fd580bcecda68dcdcd5297085ade6a3dc552cd8b030d2b94a9b089ef7ab8" +dependencies = [ + "build-env", + "libc", + "pkg-config", +] + +[[package]] +name = "log" +version = "0.4.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "51b9bbe6c47d51fc3e1a9b945965946b4c44142ab8792c50835a980d362c2710" +dependencies = [ + "cfg-if 1.0.0", +] + +[[package]] +name = "memchr" +version = "2.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b16bd47d9e329435e309c58469fe0791c2d0d1ba96ec0954152a5ae2b04387dc" + +[[package]] +name = "memoffset" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "59accc507f1338036a0477ef61afdae33cde60840f4dfe481319ce3ad116ddf9" +dependencies = [ + "autocfg", +] + +[[package]] +name = "miniz_oxide" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a92518e98c078586bc6c934028adcca4c92a53d6a958196de835170a01d84e4b" +dependencies = [ + "adler", + "autocfg", +] + +[[package]] +name = "nix" +version = "0.22.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf1e25ee6b412c2a1e3fcb6a4499a5c1bfe7f43e014bdce9a6b6666e5aa2d187" +dependencies = [ + "bitflags", + "cc", + "cfg-if 1.0.0", + "libc", + "memoffset", +] + +[[package]] +name = "num-integer" +version = "0.1.44" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2cc698a63b549a70bc047073d2949cce27cd1c7b0a4a862d08a8031bc2801db" +dependencies = [ + "autocfg", + "num-traits", +] + +[[package]] +name = "num-traits" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a64b1ec5cda2586e284722486d802acf1f7dbdc623e2bfc57e65ca1cd099290" +dependencies = [ + "autocfg", +] + +[[package]] +name = "oci_spec" +version = "0.1.0" +dependencies = [ + "anyhow", + "caps", + "nix", + "quickcheck", + "serde", + "serde_json", + "tempfile", +] + +[[package]] +name = "pkg-config" +version = "0.3.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3831453b3449ceb48b6d9c7ad7c96d5ea673e9b470a1dc578c2ce6521230884c" + +[[package]] +name = "ppv-lite86" +version = "0.2.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac74c624d6b2d21f425f752262f42188365d7b8ff1aff74c82e45136510a4857" + +[[package]] +name = "proc-macro2" +version = "1.0.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c7ed8b8c7b886ea3ed7dde405212185f423ab44682667c8c6dd14aa1d9f6612" +dependencies = [ + "unicode-xid", +] + +[[package]] +name = "procfs" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ab8809e0c18450a2db0f236d2a44ec0b4c1412d0eb936233579f0990faa5d5cd" +dependencies = [ + "bitflags", + "byteorder", + "chrono", + "flate2", + "hex", + "lazy_static", + "libc", +] + +[[package]] +name = "quickcheck" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "588f6378e4dd99458b60ec275b4477add41ce4fa9f64dcba6f15adccb19b50d6" +dependencies = [ + "env_logger", + "log", + "rand", +] + +[[package]] +name = "quote" +version = "1.0.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3d0b9745dc2debf507c8422de05d7226cc1f0644216dfdfead988f9b1ab32a7" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "rand" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2e7573632e6454cf6b99d7aac4ccca54be06da05aca2ef7423d22d27d4d4bcd8" +dependencies = [ + "libc", + "rand_chacha", + "rand_core", + "rand_hc", +] + +[[package]] +name = "rand_chacha" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.6.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d34f1408f55294453790c48b2f1ebbb1c5b4b7563eb1f418bcfcfdbb06ebb4e7" +dependencies = [ + "getrandom", +] + +[[package]] +name = "rand_hc" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d51e9f596de227fda2ea6c84607f5558e196eeaf43c986b724ba4fb8fdf497e7" +dependencies = [ + "rand_core", +] + +[[package]] +name = "redox_syscall" +version = "0.2.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5ab49abadf3f9e1c4bc499e8845e152ad87d2ad2d30371841171169e9d75feee" +dependencies = [ + "bitflags", +] + +[[package]] +name = "regex" +version = "1.5.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d07a8629359eb56f1e2fb1652bb04212c072a87ba68546a04065d525673ac461" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.6.25" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f497285884f3fcff424ffc933e56d7cbca511def0c9831a7f9b5f6153e3cc89b" + +[[package]] +name = "remove_dir_all" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3acd125665422973a33ac9d3dd2df85edad0f4ae9b00dafb1a05e43a9f5ef8e7" +dependencies = [ + "winapi", +] + +[[package]] +name = "ryu" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "71d301d4193d031abdd79ff7e3dd721168a9572ef3fe51a1517aba235bd8f86e" + +[[package]] +name = "serde" +version = "1.0.127" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f03b9878abf6d14e6779d3f24f07b2cfa90352cfec4acc5aab8f1ac7f146fae8" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.127" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a024926d3432516606328597e0f224a51355a493b49fdd67e9209187cbe55ecc" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "serde_json" +version = "1.0.66" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "336b10da19a12ad094b59d870ebde26a45402e5b470add4b5fd03c5048a32127" +dependencies = [ + "itoa", + "ryu", + "serde", +] + +[[package]] +name = "syn" +version = "1.0.74" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1873d832550d4588c3dbc20f01361ab00bfe741048f71e3fecf145a7cc18b29c" +dependencies = [ + "proc-macro2", + "quote", + "unicode-xid", +] + +[[package]] +name = "systemd" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f722cabda922e471742300045f56dbaa53fafbb4520fca304e51258019bfe91d" +dependencies = [ + "cstr-argument", + "foreign-types", + "libc", + "libsystemd-sys", + "log", + "memchr", + "utf8-cstr", +] + +[[package]] +name = "tempfile" +version = "3.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dac1c663cfc93810f88aed9b8941d48cabf856a1b111c29a40439018d870eb22" +dependencies = [ + "cfg-if 1.0.0", + "libc", + "rand", + "redox_syscall", + "remove_dir_all", + "winapi", +] + +[[package]] +name = "thiserror" +version = "1.0.26" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "93119e4feac1cbe6c798c34d3a53ea0026b0b1de6a120deef895137c0529bfe2" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.26" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "060d69a0afe7796bf42e9e2ff91f5ee691fb15c53d38b4b62a9a53eb23164745" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "time" +version = "0.1.43" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca8a50ef2360fbd1eeb0ecd46795a87a19024eb4b53c5dc916ca1fd95fe62438" +dependencies = [ + "libc", + "winapi", +] + +[[package]] +name = "unicode-xid" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3" + +[[package]] +name = "utf8-cstr" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "55bcbb425141152b10d5693095950b51c3745d019363fc2929ffd8f61449b628" + +[[package]] +name = "wasi" +version = "0.10.2+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6" + +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" diff --git a/cgroups/Cargo.toml b/cgroups/Cargo.toml new file mode 100644 index 00000000..a6515f17 --- /dev/null +++ b/cgroups/Cargo.toml @@ -0,0 +1,22 @@ +[package] +name = "cgroups" +version = "0.1.0" +edition = "2018" + +[features] +default = ["systemd_cgroups"] +systemd_cgroups = ["systemd"] + +[dependencies] +nix = "0.22.0" +procfs = "0.9.1" +log = "0.4" +anyhow = "1.0" +oci_spec = { version = "0.1.0", path = "../oci_spec" } +systemd = { version = "0.8", default-features = false, optional = true } +dbus = "0.9.2" +serde = { version = "1.0", features = ["derive"] } + +[dev-dependencies] +oci_spec = { version = "0.1.0", path = "../oci_spec", features = ["proptests"] } +quickcheck = "1" diff --git a/src/cgroups/common.rs b/cgroups/src/common.rs similarity index 92% rename from src/cgroups/common.rs rename to cgroups/src/common.rs index b455f79b..0e9049e8 100644 --- a/src/cgroups/common.rs +++ b/cgroups/src/common.rs @@ -17,8 +17,8 @@ fn booted() -> Result { bail!("This build does not include the systemd cgroups feature") } -use crate::cgroups::v1; -use crate::cgroups::v2; +use super::v1; +use super::v2; use super::stats::Stats; @@ -211,3 +211,19 @@ where } Ok(()) } + +pub trait PathBufExt { + fn join_safely(&self, p: &Path) -> Result; +} + +impl PathBufExt for PathBuf { + fn join_safely(&self, p: &Path) -> Result { + if !p.is_absolute() && !p.as_os_str().is_empty() { + bail!( + "cannot join {:?} because it is not the absolute path.", + p.display() + ) + } + Ok(PathBuf::from(format!("{}{}", self.display(), p.display()))) + } +} diff --git a/src/cgroups/mod.rs b/cgroups/src/lib.rs similarity index 84% rename from src/cgroups/mod.rs rename to cgroups/src/lib.rs index 6fde5b72..35eca40e 100644 --- a/src/cgroups/mod.rs +++ b/cgroups/src/lib.rs @@ -1,6 +1,9 @@ //! Control groups provide a way of controlling groups of processes. //! Examples: controlling resource limits, execution priority, measuring resource usage, //! freezing, checkpointing and restarting groups of processes. +#[cfg(test)] +#[macro_use] +extern crate quickcheck; pub mod common; pub mod stats; diff --git a/src/cgroups/stats.rs b/cgroups/src/stats.rs similarity index 98% rename from src/cgroups/stats.rs rename to cgroups/src/stats.rs index 1ea4096d..a9102beb 100644 --- a/src/cgroups/stats.rs +++ b/cgroups/src/stats.rs @@ -306,7 +306,7 @@ fn extract_page_size(dir_name: &str) -> Result { /// Parses this string slice into an u64 /// # Example /// ``` -/// use youki::cgroups::stats::parse_value; +/// use cgroups::stats::parse_value; /// /// let value = parse_value("32").unwrap(); /// assert_eq!(value, 32); @@ -321,7 +321,7 @@ pub fn parse_value(value: &str) -> Result { /// # Example /// ```no_run /// use std::path::Path; -/// use youki::cgroups::stats::parse_single_value; +/// use cgroups::stats::parse_single_value; /// /// let value = parse_single_value(&Path::new("memory.current")).unwrap(); /// assert_eq!(value, 32); @@ -396,7 +396,7 @@ pub fn parse_nested_keyed_data(file_path: &Path) -> Result Result { #[cfg(test)] mod tests { - use crate::{cgroups::test::set_fixture, utils::create_temp_dir}; + use crate::test::{create_temp_dir, set_fixture}; use super::*; diff --git a/src/cgroups/test.rs b/cgroups/src/test.rs similarity index 69% rename from src/cgroups/test.rs rename to cgroups/src/test.rs index 016975dd..b84bbd4e 100644 --- a/src/cgroups/test.rs +++ b/cgroups/src/test.rs @@ -2,13 +2,63 @@ use anyhow::{Context, Result}; use std::{ + fs, io::Write, + ops::Deref, path::{Path, PathBuf}, }; use oci_spec::LinuxCpu; -use crate::utils::{create_temp_dir, TempDir}; +pub struct TempDir { + path: Option, +} + +impl TempDir { + pub fn new>(path: P) -> Result { + let p = path.into(); + std::fs::create_dir_all(&p)?; + Ok(Self { path: Some(p) }) + } + + pub fn path(&self) -> &Path { + self.path + .as_ref() + .expect("temp dir has already been removed") + } + + pub fn remove(&mut self) { + if let Some(p) = &self.path { + let _ = fs::remove_dir_all(p); + self.path = None; + } + } +} + +impl Drop for TempDir { + fn drop(&mut self) { + self.remove(); + } +} + +impl AsRef for TempDir { + fn as_ref(&self) -> &Path { + self.path() + } +} + +impl Deref for TempDir { + type Target = Path; + + fn deref(&self) -> &Self::Target { + self.path() + } +} + +pub fn create_temp_dir(test_name: &str) -> Result { + let dir = TempDir::new(std::env::temp_dir().join(test_name))?; + Ok(dir) +} pub fn setup(testname: &str, cgroup_file: &str) -> (TempDir, PathBuf) { let tmp = create_temp_dir(testname).expect("create temp directory for test"); diff --git a/src/cgroups/v1/blkio.rs b/cgroups/src/v1/blkio.rs similarity index 99% rename from src/cgroups/v1/blkio.rs rename to cgroups/src/v1/blkio.rs index eaff619e..6a3119be 100644 --- a/src/cgroups/v1/blkio.rs +++ b/cgroups/src/v1/blkio.rs @@ -1,10 +1,11 @@ use std::path::Path; -use crate::cgroups::{ +use crate::{ common, stats::{self, BlkioDeviceStat, BlkioStats, StatsProvider}, v1::Controller, }; + use anyhow::{Context, Result}; use oci_spec::{LinuxBlockIo, LinuxResources}; @@ -224,10 +225,8 @@ mod tests { use std::fs; use super::*; - use crate::{ - cgroups::test::{set_fixture, setup}, - utils::create_temp_dir, - }; + use crate::test::{create_temp_dir, set_fixture, setup}; + use anyhow::Result; use oci_spec::{LinuxBlockIo, LinuxThrottleDevice}; diff --git a/src/cgroups/v1/controller.rs b/cgroups/src/v1/controller.rs similarity index 93% rename from src/cgroups/v1/controller.rs rename to cgroups/src/v1/controller.rs index 9aaa8fca..a9a15a66 100644 --- a/src/cgroups/v1/controller.rs +++ b/cgroups/src/v1/controller.rs @@ -5,7 +5,7 @@ use nix::unistd::Pid; use oci_spec::LinuxResources; -use crate::cgroups::common::{self, CGROUP_PROCS}; +use crate::common::{self, CGROUP_PROCS}; pub trait Controller { type Resource; diff --git a/src/cgroups/v1/controller_type.rs b/cgroups/src/v1/controller_type.rs similarity index 100% rename from src/cgroups/v1/controller_type.rs rename to cgroups/src/v1/controller_type.rs diff --git a/src/cgroups/v1/cpu.rs b/cgroups/src/v1/cpu.rs similarity index 98% rename from src/cgroups/v1/cpu.rs rename to cgroups/src/v1/cpu.rs index 0643d9b9..c86c8baa 100644 --- a/src/cgroups/v1/cpu.rs +++ b/cgroups/src/v1/cpu.rs @@ -3,7 +3,7 @@ use std::path::Path; use anyhow::{bail, Context, Result}; use oci_spec::{LinuxCpu, LinuxResources}; -use crate::cgroups::{ +use crate::{ common, stats::{CpuThrottling, StatsProvider}, }; @@ -132,10 +132,7 @@ impl Cpu { #[cfg(test)] mod tests { use super::*; - use crate::{ - cgroups::test::{set_fixture, setup, LinuxCpuBuilder}, - utils::create_temp_dir, - }; + use crate::test::{create_temp_dir, set_fixture, setup, LinuxCpuBuilder}; use std::fs; #[test] diff --git a/src/cgroups/v1/cpuacct.rs b/cgroups/src/v1/cpuacct.rs similarity index 97% rename from src/cgroups/v1/cpuacct.rs rename to cgroups/src/v1/cpuacct.rs index 0da62d9a..9ca07dfe 100644 --- a/src/cgroups/v1/cpuacct.rs +++ b/cgroups/src/v1/cpuacct.rs @@ -3,7 +3,7 @@ use std::path::Path; use anyhow::{bail, Context, Result}; use oci_spec::LinuxResources; -use crate::cgroups::{ +use crate::{ common, stats::{CpuUsage, StatsProvider}, }; @@ -132,11 +132,9 @@ mod tests { use super::*; use crate::{ - cgroups::{ - common::CGROUP_PROCS, - test::{set_fixture, setup}, - }, - utils::{create_temp_dir, TempDir}, + common::CGROUP_PROCS, + test::{create_temp_dir, TempDir}, + test::{set_fixture, setup}, }; fn setup_total_cpu(test_name: &str, stat_content: &str, usage_content: &str) -> TempDir { diff --git a/src/cgroups/v1/cpuset.rs b/cgroups/src/v1/cpuset.rs similarity index 97% rename from src/cgroups/v1/cpuset.rs rename to cgroups/src/v1/cpuset.rs index b7d66931..d7b86d49 100644 --- a/src/cgroups/v1/cpuset.rs +++ b/cgroups/src/v1/cpuset.rs @@ -5,7 +5,7 @@ use nix::unistd; use oci_spec::{LinuxCpu, LinuxResources}; use unistd::Pid; -use crate::cgroups::common::{self, CGROUP_PROCS}; +use crate::common::{self, CGROUP_PROCS}; use super::{util, Controller, ControllerType}; @@ -92,7 +92,7 @@ mod tests { use std::fs; use super::*; - use crate::cgroups::test::{setup, LinuxCpuBuilder}; + use crate::test::{setup, LinuxCpuBuilder}; #[test] fn test_set_cpus() { diff --git a/src/cgroups/v1/devices.rs b/cgroups/src/v1/devices.rs similarity index 79% rename from src/cgroups/v1/devices.rs rename to cgroups/src/v1/devices.rs index bc69f9d4..7bc4996f 100644 --- a/src/cgroups/v1/devices.rs +++ b/cgroups/src/v1/devices.rs @@ -1,10 +1,10 @@ -use std::path::Path; +use std::path::{Path, PathBuf}; use anyhow::Result; -use crate::cgroups::common; -use crate::{cgroups::v1::Controller, rootfs::default_devices}; -use oci_spec::{LinuxDeviceCgroup, LinuxDeviceType, LinuxResources}; +use super::controller::Controller; +use crate::common; +use oci_spec::{LinuxDevice, LinuxDeviceCgroup, LinuxDeviceType, LinuxResources}; pub struct Devices {} @@ -21,7 +21,7 @@ impl Controller for Devices { } for d in [ - default_devices().iter().map(|d| d.into()).collect(), + Self::default_devices().iter().map(|d| d.into()).collect(), Self::default_allow_devices(), ] .concat() @@ -99,13 +99,72 @@ impl Devices { }, ] } + + pub fn default_devices() -> Vec { + vec![ + LinuxDevice { + path: PathBuf::from("/dev/null"), + typ: LinuxDeviceType::C, + major: 1, + minor: 3, + file_mode: Some(0o066), + uid: None, + gid: None, + }, + LinuxDevice { + path: PathBuf::from("/dev/zero"), + typ: LinuxDeviceType::C, + major: 1, + minor: 5, + file_mode: Some(0o066), + uid: None, + gid: None, + }, + LinuxDevice { + path: PathBuf::from("/dev/full"), + typ: LinuxDeviceType::C, + major: 1, + minor: 7, + file_mode: Some(0o066), + uid: None, + gid: None, + }, + LinuxDevice { + path: PathBuf::from("/dev/tty"), + typ: LinuxDeviceType::C, + major: 5, + minor: 0, + file_mode: Some(0o066), + uid: None, + gid: None, + }, + LinuxDevice { + path: PathBuf::from("/dev/urandom"), + typ: LinuxDeviceType::C, + major: 1, + minor: 9, + file_mode: Some(0o066), + uid: None, + gid: None, + }, + LinuxDevice { + path: PathBuf::from("/dev/random"), + typ: LinuxDeviceType::C, + major: 1, + minor: 8, + file_mode: Some(0o066), + uid: None, + gid: None, + }, + ] + } } #[cfg(test)] mod tests { use super::*; - use crate::cgroups::test::set_fixture; - use crate::utils::create_temp_dir; + use crate::test::create_temp_dir; + use crate::test::set_fixture; use oci_spec::{LinuxDeviceCgroup, LinuxDeviceType}; use std::fs::read_to_string; diff --git a/src/cgroups/v1/freezer.rs b/cgroups/src/v1/freezer.rs similarity index 98% rename from src/cgroups/v1/freezer.rs rename to cgroups/src/v1/freezer.rs index 36e01214..7cf197b5 100644 --- a/src/cgroups/v1/freezer.rs +++ b/cgroups/src/v1/freezer.rs @@ -7,8 +7,8 @@ use std::{ use anyhow::{Result, *}; -use crate::cgroups::common; -use crate::cgroups::v1::Controller; +use super::Controller; +use crate::common; use oci_spec::{FreezerState, LinuxResources}; const CGROUP_FREEZER_STATE: &str = "freezer.state"; @@ -124,9 +124,8 @@ impl Freezer { #[cfg(test)] mod tests { use super::*; - use crate::cgroups::common::CGROUP_PROCS; - use crate::cgroups::test::set_fixture; - use crate::utils::create_temp_dir; + use crate::common::CGROUP_PROCS; + use crate::test::{create_temp_dir, set_fixture}; use nix::unistd::Pid; use oci_spec::FreezerState; diff --git a/src/cgroups/v1/hugetlb.rs b/cgroups/src/v1/hugetlb.rs similarity index 97% rename from src/cgroups/v1/hugetlb.rs rename to cgroups/src/v1/hugetlb.rs index 715162b8..75814fd5 100644 --- a/src/cgroups/v1/hugetlb.rs +++ b/cgroups/src/v1/hugetlb.rs @@ -2,11 +2,12 @@ use std::{collections::HashMap, path::Path}; use anyhow::{bail, Result}; -use crate::cgroups::{ +use crate::{ common, stats::{supported_page_sizes, HugeTlbStats, StatsProvider}, - v1::Controller, }; + +use super::Controller; use oci_spec::{LinuxHugepageLimit, LinuxResources}; pub struct HugeTlb {} @@ -98,8 +99,7 @@ impl HugeTlb { #[cfg(test)] mod tests { use super::*; - use crate::cgroups::test::set_fixture; - use crate::utils::create_temp_dir; + use crate::test::{create_temp_dir, set_fixture}; use oci_spec::LinuxHugepageLimit; use std::fs::read_to_string; diff --git a/src/cgroups/v1/manager.rs b/cgroups/src/v1/manager.rs similarity index 95% rename from src/cgroups/v1/manager.rs rename to cgroups/src/v1/manager.rs index 30a1b699..0288862b 100644 --- a/src/cgroups/v1/manager.rs +++ b/cgroups/src/v1/manager.rs @@ -16,10 +16,8 @@ use super::{ perf_event::PerfEvent, pids::Pids, util, Controller, }; -use crate::cgroups::common::{self, CGROUP_PROCS}; -use crate::cgroups::stats::{Stats, StatsProvider}; -use crate::utils; -use crate::{cgroups::common::CgroupManager, utils::PathBufExt}; +use crate::common::{self, CgroupManager, PathBufExt, CGROUP_PROCS}; +use crate::stats::{Stats, StatsProvider}; use oci_spec::{FreezerState, LinuxResources}; pub struct Manager { subsystems: HashMap, @@ -51,9 +49,9 @@ impl Manager { .unwrap(); let p = if cgroup_path.to_string_lossy().into_owned().is_empty() { - mount_point.join_absolute_path(Path::new(&cgroup.pathname))? + mount_point.join_safely(Path::new(&cgroup.pathname))? } else if cgroup_path.is_absolute() { - mount_point.join_absolute_path(cgroup_path)? + mount_point.join_safely(&cgroup_path)? } else { mount_point.join(cgroup_path) }; @@ -163,7 +161,7 @@ impl CgroupManager for Manager { let _ = nix::sys::signal::kill(Pid::from_raw(pid), nix::sys::signal::SIGKILL); } - utils::delete_with_retry(cgroup_path.1)?; + util::delete_with_retry(cgroup_path.1)?; } } diff --git a/src/cgroups/v1/memory.rs b/cgroups/src/v1/memory.rs similarity index 98% rename from src/cgroups/v1/memory.rs rename to cgroups/src/v1/memory.rs index 3bde8e71..3acddfb7 100644 --- a/src/cgroups/v1/memory.rs +++ b/cgroups/src/v1/memory.rs @@ -5,9 +5,10 @@ use std::{fs::OpenOptions, path::Path}; use anyhow::{anyhow, bail, Result}; use nix::errno::Errno; -use crate::cgroups::common::{self}; -use crate::cgroups::stats::{self, parse_single_value, MemoryData, MemoryStats, StatsProvider}; -use crate::cgroups::v1::Controller; +use super::Controller; +use crate::common::{self}; +use crate::stats::{self, parse_single_value, MemoryData, MemoryStats, StatsProvider}; + use oci_spec::{LinuxMemory, LinuxResources}; const CGROUP_MEMORY_SWAP_LIMIT: &str = "memory.memsw.limit_in_bytes"; @@ -321,9 +322,8 @@ impl Memory { #[cfg(test)] mod tests { use super::*; - use crate::cgroups::common::CGROUP_PROCS; - use crate::cgroups::test::set_fixture; - use crate::utils::create_temp_dir; + use crate::common::CGROUP_PROCS; + use crate::test::{create_temp_dir, set_fixture}; use oci_spec::LinuxMemory; #[test] diff --git a/src/cgroups/v1/mod.rs b/cgroups/src/v1/mod.rs similarity index 100% rename from src/cgroups/v1/mod.rs rename to cgroups/src/v1/mod.rs diff --git a/src/cgroups/v1/network_classifier.rs b/cgroups/src/v1/network_classifier.rs similarity index 92% rename from src/cgroups/v1/network_classifier.rs rename to cgroups/src/v1/network_classifier.rs index f7625e4b..e90fbd83 100644 --- a/src/cgroups/v1/network_classifier.rs +++ b/cgroups/src/v1/network_classifier.rs @@ -2,8 +2,8 @@ use std::path::Path; use anyhow::Result; -use crate::cgroups::common; -use crate::cgroups::v1::Controller; +use super::Controller; +use crate::common; use oci_spec::{LinuxNetwork, LinuxResources}; pub struct NetworkClassifier {} @@ -43,8 +43,7 @@ impl NetworkClassifier { #[cfg(test)] mod tests { use super::*; - use crate::cgroups::test::set_fixture; - use crate::utils::create_temp_dir; + use crate::test::{create_temp_dir, set_fixture}; #[test] fn test_apply_network_classifier() { diff --git a/src/cgroups/v1/network_priority.rs b/cgroups/src/v1/network_priority.rs similarity index 93% rename from src/cgroups/v1/network_priority.rs rename to cgroups/src/v1/network_priority.rs index 025fbc49..4c78ad6d 100644 --- a/src/cgroups/v1/network_priority.rs +++ b/cgroups/src/v1/network_priority.rs @@ -2,8 +2,8 @@ use std::path::Path; use anyhow::Result; -use crate::cgroups::common; -use crate::cgroups::v1::Controller; +use super::Controller; +use crate::common; use oci_spec::{LinuxNetwork, LinuxResources}; pub struct NetworkPriority {} @@ -44,8 +44,7 @@ impl NetworkPriority { #[cfg(test)] mod tests { use super::*; - use crate::cgroups::test::set_fixture; - use crate::utils::create_temp_dir; + use crate::test::{create_temp_dir, set_fixture}; use oci_spec::LinuxInterfacePriority; #[test] diff --git a/src/cgroups/v1/perf_event.rs b/cgroups/src/v1/perf_event.rs similarity index 90% rename from src/cgroups/v1/perf_event.rs rename to cgroups/src/v1/perf_event.rs index b48484be..ed53c46b 100644 --- a/src/cgroups/v1/perf_event.rs +++ b/cgroups/src/v1/perf_event.rs @@ -1,4 +1,4 @@ -use crate::cgroups::v1::Controller; +use super::Controller; use anyhow::Result; use oci_spec::LinuxResources; use std::path::Path; @@ -24,7 +24,7 @@ mod tests { use nix::unistd::Pid; use super::*; - use crate::cgroups::{common::CGROUP_PROCS, test::setup}; + use crate::{common::CGROUP_PROCS, test::setup}; #[test] fn test_add_task() { diff --git a/src/cgroups/v1/pids.rs b/cgroups/src/v1/pids.rs similarity index 96% rename from src/cgroups/v1/pids.rs rename to cgroups/src/v1/pids.rs index ca850d6a..98f22b1e 100644 --- a/src/cgroups/v1/pids.rs +++ b/cgroups/src/v1/pids.rs @@ -2,10 +2,10 @@ use std::path::Path; use anyhow::Result; -use crate::cgroups::{ +use super::Controller; +use crate::{ common, stats::{self, PidStats, StatsProvider}, - v1::Controller, }; use oci_spec::{LinuxPids, LinuxResources}; @@ -60,8 +60,7 @@ impl Pids { #[cfg(test)] mod tests { use super::*; - use crate::cgroups::test::set_fixture; - use crate::utils::create_temp_dir; + use crate::test::{create_temp_dir, set_fixture}; use oci_spec::LinuxPids; // Contains the current number of active pids diff --git a/src/cgroups/v1/util.rs b/cgroups/src/v1/util.rs similarity index 78% rename from src/cgroups/v1/util.rs rename to cgroups/src/v1/util.rs index 389bbddba..0b95c2fe 100644 --- a/src/cgroups/v1/util.rs +++ b/cgroups/src/v1/util.rs @@ -1,6 +1,11 @@ -use std::{collections::HashMap, path::PathBuf}; +use std::{ + collections::HashMap, + fs, + path::{Path, PathBuf}, + time::Duration, +}; -use anyhow::{anyhow, Result}; +use anyhow::{anyhow, bail, Result}; use procfs::process::Process; use super::{controller_type::CONTROLLERS, ControllerType}; @@ -51,3 +56,21 @@ pub fn get_subsystem_mount_point(subsystem: &ControllerType) -> Result .map(|m| m.mount_point) .ok_or_else(|| anyhow!("could not find mountpoint for {}", subsystem)) } + +pub(crate) fn delete_with_retry>(path: P) -> Result<()> { + let mut attempts = 0; + let mut delay = Duration::from_millis(10); + let path = path.as_ref(); + + while attempts < 5 { + if fs::remove_dir(path).is_ok() { + return Ok(()); + } + + std::thread::sleep(delay); + attempts += attempts; + delay *= attempts; + } + + bail!("could not delete {:?}", path) +} diff --git a/src/cgroups/v2/controller.rs b/cgroups/src/v2/controller.rs similarity index 100% rename from src/cgroups/v2/controller.rs rename to cgroups/src/v2/controller.rs diff --git a/src/cgroups/v2/controller_type.rs b/cgroups/src/v2/controller_type.rs similarity index 100% rename from src/cgroups/v2/controller_type.rs rename to cgroups/src/v2/controller_type.rs diff --git a/src/cgroups/v2/cpu.rs b/cgroups/src/v2/cpu.rs similarity index 98% rename from src/cgroups/v2/cpu.rs rename to cgroups/src/v2/cpu.rs index ba56619d..6b3c45ae 100644 --- a/src/cgroups/v2/cpu.rs +++ b/cgroups/src/v2/cpu.rs @@ -1,10 +1,11 @@ use anyhow::{bail, Result}; use std::path::Path; -use crate::cgroups::{ +use crate::{ common, stats::{CpuUsage, StatsProvider}, }; + use oci_spec::{LinuxCpu, LinuxResources}; use super::controller::Controller; @@ -117,8 +118,7 @@ impl Cpu { #[cfg(test)] mod tests { use super::*; - use crate::cgroups::test::{set_fixture, setup, LinuxCpuBuilder}; - use crate::utils::create_temp_dir; + use crate::test::{create_temp_dir, set_fixture, setup, LinuxCpuBuilder}; use std::fs; #[test] diff --git a/src/cgroups/v2/cpuset.rs b/cgroups/src/v2/cpuset.rs similarity index 95% rename from src/cgroups/v2/cpuset.rs rename to cgroups/src/v2/cpuset.rs index b477080b..c1cac883 100644 --- a/src/cgroups/v2/cpuset.rs +++ b/cgroups/src/v2/cpuset.rs @@ -1,7 +1,7 @@ use anyhow::Result; use std::path::Path; -use crate::cgroups::common; +use crate::common; use oci_spec::{LinuxCpu, LinuxResources}; use super::controller::Controller; @@ -40,7 +40,7 @@ mod tests { use std::fs; use super::*; - use crate::cgroups::test::{setup, LinuxCpuBuilder}; + use crate::test::{setup, LinuxCpuBuilder}; #[test] fn test_set_cpus() { diff --git a/src/cgroups/v2/freezer.rs b/cgroups/src/v2/freezer.rs similarity index 98% rename from src/cgroups/v2/freezer.rs rename to cgroups/src/v2/freezer.rs index 5827f813..a82e19fb 100644 --- a/src/cgroups/v2/freezer.rs +++ b/cgroups/src/v2/freezer.rs @@ -122,8 +122,7 @@ impl Freezer { #[cfg(test)] mod tests { use super::*; - use crate::cgroups::test::set_fixture; - use crate::utils::create_temp_dir; + use crate::test::{create_temp_dir, set_fixture}; use oci_spec::FreezerState; use std::sync::Arc; diff --git a/src/cgroups/v2/hugetlb.rs b/cgroups/src/v2/hugetlb.rs similarity index 98% rename from src/cgroups/v2/hugetlb.rs rename to cgroups/src/v2/hugetlb.rs index b114036a..023fff4d 100644 --- a/src/cgroups/v2/hugetlb.rs +++ b/cgroups/src/v2/hugetlb.rs @@ -2,10 +2,11 @@ use anyhow::{bail, Context, Result}; use std::{collections::HashMap, path::Path}; use super::controller::Controller; -use crate::cgroups::{ +use crate::{ common, stats::{parse_single_value, supported_page_sizes, HugeTlbStats, StatsProvider}, }; + use oci_spec::{LinuxHugepageLimit, LinuxResources}; pub struct HugeTlb {} @@ -85,8 +86,7 @@ impl HugeTlb { #[cfg(test)] mod tests { use super::*; - use crate::cgroups::test::set_fixture; - use crate::utils::create_temp_dir; + use crate::test::{create_temp_dir, set_fixture}; use oci_spec::LinuxHugepageLimit; use std::fs::read_to_string; diff --git a/src/cgroups/v2/io.rs b/cgroups/src/v2/io.rs similarity index 99% rename from src/cgroups/v2/io.rs rename to cgroups/src/v2/io.rs index 38598086..0dba2f99 100644 --- a/src/cgroups/v2/io.rs +++ b/cgroups/src/v2/io.rs @@ -2,7 +2,7 @@ use std::path::{Path, PathBuf}; use anyhow::{bail, Result}; -use crate::cgroups::{ +use crate::{ common, stats::{self, BlkioDeviceStat, BlkioStats, StatsProvider}, }; @@ -149,10 +149,8 @@ impl Io { #[cfg(test)] mod test { use super::*; - use crate::{ - cgroups::test::{set_fixture, setup}, - utils::create_temp_dir, - }; + use crate::test::{create_temp_dir, set_fixture, setup}; + use oci_spec::{LinuxBlockIo, LinuxThrottleDevice, LinuxWeightDevice}; use std::fs; struct BlockIoBuilder { diff --git a/src/cgroups/v2/manager.rs b/cgroups/src/v2/manager.rs similarity index 93% rename from src/cgroups/v2/manager.rs rename to cgroups/src/v2/manager.rs index b994adc7..d14a454e 100644 --- a/src/cgroups/v2/manager.rs +++ b/cgroups/src/v2/manager.rs @@ -10,17 +10,12 @@ use nix::unistd::Pid; use oci_spec::{FreezerState, LinuxResources}; use super::{ - cpu::Cpu, cpuset::CpuSet, freezer::Freezer, hugetlb::HugeTlb, io::Io, memory::Memory, - pids::Pids, + controller::Controller, controller_type::ControllerType, cpu::Cpu, cpuset::CpuSet, + freezer::Freezer, hugetlb::HugeTlb, io::Io, memory::Memory, pids::Pids, }; use crate::{ - cgroups::v2::controller::Controller, - cgroups::{ - common::{self, CgroupManager, CGROUP_PROCS}, - stats::{Stats, StatsProvider}, - v2::controller_type::ControllerType, - }, - utils::PathBufExt, + common::{self, CgroupManager, PathBufExt, CGROUP_PROCS}, + stats::{Stats, StatsProvider}, }; const CGROUP_CONTROLLERS: &str = "cgroup.controllers"; @@ -46,7 +41,7 @@ impl Manager { /// Constructs a new cgroup manager with root path being the mount point /// of a cgroup v2 fs and cgroup path being a relative path from the root pub fn new(root_path: PathBuf, cgroup_path: PathBuf) -> Result { - let full_path = root_path.join_absolute_path(&cgroup_path)?; + let full_path = root_path.join_safely(&cgroup_path)?; Ok(Self { root_path, diff --git a/src/cgroups/v2/memory.rs b/cgroups/src/v2/memory.rs similarity index 99% rename from src/cgroups/v2/memory.rs rename to cgroups/src/v2/memory.rs index 1858597f..7dc68108 100644 --- a/src/cgroups/v2/memory.rs +++ b/cgroups/src/v2/memory.rs @@ -3,7 +3,7 @@ use std::path::Path; use oci_spec::{LinuxMemory, LinuxResources}; -use crate::cgroups::{ +use crate::{ common, stats::{self, MemoryData, MemoryStats, StatsProvider}, }; @@ -127,8 +127,7 @@ impl Memory { #[cfg(test)] mod tests { use super::*; - use crate::cgroups::test::set_fixture; - use crate::utils::create_temp_dir; + use crate::test::{create_temp_dir, set_fixture}; use oci_spec::LinuxMemory; use std::fs::read_to_string; diff --git a/src/cgroups/v2/mod.rs b/cgroups/src/v2/mod.rs similarity index 100% rename from src/cgroups/v2/mod.rs rename to cgroups/src/v2/mod.rs diff --git a/src/cgroups/v2/pids.rs b/cgroups/src/v2/pids.rs similarity index 95% rename from src/cgroups/v2/pids.rs rename to cgroups/src/v2/pids.rs index b7790781..0b5f979d 100644 --- a/src/cgroups/v2/pids.rs +++ b/cgroups/src/v2/pids.rs @@ -2,7 +2,7 @@ use std::path::Path; use anyhow::Result; -use crate::cgroups::{ +use crate::{ common, stats::{self, PidStats, StatsProvider}, }; @@ -44,8 +44,7 @@ impl Pids { #[cfg(test)] mod tests { use super::*; - use crate::cgroups::test::set_fixture; - use crate::utils::create_temp_dir; + use crate::test::{create_temp_dir, set_fixture}; use oci_spec::LinuxPids; #[test] diff --git a/src/cgroups/v2/systemd_manager.rs b/cgroups/src/v2/systemd_manager.rs similarity index 96% rename from src/cgroups/v2/systemd_manager.rs rename to cgroups/src/v2/systemd_manager.rs index 2a49c36b..2716dcad 100644 --- a/src/cgroups/v2/systemd_manager.rs +++ b/cgroups/src/v2/systemd_manager.rs @@ -9,14 +9,11 @@ use oci_spec::{FreezerState, LinuxResources}; use std::path::{Path, PathBuf}; use super::{ - cpu::Cpu, cpuset::CpuSet, freezer::Freezer, hugetlb::HugeTlb, io::Io, memory::Memory, - pids::Pids, + controller::Controller, controller_type::ControllerType, cpu::Cpu, cpuset::CpuSet, + freezer::Freezer, hugetlb::HugeTlb, io::Io, memory::Memory, pids::Pids, }; -use crate::cgroups::common::CgroupManager; -use crate::cgroups::v2::controller::Controller; -use crate::cgroups::v2::controller_type::ControllerType; -use crate::cgroups::{common, stats::Stats}; -use crate::utils::PathBufExt; +use crate::common::{self, CgroupManager, PathBufExt}; +use crate::stats::Stats; const CGROUP_PROCS: &str = "cgroup.procs"; const CGROUP_CONTROLLERS: &str = "cgroup.controllers"; @@ -52,7 +49,7 @@ impl SystemDCGroupManager { // TODO: create the systemd unit using a dbus client. let destructured_path = Self::destructure_cgroups_path(cgroups_path)?; let cgroups_path = Self::construct_cgroups_path(destructured_path)?; - let full_path = root_path.join_absolute_path(&cgroups_path)?; + let full_path = root_path.join_safely(&cgroups_path)?; Ok(SystemDCGroupManager { root_path, diff --git a/src/cgroups/v2/util.rs b/cgroups/src/v2/util.rs similarity index 100% rename from src/cgroups/v2/util.rs rename to cgroups/src/v2/util.rs diff --git a/src/commands/delete.rs b/src/commands/delete.rs index 55295000..2f9f9a80 100644 --- a/src/commands/delete.rs +++ b/src/commands/delete.rs @@ -5,9 +5,9 @@ use anyhow::{bail, Context, Result}; use clap::Clap; use nix::sys::signal::Signal; -use crate::cgroups; use crate::container::{Container, ContainerStatus}; use crate::utils; +use cgroups; use nix::sys::signal as nix_signal; #[derive(Clap, Debug)] diff --git a/src/commands/events.rs b/src/commands/events.rs index b9f45aba..1786373b 100644 --- a/src/commands/events.rs +++ b/src/commands/events.rs @@ -1,4 +1,5 @@ -use crate::{cgroups::common, utils}; +use crate::utils; +use cgroups::common; use clap::Clap; use std::{path::PathBuf, thread, time::Duration}; diff --git a/src/commands/info.rs b/src/commands/info.rs index 11b96fff..48301003 100644 --- a/src/commands/info.rs +++ b/src/commands/info.rs @@ -5,7 +5,7 @@ use anyhow::Result; use clap::Clap; use procfs::{CpuInfo, Meminfo}; -use crate::cgroups; +use cgroups; #[derive(Clap, Debug)] pub struct Info {} diff --git a/src/commands/pause.rs b/src/commands/pause.rs index abd02dcf..7010698b 100644 --- a/src/commands/pause.rs +++ b/src/commands/pause.rs @@ -5,10 +5,10 @@ use std::path::PathBuf; use anyhow::{bail, Context, Result}; use clap::Clap; -use crate::cgroups; use crate::container::Container; use crate::container::ContainerStatus; use crate::utils; +use cgroups; use oci_spec::FreezerState; /// Structure to implement pause command diff --git a/src/commands/ps.rs b/src/commands/ps.rs index b4c00a73..ac6f1716 100644 --- a/src/commands/ps.rs +++ b/src/commands/ps.rs @@ -1,5 +1,6 @@ -use crate::{cgroups, container::Container, utils}; +use crate::{container::Container, utils}; use anyhow::{bail, Context, Result}; +use cgroups; use clap::{self, Clap}; use std::{path::PathBuf, process::Command}; diff --git a/src/commands/resume.rs b/src/commands/resume.rs index b21b18af..23455414 100644 --- a/src/commands/resume.rs +++ b/src/commands/resume.rs @@ -5,10 +5,10 @@ use std::path::PathBuf; use anyhow::{bail, Context, Result}; use clap::Clap; -use crate::cgroups; use crate::container::Container; use crate::container::ContainerStatus; use crate::utils; +use cgroups; use oci_spec::FreezerState; /// Structure to implement resume command diff --git a/src/container/builder_impl.rs b/src/container/builder_impl.rs index 55401bde..e7720609 100644 --- a/src/container/builder_impl.rs +++ b/src/container/builder_impl.rs @@ -1,10 +1,12 @@ use anyhow::{Context, Result}; use nix::sched::CloneFlags; + +use cgroups; + use oci_spec::Spec; use std::{fs, os::unix::prelude::RawFd, path::PathBuf}; use crate::{ - cgroups, namespaces::Namespaces, process::{child, fork, init, parent}, rootless::Rootless, diff --git a/src/lib.rs b/src/lib.rs index d24299e0..aa43c9f7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,9 +1,4 @@ -#[cfg(test)] -#[macro_use] -extern crate quickcheck; - pub mod capabilities; -pub mod cgroups; pub mod commands; pub mod container; pub mod dbus;