Skip to content

Commit

Permalink
Warn about SYS_PTRACE when running in docker (#459)
Browse files Browse the repository at this point in the history
If we get a permissions denied error when running in docker, ask about the
SYS_PTRACE capabality. Also only ask to run as sudo when we're not running
as root already.
  • Loading branch information
benfred authored Oct 21, 2021
1 parent e1b117a commit fee40b1
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -415,8 +415,26 @@ fn main() {
#[cfg(unix)]
{
if permission_denied(&err) {
eprintln!("Permission Denied: Try running again with elevated permissions by going 'sudo env \"PATH=$PATH\" !!'");
std::process::exit(1);
// Got a permission denied error, if we're not running as root - ask to use sudo
if unsafe { libc::geteuid() } != 0 {
eprintln!("Permission Denied: Try running again with elevated permissions by going 'sudo env \"PATH=$PATH\" !!'");
std::process::exit(1);
}

// We got a permission denied error running as root, check to see if we're running
// as docker, and if so ask the user to check the SYS_PTRACE capability is added
// Otherwise, fall through to the generic error handling
#[cfg(target_os="linux")]
if let Ok(cgroups) = std::fs::read_to_string("/proc/self/cgroup") {
if cgroups.contains("/docker/") {
eprintln!("Permission Denied");
eprintln!("\nIt looks like you are running in a docker container. Please make sure \
you started your container with the SYS_PTRACE capability. See \
https://github.com/benfred/py-spy#how-do-i-run-py-spy-in-docker for \
more details");
std::process::exit(1);
}
}
}
}

Expand Down

0 comments on commit fee40b1

Please sign in to comment.