|
1 | | -mod error; |
| 1 | +mod connection; |
| 2 | +mod daemon; |
| 3 | +mod logger; |
| 4 | +mod queue; |
2 | 5 |
|
3 | | -pub use error::*; |
4 | | -use smol::{Timer, net::TcpStream}; |
| 6 | +pub use crate::{connection::Connection, daemon::Daemon}; |
| 7 | +use lsp_message::LspMessage; |
| 8 | +pub use queue::*; |
5 | 9 | use std::{ |
6 | | - fs::remove_file, |
7 | | - io, |
8 | | - process::{Command, ExitCode}, |
| 10 | + io::{self, BufReader, ErrorKind}, |
| 11 | + os::unix::net::{SocketAddr, UnixStream}, |
| 12 | + sync::Arc, |
9 | 13 | time::Duration, |
10 | 14 | }; |
11 | 15 |
|
12 | | -pub fn start() -> ExitCode { |
13 | | - match try_become() { |
14 | | - Ok(()) => ExitCode::SUCCESS, |
15 | | - Err(error) => { |
16 | | - eprintln!("{}", error); |
17 | | - ExitCode::FAILURE |
18 | | - } |
| 16 | +pub fn main_loop(daemon: Daemon) -> io::Result<()> { |
| 17 | + let daemon = Arc::new(daemon); |
| 18 | + |
| 19 | + if logger::setup().is_err() { |
| 20 | + eprintln!("Failed to create daemon log file"); |
19 | 21 | } |
20 | | -} |
21 | 22 |
|
22 | | -/// Become the daemon process |
23 | | -pub fn try_become() -> io::Result<()> { |
24 | | - let cwd = std::env::current_dir().expect("Failed to get current directory"); |
25 | | - let filepath = cwd.join("adeptd.lock"); |
| 23 | + daemon |
| 24 | + .listener |
| 25 | + .set_nonblocking(true) |
| 26 | + .expect("Failed to set to non-blocking"); |
26 | 27 |
|
27 | | - let Some(lock) = lock_file::acquire(&filepath)? else { |
28 | | - eprintln!("Daemon already running."); |
29 | | - return Ok(()); |
30 | | - }; |
| 28 | + // Executor thread |
| 29 | + std::thread::spawn(|| {}); |
31 | 30 |
|
32 | | - eprintln!("Starting daemon..."); |
33 | | - daemon_scheduler::main()?; |
| 31 | + // Accept clients |
| 32 | + #[cfg(target_family = "unix")] |
| 33 | + loop { |
| 34 | + match daemon.listener.accept() { |
| 35 | + Ok((stream, address)) => { |
| 36 | + let daemon = Arc::clone(&daemon); |
| 37 | + std::thread::spawn(move || handle_client(daemon, stream, address)); |
| 38 | + } |
| 39 | + Err(error) => { |
| 40 | + if let io::ErrorKind::WouldBlock = error.kind() { |
| 41 | + // No clients ready to connect to us yet |
| 42 | + } else { |
| 43 | + log::error!("Failed to accept client: {:?}", error); |
| 44 | + } |
| 45 | + } |
| 46 | + } |
34 | 47 |
|
35 | | - eprintln!("Daemon shutting down..."); |
36 | | - drop(lock); |
| 48 | + if daemon.should_exit() { |
| 49 | + return Ok(()); |
| 50 | + } |
37 | 51 |
|
38 | | - remove_file(&filepath) |
| 52 | + std::thread::sleep(Duration::from_millis(50)); |
| 53 | + } |
39 | 54 | } |
40 | 55 |
|
41 | | -/// Tries to connect to the daemon process. If the daemon process |
42 | | -/// is not running yet, this function attempts to launch it. |
43 | | -pub async fn connect() -> Result<TcpStream, StartError> { |
44 | | - if let Ok(connection) = TcpStream::connect("127.0.0.1:6000").await { |
45 | | - eprintln!("Connected to existing daemon."); |
46 | | - return Ok(connection); |
47 | | - } |
| 56 | +fn handle_client(_daemon: Arc<Daemon>, stream: UnixStream, address: SocketAddr) { |
| 57 | + log::info!("Accepted client {:?} {:?}", stream, address); |
| 58 | + std::thread::sleep(Duration::from_millis(50)); |
48 | 59 |
|
49 | | - spawn()?; |
| 60 | + stream.set_nonblocking(false).unwrap(); |
| 61 | + stream |
| 62 | + .set_read_timeout(Some(Duration::from_millis(50))) |
| 63 | + .unwrap(); |
| 64 | + let reader = &mut BufReader::new(&stream); |
50 | 65 |
|
51 | | - for _ in 0..10 { |
52 | | - if let Ok(connection) = TcpStream::connect("127.0.0.1:6000").await { |
53 | | - return Ok(connection); |
| 66 | + loop { |
| 67 | + match LspMessage::read(reader) { |
| 68 | + Ok(None) => { |
| 69 | + log::info!("Shutting down connection to client"); |
| 70 | + break; |
| 71 | + } |
| 72 | + Ok(message) => { |
| 73 | + log::info!("Got message {:?}", message); |
| 74 | + } |
| 75 | + Err(error) => { |
| 76 | + if let ErrorKind::WouldBlock = error.kind() { |
| 77 | + // Nothing to do |
| 78 | + } else { |
| 79 | + log::error!("Error receiving message from client - {:?}", error); |
| 80 | + } |
| 81 | + } |
54 | 82 | } |
55 | | - Timer::after(Duration::from_millis(20)).await; |
56 | 83 | } |
57 | | - |
58 | | - Err(StartError::FailedToStart) |
59 | | -} |
60 | | - |
61 | | -pub fn spawn() -> std::io::Result<()> { |
62 | | - let exe = std::env::current_exe()?; |
63 | | - |
64 | | - // WARNING: SECURITY: This could lead to privilege escalation |
65 | | - // to the level the compiler is running at if an attacker |
66 | | - // overwrites the current executable. |
67 | | - // TL;DR - Don't let the compiler executable be changed |
68 | | - // by less privileged users. |
69 | | - Command::new(exe).arg("--daemon").spawn()?; |
70 | | - Ok(()) |
71 | 84 | } |
0 commit comments