{"record":{"id":"2217f6600cf3fe84","repo":"ClementTsang/bottom","slug":"error-code-err-code-err","errorCode":null,"errorMessage":"Error code {err_code} - {err}","messagePattern":"Error code (.+?) - (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/utils/process_killer.rs","lineNumber":81,"sourceCode":"pub fn kill_process_given_pid(pid: Pid, signal: usize) -> anyhow::Result<()> {\n    // SAFETY: the signal should be valid, and we act properly on an error (exit\n    // code not 0).\n    let output = unsafe { libc::kill(pid, signal as i32) };\n\n    if output != 0 {\n        // We had an error...\n        let err_code = std::io::Error::last_os_error().raw_os_error();\n        let err = match err_code {\n            Some(libc::ESRCH) => \"the target process did not exist.\",\n            Some(libc::EPERM) => {\n                \"the calling process does not have the permissions to terminate the target process(es).\"\n            }\n            Some(libc::EINVAL) => \"an invalid signal was specified.\",\n            _ => \"Unknown error occurred.\",\n        };\n\n        if let Some(err_code) = err_code {\n            bail!(format!(\"Error code {err_code} - {err}\"))\n        } else {\n            bail!(format!(\"Error code unknown - {err}\"))\n        };\n    }\n\n    Ok(())\n}\n","sourceCodeStart":63,"sourceCodeEnd":89,"githubUrl":"https://github.com/ClementTsang/bottom/blob/b77d3175028849824e987c35177e8f61450d72e7/src/utils/process_killer.rs#L63-L89","documentation":"On Unix, the public `kill_process_given_pid(pid, signal)` calls `libc::kill`. On failure (non-zero return) it reads the last OS error and bails with `Error code {err_code} - {err}` where `err` is a human-readable description of the errno (ESRCH, EPERM, or EINVAL). The numeric errno accompanies the message because any libc error beyond the three mapped cases also lands here with the generic 'Unknown error occurred.' text.","triggerScenarios":"`libc::kill` returning -1: ESRCH (target PID does not exist), EPERM (insufficient permission to signal the target), EINVAL (invalid signal value passed as `signal`), or any other errno (e.g. EPERM in containers, init restrictions) with a known code.","commonSituations":"Killing a stale PID after the process exited (ESRCH); non-root user killing another user's process (EPERM); passing an out-of-range signal number like 99 (EINVAL); running inside Docker without CAP_KILL.","solutions":["Match on the errno shown in the message: 3 (ESRCH) means the process is already gone — treat as success; 1 (EPERM) means run as root/sudo or fix ownership; 22 (EINVAL) means pass a valid signal number","Verify the PID is still alive before killing","If EINVAL, pass a valid signal (e.g. 9 for SIGKILL, 15 for SIGTERM)","Check container capabilities (CAP_KILL) or user namespaces if EPERM occurs inside Docker/Kubernetes"],"exampleFix":"// before\nkill_process_given_pid(pid, signal)?;\n// after\nmatch kill_process_given_pid(pid, signal) {\n    Ok(()) => {},\n    Err(e) if e.to_string().contains(\"Error code 3\") => {\n        // ESRCH: process already gone, ignore\n    },\n    Err(e) => return Err(e),\n}","handlingStrategy":"try-catch","validationCode":"// Unix: validate signal and liveness before libc::kill\nassert!((1..=31).contains(&signal), \"invalid signal {signal}\");\n// ESRCH check: kill(pid, 0) probes existence without sending a signal\nlet alive = unsafe { libc::kill(pid, 0) } == 0\n    || std::io::Error::last_os_error().raw_os_error() != Some(libc::ESRCH);","typeGuard":"fn errno_of(err: &anyhow::Error) -> Option<i32> {\n    err.chain().find_map(|c|\n        c.downcast_ref::<std::io::Error>().and_then(|e| e.raw_os_error()))\n}\nfn is_esrch(err: &anyhow::Error) -> bool { errno_of(err) == Some(libc::ESRCH) }","tryCatchPattern":"match kill_process_given_pid(pid, signal) {\n    Ok(()) => {}\n    Err(e) if e.to_string().contains(\"Error code 3\") => {} // ESRCH: gone\n    Err(e) if e.to_string().contains(\"Error code 1\") => {\n        eprintln!(\"need elevated privileges: {e}\") // EPERM\n    }\n    Err(e) => return Err(e),\n}","preventionTips":["Probe with kill(pid, 0) before sending a real signal","Pass only valid signal numbers (SIGKILL=9, SIGTERM=15)","Run as root or with CAP_KILL when targeting other users' processes","Map errnos 3/1/22 to distinct handling paths instead of one generic failure"],"tags":["unix","signal","libc","process-kill","errno"],"backgroundTag":"permission-denied","analyzedSha":"b77d3175028849824e987c35177e8f61450d72e7","analyzedAt":"2026-09-07T14:53:21.246Z","contentChangedAt":"2026-09-07T14:53:21.246Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}