@@ -87,8 +87,8 @@ impl From<u32> for ProcFDType {
8787 }
8888}
8989
90- /// The `PIDFDInfo` trait is needed for polymorphism on pidfdinfo types, also abstracting flavor in order to provide
91- /// type-guaranteed flavor correctness
90+ /// The `PIDFDInfo` trait is needed for polymorphism on pidfdinfo types, also abstracting flavor
91+ /// in order to provide type-guaranteed flavor correctness
9292pub trait PIDFDInfo : Default {
9393 /// Return the Pid File Descriptor Info flavor of the implementing struct
9494 fn flavor ( ) -> PIDFDInfoFlavor ;
@@ -105,7 +105,7 @@ pub trait PIDFDInfo: Default {
105105/// ```
106106/// use std::io::Write;
107107/// use std::net::TcpListener;
108- /// use libproc::libproc::proc_pid::{listpidinfo, pidinfo, ListThreads };
108+ /// use libproc::libproc::proc_pid::{listpidinfo, pidinfo};
109109/// use libproc::libproc::bsd_info::{BSDInfo};
110110/// use libproc::libproc::net_info::{SocketFDInfo, SocketInfoKind};
111111/// use libproc::libproc::file_info::{pidfdinfo, ListFDs, ProcFDType};
@@ -116,45 +116,38 @@ pub trait PIDFDInfo: Default {
116116/// // Open TCP port:8000 to test.
117117/// let _listener = TcpListener::bind("127.0.0.1:8000");
118118///
119- /// if let Ok( info) = pidinfo::<BSDInfo>(pid, 0) {
120- /// if let Ok( fds) = listpidinfo::<ListFDs>(pid, info.pbi_nfiles as usize) {
121- /// for fd in &fds {
122- /// match fd.proc_fdtype.into() {
123- /// ProcFDType::Socket => {
124- /// if let Ok( socket) = pidfdinfo::<SocketFDInfo>(pid, fd.proc_fd) {
125- /// match socket.psi.soi_kind.into() {
126- /// SocketInfoKind::Tcp => {
127- /// // access to the member of `soi_proto` is unsafe becasuse of union type.
128- /// let info = unsafe { socket.psi.soi_proto.pri_tcp };
119+ /// let info = pidinfo::<BSDInfo>(pid, 0).expect("Could not get BSDInfo on {pid}");///
120+ /// let fds = listpidinfo::<ListFDs>(pid, info.pbi_nfiles as usize)
121+ /// .expect("Could not list FD of {pid}");
122+ /// for fd in &fds {
123+ /// if let( ProcFDType::Socket) = fd.proc_fdtype.into() {
124+ /// let socket = pidfdinfo::<SocketFDInfo>(pid, fd.proc_fd)
125+ /// .expect("Could not get SocketFDInfo");
126+ /// if let( SocketInfoKind::Tcp) = socket.psi.soi_kind.into() {
127+ /// // access to the member of `soi_proto` is unsafe becasuse of union type.
128+ /// let info = unsafe { socket.psi.soi_proto.pri_tcp };
129129///
130- /// // change endian and cut off because insi_lport is network endian and 16bit witdh.
131- /// let mut port = 0;
132- /// port |= info.tcpsi_ini.insi_lport >> 8 & 0x00ff;
133- /// port |= info.tcpsi_ini.insi_lport << 8 & 0xff00;
130+ /// // change endian and cut off because insi_lport is network endian and 16bit witdh.
131+ /// let mut port = 0;
132+ /// port |= info.tcpsi_ini.insi_lport >> 8 & 0x00ff;
133+ /// port |= info.tcpsi_ini.insi_lport << 8 & 0xff00;
134134///
135- /// // access to the member of `insi_laddr` is unsafe becasuse of union type.
136- /// let s_addr = unsafe { info.tcpsi_ini.insi_laddr.ina_46.i46a_addr4.s_addr };
135+ /// // access to the member of `insi_laddr` is unsafe becasuse of union type.
136+ /// let s_addr = unsafe { info.tcpsi_ini.insi_laddr.ina_46.i46a_addr4.s_addr };
137137///
138- /// // change endian because insi_laddr is network endian.
139- /// let mut addr = 0;
140- /// addr |= s_addr >> 24 & 0x000000ff;
141- /// addr |= s_addr >> 8 & 0x0000ff00;
142- /// addr |= s_addr << 8 & 0x00ff0000;
143- /// addr |= s_addr << 24 & 0xff000000;
138+ /// // change endian because insi_laddr is network endian.
139+ /// let mut addr = 0;
140+ /// addr |= s_addr >> 24 & 0x000000ff;
141+ /// addr |= s_addr >> 8 & 0x0000ff00;
142+ /// addr |= s_addr << 8 & 0x00ff0000;
143+ /// addr |= s_addr << 24 & 0xff000000;
144144///
145- /// println!("{}.{}.{}.{}:{}", addr >> 24 & 0xff, addr >> 16 & 0xff, addr >> 8 & 0xff, addr & 0xff, port);
146- /// }
147- /// _ => (),
148- /// }
149- /// }
150- /// }
151- /// _ => (),
152- /// }
145+ /// println!("{}.{}.{}.{}:{}", addr >> 24 & 0xff, addr >> 16 & 0xff, addr >> 8 & 0xff,
146+ /// addr & 0xff, port);
153147/// }
154148/// }
155149/// }
156150/// ```
157- ///
158151#[ cfg( target_os = "macos" ) ]
159152pub fn pidfdinfo < T : PIDFDInfo > ( pid : i32 , fd : i32 ) -> Result < T , String > {
160153 let flavor = T :: flavor ( ) as i32 ;
0 commit comments