1- extern crate libc;
21extern crate errno;
2+ extern crate libc;
33
4- use self :: libc:: { c_int} ;
5-
4+ use std:: fmt;
65use std:: mem;
76use std:: ptr;
8- use std:: fmt;
97
108use 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 ) ]
1818struct 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
2628impl 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" ) ]
4749extern {
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
6971pub 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> {
148147mod 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