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