Skip to content

Commit f8d2e37

Browse files
committed
Use ioctl numbers from C libs
Generate the desired ioctl numbers are compile time using the native macros and compare to the ones generated by our own macros.
1 parent 44ed4c0 commit f8d2e37

File tree

5 files changed

+137
-125
lines changed

5 files changed

+137
-125
lines changed

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ bitflags = "0.9"
2121
cfg-if = "0.1.0"
2222
void = "1.0.2"
2323

24+
[build-dependencies]
25+
gcc = "0.3"
26+
2427
[dev-dependencies]
2528
lazy_static = "0.2"
2629
rand = "0.3.8"

build.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
extern crate gcc;
2+
3+
fn main() {
4+
gcc::Config::new()
5+
.file("ioctls.c")
6+
.compile("libioctls.a");
7+
}

ioctls.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include <sys/ioctl.h>
2+
#include <stdint.h>
3+
4+
uint64_t io_a_255() {
5+
return _IO('a', 255);
6+
}
7+
8+
uint64_t io_q_10() {
9+
return _IO('q', 10);
10+
}

src/sys/ioctl/mod.rs

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,3 +318,120 @@ macro_rules! ioctl {
318318
}
319319
);
320320
}
321+
322+
mod test {
323+
pub use super::ioctl_num_type as ioctl_num_type;
324+
325+
extern {
326+
fn io_q_10() -> u64();
327+
fn io_a_255() -> u64();
328+
}
329+
330+
#[test]
331+
fn test_io() {
332+
assert_eq!(io!(b'q', 10), unsafe { io_q_10() } as ioctl_num_type);
333+
assert_eq!(io!(b'a', 255), unsafe { io_a_255() } as ioctl_num_type);
334+
}
335+
}
336+
337+
#[cfg(any(target_os = "linux", target_os = "android"))]
338+
mod linux {
339+
#[test]
340+
fn test_op_write() {
341+
if cfg!(any(target_arch = "mips", target_arch="powerpc", target_arch="powerpc64")){
342+
assert_eq!(iow!(b'z', 10, 1), 0x80017A0A);
343+
assert_eq!(iow!(b'z', 10, 512), 0x82007A0A);
344+
} else {
345+
assert_eq!(iow!(b'z', 10, 1), 0x40017A0A);
346+
assert_eq!(iow!(b'z', 10, 512), 0x42007A0A);
347+
}
348+
}
349+
350+
#[cfg(target_pointer_width = "64")]
351+
#[test]
352+
fn test_op_write_64() {
353+
if cfg!(any(target_arch="powerpc64")){
354+
assert_eq!(iow!(b'z', 10, (1 as u64) << 32), 0x80007A0A);
355+
} else {
356+
assert_eq!(iow!(b'z', 10, (1 as u64) << 32), 0x40007A0A);
357+
}
358+
359+
}
360+
361+
#[test]
362+
fn test_op_read() {
363+
if cfg!(any(target_arch = "mips", target_arch="powerpc", target_arch="powerpc64")){
364+
assert_eq!(ior!(b'z', 10, 1), 0x40017A0A);
365+
assert_eq!(ior!(b'z', 10, 512), 0x42007A0A);
366+
} else {
367+
assert_eq!(ior!(b'z', 10, 1), 0x80017A0A);
368+
assert_eq!(ior!(b'z', 10, 512), 0x82007A0A);
369+
}
370+
}
371+
372+
#[cfg(target_pointer_width = "64")]
373+
#[test]
374+
fn test_op_read_64() {
375+
if cfg!(any(target_arch="powerpc64")){
376+
assert_eq!(ior!(b'z', 10, (1 as u64) << 32), 0x40007A0A);
377+
} else {
378+
assert_eq!(ior!(b'z', 10, (1 as u64) << 32), 0x80007A0A);
379+
}
380+
}
381+
382+
#[test]
383+
fn test_op_read_write() {
384+
assert_eq!(iorw!(b'z', 10, 1), 0xC0017A0A);
385+
assert_eq!(iorw!(b'z', 10, 512), 0xC2007A0A);
386+
}
387+
388+
#[cfg(target_pointer_width = "64")]
389+
#[test]
390+
fn test_op_read_write_64() {
391+
assert_eq!(iorw!(b'z', 10, (1 as u64) << 32), 0xC0007A0A);
392+
}
393+
}
394+
395+
#[cfg(any(target_os = "macos",
396+
target_os = "ios",
397+
target_os = "netbsd",
398+
target_os = "openbsd",
399+
target_os = "freebsd",
400+
target_os = "dragonfly"))]
401+
mod bsd {
402+
#[test]
403+
fn test_op_write() {
404+
assert_eq!(iow!(b'z', 10, 1), 0x80017A0A);
405+
assert_eq!(iow!(b'z', 10, 512), 0x82007A0A);
406+
}
407+
408+
#[cfg(target_pointer_width = "64")]
409+
#[test]
410+
fn test_op_write_64() {
411+
assert_eq!(iow!(b'z', 10, (1 as u64) << 32), 0x80007A0A);
412+
}
413+
414+
#[test]
415+
fn test_op_read() {
416+
assert_eq!(ior!(b'z', 10, 1), 0x40017A0A);
417+
assert_eq!(ior!(b'z', 10, 512), 0x42007A0A);
418+
}
419+
420+
#[cfg(target_pointer_width = "64")]
421+
#[test]
422+
fn test_op_read_64() {
423+
assert_eq!(ior!(b'z', 10, (1 as u64) << 32), 0x40007A0A);
424+
}
425+
426+
#[test]
427+
fn test_op_read_write() {
428+
assert_eq!(iorw!(b'z', 10, 1), 0xC0017A0A);
429+
assert_eq!(iorw!(b'z', 10, 512), 0xC2007A0A);
430+
}
431+
432+
#[cfg(target_pointer_width = "64")]
433+
#[test]
434+
fn test_op_read_write_64() {
435+
assert_eq!(iorw!(b'z', 10, (1 as u64) << 32), 0xC0007A0A);
436+
}
437+
}

