Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,6 @@ In order to call a script with shorthand, a user may apply an *alias* using the
alias <shorthand> <full_name>
```
Subsequent use of the shorthand will then be interpolated as the process ID.
Aliases are not currently persisted between boots, although this may change.

A list of the other terminal scripts included in this distro:

Expand Down
12 changes: 8 additions & 4 deletions kinode/packages/chess/chess/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,14 @@ fn load_chess_state() -> ChessState {
games,
clients: HashSet::new(),
},
None => ChessState {
games: HashMap::new(),
clients: HashSet::new(),
},
None => {
let state = ChessState {
games: HashMap::new(),
clients: HashSet::new(),
};
save_chess_state(&state);
state
}
}
}

Expand Down
211 changes: 86 additions & 125 deletions kinode/packages/terminal/terminal/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use anyhow::anyhow;
use kinode_process_lib::kernel_types as kt;
use kinode_process_lib::kinode::process::standard as wit;
use kinode_process_lib::{
call_init, get_blob, get_typed_state, our_capabilities, print_to_terminal, println, set_state,
vfs, Address, Capability, ProcessId, Request,
call_init, get_blob, get_typed_state, our_capabilities, println, set_state, vfs, Address,
Capability, ProcessId, Request,
};
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet};
Expand Down Expand Up @@ -42,69 +42,70 @@ fn parse_command(state: &mut TerminalState, line: &str) -> anyhow::Result<()> {
},
};

match handle_run(&state.our, &process, args.to_string()) {
Ok(_) => Ok(()), // TODO clean up process
Err(e) => Err(anyhow!("failed to instantiate script: {}", e)),
}
handle_run(&state.our, &process, args.to_string())
}

call_init!(init);
fn init(our: Address) {
let mut state: TerminalState = match get_typed_state(|bytes| Ok(bincode::deserialize(bytes)?)) {
Some(s) => s,
None => TerminalState {
our,
aliases: HashMap::from([
(
"alias".to_string(),
ProcessId::new(Some("alias"), "terminal", "sys"),
),
(
"cat".to_string(),
ProcessId::new(Some("cat"), "terminal", "sys"),
),
(
"echo".to_string(),
ProcessId::new(Some("echo"), "terminal", "sys"),
),
(
"hi".to_string(),
ProcessId::new(Some("hi"), "terminal", "sys"),
),
(
"kill".to_string(),
ProcessId::new(Some("kill"), "terminal", "sys"),
),
(
"kfetch".to_string(),
ProcessId::new(Some("kfetch"), "terminal", "sys"),
),
(
"m".to_string(),
ProcessId::new(Some("m"), "terminal", "sys"),
),
(
"namehash_to_name".to_string(),
ProcessId::new(Some("namehash_to_name"), "terminal", "sys"),
),
(
"net_diagnostics".to_string(),
ProcessId::new(Some("net_diagnostics"), "terminal", "sys"),
),
(
"peer".to_string(),
ProcessId::new(Some("peer"), "terminal", "sys"),
),
(
"peers".to_string(),
ProcessId::new(Some("peers"), "terminal", "sys"),
),
(
"top".to_string(),
ProcessId::new(Some("top"), "terminal", "sys"),
),
]),
},
None => {
let state = TerminalState {
our,
aliases: HashMap::from([
(
"alias".to_string(),
ProcessId::new(Some("alias"), "terminal", "sys"),
),
(
"cat".to_string(),
ProcessId::new(Some("cat"), "terminal", "sys"),
),
(
"echo".to_string(),
ProcessId::new(Some("echo"), "terminal", "sys"),
),
(
"hi".to_string(),
ProcessId::new(Some("hi"), "terminal", "sys"),
),
(
"kill".to_string(),
ProcessId::new(Some("kill"), "terminal", "sys"),
),
(
"kfetch".to_string(),
ProcessId::new(Some("kfetch"), "terminal", "sys"),
),
(
"m".to_string(),
ProcessId::new(Some("m"), "terminal", "sys"),
),
(
"namehash_to_name".to_string(),
ProcessId::new(Some("namehash_to_name"), "terminal", "sys"),
),
(
"net_diagnostics".to_string(),
ProcessId::new(Some("net_diagnostics"), "terminal", "sys"),
),
(
"peer".to_string(),
ProcessId::new(Some("peer"), "terminal", "sys"),
),
(
"peers".to_string(),
ProcessId::new(Some("peers"), "terminal", "sys"),
),
(
"top".to_string(),
ProcessId::new(Some("top"), "terminal", "sys"),
),
]),
};
set_state(&bincode::serialize(&state).unwrap());
state
}
};

loop {
Expand All @@ -126,7 +127,7 @@ fn init(our: Address) {
// checks for a request from a terminal script (different process, same package)
} else if state.our.node == source.node && state.our.package() == source.package() {
let Ok(action) = serde_json::from_slice::<TerminalAction>(&body) else {
println!("failed to parse action from: {}", source);
println!("failed to parse action from {source}");
continue;
};
match action {
Expand All @@ -138,7 +139,7 @@ fn init(our: Address) {
}
}
} else {
println!("ignoring message from: {}", source);
println!("ignoring message from {source}");
continue;
}
}
Expand All @@ -154,26 +155,16 @@ fn init(our: Address) {
}

fn handle_run(our: &Address, process: &ProcessId, args: String) -> anyhow::Result<()> {
let wasm_path = format!("{}.wasm", process.process());
let package = format!("{}:{}", process.package(), process.publisher());
let drive_path = format!("/{}/pkg", package);
let drive_path = format!("/{}:{}/pkg", process.package(), process.publisher());
let Ok(entry) = get_entry(process) else {
return Err(anyhow::anyhow!("script not in scripts.json file"));
};
let wasm_path = if wasm_path.starts_with("/") {
wasm_path
} else {
format!("/{}", wasm_path)
};
let wasm_path = format!("{}{}", drive_path, wasm_path);
// build initial caps
let process_id = format!("{}:{}", rand::random::<u64>(), package); // all scripts are given random process IDs
let Ok(parsed_new_process_id) = process_id.parse::<ProcessId>() else {
return Err(anyhow::anyhow!("invalid process id!"));
};
let wasm_path = format!("{drive_path}/{}.wasm", process.process());

let _bytes_response = Request::new()
.target(("our", "vfs", "distro", "sys"))
// all scripts are given random process IDs
let process_id = ProcessId::new(None, process.package(), process.publisher());

Request::to(("our", "vfs", "distro", "sys"))
.body(serde_json::to_vec(&vfs::VfsRequest {
path: wasm_path.clone(),
action: vfs::VfsAction::Read,
Expand All @@ -191,7 +182,7 @@ fn handle_run(our: &Address, process: &ProcessId, args: String) -> anyhow::Resul
Capability {
issuer: Address {
node: our.node.clone(),
process: parsed_new_process_id.clone(),
process: process_id.clone(),
},
params: "\"messaging\"".into(),
},
Expand All @@ -211,7 +202,7 @@ fn handle_run(our: &Address, process: &ProcessId, args: String) -> anyhow::Resul
Capability {
issuer: Address {
node: our.node.clone(),
process: parsed_new_process_id.clone(),
process: process_id.clone(),
},
params: params.to_string(),
},
Expand All @@ -227,8 +218,7 @@ fn handle_run(our: &Address, process: &ProcessId, args: String) -> anyhow::Resul
}
}
for (process, cap) in granted_caps.into_iter() {
Request::new()
.target(("our", "kernel", "distro", "sys"))
Request::to(("our", "kernel", "distro", "sys"))
.body(serde_json::to_vec(&kt::KernelCommand::GrantCapabilities {
target: process,
capabilities: vec![kt::de_wit_capability(cap)],
Expand All @@ -237,10 +227,9 @@ fn handle_run(our: &Address, process: &ProcessId, args: String) -> anyhow::Resul
}
// inherits the blob from the previous request, `_bytes_response`,
// containing the wasm byte code of the process
Request::new()
.target(("our", "kernel", "distro", "sys"))
Request::to(("our", "kernel", "distro", "sys"))
.body(serde_json::to_vec(&kt::KernelCommand::InitializeProcess {
id: parsed_new_process_id.clone(),
id: process_id.clone(),
wasm_bytes_handle: wasm_path.clone(),
wit_version: entry.wit_version,
on_exit: kt::OnExit::None,
Expand Down Expand Up @@ -305,42 +294,20 @@ fn handle_run(our: &Address, process: &ProcessId, args: String) -> anyhow::Resul
requested_caps.push(kt::de_wit_capability(cap.clone()));
}
}
print_to_terminal(
3,
&format!(
"{}: Process {{\n wasm_bytes_handle: {},\n on_exit: {:?},\n public: {}\n capabilities: {}\n}}",
parsed_new_process_id.clone(),
wasm_path.clone(),
kt::OnExit::None,
entry.public,
{
let mut caps_string = "[".to_string();
for cap in requested_caps.iter() {
caps_string += &format!("\n {}({})", cap.issuer.to_string(), cap.params);
}
caps_string + "\n ]"
},
),
);
Request::new()
.target(("our", "kernel", "distro", "sys"))
Request::to(("our", "kernel", "distro", "sys"))
.body(serde_json::to_vec(&kt::KernelCommand::GrantCapabilities {
target: parsed_new_process_id.clone(),
target: process_id.clone(),
capabilities: requested_caps,
})?)
.send()?;
let _ = Request::new()
.target(("our", "kernel", "distro", "sys"))
Request::to(("our", "kernel", "distro", "sys"))
.body(serde_json::to_vec(&kt::KernelCommand::RunProcess(
parsed_new_process_id.clone(),
process_id.clone(),
))?)
.send_and_await_response(5)??;
let req = Request::new()
.target(("our", parsed_new_process_id))
.body(args.into_bytes());

req.send().unwrap();

Request::to(("our", process_id))
.body(args.into_bytes())
.send()?;
Ok(())
}

Expand All @@ -351,20 +318,15 @@ fn handle_alias_change(
) -> anyhow::Result<()> {
match process {
Some(process) => {
// first check to make sure the script is actually a script
let Ok(_) = get_entry(&process) else {
return Err(anyhow!("process {} not found", process));
};

state.aliases.insert(alias.clone(), process.clone());
println!("alias {} set to {}", alias, process);
println!("alias {alias} set for {process}");
state.aliases.insert(alias, process);
}
None => {
if state.aliases.contains_key(&alias) {
state.aliases.remove(&alias);
println!("alias {} removed", alias);
println!("alias {alias} removed");
} else {
println!("alias {} not found", alias);
println!("alias {alias} not found");
}
}
}
Expand All @@ -374,10 +336,9 @@ fn handle_alias_change(

fn get_entry(process: &ProcessId) -> anyhow::Result<kt::DotScriptsEntry> {
let drive_path = format!("/{}:{}/pkg", process.package(), process.publisher());
Request::new()
.target(("our", "vfs", "distro", "sys"))
Request::to(("our", "vfs", "distro", "sys"))
.body(serde_json::to_vec(&vfs::VfsRequest {
path: format!("{}/scripts.json", drive_path),
path: format!("{drive_path}/scripts.json"),
action: vfs::VfsAction::Read,
})?)
.send_and_await_response(5)??;
Expand Down
11 changes: 1 addition & 10 deletions kinode/src/eth/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -908,16 +908,7 @@ async fn check_for_root_cap(
caps_oracle
.send(CapMessage::Has {
on: process.clone(),
cap: Capability {
issuer: Address {
node: our.to_string(),
process: ETH_PROCESS_ID.clone(),
},
params: serde_json::to_string(&serde_json::json!({
"root": true,
}))
.unwrap(),
},
cap: Capability::new((our, ETH_PROCESS_ID.clone()), "{\"root\":true}"),
responder: send_cap_bool,
})
.await
Expand Down
Loading