[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20241213134715.601415-6-lee@kernel.org>
Date: Fri, 13 Dec 2024 13:47:10 +0000
From: Lee Jones <lee@...nel.org>
To: lee@...nel.org
Cc: linux-kernel@...r.kernel.org,
arnd@...db.de,
gregkh@...uxfoundation.org,
ojeda@...nel.org,
alex.gaynor@...il.com,
boqun.feng@...il.com,
gary@...yguo.net,
bjorn3_gh@...tonmail.com,
benno.lossin@...ton.me,
a.hindborg@...nel.org,
aliceryhl@...gle.com,
tmgross@...ch.edu,
rust-for-linux@...r.kernel.org
Subject: [PATCH v6 5/5] samples: rust_misc_device: Provide an example C program to exercise functionality
Here is the expected output (manually spliced together):
USERSPACE: Opening /dev/rust-misc-device for reading and writing
KERNEL: rust_misc_device: Opening Rust Misc Device Sample
USERSPACE: Calling Hello
KERNEL: rust_misc_device: IOCTLing Rust Misc Device Sample
KERNEL: rust_misc_device: -> Hello from the Rust Misc Device
USERSPACE: Fetching initial value
KERNEL: rust_misc_device: IOCTLing Rust Misc Device Sample
KERNEL: rust_misc_device: -> Copying data to userspace (value: 0)
USERSPACE: Submitting new value (1)
KERNEL: rust_misc_device: IOCTLing Rust Misc Device Sample
KERNEL: rust_misc_device: -> Copying data from userspace (value: 1)
USERSPACE: Fetching new value
KERNEL: rust_misc_device: IOCTLing Rust Misc Device Sample
KERNEL: rust_misc_device: -> Copying data to userspace (value: 1)
USERSPACE: Attempting to call in to an non-existent IOCTL
KERNEL: rust_misc_device: IOCTLing Rust Misc Device Sample
KERNEL: rust_misc_device: -> IOCTL not recognised: 20992
USERSPACE: ioctl: Succeeded to fail - this was expected: Inappropriate ioctl for device
USERSPACE: Closing /dev/rust-misc-device
KERNEL: rust_misc_device: Exiting the Rust Misc Device Sample
USERSPACE: Success
Signed-off-by: Lee Jones <lee@...nel.org>
---
samples/rust/rust_misc_device.rs | 90 ++++++++++++++++++++++++++++++++
1 file changed, 90 insertions(+)
diff --git a/samples/rust/rust_misc_device.rs b/samples/rust/rust_misc_device.rs
index ae1474a451f1..40ad7266c225 100644
--- a/samples/rust/rust_misc_device.rs
+++ b/samples/rust/rust_misc_device.rs
@@ -4,6 +4,96 @@
//! Rust misc device sample.
+/// Below is an example userspace C program that exercises this sample's functionality.
+///
+/// ```c
+/// #include <stdio.h>
+/// #include <stdlib.h>
+/// #include <errno.h>
+/// #include <fcntl.h>
+/// #include <unistd.h>
+/// #include <sys/ioctl.h>
+///
+/// #define RUST_MISC_DEV_FAIL _IO('|', 0)
+/// #define RUST_MISC_DEV_HELLO _IO('|', 0x80)
+/// #define RUST_MISC_DEV_GET_VALUE _IOR('|', 0x81, int)
+/// #define RUST_MISC_DEV_SET_VALUE _IOW('|', 0x82, int)
+///
+/// int main() {
+/// int value, new_value;
+/// int fd, ret;
+///
+/// // Open the device file
+/// printf("Opening /dev/rust-misc-device for reading and writing\n");
+/// fd = open("/dev/rust-misc-device", O_RDWR);
+/// if (fd < 0) {
+/// perror("open");
+/// return errno;
+/// }
+///
+/// // Make call into driver to say "hello"
+/// printf("Calling Hello\n");
+/// ret = ioctl(fd, RUST_MISC_DEV_HELLO, NULL);
+/// if (ret < 0) {
+/// perror("ioctl: Failed to call into Hello");
+/// close(fd);
+/// return errno;
+/// }
+///
+/// // Get initial value
+/// printf("Fetching initial value\n");
+/// ret = ioctl(fd, RUST_MISC_DEV_GET_VALUE, &value);
+/// if (ret < 0) {
+/// perror("ioctl: Failed to fetch the initial value");
+/// close(fd);
+/// return errno;
+/// }
+///
+/// value++;
+///
+/// // Set value to something different
+/// printf("Submitting new value (%d)\n", value);
+/// ret = ioctl(fd, RUST_MISC_DEV_SET_VALUE, &value);
+/// if (ret < 0) {
+/// perror("ioctl: Failed to submit new value");
+/// close(fd);
+/// return errno;
+/// }
+///
+/// // Ensure new value was applied
+/// printf("Fetching new value\n");
+/// ret = ioctl(fd, RUST_MISC_DEV_GET_VALUE, &new_value);
+/// if (ret < 0) {
+/// perror("ioctl: Failed to fetch the new value");
+/// close(fd);
+/// return errno;
+/// }
+///
+/// if (value != new_value) {
+/// printf("Failed: Committed and retrieved values are different (%d - %d)\n", value, new_value);
+/// close(fd);
+/// return -1;
+/// }
+///
+/// // Call the unsuccessful ioctl
+/// printf("Attempting to call in to an non-existent IOCTL\n");
+/// ret = ioctl(fd, RUST_MISC_DEV_FAIL, NULL);
+/// if (ret < 0) {
+/// perror("ioctl: Succeeded to fail - this was expected");
+/// } else {
+/// printf("ioctl: Failed to fail\n");
+/// close(fd);
+/// return -1;
+/// }
+///
+/// // Close the device file
+/// printf("Closing /dev/rust-misc-device\n");
+/// close(fd);
+///
+/// printf("Success\n");
+/// return 0;
+/// }
+/// ```
use core::pin::Pin;
use kernel::{
--
2.47.1.613.gc27f4b7a9f-goog
Powered by blists - more mailing lists