diff --git a/src/rust/lqos_bus/src/bus/mod.rs b/src/rust/lqos_bus/src/bus/mod.rs index 90d68363..b2ede461 100644 --- a/src/rust/lqos_bus/src/bus/mod.rs +++ b/src/rust/lqos_bus/src/bus/mod.rs @@ -16,7 +16,11 @@ pub use persistent_client::BusClient; /// The local socket path to which `lqosd` will bind itself, /// listening for requets. -pub const BUS_SOCKET_PATH: &str = "/tmp/lqos_bus"; +pub const BUS_SOCKET_PATH: &str = "/run/lqos/bus"; + +/// The directory containing the bus socket. Used for ensuring +/// that the directory exists. +pub(crate) const BUS_SOCKET_DIRECTORY: &str = "/run/lqos/."; /// Encodes a BusSession with `bincode`, providing a tight binary /// representation of the request object for TCP transmission. diff --git a/src/rust/lqos_bus/src/bus/unix_socket_server.rs b/src/rust/lqos_bus/src/bus/unix_socket_server.rs index 832e0c11..839e870c 100644 --- a/src/rust/lqos_bus/src/bus/unix_socket_server.rs +++ b/src/rust/lqos_bus/src/bus/unix_socket_server.rs @@ -5,6 +5,8 @@ use nix::libc::mode_t; use tokio::{net::{UnixListener, UnixStream}, io::{AsyncReadExt, AsyncWriteExt}}; use log::warn; +use super::BUS_SOCKET_DIRECTORY; + /// Implements a Tokio-friendly server using Unix Sockets and the bus protocol. /// Requests are handled and then forwarded to the handler. pub struct UnixSocketServer {} @@ -13,10 +15,25 @@ impl UnixSocketServer { /// Creates a new `UnixSocketServer`. Will delete any pre-existing /// socket file. pub fn new() -> Result { + Self::check_directory()?; Self::delete_local_socket()?; Ok(Self {}) } + fn check_directory() -> Result<()> { + let dir_path = std::path::Path::new(BUS_SOCKET_DIRECTORY); + if dir_path.exists() && dir_path.is_dir() { + Ok(()) + } else { + std::fs::create_dir(dir_path)?; + let unix_path = CString::new(BUS_SOCKET_DIRECTORY)?; + unsafe { + nix::libc::chmod(unix_path.as_ptr(), mode_t::from_le(666)); + } + Ok(()) + } + } + fn delete_local_socket() -> Result<()> { let socket_path = std::path::Path::new(BUS_SOCKET_PATH); if socket_path.exists() {