init: Move run and run_command to the scheduler
This commit is contained in:
+3
-62
@@ -1,21 +1,19 @@
|
||||
use std::collections::BTreeMap;
|
||||
use std::env;
|
||||
use std::ffi::OsString;
|
||||
use std::io::Result;
|
||||
use std::path::Path;
|
||||
use std::{env, io};
|
||||
|
||||
use libredox::flag::{O_RDONLY, O_WRONLY};
|
||||
|
||||
use crate::scheduler::Scheduler;
|
||||
use crate::script::Command;
|
||||
use crate::unit::{Unit, UnitId, UnitStore};
|
||||
use crate::unit::{UnitId, UnitStore};
|
||||
|
||||
mod scheduler;
|
||||
mod script;
|
||||
mod service;
|
||||
mod unit;
|
||||
|
||||
fn switch_stdio(stdio: &str) -> Result<()> {
|
||||
fn switch_stdio(stdio: &str) -> io::Result<()> {
|
||||
let stdin = libredox::Fd::open(stdio, O_RDONLY, 0)?;
|
||||
let stdout = libredox::Fd::open(stdio, O_WRONLY, 0)?;
|
||||
let stderr = libredox::Fd::open(stdio, O_WRONLY, 0)?;
|
||||
@@ -68,63 +66,6 @@ fn switch_root(unit_store: &mut UnitStore, config: &mut InitConfig, prefix: &Pat
|
||||
unit_store.config_dirs = vec![prefix.join("lib").join("init.d"), etcdir.join("init.d")];
|
||||
}
|
||||
|
||||
fn run(unit: &mut Unit, config: &mut InitConfig) -> Result<()> {
|
||||
match &unit.kind {
|
||||
unit::UnitKind::LegacyScript { script } => {
|
||||
for cmd in script.0.clone() {
|
||||
if config.log_debug {
|
||||
eprintln!("init: running: {cmd:?}");
|
||||
}
|
||||
run_command(cmd, config);
|
||||
}
|
||||
}
|
||||
unit::UnitKind::Service { service } => {
|
||||
if config.skip_cmd.contains(&service.cmd) {
|
||||
eprintln!("Skipping '{} {}'", service.cmd, service.args.join(" "));
|
||||
return Ok(());
|
||||
}
|
||||
if config.log_debug {
|
||||
eprintln!(
|
||||
"Starting {} ({})",
|
||||
unit.info.description.as_ref().unwrap_or(&unit.id.0),
|
||||
service.cmd,
|
||||
);
|
||||
}
|
||||
service.spawn(&config.envs);
|
||||
}
|
||||
unit::UnitKind::Target {} => {
|
||||
if config.log_debug {
|
||||
eprintln!(
|
||||
"Reached target {}",
|
||||
unit.info.description.as_ref().unwrap_or(&unit.id.0),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn run_command(cmd: Command, config: &mut InitConfig) {
|
||||
match cmd {
|
||||
Command::RequiresWeak(_) => {} // handled by unit parsing code
|
||||
Command::Nothing => {}
|
||||
Command::Echo(text) => println!("{text}"),
|
||||
Command::Service(service) => {
|
||||
if config.skip_cmd.contains(&service.cmd) {
|
||||
eprintln!(
|
||||
"init: skipping '{} {}'",
|
||||
service.cmd,
|
||||
service.args.join(" ")
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
service.spawn(&config.envs);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let mut init_config = InitConfig::new();
|
||||
let mut unit_store = UnitStore::new();
|
||||
|
||||
+58
-4
@@ -1,7 +1,8 @@
|
||||
use std::collections::VecDeque;
|
||||
|
||||
use crate::InitConfig;
|
||||
use crate::unit::{UnitId, UnitStore};
|
||||
use crate::script::Command;
|
||||
use crate::unit::{Unit, UnitId, UnitKind, UnitStore};
|
||||
|
||||
pub struct Scheduler {
|
||||
pending: VecDeque<Job>,
|
||||
@@ -73,11 +74,64 @@ impl Scheduler {
|
||||
}
|
||||
}
|
||||
|
||||
if let Err(err) = crate::run(unit, init_config) {
|
||||
eprintln!("init: failed to run {}: {}", job.unit.0, err);
|
||||
}
|
||||
run(unit, init_config);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn run(unit: &mut Unit, config: &mut InitConfig) {
|
||||
match &unit.kind {
|
||||
UnitKind::LegacyScript { script } => {
|
||||
for cmd in script.0.clone() {
|
||||
if config.log_debug {
|
||||
eprintln!("init: running: {cmd:?}");
|
||||
}
|
||||
run_command(cmd, config);
|
||||
}
|
||||
}
|
||||
UnitKind::Service { service } => {
|
||||
if config.skip_cmd.contains(&service.cmd) {
|
||||
eprintln!("Skipping '{} {}'", service.cmd, service.args.join(" "));
|
||||
return;
|
||||
}
|
||||
if config.log_debug {
|
||||
eprintln!(
|
||||
"Starting {} ({})",
|
||||
unit.info.description.as_ref().unwrap_or(&unit.id.0),
|
||||
service.cmd,
|
||||
);
|
||||
}
|
||||
service.spawn(&config.envs);
|
||||
}
|
||||
UnitKind::Target {} => {
|
||||
if config.log_debug {
|
||||
eprintln!(
|
||||
"Reached target {}",
|
||||
unit.info.description.as_ref().unwrap_or(&unit.id.0),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn run_command(cmd: Command, config: &mut InitConfig) {
|
||||
match cmd {
|
||||
Command::RequiresWeak(_) => {} // handled by unit parsing code
|
||||
Command::Nothing => {}
|
||||
Command::Echo(text) => println!("{text}"),
|
||||
Command::Service(service) => {
|
||||
if config.skip_cmd.contains(&service.cmd) {
|
||||
eprintln!(
|
||||
"init: skipping '{} {}'",
|
||||
service.cmd,
|
||||
service.args.join(" ")
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
service.spawn(&config.envs);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user