mirror of
https://forgejo.ellis.link/continuwuation/continuwuity.git
synced 2026-11-26 10:27:32 +00:00
refactor(core): Drop hot reload support
This was unused and a hotbed of potential issues
This commit is contained in:
27 files changed
+11
-871
No files matched your search
@@ -20,9 +20,6 @@ crate-type = [
|
||||
brotli_compression = [
|
||||
"reqwest/brotli",
|
||||
]
|
||||
conduwuit_mods = [
|
||||
"dep:libloading"
|
||||
]
|
||||
gzip_compression = [
|
||||
"reqwest/gzip",
|
||||
]
|
||||
@@ -85,8 +82,6 @@ http-body-util.workspace = true
|
||||
http.workspace = true
|
||||
ipaddress.workspace = true
|
||||
libc.workspace = true
|
||||
libloading.workspace = true
|
||||
libloading.optional = true
|
||||
lettre.workspace = true
|
||||
num-traits.workspace = true
|
||||
rand.workspace = true
|
||||
|
||||
@@ -8,19 +8,6 @@ pub mod info;
|
||||
pub mod log;
|
||||
pub mod matrix;
|
||||
pub mod metrics;
|
||||
pub mod mods;
|
||||
|
||||
#[cfg(any(not(conduwuit_mods), not(feature = "conduwuit_mods")))]
|
||||
pub mod mods {
|
||||
#[macro_export]
|
||||
macro_rules! mod_ctor {
|
||||
() => {};
|
||||
}
|
||||
#[macro_export]
|
||||
macro_rules! mod_dtor {
|
||||
() => {};
|
||||
}
|
||||
}
|
||||
pub mod server;
|
||||
pub mod utils;
|
||||
|
||||
|
||||
@@ -1,28 +0,0 @@
|
||||
use std::sync::atomic::{AtomicI32, Ordering};
|
||||
|
||||
const ORDERING: Ordering = Ordering::Relaxed;
|
||||
static STATIC_DTORS: AtomicI32 = AtomicI32::new(0);
|
||||
|
||||
/// Called by Module::unload() to indicate module is about to be unloaded and
|
||||
/// static destruction is intended. This will allow verifying it actually took
|
||||
/// place.
|
||||
pub(crate) fn prepare() {
|
||||
let count = STATIC_DTORS.fetch_sub(1, ORDERING);
|
||||
debug_assert!(count <= 0, "STATIC_DTORS should not be greater than zero.");
|
||||
}
|
||||
|
||||
/// Called by static destructor of a module. This call should only be found
|
||||
/// inside a mod_fini! macro. Do not call from anywhere else.
|
||||
#[inline(always)]
|
||||
pub fn report() { let _count = STATIC_DTORS.fetch_add(1, ORDERING); }
|
||||
|
||||
/// Called by Module::unload() (see check()) with action in case a check()
|
||||
/// failed. This can allow a stuck module to be noted while allowing for other
|
||||
/// independent modules to be diagnosed.
|
||||
pub(crate) fn check_and_reset() -> bool { STATIC_DTORS.swap(0, ORDERING) == 0 }
|
||||
|
||||
/// Called by Module::unload() after unload to verify static destruction took
|
||||
/// place. A call to prepare() must be made prior to Module::unload() and making
|
||||
/// this call.
|
||||
#[allow(dead_code)]
|
||||
pub(crate) fn check() -> bool { STATIC_DTORS.load(ORDERING) == 0 }
|
||||
@@ -1,70 +0,0 @@
|
||||
#[macro_export]
|
||||
macro_rules! mod_ctor {
|
||||
( $($body:block)? ) => {
|
||||
$crate::mod_init! {{
|
||||
$crate::debug_info!("Module loaded");
|
||||
$($body)?
|
||||
}}
|
||||
}
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! mod_dtor {
|
||||
( $($body:block)? ) => {
|
||||
$crate::mod_fini! {{
|
||||
$crate::debug_info!("Module unloading");
|
||||
$($body)?
|
||||
$crate::mods::canary::report();
|
||||
}}
|
||||
}
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! mod_init {
|
||||
($body:block) => {
|
||||
#[cfg(not(all(target_family = "unix", not(target_vendor = "apple"))))]
|
||||
compile_error!("conduwuit_mods (mod_init!) requires an ELF Unix target");
|
||||
|
||||
#[used]
|
||||
// SAFETY: `.init_array` is the standard ELF constructor pointer table;
|
||||
// the dynamic loader invokes `_mod_init` before `main()` or on `dlopen`.
|
||||
#[cfg_attr(
|
||||
all(target_family = "unix", not(target_vendor = "apple")),
|
||||
unsafe(link_section = ".init_array")
|
||||
)]
|
||||
static MOD_INIT: unsafe extern "C" fn() = { _mod_init };
|
||||
|
||||
// SAFETY: `.text.startup` is the standard ELF text subsection for
|
||||
// initialization functions.
|
||||
#[cfg_attr(
|
||||
all(target_family = "unix", not(target_vendor = "apple")),
|
||||
unsafe(link_section = ".text.startup")
|
||||
)]
|
||||
unsafe extern "C" fn _mod_init() -> () $body
|
||||
};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! mod_fini {
|
||||
($body:block) => {
|
||||
#[cfg(not(all(target_family = "unix", not(target_vendor = "apple"))))]
|
||||
compile_error!("conduwuit_mods (mod_fini!) requires an ELF Unix target");
|
||||
|
||||
#[used]
|
||||
// SAFETY: `.fini_array` is the standard ELF destructor pointer table;
|
||||
// the dynamic loader invokes `_mod_fini` on `dlclose` or process teardown.
|
||||
#[cfg_attr(
|
||||
all(target_family = "unix", not(target_vendor = "apple")),
|
||||
unsafe(link_section = ".fini_array")
|
||||
)]
|
||||
static MOD_FINI: unsafe extern "C" fn() = { _mod_fini };
|
||||
|
||||
// SAFETY: `.text.exit` / `.text.startup` is a standard ELF text
|
||||
// subsection for teardown/initialization functions.
|
||||
#[cfg_attr(
|
||||
all(target_family = "unix", not(target_vendor = "apple")),
|
||||
unsafe(link_section = ".text.startup")
|
||||
)]
|
||||
unsafe extern "C" fn _mod_fini() -> () $body
|
||||
};
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
#![cfg(all(conduwuit_mods, feature = "conduwuit_mods"))]
|
||||
|
||||
pub(crate) use libloading::os::unix::{Library, Symbol};
|
||||
|
||||
pub mod canary;
|
||||
pub mod macros;
|
||||
pub mod module;
|
||||
pub mod new;
|
||||
pub mod path;
|
||||
|
||||
pub use module::Module;
|
||||
@@ -1,93 +0,0 @@
|
||||
use std::{
|
||||
ffi::{CString, OsString},
|
||||
time::SystemTime,
|
||||
};
|
||||
|
||||
use super::{Library, Symbol, canary, new, path};
|
||||
use crate::{Result, error};
|
||||
|
||||
pub struct Module {
|
||||
handle: Option<Library>,
|
||||
loaded: SystemTime,
|
||||
path: OsString,
|
||||
}
|
||||
|
||||
impl Module {
|
||||
/// # Safety
|
||||
/// See [`new::from_name`]: loads a shared library via `dlopen(3)` and runs
|
||||
/// its initialization routines.
|
||||
pub unsafe fn from_name(name: &str) -> Result<Self> {
|
||||
// SAFETY: Upheld by caller.
|
||||
unsafe { Self::from_path(path::from_name(name)?) }
|
||||
}
|
||||
|
||||
/// # Safety
|
||||
/// See [`new::from_path`]: loads a shared library via `dlopen(3)` and runs
|
||||
/// its initialization routines.
|
||||
pub unsafe fn from_path(path: OsString) -> Result<Self> {
|
||||
Ok(Self {
|
||||
// SAFETY: Upheld by caller.
|
||||
handle: Some(unsafe { new::from_path(&path)? }),
|
||||
loaded: SystemTime::now(),
|
||||
path,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn unload(&mut self) {
|
||||
canary::prepare();
|
||||
self.close();
|
||||
if !canary::check_and_reset() {
|
||||
let name = self.name().expect("Module is named");
|
||||
error!("Module {name:?} is stuck and failed to unload.");
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn close(&mut self) {
|
||||
if let Some(handle) = self.handle.take() {
|
||||
handle.close().expect("Module handle closed");
|
||||
}
|
||||
}
|
||||
|
||||
/// Look up a symbol in this module via `dlsym(3)`.
|
||||
///
|
||||
/// # Safety
|
||||
/// `Prototype` is not checked against the symbol's actual type or ABI in
|
||||
/// the loaded shared library. The caller must ensure `name` refers to a
|
||||
/// symbol whose type, calling convention, and ABI exactly match
|
||||
/// `Prototype`.
|
||||
pub unsafe fn get<Prototype>(&self, name: &str) -> Result<Symbol<'_, Prototype>> {
|
||||
let cname = CString::new(name.to_owned()).expect("terminated string from provided name");
|
||||
let handle = self
|
||||
.handle
|
||||
.as_ref()
|
||||
.expect("backing library loaded by this instance");
|
||||
|
||||
// SAFETY: Caller guarantees `Prototype` matches the symbol's actual
|
||||
// type and ABI. Passing `as_bytes_with_nul()` avoids a redundant copy
|
||||
// inside `libloading`.
|
||||
let sym = unsafe { handle.get::<Prototype>(cname.as_bytes_with_nul()) };
|
||||
let sym = sym.expect("symbol found; binding successful");
|
||||
|
||||
Ok(sym)
|
||||
}
|
||||
|
||||
pub fn deleted(&self) -> Result<bool> {
|
||||
let mtime = path::mtime(self.path())?;
|
||||
let res = mtime > self.loaded;
|
||||
|
||||
Ok(res)
|
||||
}
|
||||
|
||||
pub fn name(&self) -> Result<String> { path::to_name(self.path()) }
|
||||
|
||||
#[must_use]
|
||||
pub fn path(&self) -> &OsString { &self.path }
|
||||
}
|
||||
|
||||
impl Drop for Module {
|
||||
fn drop(&mut self) {
|
||||
if self.handle.is_some() {
|
||||
self.unload();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
use std::ffi::OsStr;
|
||||
|
||||
use super::{Library, path};
|
||||
use crate::{Err, Result};
|
||||
|
||||
const OPEN_FLAGS: i32 = libloading::os::unix::RTLD_LAZY | libloading::os::unix::RTLD_GLOBAL;
|
||||
|
||||
/// # Safety
|
||||
/// Loads a shared library via `dlopen(3)`, which executes the library's
|
||||
/// initialization routines (`.init_array` / constructors). The caller must
|
||||
/// ensure the resolved module is trusted and ABI-compatible with this binary.
|
||||
pub unsafe fn from_name(name: &str) -> Result<Library> {
|
||||
let path = path::from_name(name)?;
|
||||
// SAFETY: Upheld by caller.
|
||||
unsafe { from_path(&path) }
|
||||
}
|
||||
|
||||
/// # Safety
|
||||
/// Loads a shared library via `dlopen(3)`, which executes the library's
|
||||
/// initialization routines (`.init_array` / constructors). The caller must
|
||||
/// ensure `path` points to a trusted, ABI-compatible shared library.
|
||||
pub unsafe fn from_path(path: &OsStr) -> Result<Library> {
|
||||
// SAFETY: Caller guarantees `path` is a trusted, ABI-compatible library.
|
||||
let lib = unsafe { Library::open(Some(path), OPEN_FLAGS) };
|
||||
if let Err(e) = lib {
|
||||
let name = path::to_name(path)?;
|
||||
return Err!("Loading module {name:?} failed: {e}");
|
||||
}
|
||||
|
||||
Ok(lib.expect("module loaded"))
|
||||
}
|
||||
@@ -1,41 +0,0 @@
|
||||
use std::{
|
||||
env::current_exe,
|
||||
ffi::{OsStr, OsString},
|
||||
path::{Path, PathBuf},
|
||||
time::SystemTime,
|
||||
};
|
||||
|
||||
use libloading::library_filename;
|
||||
|
||||
use crate::Result;
|
||||
|
||||
pub fn from_name(name: &str) -> Result<OsString> {
|
||||
let root = PathBuf::new();
|
||||
let exe_path = current_exe()?;
|
||||
let exe_dir = exe_path.parent().unwrap_or(&root);
|
||||
let mut mod_path = exe_dir.to_path_buf();
|
||||
let mod_file = library_filename(name);
|
||||
mod_path.push(mod_file);
|
||||
|
||||
Ok(mod_path.into_os_string())
|
||||
}
|
||||
|
||||
pub fn to_name(path: &OsStr) -> Result<String> {
|
||||
let path = Path::new(path);
|
||||
let name = path
|
||||
.file_stem()
|
||||
.expect("path file stem")
|
||||
.to_str()
|
||||
.expect("name string");
|
||||
|
||||
let name = name.strip_prefix("lib").unwrap_or(name).to_owned();
|
||||
|
||||
Ok(name)
|
||||
}
|
||||
|
||||
pub fn mtime(path: &OsStr) -> Result<SystemTime> {
|
||||
let meta = std::fs::metadata(path)?;
|
||||
let mtime = meta.modified()?;
|
||||
|
||||
Ok(mtime)
|
||||
}
|
||||
@@ -27,10 +27,6 @@ pub struct Server {
|
||||
/// observable used on shutdown and should not be modified.
|
||||
pub stopping: AtomicBool,
|
||||
|
||||
/// Reload/shutdown desired indicator; when false, shutdown is desired. This
|
||||
/// is an observable used on shutdown and modifying is not recommended.
|
||||
pub reloading: AtomicBool,
|
||||
|
||||
/// Restart desired; when true, restart it desired after shutdown.
|
||||
pub restarting: AtomicBool,
|
||||
|
||||
@@ -60,7 +56,6 @@ impl Server {
|
||||
config: config::Manager::new(config),
|
||||
started: SystemTime::now(),
|
||||
stopping: AtomicBool::new(false),
|
||||
reloading: AtomicBool::new(false),
|
||||
restarting: AtomicBool::new(false),
|
||||
runtime,
|
||||
signal: broadcast::channel::<&'static str>(1).0,
|
||||
@@ -69,25 +64,6 @@ impl Server {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn reload(&self) -> Result<()> {
|
||||
if cfg!(any(not(conduwuit_mods), not(feature = "conduwuit_mods"))) {
|
||||
return Err!("Reloading not enabled");
|
||||
}
|
||||
|
||||
if self.reloading.swap(true, Ordering::AcqRel) {
|
||||
return Err!("Reloading already in progress");
|
||||
}
|
||||
|
||||
if self.stopping.swap(true, Ordering::AcqRel) {
|
||||
return Err!("Shutdown already in progress");
|
||||
}
|
||||
|
||||
self.signal("SIGINT").inspect_err(|_| {
|
||||
self.stopping.store(false, Ordering::Release);
|
||||
self.reloading.store(false, Ordering::Release);
|
||||
})
|
||||
}
|
||||
|
||||
pub fn restart(&self) -> Result {
|
||||
if self.restarting.swap(true, Ordering::AcqRel) {
|
||||
return Err!("Restart already in progress");
|
||||
@@ -146,9 +122,6 @@ impl Server {
|
||||
#[inline]
|
||||
pub fn is_stopping(&self) -> bool { self.stopping.load(Ordering::Relaxed) }
|
||||
|
||||
#[inline]
|
||||
pub fn is_reloading(&self) -> bool { self.reloading.load(Ordering::Relaxed) }
|
||||
|
||||
#[inline]
|
||||
pub fn is_restarting(&self) -> bool { self.restarting.load(Ordering::Relaxed) }
|
||||
|
||||
|
||||
Reference in new issue
Block a user