]> git.proxmox.com Git - pve-lxc-syscalld.git/blame - src/main.rs
use c_str macro
[pve-lxc-syscalld.git] / src / main.rs
CommitLineData
9aa2a15a 1use std::future::Future;
9cffeac4
WB
2use std::io;
3
4use failure::{bail, format_err, Error};
5use nix::sys::socket::SockAddr;
6
9aa2a15a
WB
7#[macro_use]
8mod macros;
9
42f25756 10pub mod apparmor;
738dbfbe 11pub mod capability;
e420f6f9
WB
12pub mod client;
13pub mod fork;
9cffeac4 14pub mod lxcseccomp;
e420f6f9
WB
15pub mod nsfd;
16pub mod pidfd;
9cffeac4 17pub mod seccomp;
e420f6f9 18pub mod sys_mknod;
3e69a521 19pub mod sys_quotactl;
c95be5f6 20pub mod syscall;
9cffeac4
WB
21pub mod tools;
22
9aa2a15a
WB
23use io_uring::socket::SeqPacketListener;
24
25static mut EXECUTOR: *mut futures::executor::ThreadPool = std::ptr::null_mut();
26
27pub fn executor() -> &'static futures::executor::ThreadPool {
28 unsafe { &*EXECUTOR }
29}
30
31pub fn spawn(fut: impl Future<Output = ()> + Send + 'static) {
32 executor().spawn_ok(fut)
33}
34
35fn 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);
e420f6f9 41
9aa2a15a 42 if let Err(err) = executor.run(do_main()) {
9cffeac4
WB
43 eprintln!("error: {}", err);
44 std::process::exit(1);
45 }
46}
47
a563caf2 48async fn do_main() -> Result<(), Error> {
571dbe03 49 let socket_path = std::env::args_os()
9486338a 50 .nth(1)
571dbe03 51 .ok_or_else(|| format_err!("missing parameter: socket path to listen on"))?;
9cffeac4 52
571dbe03 53 match std::fs::remove_file(&socket_path) {
9cffeac4
WB
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
571dbe03
WB
59 let address =
60 SockAddr::new_unix(socket_path.as_os_str()).expect("cannot create struct sockaddr_un?");
9cffeac4 61
9aa2a15a 62 let mut listener = SeqPacketListener::bind_default(&address)
9cffeac4
WB
63 .map_err(|e| format_err!("failed to create listening socket: {}", e))?;
64 loop {
65 let client = listener.accept().await?;
e420f6f9 66 let client = client::Client::new(client);
9aa2a15a 67 spawn(client.main());
9cffeac4
WB
68 }
69}