Experiment with NETLINK socket from Rust
This commit is contained in:
parent
d03d249701
commit
fe2d36b73c
|
@ -0,0 +1 @@
|
|||
target/
|
|
@ -0,0 +1,62 @@
|
|||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "autocfg"
|
||||
version = "1.0.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a"
|
||||
|
||||
[[package]]
|
||||
name = "bitflags"
|
||||
version = "1.3.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2da1976d75adbe5fbc88130ecd119529cf1cc6a93ae1546d8696ee66f0d21af1"
|
||||
|
||||
[[package]]
|
||||
name = "cc"
|
||||
version = "1.0.69"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e70cc2f62c6ce1868963827bd677764c62d07c3d9a3e1fb1177ee1a9ab199eb2"
|
||||
|
||||
[[package]]
|
||||
name = "cfg-if"
|
||||
version = "1.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
||||
|
||||
[[package]]
|
||||
name = "explore-kobject-uevent"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"nix",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "libc"
|
||||
version = "0.2.99"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a7f823d141fe0a24df1e23b4af4e3c7ba9e5966ec514ea068c93024aa7deb765"
|
||||
|
||||
[[package]]
|
||||
name = "memoffset"
|
||||
version = "0.6.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "59accc507f1338036a0477ef61afdae33cde60840f4dfe481319ce3ad116ddf9"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "nix"
|
||||
version = "0.22.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "cf1e25ee6b412c2a1e3fcb6a4499a5c1bfe7f43e014bdce9a6b6666e5aa2d187"
|
||||
dependencies = [
|
||||
"bitflags",
|
||||
"cc",
|
||||
"cfg-if",
|
||||
"libc",
|
||||
"memoffset",
|
||||
]
|
|
@ -0,0 +1,10 @@
|
|||
[package]
|
||||
name = "explore-kobject-uevent"
|
||||
version = "0.1.0"
|
||||
edition = "2018"
|
||||
|
||||
[profile.release]
|
||||
lto = true
|
||||
|
||||
[dependencies]
|
||||
nix = "^0.22"
|
|
@ -0,0 +1,34 @@
|
|||
use nix::sys::socket;
|
||||
use nix::sys::uio::IoVec;
|
||||
|
||||
use std::io;
|
||||
|
||||
fn main() -> io::Result<()> {
|
||||
let sock = socket::socket(socket::AddressFamily::Netlink,
|
||||
socket::SockType::Raw,
|
||||
socket::SockFlag::SOCK_CLOEXEC,
|
||||
socket::SockProtocol::NetlinkKObjectUEvent)?;
|
||||
|
||||
let src = socket::SockAddr::new_netlink(0, 1 /* is this the group for kernel-announced events? */);
|
||||
socket::bind(sock, &src)?;
|
||||
|
||||
loop {
|
||||
println!("Waiting...");
|
||||
let mut buf = vec![0u8; 8192];
|
||||
let iov = [IoVec::from_mut_slice(&mut buf)];
|
||||
let mut cmsg = Vec::with_capacity(8192);
|
||||
let r = socket::recvmsg(sock, &iov, Some(&mut cmsg), socket::MsgFlags::MSG_CMSG_CLOEXEC)?;
|
||||
println!("\n{:#?}", r);
|
||||
for i in 0 .. r.bytes {
|
||||
let b = buf[i];
|
||||
if b == 0 {
|
||||
println!();
|
||||
} else if b >= 32 && b < 128 {
|
||||
print!("{}", b as char);
|
||||
} else {
|
||||
print!("\\x{:02x}", b);
|
||||
}
|
||||
}
|
||||
println!();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue