luafcgi/src/main.rs

186 lines
5.6 KiB
Rust
Raw Normal View History

2023-01-16 01:03:00 +01:00
//! Lua FastCGI main application
//!
//! **Author**: "Dany LE <mrsang@iohub.dev>"
//!
2023-01-17 00:59:57 +01:00
//!
2023-01-16 01:03:00 +01:00
#![warn(
trivial_casts,
trivial_numeric_casts,
unused_extern_crates,
unused_qualifications,
unused_results,
missing_docs,
clippy::pedantic,
clippy::missing_docs_in_private_items
)]
2023-01-17 00:59:57 +01:00
use clap;
2023-01-16 01:03:00 +01:00
use serde;
use toml;
//use std::fs::File;
2023-01-17 00:59:57 +01:00
use luad::*;
use std::io::Read;
2023-01-16 01:03:00 +01:00
use std::io::Write;
use std::net::TcpListener;
2023-01-17 00:59:57 +01:00
use std::os::fd::FromRawFd;
2023-01-16 01:03:00 +01:00
use std::os::unix::io::AsRawFd;
use std::os::unix::net::UnixListener;
use std::path::Path;
use std::thread;
/// Callback: clean up function
///
/// This function remove the unix socket file if
/// exist before quiting the program
///
/// # Arguments
///
/// * `n` - system exit code
fn clean_up(n: i32) {
if let Ok(socket_name) = std::env::var("socket") {
let file = socket_name.replace("unix:", "");
let path = Path::new(&file);
if path.exists() {
std::fs::remove_file(path).unwrap();
}
}
if n != 0 {
2023-01-17 00:59:57 +01:00
ERROR!(
"The LUA fastCGI daemon is terminated by system signal: {}",
n
);
std::process::exit(0);
2023-01-16 01:03:00 +01:00
}
}
2023-01-17 00:59:57 +01:00
fn handle_request<T: Read + Write + AsRawFd>(stream: &mut T) {
if let Err(error) = process_request(stream) {
2023-01-16 01:03:00 +01:00
ERROR!("Unable to process request: {}", error);
}
2023-01-26 02:22:05 +01:00
DEBUG!("Request on socket {} is processed", stream.as_raw_fd());
2023-01-16 01:03:00 +01:00
}
/// Start the `fastCGI` server
///
/// # Arguments
///
/// * `socket_opt` - The socket string that the server listens on
2023-01-17 00:59:57 +01:00
fn serve(config: &Config) {
2023-01-16 01:03:00 +01:00
// bind to a socket if any
2023-01-17 00:59:57 +01:00
if let Some(socket_name) = config.socket.as_deref() {
2023-01-16 01:03:00 +01:00
// test if the socket name is an unix domain socket
2023-01-17 00:59:57 +01:00
if socket_name.starts_with("unix:") {
2023-01-16 01:03:00 +01:00
// e.g unix:/var/run/lighttpd/maint/efcgi.socket
INFO!("Use unix domain socket: {}", socket_name);
std::env::set_var("socket", socket_name);
2023-01-17 00:59:57 +01:00
clean_up(0);
2023-01-16 01:03:00 +01:00
let listener = UnixListener::bind(socket_name.replace("unix:", "")).unwrap();
for client in listener.incoming() {
let mut stream = client.unwrap();
2023-01-17 00:59:57 +01:00
let _ = std::thread::spawn(move || {
2023-01-16 01:03:00 +01:00
handle_request(&mut stream);
});
}
} else {
// TCP socket eg. 127.0.0.1:9000
INFO!("Use TCP socket: {}", socket_name);
let listener = TcpListener::bind(socket_name).unwrap();
for client in listener.incoming() {
let mut stream = client.unwrap();
2023-01-17 00:59:57 +01:00
let _ = thread::spawn(move || {
2023-01-16 01:03:00 +01:00
handle_request(&mut stream);
});
}
}
} else {
// if there is no socket configuration, assume that the stdio is already mapped
// to a socket. This is usually done by by the parent process (e.g. webserver) that launches efcgi
2023-01-26 02:22:05 +01:00
INFO!("No socket specified! use stdin as listening socket");
2023-01-16 01:03:00 +01:00
let stdin = std::io::stdin();
2023-01-17 00:59:57 +01:00
let fd = stdin.as_raw_fd();
if is_unix_socket(fd).unwrap() {
2023-01-26 02:22:05 +01:00
DEBUG!("Stdin is used as Unix domain socket");
2023-01-17 00:59:57 +01:00
let listener = unsafe { UnixListener::from_raw_fd(stdin.as_raw_fd()) };
for client in listener.incoming() {
let mut stream = client.unwrap();
2023-01-16 01:03:00 +01:00
2023-01-17 00:59:57 +01:00
let _ = thread::spawn(move || {
handle_request(&mut stream);
});
}
} else {
2023-01-26 02:22:05 +01:00
DEBUG!("Stdin is used as TCP Socket");
2023-01-17 00:59:57 +01:00
let listener = unsafe { TcpListener::from_raw_fd(stdin.as_raw_fd()) };
for client in listener.incoming() {
let mut stream = client.unwrap();
let _ = thread::spawn(move || {
handle_request(&mut stream);
});
}
2023-01-16 01:03:00 +01:00
}
}
}
#[derive(serde::Deserialize, Debug)]
struct Config {
socket: Option<String>,
pidfile: Option<String>,
user: Option<String>,
group: Option<String>,
2023-01-17 00:59:57 +01:00
debug: bool,
2023-01-16 01:03:00 +01:00
}
/// Main application entry
///
/// Run a `fastCGI` server
fn main() {
2023-01-17 00:59:57 +01:00
on_exit(clean_up);
2023-01-16 01:03:00 +01:00
let _log = LOG::init_log();
let matches = clap::App::new(DAEMON_NAME)
.author(APP_AUTHOR)
2023-01-17 00:59:57 +01:00
.about("Lua FastCGI daemon")
2023-01-16 01:03:00 +01:00
.version(APP_VERSION)
.arg(
clap::Arg::with_name("file")
.short("f")
.long("file")
.value_name("FILE")
.help("Configuration file")
.required(false)
.takes_value(true),
)
.get_matches();
2023-01-17 00:59:57 +01:00
let mut config = Config {
socket: None,
pidfile: None,
user: None,
group: None,
debug: false,
};
2023-01-16 01:03:00 +01:00
match matches.value_of("file") {
Some(path) => {
INFO!("Configuration file: {}", path);
let contents = std::fs::read_to_string(path).unwrap();
2023-01-17 00:59:57 +01:00
config = toml::from_str(&contents).unwrap();
if config.debug {
std::env::set_var("luad_debug", "true");
}
// drop user privilege if only user and group available in
// the configuration file, otherwise ignore
privdrop(config.user.as_deref(), config.group.as_deref()).unwrap();
2023-01-16 01:03:00 +01:00
// write pid file
2023-01-17 00:59:57 +01:00
match &config.pidfile {
2023-01-16 01:03:00 +01:00
Some(pidfile) => {
let mut f = std::fs::File::create(&pidfile).unwrap();
write!(f, "{}", std::process::id()).unwrap();
2023-01-26 02:22:05 +01:00
DEBUG!("PID file created at {}", pidfile);
2023-01-17 00:59:57 +01:00
}
2023-01-16 01:03:00 +01:00
None => {}
}
}
2023-01-17 00:59:57 +01:00
None => {}
2023-01-16 01:03:00 +01:00
}
2023-01-17 00:59:57 +01:00
serve(&config);
2023-01-16 01:03:00 +01:00
}