Skip to content

Commit 106748e

Browse files
committed
Unify the ioctl declarations on linux
See the comment for why
1 parent acc7bb1 commit 106748e

File tree

4 files changed

+31
-33
lines changed

4 files changed

+31
-33
lines changed

src/unix/linux_like/linux/gnu/mod.rs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,6 @@ pub type Lmid_t = ::c_long;
55
pub type regoff_t = ::c_int;
66
pub type __kernel_rwf_t = ::c_int;
77

8-
cfg_if! {
9-
if #[cfg(doc)] {
10-
// Used in `linux::arch` to define ioctl constants.
11-
pub(crate) type Ioctl = ::c_ulong;
12-
} else {
13-
#[doc(hidden)]
14-
pub type Ioctl = ::c_ulong;
15-
}
16-
}
17-
188
s! {
199
pub struct statx {
2010
pub stx_mask: u32,
@@ -1393,7 +1383,6 @@ extern "C" {
13931383
pub fn reallocarray(ptr: *mut ::c_void, nmemb: ::size_t, size: ::size_t) -> *mut ::c_void;
13941384

13951385
pub fn ctermid(s: *mut ::c_char) -> *mut ::c_char;
1396-
pub fn ioctl(fd: ::c_int, request: ::c_ulong, ...) -> ::c_int;
13971386
pub fn backtrace(buf: *mut *mut ::c_void, sz: ::c_int) -> ::c_int;
13981387
pub fn glob64(
13991388
pattern: *const ::c_char,

src/unix/linux_like/linux/mod.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,35 @@ cfg_if! {
5353
}
5454
}
5555

56+
// In POSIX, the request is a int, and that is what is used by
57+
// musl.
58+
//
59+
// In the linux kernel the request is treated as a 32 bit packed
60+
// value, where, in particular, the most significant bit is used
61+
// to indicate that this ioctl is a read operation.
62+
//
63+
// In glibc and ulibc the request is declared as an unsigned long.
64+
//
65+
// In C these differences are not super important since the value
66+
// is passed the same way. Rust is stricter and complains if,
67+
// using a c_int on musl, one tries to call
68+
//
69+
// libc::ioctl(..., 0xc0104801, ...);
70+
//
71+
// We then use a c_uint to help portability, since:
72+
// * Anyone using a value larger than 32 bits has a bug
73+
// * Using unsigned constants is more convenient
74+
// * The most common implementation, glibc, used an unsigned request before.
75+
cfg_if! {
76+
if #[cfg(doc)] {
77+
// Used in `linux::arch` to define ioctl constants.
78+
pub(crate) type Ioctl = ::c_uint;
79+
} else {
80+
#[doc(hidden)]
81+
pub type Ioctl = ::c_uint;
82+
}
83+
}
84+
5685
// linux/can.h
5786
pub type canid_t = u32;
5887

@@ -6087,6 +6116,8 @@ extern "C" {
60876116
len: ::size_t,
60886117
flags: ::c_uint,
60896118
) -> ::ssize_t;
6119+
6120+
pub fn ioctl(fd: ::c_int, request: ::Ioctl, ...) -> ::c_int;
60906121
}
60916122

60926123
// LFS64 extensions

src/unix/linux_like/linux/musl/mod.rs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,6 @@ pub type fsblkcnt_t = ::c_ulonglong;
2222
pub type fsfilcnt_t = ::c_ulonglong;
2323
pub type rlim_t = ::c_ulonglong;
2424

25-
cfg_if! {
26-
if #[cfg(doc)] {
27-
// Used in `linux::arch` to define ioctl constants.
28-
pub(crate) type Ioctl = ::c_int;
29-
} else {
30-
#[doc(hidden)]
31-
pub type Ioctl = ::c_int;
32-
}
33-
}
34-
3525
impl siginfo_t {
3626
pub unsafe fn si_addr(&self) -> *mut ::c_void {
3727
#[repr(C)]
@@ -843,7 +833,6 @@ extern "C" {
843833
new_limit: *const ::rlimit,
844834
old_limit: *mut ::rlimit,
845835
) -> ::c_int;
846-
pub fn ioctl(fd: ::c_int, request: ::c_int, ...) -> ::c_int;
847836
pub fn gettimeofday(tp: *mut ::timeval, tz: *mut ::c_void) -> ::c_int;
848837
pub fn ptrace(request: ::c_int, ...) -> ::c_long;
849838
pub fn getpriority(which: ::c_int, who: ::id_t) -> ::c_int;

src/unix/linux_like/linux/uclibc/mod.rs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,6 @@ pub type rlim_t = ::c_ulong;
66
pub type __rlimit_resource_t = ::c_ulong;
77
pub type __priority_which_t = ::c_uint;
88

9-
cfg_if! {
10-
if #[cfg(doc)] {
11-
// Used in `linux::arch` to define ioctl constants.
12-
pub(crate) type Ioctl = ::c_ulong;
13-
} else {
14-
#[doc(hidden)]
15-
pub type Ioctl = ::c_ulong;
16-
}
17-
}
18-
199
s! {
2010
pub struct statvfs { // Different than GNU!
2111
pub f_bsize: ::c_ulong,
@@ -353,7 +343,6 @@ pub const UDP_SEGMENT: ::c_int = 103;
353343
pub const YESEXPR: ::c_int = ((5) << 8) | (0);
354344

355345
extern "C" {
356-
pub fn ioctl(fd: ::c_int, request: ::c_ulong, ...) -> ::c_int;
357346
pub fn gettimeofday(tp: *mut ::timeval, tz: *mut ::timezone) -> ::c_int;
358347

359348
pub fn pthread_rwlockattr_getkind_np(

0 commit comments

Comments
 (0)