Skip to content

Commit f2878ea

Browse files
Merge pull request #47 from andrewdavidmackenzie/minor_fix
add clippy checks and fix them
2 parents 36a1ca4 + 245d648 commit f2878ea

File tree

5 files changed

+123
-107
lines changed

5 files changed

+123
-107
lines changed

.travis.yml

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ os:
1111
- linux
1212
- osx
1313

14+
before_script:
15+
- rustup component add clippy
16+
1417
addons:
1518
apt:
1619
packages:
@@ -31,7 +34,20 @@ matrix:
3134
allow_failures:
3235
- rust: nightly
3336

34-
script: sudo env "PATH=$PATH" cargo test
37+
script:
38+
- cargo clippy --all --all-targets --all-features -- -D warnings
39+
- sudo env "PATH=$PATH" cargo test
40+
- cargo doc --workspace --quiet --all-features --no-deps --target-dir=target/html/code
41+
42+
deploy:
43+
provider: pages
44+
skip-cleanup: true
45+
github-token: $GITHUB_PAGES_TOKEN
46+
local-dir: target/html
47+
keep-history: false
48+
verbose: true
49+
on:
50+
branch: master
3551

3652
after_success:
3753
- |
@@ -46,7 +62,7 @@ after_success:
4662
sudo make install &&
4763
cd ../.. &&
4864
rm -rf kcov-master &&
49-
for file in target/debug/libproc-*[^\.d]; do mkdir -p "target/cov/$(basename $file)"; kcov --exclude-pattern=/.cargo,/usr/lib --verify "target/cov/$(basename $file)" "$file"; done &&
65+
for file in target/debug/libproc-*[^\.d]; do mkdir -p "target/cov/$(basename $file)"; kcov --exclude-pattern=/.cargo,/usr/lib "target/cov/$(basename $file)" "$file"; done &&
5066
bash <(curl -s https://codecov.io/bash) &&
5167
echo "Uploaded code coverage"
5268
fi

src/libproc/file_info.rs

Lines changed: 38 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,18 @@ use self::libc::{c_int, c_void};
1313
#[cfg(target_os = "macos")]
1414
#[link(name = "proc", kind = "dylib")]
1515
extern {
16-
fn proc_pidfdinfo(pid : c_int, fd : c_int, flavor : c_int, buffer : *mut c_void, buffersize : c_int) -> c_int;
16+
fn proc_pidfdinfo(pid: c_int, fd: c_int, flavor: c_int, buffer: *mut c_void, buffersize: c_int) -> c_int;
1717
}
1818

1919
pub enum PIDFDInfoFlavor {
20-
VNodeInfo = 1,
21-
VNodePathInfo = 2,
22-
SocketInfo = 3,
23-
PSEMInfo = 4,
24-
PSHMInfo = 5,
25-
PipeInfo = 6,
26-
KQueueInfo = 7,
27-
ATalkInfo = 8
20+
VNodeInfo = 1,
21+
VNodePathInfo = 2,
22+
SocketInfo = 3,
23+
PSEMInfo = 4,
24+
PSHMInfo = 5,
25+
PipeInfo = 6,
26+
KQueueInfo = 7,
27+
ATalkInfo = 8,
2828
}
2929

3030
pub struct ListFDs;
@@ -43,19 +43,19 @@ pub struct ProcFDInfo {
4343
#[derive(Copy, Clone, Debug)]
4444
pub enum ProcFDType {
4545
/// AppleTalk
46-
ATalk = 0,
46+
ATalk = 0,
4747
/// Vnode
48-
VNode = 1,
48+
VNode = 1,
4949
/// Socket
50-
Socket = 2,
50+
Socket = 2,
5151
/// POSIX shared memory
52-
PSHM = 3,
52+
PSHM = 3,
5353
/// POSIX semaphore
54-
PSEM = 4,
54+
PSEM = 4,
5555
/// Kqueue
56-
KQueue = 5,
56+
KQueue = 5,
5757
/// Pipe
58-
Pipe = 6,
58+
Pipe = 6,
5959
/// FSEvents
6060
FSEvents = 7,
6161
/// Unknown
@@ -65,15 +65,15 @@ pub enum ProcFDType {
6565
impl From<u32> for ProcFDType {
6666
fn from(value: u32) -> ProcFDType {
6767
match value {
68-
0 => ProcFDType::ATalk ,
69-
1 => ProcFDType::VNode ,
70-
2 => ProcFDType::Socket ,
71-
3 => ProcFDType::PSHM ,
72-
4 => ProcFDType::PSEM ,
73-
5 => ProcFDType::KQueue ,
74-
6 => ProcFDType::Pipe ,
68+
0 => ProcFDType::ATalk,
69+
1 => ProcFDType::VNode,
70+
2 => ProcFDType::Socket,
71+
3 => ProcFDType::PSHM,
72+
4 => ProcFDType::PSEM,
73+
5 => ProcFDType::KQueue,
74+
6 => ProcFDType::Pipe,
7575
7 => ProcFDType::FSEvents,
76-
_ => ProcFDType::Unknown ,
76+
_ => ProcFDType::Unknown,
7777
}
7878
}
7979
}
@@ -129,11 +129,11 @@ pub trait PIDFDInfo: Default {
129129
/// addr |= s_addr << 24 & 0xff000000;
130130
///
131131
/// println!("{}.{}.{}.{}:{}", addr >> 24 & 0xff, addr >> 16 & 0xff, addr >> 8 & 0xff, addr & 0xff, port);
132-
/// },
132+
/// }
133133
/// _ => (),
134134
/// }
135135
/// }
136-
/// },
136+
/// }
137137
/// _ => (),
138138
/// }
139139
/// }
@@ -142,7 +142,7 @@ pub trait PIDFDInfo: Default {
142142
/// ```
143143
///
144144
#[cfg(target_os = "macos")]
145-
pub fn pidfdinfo<T: PIDFDInfo>(pid : i32, fd: i32) -> Result<T, String> {
145+
pub fn pidfdinfo<T: PIDFDInfo>(pid: i32, fd: i32) -> Result<T, String> {
146146
let flavor = T::flavor() as i32;
147147
let buffer_size = mem::size_of::<T>() as i32;
148148
let mut pidinfo = T::default();
@@ -161,8 +161,8 @@ pub fn pidfdinfo<T: PIDFDInfo>(pid : i32, fd: i32) -> Result<T, String> {
161161
}
162162

163163
#[cfg(not(target_os = "macos"))]
164-
pub fn pidfdinfo<T: PIDFDInfo>(_pid : i32, _fd: i32) -> Result<T, String> {
165-
unimplemented!()
164+
pub fn pidfdinfo<T: PIDFDInfo>(_pid: i32, _fd: i32) -> Result<T, String> {
165+
unimplemented!()
166166
}
167167

168168
#[cfg(all(test, target_os = "macos"))]
@@ -171,6 +171,7 @@ mod test {
171171
use crate::libproc::file_info::{ListFDs, ProcFDType};
172172
use crate::libproc::net_info::{SocketFDInfo, SocketInfoKind};
173173
use crate::libproc::proc_pid::{listpidinfo, pidinfo};
174+
174175
use super::pidfdinfo;
175176

176177
#[test]
@@ -184,19 +185,15 @@ mod test {
184185
let info = pidinfo::<BSDInfo>(pid, 0).unwrap();
185186
let fds = listpidinfo::<ListFDs>(pid, info.pbi_nfiles as usize).unwrap();
186187
for fd in fds {
187-
match fd.proc_fdtype.into() {
188-
ProcFDType::Socket => {
189-
let socket = pidfdinfo::<SocketFDInfo>(pid, fd.proc_fd).unwrap();
190-
match socket.psi.soi_kind.into() {
191-
SocketInfoKind::Tcp => unsafe {
192-
let info = socket.psi.soi_proto.pri_tcp;
193-
assert_eq!(socket.psi.soi_protocol, libc::IPPROTO_TCP);
194-
assert_eq!(info.tcpsi_ini.insi_lport as u32, 65535);
195-
}
196-
_ => (),
188+
if let ProcFDType::Socket = fd.proc_fdtype.into() {
189+
let socket = pidfdinfo::<SocketFDInfo>(pid, fd.proc_fd).unwrap();
190+
if let SocketInfoKind::Tcp = socket.psi.soi_kind.into() {
191+
unsafe {
192+
let info = socket.psi.soi_proto.pri_tcp;
193+
assert_eq!(socket.psi.soi_protocol, libc::IPPROTO_TCP);
194+
assert_eq!(info.tcpsi_ini.insi_lport as u32, 65535);
197195
}
198-
},
199-
_ => (),
196+
}
200197
}
201198
}
202199
}

src/libproc/kmesg_buffer.rs

Lines changed: 53 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,38 @@
1-
extern crate libc;
21
extern crate errno;
2+
extern crate libc;
33

4-
use self::libc::{c_int};
5-
4+
use std::fmt;
65
use std::mem;
76
use std::ptr;
8-
use std::fmt;
97

108
use crate::libproc::helpers;
119

10+
use self::libc::c_int;
11+
1212
// See https://opensource.apple.com/source/xnu/xnu-1456.1.26/bsd/sys/msgbuf.h
13-
const MAX_MSG_BSIZE : c_int = (1*1024*1024);
14-
const MSG_MAGIC : c_int = 0x063061;
13+
const MAX_MSG_BSIZE: c_int = 1024 * 1024;
14+
const MSG_MAGIC: c_int = 0x063_061;
1515

1616
// See /usr/include/sys/msgbuf.h on your Mac.
1717
#[repr(C)]
1818
struct MessageBuffer {
19-
pub msg_magic : c_int,
20-
pub msg_size : c_int,
21-
pub msg_bufx : c_int, // write pointer
22-
pub msg_bufr : c_int, // read pointer
23-
pub msg_bufc : * mut u8 // buffer
19+
pub msg_magic: c_int,
20+
pub msg_size: c_int,
21+
pub msg_bufx: c_int,
22+
// write pointer
23+
pub msg_bufr: c_int,
24+
// read pointer
25+
pub msg_bufc: *mut u8, // buffer
2426
}
2527

2628
impl Default for MessageBuffer {
2729
fn default() -> MessageBuffer {
2830
MessageBuffer {
29-
msg_magic : 0,
30-
msg_size : 0,
31-
msg_bufx : 0,
32-
msg_bufr : 0,
33-
msg_bufc : ptr::null_mut() as * mut u8
31+
msg_magic: 0,
32+
msg_size: 0,
33+
msg_bufx: 0,
34+
msg_bufr: 0,
35+
msg_bufc: ptr::null_mut() as *mut u8,
3436
}
3537
}
3638
}
@@ -45,7 +47,7 @@ impl fmt::Debug for MessageBuffer {
4547
// Original signatures of functions can be found at http://opensource.apple.com/source/Libc/Libc-594.9.4/darwin/libproc.c
4648
#[link(name = "proc", kind = "dylib")]
4749
extern {
48-
fn proc_kmsgbuf(buffer : *mut MessageBuffer, buffersize : u32) -> c_int;
50+
fn proc_kmsgbuf(buffer: *mut MessageBuffer, buffersize: u32) -> c_int;
4951
}
5052

5153
/// Get upto buffersize bytes from the the kernel message buffer - as used by dmesg
@@ -67,7 +69,7 @@ extern {
6769
/// }
6870
// See http://opensource.apple.com//source/system_cmds/system_cmds-336.6/dmesg.tproj/dmesg.c
6971
pub fn kmsgbuf() -> Result<String, String> {
70-
let mut message_buffer : MessageBuffer = Default::default();
72+
let mut message_buffer: MessageBuffer = Default::default();
7173
let ret: i32;
7274

7375
unsafe {
@@ -76,37 +78,35 @@ pub fn kmsgbuf() -> Result<String, String> {
7678

7779
if ret <= 0 {
7880
Err(helpers::get_errno_with_message(ret))
79-
} else
80-
{
81-
if message_buffer.msg_magic != MSG_MAGIC {
82-
println!("Message buffer: {:?}", message_buffer);
83-
Err(format!("The magic number 0x{:x} is incorrect", message_buffer.msg_magic))
84-
} else {
85-
// Avoid starting beyond the end of the buffer
86-
if message_buffer.msg_bufx >= MAX_MSG_BSIZE {
87-
message_buffer.msg_bufx = 0;
88-
}
89-
let mut output : Vec<u8> = Vec::new();
81+
} else if message_buffer.msg_magic != MSG_MAGIC {
82+
println!("Message buffer: {:?}", message_buffer);
83+
Err(format!("The magic number 0x{:x} is incorrect", message_buffer.msg_magic))
84+
} else {
85+
// Avoid starting beyond the end of the buffer
86+
if message_buffer.msg_bufx >= MAX_MSG_BSIZE {
87+
message_buffer.msg_bufx = 0;
88+
}
89+
let mut output: Vec<u8> = Vec::new();
9090

91-
// The message buffer is circular; start at the read pointer, and go to the write pointer - 1.
92-
unsafe {
93-
let mut ch : u8;
91+
// The message buffer is circular; start at the read pointer, and go to the write pointer - 1.
92+
unsafe {
93+
let mut ch: u8;
9494
// let newl : bool = false;
9595
// let skip : bool = false;
96-
let mut p : * mut u8 = message_buffer.msg_bufc.offset(message_buffer.msg_bufx as isize);
97-
let ep : * mut u8 = message_buffer.msg_bufc.offset((message_buffer.msg_bufx - 1) as isize);
96+
let mut p: *mut u8 = message_buffer.msg_bufc.offset(message_buffer.msg_bufx as isize);
97+
let ep: *mut u8 = message_buffer.msg_bufc.offset((message_buffer.msg_bufx - 1) as isize);
9898
// let buf : [u8; 5];
9999

100-
while p != ep {
101-
// If at the end, then loop around to the start
102-
// TODO should use actual size (from struct element) - not the max size??
103-
if p == message_buffer.msg_bufc.offset(MAX_MSG_BSIZE as isize) {
104-
p = message_buffer.msg_bufc;
105-
}
100+
while p != ep {
101+
// If at the end, then loop around to the start
102+
// TODO should use actual size (from struct element) - not the max size??
103+
if p == message_buffer.msg_bufc.offset(MAX_MSG_BSIZE as isize) {
104+
p = message_buffer.msg_bufc;
105+
}
106106

107-
ch = *p;
107+
ch = *p;
108108

109-
/* Skip "\n<.*>" syslog sequences.
109+
/* Skip "\n<.*>" syslog sequences.
110110
if skip {
111111
if ch == '>' {
112112
newl = skip = false;
@@ -134,12 +134,11 @@ pub fn kmsgbuf() -> Result<String, String> {
134134
}
135135
*/
136136

137-
output.push(ch);
138-
p = p.offset(1);
139-
}
140-
141-
Ok(String::from_utf8(output).unwrap())
137+
output.push(ch);
138+
p = p.offset(1);
142139
}
140+
141+
Ok(String::from_utf8(output).unwrap())
143142
}
144143
}
145144
}
@@ -148,19 +147,22 @@ pub fn kmsgbuf() -> Result<String, String> {
148147
mod test {
149148
use std::io;
150149
use std::io::Write;
151-
use super::kmsgbuf;
150+
152151
use crate::libproc::proc_pid::am_root;
153152

153+
use super::kmsgbuf;
154+
154155
#[test]
155-
// TODO implement ksmgbuf() on linux
156-
// TODO fix this on macos: error message returned is
156+
#[ignore]
157+
// TODO implement ksmgbuf() on linux - https://github.com/andrewdavidmackenzie/libproc-rs/issues/43
158+
// TODO fix on macos: an error message is returned - https://github.com/andrewdavidmackenzie/libproc-rs/issues/39
157159
// Message buffer: MessageBuffer { magic: 0x3a657461, size: 1986947360, bufx: 1684630625}
158160
// thread 'libproc::kmesg_buffer::test::kmessagebuffer_test' panicked at 'The magic number 0x3a657461 is incorrect', src/libproc/kmesg_buffer.rs:194:33
159161
fn kmessagebuffer_test() {
160162
if am_root() {
161163
match kmsgbuf() {
162164
Ok(buffer) => println!("Buffer: {:?}", buffer),
163-
Err(message) => assert!(true, message) // TODO cause the test to fail!!
165+
Err(message) => panic!(message)
164166
}
165167
} else {
166168
writeln!(&mut io::stdout(), "test libproc::kmesg_buffer::kmessagebuffer_test ... skipped as it needs to be run as root").unwrap();

0 commit comments

Comments
 (0)