Skip to content

Commit 9895cc9

Browse files
committed
refactor: clarify computation of jailer_cpu_time_us
The components that go into the calculation are 1. Exclude thing before calling Env::new() (including potential time spent in a parent of the jailer itself that ended up exec()-ing 2. The time up until a potential fork() for daemonization 3. The time spent in each temporary process during triple-fork()ing Signed-off-by: Patrick Roy <[email protected]>
1 parent e725e3b commit 9895cc9

File tree

1 file changed

+6
-8
lines changed

1 file changed

+6
-8
lines changed

src/jailer/src/env.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -645,11 +645,10 @@ impl Env {
645645
self.mknod_and_own_dev(DEV_UFFD_PATH, DEV_UFFD_MAJOR, minor)?;
646646
}
647647

648+
self.jailer_cpu_time_us = get_time_us(ClockType::ProcessCpu) - self.start_time_cpu_us;
649+
648650
// Daemonize before exec, if so required (when the dev_null variable != None).
649651
if let Some(dev_null) = dev_null {
650-
// Meter CPU usage before fork()
651-
self.jailer_cpu_time_us = get_time_us(ClockType::ProcessCpu);
652-
653652
// We follow the double fork method to daemonize the jailer referring to
654653
// https://0xjet.github.io/3OHA/2022/04/11/post.html
655654
// setsid() will fail if the calling process is a process group leader.
@@ -672,7 +671,7 @@ impl Env {
672671
.into_empty_result()
673672
.map_err(JailerError::SetSid)?;
674673

675-
// Meter CPU usage before fork()
674+
// Meter CPU usage after first fork()
676675
self.jailer_cpu_time_us += get_time_us(ClockType::ProcessCpu);
677676

678677
// Daemons should not have controlling terminals.
@@ -696,11 +695,10 @@ impl Env {
696695
dup2(dev_null.as_raw_fd(), STDIN_FILENO)?;
697696
dup2(dev_null.as_raw_fd(), STDOUT_FILENO)?;
698697
dup2(dev_null.as_raw_fd(), STDERR_FILENO)?;
699-
}
700698

701-
// Compute jailer's total CPU time up to the current time.
702-
self.jailer_cpu_time_us += get_time_us(ClockType::ProcessCpu);
703-
self.jailer_cpu_time_us -= self.start_time_cpu_us;
699+
// Meter CPU usage after second fork()
700+
self.jailer_cpu_time_us += get_time_us(ClockType::ProcessCpu);
701+
}
704702

705703
// If specified, exec the provided binary into a new PID namespace.
706704
if self.new_pid_ns {

0 commit comments

Comments
 (0)