1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
#![deny(warnings, missing_docs, clippy::pedantic)]
#![warn(rust_2018_idioms)]
#![cfg(target_family = "unix")]

//! Yet Another Daemonizer is a daemonizing crate to easily, simply, and **correctly** create legacy
//! daemons.
//!
//! This crate focuses on manually creating a background process which is not managed by a
//! supervisor such as systemd or launchd. It strives to follow all the best practices
//! to correctly daemonize a process.
//!
//! # Example
//! ```no_run
//! match yad::with_options()
//!     .stdin(yad::options::Stdio::Null)
//!     .stderr(yad::options::Stdio::Null)
//!     .stdout(yad::options::Stdio::output("/var/log/daemon.log"))
//!     .daemonize()
//! {
//!     Ok(_) => println!("I'm a daemon"),
//!     Err(err) => eprintln!("Failed to lauch daemon: {}", err),
//! }
//! ```
//!
//! # References
//! * [Man page for daemon()](https://man7.org/linux/man-pages/man7/daemon.7.html)
//! * [Reference project in C](https://chaoticlab.io/c/c++/unix/2018/10/01/daemonize.html)

pub mod options;

type InvocationResult<T = ()> = Result<T, Error>;
type DaemonResult<T = ()> = Result<T, (DaemonError, nix::Error)>;

/// Errors that can happen while daemonizing.
///
/// These errors are received in the invoking process, i.e. the proccess that called `daemonize()`.
#[derive(thiserror::Error, Debug, Copy, Clone, Eq, PartialEq)]
pub enum Error {
    /// Daemon pid file already exists
    #[error("Daemon pid file already exists")]
    DaemonAlreadyRunning,

    /// Failed to close file descriptors
    #[error("Failed to close file descriptors: {0}")]
    CloseDescriptors(nix::Error),

    /// Failed to fetch open file descriptors
    #[error("Failed to fetch open file descriptors: {0}")]
    ListOpenDescriptors(nix::Error),

    /// Failed to reset signal handlers
    #[error("Failed to reset signal handlers: {0}")]
    ResetSignals(nix::Error),

    /// Failed to block signals
    #[error("Failed to block signals: {0}")]
    BlockSignals(nix::Error),

    /// Failed to create status reporting pipe
    #[error("Failed to create status reporting pipe: {0}")]
    CreatePipe(nix::Error),

    /// Failed to fork daemon process
    #[error("Failed to fork daemon process: {0}")]
    Fork(nix::Error),

    /// Failed to receive daemon status report
    #[error("Failed to receive daemon status report: {0}")]
    ReadStatus(nix::Error),

    /// Failed to start daemon after forking
    #[error("Daemon failed to initialize: {error}: {cause}")]
    Daemon {
        /// The received error
        error: DaemonError,
        /// The raw underlying error
        cause: nix::Error,
    },

    /// The [`Heartbeat`](struct.Heartbeat.html) from the daemon reported an error
    #[error("Daemon sent failed heart beat: Status code: {status}")]
    Heartbeat {
        /// The status code reported back by the daemon
        status: i32,
    },
}

impl Error {
    fn daemon(error: DaemonError, cause: nix::Error) -> Self {
        Self::Daemon { error, cause }
    }
}

/// Errors that can happen after the daemon has forked.
///
/// The error is reported by another process through pipes and received by the invoking process,
/// i.e. the process that called `daemonize()`, will handle the error.
///
/// The forked process will be guaranteed to be stopped, unless the error is a
/// [`Heartbeat`](enum.DaemonError.html#variant.Heartbeat), in which case the forked process is
/// responsible for terminating after cleaning up.
#[derive(thiserror::Error, Debug, Copy, Clone, Eq, PartialEq)]
#[repr(u8)]
pub enum DaemonError {
    /// The [`Heartbeat`](struct.Heartbeat.html) from the daemon reported an error
    #[error("Failed heart beat")]
    Heartbeat = 1,

    /// Failure detaching from session
    #[error("Failed to detach from session")]
    Setsid = 2,

