Skip to content

Commit 5d2da19

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 a1d7820 commit 5d2da19

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
@@ -642,11 +642,10 @@ impl Env {
642642
self.mknod_and_own_dev(DEV_UFFD_PATH, DEV_UFFD_MAJOR, minor)?;
643643
}
644644

645+
self.jailer_cpu_time_us = get_time_us(ClockType::ProcessCpu) - self.start_time_cpu_us;
646+
645647
// Daemonize before exec, if so required (when the dev_null variable != None).
646648
if let Some(dev_null) = dev_null {
647-
// Meter CPU usage before fork()
648-
self.jailer_cpu_time_us = get_time_us(ClockType::ProcessCpu);
649-
650649
// We follow the double fork method to daemonize the jailer referring to
651650
// https://0xjet.github.io/3OHA/2022/04/11/post.html
652651
// setsid() will fail if the calling process is a process group leader.
@@ -669,7 +668,7 @@ impl Env {
669668
.into_empty_result()
670669
.map_err(JailerError::SetSid)?;
671670

672-
// Meter CPU usage before fork()
671+
// Meter CPU usage after first fork()
673672
self.jailer_cpu_time_us += get_time_us(ClockType::ProcessCpu);
674673

675674
// Daemons should not have controlling terminals.
@@ -693,11 +692,10 @@ impl Env {
693692
dup2(dev_null.as_raw_fd(), STDIN_FILENO)?;
694693
dup2(dev_null.as_raw_fd(), STDOUT_FILENO)?;
695694
dup2(dev_null.as_raw_fd(), STDERR_FILENO)?;
696-
}
697695

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

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

0 commit comments

Comments
 (0)