oxedyne/fe2o3/fe2o3_core/src/bin/stop_signal_child.rs
1.6 KiB, 1 run
created by r1870400018:65716, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | //! A tiny process whose only job is to be sent a real signal and say which one. |
| 2 | //! |
| 3 | //! `tests/stop_signal.rs` is the only caller: it spawns this binary, sends it |
| 4 | //! `SIGINT`, `SIGTERM` or `SIGHUP` with the operating system's own `kill`, and |
| 5 | //! reads back which [`Stop`](oxedyne_fe2o3_core::stop::Stop) variant the |
| 6 | //! listener told it about. A signal cannot be sent to a running test binary and |
| 7 | //! still leave that binary able to keep testing, so the process under signal has |
| 8 | //! to be a child, and this is that child. |
| 9 | //! |
| 10 | //! [Written with AI entirely](https://need2know.ai/entirely-ai/code)\ |
| 11 | //! Anthropic Claude |
| 12 | |
| 13 | #![forbid(unsafe_code)] |
| 14 | |
| 15 | #[cfg(unix)] |
| 16 | fn main() -> oxedyne_fe2o3_core::prelude::Outcome<()> { |
| 17 | use oxedyne_fe2o3_core::prelude::*; |
| 18 | use oxedyne_fe2o3_core::stop::{ |
| 19 | on_stop_request, |
| 20 | Stop, |
| 21 | }; |
| 22 | use std::io::Write; |
| 23 | |
| 24 | res!(on_stop_request(|which: Stop| { |
| 25 | let word = match which { |
| 26 | Stop::Interrupt => "Interrupt", |
| 27 | Stop::Terminate => "Terminate", |
| 28 | Stop::Hangup => "Hangup", |
| 29 | }; |
| 30 | // One line, flushed before exit: the parent reads this from a pipe and |
| 31 | // a buffered line still sitting in libc's stdio at `exit` would be |
| 32 | // lost. |
| 33 | println!("{}", word); |
| 34 | let _ = std::io::stdout().flush(); |
| 35 | std::process::exit(0); |
| 36 | })); |
| 37 | |
| 38 | // Nothing to do until asked. The parent decides how long to wait before |
| 39 | // giving up on it. |
| 40 | loop { |
| 41 | std::thread::sleep(std::time::Duration::from_millis(50)); |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | /// Signals are a unix concept; nothing here would be exercised elsewhere. |
| 46 | #[cfg(not(unix))] |
| 47 | fn main() {} |