Skip to content

Commit 359b222

Browse files
pub use of mods (#126)
* pub use mods of libproc to clean up the API * Re-exports and restructuring of docs for re-exported modules * Re-exports and restructuring of docs for re-exported modules * Re-exports and restructuring of docs for re-exported modules
1 parent 465c377 commit 359b222

File tree

15 files changed

+171
-217
lines changed

15 files changed

+171
-217
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ all: clippy run-tests build-docs
33

44
.PHONY: clippy
55
clippy:
6-
cargo clippy --all --tests --all-targets --all-features -- --warn clippy::pedantic -D warnings
6+
cargo clippy --all --tests --no-deps --all-targets --all-features -- --warn clippy::pedantic -D warnings
77

88
.PHONY: configure-coverage
99
configure-coverage:

examples/dmesg.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
1-
//! A `dmesg` command that is a simple demonstration program for using the [`libproc`](../libproc/index.html) library
1+
//! A `dmesg` command that is a simple demonstration program for using
2+
//! the `libproc` library
23
//!
34
//! Usage
45
//! =
56
//!
67
//! `> dmesg`
78
//!
89
9-
extern crate libproc;
10-
extern crate libc;
11-
12-
use crate::libproc::libproc::kmesg_buffer;
10+
use libproc::kmesg_buffer;
1311

1412
fn main() {
1513
match kmesg_buffer::kmsgbuf() {
1614
Ok(message) => print!("{message}"),
17-
Err(e) => eprintln!("{e}")
15+
Err(e) => eprintln!("{e}"),
1816
}
19-
}
17+
}

examples/procinfo.rs

Lines changed: 19 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
//! `procinfo` is a simple program to demonstrate the use of the [`libproc`](../libproc/index.html)
2-
//! library functions.
1+
//! `procinfo` is a simple program to demonstrate the use of the `libproc` library.
32
//!
43
//! It prints out info about a process specified by its pid, or the current process if no pid
54
//! specified.
@@ -23,54 +22,39 @@
2322
//! ...
2423
//! ```
2524
26-
extern crate libproc;
27-
extern crate libc;
28-
25+
use libproc::pid_rusage::{pidrusage, RUsageInfoV0};
26+
use libproc::proc_pid;
27+
use libproc::processes;
2928
use std::env;
3029
use std::io::Write;
31-
use libproc::libproc::proc_pid;
32-
use libproc::libproc::pid_rusage::{pidrusage, RUsageInfoV0};
33-
use libproc::processes;
34-
35-
mod c {
36-
extern crate libc;
37-
extern {
38-
pub fn getpid() -> libc::pid_t;
39-
}
40-
}
41-
42-
fn getpid() -> i32 {
43-
unsafe {
44-
c::getpid()
45-
}
46-
}
30+
use std::process;
4731

