From a43613ac779a27b7299ab4d0f081638fe9b82faf Mon Sep 17 00:00:00 2001 From: Paul Osborne Date: Sat, 9 May 2015 01:57:16 -0500 Subject: ioctl: implement generic support for the ioctl syscall and supporting functions This commit provides a new implementation for ioctl that is much more generic, allowing for clients to use send any manner of ioctl requests at special files. The implementation provides two main features that help to raise the level of abstraction over that provided by libc. 1. The module now provides functions that provide the same functionality as the linux kernel _IO* macros. These are used frequently in the linux kernel for building ops for ioctls. The use of these helper functions are not required. 2. Functions are provided for the 3 main types of ioctl usage patterns (read, write, and execute). For many subystems, the read() call which returns a Result and the write calls taking a &T provide a nice interface. All of the methods wrapping ioctl are unsafe and will probably need to remain that way unless knowledge of the semantics of every possible ioctl call are added to the nix library. The best that exists for ioctls are some conventions around the op, but even these conventions are really only used for newer devices added to the kernel. This change resolves #108 --- test/sys/test_ioctl.rs | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 test/sys/test_ioctl.rs (limited to 'test/sys/test_ioctl.rs') diff --git a/test/sys/test_ioctl.rs b/test/sys/test_ioctl.rs new file mode 100644 index 00000000..2654bd81 --- /dev/null +++ b/test/sys/test_ioctl.rs @@ -0,0 +1,31 @@ +use nix::sys::ioctl::*; + +// See C code for source of values for op calculations: +// https://gist.github.com/posborne/83ea6880770a1aef332e + +#[test] +fn test_op_none() { + assert_eq!(op_none('q' as u8, 10), 0x0000710A); + assert_eq!(op_none('a' as u8, 255), 0x000061FF); +} + +#[test] +fn test_op_write() { + assert_eq!(op_write('z' as u8, 10, 1), 0x40017A0A); + assert_eq!(op_write('z' as u8, 10, 512), 0x42007A0A); + assert_eq!(op_write('z' as u8, 10, 1 << 32), 0x40007A0A); +} + +#[test] +fn test_op_read() { + assert_eq!(op_read('z' as u8, 10, 1), 0x80017A0A); + assert_eq!(op_read('z' as u8, 10, 512), 0x82007A0A); + assert_eq!(op_read('z' as u8, 10, 1 << 32), 0x80007A0A); +} + +#[test] +fn test_op_read_write() { + assert_eq!(op_read_write('z' as u8, 10, 1), 0xC0017A0A); + assert_eq!(op_read_write('z' as u8, 10, 512), 0xC2007A0A); + assert_eq!(op_read_write('z' as u8, 10, 1 << 32), 0xC0007A0A); +} -- cgit v1.2.3