-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
dmesg: fix a non working support of /dev/kmesg #177
base: main
Are you sure you want to change the base?
Conversation
Thanks for your PR. I think the "problem" is that this functionality has not been implemented yet. So far "only"
And so I think your changes should work alongside the existing functionality instead of replacing it :) Any additional thoughts @fuad1502 ? |
Ops, I tough (my bad) that even the kmsg format had the same format of /dev/kmsg. Instead it uses \0 as separator instead of \n. Let me to to update this patch. |
Add the field kmsg_record_separator to the struct Dmesg so it can be changed from \0 to \n when dmesg reads from a file.
a4c0201
to
553e78b
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Overall, I think it looks good! But please note my comments below. Thank you!
src/uu/dmesg/src/dmesg.rs
Outdated
@@ -196,6 +201,7 @@ impl Dmesg<'_> { | |||
fn new() -> Self { | |||
Dmesg { | |||
kmsg_file: "/dev/kmsg", | |||
kmsg_record_separator: 10, // '\n' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can write it more clearly like so:
kmsg_record_separator: 10, // '\n' | |
kmsg_record_separator: b'\n', |
src/uu/dmesg/src/dmesg.rs
Outdated
@@ -256,10 +262,16 @@ impl Dmesg<'_> { | |||
} | |||
|
|||
fn try_iter(&self) -> UResult<RecordIterator> { | |||
let file = File::open(self.kmsg_file) | |||
let file = OpenOptions::new() | |||
.custom_flags(libc::O_NONBLOCK) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should add conditional compilations for Windows #[cfg(target_os = "windows")]
to remove any reference to UNIX specific functionalities. If we're still going to support Windows, we'll probably only support it for reading offline files with --kmsg-file
option, therefore not having the non-blocking read would not be an issue.
src/uu/dmesg/src/dmesg.rs
Outdated
@@ -256,10 +262,16 @@ impl Dmesg<'_> { | |||
} | |||
|
|||
fn try_iter(&self) -> UResult<RecordIterator> { | |||
let file = File::open(self.kmsg_file) | |||
let file = OpenOptions::new() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cleared /dev/kmsg
records, are not shown by dmesg
. To simulate this behavior, we need to (quoting from https://www.kernel.org/doc/Documentation/ABI/testing/dev-kmsg):
SEEK_DATA, 0
seek after the last record available at the time
the last SYSLOG_ACTION_CLEAR was issued.
I am not sure if there are any Rust library that support seeking with SEEK_DATA
, if you can't find it too, you can use libc
unsafe method:
let fd = file.as_raw_fd();
unsafe { libc::lseek(fd, 0, libc::SEEK_DATA.into()) };
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I updated the patch with your suggestion. I take care to protect the code by #cfg[target_os = "windows"] for the code unix specific.
553e78b
to
d34cd72
Compare
src/uu/dmesg/src/dmesg.rs
Outdated
@@ -256,10 +268,26 @@ impl Dmesg<'_> { | |||
} | |||
|
|||
fn try_iter(&self) -> UResult<RecordIterator> { | |||
let file = File::open(self.kmsg_file) | |||
.map_err_context(|| format!("cannot open {}", self.kmsg_file))?; | |||
let mut oo = OpenOptions::new(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please rename this variable
oo isn't clear
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
renamed as open_options
it would be nice to have tests |
d34cd72
to
358f905
Compare
In order to properly read from /dev/kmsg, we need [*]: 1) open /dev/kmsg with O_NONBLOCK 2) handle the EAGAIN/WouldBlock error as end of records 3) treat '\n' (and not '\0') as record separator. 4) do lseek(fd, 0, SEEK_DATA) Because Windows doesn't support O_NONBLOCK and SEEK_DATA, we had protect these code with #[cfg(not(target_os = "windows"))]. Moreover because Windows doesn't have /dev/kmsg, it is mandatory to use the '-K' switch. [*] https://www.kernel.org/doc/Documentation/ABI/testing/dev-kmsg
358f905
to
193f0c0
Compare
I updated tests in order to avoid failing in windows. However I don't know how can test a correct /dev/kmsg reading.
Where 1) and 2) will not be able to test O_NONBLOCK and WouldBlock |
e2672ac
to
5cfa874
Compare
The original dmesg, uses the -F/--file options to read from a file instead of /dev/kmsg. But this would be another patches set. |
Can you please run |
Because windows build complain if we don't pass a valid -K kmsg_file, pass a kmsg_file in every tests.
5cfa874
to
a32aff7
Compare
I should have correct the last CI warnings |
When I tried to use rust
dmesg
program, I found that it hangs during a read of/dev/kmsg
. Usingstrace
I found few errors.In order to properly read from /dev/kmsg, 3 things are needed [*]:
[*] https://www.kernel.org/doc/Documentation/ABI/testing/dev-kmsg