4832
fn procinfo(pid: i32) {
4933
match proc_pid::libversion() {
5034
Ok((major, minor)) => println!("Libversion: {major}.{minor}"),
51-
Err(err) => writeln!(&mut std::io::stderr(), "Error: {err}").unwrap()
35+
Err(err) => writeln!(&mut std::io::stderr(), "Error: {err}").unwrap(),
5236
}
5337

5438
println!("Pid: {pid}");
5539

5640
match proc_pid::pidpath(pid) {
5741
Ok(path) => println!("Path: {path}"),
58-
Err(err) => writeln!(&mut std::io::stderr(), "Error: {err}").unwrap()
42+
Err(err) => writeln!(&mut std::io::stderr(), "Error: {err}").unwrap(),
5943
}
6044

6145
match pidrusage::<RUsageInfoV0>(pid) {
6246
Ok(resource_usage) => println!("Memory Used: {} Bytes", resource_usage.ri_resident_size),
63-
Err(err) => writeln!(&mut std::io::stderr(), "Error: {err}").unwrap()
47+
Err(err) => writeln!(&mut std::io::stderr(), "Error: {err}").unwrap(),
6448
}
6549

6650
match proc_pid::name(pid) {
6751
Ok(name) => println!("Name: {name}"),
68-
Err(err) => writeln!(&mut std::io::stderr(), "Error: {err}").unwrap()
52+
Err(err) => writeln!(&mut std::io::stderr(), "Error: {err}").unwrap(),
6953
}
7054

7155
match proc_pid::regionfilename(pid, 0) {
7256
Ok(regionfilename) => println!("Region Filename (at address 0): {regionfilename}"),
73-
Err(err) => writeln!(&mut std::io::stderr(), "Error: {err}").unwrap()
57+
Err(err) => writeln!(&mut std::io::stderr(), "Error: {err}").unwrap(),
7458
}
7559

7660
match processes::pids_by_type(processes::ProcFilter::All) {
@@ -79,19 +63,22 @@ fn procinfo(pid: i32) {
7963
for pid in pids {
8064
println!("{pid}");
8165
}
82-
},
83-
Err(err) => writeln!(&mut std::io::stderr(), "Error: {err}").unwrap()
66+
}
67+
Err(err) => writeln!(&mut std::io::stderr(), "Error: {err}").unwrap(),
8468
}
8569
}
8670

71+
/// Print out some information about the current process (if no arguments provided)
72+
/// or a particular process (by passing that process's PID as the first argument
8773
fn main() {
8874
let args: Vec<String> = env::args().collect();
8975

9076
let pid = if args.len() == 1 {
91-
getpid()
77+
process::id()
9278
} else {
93-
args[1].clone().parse::<i32>().unwrap()
79+
args[1].clone().parse::<u32>().unwrap()
9480
};
9581

96-
procinfo(pid);
97-
}
82+
#[allow(clippy::cast_possible_wrap)]
83+
procinfo(pid as i32);
84+
}

src/lib.rs

Lines changed: 52 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -5,54 +5,62 @@
55
//!
66
//! Not all methods are available on both Operating Systems yet, but more will be made
77
//! cross-platform over time.
8+
//!
9+
//! Get information (such as name, path, process info, fd) about running processes by pid, process type, etc.
10+
//!
11+
//! At the moment these methods have been implemented, most of which have examples in their docs:
12+
//!
813
9-
extern crate errno;
10-
extern crate libc;
11-
12-
/// Get information (such as name, path, process info, fd) about running processes by pid, process type, etc.
13-
/// At the moment these methods have been implemented, most of which have examples in their docs:
14-
///
15-
/// # `libproc::libproc` (names that way for bad, historical reasons!)
16-
/// ## Process / PID related
17-
/// `pub fn listpids(proc_types: ProcType) -> Result<Vec<u32>, String> (macos) (linux)`
18-
///
19-
/// `pub fn listpidspath(proc_types: ProcType, path: &str) -> Result<Vec<u32>, String> (macos) (linux)`
20-
///
21-
/// `pub fn pidinfo<T: PIDInfo>(pid : i32, arg: u64) -> Result<T, String> (macos)`
22-
///
23-
/// `pub fn regionfilename(pid: i32, address: u64) -> Result<String, String> (macos)`
24-
///
25-
/// `pub fn pidpath(pid : i32) -> Result<String, String> (macos) (linux)`
26-
///
27-
/// `pub fn libversion() -> Result<(i32, i32), String> (macos)`
28-
///
29-
/// `pub fn name(pid: i32) -> Result<String, String> (linux) (macos)`
30-
///
31-
/// `pub fn listpidinfo<T: ListPIDInfo>(pid : i32, max_len: usize) -> Result<Vec<T::Item>, String> (macos)`
32-
///
33-
/// `pub fn pidcwd(pid: pid_t) -> Result<PathBuf, String> (linux)`
34-
///
35-
/// `pub fn cwdself() -> Result<PathBuf, String> (linux)`
36-
///
37-
/// ## File and FileDescriptor related
38-
/// `pub fn pidfdinfo<T: PIDFDInfo>(pid : i32, fd: i32) -> Result<T, String> (macos)`
39-
///
40-
/// ## PID Resource Usage related
41-
/// (Added in Mac OS X 10.9 - under "macosx_10_9" feature)
42-
/// `pub fn pidrusage<T: PIDRUsage>(pid : i32) -> Result<T, String> (macos)`
43-
///
44-
/// ## Kernel Message Buffer - kmsgbuf
45-
/// `pub fn kmsgbuf() -> Result<String, String>`
46-
pub mod libproc;
4714
/// List processes by type, path or by type and path.
48-
///
49-
/// `pub fn pids_by_type(filter: ProcFilter)` (macos) (linux)
50-
///
51-
/// `pub fn pids_by_path(path: &Path, is_volume: bool, exclude_event_only: bool)` (macos)
52-
///
53-
/// `pub fn pids_by_type_and_path(filter: ProcFilter, path: &Path, is_volume: bool, exclude_event_only: bool)` (macos)
5415
pub mod processes;
5516