    /// Failure during the second call to `fork()`
    #[error("Failed to double fork daemon")]
    Fork = 3,

    /// Failure changing root directory
    #[error("Failed to change root directory")]
    ChangeRoot = 4,

    /// Failure setting UID
    #[error("Failed to set user")]
    SetUser = 5,

    /// Failure setting GID
    #[error("Failed to set group")]
    SetGroup = 6,

    /// Failure unblocking the signals
    #[error("Failed to unblock signals")]
    UnblockSignals = 7,

    /// Failure closing file descriptors
    #[error("Failed to close file descriptors")]
    CloseDescriptors = 8,

    /// Failure listing open file descriptors
    #[error("Failed to fetch open file descriptors")]
    ListOpenDescriptors = 9,

    /// Failure resetting signal handlers
    #[error("Failed to reset signal handlers")]
    ResetSignals = 10,

    /// Failure redirecting STDIN
    #[error("Failed to redirect stdin")]
    RedirectStdin = 11,

    /// Failure redirecting STDOUT
    #[error("Failed to redirect stdout")]
    RedirectStdout = 12,

    /// Failure redirecting STDERR
    #[error("Failed to redirect stderr")]
    RedirectStderr = 13,
}

#[derive(Debug)]
enum ForkResult {
    Invoker(Pipe, nix::unistd::Pid),
    Daemon(Pipe),
}

/// Starts the daemon with default options.
///
/// # Example
/// ```no_run
/// match yad::daemonize() {
///     Ok(_) => println!("I'm a daemon"),
///     Err(err) => eprintln!("Failed to lauch daemon: {}", err),
/// }
/// ```
///
/// # Errors
/// If the daemonizing operation fails.
///
/// The invoking process, i.e. the process that called `daemonize()`, will handle the
/// error. The forked process will be guaranteed to be stopped, unless the error is a
/// [`Heartbeat`](enum.DaemonError.html#variant.Heartbeat), in which case the forked process is
/// responsible for terminating after cleaning up.
pub fn daemonize() -> InvocationResult<Heartbeat> {
    daemonize_inner(options::Options::new())
}

/// Starts the daemon with the given options.
///
/// # Example
/// ```no_run
/// match yad::with_options()
///     .stdin(yad::options::Stdio::Null)
///     .stderr(yad::options::Stdio::Null)
///     .stdout(yad::options::Stdio::output("/var/log/daemon.log"))
///     .daemonize()
/// {
///     Ok(_) => println!("I'm a daemon"),
///     Err(err) => eprintln!("Failed to lauch daemon: {}", err),
/// }
/// ```
///
/// # Errors
/// If the daemonizing operation fails.
///
/// The invoking process, i.e. the process that called `daemonize()`, will handle the
/// error. The forked process will be guaranteed to be stopped, unless the error is a
/// [`Heartbeat`](enum.DaemonError.html#variant.Heartbeat), in which case the forked process is
/// responsible for terminating after cleaning up.
#[must_use]
pub fn with_options() -> options::Options {
    options::Options::new()
}

fn daemonize_inner(options: options::Options) -> InvocationResult<Heartbeat> {
    close_descriptors()?;
    reset_signals()?;
    block_signals()?;
    let pipe = Pipe::new()?;

    match fork(pipe)? {
        ForkResult::Invoker(pipe, child) => match finalize_invoker(pipe, child) {
            Ok(_) => std::process::exit(0),
            Err(err) => Err(err),
        },
        ForkResult::Daemon(pipe) => match finalize_daemon(options) {
            Ok(_) => Ok(Heartbeat(Some(pipe), false)),
            Err((error, cause)) => exit_error(pipe, error, cause),
        },
    }
}

