patches and low-level development discussion
 help / color / mirror / code / Atom feed
blob c953d26badfdac0a1e3d7057a867aec3b3247e18 12156 bytes (raw)
name: tools/cgroup-setup/src/cgroup.rs 	 # note: path name is non-authoritative(*)

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
 
// SPDX-License-Identifier: EUPL-1.2+
// SPDX-FileCopyrightText: 2026 Demi Marie Obenour <demiobenour@gmail.com>

use std::ffi::OsStr;
use std::fmt::Display;
use std::fs::File;
use std::io::{Read as _, Seek as _, Write as _};
use std::os::unix::prelude::*;

use std::path::{Component, Path, PathBuf};

use rustix::fs::{AtFlags, FlockOperation, XattrFlags};
use rustix::{
    fs::{Mode, OFlags, ResolveFlags},
    io::Errno,
};

#[derive(Debug)]
pub(crate) struct Cgroup {
    path: PathBuf,
    fd: Vec<(OwnedFd, bool)>,
}

impl AsFd for Cgroup {
    fn as_fd(&self) -> BorrowedFd<'_> {
        self.fd.last().unwrap().0.as_fd()
    }
}

fn assert_single_component(component: &[u8]) {
    match component {
        b"" | b"." | b".." => panic!("bad component"),
        _ if component.contains(&b'\0') => panic!("NUL in component"),
        _ if component.contains(&b'/') => panic!("/ in component"),
        _ => {}
    }
}

impl Cgroup {
    pub fn new(exclusive: bool) -> Result<Self, String> {
        let cgroup_root = rustix::fs::openat2(
            rustix::fs::CWD,
            Path::new("/sys/fs/cgroup"),
            OFlags::DIRECTORY | OFlags::RDONLY | OFlags::CLOEXEC | OFlags::NOFOLLOW,
            Mode::empty(),
            ResolveFlags::NO_MAGICLINKS | ResolveFlags::NO_SYMLINKS,
        )
        .map_err(|e| format!("Cannot open /sys/fs/cgroup: {e}"))?;

        let lock_operation = if exclusive {
            FlockOperation::LockExclusive
        } else {
            FlockOperation::LockShared
        };
        rustix::fs::flock(cgroup_root.as_fd(), lock_operation)
            .map_err(|e| format!("Cannot lock /sys/fs/cgroup: {e}"))?;
        Ok(Self {
            path: PathBuf::from("/sys/fs/cgroup"),
            fd: vec![(cgroup_root, exclusive)],
        })
    }

    pub fn enable_delegation(&self, depth: usize) -> Result<(), Errno> {
        let (fd, exclusive) = &self.fd[self.fd.len() - depth];
        assert!(exclusive);
        rustix::fs::fsetxattr(fd.as_fd(), c"user.delegate", b"1", XattrFlags::empty())
    }

    pub fn enable_subtree_control(&self, depth: usize) -> Result<(), String> {
        let (fd, exclusive) = &self.fd[self.fd.len() - depth];
        assert!(exclusive);
        let p = Path::new("cgroup.controllers");
        let mut buf = self.read_control_file(fd.as_fd(), p)?;
        let mut subtree = vec![];
        if buf.ends_with(b"\n") {
            buf.pop();
        }
        for controller in buf.split(|&b| b == b' ').filter(|e| !e.is_empty()) {
            for &c in controller {
                if c <= b' ' || c >= 0x7F {
                    return Err(format!("Bad byte {c} in cgroup.controllers"));
                }
            }
            if !subtree.is_empty() {
                subtree.push(b' ');
            }
            subtree.push(b'+');
            subtree.extend_from_slice(controller);
        }
        if !subtree.is_empty() {
            self.write_cgroup_value("cgroup.subtree_control", str::from_utf8(&subtree).unwrap())?;
        }
        Ok(())
    }

    pub fn read_control_file(&self, fd: BorrowedFd, p: &Path) -> Result<Vec<u8>, String> {
        let mut buf = Vec::new();
        let err = |e: &dyn Display, p: &Path, msg: &str| {
            let path = self.path.join(p);
            format!("Cannot {msg} {path:?}: {e}")
        };
        File::from(open_subtree_raw(Path::new(p), fd.as_fd()).map_err(|e| err(&e, p, "open"))?)
            .read_to_end(&mut buf)
            .map_err(|e| err(&e, p, "read"))?;
        Ok(buf)
    }

    /// Open a single component as a sub-cgroup
    fn open_sub_cgroup_raw(&self, access: OFlags, component: &[u8]) -> Result<OwnedFd, Errno> {
        assert_single_component(component);
        rustix::fs::openat2(
            self.as_fd(),
            Path::new(OsStr::from_bytes(component)),
            OFlags::CLOEXEC | OFlags::NOFOLLOW | access,
            Mode::empty(),
            ResolveFlags::NO_SYMLINKS
                | ResolveFlags::NO_MAGICLINKS
                | ResolveFlags::BENEATH
                | ResolveFlags::NO_XDEV,
        )
    }

