Skip to content

Commit 456b47b

Browse files
authored
Merge pull request #224 from Berrysoft/dev/process
2 parents 9a7f188 + abce8fc commit 456b47b

File tree

19 files changed

+938
-24
lines changed

19 files changed

+938
-24
lines changed

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ members = [
1212
"compio-io",
1313
"compio-tls",
1414
"compio-log",
15+
"compio-process",
1516
]
1617
resolver = "2"
1718

@@ -34,6 +35,7 @@ compio-signal = { path = "./compio-signal", version = "0.2.0" }
3435
compio-dispatcher = { path = "./compio-dispatcher", version = "0.2.0" }
3536
compio-log = { path = "./compio-log", version = "0.1.0" }
3637
compio-tls = { path = "./compio-tls", version = "0.2.0", default-features = false }
38+
compio-process = { path = "./compio-process", version = "0.1.0" }
3739

3840
flume = "0.11.0"
3941
cfg-if = "1.0.0"

compio-driver/src/iocp/cp/mod.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ use compio_buf::arrayvec::ArrayVec;
2222
use compio_log::*;
2323
use windows_sys::Win32::{
2424
Foundation::{
25-
RtlNtStatusToDosError, ERROR_BAD_COMMAND, ERROR_HANDLE_EOF, ERROR_IO_INCOMPLETE,
26-
ERROR_NO_DATA, FACILITY_NTWIN32, INVALID_HANDLE_VALUE, NTSTATUS, STATUS_PENDING,
27-
STATUS_SUCCESS,
25+
RtlNtStatusToDosError, ERROR_BAD_COMMAND, ERROR_BROKEN_PIPE, ERROR_HANDLE_EOF,
26+
ERROR_IO_INCOMPLETE, ERROR_NO_DATA, ERROR_PIPE_CONNECTED, ERROR_PIPE_NOT_CONNECTED,
27+
FACILITY_NTWIN32, INVALID_HANDLE_VALUE, NTSTATUS, STATUS_PENDING, STATUS_SUCCESS,
2828
},
2929
Storage::FileSystem::SetFileCompletionNotificationModes,
3030
System::{
@@ -173,7 +173,12 @@ impl CompletionPort {
173173
} else {
174174
let error = unsafe { RtlNtStatusToDosError(overlapped.base.Internal as _) };
175175
match error {
176-
ERROR_IO_INCOMPLETE | ERROR_HANDLE_EOF | ERROR_NO_DATA => Ok(0),
176+
ERROR_IO_INCOMPLETE
177+
| ERROR_HANDLE_EOF
178+
| ERROR_BROKEN_PIPE
179+
| ERROR_PIPE_CONNECTED
180+
| ERROR_PIPE_NOT_CONNECTED
181+
| ERROR_NO_DATA => Ok(0),
177182
_ => Err(io::Error::from_raw_os_error(error as _)),
178183
}
179184
};

compio-driver/src/iocp/mod.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,24 @@ impl AsRawFd for OwnedSocket {
9797
}
9898
}
9999

100+
impl AsRawFd for std::process::ChildStdin {
101+
fn as_raw_fd(&self) -> RawFd {
102+
self.as_raw_handle() as _
103+
}
104+
}
105+
106+
impl AsRawFd for std::process::ChildStdout {
107+
fn as_raw_fd(&self) -> RawFd {
108+
self.as_raw_handle() as _
109+
}
110+
}
111+
112+
impl AsRawFd for std::process::ChildStderr {
113+
fn as_raw_fd(&self) -> RawFd {
114+
self.as_raw_handle() as _
115+
}
116+
}
117+
100118
impl From<OwnedHandle> for OwnedFd {
101119
fn from(value: OwnedHandle) -> Self {
102120
Self::File(value)
@@ -109,6 +127,24 @@ impl From<std::fs::File> for OwnedFd {
109127
}
110128
}
111129

130+
impl From<std::process::ChildStdin> for OwnedFd {
131+
fn from(value: std::process::ChildStdin) -> Self {
132+
Self::File(OwnedHandle::from(value))
133+
}
134+
}
135+
136+
impl From<std::process::ChildStdout> for OwnedFd {
137+
fn from(value: std::process::ChildStdout) -> Self {
138+
Self::File(OwnedHandle::from(value))
139+
}
140+
}
141+
142+
impl From<std::process::ChildStderr> for OwnedFd {
143+
fn from(value: std::process::ChildStderr) -> Self {
144+
Self::File(OwnedHandle::from(value))
145+
}
146+
}
147+
112148
impl From<OwnedSocket> for OwnedFd {
113149
fn from(value: OwnedSocket) -> Self {
114150
Self::Socket(value)

compio-driver/src/iocp/op.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ use windows_sys::{
1919
core::GUID,
2020
Win32::{
2121
Foundation::{
22-
CloseHandle, GetLastError, ERROR_HANDLE_EOF, ERROR_IO_INCOMPLETE, ERROR_IO_PENDING,
23-
ERROR_NOT_FOUND, ERROR_NO_DATA, ERROR_PIPE_CONNECTED,
22+
CloseHandle, GetLastError, ERROR_BROKEN_PIPE, ERROR_HANDLE_EOF, ERROR_IO_INCOMPLETE,
23+
ERROR_IO_PENDING, ERROR_NOT_FOUND, ERROR_NO_DATA, ERROR_PIPE_CONNECTED,
24+
ERROR_PIPE_NOT_CONNECTED,
2425
},
2526
Networking::WinSock::{
2627
closesocket, setsockopt, shutdown, socklen_t, WSAIoctl, WSARecv, WSARecvFrom, WSASend,
@@ -45,9 +46,12 @@ fn winapi_result(transferred: u32) -> Poll<io::Result<usize>> {
4546
assert_ne!(error, 0);
4647
match error {
4748
ERROR_IO_PENDING => Poll::Pending,
48-
ERROR_IO_INCOMPLETE | ERROR_HANDLE_EOF | ERROR_PIPE_CONNECTED | ERROR_NO_DATA => {
49-
Poll::Ready(Ok(transferred as _))
50-
}
49+
ERROR_IO_INCOMPLETE
50+
| ERROR_HANDLE_EOF
51+
| ERROR_BROKEN_PIPE
52+
| ERROR_PIPE_CONNECTED
53+
| ERROR_PIPE_NOT_CONNECTED
54+
| ERROR_NO_DATA => Poll::Ready(Ok(transferred as _)),
5155
_ => Poll::Ready(Err(io::Error::from_raw_os_error(error as _))),
5256
}
5357
}

compio-driver/src/iour/op.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -555,3 +555,15 @@ impl<T: IoVectoredBuf, S> IntoInner for SendToVectored<T, S> {
555555
self.buffer
556556
}
557557
}
558+
559+
impl<S: AsRawFd> OpCode for PollOnce<S> {
560+
fn create_entry(self: Pin<&mut Self>) -> OpEntry {
561+
let flags = match self.interest {
562+
Interest::Readable => libc::POLLIN,
563+
Interest::Writable => libc::POLLOUT,
564+
};
565+
opcode::PollAdd::new(Fd(self.fd.as_raw_fd()), flags as _)
566+
.build()
567+
.into()
568+
}
569+
}

compio-driver/src/op.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ pub use crate::sys::op::{
1616
};
1717
#[cfg(unix)]
1818
pub use crate::sys::op::{
19-
CreateDir, CreateSocket, FileStat, HardLink, OpenFile, PathStat, ReadVectoredAt, Rename,
20-
Symlink, Unlink, WriteVectoredAt,
19+
CreateDir, CreateSocket, FileStat, HardLink, Interest, OpenFile, PathStat, PollOnce,
20+
ReadVectoredAt, Rename, Symlink, Unlink, WriteVectoredAt,
2121
};
2222
use crate::{
2323
sys::{sockaddr_storage, socklen_t},

compio-driver/src/poll/mod.rs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use crossbeam_queue::SegQueue;
1717
pub(crate) use libc::{sockaddr_storage, socklen_t};
1818
use polling::{Event, Events, PollMode, Poller};
1919

20-
use crate::{syscall, AsyncifyPool, Entry, Key, OutEntries, ProactorBuilder};
20+
use crate::{op::Interest, syscall, AsyncifyPool, Entry, Key, OutEntries, ProactorBuilder};
2121

2222
pub(crate) mod op;
2323

@@ -84,15 +84,6 @@ pub struct WaitArg {
8484
pub interest: Interest,
8585
}
8686

87-
/// The interest of the operation
88-
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
89-
pub enum Interest {
90-
/// Represents a read operation.
91-
Readable,
92-
/// Represents a write operation.
93-
Writable,
94-
}
95-
9687
#[derive(Debug, Default)]
9788
struct FdQueue {
9889
read_queue: VecDeque<usize>,

compio-driver/src/poll/op.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -748,3 +748,18 @@ impl<T: IoVectoredBuf, S> IntoInner for SendToVectored<T, S> {
748748
self.buffer
749749
}
750750
}
751+
752+
impl<S: AsRawFd> OpCode for PollOnce<S> {
753+
fn pre_submit(self: Pin<&mut Self>) -> io::Result<Decision> {
754+
Ok(Decision::wait_for(self.fd.as_raw_fd(), self.interest))
755+
}
756+
757+
fn on_event(self: Pin<&mut Self>, event: &Event) -> Poll<io::Result<usize>> {
758+
match self.interest {
759+
Interest::Readable => debug_assert!(event.readable),
760+
Interest::Writable => debug_assert!(event.writable),
761+
}
762+
763+
Poll::Ready(Ok(0))
764+
}
765+
}

compio-driver/src/unix/op.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,3 +369,25 @@ impl<T: IoVectoredBuf, S> IntoInner for SendVectored<T, S> {
369369
self.buffer
370370
}
371371
}
372+
373+
/// The interest to poll a file descriptor.
374+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
375+
pub enum Interest {
376+
/// Represents a read operation.
377+
Readable,
378+
/// Represents a write operation.
379+
Writable,
380+
}
381+
382+
/// Poll a file descriptor for specified [`Interest`].
383+
pub struct PollOnce<S> {
384+
pub(crate) fd: SharedFd<S>,
385+
pub(crate) interest: Interest,
386+
}
387+
388+
impl<S> PollOnce<S> {
389+
/// Create [`PollOnce`].
390+
pub fn new(fd: SharedFd<S>, interest: Interest) -> Self {
391+
Self { fd, interest }
392+
}
393+
}

compio-fs/src/named_pipe.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,8 @@ impl NamedPipeServer {
168168
///
169169
/// // Write fails with an OS-specific error after client has been
170170
/// // disconnected.
171-
/// let e = client.write("ping").await.0.unwrap_err();
172-
/// assert_eq!(e.raw_os_error(), Some(ERROR_PIPE_NOT_CONNECTED as i32));
171+
/// let e = client.write("ping").await.0.unwrap();
172+
/// assert_eq!(e, 0);
173173
/// # })
174174
/// ```
175175
pub fn disconnect(&self) -> io::Result<()> {

0 commit comments

Comments
 (0)