aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/export_fun.rs50
1 files changed, 50 insertions, 0 deletions
diff --git a/src/export_fun.rs b/src/export_fun.rs
index 006bec3..64ac08b 100644
--- a/src/export_fun.rs
+++ b/src/export_fun.rs
@@ -1,14 +1,17 @@
//! allow sub-programs to invoke arbitrary user-defined functions via a unix socket
use crate::Session;
+use crate::run::get_command_kind;
use std::env::current_exe;
use std::ffi::OsStr;
use std::fs;
use std::io;
+use std::io::IoSliceMut;
use std::io::Read;
use std::io::Write;
use std::os::unix::ffi::OsStrExt;
use std::os::unix::fs::symlink;
+use std::os::unix::net::AncillaryData;
use std::os::unix::net::SocketAncillary;
use std::os::unix::net::UnixListener;
use std::os::unix::net::UnixStream;
@@ -20,6 +23,53 @@ use std::sync::Mutex;
use std::thread;
fn handle_server(session: Arc<Mutex<Session>>, stream: UnixStream) -> io::Result<()> {
+ // TODO: figure out how to get a reasonable limit on CLI arg len
+ let mut buf = [0u8; 8192];
+ let mut iov = [IoSliceMut::new(&mut buf)];
+
+ let mut ancillary_buf = [0u8; 128];
+ let mut ancillary = SocketAncillary::new(&mut ancillary_buf);
+
+ let mut fds: Vec<i32> = Vec::new();
+
+ let bytelen = stream.recv_vectored_with_ancillary(&mut iov, &mut ancillary)?;
+
+ for msg in ancillary.messages() {
+ if let Ok(msg) = msg {
+ match msg {
+ AncillaryData::ScmRights(rights) => {
+ for fd in rights {
+ fds.push(fd);
+ }
+ }
+ _ => (),
+ }
+ }
+ }
+
+ if fds.len() != 3 {
+ // malformed
+ return Ok(());
+ }
+
+ let Ok(cli_args) = crate::serialization::deserialize_cli_args(&buf[..bytelen]) else {
+ // cli args malformed
+ return Ok(());
+ };
+
+ if cli_args.is_empty() {
+ // malformed
+ return Ok(());
+ };
+
+ let se = session.lock().unwrap();
+ match get_command_kind(&se, cli_args[0].as_slice()) {
+ crate::run::CommandKind::Fun(ast) => todo!(),
+ crate::run::CommandKind::Path(_) | crate::run::CommandKind::Builtin(_) => {
+ return Ok(());
+ }
+ }
+
todo!()
}