]> git.proxmox.com Git - pve-lxc-syscalld.git/blob - src/main.rs
started working on a replacement executor
[pve-lxc-syscalld.git] / src / main.rs
1 use std::future::Future;
2 use std::io;
3
4 use failure::{bail, format_err, Error};
5 use nix::sys::socket::SockAddr;
6
7 #[macro_use]
8 mod macros;
9
10 pub mod apparmor;
11 pub mod capability;
12 pub mod client;
13 pub mod executor;
14 pub mod fork;
15 pub mod lxcseccomp;
16 pub mod nsfd;
17 pub mod process;
18 pub mod seccomp;
19 pub mod sys_mknod;
20 pub mod sys_quotactl;
21 pub mod syscall;
22 pub mod tools;
23
24 use io_uring::socket::SeqPacketListener;
25
26 static mut EXECUTOR: *mut futures_executor::ThreadPool = std::ptr::null_mut();
27
28 pub fn executor() -> &'static futures_executor::ThreadPool {
29 unsafe { &*EXECUTOR }
30 }
31
32 pub fn spawn(fut: impl Future<Output = ()> + Send + 'static) {
33 executor().spawn_ok(fut)
34 }
35
36 fn main() {
37 let mut executor = futures_executor::ThreadPool::new().expect("spawning worker threadpool");
38 unsafe {
39 EXECUTOR = &mut executor;
40 }
41 std::sync::atomic::fence(std::sync::atomic::Ordering::Release);
42
43 if let Err(err) = executor.run(do_main()) {
44 eprintln!("error: {}", err);
45 std::process::exit(1);
46 }
47 }
48
49 async fn do_main() -> Result<(), Error> {
50 let socket_path = std::env::args_os()
51 .nth(1)
52 .ok_or_else(|| format_err!("missing parameter: socket path to listen on"))?;
53
54 match std::fs::remove_file(&socket_path) {
55 Ok(_) => (),
56 Err(ref e) if e.kind() == io::ErrorKind::NotFound => (), // Ok
57 Err(e) => bail!("failed to remove previous socket: {}", e),
58 }
59
60 let address =
61 SockAddr::new_unix(socket_path.as_os_str()).expect("cannot create struct sockaddr_un?");
62
63 let mut listener = SeqPacketListener::bind_default(&address)
64 .map_err(|e| format_err!("failed to create listening socket: {}", e))?;
65 loop {
66 let client = listener.accept().await?;
67 let client = client::Client::new(client);
68 spawn(client.main());
69 }
70 }