Skip to content

Commit 35121f0

Browse files
committed
Use the proper ioctl number type depending on target
This also means that we need to properly mask off bits to the valid range of ioctl numbers.
1 parent 0425788 commit 35121f0

File tree

3 files changed

+42
-29
lines changed

3 files changed

+42
-29
lines changed

src/sys/ioctl/mod.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -174,63 +174,63 @@ macro_rules! ioctl {
174174
pub unsafe fn $name(fd: $crate::libc::c_int,
175175
data: *mut u8)
176176
-> $crate::Result<$crate::libc::c_int> {
177-
convert_ioctl_res!($crate::libc::ioctl(fd, $nr as $crate::libc::c_ulong, data))
177+
convert_ioctl_res!($crate::libc::ioctl(fd, $nr as $crate::sys::ioctl::ioctl_num_type, data))
178178
}
179179
);
180180
(bad none $name:ident with $nr:expr) => (
181181
pub unsafe fn $name(fd: $crate::libc::c_int)
182182
-> $crate::Result<$crate::libc::c_int> {
183-
convert_ioctl_res!($crate::libc::ioctl(fd, $nr as $crate::libc::c_ulong))
183+
convert_ioctl_res!($crate::libc::ioctl(fd, $nr as $crate::sys::ioctl::ioctl_num_type))
184184
}
185185
);
186186
(none $name:ident with $ioty:expr, $nr:expr) => (
187187
pub unsafe fn $name(fd: $crate::libc::c_int)
188188
-> $crate::Result<$crate::libc::c_int> {
189-
convert_ioctl_res!($crate::libc::ioctl(fd, io!($ioty, $nr) as $crate::libc::c_ulong))
189+
convert_ioctl_res!($crate::libc::ioctl(fd, io!($ioty, $nr) as $crate::sys::ioctl::ioctl_num_type))
190190
}
191191
);
192192
(read $name:ident with $ioty:expr, $nr:expr; $ty:ty) => (
193193
pub unsafe fn $name(fd: $crate::libc::c_int,
194194
val: *mut $ty)
195195
-> $crate::Result<$crate::libc::c_int> {
196-
convert_ioctl_res!($crate::libc::ioctl(fd, ior!($ioty, $nr, ::std::mem::size_of::<$ty>()) as $crate::libc::c_ulong, val))
196+
convert_ioctl_res!($crate::libc::ioctl(fd, ior!($ioty, $nr, ::std::mem::size_of::<$ty>()) as $crate::sys::ioctl::ioctl_num_type, val))
197197
}
198198
);
199199
(write $name:ident with $ioty:expr, $nr:expr; $ty:ty) => (
200200
pub unsafe fn $name(fd: $crate::libc::c_int,
201201
val: $ty)
202202
-> $crate::Result<$crate::libc::c_int> {
203-
convert_ioctl_res!($crate::libc::ioctl(fd, iow!($ioty, $nr, ::std::mem::size_of::<$ty>()) as $crate::libc::c_ulong, val))
203+
convert_ioctl_res!($crate::libc::ioctl(fd, iow!($ioty, $nr, ::std::mem::size_of::<$ty>()) as $crate::sys::ioctl::ioctl_num_type, val))
204204
}
205205
);
206206
(readwrite $name:ident with $ioty:expr, $nr:expr; $ty:ty) => (
207207
pub unsafe fn $name(fd: $crate::libc::c_int,
208208
val: *mut $ty)
209209
-> $crate::Result<$crate::libc::c_int> {
210-
convert_ioctl_res!($crate::libc::ioctl(fd, iorw!($ioty, $nr, ::std::mem::size_of::<$ty>()) as $crate::libc::c_ulong, val))
210+
convert_ioctl_res!($crate::libc::ioctl(fd, iorw!($ioty, $nr, ::std::mem::size_of::<$ty>()) as $crate::sys::ioctl::ioctl_num_type, val))
211211
}
212212
);
213213
(read buf $name:ident with $ioty:expr, $nr:expr; $ty:ty) => (
214214
pub unsafe fn $name(fd: $crate::libc::c_int,
215215
val: *mut $ty,
216216
len: usize)
217217
-> $crate::Result<$crate::libc::c_int> {
218-
convert_ioctl_res!($crate::libc::ioctl(fd, ior!($ioty, $nr, len) as $crate::libc::c_ulong, val))
218+
convert_ioctl_res!($crate::libc::ioctl(fd, ior!($ioty, $nr, len) as $crate::sys::ioctl::ioctl_num_type, val))
219219
}
220220
);
221221
(write buf $name:ident with $ioty:expr, $nr:expr; $ty:ty) => (
222222
pub unsafe fn $name(fd: $crate::libc::c_int,
223223
val: *const $ty,
224224
len: usize) -> $crate::Result<$crate::libc::c_int> {
225-
convert_ioctl_res!($crate::libc::ioctl(fd, iow!($ioty, $nr, len) as $crate::libc::c_ulong, val))
225+
convert_ioctl_res!($crate::libc::ioctl(fd, iow!($ioty, $nr, len) as $crate::sys::ioctl::ioctl_num_type, val))
226226
}
227227
);
228228
(readwrite buf $name:ident with $ioty:expr, $nr:expr; $ty:ty) => (
229229
pub unsafe fn $name(fd: $crate::libc::c_int,
230230
val: *mut $ty,
231231
len: usize)
232232
-> $crate::Result<$crate::libc::c_int> {
233-
convert_ioctl_res!($crate::libc::ioctl(fd, iorw!($ioty, $nr, len) as $crate::libc::c_ulong, val))
233+
convert_ioctl_res!($crate::libc::ioctl(fd, iorw!($ioty, $nr, len) as $crate::sys::ioctl::ioctl_num_type, val))
234234
}
235235
);
236236
}

