Skip to content

Commit

Permalink
Implement pipe2 for AIX
Browse files Browse the repository at this point in the history
AIX does not have pipe2 system call. Use pipe with fcntl instead.
  • Loading branch information
ecnelises authored and Thomasdezeeuw committed Aug 21, 2023
1 parent 605ba78 commit 808dbb0
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
1 change: 1 addition & 0 deletions src/sys/unix/pipe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ pub fn new() -> io::Result<(Sender, Receiver)> {
}

#[cfg(any(
target_os = "aix",
target_os = "ios",
target_os = "macos",
target_os = "tvos",
Expand Down
37 changes: 29 additions & 8 deletions src/sys/unix/waker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,14 +223,9 @@ mod pipe {

impl WakerInternal {
pub fn new() -> io::Result<WakerInternal> {
let mut fds = [-1; 2];
#[cfg(not(target_os = "aix"))]
syscall!(pipe2(fds.as_mut_ptr(), libc::O_NONBLOCK | libc::O_CLOEXEC))?;
#[cfg(target_os = "aix")]
syscall!(pipe(fds.as_mut_ptr()))?;
let sender = unsafe { File::from_raw_fd(fds[1]) };
let receiver = unsafe { File::from_raw_fd(fds[0]) };

let (receiver, sender) = Self::pipe2()?;
let receiver = unsafe { File::from_raw_fd(receiver) };
let sender = unsafe { File::from_raw_fd(sender) };
Ok(WakerInternal { sender, receiver })
}

Expand Down Expand Up @@ -270,6 +265,32 @@ mod pipe {
}
}
}

#[cfg(target_os = "aix")]
fn pipe2() -> io::Result<(RawFd, RawFd)> {
let mut fds = [-1; 2];
syscall!(pipe(fds.as_mut_ptr()))?;
unsafe {
if libc::fcntl(fds[0], libc::F_SETFL, libc::O_NONBLOCK) != 0
|| libc::fcntl(fds[0], libc::F_SETFD, libc::O_CLOEXEC) != 0
|| libc::fcntl(fds[1], libc::F_SETFL, libc::O_NONBLOCK) != 0
|| libc::fcntl(fds[1], libc::F_SETFD, libc::O_CLOEXEC) != 0
{
let err = io::Error::last_os_error();
let _ = libc::close(fds[0]);
let _ = libc::close(fds[1]);
return Err(err);
}
}
Ok((fds[0], fds[1]))
}

#[cfg(not(target_os = "aix"))]
fn pipe2() -> io::Result<(RawFd, RawFd)> {
let mut fds = [-1; 2];
syscall!(pipe2(fds.as_mut_ptr(), libc::O_NONBLOCK | libc::O_CLOEXEC))?;
Ok((fds[0], fds[1]))
}
}

impl AsRawFd for WakerInternal {
Expand Down

0 comments on commit 808dbb0

Please sign in to comment.