fn close_descriptors() -> InvocationResult {
    // Allowed because of the filter_map flow
    #[allow(clippy::needless_pass_by_value)]
    fn file_to_fd(entry: std::fs::DirEntry) -> Option<i32> {
        entry
            .file_name()
            .to_str()
            .and_then(|name| name.parse().ok())
    }

    let fd_dir = std::path::PathBuf::from("/dev/fd/");

    match fd_dir.read_dir() {
        Ok(dir) => {
            for fd in dir
                .filter_map(Result::ok)
                .filter_map(file_to_fd)
                .filter(|fd| fd > &2)
            {
                nix::unistd::close(fd).map_err(Error::CloseDescriptors)?
            }
            Ok(())
        }
        Err(_) => Err(Error::ListOpenDescriptors(nix::errno::Errno::last().into())),
    }
}

fn reset_signals() -> InvocationResult {
    use nix::sys::signal as nix;

    // Allowed because it is just more readable
    #[allow(clippy::filter_map)]
    nix::Signal::iterator()
        .filter(|signal| signal != &nix::SIGKILL && signal != &nix::SIGSTOP)
        .map(|signal| unsafe { nix::signal(signal, nix::SigHandler::SigDfl) })
        .find_map(Result::err)
        .map_or(Ok(()), |err| Err(Error::ResetSignals(err)))
}

fn block_signals() -> InvocationResult {
    use nix::sys::signal as nix;
    let mask = nix::SigmaskHow::SIG_BLOCK;
    let sigset = nix::SigSet::all();
    nix::sigprocmask(mask, Some(&sigset), None).map_err(Error::BlockSignals)
}

fn unblock_signals() -> nix::Result<()> {
    use nix::sys::signal as nix;
    let mask = nix::SigmaskHow::SIG_UNBLOCK;
    let sigset = nix::SigSet::all();
    nix::sigprocmask(mask, Some(&sigset), None)
}

fn fork(pipe: Pipe) -> InvocationResult<ForkResult> {
    use nix::unistd;

    match unsafe { unistd::fork() } {
        Err(err) => Err(Error::Fork(err)),
        Ok(unistd::ForkResult::Parent { child }) => Ok(ForkResult::Invoker(pipe, child)),
        Ok(unistd::ForkResult::Child) => match unistd::setsid() {
            Err(err) => exit_error(pipe, DaemonError::Setsid, err),
            Ok(_) => match unsafe { nix::unistd::fork() } {
                Err(err) => exit_error(pipe, DaemonError::Fork, err),
                Ok(unistd::ForkResult::Parent { child: _ }) => exit_success(pipe),
                Ok(unistd::ForkResult::Child) => Ok(ForkResult::Daemon(pipe)),
            },
        },
    }
}

fn finalize_invoker(pipe: Pipe, child: nix::unistd::Pid) -> InvocationResult {
    let _ = unblock_signals();
    let _ = nix::sys::wait::waitpid(child, None);
    pipe.read().and_then(Pipe::read).map(|_| ())
}

fn finalize_daemon(options: options::Options) -> DaemonResult {
    unblock_signals().map_err(|err| (DaemonError::UnblockSignals, err))?;
    nix::unistd::chdir(&options.root).map_err(|err| (DaemonError::ChangeRoot, err))?;
    nix::sys::stat::umask(nix::sys::stat::Mode::empty());
    change_user(options.user, options.group)?;
    redirect_streams(options.stdin, options.stdout, options.stderr)
}

fn change_user(user: Option<nix::unistd::User>, group: Option<nix::unistd::Group>) -> DaemonResult {
    if let Some(user) = user {
        nix::unistd::setuid(user.uid).map_err(|err| (DaemonError::SetUser, err))?;
    }

    if let Some(group) = group {
        nix::unistd::setgid(group.gid).map_err(|err| (DaemonError::SetGroup, err))?;
    }

    Ok(())
}

