Added macos support for libc::pipe

Signed-off-by: Yoram de Langen <yoramdelangen@gmail.com>
This commit is contained in:
Yoram de Langen 2023-11-17 18:03:31 +01:00
parent 043a30008e
commit e49466b3a6

View File

@ -1,8 +1,6 @@
use crate::borrow_unchecked::borrow_unchecked;
use ext_php_rs::{boxed::ZBox, call_user_func, prelude::*, types::ZendHashTable, zend::Function};
use lazy_static::lazy_static;
use ext_php_rs::{
boxed::ZBox, call_user_func, prelude::*, types::ZendHashTable, zend::Function,
};
use std::{
cell::RefCell,
fs::File,
@ -31,6 +29,50 @@ fn sys_pipe() -> io::Result<(RawFd, RawFd)> {
Ok((pipefd[0], pipefd[1]))
}
#[cfg(any(target_os = "macos"))]
fn set_cloexec(fd: RawFd) -> io::Result<()> {
use libc::{F_SETFD, FD_CLOEXEC, F_GETFD};
use libc::fcntl;
let flags = unsafe { fcntl(fd, F_GETFD, 0) };
if flags == -1 {
return Err(io::Error::last_os_error());
}
let ret = unsafe { fcntl(fd, F_SETFD, flags | FD_CLOEXEC) };
if ret == -1 {
return Err(io::Error::last_os_error());
}
Ok(())
}
#[cfg(any(target_os = "macos"))]
fn set_nonblocking(fd: RawFd) -> io::Result<()> {
use libc::{fcntl, F_SETFL, O_NONBLOCK};
let ret = unsafe { fcntl(fd, F_SETFL, O_NONBLOCK) };
if ret == -1 {
return Err(io::Error::last_os_error());
}
Ok(())
}
#[cfg(any(target_os = "macos"))]
fn sys_pipe() -> io::Result<(RawFd, RawFd)> {
let mut pipefd = [0; 2];
let ret = unsafe { libc::pipe(pipefd.as_mut_ptr()) };
if ret == -1 {
return Err(io::Error::last_os_error());
}
for fd in &pipefd {
set_cloexec(*fd)?;
set_nonblocking(*fd)?;
}
Ok((pipefd[0], pipefd[1]))
}
pub struct EventLoop {
fibers: ZBox<ZendHashTable>,