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
| | // SPDX-License-Identifier: EUPL-1.2+
// SPDX-FileCopyrightText: 2026 Demi Marie Obenour <demiobenour@gmail.com>
use std::{
ffi::{OsStr, OsString},
fs::File,
io::Write as _,
os::unix::prelude::*,
path::{Component, Path, PathBuf},
};
use rustix::{
fs::{FlockOperation, Mode, OFlags, ResolveFlags},
io::Errno,
};
mod cgroup;
macro_rules! fail {
($($arg:tt)*) => {{
eprintln!($($arg)*);
std::process::exit(1)}
};
}
fn main() {
let mut args = std::env::args_os();
let Some(prog_name) = args.next() else {
fail!("No command line arguments (argv[0] is NULL)");
};
let mut cgroup_relative_path = args.next();
if cgroup_relative_path.as_deref() == Some(OsStr::from_bytes(b"--")) {
cgroup_relative_path = args.next();
}
let Some(cgroup_relative_path) = cgroup_relative_path else {
fail!("{prog_name:?}: Have no positional arguments, expect at least 1")
};
let mut cgroup_relative_path = cgroup_relative_path.into_vec();
// slow but we do not care
if cgroup_relative_path.len() > 247 {
fail!("{prog_name:?}: Cgroup name {cgroup_relative_path:?} too long");
}
if cgroup_relative_path.is_empty() {
fail!("{prog_name:?}: Cgroup name {cgroup_relative_path:?} empty");
}
if cgroup_relative_path[0] == b'-' {
fail!("{prog_name:?}: Cgroup name {cgroup_relative_path:?} starts with '-'");
}
for &i in &cgroup_relative_path {
match i {
b'/' => fail!(
"{prog_name:?}: Cgroup name {cgroup_relative_path:?} \
contains /"
),
b'$' => fail!(
"{prog_name:?}: Cgroup name {cgroup_relative_path:?} \
contains $: did you forget an execline substitution?"
),
b'!'..=b'~' => {}
_ => fail!(
"{prog_name:?}: Cgroup name {cgroup_relative_path:?} \
contains space, non-ASCII character, or control character (byte {i})"
),
}
}
cgroup_relative_path.extend_from_slice(b".service");
let cgroup_relative_path = Path::new(OsStr::from_bytes(&cgroup_relative_path));
let local_cgroup = std::fs::read("/proc/thread-self/cgroup")
.unwrap_or_else(|e| fail!("{prog_name:?}: cannot read /proc/thread-self/cgroup: {e}"));
if !local_cgroup.starts_with(b"0::/")
|| !local_cgroup.ends_with(b"\n")
|| local_cgroup.contains(&b'\0')
{
fail!("{prog_name:?}: Kernel bug: Bad contents of /proc/thread-self/cgroup");
}
let mut local_cgroup = PathBuf::from(<OsString as OsStringExt>::from_vec(
local_cgroup[4..local_cgroup.len() - 1].to_owned(),
));
match local_cgroup.components().next_back() {
Some(Component::Prefix(_)) => unreachable!("does not occur on Linux"),
Some(Component::RootDir) | None => {}
Some(Component::CurDir) => unreachable!("not produced by kernel"),
Some(Component::ParentDir) => unreachable!("not produced by kernel"),
Some(Component::Normal(os_str)) => {
if os_str.as_bytes() == b"@inner.service" {
let _ = local_cgroup.pop();
}
}
}
if local_cgroup.as_os_str().is_empty() {
local_cgroup = PathBuf::from(".");
}
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,
)
.unwrap_or_else(|e| fail!("{prog_name:?}: cannot open /sys/fs/cgroup: {e}"));
let cgroup = match cgroup::LeafCgroup::open_cgroup(cgroup_root.as_fd(), &local_cgroup) {
Ok(e) => e,
Err(e) => fail!(
"{prog_name:?}: Failed to open {}: {e:?}",
local_cgroup.display()
),
};
match rustix::fs::flock(cgroup.as_fd(), FlockOperation::LockExclusive) {
Ok(()) => {}
Err(e) => fail!("{prog_name:?}: cannot lock cgroup: {e}"),
}
purge_cgroup(&prog_name, &cgroup, cgroup_relative_path);
let Some(program_name) = args.next() else {
return;
};
if let Err(e) = cgroup.make_child(cgroup_relative_path) {
fail!("{prog_name:?}: cannot create child cgroup: {e}")
}
let child = cgroup
.open_cgroup_at(cgroup_relative_path)
.unwrap_or_else(|e| {
fail!("{prog_name:?}: cannot create child cgroup {cgroup_relative_path:?}: {e}")
});
child
.make_child(Path::new("@inner.service"))
.unwrap_or_else(|e| {
fail!("{prog_name:?}: cannot create child cgroup {cgroup_relative_path:?}/@inner.service: {e}")
});
let child = child
.open_cgroup_at(Path::new("@inner.service"))
.unwrap_or_else(|e| {
fail!("{prog_name:?}: cannot open child cgroup {cgroup_relative_path:?}/@inner.service: {e}")
});
// SAFETY: safe FFI call
let pid = unsafe { libc::getpid() };
write_cgroup_value(&prog_name, &child, "cgroup.procs", &pid.to_string());
let e = std::process::Command::new(&program_name).args(args).exec();
fail!(
"{prog_name:?}: cannot spawn child {:?}: {}",
program_name,
e
);
}
fn write_cgroup_value(prog_name: &OsStr, child: &cgroup::LeafCgroup, name: &str, value: &str) {
let path = Path::new(name);
let fd = rustix::fs::openat2(
child.as_fd(),
path,
OFlags::NOATIME | OFlags::CLOEXEC | OFlags::NOFOLLOW | OFlags::WRONLY,
Mode::empty(),
ResolveFlags::NO_SYMLINKS | ResolveFlags::BENEATH | ResolveFlags::NO_XDEV,
)
.unwrap_or_else(|e| fail!("{prog_name:?}: cannot open {:?}: {}", path, e));
let () = File::from(fd)
.write_all(value.as_bytes())
.unwrap_or_else(|e| {
fail!(
"{prog_name:?}: Cannot write {:?} to {:?}: {}",
name,
path,
e
)
});
}
fn purge_cgroup(prog_name: &OsStr, cgroup: &cgroup::LeafCgroup, cgroup_relative_path: &Path) {
let child = match cgroup.open_cgroup_at(cgroup_relative_path) {
Ok(child_cgroup) => child_cgroup,
Err(Errno::NOENT) => return,
Err(other) => {
fail!("{prog_name:?}: cannot open child cgroup {cgroup_relative_path:?}: {other}")
}
};
child.kill_processes().unwrap_or_else(|e| {
fail!("{prog_name:?}: cannot kill programs in {cgroup_relative_path:?}: {e}")
});
cgroup
.delete_child(cgroup_relative_path)
.unwrap_or_else(|e| {
fail!("{prog_name:?}: delete child cgroup {cgroup_relative_path:?}: {e}")
});
}
|