Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/2727.changed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Accept `AsFd` in `ioctl` macros
73 changes: 43 additions & 30 deletions src/sys/ioctl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ macro_rules! convert_ioctl_res {
/// The generated function has the following signature:
///
/// ```rust,ignore
/// pub unsafe fn FUNCTION_NAME(fd: libc::c_int) -> Result<libc::c_int>
/// pub unsafe fn FUNCTION_NAME<Fd: AsFd>(fd: Fd) -> Result<libc::c_int>
/// ```
///
/// For a more in-depth explanation of ioctls, see [`::sys::ioctl`](sys/ioctl/index.html).
Expand All @@ -285,8 +285,9 @@ macro_rules! convert_ioctl_res {
macro_rules! ioctl_none {
($(#[$attr:meta])* $name:ident, $ioty:expr, $nr:expr) => (
$(#[$attr])*
pub unsafe fn $name(fd: $crate::libc::c_int)
pub unsafe fn $name<Fd: ::std::os::fd::AsFd>(fd: Fd)
-> $crate::Result<$crate::libc::c_int> {
let fd = ::std::os::fd::AsRawFd::as_raw_fd(&fd.as_fd());
unsafe {
convert_ioctl_res!($crate::libc::ioctl(fd, request_code_none!($ioty, $nr) as $crate::sys::ioctl::ioctl_num_type))
}
Expand All @@ -304,7 +305,7 @@ macro_rules! ioctl_none {
/// The generated function has the following signature:
///
/// ```rust,ignore
/// pub unsafe fn FUNCTION_NAME(fd: libc::c_int) -> Result<libc::c_int>
/// pub unsafe fn FUNCTION_NAME<Fd: AsFd>(fd: Fd) -> Result<libc::c_int>
/// ```
///
/// For a more in-depth explanation of ioctls, see [`::sys::ioctl`](sys/ioctl/index.html).
Expand All @@ -315,20 +316,20 @@ macro_rules! ioctl_none {
/// # #[macro_use] extern crate nix;
/// # use libc::TIOCNXCL;
/// # use std::fs::File;
/// # use std::os::unix::io::AsRawFd;
/// ioctl_none_bad!(tiocnxcl, TIOCNXCL);
/// fn main() {
/// let file = File::open("/dev/ttyUSB0").unwrap();
/// unsafe { tiocnxcl(file.as_raw_fd()) }.unwrap();
/// unsafe { tiocnxcl(&file) }.unwrap();
/// }
/// ```
// TODO: add an example using request_code_*!()
#[macro_export(local_inner_macros)]
macro_rules! ioctl_none_bad {
($(#[$attr:meta])* $name:ident, $nr:expr) => (
$(#[$attr])*
pub unsafe fn $name(fd: $crate::libc::c_int)
pub unsafe fn $name<Fd: ::std::os::fd::AsFd>(fd: Fd)
-> $crate::Result<$crate::libc::c_int> {
let fd = ::std::os::fd::AsRawFd::as_raw_fd(&fd.as_fd());
unsafe {
convert_ioctl_res!($crate::libc::ioctl(fd, $nr as $crate::sys::ioctl::ioctl_num_type))
}
Expand All @@ -348,7 +349,7 @@ macro_rules! ioctl_none_bad {
/// The generated function has the following signature:
///
/// ```rust,ignore
/// pub unsafe fn FUNCTION_NAME(fd: libc::c_int, data: *mut DATA_TYPE) -> Result<libc::c_int>
/// pub unsafe fn FUNCTION_NAME<Fd: AsFd>(fd: Fd, data: *mut DATA_TYPE) -> Result<libc::c_int>
/// ```
///
/// For a more in-depth explanation of ioctls, see [`::sys::ioctl`](sys/ioctl/index.html).
Expand All @@ -366,9 +367,10 @@ macro_rules! ioctl_none_bad {
macro_rules! ioctl_read {
($(#[$attr:meta])* $name:ident, $ioty:expr, $nr:expr, $ty:ty) => (
$(#[$attr])*
pub unsafe fn $name(fd: $crate::libc::c_int,
pub unsafe fn $name<Fd: ::std::os::fd::AsFd>(fd: Fd,
data: *mut $ty)
-> $crate::Result<$crate::libc::c_int> {
let fd = ::std::os::fd::AsRawFd::as_raw_fd(&fd.as_fd());
unsafe {
convert_ioctl_res!($crate::libc::ioctl(fd, request_code_read!($ioty, $nr, ::std::mem::size_of::<$ty>()) as $crate::sys::ioctl::ioctl_num_type, data))
}
Expand All @@ -387,7 +389,7 @@ macro_rules! ioctl_read {
/// The generated function has the following signature:
///
/// ```rust,ignore
/// pub unsafe fn FUNCTION_NAME(fd: libc::c_int, data: *mut DATA_TYPE) -> Result<libc::c_int>
/// pub unsafe fn FUNCTION_NAME<Fd: AsFd>(fd: Fd, data: *mut DATA_TYPE) -> Result<libc::c_int>
/// ```
///
/// For a more in-depth explanation of ioctls, see [`::sys::ioctl`](sys/ioctl/index.html).
Expand All @@ -404,9 +406,10 @@ macro_rules! ioctl_read {
macro_rules! ioctl_read_bad {
($(#[$attr:meta])* $name:ident, $nr:expr, $ty:ty) => (
$(#[$attr])*
pub unsafe fn $name(fd: $crate::libc::c_int,
pub unsafe fn $name<Fd: ::std::os::fd::AsFd>(fd: Fd,
data: *mut $ty)
-> $crate::Result<$crate::libc::c_int> {
let fd = ::std::os::fd::AsRawFd::as_raw_fd(&fd.as_fd());
unsafe {
convert_ioctl_res!($crate::libc::ioctl(fd, $nr as $crate::sys::ioctl::ioctl_num_type, data))
}
Expand All @@ -426,7 +429,7 @@ macro_rules! ioctl_read_bad {
/// The generated function has the following signature:
///
/// ```rust,ignore
/// pub unsafe fn FUNCTION_NAME(fd: libc::c_int, data: *const DATA_TYPE) -> Result<libc::c_int>
/// pub unsafe fn FUNCTION_NAME<Fd: AsFd>(fd: Fd, data: *const DATA_TYPE) -> Result<libc::c_int>
/// ```
///
/// For a more in-depth explanation of ioctls, see [`::sys::ioctl`](sys/ioctl/index.html).
Expand All @@ -443,9 +446,10 @@ macro_rules! ioctl_read_bad {
macro_rules! ioctl_write_ptr {
($(#[$attr:meta])* $name:ident, $ioty:expr, $nr:expr, $ty:ty) => (
$(#[$attr])*
pub unsafe fn $name(fd: $crate::libc::c_int,
pub unsafe fn $name<Fd: ::std::os::fd::AsFd>(fd: Fd,
data: *const $ty)
-> $crate::Result<$crate::libc::c_int> {
let fd = ::std::os::fd::AsRawFd::as_raw_fd(&fd.as_fd());
unsafe {
convert_ioctl_res!($crate::libc::ioctl(fd, request_code_write!($ioty, $nr, ::std::mem::size_of::<$ty>()) as $crate::sys::ioctl::ioctl_num_type, data))
}
Expand All @@ -464,7 +468,7 @@ macro_rules! ioctl_write_ptr {
/// The generated function has the following signature:
///
/// ```rust,ignore
/// pub unsafe fn FUNCTION_NAME(fd: libc::c_int, data: *const DATA_TYPE) -> Result<libc::c_int>
/// pub unsafe fn FUNCTION_NAME<Fd: AsFd>(fd: Fd, data: *const DATA_TYPE) -> Result<libc::c_int>
/// ```
///
/// For a more in-depth explanation of ioctls, see [`::sys::ioctl`](sys/ioctl/index.html).
Expand All @@ -481,9 +485,10 @@ macro_rules! ioctl_write_ptr {
macro_rules! ioctl_write_ptr_bad {
($(#[$attr:meta])* $name:ident, $nr:expr, $ty:ty) => (
$(#[$attr])*
pub unsafe fn $name(fd: $crate::libc::c_int,
pub unsafe fn $name<Fd: ::std::os::fd::AsFd>(fd: Fd,
data: *const $ty)
-> $crate::Result<$crate::libc::c_int> {
let fd = ::std::os::fd::AsRawFd::as_raw_fd(&fd.as_fd());
unsafe {
convert_ioctl_res!($crate::libc::ioctl(fd, $nr as $crate::sys::ioctl::ioctl_num_type, data))
}
Expand All @@ -504,7 +509,7 @@ cfg_if! {
/// The generated function has the following signature:
///
/// ```rust,ignore
/// pub unsafe fn FUNCTION_NAME(fd: libc::c_int, data: nix::sys::ioctl::ioctl_param_type) -> Result<libc::c_int>
/// pub unsafe fn FUNCTION_NAME<Fd: AsFd>(fd: Fd, data: nix::sys::ioctl::ioctl_param_type) -> Result<libc::c_int>
/// ```
///
/// `nix::sys::ioctl::ioctl_param_type` depends on the OS:
Expand All @@ -524,9 +529,10 @@ cfg_if! {
macro_rules! ioctl_write_int {
($(#[$attr:meta])* $name:ident, $ioty:expr, $nr:expr) => (
$(#[$attr])*
pub unsafe fn $name(fd: $crate::libc::c_int,
pub unsafe fn $name<Fd: ::std::os::fd::AsFd>(fd: Fd,
data: $crate::sys::ioctl::ioctl_param_type)
-> $crate::Result<$crate::libc::c_int> {
let fd = ::std::os::fd::AsRawFd::as_raw_fd(&fd.as_fd());
unsafe {
convert_ioctl_res!($crate::libc::ioctl(fd, request_code_write_int!($ioty, $nr) as $crate::sys::ioctl::ioctl_num_type, data))
}
Expand All @@ -545,7 +551,7 @@ cfg_if! {
/// The generated function has the following signature:
///
/// ```rust,ignore
/// pub unsafe fn FUNCTION_NAME(fd: libc::c_int, data: nix::sys::ioctl::ioctl_param_type) -> Result<libc::c_int>
/// pub unsafe fn FUNCTION_NAME<Fd: AsFd>(fd: Fd, data: nix::sys::ioctl::ioctl_param_type) -> Result<libc::c_int>
/// ```
///
/// `nix::sys::ioctl::ioctl_param_type` depends on the OS:
Expand All @@ -567,9 +573,10 @@ cfg_if! {
macro_rules! ioctl_write_int {
($(#[$attr:meta])* $name:ident, $ioty:expr, $nr:expr) => (
$(#[$attr])*
pub unsafe fn $name(fd: $crate::libc::c_int,
pub unsafe fn $name<Fd: ::std::os::fd::AsFd>(fd: Fd,
data: $crate::sys::ioctl::ioctl_param_type)
-> $crate::Result<$crate::libc::c_int> {
let fd = ::std::os::fd::AsRawFd::as_raw_fd(&fd.as_fd());
unsafe {
convert_ioctl_res!($crate::libc::ioctl(fd, request_code_write!($ioty, $nr, ::std::mem::size_of::<$crate::libc::c_int>()) as $crate::sys::ioctl::ioctl_num_type, data))
}
Expand All @@ -589,7 +596,7 @@ cfg_if! {
/// The generated function has the following signature:
///
/// ```rust,ignore
/// pub unsafe fn FUNCTION_NAME(fd: libc::c_int, data: libc::c_int) -> Result<libc::c_int>
/// pub unsafe fn FUNCTION_NAME<Fd: AsFd>(fd: Fd, data: libc::c_int) -> Result<libc::c_int>
/// ```
///
/// For a more in-depth explanation of ioctls, see [`::sys::ioctl`](sys/ioctl/index.html).
Expand All @@ -613,9 +620,10 @@ cfg_if! {
macro_rules! ioctl_write_int_bad {
($(#[$attr:meta])* $name:ident, $nr:expr) => (
$(#[$attr])*
pub unsafe fn $name(fd: $crate::libc::c_int,
pub unsafe fn $name<Fd: ::std::os::fd::AsFd>(fd: Fd,
data: $crate::libc::c_int)
-> $crate::Result<$crate::libc::c_int> {
let fd = ::std::os::fd::AsRawFd::as_raw_fd(&fd.as_fd());
unsafe {
convert_ioctl_res!($crate::libc::ioctl(fd, $nr as $crate::sys::ioctl::ioctl_num_type, data))
}
Expand All @@ -635,7 +643,7 @@ macro_rules! ioctl_write_int_bad {
/// The generated function has the following signature:
///
/// ```rust,ignore
/// pub unsafe fn FUNCTION_NAME(fd: libc::c_int, data: *mut DATA_TYPE) -> Result<libc::c_int>
/// pub unsafe fn FUNCTION_NAME<Fd: AsFd>(fd: Fd, data: *mut DATA_TYPE) -> Result<libc::c_int>
/// ```
///
/// For a more in-depth explanation of ioctls, see [`::sys::ioctl`](sys/ioctl/index.html).
Expand All @@ -652,9 +660,10 @@ macro_rules! ioctl_write_int_bad {
macro_rules! ioctl_readwrite {
($(#[$attr:meta])* $name:ident, $ioty:expr, $nr:expr, $ty:ty) => (
$(#[$attr])*
pub unsafe fn $name(fd: $crate::libc::c_int,
pub unsafe fn $name<Fd: ::std::os::fd::AsFd>(fd: Fd,
data: *mut $ty)
-> $crate::Result<$crate::libc::c_int> {
let fd = ::std::os::fd::AsRawFd::as_raw_fd(&fd.as_fd());
let ioty = $ioty;
let nr = $nr;
unsafe {
Expand All @@ -675,7 +684,7 @@ macro_rules! ioctl_readwrite {
/// The generated function has the following signature:
///
/// ```rust,ignore
/// pub unsafe fn FUNCTION_NAME(fd: libc::c_int, data: *mut DATA_TYPE) -> Result<libc::c_int>
/// pub unsafe fn FUNCTION_NAME<Fd: AsFd>(fd: Fd, data: *mut DATA_TYPE) -> Result<libc::c_int>
/// ```
///
/// For a more in-depth explanation of ioctls, see [`::sys::ioctl`](sys/ioctl/index.html).
Expand All @@ -684,9 +693,10 @@ macro_rules! ioctl_readwrite {
macro_rules! ioctl_readwrite_bad {
($(#[$attr:meta])* $name:ident, $nr:expr, $ty:ty) => (
$(#[$attr])*
pub unsafe fn $name(fd: $crate::libc::c_int,
pub unsafe fn $name<Fd: ::std::os::fd::AsFd>(fd: Fd,
data: *mut $ty)
-> $crate::Result<$crate::libc::c_int> {
let fd = ::std::os::fd::AsRawFd::as_raw_fd(&fd.as_fd());
unsafe {
convert_ioctl_res!($crate::libc::ioctl(fd, $nr as $crate::sys::ioctl::ioctl_num_type, data))
}
Expand All @@ -706,7 +716,7 @@ macro_rules! ioctl_readwrite_bad {
/// The generated function has the following signature:
///
/// ```rust,ignore
/// pub unsafe fn FUNCTION_NAME(fd: libc::c_int, data: &mut [DATA_TYPE]) -> Result<libc::c_int>
/// pub unsafe fn FUNCTION_NAME<Fd: AsFd>(fd: Fd, data: &mut [DATA_TYPE]) -> Result<libc::c_int>
/// ```
///
/// For a more in-depth explanation of ioctls, see [`::sys::ioctl`](sys/ioctl/index.html).
Expand All @@ -715,9 +725,10 @@ macro_rules! ioctl_readwrite_bad {
macro_rules! ioctl_read_buf {
($(#[$attr:meta])* $name:ident, $ioty:expr, $nr:expr, $ty:ty) => (
$(#[$attr])*
pub unsafe fn $name(fd: $crate::libc::c_int,
pub unsafe fn $name<Fd: ::std::os::fd::AsFd>(fd: Fd,
data: &mut [$ty])
-> $crate::Result<$crate::libc::c_int> {
let fd = ::std::os::fd::AsRawFd::as_raw_fd(&fd.as_fd());
unsafe {
convert_ioctl_res!($crate::libc::ioctl(fd, request_code_read!($ioty, $nr, ::std::mem::size_of_val(data)) as $crate::sys::ioctl::ioctl_num_type, data.as_mut_ptr()))
}
Expand All @@ -737,7 +748,7 @@ macro_rules! ioctl_read_buf {
/// The generated function has the following signature:
///
/// ```rust,ignore
/// pub unsafe fn FUNCTION_NAME(fd: libc::c_int, data: &[DATA_TYPE]) -> Result<libc::c_int>
/// pub unsafe fn FUNCTION_NAME<Fd: AsFd>(fd: Fd, data: &[DATA_TYPE]) -> Result<libc::c_int>
/// ```
///
/// For a more in-depth explanation of ioctls, see [`::sys::ioctl`](sys/ioctl/index.html).
Expand All @@ -756,9 +767,10 @@ macro_rules! ioctl_read_buf {
macro_rules! ioctl_write_buf {
($(#[$attr:meta])* $name:ident, $ioty:expr, $nr:expr, $ty:ty) => (
$(#[$attr])*
pub unsafe fn $name(fd: $crate::libc::c_int,
pub unsafe fn $name<Fd: ::std::os::fd::AsFd>(fd: Fd,
data: &[$ty])
-> $crate::Result<$crate::libc::c_int> {
let fd = ::std::os::fd::AsRawFd::as_raw_fd(&fd.as_fd());
unsafe {
convert_ioctl_res!($crate::libc::ioctl(fd, request_code_write!($ioty, $nr, ::std::mem::size_of_val(data)) as $crate::sys::ioctl::ioctl_num_type, data.as_ptr()))
}
Expand All @@ -778,7 +790,7 @@ macro_rules! ioctl_write_buf {
/// The generated function has the following signature:
///
/// ```rust,ignore
/// pub unsafe fn FUNCTION_NAME(fd: libc::c_int, data: &mut [DATA_TYPE]) -> Result<libc::c_int>
/// pub unsafe fn FUNCTION_NAME<Fd: AsFd>(fd: Fd, data: &mut [DATA_TYPE]) -> Result<libc::c_int>
/// ```
///
/// For a more in-depth explanation of ioctls, see [`::sys::ioctl`](sys/ioctl/index.html).
Expand All @@ -787,9 +799,10 @@ macro_rules! ioctl_write_buf {
macro_rules! ioctl_readwrite_buf {
($(#[$attr:meta])* $name:ident, $ioty:expr, $nr:expr, $ty:ty) => (
$(#[$attr])*
pub unsafe fn $name(fd: $crate::libc::c_int,
pub unsafe fn $name<Fd: ::std::os::fd::AsFd>(fd: Fd,
data: &mut [$ty])
-> $crate::Result<$crate::libc::c_int> {
let fd = ::std::os::fd::AsRawFd::as_raw_fd(&fd.as_fd());
unsafe {
convert_ioctl_res!($crate::libc::ioctl(fd, request_code_readwrite!($ioty, $nr, ::std::mem::size_of_val(data)) as $crate::sys::ioctl::ioctl_num_type, data.as_mut_ptr()))
}
Expand Down
4 changes: 2 additions & 2 deletions src/sys/socket/addr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1732,7 +1732,7 @@ pub mod sys_control {
use crate::sys::socket::addr::AddressFamily;
use libc::{self, c_uchar};
use std::{fmt, mem, ptr};
use std::os::unix::io::RawFd;

use crate::{Errno, Result};
use super::{private, SockaddrLike};

Expand Down Expand Up @@ -1801,7 +1801,7 @@ pub mod sys_control {

/// Construct a new `SysControlAddr` from its human readable name and
/// unit number.
pub fn from_name(sockfd: RawFd, name: &str, unit: u32) -> Result<SysControlAddr> {
pub fn from_name<Fd: ::std::os::fd::AsFd>(sockfd: Fd, name: &str, unit: u32) -> Result<SysControlAddr> {
if name.len() > MAX_KCTL_NAME {
return Err(Errno::ENAMETOOLONG);
}
Expand Down
Loading