1 // Copyright 2021-2022 Ian Jackson and contributors to Hippotat
2 // SPDX-License-Identifier: GPL-3.0-or-later
3 // There is NO WARRANTY.
5 use std::convert::TryInto;
6 use std::ffi::{c_int, CStr};
8 use std::os::unix::io::RawFd;
11 use std::thread::panicking;
18 use nix::sys::stat::*;
19 use nix::sys::signal::*;
21 use nix::sys::wait::*;
23 use hippotat::prelude as prelude;
26 pub struct Daemoniser {
27 drop_bomb: Option<()>,
28 intermediate_pid: Pid,
33 fn crashv(ms: &[IoSlice<'_>]) -> ! {
35 let _ = writev(2, ms);
40 macro_rules! crashv { { $( $m:expr ),* $(,)? } => {
47 let ms = ms.map(|m| IoSlice::new(m.as_bytes()));
53 macro_rules! cstr { { $b:tt } => {
54 CStr::from_bytes_with_nul($b)
55 .unwrap_or_else(|_| crashm("cstr not nul terminated?! bug!"))
58 fn crashm(m: &str) -> ! {
61 fn crashe(m: &str, e: Errno) -> ! {
62 crashv!(m, ": ", e.desc())
66 impl<T> nix::Result<T> {
67 fn context(self, m: &str) -> T {
70 Err(e) => crashe(m, e),
75 const ITOA_BUFL: usize = 12;
76 fn c_itoa(value: c_int, buf: &mut [u8; ITOA_BUFL]) -> &str {
78 *buf = [b'.'; ITOA_BUFL];
79 libc::snprintf({ let buf: *mut u8 = buf.as_mut_ptr(); buf as *mut i8 },
81 cstr!(b"%x\0").as_ptr(),
84 let s = buf.splitn(2, |&c| c == b'\0').next()
85 .unwrap_or_else(|| crashm("splitn no next"));
86 str::from_utf8(s).unwrap_or_else(|_| crashm("non-utf-8 from snprintf!"))
89 unsafe fn mdup2(oldfd: RawFd, newfd: RawFd, what: &str) {
90 match dup2(oldfd, newfd) {
91 Ok(got) if got == newfd => { },
92 Ok(_) => crashm("dup2 gave wrong return value"),
93 Err(e) => crashv!("dup2 ", what, ": ", e.desc()),
97 unsafe fn write_status(st_wfd: RawFd, estatus: u8) {
98 match write(st_wfd, slice::from_ref(&estatus)) {
100 Ok(_) => crashm("write child startup exit status: short write"),
101 Err(e) => crashe("write child startup exit status", e),
105 unsafe fn parent(st_rfd: RawFd) -> ! {
106 let mut exitstatus = 0u8;
108 match read(st_rfd, slice::from_mut(&mut exitstatus)) {
109 Ok(0) => crashm("startup/daemonisation failed"),
110 Ok(1) => libc::_exit(exitstatus.into()),
111 Ok(_) => crashm("read startup: excess read!"),
112 Err(e) if e == Errno::EINTR => continue,
113 Err(e) => crashe("read startup signal pipe", e),
118 unsafe fn intermediate(child: Pid, st_wfd: RawFd) -> ! {
119 let mut wstatus: c_int = 0;
121 let r = libc::waitpid(child.as_raw(), &mut wstatus, 0);
122 if r == -1 { crashe("await child startup status",
123 nix::errno::from_i32(errno())) }
124 if r != child.as_raw() { crashm("await child startup status: wrong pid") }
126 let cooked = WaitStatus::from_raw(child, wstatus)
127 .context("await child startup status: convert wait status");
129 WaitStatus::Exited(_, estatus) => {
130 let estatus: u8 = estatus.try_into()
131 .unwrap_or_else(|_| crashm(
132 "await child startup status: exit status out of range!"));
133 write_status(st_wfd, estatus);
137 WaitStatus::Signaled(_, signal, coredump) => {
138 crashv!("startup failed: died due to signal: ", signal.as_str(),
139 if coredump { " (core dumped)" } else { "" });
143 crashv!("child startup exit status was strange! 0x",
144 c_itoa(wstatus, &mut default()))
150 /// Start daemonising - call before any threads created!
151 pub fn phase1() -> Self {
153 let null_fd = open(cstr!(b"/dev/null\0"), OFlag::O_RDWR, Mode::empty())
154 .context("open /dev/null");
155 mdup2(null_fd, 0, "null onto stdin");
157 let (st_rfd, st_wfd) = pipe().context("pipe");
159 match fork().context("fork (1)") {
160 ForkResult::Child => { }
161 ForkResult::Parent { child: _ } => {
162 close(st_wfd).context("close st_wfd pipe");
167 close(st_rfd).context("close st_rfd pipe");
168 setsid().context("setsid");
169 let intermediate_pid = Pid::this();
171 match fork().context("fork (2)") {
172 ForkResult::Child => { }
173 ForkResult::Parent { child } => {
174 intermediate(child, st_wfd)
187 pub fn complete(mut self) {
189 mdup2(self.null_fd, 1, "null over stdin");
191 if Pid::parent() != self.intermediate_pid {
193 "startup complete, but our parent is no longer the intermediate?");
195 kill(self.intermediate_pid, Some(Signal::SIGKILL))
196 .context("kill intermediate (after startup complete)");
198 write_status(self.st_wfd, 0);
199 mdup2(self.null_fd, 2, "null over stderrr");
201 self.drop_bomb.take();
206 impl Drop for Daemoniser {
208 if let Some(()) = self.drop_bomb.take() {
210 // We will crash in due course, having printed some messages
211 // to stderr, presumably.
214 panic!("Daemonizer object dropped unexpectedly, startup failed");