Skip to content

Commit

Permalink
Merge pull request #10 from jacek-kurlit/main
Browse files Browse the repository at this point in the history
fix linux read permissions issue
  • Loading branch information
GyulyVGC authored Jul 11, 2024
2 parents e82c1e4 + 06fa911 commit ff72556
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 5 deletions.
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);
}

0 comments on commit ff72556

Please sign in to comment.