Skip to content

Commit

Permalink
Refactor command construction out of herder
Browse files Browse the repository at this point in the history
  • Loading branch information
ifd3f committed May 5, 2024
1 parent cb52e84 commit cca7699
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 14 deletions.
41 changes: 40 additions & 1 deletion src/run_mode.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
use std::{borrow::Cow, fmt::Debug};

use process_path::get_executable_path;
use serde::{de::DeserializeOwned, Serialize};
use valuable::Valuable;

use crate::{
escalation::Command, logging::get_log_paths, writer_process::ipc::WriterProcessConfig,
};

pub const RUN_MODE_ENV_NAME: &str = "__CALIGULA_RUN_MODE";

/// [RunMode] is a flag set in the environment variable `__CALIGULA_RUN_MODE`
Expand Down Expand Up @@ -46,11 +56,40 @@ impl RunMode {
}
}

pub fn as_str(&self) -> &str {
pub fn as_str(&self) -> &'static str {
match self {
RunMode::Main => "main",
RunMode::Writer => "writer",
RunMode::EscalatedDaemon => "escalated_daemon",
}
}
}

/// Build a [Command] that, when run, spawns a process with a specific configuration.
pub fn make_spawn_command<'a, C: Serialize + Debug + Valuable>(
socket: Cow<'a, str>,
log_path: Cow<'a, str>,
run_mode: RunMode,
init_config: C,
) -> Command<'a> {
let proc = get_executable_path().unwrap();

Command {
proc: proc.to_str().unwrap().to_owned().into(),
envs: vec![(RUN_MODE_ENV_NAME.into(), run_mode.as_str().into())],
// Arg order is documented in childproc_common.
args: vec![
log_path.into(),
socket.into(),
serde_json::to_string(&init_config).unwrap().into(),
],
}
}

pub fn make_writer_spawn_command<'a>(
socket: Cow<'a, str>,
log_path: Cow<'a, str>,
init_config: &WriterProcessConfig,
) -> Command<'a> {
make_spawn_command(socket, log_path, RunMode::Writer, init_config)
}
19 changes: 6 additions & 13 deletions src/ui/herder/herder.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::{
ipc_common::read_msg_async,
logging::get_log_paths,
run_mode::make_writer_spawn_command,
ui::herder::{
socket::HerderSocket,
writer::handle::{StartProcessError, WriterHandle},
Expand Down Expand Up @@ -45,19 +46,11 @@ impl Herder {
"Read absolute path to this program"
);

let args = serde_json::to_string(args)?;
debug!(?args, "Converted WriterProcessConfig to JSON");

let cmd = Command {
proc: proc.to_string_lossy(),
envs: vec![(RUN_MODE_ENV_NAME.into(), RunMode::Writer.as_str().into())],
// Arg order is documented in childproc_common.
args: vec![
get_log_paths().child.to_string_lossy().into(),
self.socket.socket_name().to_string_lossy().into(),
args.into(),
],
};
let cmd = make_writer_spawn_command(
self.socket.socket_name().to_string_lossy(),
get_log_paths().child.to_string_lossy(),
args,
);

debug!("Starting child process with command: {:?}", cmd);
fn modify_cmd(cmd: &mut tokio::process::Command) {
Expand Down
1 change: 1 addition & 0 deletions src/writer_process/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! This module has logic for the child process that writes to the disk.
//!
//! IT IS NOT TO BE USED DIRECTLY BY THE USER! ITS API HAS NO STABILITY GUARANTEES!

pub mod child;
pub mod ipc;
mod xplat;

0 comments on commit cca7699

Please sign in to comment.