diff --git a/user/apps/test-uevent/.cargo/config.toml b/user/apps/test-uevent/.cargo/config.toml deleted file mode 100644 index c1ca86b1..00000000 --- a/user/apps/test-uevent/.cargo/config.toml +++ /dev/null @@ -1,2 +0,0 @@ -[build] -target = "x86_64-unknown-linux-musl" \ No newline at end of file diff --git a/user/apps/test-uevent/.gitignore b/user/apps/test-uevent/.gitignore deleted file mode 100644 index 1ac35461..00000000 --- a/user/apps/test-uevent/.gitignore +++ /dev/null @@ -1,3 +0,0 @@ -/target -Cargo.lock -/install/ \ No newline at end of file diff --git a/user/apps/test-uevent/Cargo.toml b/user/apps/test-uevent/Cargo.toml deleted file mode 100644 index 68597ed3..00000000 --- a/user/apps/test-uevent/Cargo.toml +++ /dev/null @@ -1,12 +0,0 @@ -[package] -name = "test-uevent" -version = "0.1.0" -edition = "2021" -description = "test for uevent" -authors = [ "val213 " ] - -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - -[dependencies] -netlink-sys = "0.5" -nix = "0.24" diff --git a/user/apps/test-uevent/Makefile b/user/apps/test-uevent/Makefile deleted file mode 100644 index 1b0274d2..00000000 --- a/user/apps/test-uevent/Makefile +++ /dev/null @@ -1,56 +0,0 @@ -TOOLCHAIN="+nightly-2023-08-15-x86_64-unknown-linux-gnu" -RUSTFLAGS+="" - -ifdef DADK_CURRENT_BUILD_DIR -# 如果是在dadk中编译,那么安装到dadk的安装目录中 - INSTALL_DIR = $(DADK_CURRENT_BUILD_DIR) -else -# 如果是在本地编译,那么安装到当前目录下的install目录中 - INSTALL_DIR = ./install -endif - -ifeq ($(ARCH), x86_64) - export RUST_TARGET=x86_64-unknown-linux-musl -else ifeq ($(ARCH), riscv64) - export RUST_TARGET=riscv64gc-unknown-linux-gnu -else -# 默认为x86_86,用于本地编译 - export RUST_TARGET=x86_64-unknown-linux-musl -endif - -run: - RUSTFLAGS=$(RUSTFLAGS) cargo $(TOOLCHAIN) run --target $(RUST_TARGET) - -build: - RUSTFLAGS=$(RUSTFLAGS) cargo $(TOOLCHAIN) build --target $(RUST_TARGET) - -clean: - RUSTFLAGS=$(RUSTFLAGS) cargo $(TOOLCHAIN) clean --target $(RUST_TARGET) - -test: - RUSTFLAGS=$(RUSTFLAGS) cargo $(TOOLCHAIN) test --target $(RUST_TARGET) - -doc: - RUSTFLAGS=$(RUSTFLAGS) cargo $(TOOLCHAIN) doc --target $(RUST_TARGET) - -fmt: - RUSTFLAGS=$(RUSTFLAGS) cargo $(TOOLCHAIN) fmt - -fmt-check: - RUSTFLAGS=$(RUSTFLAGS) cargo $(TOOLCHAIN) fmt --check - -run-release: - RUSTFLAGS=$(RUSTFLAGS) cargo $(TOOLCHAIN) run --target $(RUST_TARGET) --release - -build-release: - RUSTFLAGS=$(RUSTFLAGS) cargo $(TOOLCHAIN) build --target $(RUST_TARGET) --release - -clean-release: - RUSTFLAGS=$(RUSTFLAGS) cargo $(TOOLCHAIN) clean --target $(RUST_TARGET) --release - -test-release: - RUSTFLAGS=$(RUSTFLAGS) cargo $(TOOLCHAIN) test --target $(RUST_TARGET) --release - -.PHONY: install -install: - RUSTFLAGS=$(RUSTFLAGS) cargo $(TOOLCHAIN) install --target $(RUST_TARGET) --path . --no-track --root $(INSTALL_DIR) --force diff --git a/user/apps/test-uevent/README.md b/user/apps/test-uevent/README.md deleted file mode 100644 index c24d8cc9..00000000 --- a/user/apps/test-uevent/README.md +++ /dev/null @@ -1,4 +0,0 @@ -# test_uevent -这是一个测试 uevent 机制的应用,用于测试uevent的功能。 - -执行此测试,将会执行以下操作: diff --git a/user/apps/test-uevent/src/main.rs b/user/apps/test-uevent/src/main.rs deleted file mode 100644 index d76b5727..00000000 --- a/user/apps/test-uevent/src/main.rs +++ /dev/null @@ -1,166 +0,0 @@ -use libc::{sockaddr, recvfrom, bind, sendto, socket, AF_NETLINK, SOCK_DGRAM, getpid, c_void}; -use nix::libc; -use std::os::unix::io::RawFd; -use std::{io, mem}; - -#[repr(C)] -struct Nlmsghdr { - nlmsg_len: u32, - nlmsg_type: u16, - nlmsg_flags: u16, - nlmsg_seq: u32, - nlmsg_pid: u32, -} - -fn create_netlink_socket() -> io::Result { - let sockfd = unsafe { - socket(AF_NETLINK, SOCK_DGRAM, libc::NETLINK_KOBJECT_UEVENT) - }; - - if sockfd < 0 { - println!("Error: {}", io::Error::last_os_error()); - return Err(io::Error::last_os_error()); - } - - Ok(sockfd) -} - -fn bind_netlink_socket(sock: RawFd) -> io::Result<()> { - let pid = unsafe { getpid() }; - let mut addr: libc::sockaddr_nl = unsafe { mem::zeroed() }; - addr.nl_family = AF_NETLINK as u16; - addr.nl_pid = pid as u32; - addr.nl_groups = 1; - - let ret = unsafe { - bind( - sock, - &addr as *const _ as *const sockaddr, - mem::size_of::() as u32, - ) - }; - - if ret < 0 { - println!("Error: {}", io::Error::last_os_error()); - return Err(io::Error::last_os_error()); - } - - Ok(()) -} - -fn send_uevent(sock: RawFd, message: &str) -> io::Result<()> { - let mut addr: libc::sockaddr_nl = unsafe { mem::zeroed() }; - addr.nl_family = AF_NETLINK as u16; - addr.nl_pid = 0; - addr.nl_groups = 0; - - let nlmsghdr = Nlmsghdr { - nlmsg_len: (mem::size_of::() + message.len()) as u32, - nlmsg_type: 0, - nlmsg_flags: 0, - nlmsg_seq: 0, - nlmsg_pid: 0, - }; - - let mut buffer = Vec::with_capacity(nlmsghdr.nlmsg_len as usize); - buffer.extend_from_slice(unsafe { - std::slice::from_raw_parts( - &nlmsghdr as *const Nlmsghdr as *const u8, - mem::size_of::(), - ) - }); - buffer.extend_from_slice(message.as_bytes()); - - let ret = unsafe { - sendto( - sock, - buffer.as_ptr() as *const c_void, - buffer.len(), - 0, - &addr as *const _ as *const sockaddr, - mem::size_of::() as u32, - ) - }; - - if ret < 0 { - println!("Error: {}", io::Error::last_os_error()); - return Err(io::Error::last_os_error()); - } - - Ok(()) -} - -fn receive_uevent(sock: RawFd) -> io::Result { - // 检查套接字文件描述符是否有效 - if sock < 0 { - println!("Invalid socket file descriptor: {}", sock); - return Err(io::Error::new( - io::ErrorKind::InvalidInput, - "Invalid socket file descriptor", - )); - } - - let mut buf = [0u8; 1024]; - // let mut addr: sockaddr_storage = unsafe { mem::zeroed() }; - // let mut addr_len = mem::size_of::() as u32; - - // 检查缓冲区指针和长度是否有效 - if buf.is_empty() { - println!("Buffer is empty"); - return Err(io::Error::new( - io::ErrorKind::InvalidInput, - "Buffer is empty", - )); - } - let len = unsafe { - recvfrom( - sock, - buf.as_mut_ptr() as *mut c_void, - buf.len(), - 0, - core::ptr::null_mut(), // 不接收发送方地址 - core::ptr::null_mut(), // 不接收发送方地址长度 - ) - }; - println!("Received {} bytes", len); - println!("Received message: {:?}", &buf[..len as usize]); - if len < 0 { - println!("Error: {}", io::Error::last_os_error()); - return Err(io::Error::last_os_error()); - } - - let nlmsghdr_size = mem::size_of::(); - if (len as usize) < nlmsghdr_size { - println!("Received message is too short"); - return Err(io::Error::new( - io::ErrorKind::InvalidData, - "Received message is too short", - )); - } - - let nlmsghdr = unsafe { &*(buf.as_ptr() as *const Nlmsghdr) }; - if nlmsghdr.nlmsg_len as isize > len { - println!("Received message is incomplete"); - return Err(io::Error::new( - io::ErrorKind::InvalidData, - "Received message is incomplete", - )); - } - - let message_data = &buf[nlmsghdr_size..nlmsghdr.nlmsg_len as usize]; - Ok(String::from_utf8_lossy(message_data).to_string()) -} - -fn main() { - let socket = create_netlink_socket().expect("Failed to create Netlink socket"); - println!("Netlink socket created successfully"); - - bind_netlink_socket(socket).expect("Failed to bind Netlink socket"); - println!("Netlink socket created and bound successfully"); - - send_uevent(socket, "add@/devices/virtual/block/loop0").expect("Failed to send uevent message"); - println!("Custom uevent message sent successfully"); - - let message = receive_uevent(socket).expect("Failed to receive uevent message"); - println!("Received uevent message: {}", message); -} diff --git a/user/dadk/config/test_uevent_0_1_0.dadk b/user/dadk/config/test_uevent_0_1_0.dadk deleted file mode 100644 index 0d9b74bb..00000000 --- a/user/dadk/config/test_uevent_0_1_0.dadk +++ /dev/null @@ -1,29 +0,0 @@ -{ - "name": "test-uevent", - "version": "0.1.0", - "description": "", - "rust_target": null, - "task_type": { - "BuildFromSource": { - "Local": { - "path": "apps/test-uevent" - } - } - }, - "depends": [], - "build": { - "build_command": "make install" - }, - "install": { - "in_dragonos_path": "/" - }, - "clean": { - "clean_command": "make clean" - }, - "envs": [], - "build_once": false, - "install_once": false, - "target_arch": [ - "x86_64" - ] -} \ No newline at end of file