test/sys/test_ioctl.rs

Lines changed: 0 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -160,128 +160,3 @@ mod linux_ioctls {
160160

161161
// FIXME: Find a suitable example for readwrite_buf.
162162
}
163-
164-
// See C code for source of values for op calculations (does NOT work for mips/powerpc):
165-
// https://gist.github.com/posborne/83ea6880770a1aef332e
166-
//
167-
// TODO: Need a way to compute these constants at test time. Using precomputed
168-
// values is fragile and needs to be maintained.
169-
170-
#[cfg(any(target_os = "linux", target_os = "android"))]
171-
mod linux {
172-
#[test]
173-
fn test_op_none() {
174-
if cfg!(any(target_arch = "mips", target_arch="powerpc", target_arch="powerpc64")){
175-
assert_eq!(io!(b'q', 10), 0x2000710A);
176-
assert_eq!(io!(b'a', 255), 0x200061FF);
177-
} else {
178-
assert_eq!(io!(b'q', 10), 0x0000710A);
179-
assert_eq!(io!(b'a', 255), 0x000061FF);
180-
}
181-
}
182-
183-
#[test]
184-
fn test_op_write() {
185-
if cfg!(any(target_arch = "mips", target_arch="powerpc", target_arch="powerpc64")){
186-
assert_eq!(iow!(b'z', 10, 1), 0x80017A0A);
187-
assert_eq!(iow!(b'z', 10, 512), 0x82007A0A);
188-
} else {
189-
assert_eq!(iow!(b'z', 10, 1), 0x40017A0A);
190-
assert_eq!(iow!(b'z', 10, 512), 0x42007A0A);
191-
}
192-
}
193-
194-
#[cfg(target_pointer_width = "64")]
195-
#[test]
196-
fn test_op_write_64() {
197-
if cfg!(any(target_arch="powerpc64")){
198-
assert_eq!(iow!(b'z', 10, (1 as u64) << 32), 0x80007A0A);
199-
} else {
200-
assert_eq!(iow!(b'z', 10, (1 as u64) << 32), 0x40007A0A);
201-
}
202-
203-
}
204-
205-
#[test]
206-
fn test_op_read() {
207-
if cfg!(any(target_arch = "mips", target_arch="powerpc", target_arch="powerpc64")){
208-
assert_eq!(ior!(b'z', 10, 1), 0x40017A0A);
209-
assert_eq!(ior!(b'z', 10, 512), 0x42007A0A);
210-
} else {
211-
assert_eq!(ior!(b'z', 10, 1), 0x80017A0A);
212-
assert_eq!(ior!(b'z', 10, 512), 0x82007A0A);
213-
}
214-
}
215-
216-
#[cfg(target_pointer_width = "64")]
217-
#[test]
218-
fn test_op_read_64() {
219-
if cfg!(any(target_arch="powerpc64")){
220-
assert_eq!(ior!(b'z', 10, (1 as u64) << 32), 0x40007A0A);
221-
} else {
222-
assert_eq!(ior!(b'z', 10, (1 as u64) << 32), 0x80007A0A);
223-
}
224-
}
225-
226-
#[test]
227-
fn test_op_read_write() {
228-
assert_eq!(iorw!(b'z', 10, 1), 0xC0017A0A);
229-
assert_eq!(iorw!(b'z', 10, 512), 0xC2007A0A);
230-
}
231-
232-
#[cfg(target_pointer_width = "64")]
233-
#[test]
234-
fn test_op_read_write_64() {
235-
assert_eq!(iorw!(b'z', 10, (1 as u64) << 32), 0xC0007A0A);
236-
}
237-
}
238-
239-
#[cfg(any(target_os = "macos",
240-
target_os = "ios",
241-
target_os = "netbsd",
242-
target_os = "openbsd",
243-
target_os = "freebsd",
244-
target_os = "dragonfly"))]
245-
mod bsd {
246-
#[test]
247-
fn test_op_none() {
248-
assert_eq!(io!(b'q', 10), 0x2000710A);
249-
assert_eq!(io!(b'a', 255), 0x200061FF);
250-
}
251-
252-
#[test]
253-
fn test_op_write() {
254-
assert_eq!(iow!(b'z', 10, 1), 0x80017A0A);
255-
assert_eq!(iow!(b'z', 10, 512), 0x82007A0A);
256-
}
257-
258-
#[cfg(target_pointer_width = "64")]
259-
#[test]
260-
fn test_op_write_64() {
261-
assert_eq!(iow!(b'z', 10, (1 as u64) << 32), 0x80007A0A);
262-
}
263-
264-
#[test]
265-
fn test_op_read() {
266-
assert_eq!(ior!(b'z', 10, 1), 0x40017A0A);
267-
assert_eq!(ior!(b'z', 10, 512), 0x42007A0A);
268-
}
269-
270-
#[cfg(target_pointer_width = "64")]
271-
#[test]
272-
fn test_op_read_64() {
273-
assert_eq!(ior!(b'z', 10, (1 as u64) << 32), 0x40007A0A);
274-
}
275-
276-
#[test]
277-
fn test_op_read_write() {
278-
assert_eq!(iorw!(b'z', 10, 1), 0xC0017A0A);
279-
assert_eq!(iorw!(b'z', 10, 512), 0xC2007A0A);
280-
}
281-
282-
#[cfg(target_pointer_width = "64")]
283-
#[test]
284-
fn test_op_read_write_64() {
285-
assert_eq!(iorw!(b'z', 10, (1 as u64) << 32), 0xC0007A0A);
286-
}
287-
}

0 commit comments

Comments
 (0)