| Crates.io | minilsof |
| lib.rs | minilsof |
| version | 0.1.4 |
| created_at | 2024-01-11 02:05:06.407131+00 |
| updated_at | 2025-03-30 06:31:08.056929+00 |
| description | minilsof |
| homepage | |
| repository | https://github.com/jokemanfire/minilsof |
| max_upload_size | |
| id | 1095783 |
| size | 24,978 |
minilsof is a lightweight Rust library that provides functionality similar to the Linux lsof command. It allows you to list open files, find processes using specific files, and identify processes listening on specific ports.
Add this to your Cargo.toml:
[dependencies]
minilsof = "0.1.2"
If you want to use the async API, enable the async feature:
[dependencies]
minilsof = { version = "0.1.2", features = ["async"] }
use minilsof::filesync::LsofSync;
// List all open files
fn list_all_files() {
let mut lsof = LsofSync::new();
match lsof.file_ls() {
Ok(result) => {
println!("Found {} processes with open files", result.len());
// Process the results
},
Err(err) => eprintln!("Error: {}", err),
}
}
// Find processes using a specific file
fn find_processes_using_file() {
let mut lsof = LsofSync::new();
let filepath = "/usr/lib/libssl.so.3";
match lsof.target_file_ls(filepath) {
Ok(processes) => {
println!("Found {} processes using {}", processes.len(), filepath);
for process in processes {
println!("PID: {}, Name: {:?}", process.pid, process.name);
}
},
Err(err) => eprintln!("Error: {}", err),
}
}
// Find processes using a specific port
fn find_processes_using_port() {
let mut lsof = LsofSync::new();
let port = "80";
match lsof.port_ls(port) {
Ok(processes) => {
println!("Found {} processes using port {}", processes.len(), port);
for process in processes {
println!("PID: {}, Name: {:?}", process.pid, process.name);
}
},
Err(err) => eprintln!("Error: {}", err),
}
}
With the async feature enabled:
// This example requires the "async" feature to be enabled
use minilsof::fileasync::LsofAsync;
// List all open files asynchronously
async fn list_all_files_async() {
let lsof = LsofAsync::new();
match lsof.file_ls().await {
Ok(result) => {
println!("Found {} processes with open files", result.len());
// Process the results
},
Err(err) => eprintln!("Error: {}", err),
}
}
// Find processes using a specific file asynchronously
async fn find_processes_using_file_async() {
let lsof = LsofAsync::new();
let filepath = "/usr/lib/libssl.so.3";
match lsof.target_file_ls(filepath).await {
Ok(processes) => {
println!("Found {} processes using {}", processes.len(), filepath);
for process in processes {
println!("PID: {}, Name: {:?}", process.pid, process.name);
}
},
Err(err) => eprintln!("Error: {}", err),
}
}
This library is designed for Linux systems and requires access to the /proc filesystem.
Licensed under either of Apache License, Version 2.0 or MIT license at your option.
use minilsof::LsofData;
//if return data is none unwarp will panic!
//lsof all
#[test]
fn test_lsall(){
let mut d = LsofData::new();
if let Some(result) = d.file_ls(){
println!("{:?}",result);
}
}
//target file
#[test]
fn test_target(){
let filepath = "/usr/lib64/librt-2.28.so".to_string();
let mut d = LsofData::new();
let result = d.target_file_ls(filepath);
if let Some(result) = d.target_file_ls(){
println!("{:?}",result);
}
}