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
//! Process handlings

use crate::conf::{BgProcessConf, BgProcessWaitSignal};
use anyhow::Result;
use crossbeam_channel::Receiver;
use std::process::{Command, Stdio};

/// Creates `Command` from a string
pub fn command(s: &str) -> Command {
    let mut cmd = Command::new("/bin/sh");
    cmd.arg("-c");
    cmd.arg(s);
    cmd.stdin(Stdio::null());
    cmd.stdout(Stdio::inherit());
    cmd.stderr(Stdio::inherit());
    cmd
}

/// Starts background processes
pub fn start_bg_processes(bg_processes: &[BgProcessConf]) -> Result<()> {
    log::info!("start background processes");

    for bg_process in bg_processes {
        let waiter = if let Some(signal) = bg_process.wait_signal {
            Some(signal_waiter(signal)?)
        } else {
            None
        };

        let self_pid = format!("{}", std::process::id());
        command(&bg_process.command)
            .env("DEVICE_CONNECTOR_PID", self_pid)
            .spawn()?;

        if let Some(waiter) = waiter {
            waiter.recv()?;
            log::trace!("received signal from background process");
        }
    }

    Ok(())
}

/// Executes before script
pub fn exec_before_script(script: &[String]) -> Result<()> {
    log::info!("execute before script");
    exec_script_lines(script)
}

/// Executes script lines
pub(crate) fn exec_script_lines(script: &[String]) -> Result<()> {
    for s in script {
        command(s).spawn()?.wait()?;
    }

    Ok(())
}

/// Registers after script
pub fn register_after_script(script: &[String]) {
    *crate::finalizer::AFTER_SCRIPT.lock().unwrap() = script.to_vec();
}

/// Creates signal waiter
pub fn signal_waiter(signal: BgProcessWaitSignal) -> Result<Receiver<()>> {
    let signal = match signal {
        BgProcessWaitSignal::Sigusr1 => signal_hook::consts::SIGUSR1,
        BgProcessWaitSignal::Sigusr2 => signal_hook::consts::SIGUSR2,
    };

    let (sender, receiver) = crossbeam_channel::unbounded();
    let mut signals = signal_hook::iterator::Signals::new([signal])?;

    std::thread::spawn(move || {
        for _signal in signals.forever() {
            if sender.send(()).is_err() {
                break;
            }
        }
    });

    Ok(receiver)
}