feat: group should have write permission on the socket file

This commit is contained in:
2026-04-01 06:33:34 +02:00
parent 6e81197419
commit a49e7c7e4b
4 changed files with 39 additions and 6 deletions

View File

@@ -5,7 +5,7 @@ socket = "unix:/tmp/lua1.sock"
pidfile = "/tmp/luad.pid"
# user name
user = "dany"
user = "root"
# group name
group = "dany"

View File

@@ -23,7 +23,7 @@ pub const APP_VERSION: &str = env!("CARGO_PKG_VERSION");
pub const DAEMON_NAME: &str = "luad";
mod utils;
pub use utils::{is_unix_socket, on_exit, privdrop};
pub use utils::{chmod_file, is_unix_socket, on_exit, privdrop};
mod logs;
pub use logs::{LogLevel, LogManager};

View File

@@ -37,11 +37,17 @@ const DEFAULT_WORKER_NUMBER: usize = 4;
///
/// * `n` - system exit code
fn clean_up(n: i32) {
if let Ok(socket_name) = std::env::var("socket") {
if let Ok(socket_name) = std::env::var("LUAD_SOCKET") {
let file = socket_name.replace("unix:", "");
let path = Path::new(&file);
if path.exists() {
std::fs::remove_file(path).unwrap();
let _ = std::fs::remove_file(path);
}
}
if let Ok(pidfile) = std::env::var("LUAD_PID") {
let path = Path::new(&pidfile);
if path.exists() {
let _ = std::fs::remove_file(path);
}
}
if n != 0 {
@@ -73,9 +79,15 @@ fn serve(config: &Config) {
if socket_name.starts_with("unix:") {
// e.g unix:/var/run/lighttpd/maint/efcgi.socket
INFO!("Use unix domain socket: {}", socket_name);
std::env::set_var("socket", socket_name);
std::env::set_var("LUAD_SOCKET", socket_name);
clean_up(0);
let listener = UnixListener::bind(socket_name.replace("unix:", "")).unwrap();
let path = socket_name.replace("unix:", "");
let listener = UnixListener::bind(&path).unwrap();
INFO!("Allow writeable for group on {}", path);
if let Err(error) = chmod_file(&path, 0o660) {
ERROR!("Unable to allow writable for group on {}: {}", path, error);
}
for client in listener.incoming() {
let mut stream = client.unwrap();
pool.execute(move || handle_request(&mut stream));
@@ -174,6 +186,7 @@ fn main() {
let mut f = std::fs::File::create(&pidfile).unwrap();
write!(f, "{}", std::process::id()).unwrap();
DEBUG!("PID file created at {}", pidfile);
std::env::set_var("LUAD_PID", pidfile);
}
None => {}
}

View File

@@ -1,3 +1,5 @@
use std::ffi::CString;
use crate::{ERR, INFO};
use nix;
@@ -80,6 +82,24 @@ pub fn on_exit(f: fn(n: i32) -> ()) {
};
}
/// Change file mode
///
/// # Arguments
///
/// - `file` (`&str`) - input file
///
/// # Returns
///
/// - `Result<(), std::io::Error>`
///
pub fn chmod_file(file: &str, mode: u32) -> Result<(), std::io::Error> {
let c_path = CString::new(file)?;
if unsafe { libc::chmod(c_path.as_ptr(), mode) } != 0 {
return Err(ERR!(format!("Chmod failed: {}", file)));
}
Ok(())
}
/// Return an Error Result object from error string
///
#[macro_export]