Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix linux read permissions issue #10

Merged
merged 2 commits into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,6 @@ rustix = {version = "0.38.31", features = ["fs"]}
#[target.'cfg(all(not(target_os = "linux"), not(target_os = "macos"), not(target_os = "windows")))'.dependencies]
#bsd-kvm = "0.1.5"
#sysctl = "0.5.5"
[target.'cfg(target_os = "linux")'.dev-dependencies]
http-test-server = "2.1.1"

11 changes: 8 additions & 3 deletions src/platform/linux/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::os::fd::{AsFd, BorrowedFd, RawFd};
use std::path::Path;
use std::str::FromStr;

use rustix::fs::{Mode, OFlags};
use rustix::fs::{Access, AtFlags, Mode, OFlags};

use crate::platform::linux::proc_fd::ProcFd;
use crate::platform::linux::proc_info::ProcInfo;
Expand All @@ -14,9 +14,14 @@ pub(super) fn build_inode_proc_map(proc_fds: Vec<ProcFd>) -> crate::Result<HashM
let mut map: HashMap<u64, ProcInfo> = HashMap::new();

for proc_fd in proc_fds {
let dirfd = proc_fd.as_fd();
let path = "fd";
if rustix::fs::accessat(dirfd, path, Access::READ_OK, AtFlags::empty()).is_err() {
continue;
}
let dir_fd = rustix::fs::openat(
proc_fd.as_fd(),
"fd",
dirfd,
path,
OFlags::RDONLY | OFlags::DIRECTORY | OFlags::CLOEXEC,
Mode::empty(),
)?;
Expand Down
21 changes: 19 additions & 2 deletions tests/integration_tests.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use listeners;

#[test]
#[cfg(not(target_os = "linux"))]
fn test_consistency() {
Expand Down Expand Up @@ -37,3 +35,22 @@ fn test_macos() {
let ports = listeners::get_ports_by_process_name("launchd").unwrap();
assert!(!ports.is_empty());
}

#[test]
#[cfg(target_os = "linux")]
fn test_linux() {
// starts test server
use http_test_server::TestServer;
let test_server = TestServer::new().unwrap();
let test_server_port = test_server.port();

// get process by port
let processes = listeners::get_processes_by_port(test_server_port).unwrap();
assert_eq!(processes.len(), 1);

// get port by process
let pid = processes.into_iter().next().unwrap().pid;
let ports = listeners::get_ports_by_pid(pid).unwrap();
assert_eq!(ports.len(), 1);
assert_eq!(ports.into_iter().next().unwrap(), test_server_port);
}