From: Daniel Sedlak <daniel@sedlak.dev>
To: Miguel Ojeda <ojeda@kernel.org>, Alex Gaynor <alex.gaynor@gmail.com>
Cc: "Boqun Feng" <boqun.feng@gmail.com>,
"Gary Guo" <gary@garyguo.net>,
"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
"Benno Lossin" <benno.lossin@proton.me>,
"Andreas Hindborg" <a.hindborg@kernel.org>,
"Alice Ryhl" <aliceryhl@google.com>,
"Trevor Gross" <tmgross@umich.edu>,
rust-for-linux@vger.kernel.org,
"Daniel Sedlak" <daniel@sedlak.dev>
Subject: [RFC PATCH 3/3] samples: rust: add kobject sample
Date: Sun, 8 Dec 2024 14:15:45 +0100 [thread overview]
Message-ID: <20241208131545.386897-4-daniel@sedlak.dev> (raw)
In-Reply-To: <20241208131545.386897-1-daniel@sedlak.dev>
Add basic example using the kobject API using Rust.
The example is similar to the already existing
examples samples/kobject/{kobject-example.c,kset-example.c}.
Signed-off-by: Daniel Sedlak <daniel@sedlak.dev>
---
samples/rust/Kconfig | 8 ++++
samples/rust/Makefile | 1 +
samples/rust/kobject_example.rs | 66 +++++++++++++++++++++++++++++++++
3 files changed, 75 insertions(+)
create mode 100644 samples/rust/kobject_example.rs
diff --git a/samples/rust/Kconfig b/samples/rust/Kconfig
index b0f74a81c8f9..75e6db1ba6c0 100644
--- a/samples/rust/Kconfig
+++ b/samples/rust/Kconfig
@@ -37,4 +37,12 @@ config SAMPLE_RUST_HOSTPROGS
If unsure, say N.
+config SAMPLE_RUST_KOBJECT
+ bool "Build kobject example in Rust"
+ help
+ This config option allows to build kobject
+ exampel written in Rust.
+
+ If unsure, say N.
+
endif # SAMPLES_RUST
diff --git a/samples/rust/Makefile b/samples/rust/Makefile
index c1a5c1655395..b3ae09ef2c24 100644
--- a/samples/rust/Makefile
+++ b/samples/rust/Makefile
@@ -3,6 +3,7 @@ ccflags-y += -I$(src) # needed for trace events
obj-$(CONFIG_SAMPLE_RUST_MINIMAL) += rust_minimal.o
obj-$(CONFIG_SAMPLE_RUST_PRINT) += rust_print.o
+obj-$(CONFIG_SAMPLE_RUST_KOBJECT) += kobject_example.o
rust_print-y := rust_print_main.o rust_print_events.o
diff --git a/samples/rust/kobject_example.rs b/samples/rust/kobject_example.rs
new file mode 100644
index 000000000000..90db937dea5a
--- /dev/null
+++ b/samples/rust/kobject_example.rs
@@ -0,0 +1,66 @@
+// SPDX-License-Identifier: GPL-2.0
+
+//! Rust example using kobjects.
+
+use kernel::{
+ c_str,
+ kobject::{subsystems, KObject, KObjectTextAttribute},
+ prelude::*,
+ str::CString,
+};
+
+module! {
+ type: RustKObject,
+ name: "rust_kobject",
+ author: "Rust for Linux Contributors",
+ license: "GPL",
+}
+
+struct RustKObject {
+ // This kobject may be referenced later by other parts of code to update
+ // the values stored within [`MyKObject`].
+ _kobject: Pin<KBox<KObject<MyKObject>>>,
+}
+
+/// Sample KObject state which creates directory in the `/sys/kernel/my_kobject`.
+#[derive(Default)]
+pub struct MyKObject {
+ text: Option<CString>,
+}
+
+/// Attribute of the [`MyKObject`], creates file in the `/sys/kernel/my_kobject/my-text`.
+/// Where `show()` is triggered when read is called and `store()` is called
+/// when new data are written.
+pub struct MyKObjectAttribute;
+#[vtable]
+impl KObjectTextAttribute<MyKObject> for MyKObjectAttribute {
+ const NAME: &'static CStr = c_str!("my-text");
+
+ /// Called for example by `cat /sys/kernel/my_kobject/my-text`.
+ fn show(this: &mut MyKObject) -> Result<CString> {
+ if let Some(text) = &this.text {
+ CString::try_from_fmt(fmt!("Text that was stored: '{text:?}'\n"))
+ } else {
+ CString::try_from_fmt(fmt!("No one stored anything yet, value {}.\n", this.value))
+ }
+ }
+
+ /// Called for example by `echo "Hi Rust!" > /sys/kernel/my_kobject/my-text`.
+ fn store(this: &mut MyKObject, input: &CStr) -> Result {
+ this.text = Some(input.to_cstring()?);
+ Ok(())
+ }
+}
+
+impl kernel::Module for RustKObject {
+ fn init(_module: &'static ThisModule) -> kernel::error::Result<Self> {
+ let mut kobject = KObject::new_with_dynamic_kobject_parent(
+ CString::try_from_fmt(fmt!("my_kobject"))?,
+ &subsystems::KERNEL_KOBJECT,
+ MyKObject::default(),
+ )?;
+ kobject.add_attribute(MyKObjectAttribute)?;
+
+ Ok(RustKObject { _kobject: kobject })
+ }
+}
--
2.47.1
next prev parent reply other threads:[~2024-12-08 13:16 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-12-08 13:15 [RFC PATCH 0/3] rust abstractions for interacting with sysfs Daniel Sedlak
2024-12-08 13:15 ` [RFC PATCH 1/3] rust: kernel: types: add mode wrapper Daniel Sedlak
2024-12-09 7:21 ` Alice Ryhl
2024-12-09 16:19 ` Daniel Sedlak
2024-12-08 13:15 ` [RFC PATCH 2/3] rust: kernel: kobject: basic sysfs implementation Daniel Sedlak
2024-12-08 13:43 ` Greg KH
2024-12-09 15:04 ` Daniel Sedlak
2024-12-08 13:56 ` Greg KH
2024-12-08 13:15 ` Daniel Sedlak [this message]
2024-12-08 13:46 ` [RFC PATCH 3/3] samples: rust: add kobject sample Greg KH
2024-12-09 15:17 ` Daniel Sedlak
2024-12-08 13:34 ` [RFC PATCH 0/3] rust abstractions for interacting with sysfs Greg KH
2024-12-09 16:12 ` Daniel Sedlak
2024-12-09 16:38 ` Greg KH
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20241208131545.386897-4-daniel@sedlak.dev \
--to=daniel@sedlak.dev \
--cc=a.hindborg@kernel.org \
--cc=alex.gaynor@gmail.com \
--cc=aliceryhl@google.com \
--cc=benno.lossin@proton.me \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun.feng@gmail.com \
--cc=gary@garyguo.net \
--cc=ojeda@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=tmgross@umich.edu \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.