Pausa: 11 crates del fractal Ente #0 con cerebro completo
PID 1 boot + bus interno autenticado + cerebro KCL/Rust: - 6 lib crates de infra (card, bus, cas, kernel, soma, wasm, snapshot) - ente-brain: motor de reglas O(1), observer Shannon, cristalización, audit hash-chain, persistencia rules.k, Prometheus /metrics - KCL schemas card.k + rule.k como gramática autoritativa - compat-logind D-Bus, ente-echo demo provider, ente-zero PID 1 - 22 tests OK, ~3.8k LOC Rust + ~300 LOC KCL Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
//! SIGCHLD vía signalfd, no signal handler.
|
||||
//!
|
||||
//! Los handlers async-signal sólo pueden invocar funciones async-signal-safe
|
||||
//! — no allocator, no `mpsc::send`. Con signalfd la señal entra al runtime de
|
||||
//! Tokio como un `fd` legible y la cosechamos en el bucle como cualquier otro
|
||||
//! evento. Esto es lo que hace que un init en Rust moderno sea sano.
|
||||
|
||||
use anyhow::Context;
|
||||
use nix::sys::signal::Signal;
|
||||
use nix::sys::signalfd::{SfdFlags, SigSet, SignalFd};
|
||||
use std::os::fd::AsRawFd;
|
||||
use tokio::io::unix::AsyncFd;
|
||||
use tokio::sync::mpsc;
|
||||
use tracing::{error, trace};
|
||||
|
||||
/// Bloquea SIGCHLD para entrega asíncrona, abre signalfd, y emite un `()`
|
||||
/// en el canal cada vez que llega al menos una señal.
|
||||
pub fn spawn_sigchld_stream() -> anyhow::Result<mpsc::Receiver<()>> {
|
||||
let mut mask = SigSet::empty();
|
||||
mask.add(Signal::SIGCHLD);
|
||||
mask.thread_block().context("SIGCHLD thread_block")?;
|
||||
|
||||
let sfd = SignalFd::with_flags(&mask, SfdFlags::SFD_NONBLOCK | SfdFlags::SFD_CLOEXEC)
|
||||
.context("signalfd creation")?;
|
||||
|
||||
let async_fd = AsyncFd::new(SignalFdHandle(sfd)).context("AsyncFd::new")?;
|
||||
|
||||
let (tx, rx) = mpsc::channel(8);
|
||||
tokio::spawn(async move {
|
||||
loop {
|
||||
let mut guard = match async_fd.readable().await {
|
||||
Ok(g) => g,
|
||||
Err(e) => { error!(?e, "signalfd readable failed"); return; }
|
||||
};
|
||||
// Drenamos todas las siginfos pendientes; signalfd las coalesce
|
||||
// pero no las cuenta — un read por evento legible es suficiente.
|
||||
drain(guard.get_inner());
|
||||
guard.clear_ready();
|
||||
if tx.send(()).await.is_err() { return; }
|
||||
trace!("SIGCHLD batch coalesced");
|
||||
}
|
||||
});
|
||||
|
||||
Ok(rx)
|
||||
}
|
||||
|
||||
struct SignalFdHandle(SignalFd);
|
||||
|
||||
impl AsRawFd for SignalFdHandle {
|
||||
fn as_raw_fd(&self) -> std::os::fd::RawFd {
|
||||
self.0.as_raw_fd()
|
||||
}
|
||||
}
|
||||
|
||||
fn drain(handle: &SignalFdHandle) {
|
||||
let fd = handle.as_raw_fd();
|
||||
// Tamaño exacto de signalfd_siginfo. Leemos en bucle hasta EAGAIN.
|
||||
let mut buf = [0u8; std::mem::size_of::<libc::signalfd_siginfo>()];
|
||||
loop {
|
||||
let n = unsafe {
|
||||
libc::read(fd, buf.as_mut_ptr() as *mut _, buf.len())
|
||||
};
|
||||
if n < 0 { return; }
|
||||
if n == 0 { return; }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user