1
0
mirror of https://github.com/containers/youki synced 2024-11-22 17:02:00 +01:00

Merge pull request #198 from Furisto/cg-crate

Move cgroups into own crate
This commit is contained in:
utam0k 2021-08-12 21:06:41 +09:00 committed by GitHub
commit 1f613aefde
Signed by: GitHub
GPG Key ID: 4AEE18F83AFDEB23
48 changed files with 913 additions and 122 deletions

@ -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:

15
Cargo.lock generated

@ -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",

@ -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"

4
cgroups/.gitignore vendored Normal file

@ -0,0 +1,4 @@
debug/
target/
**/*.rs.bk

621
cgroups/Cargo.lock generated Normal file

@ -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"

22
cgroups/Cargo.toml Normal file

@ -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"

@ -17,8 +17,8 @@ fn booted() -> Result<bool> {
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<PathBuf>;
}
impl PathBufExt for PathBuf {
fn join_safely(&self, p: &Path) -> Result<PathBuf> {
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())))
}
}

@ -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;

@ -306,7 +306,7 @@ fn extract_page_size(dir_name: &str) -> Result<String> {
/// 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<u64> {
/// # 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<HashMap<String, Vec<S
/// Parses a file that is structed according to the nested keyed format
/// # Example
/// ```
/// use youki::cgroups::stats::parse_device_number;
/// use cgroups::stats::parse_device_number;
///
/// let (major, minor) = parse_device_number("8:0").unwrap();
/// assert_eq!((major, minor), (8, 0));
@ -431,7 +431,7 @@ pub fn pid_stats(cgroup_path: &Path) -> Result<PidStats> {
#[cfg(test)]
mod tests {
use crate::{cgroups::test::set_fixture, utils::create_temp_dir};
use crate::test::{create_temp_dir, set_fixture};
use super::*;

@ -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<PathBuf>,
}
impl TempDir {
pub fn new<P: Into<PathBuf>>(path: P) -> Result<Self> {
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<Path> 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<TempDir> {
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");

@ -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};

@ -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;

@ -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]

@ -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 {

@ -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() {

@ -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<LinuxDevice> {
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;

@ -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;

@ -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;

@ -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<CtrlType, PathBuf>,
@ -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)?;
}
}

@ -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]

@ -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() {

@ -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]

@ -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() {

@ -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

@ -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<PathBuf>
.map(|m| m.mount_point)
.ok_or_else(|| anyhow!("could not find mountpoint for {}", subsystem))
}
pub(crate) fn delete_with_retry<P: AsRef<Path>>(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)
}

@ -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]

@ -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() {

@ -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;

@ -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;

@ -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 {

@ -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<Self> {
let full_path = root_path.join_absolute_path(&cgroup_path)?;
let full_path = root_path.join_safely(&cgroup_path)?;
Ok(Self {
root_path,

@ -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;

@ -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]

@ -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,

@ -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)]

@ -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};

@ -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 {}

@ -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

@ -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};

@ -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

@ -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,

@ -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;