From nobody Tue Dec 16 09:31:16 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1320C1E1C07; Fri, 13 Dec 2024 13:47:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734097676; cv=none; b=enAPHqps1q8AkekEeH0Fd0hBNTkXElmGRkpH+XQcouvCiHwL7H54Mb3pWijzoai2hzkDvJa4ToLUTtIb0LGt5lyeZqNdVcES8OX9hOMtBJR/RSp3/p3Q0zcj8voq64wXOtZlE3f8VxKVrPSDhJuJEIt3HfQTzCgKX9n//NvKW74= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734097676; c=relaxed/simple; bh=2Cpd5nmMdpE1QJyo7HkmRlJyt5ZRsTgDLqFZwGiXn2M=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=R4m0vGGvVdkNvyE7GpH1vSrC+1FVjB2xTlOFEwO2cwFe7phqNzaIEJnjejPvgeXj3k2n5WqEfKQ2NRFBYdZL6zkoLQlpJa1i+1EsWHGfSq9OP6NG8ZlzIrlhqXEE0kLAS57P4KU8tppXooBL2xuw30XneFg3jALe9a1cpoNMUtg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Z1yQALhO; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="Z1yQALhO" Received: by smtp.kernel.org (Postfix) with ESMTPSA id CB6CBC4CEDE; Fri, 13 Dec 2024 13:47:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1734097675; bh=2Cpd5nmMdpE1QJyo7HkmRlJyt5ZRsTgDLqFZwGiXn2M=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Z1yQALhOOLQ8LMgGi1jrflJxFbg3+XxKnUOMVWKz3aWRZPrHeCFUR93YW4d1XFVLZ rX0X5SLIZDBt0K5r5HJVe+RNE0Jivaes42dRSW0wp7p4vDJc6j1o/BEVpkEpMCTPFs n/J3upUp8HWnq44WpFEWC3Go66Ztkcc9uAD+LiwkYiA8lWJVjBMv2C0Vwo64sM5MhK TKnwuwhy4n1wLnl/c1ScTORP94i3x4pKK5+RNohMNDlZpG3CjkYZgOpY6iCsDiuhHa TpC66GbNvWofEmQNdJitUdvV1wnbstkxqmkZroGTklH/KRfcopiN+z4Qtt71NVyVpS zzXcxLo3IaFTA== From: Lee Jones To: lee@kernel.org Cc: linux-kernel@vger.kernel.org, arnd@arndb.de, gregkh@linuxfoundation.org, ojeda@kernel.org, alex.gaynor@gmail.com, boqun.feng@gmail.com, gary@garyguo.net, bjorn3_gh@protonmail.com, benno.lossin@proton.me, a.hindborg@kernel.org, aliceryhl@google.com, tmgross@umich.edu, rust-for-linux@vger.kernel.org Subject: [PATCH v6 5/5] samples: rust_misc_device: Provide an example C program to exercise functionality Date: Fri, 13 Dec 2024 13:47:10 +0000 Message-ID: <20241213134715.601415-6-lee@kernel.org> X-Mailer: git-send-email 2.47.1.613.gc27f4b7a9f-goog In-Reply-To: <20241213134715.601415-1-lee@kernel.org> References: <20241213134715.601415-1-lee@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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 ioct= l 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 --- 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_devi= ce.rs index ae1474a451f1..40ad7266c225 100644 --- a/samples/rust/rust_misc_device.rs +++ b/samples/rust/rust_misc_device.rs @@ -4,6 +4,96 @@ =20 //! Rust misc device sample. =20 +/// Below is an example userspace C program that exercises this sample's f= unctionality. +/// +/// ```c +/// #include +/// #include +/// #include +/// #include +/// #include +/// #include +/// +/// #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 =3D 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 =3D 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 =3D 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 =3D 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 =3D 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 !=3D 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 =3D 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; =20 use kernel::{ --=20 2.47.1.613.gc27f4b7a9f-goog