Move the socket from /tmp/lqos_bus to /run/lqos/bus and change permissions to match.

This commit is contained in:
Herbert Wolverson 2023-01-20 14:27:37 +00:00
parent e857f20ea1
commit fba591595b
2 changed files with 22 additions and 1 deletions

View File

@ -16,7 +16,11 @@ pub use persistent_client::BusClient;
/// The local socket path to which `lqosd` will bind itself, /// The local socket path to which `lqosd` will bind itself,
/// listening for requets. /// 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 /// Encodes a BusSession with `bincode`, providing a tight binary
/// representation of the request object for TCP transmission. /// representation of the request object for TCP transmission.

View File

@ -5,6 +5,8 @@ use nix::libc::mode_t;
use tokio::{net::{UnixListener, UnixStream}, io::{AsyncReadExt, AsyncWriteExt}}; use tokio::{net::{UnixListener, UnixStream}, io::{AsyncReadExt, AsyncWriteExt}};
use log::warn; use log::warn;
use super::BUS_SOCKET_DIRECTORY;
/// Implements a Tokio-friendly server using Unix Sockets and the bus protocol. /// Implements a Tokio-friendly server using Unix Sockets and the bus protocol.
/// Requests are handled and then forwarded to the handler. /// Requests are handled and then forwarded to the handler.
pub struct UnixSocketServer {} pub struct UnixSocketServer {}
@ -13,10 +15,25 @@ impl UnixSocketServer {
/// Creates a new `UnixSocketServer`. Will delete any pre-existing /// Creates a new `UnixSocketServer`. Will delete any pre-existing
/// socket file. /// socket file.
pub fn new() -> Result<Self> { pub fn new() -> Result<Self> {
Self::check_directory()?;
Self::delete_local_socket()?; Self::delete_local_socket()?;
Ok(Self {}) 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<()> { fn delete_local_socket() -> Result<()> {
let socket_path = std::path::Path::new(BUS_SOCKET_PATH); let socket_path = std::path::Path::new(BUS_SOCKET_PATH);
if socket_path.exists() { if socket_path.exists() {