    pub fn open_sub_cgroup(
        &mut self,
        path: &std::path::Path,
        exclusive: bool,
        allow_missing: bool,
    ) -> Result<bool, String> {
        let mut iter = path.components().peekable();
        while let Some(component) = iter.next() {
            let component = match component {
                Component::Normal(component) => component,
                _ => unreachable!(),
            };
            let sub_fd = match self
                .open_sub_cgroup_raw(OFlags::DIRECTORY | OFlags::RDONLY, component.as_bytes())
            {
                Ok(sub_fd) => {
                    self.path.push(component);
                    sub_fd
                }
                Err(Errno::NOENT) if allow_missing => return Ok(false),
                Err(e) => {
                    return Err(format!(
                        "Cannot open sub-cgroup {component:?} of {:?}: {e}",
                        self.path
                    ));
                }
            };
            let exclusive = exclusive && iter.peek().is_none();
            let lock_operation = if exclusive {
                FlockOperation::LockExclusive
            } else {
                FlockOperation::LockShared
            };
            rustix::fs::flock(sub_fd.as_fd(), lock_operation).map_err(|e| {
                let msg = format!("Cannot lock sub-cgroup {:?}: {e}", self.path);
                self.path.pop();
                msg
            })?;
            self.fd.push((sub_fd, exclusive));
        }
        Ok(true)
    }

    pub fn open_subtree(&self, path: &std::path::Path) -> Result<OwnedFd, Errno> {
        let dirfd = self.as_fd();
        open_subtree_raw(path, dirfd)
    }

    fn exclusive(&self) -> bool {
        self.fd.last().unwrap().1
    }

    pub fn joined_path(&self, p: &Path) -> PathBuf {
        let mut owned_p = self.path.clone();
        owned_p.push(p);
        owned_p
    }

    pub fn wait_for_empty(&self) -> std::io::Result<()> {
        assert!(self.exclusive());
        let wait_file = self.open_subtree(std::path::Path::new("cgroup.events"))?;
        let poll_fd = wait_file.as_raw_fd();
        let mut wait_fd = File::from(wait_file);
        let mut fds = libc::pollfd {
            fd: poll_fd,
            events: libc::POLLPRI | libc::POLLERR,
            revents: 0,
        };
        let mut v = vec![];
        loop {
            v.clear();
            wait_fd
                .seek(std::io::SeekFrom::Start(0))
                .expect("Seek on control group file should succeed");
            wait_fd
                .read_to_end(&mut v)
                .expect("reading from control group should work");
            if v.split(|&c| c == b'\n').any(|line| line == b"populated 0") {
                break;
            }
            // SAFETY: FFI call, valid arguments, fds contains 1 element
            if unsafe { libc::poll(&raw mut fds, 1, -1) } != 1 {
                panic!("poll failed");
            }
        }
        Ok(())
    }

    pub(crate) fn make_child(&mut self, path: &Path) -> Result<(), Errno> {
        assert!(self.exclusive());
        let component = path.as_os_str().as_bytes();
        assert_single_component(component);
        match rustix::fs::mkdirat(
            self.as_fd(),
            path,
            Mode::RUSR
                | Mode::WUSR
                | Mode::XUSR
                | Mode::RGRP
                | Mode::XGRP
                | Mode::ROTH
                | Mode::XOTH,
        ) {
            Ok(()) | Err(Errno::EXIST) => {}
            bad => return bad,
        }
        let p = self.open_sub_cgroup_raw(OFlags::RDONLY | OFlags::DIRECTORY, component)?;
        // exclusive lock on parent acts as exclusive lock on child
        self.fd.push((p, true));
        self.path.push(path);
        Ok(())
    }

    pub(super) fn purge(&mut self, path: &Path) -> Result<(), String> {
        assert!(self.exclusive());
        match rustix::fs::unlinkat(self.as_fd(), Path::new(path), AtFlags::REMOVEDIR) {
            // Trying to purge a deleted cgroup is not an error.
            Ok(()) | Err(Errno::NOENT) => return Ok(()),
            Err(Errno::BUSY) => {}
            Err(e) => return Err(format!("Cannot purge {:?}: {e}", self.joined_path(path))),
        }
        if !self.open_sub_cgroup(path, true, true)? {
            return Ok(());
        }

        rustix::fs::flock(
            self.fd[self.fd.len() - 2].0.as_fd(),
            FlockOperation::LockShared,
        )
        .map_err(|e| format!("Cannot relock {:?}: {e}", self.path.parent()))?;
        self.write_cgroup_value("cgroup.kill", "1")?;
        self.wait_for_empty()
            .map_err(|e| format!("Cannot wait for cgroup to become empty: {e}"))?;
        let fd = self.fd.pop().unwrap().0;
        let v = (|| {
            remove_recursively(fd, 1000)
                .map_err(|e| format!("Cannot remove {:?}: {e}", self.path))?;
            rustix::fs::flock(self.as_fd(), FlockOperation::LockExclusive)
                .map_err(|e| format!("Cannot lock {:?}: {e}", self.path))?;
            match rustix::fs::unlinkat(
                self.as_fd(),
                Path::new(self.path.file_name().unwrap()),
                AtFlags::REMOVEDIR,
            ) {
                // something might have re-created the cgroup in the meantime, which is okay
                Ok(()) | Err(Errno::BUSY) => Ok(()),
                Err(e) => Err(format!("Cannot lock {:?}: {e}", self.path)),
            }
        })();
        assert!(self.path.pop());
        v
    }

