diff options
author | Quentin Dufour <quentin@deuxfleurs.fr> | 2023-12-14 15:36:54 +0100 |
---|---|---|
committer | Quentin Dufour <quentin@deuxfleurs.fr> | 2023-12-14 15:36:54 +0100 |
commit | 1b5f2eb695d658c57ba9c4264e76ca13bd82a958 (patch) | |
tree | 0d0be5a64b791fe6673a05aa2a61762aa208ab4d /src | |
parent | 1f6e64d34e44b8b7bc7247af38bccf3ade86cf0b (diff) | |
download | aerogramme-1b5f2eb695d658c57ba9c4264e76ca13bd82a958.tar.gz aerogramme-1b5f2eb695d658c57ba9c4264e76ca13bd82a958.zip |
implement the reload feature
Diffstat (limited to 'src')
-rw-r--r-- | src/config.rs | 4 | ||||
-rw-r--r-- | src/main.rs | 33 | ||||
-rw-r--r-- | src/server.rs | 24 |
3 files changed, 48 insertions, 13 deletions
diff --git a/src/config.rs b/src/config.rs index e50cd68..1438910 100644 --- a/src/config.rs +++ b/src/config.rs @@ -8,7 +8,7 @@ use serde::{Deserialize, Serialize}; #[derive(Serialize, Deserialize, Debug, Clone)] pub struct CompanionConfig { - pub pid: Option<String>, + pub pid: Option<PathBuf>, pub imap: ImapConfig, #[serde(flatten)] @@ -17,7 +17,7 @@ pub struct CompanionConfig { #[derive(Serialize, Deserialize, Debug, Clone)] pub struct ProviderConfig { - pub pid: Option<String>, + pub pid: Option<PathBuf>, pub imap: ImapConfig, pub lmtp: LmtpConfig, pub users: UserManagement, diff --git a/src/main.rs b/src/main.rs index 02ba5e4..f08f1a3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -13,9 +13,11 @@ mod server; mod storage; use std::path::PathBuf; +use std::io::Read; use anyhow::{bail, Result, Context}; use clap::{Parser, Subcommand}; +use nix::{unistd::Pid, sys::signal}; use config::*; use server::Server; @@ -92,7 +94,7 @@ enum CompanionCommand { Daemon, Reload { #[clap(short, long, env = "AEROGRAMME_PID")] - pid: Option<u64>, + pid: Option<i32>, }, Wizard, #[clap(subcommand)] @@ -104,7 +106,10 @@ enum ProviderCommand { /// Runs the IMAP+LMTP server daemon Daemon, /// Reload the daemon - Reload, + Reload { + #[clap(short, long, env = "AEROGRAMME_PID")] + pid: Option<i32>, + }, /// Manage static accounts #[clap(subcommand)] Account(AccountManagement), @@ -161,9 +166,7 @@ async fn main() -> Result<()> { let server = Server::from_companion_config(config).await?; server.run().await?; }, - CompanionCommand::Reload { pid: _pid } => { - unimplemented!(); - }, + CompanionCommand::Reload { pid } => reload(*pid, config.pid)?, CompanionCommand::Wizard => { unimplemented!(); }, @@ -177,9 +180,7 @@ async fn main() -> Result<()> { let server = Server::from_provider_config(config).await?; server.run().await?; }, - ProviderCommand::Reload => { - unimplemented!(); - }, + ProviderCommand::Reload { pid } => reload(*pid, config.pid)?, ProviderCommand::Account(cmd) => { let user_file = match config.users { UserManagement::Static(conf) => conf.user_list, @@ -260,6 +261,22 @@ async fn main() -> Result<()> { Ok(()) } +fn reload(pid: Option<i32>, pid_path: Option<PathBuf>) -> Result<()> { + let final_pid = match (pid, pid_path) { + (Some(pid), _) => pid, + (_, Some(path)) => { + let mut f = std::fs::OpenOptions::new().read(true).open(path)?; + let mut pidstr = String::new(); + f.read_to_string(&mut pidstr)?; + pidstr.parse::<i32>()? + }, + _ => bail!("Unable to infer your daemon's PID"), + }; + let pid = Pid::from_raw(final_pid); + signal::kill(pid, signal::Signal::SIGUSR1)?; + Ok(()) +} + fn account_management(root: &Command, cmd: &AccountManagement, users: PathBuf) -> Result<()> { let mut ulist: UserList = read_config(users.clone()).context(format!("'{:?}' must be a user database", users))?; diff --git a/src/server.rs b/src/server.rs index 8abdb86..552a0e6 100644 --- a/src/server.rs +++ b/src/server.rs @@ -1,4 +1,6 @@ use std::sync::Arc; +use std::path::PathBuf; +use std::io::Write; use anyhow::Result; use futures::try_join; @@ -14,18 +16,21 @@ use crate::login::{ldap_provider::*, static_provider::*}; pub struct Server { lmtp_server: Option<Arc<LmtpServer>>, imap_server: Option<imap::Server>, + pid_file: Option<PathBuf>, } impl Server { pub async fn from_companion_config(config: CompanionConfig) -> Result<Self> { + tracing::info!("Init as companion"); let login = Arc::new(StaticLoginProvider::new(config.users).await?); let lmtp_server = None; let imap_server = Some(imap::new(config.imap, login.clone()).await?); - Ok(Self { lmtp_server, imap_server }) + Ok(Self { lmtp_server, imap_server, pid_file: config.pid }) } pub async fn from_provider_config(config: ProviderConfig) -> Result<Self> { + tracing::info!("Init as provider"); let login: ArcLoginProvider = match config.users { UserManagement::Static(x) => Arc::new(StaticLoginProvider::new(x).await?), UserManagement::Ldap(x) => Arc::new(LdapLoginProvider::new(x)?), @@ -34,11 +39,24 @@ impl Server { let lmtp_server = Some(LmtpServer::new(config.lmtp, login.clone())); let imap_server = Some(imap::new(config.imap, login.clone()).await?); - Ok(Self { lmtp_server, imap_server }) + Ok(Self { lmtp_server, imap_server, pid_file: config.pid }) } pub async fn run(self) -> Result<()> { - tracing::info!("Starting Aerogramme..."); + let pid = std::process::id(); + tracing::info!(pid=pid, "Starting main loops"); + + // write the pid file + if let Some(pid_file) = self.pid_file { + let mut file = std::fs::OpenOptions::new() + .write(true) + .create(true) + .truncate(true) + .open(pid_file)?; + file.write_all(pid.to_string().as_bytes())?; + drop(file); + } + let (exit_signal, provoke_exit) = watch_ctrl_c(); let _exit_on_err = move |err: anyhow::Error| { |