fn redirect_streams(
    stdin: Option<options::Stdio<options::Input>>,
    stdout: Option<options::Stdio<options::Output>>,
    stderr: Option<options::Stdio<options::Output>>,
) -> DaemonResult {
    use nix::libc::{STDERR_FILENO, STDIN_FILENO, STDOUT_FILENO};
    use std::os::unix::io::{AsRawFd, RawFd};
    use DaemonError::{RedirectStderr, RedirectStdin, RedirectStdout};

    fn redirect_stream<D, F>(
        devnull_fd: &mut D,
        stdio: Option<options::Stdio<F>>,
        fd: RawFd,
        error: DaemonError,
    ) -> DaemonResult
    where
        D: FnMut(DaemonError) -> DaemonResult<RawFd>,
        F: options::File,
    {
        if let Some(stdio) = stdio {
            nix::unistd::close(fd).map_err(|err| (error, err))?;
            let new_fd = match stdio {
                options::Stdio::Null => devnull_fd(error)?,
                options::Stdio::Fd(fd) => fd,
                options::Stdio::File(file) => {
                    // Allowed because we grab the error from OS
                    #[allow(clippy::map_err_ignore)]
                    let open_file = file.open().map_err(|_| (error, nix::Error::last()))?;
                    let raw_fd = open_file.as_raw_fd();
                    std::mem::forget(open_file);
                    raw_fd
                }
            };
            nix::unistd::dup2(new_fd, fd).map_err(|err| (error, err))?;
        }
        Ok(())
    }

    let mut devnull = None::<std::fs::File>;

    let mut devnull_fd = |error: DaemonError| -> DaemonResult<RawFd> {
        // Allowed because we grab the error from OS
        #[allow(clippy::map_err_ignore)]
        if devnull.is_none() {
            devnull = Some(
                std::fs::OpenOptions::new()
                    .read(true)
                    .write(true)
                    .open("/dev/null")
                    .map_err(|_| (error, nix::Error::last()))?,
            );
        }

        Ok(devnull.as_ref().unwrap().as_raw_fd())
    };

    redirect_stream(&mut devnull_fd, stdin, STDIN_FILENO, RedirectStdin)?;
    redirect_stream(&mut devnull_fd, stdout, STDOUT_FILENO, RedirectStdout)?;
    redirect_stream(&mut devnull_fd, stderr, STDERR_FILENO, RedirectStderr)
}

#[derive(Debug)]
struct Pipe {
    reader: std::os::unix::io::RawFd,
    writer: std::os::unix::io::RawFd,
}

impl std::ops::Drop for Pipe {
    fn drop(&mut self) {
        let _ = nix::unistd::close(self.reader);
        let _ = nix::unistd::close(self.writer);
    }
}

impl Pipe {
    fn new() -> InvocationResult<Self> {
        nix::unistd::pipe()
            .map(|(reader, writer)| Self { reader, writer })
            .map_err(Error::CreatePipe)
    }

    fn read(self) -> InvocationResult<Self> {
        let mut status = [0_u8];
        nix::unistd::read(self.reader, &mut status).map_err(Error::ReadStatus)?;
        if status[0] == 0 {
            Ok(self)
        } else {
            let mut error = [0_u8; 4];
            nix::unistd::read(self.reader, &mut error).map_err(Error::ReadStatus)?;
            let error = i32::from_be_bytes(error);

            if status[0] == DaemonError::Heartbeat as u8 {
                Err(Error::Heartbeat { status: error })
            } else {
                Err(Error::daemon(
                    unsafe { std::mem::transmute::<u8, DaemonError>(status[0]) },
                    nix::Error::from_errno(nix::errno::from_i32(error)),
                ))
            }
        }
    }

    fn write(self, status: u8) {
        let _ = nix::unistd::write(self.writer, &[status]);
    }

    fn error(self, error: DaemonError, errno: i32) {
        let errno = errno.to_be();
        let errno_ptr = (&errno as *const i32) as *const u8;

        let _ = nix::unistd::write(self.writer, &[error as u8]);
        let _ = nix::unistd::write(self.writer, unsafe {
            std::slice::from_raw_parts(errno_ptr, 4)
        });
    }
}

