Commit a0840b5b authored by SamwiseFilmore's avatar SamwiseFilmore

Add Process struct; Replace logger + colored logging

Using fern instead of simple_logger. Idk if it's a good idea, but I like
fern and it works well. I'm using standard colored too for output, I've
heard stories about it being slow, so we'll see if it's a problem. Might
change libs at some point, but it works well enough for now.
parent b95e039a
Pipeline #2773 passed with stage
in 2 minutes and 35 seconds
......@@ -71,13 +71,11 @@ dependencies = [
]
[[package]]
name = "chrono"
version = "0.4.6"
name = "colored"
version = "1.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"num-integer 0.1.39 (registry+https://github.com/rust-lang/crates.io-index)",
"num-traits 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)",
"time 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)",
"lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
......@@ -105,6 +103,14 @@ dependencies = [
"synstructure 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "fern"
version = "0.5.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "fuchsia-cprng"
version = "0.1.0"
......@@ -123,17 +129,29 @@ name = "init"
version = "0.1.0"
dependencies = [
"chashmap 2.2.1 (git+https://gitlab.redox-os.org/redox-os/tfs)",
"colored 1.7.0 (registry+https://github.com/rust-lang/crates.io-index)",
"failure 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
"fern 0.5.7 (registry+https://github.com/rust-lang/crates.io-index)",
"generational-arena 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
"latest 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
"redox_syscall 0.1.51 (registry+https://github.com/rust-lang/crates.io-index)",
"redox_users 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.85 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_derive 1.0.85 (registry+https://github.com/rust-lang/crates.io-index)",
"simple_logger 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
"toml 0.4.10 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "latest"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "lazy_static"
version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "libc"
version = "0.2.48"
......@@ -152,19 +170,6 @@ name = "nodrop"
version = "0.1.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "num-integer"
version = "0.1.39"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"num-traits 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "num-traits"
version = "0.2.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "owning_ref"
version = "0.3.3"
......@@ -283,15 +288,6 @@ dependencies = [
"syn 0.15.26 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "simple_logger"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"chrono 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "smallvec"
version = "0.6.8"
......@@ -326,16 +322,6 @@ dependencies = [
"unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "time"
version = "0.1.42"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"libc 0.2.48 (registry+https://github.com/rust-lang/crates.io-index)",
"redox_syscall 0.1.51 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "toml"
version = "0.4.10"
......@@ -391,17 +377,18 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum cc 1.0.28 (registry+https://github.com/rust-lang/crates.io-index)" = "bb4a8b715cb4597106ea87c7c84b2f1d452c7492033765df7f32651e66fcf749"
"checksum cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "082bb9b28e00d3c9d39cc03e64ce4cea0f1bb9b3fde493f0cbc008472d22bdf4"
"checksum chashmap 2.2.1 (git+https://gitlab.redox-os.org/redox-os/tfs)" = "<none>"
"checksum chrono 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)" = "45912881121cb26fad7c38c17ba7daa18764771836b34fab7d3fbd93ed633878"
"checksum colored 1.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "6e9a455e156a4271e12fd0246238c380b1e223e3736663c7a18ed8b6362028a9"
"checksum constant_time_eq 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "8ff012e225ce166d4422e0e78419d901719760f62ae2b7969ca6b564d1b54a9e"
"checksum failure 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "795bd83d3abeb9220f257e597aa0080a508b27533824adf336529648f6abf7e2"
"checksum failure_derive 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "ea1063915fd7ef4309e222a5a07cf9c319fb9c7836b1f89b85458672dbb127e1"
"checksum fern 0.5.7 (registry+https://github.com/rust-lang/crates.io-index)" = "b48af88aaf938b11baef948a5599e66e709cf92854aa2b87c71f1bcf20f80a01"
"checksum fuchsia-cprng 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "81f7f8eb465745ea9b02e2704612a9946a59fa40572086c6fd49d6ddcf30bf31"
"checksum generational-arena 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "e21bdad35146e7292a8462f37e6c6eafe26e1401737f4ff95b60e2a9afc4f9d5"
"checksum latest 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "dbcecbffc965131a019eb69ad21f897fedd12cc2a22577c63767721ec99ddb0a"
"checksum lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a374c89b9db55895453a74c1e38861d9deec0b01b405a82516e9d5de4820dea1"
"checksum libc 0.2.48 (registry+https://github.com/rust-lang/crates.io-index)" = "e962c7641008ac010fa60a7dfdc1712449f29c44ef2d4702394aea943ee75047"
"checksum log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)" = "c84ec4b527950aa83a329754b01dbe3f58361d1c5efacd1f6d68c494d08a17c6"
"checksum nodrop 0.1.13 (registry+https://github.com/rust-lang/crates.io-index)" = "2f9667ddcc6cc8a43afc9b7917599d7216aa09c463919ea32c59ed6cac8bc945"
"checksum num-integer 0.1.39 (registry+https://github.com/rust-lang/crates.io-index)" = "e83d528d2677f0518c570baf2b7abdcf0cd2d248860b68507bdcb3e91d4c0cea"
"checksum num-traits 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)" = "0b3a5d7cc97d6d30d8b9bc8fa19bf45349ffe46241e8816f50f62f6d6aaabee1"
"checksum owning_ref 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "cdf84f41639e037b484f93433aa3897863b561ed65c6e59c7073d7c561710f37"
"checksum parking_lot 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)" = "149d8f5b97f3c1133e3cfcd8886449959e856b557ff281e292b733d7c69e005e"
"checksum parking_lot_core 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)" = "4db1a8ccf734a7bce794cc19b3df06ed87ab2f3907036b693c68f56b4d4537fa"
......@@ -417,12 +404,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum scoped_threadpool 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)" = "1d51f5df5af43ab3f1360b429fa5e0152ac5ce8c0bd6485cae490332e96846a8"
"checksum serde 1.0.85 (registry+https://github.com/rust-lang/crates.io-index)" = "534b8b91a95e0f71bca3ed5824752d558da048d4248c91af873b63bd60519752"
"checksum serde_derive 1.0.85 (registry+https://github.com/rust-lang/crates.io-index)" = "a915306b0f1ac5607797697148c223bedeaa36bcc2e28a01441cd638cc6567b4"
"checksum simple_logger 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "25111f1d77db1ac3ee11b62ba4b7a162e6bb3be43e28273f0d3935cc8d3ff7fb"
"checksum smallvec 0.6.8 (registry+https://github.com/rust-lang/crates.io-index)" = "88aea073965ab29f6edb5493faf96ad662fb18aa9eeb186a3b7057951605ed15"
"checksum stable_deref_trait 1.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "dba1a27d3efae4351c8051072d619e3ade2820635c3958d826bfea39d59b54c8"
"checksum syn 0.15.26 (registry+https://github.com/rust-lang/crates.io-index)" = "f92e629aa1d9c827b2bb8297046c1ccffc57c99b947a680d3ccff1f136a3bee9"
"checksum synstructure 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)" = "73687139bf99285483c96ac0add482c3776528beac1d97d444f6e91f203a2015"
"checksum time 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)" = "db8dcfca086c1143c9270ac42a2bbd8a7ee477b78ac8e45b19abfb0cbede4b6f"
"checksum toml 0.4.10 (registry+https://github.com/rust-lang/crates.io-index)" = "758664fc71a3a69038656bee8b6be6477d2a6c315a6b81f7081f591bffa4111f"
"checksum unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc"
"checksum unreachable 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "382810877fe448991dfc7f0dd6e3ae5d58088fd0ea5e35189655f84e6814fa56"
......
......@@ -5,15 +5,16 @@ name = "init"
version = "0.1.0"
[dependencies]
colored = "1.7"
failure = "0.1"
fern = "0.5"
generational-arena = "0.2"
latest = "*"
log = "0.4"
#rayon = "1.0"
redox_syscall = "0.1"
redox_users = "*"
redox_users = "0.2"
serde = "1.0"
serde_derive = "1.0"
simple_logger = "1.0"
toml = "0.4"
[dependencies.chashmap]
......
......@@ -10,6 +10,10 @@ use syscall::{
error::Error as SyscallError
};
fn as_io_err(err: SyscallError) -> Error {
Error::from_raw_os_error(err.errno)
}
/// An alternative to `std::process::Command` for
/// just redox, which allows for more flexibility
/// in extending the API.
......@@ -41,11 +45,6 @@ impl Command {
}
}
pub fn arg(&mut self, arg: String) -> &mut Command {
self.args.push(arg);
self
}
pub fn args(&mut self, mut args: Vec<String>) -> &mut Command {
self.args.append(&mut args);
self
......@@ -81,17 +80,17 @@ impl Command {
self
}
pub fn spawn(&self) -> Result<()> {
const CLOEXEC_MSG_FOOTER: &[u8] = b"NOEX";
pub fn spawn(self) -> Result<Process> {
//const CLOEXEC_MSG_FOOTER: &[u8] = b"NOEX";
let bin = File::open(&self.bin)?;
// This is ust copied from the std redox impl
let pid = unsafe {
match syscall::clone(0).map_err(|e| Error::from_raw_os_error(e.errno) )? {
match syscall::clone(0).map_err(as_io_err)? {
0 => {
let err = self.do_exec(bin);
let bytes = [
let _err = self.do_exec(bin);
/*let bytes = [
(err.errno >> 24) as u8,
(err.errno >> 16) as u8,
(err.errno >> 8) as u8,
......@@ -103,18 +102,14 @@ impl Command {
// we want to be sure we *don't* run at_exit destructors as
// we're being torn down regardless
//assert!(output.write(&bytes).is_ok());
let _ = syscall::exit(1);
let _ = syscall::exit(1);*/
panic!("failed to exit");
}
n => n,
}
};
let mut status = 0;
syscall::waitpid(pid, &mut status, 0)
.map_err(|e| Error::from_raw_os_error(e.errno) )?;
Ok(())
Ok(Process::new(pid))
}
/// This puppy sets env vars, user/group ids, cwd, namespaces,
......@@ -122,7 +117,7 @@ impl Command {
// Currently not parsing shebangs or $PATH for bin locations
// Open files and things at the top here, so that it
// doesn't interfere with namespace setting
fn do_exec(&self, bin: File) -> SyscallError {
fn do_exec(self, bin: File) -> SyscallError {
macro_rules! t {
($err:expr) => {
match $err {
......@@ -191,3 +186,29 @@ fn raw_ns(schemes: &Vec<String>) -> Vec<[usize; 2]> {
}
ptrs
}
pub struct Process {
pid: usize,
status: Option<usize>,
}
impl Process {
fn new(pid: usize) -> Process {
Process {
pid,
status: None
}
}
pub fn wait(&mut self) -> Result<usize> {
if let Some(status) = self.status {
Ok(status)
} else {
let mut status = 0;
syscall::waitpid(self.pid, &mut status, 0)
.map_err(as_io_err)?;
self.status = Some(status);
Ok(status)
}
}
}
//#![deny(warnings)]
#![feature(dbg_macro)]
#![feature(duration_float)]
mod command;
mod dep_graph;
......@@ -12,8 +13,11 @@ use std::fs::{self, File};
use std::io::{Error, Result};
use std::os::unix::io::{AsRawFd, FromRawFd};
use std::path::{Path, PathBuf};
use std::time::Instant;
use log::error;
use colored::{ColoredString, Colorize};
use fern::Dispatch;
use log::{error, Level, LevelFilter};
use syscall::flag::{O_RDONLY, O_WRONLY};
use crate::service::Service;
......@@ -56,9 +60,37 @@ impl PathExt for Path {
}
pub fn main() {
env::set_var("RUST_BACKTRACE", "1");
let start_time = Instant::now();
//env::set_var("RUST_BACKTRACE", "1");
simple_logger::init()
// Could use fern for this, but standard length...
fn color(lvl: Level) -> ColoredString {
match lvl {
Level::Error => "Error".red(),
Level::Warn => "Warn ".yellow(),
Level::Info => "Info ".blue(),
Level::Debug => "Debug".green(),
Level::Trace => "Trace".into()
}.bold()
}
Dispatch::new()
.format(move |out, message, record| {
let time = Instant::now()
.duration_since(start_time);
let time = format!("{:.3}", time.as_float_secs());
out.finish(format_args!(
"[ {} ][ {} ] {}",
time.green(),
color(record.level()),
message
))
})
.chain(std::io::stdout())
.level(LevelFilter::Trace)
.apply()
.unwrap_or_else(|err| {
println!("init: failed to start logger: {}", err);
});
......
......@@ -146,11 +146,10 @@ impl Method {
cmd.ns(namespace.to_vec());
}
debug!("waiting on '{}'", cmd);
dbg!(&cmd);
debug!("waiting on process '{}'", cmd);
cmd.spawn()?;
//.wait()?;
cmd.spawn()?
.wait()?;
Ok(())
}
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment