Skip to content

Commit d753116

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 d952e89 commit d753116

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
@@ -170,63 +170,63 @@ macro_rules! ioctl {
170170
pub unsafe fn $name(fd: $crate::libc::c_int,
171171
data: *mut u8)
172172
-> $crate::Result<$crate::libc::c_int> {
173-
convert_ioctl_res!($crate::libc::ioctl(fd, $nr as $crate::libc::c_ulong, data))
173+
convert_ioctl_res!($crate::libc::ioctl(fd, $nr as $crate::sys::ioctl::ioctl_num_type, data))
174174
}
175175
);
176176
(bad none $name:ident with $nr:expr) => (
177177
pub unsafe fn $name(fd: $crate::libc::c_int)
178178
-> $crate::Result<$crate::libc::c_int> {
179-
convert_ioctl_res!($crate::libc::ioctl(fd, $nr as $crate::libc::c_ulong))
179+
convert_ioctl_res!($crate::libc::ioctl(fd, $nr as $crate::sys::ioctl::ioctl_num_type))
180180
}
181181
);
182182
(none $name:ident with $ioty:expr, $nr:expr) => (
183183
pub unsafe fn $name(fd: $crate::libc::c_int)
184184
-> $crate::Result<$crate::libc::c_int> {
185-
convert_ioctl_res!($crate::libc::ioctl(fd, io!($ioty, $nr) as $crate::libc::c_ulong))
185+
convert_ioctl_res!($crate::libc::ioctl(fd, io!($ioty, $nr) as $crate::sys::ioctl::ioctl_num_type))
186186
}
187187
);
188188
(read $name:ident with $ioty:expr, $nr:expr; $ty:ty) => (
189189
pub unsafe fn $name(fd: $crate::libc::c_int,
190190
val: *mut $ty)
191191
-> $crate::Result<$crate::libc::c_int> {
192-
convert_ioctl_res!($crate::libc::ioctl(fd, ior!($ioty, $nr, ::std::mem::size_of::<$ty>()) as $crate::libc::c_ulong, val))
192+
convert_ioctl_res!($crate::libc::ioctl(fd, ior!($ioty, $nr, ::std::mem::size_of::<$ty>()) as $crate::sys::ioctl::ioctl_num_type, val))
193193
}
194194
);
195195
(write $name:ident with $ioty:expr, $nr:expr; $ty:ty) => (
196196
pub unsafe fn $name(fd: $crate::libc::c_int,
197197
val: $ty)
198198
-> $crate::Result<$crate::libc::c_int> {
199-
convert_ioctl_res!($crate::libc::ioctl(fd, iow!($ioty, $nr, ::std::mem::size_of::<$ty>()) as $crate::libc::c_ulong, val))
199+
convert_ioctl_res!($crate::libc::ioctl(fd, iow!($ioty, $nr, ::std::mem::size_of::<$ty>()) as $crate::sys::ioctl::ioctl_num_type, val))
200200
}
201201
);
202202
(readwrite $name:ident with $ioty:expr, $nr:expr; $ty:ty) => (
203203
pub unsafe fn $name(fd: $crate::libc::c_int,
204204
val: *mut $ty)
205205
-> $crate::Result<$crate::libc::c_int> {
206-
convert_ioctl_res!($crate::libc::ioctl(fd, iorw!($ioty, $nr, ::std::mem::size_of::<$ty>()) as $crate::libc::c_ulong, val))
206+
convert_ioctl_res!($crate::libc::ioctl(fd, iorw!($ioty, $nr, ::std::mem::size_of::<$ty>()) as $crate::sys::ioctl::ioctl_num_type, val))
207207
}
208208
);
209209
(read buf $name:ident with $ioty:expr, $nr:expr; $ty:ty) => (
210210
pub unsafe fn $name(fd: $crate::libc::c_int,
211211
val: *mut $ty,
212212
len: usize)
213213
-> $crate::Result<$crate::libc::c_int> {
214-
convert_ioctl_res!($crate::libc::ioctl(fd, ior!($ioty, $nr, len) as $crate::libc::c_ulong, val))
214+
convert_ioctl_res!($crate::libc::ioctl(fd, ior!($ioty, $nr, len) as $crate::sys::ioctl::ioctl_num_type, val))
215215
}
216216
);
217217
(write buf $name:ident with $ioty:expr, $nr:expr; $ty:ty) => (
218218
pub unsafe fn $name(fd: $crate::libc::c_int,
219219
val: *const $ty,
220220
len: usize) -> $crate::Result<$crate::libc::c_int> {
221-
convert_ioctl_res!($crate::libc::ioctl(fd, iow!($ioty, $nr, len) as $crate::libc::c_ulong, val))
221+
convert_ioctl_res!($crate::libc::ioctl(fd, iow!($ioty, $nr, len) as $crate::sys::ioctl::ioctl_num_type, val))
222222
}
223223
);
224224
(readwrite buf $name:ident with $ioty:expr, $nr:expr; $ty:ty) => (
225225
pub unsafe fn $name(fd: $crate::libc::c_int,
226226
val: *mut $ty,
227227
len: usize)
228228
-> $crate::Result<$crate::libc::c_int> {
229-
convert_ioctl_res!($crate::libc::ioctl(fd, iorw!($ioty, $nr, len) as $crate::libc::c_ulong, val))
229+
convert_ioctl_res!($crate::libc::ioctl(fd, iorw!($ioty, $nr, len) as $crate::sys::ioctl::ioctl_num_type, val))
230230
}
231231
);
232232
}

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)