17+
#[doc(inline)]
18+
/// Get information about processes using mainly the `pid`
19+
pub use libproc::proc_pid;
20+
21+
#[doc(inline)]
22+
/// Read messages from the Kernel Message Buffer
23+
pub use libproc::kmesg_buffer;
24+
25+
#[doc(inline)]
26+
/// Get information about resource usage of processes
27+
pub use libproc::pid_rusage;
28+
29+
#[cfg(any(target_os = "macos", doc))]
30+
#[doc(inline)]
31+
/// Get information specific to BSD/Darwin on macos
32+
pub use libproc::bsd_info;
33+
34+
#[cfg(any(target_os = "macos", doc))]
35+
#[doc(inline)]
36+
/// Get information about a process's use of different types of file descriptors
37+
pub use libproc::file_info;
38+
39+
#[cfg(any(target_os = "macos", doc))]
40+
#[doc(inline)]
41+
/// Get information about a processes use of network, sockets etc.
42+
pub use libproc::net_info;
43+
44+
#[cfg(any(target_os = "macos", doc))]
45+
#[doc(inline)]
46+
/// Get information about a process's BSD Tasks
47+
pub use libproc::task_info;
48+
49+
#[cfg(any(target_os = "macos", doc))]
50+
#[doc(inline)]
51+
/// Get information about threads within a process
52+
pub use libproc::thread_info;
53+
54+
#[cfg(any(target_os = "macos", doc))]
55+
#[doc(inline)]
56+
/// Get information about Work Queues
57+
pub use libproc::work_queue_info;
58+
59+
// Not documenting this as this export is legacy, and replaced by all the re-exports of
60+
// sub-modules above
61+
#[doc(hidden)]
62+
pub mod libproc;
63+
5664
#[cfg(target_os = "macos")]
5765
#[allow(warnings, missing_docs)]
5866
mod osx_libproc_bindings {

src/libproc/bsd_info.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
use crate::libproc::proc_pid::{PIDInfo, PidInfoFlavor};
2+
#[cfg(target_os = "macos")]
23
pub use crate::osx_libproc_bindings::proc_bsdinfo as BSDInfo;
34

5+
#[cfg(target_os = "macos")]
46
impl PIDInfo for BSDInfo {
5-
fn flavor() -> PidInfoFlavor { PidInfoFlavor::TBSDInfo }
7+
fn flavor() -> PidInfoFlavor {
8+
PidInfoFlavor::TBSDInfo
9+
}
610
}

src/libproc/file_info.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
1-
extern crate libc;
2-
3-
use std::mem;
4-
51
use crate::libproc::helpers;
62
use crate::libproc::proc_pid::{ListPIDInfo, PidInfoFlavor};
3+
use std::mem;
74

8-
#[cfg(target_os = "macos")]
9-
use self::libc::c_void;
105
#[cfg(target_os = "macos")]
116
use crate::osx_libproc_bindings::proc_pidfdinfo;
7+
#[cfg(target_os = "macos")]
8+
use libc::c_void;
129

1310
/// Flavor of Pid `FileDescriptor` info for different types of File Descriptors
1411
pub enum PIDFDInfoFlavor {

src/libproc/helpers.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::errno::errno;
1+
use errno::errno;
22
#[cfg(any(target_os = "linux", target_os = "redox", target_os = "android"))]
33
use std::fs::File;
44
#[cfg(any(target_os = "linux", target_os = "redox", target_os = "android"))]
@@ -87,7 +87,7 @@ pub fn parse_memory_string(line: &str) -> Result<u64, String> {
8787
#[cfg(test)]
8888
mod test {
8989
use super::check_errno;
90-
use crate::errno::{set_errno, Errno};
90+
use errno::{set_errno, Errno};
9191

9292
#[cfg(any(target_os = "linux", target_os = "redox", target_os = "android"))]
9393
mod linux {

src/libproc/kmesg_buffer.rs

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
extern crate errno;
2-
extern crate libc;
3-
41
#[cfg(target_os = "macos")]
52
use std::str;
63

74
#[cfg(target_os = "macos")]
8-
use self::libc::c_void;
5+
use libc::c_void;
96

107
#[cfg(any(target_os = "linux", target_os = "redox", target_os = "android"))]
118
use std::fs::File;
@@ -21,16 +18,23 @@ use std::{thread, time};
2118
#[cfg(target_os = "macos")]
2219
use crate::osx_libproc_bindings::{proc_kmsgbuf, MAXBSIZE as MAX_MSG_BSIZE};
2320

24-
/// Get the contents of the kernel message buffer
21+
#[cfg(any(target_os = "macos", doc))]
22+
/// Read messages from the kernel message buffer
2523
///
2624
/// Entries are in the format:
2725
/// faclev,seqnum,timestamp[optional, ...];message\n
2826
/// TAGNAME=value (0 or more Tags)
2927
/// See <http://opensource.apple.com//source/system_cmds/system_cmds-336.6/dmesg.tproj/dmesg.c>
3028
///
29+
/// On linux:
30+
/// Turns out that reading to the end of an "infinite file" like "/dev/kmsg" with standard file
31+
/// reading methods will block at the end of file, so a workaround is required. Do the blocking
32+
/// reads on a thread that sends lines read back through a channel, and then return when the thread
33+
/// has blocked and can't send anymore. Returning will end the thread and the channel.
34+
///
3135
/// # Errors
3236
///
33-
/// Will return an `Err` if Darwin's method `proc_kmsgbuf` returns an empty message buffer
37+
/// An `Err` will be returned if `/dev/kmsg` device cannot be read
3438
#[cfg(target_os = "macos")]
3539
pub fn kmsgbuf() -> Result<String, String> {
3640
let mut message_buffer: Vec<u8> = Vec::with_capacity(MAX_MSG_BSIZE as _);
@@ -60,16 +64,6 @@ pub fn kmsgbuf() -> Result<String, String> {
6064
}
6165
}
6266

63-
/// Get a message (String) from the kernel message ring buffer
64-
/// Turns out that reading to the end of an "infinite file" like "/dev/kmsg" with standard file
65-
/// reading methods will block at the end of file, so a workaround is required. Do the blocking
66-
/// reads on a thread that sends lines read back through a channel, and then return when the thread
67-
/// has blocked and can't send anymore. Returning will end the thread and the channel.
68-
///
69-
/// # Errors
70-
///
71-
/// An `Err` will be returned if `/dev/kmsg` device cannot be read
72-
///
7367
#[cfg(any(target_os = "linux", target_os = "redox", target_os = "android"))]
7468
pub fn kmsgbuf() -> Result<String, String> {
7569
let mut file = File::open("/dev/kmsg");

0 commit comments

Comments
 (0)