summaryrefslogtreecommitdiffstats
path: root/src/app/process_killer/unix.rs
blob: 46b4b42e9856651ab48ae73beca50294f242a57d (plain)
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
use crate::utils::error::BottomError;
use crate::Pid;

/// Kills a process, given a PID, for unix.
pub(crate) fn kill_process_given_pid(pid: Pid, signal: usize) -> crate::utils::error::Result<()> {
    let output = unsafe { libc::kill(pid as i32, signal as i32) };
    if output != 0 {
        // We had an error...
        let err_code = std::io::Error::last_os_error().raw_os_error();
        let err = match err_code {
            Some(libc::ESRCH) => "the target process did not exist.",
            Some(libc::EPERM) => "the calling process does not have the permissions to terminate the target process(es).",
            Some(libc::EINVAL) => "an invalid signal was specified.",
            _ => "Unknown error occurred."
        };

        return if let Some(err_code) = err_code {
            Err(BottomError::GenericError(format!(
                "Error code {} - {}",
                err_code, err,
            )))
        } else {
            Err(BottomError::GenericError(format!(
                "Error code ??? - {}",
                err,
            )))
        };
    }

    Ok(())
}