Skip to content

Commit

Permalink
Merge pull request #23 from blockjoy/zeenix/tmux-session-name
Browse files Browse the repository at this point in the history
Allow setting tmux session name explicitly
  • Loading branch information
zeenix authored Jul 8, 2022
2 parents bbcc5fe + 2c7670a commit e6405ff
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
11 changes: 6 additions & 5 deletions src/config/jailer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub struct Jailer<'j> {
jailer_binary: Cow<'j, Path>,
chroot_base_dir: Cow<'j, Path>,
workspace_dir: Cow<'j, Path>,
pub(crate) mode: JailerMode,
pub(crate) mode: JailerMode<'j>,
// TODO: We need an equivalent of ChrootStrategy.
}

Expand Down Expand Up @@ -64,16 +64,17 @@ impl<'j> Jailer<'j> {
/// The mode of the jailer process.
#[derive(Derivative)]
#[derivative(Debug, Default)]
pub enum JailerMode {
pub enum JailerMode<'j> {
/// The jailer child process will run attached to the parent process.
#[derivative(Default)]
Attached(Stdio),
/// Calls setsid() and redirect stdin, stdout, and stderr to /dev/null.
Daemon,
/// Launch the jailer in a tmux session.
///
/// The tmux session will be named `<VM_ID>` and tmux will be launched in detached mode.
Tmux,
/// If the session name is not provided, `<VM_ID>` is used as the session name. tmux will be
/// launched in detached mode.
Tmux(Option<Cow<'j, str>>),
}

/// The standard IO handlers.
Expand Down Expand Up @@ -170,7 +171,7 @@ impl<'j> JailerBuilder<'j> {
}

/// The mode of the jailer process.
pub fn mode(mut self, mode: JailerMode) -> Self {
pub fn mode(mut self, mode: JailerMode<'j>) -> Self {
self.jailer.mode = mode;
self
}
Expand Down
14 changes: 10 additions & 4 deletions src/machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,13 +162,16 @@ impl<'m> Machine<'m> {
stdio.stdout.take().unwrap_or_else(Stdio::inherit),
stdio.stderr.take().unwrap_or_else(Stdio::inherit),
),
JailerMode::Tmux => {
JailerMode::Tmux(session_name) => {
let session_name = session_name
.clone()
.unwrap_or_else(|| vm_id.to_string().into());
let mut cmd = Command::new("tmux");
cmd.args(&[
"new-session",
"-d",
"-s",
&vm_id.to_string(),
&session_name,
jailer.jailer_binary().to_str().unwrap(),
]);

Expand Down Expand Up @@ -286,10 +289,13 @@ impl<'m> Machine<'m> {
}
trace!("{vm_id}: Successfully sent KILL signal to VM (pid: `{pid}`).");
}
JailerMode::Tmux => {
JailerMode::Tmux(session_name) => {
let session_name = session_name
.clone()
.unwrap_or_else(|| vm_id.to_string().into());
// In case of tmux, we need to kill the tmux session.
let cmd = &mut Command::new("tmux");
cmd.args(&["kill-session", "-t", &vm_id.to_string()]);
cmd.args(&["kill-session", "-t", &session_name]);
trace!("{vm_id}: Running command: {:?}", cmd);
cmd.spawn()?.wait().await?;
}
Expand Down

0 comments on commit e6405ff

Please sign in to comment.