/// Allows reporting back to the invoker process if the initialization of the service was
/// successful or not.
///
/// The heartbeat is sent after the daemonizing process has succeeded and it is reported back by
/// the daemon itself. This is, therefore, a means of reporting a healthy running daemon after all
/// configuration.
///
/// The struct will automatically send a success signal when it gets dropped. This behavior can be
/// changed by setting [`fail_on_drop`](struct.Heartbeat.html#method.fail_on_drop), which is useful
/// when using the `?` error propagation early exit.
///
/// # Example
///
/// ### Reporting heartbeat on drop
/// **Infallible:**
/// ```no_run
/// # use yad::Heartbeat;
/// fn daemon_process() -> ! {
///     loop {
///         println!("loooooooooop");
///     }
/// }
///
/// fn start() -> Result<(), yad::Error> {
///     yad::daemonize()?;
///     daemon_process();
/// }
/// ```
///
/// **Fallible:**
/// ```no_run
/// # use yad::Heartbeat;
/// # fn setup_daemon() -> Result<(), yad::Error> {
/// #     Err(yad::Error::Heartbeat{ status: 1 })
/// # }
/// fn daemon_process(mut heartbeat: Heartbeat) -> Result<(), yad::Error> {
///     heartbeat.fail_on_drop();
///     setup_daemon()?;
///     heartbeat.ok();
///
///     loop {
///         println!("loooooooooop");
///     }
/// }
///
/// fn start() -> Result<(), yad::Error> {
///     let heartbeat = yad::daemonize()?;
///     daemon_process(heartbeat)
/// }
/// ```
///
/// ### Explicitly reporting heartbeat
///
/// **Infallible:**
/// ```no_run
/// # use yad::Heartbeat;
/// fn daemon_process(heartbeat: Heartbeat) -> ! {
///     heartbeat.ok();
///     loop {
///         println!("loooooooooop");
///     }
/// }
///
/// fn start() -> Result<(), yad::Error> {
///     let heartbeat = yad::daemonize()?;
///     daemon_process(heartbeat);
/// }
/// ```
///
/// **Fallible:**
/// ```no_run
/// # use yad::Heartbeat;
/// # struct Error;
/// # impl Error {
/// #   fn as_errno(&self) -> i32 {
/// #       1
/// #   }
/// # }
/// # fn setup_daemon() -> Result<(), Error> {
/// #     Err(Error)
/// # }
/// fn daemon_process(heartbeat: Heartbeat) -> ! {
///     match setup_daemon() {
///         Ok(_) => heartbeat.ok(),
///         Err(err) => {
///             heartbeat.fail(err.as_errno());
///             std::process::exit(err.as_errno());
///         }
///     }
///
///     loop {
///         println!("loooooooooop");
///     }
/// }
///
/// fn start() -> Result<(), yad::Error> {
///     let heartbeat = yad::daemonize()?;
///     daemon_process(heartbeat);
/// }
/// ```
pub struct Heartbeat(Option<Pipe>, bool);

impl Heartbeat {
    /// Sets to emit a failure when dropped.
    ///
    /// The error emitted on drop will be of carry `UnknownErrno`. To specify an `errno`,
    /// explicitly call [`fail()`](struct.Heartbeat.htnl#method.fail).
    pub fn fail_on_drop(&mut self) {
        self.1 = true;
    }

    /// Reports back to the invoking process that the daemon failed to initialize.
    ///
    /// It is best practice to terminate the failed daemon after cleaning up.
    pub fn fail(mut self, errno: i32) {
        if let Some(pipe) = self.0.take() {
            pipe.error(DaemonError::Heartbeat, errno);
        }
    }

    /// Reports back to the invoking process that the daemon successfully initialized and it is
    /// running.
    pub fn ok(mut self) {
        if let Some(pipe) = self.0.take() {
            pipe.write(0);
        }
    }
}

impl Drop for Heartbeat {
    fn drop(&mut self) {
        if let Some(pipe) = self.0.take() {
            if self.1 {
                pipe.error(DaemonError::Heartbeat, 0);
            } else {
                pipe.write(0);
            }
        }
    }
}

fn exit_success(pipe: Pipe) -> ! {
    pipe.write(0);
    std::process::exit(0);
}

fn exit_error(pipe: Pipe, error: DaemonError, cause: nix::Error) -> ! {
    let errno = cause.as_errno().map_or(-1, |errno| errno as i32);
    pipe.error(error, errno);
    std::process::exit(errno);
}