src/sys/ioctl/platform/bsd.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
1+
/// The datatype used for the ioctl number
2+
#[doc(hidden)]
3+
pub type ioctl_num_type = ::libc::c_ulong;
4+
15
mod consts {
6+
use ::sys::ioctl::platform::ioctl_num_type;
27
#[doc(hidden)]
3-
pub const VOID: u32 = 0x20000000;
8+
pub const VOID: ioctl_num_type = 0x20000000;
49
#[doc(hidden)]
5-
pub const OUT: u32 = 0x40000000;
10+
pub const OUT: ioctl_num_type = 0x40000000;
611
#[doc(hidden)]
7-
pub const IN: u32 = 0x80000000;
12+
pub const IN: ioctl_num_type = 0x80000000;
813
#[doc(hidden)]
9-
pub const INOUT: u32 = (IN|OUT);
14+
pub const INOUT: ioctl_num_type = (IN|OUT);
1015
#[doc(hidden)]
11-
pub const IOCPARM_MASK: u32 = 0x1fff;
16+
pub const IOCPARM_MASK: ioctl_num_type = 0x1fff;
1217
}
1318

1419
pub use self::consts::*;
@@ -17,7 +22,7 @@ pub use self::consts::*;
1722
#[doc(hidden)]
1823
macro_rules! ioc {
1924
($inout:expr, $group:expr, $num:expr, $len:expr) => (
20-
$inout | (($len as u32 & $crate::sys::ioctl::IOCPARM_MASK) << 16) | (($group as u32) << 8) | ($num as u32)
25+
$inout | (($len as $crate::sys::ioctl::ioctl_num_type & $crate::sys::ioctl::IOCPARM_MASK) << 16) | (($group as $crate::sys::ioctl::ioctl_num_type) << 8) | ($num as $crate::sys::ioctl::ioctl_num_type)
2126
)
2227
}
2328