    pub(crate) fn write_cgroup_value(&self, name: &str, value: &str) -> Result<(), String> {
        let path = Path::new(name);
        let fd = rustix::fs::openat2(
            self.as_fd(),
            path,
            OFlags::NOATIME | OFlags::CLOEXEC | OFlags::NOFOLLOW | OFlags::WRONLY,
            Mode::empty(),
            ResolveFlags::NO_SYMLINKS | ResolveFlags::BENEATH | ResolveFlags::NO_XDEV,
        )
        .map_err(|e| format!("Cannot open {:?}: {}", self.joined_path(Path::new(name)), e))?;
        File::from(fd).write_all(value.as_bytes()).map_err(|e| {
            format!(
                "Cannot write {:?} to {:?}: {}",
                value,
                self.joined_path(Path::new(name)),
                e
            )
        })
    }
}

fn open_subtree_raw(path: &Path, dirfd: BorrowedFd<'_>) -> Result<OwnedFd, Errno> {
    rustix::fs::openat2(
        dirfd,
        path,
        OFlags::CLOEXEC | OFlags::NOFOLLOW | OFlags::RDONLY,
        Mode::empty(),
        ResolveFlags::NO_SYMLINKS | ResolveFlags::BENEATH | ResolveFlags::NO_XDEV,
    )
}

fn remove_recursively(fd: OwnedFd, remaining_depth: usize) -> Result<(), Errno> {
    if remaining_depth < 1 {
        panic!("control groups too deeply nested");
    }
    let mut d = rustix::fs::Dir::new(fd).expect("cannot start iterating");
    while let Some(element) = d.next() {
        let element = element.expect("Iterating through a cgroup directory failed?");
        if element.file_type() != rustix::fs::FileType::Directory {
            continue;
        }

        let remaining_depth = remaining_depth - 1;
        let d: &rustix::fs::Dir = &d;
        let dirfd = d.fd().unwrap();
        let path = element.file_name();
        remove_all(remaining_depth, dirfd, path)?;
    }
    drop(d);
    Ok(())
}

fn remove_all(
    remaining_depth: usize,
    dirfd: BorrowedFd<'_>,
    path: &std::ffi::CStr,
) -> Result<(), Errno> {
    if path == c"." || path == c".." {
        return Ok(());
    }
    if rustix::fs::unlinkat(dirfd, path, AtFlags::REMOVEDIR).is_ok() {
        return Ok(());
    }
    let fd = rustix::fs::openat2(
        dirfd,
        path,
        OFlags::CLOEXEC | OFlags::NOFOLLOW | OFlags::RDONLY | OFlags::DIRECTORY,
        Mode::empty(),
        ResolveFlags::NO_SYMLINKS | ResolveFlags::BENEATH | ResolveFlags::NO_XDEV,
    )?;
    remove_recursively(fd, remaining_depth)?;
    rustix::fs::unlinkat(dirfd, path, AtFlags::REMOVEDIR)?;
    Ok(())
}

debug log:

solving c953d26badfdac0a1e3d7057a867aec3b3247e18 ...
found c953d26badfdac0a1e3d7057a867aec3b3247e18 in https://inbox.spectrum-os.org/spectrum-devel/20260721-cgroups-v4-2-46b2e5fff7b6@gmail.com/

applying [1/1] https://inbox.spectrum-os.org/spectrum-devel/20260721-cgroups-v4-2-46b2e5fff7b6@gmail.com/
diff --git a/tools/cgroup-setup/src/cgroup.rs b/tools/cgroup-setup/src/cgroup.rs
new file mode 100644
index 0000000000000000000000000000000000000000..c953d26badfdac0a1e3d7057a867aec3b3247e18

Checking patch tools/cgroup-setup/src/cgroup.rs...
Applied patch tools/cgroup-setup/src/cgroup.rs cleanly.

index at:
100644 c953d26badfdac0a1e3d7057a867aec3b3247e18	tools/cgroup-setup/src/cgroup.rs

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this public inbox

	https://spectrum-os.org/git/doc
	https://spectrum-os.org/git/mktuntap
	https://spectrum-os.org/git/spectrum
	https://spectrum-os.org/git/ucspi-vsock

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).