src/sys/ioctl/platform/linux.rs

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
1+
/// The datatype used for the ioctl number
2+
#[cfg(any(target_os = "android", target_env = "musl"))]
13
#[doc(hidden)]
2-
pub const NRBITS: u32 = 8;
4+
pub type ioctl_num_type = ::libc::c_int;
5+
#[cfg(not(any(target_os = "android", target_env = "musl")))]
36
#[doc(hidden)]
4-
pub const TYPEBITS: u32 = 8;
7+
pub type ioctl_num_type = ::libc::c_ulong;
8+
9+
#[doc(hidden)]
10+
pub const NRBITS: ioctl_num_type = 8;
11+
#[doc(hidden)]
12+
pub const TYPEBITS: ioctl_num_type = 8;
513

614
#[cfg(any(target_arch = "mips", target_arch = "mips64", target_arch = "powerpc", target_arch = "powerpc64"))]
715
mod consts {
@@ -50,32 +58,32 @@ mod consts {
5058
pub use self::consts::*;
5159

5260
#[doc(hidden)]
53-
pub const NRSHIFT: u32 = 0;
61+
pub const NRSHIFT: ioctl_num_type = 0;
5462
#[doc(hidden)]
55-
pub const TYPESHIFT: u32 = NRSHIFT + NRBITS as u32;
63+
pub const TYPESHIFT: ioctl_num_type = NRSHIFT + NRBITS as ioctl_num_type;
5664
#[doc(hidden)]
57-
pub const SIZESHIFT: u32 = TYPESHIFT + TYPEBITS as u32;
65+
pub const SIZESHIFT: ioctl_num_type = TYPESHIFT + TYPEBITS as ioctl_num_type;
5866
#[doc(hidden)]
59-
pub const DIRSHIFT: u32 = SIZESHIFT + SIZEBITS as u32;
67+
pub const DIRSHIFT: ioctl_num_type = SIZESHIFT + SIZEBITS as ioctl_num_type;
6068

6169
#[doc(hidden)]
62-
pub const NRMASK: u32 = (1 << NRBITS) - 1;
70+
pub const NRMASK: ioctl_num_type = (1 << NRBITS) - 1;
6371
#[doc(hidden)]
64-
pub const TYPEMASK: u32 = (1 << TYPEBITS) - 1;
72+
pub const TYPEMASK: ioctl_num_type = (1 << TYPEBITS) - 1;
6573
#[doc(hidden)]
66-
pub const SIZEMASK: u32 = (1 << SIZEBITS) - 1;
74+
pub const SIZEMASK: ioctl_num_type = (1 << SIZEBITS) - 1;
6775
#[doc(hidden)]
68-
pub const DIRMASK: u32 = (1 << DIRBITS) - 1;
76+
pub const DIRMASK: ioctl_num_type = (1 << DIRBITS) - 1;
6977

7078
/// Encode an ioctl command.
7179
#[macro_export]
7280
#[doc(hidden)]
7381
macro_rules! ioc {
7482
($dir:expr, $ty:expr, $nr:expr, $sz:expr) => (
75-
(($dir as u32) << $crate::sys::ioctl::DIRSHIFT) |
76-
(($ty as u32) << $crate::sys::ioctl::TYPESHIFT) |
77-
(($nr as u32) << $crate::sys::ioctl::NRSHIFT) |
78-
(($sz as u32) << $crate::sys::ioctl::SIZESHIFT))
83+
(($dir as $crate::sys::ioctl::ioctl_num_type & $crate::sys::ioctl::DIRMASK) << $crate::sys::ioctl::DIRSHIFT) |
84+
(($ty as $crate::sys::ioctl::ioctl_num_type & $crate::sys::ioctl::TYPEMASK) << $crate::sys::ioctl::TYPESHIFT) |
85+
(($nr as $crate::sys::ioctl::ioctl_num_type & $crate::sys::ioctl::NRMASK) << $crate::sys::ioctl::NRSHIFT) |
86+
(($sz as $crate::sys::ioctl::ioctl_num_type & $crate::sys::ioctl::SIZEMASK) << $crate::sys::ioctl::SIZESHIFT))
7987
}
8088

8189
/// Encode an ioctl command that has no associated data.

0 commit comments

Comments
 (0)