-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathpipe.rs
More file actions
240 lines (213 loc) · 6.56 KB
/
pipe.rs
File metadata and controls
240 lines (213 loc) · 6.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
use alloc::{borrow::Cow, format, sync::Arc};
use core::{
any::Any,
mem,
sync::atomic::{AtomicBool, Ordering},
task::Context,
};
use axerrno::{AxError, AxResult};
use axio::{Buf, BufMut, Read, Write};
use axpoll::{IoEvents, PollSet, Pollable};
use axsync::Mutex;
use axtask::{
current,
future::{block_on, poll_io},
};
use linux_raw_sys::{general::S_IFIFO, ioctl::FIONREAD};
use memory_addr::PAGE_SIZE_4K;
use ringbuf::{
HeapRb,
traits::{Consumer, Observer, Producer},
};
use starry_core::task::{AsThread, send_signal_to_process};
use starry_signal::{SignalInfo, Signo};
use starry_vm::VmMutPtr;
use super::{FileLike, Kstat};
use crate::file::{SealedBuf, SealedBufMut};
const RING_BUFFER_INIT_SIZE: usize = 65536; // 64 KiB
struct Shared {
buffer: Mutex<HeapRb<u8>>,
poll_rx: PollSet,
poll_tx: PollSet,
poll_close: PollSet,
}
pub struct Pipe {
read_side: bool,
shared: Arc<Shared>,
non_blocking: AtomicBool,
}
impl Drop for Pipe {
fn drop(&mut self) {
self.shared.poll_close.wake();
}
}
impl Pipe {
pub fn new() -> (Pipe, Pipe) {
let shared = Arc::new(Shared {
buffer: Mutex::new(HeapRb::new(RING_BUFFER_INIT_SIZE)),
poll_rx: PollSet::new(),
poll_tx: PollSet::new(),
poll_close: PollSet::new(),
});
let read_end = Pipe {
read_side: true,
shared: shared.clone(),
non_blocking: AtomicBool::new(false),
};
let write_end = Pipe {
read_side: false,
shared,
non_blocking: AtomicBool::new(false),
};
(read_end, write_end)
}
pub const fn is_read(&self) -> bool {
self.read_side
}
pub const fn is_write(&self) -> bool {
!self.read_side
}
pub fn closed(&self) -> bool {
Arc::strong_count(&self.shared) == 1
}
pub fn capacity(&self) -> usize {
self.shared.buffer.lock().capacity().get()
}
pub fn resize(&self, new_size: usize) -> AxResult<()> {
let new_size = new_size.div_ceil(PAGE_SIZE_4K).max(1) * PAGE_SIZE_4K;
let mut buffer = self.shared.buffer.lock();
if new_size == buffer.capacity().get() {
return Ok(());
}
if new_size < buffer.occupied_len() {
return Err(AxError::ResourceBusy);
}
let old_buffer = mem::replace(&mut *buffer, HeapRb::new(new_size));
let (left, right) = old_buffer.as_slices();
buffer.push_slice(left);
buffer.push_slice(right);
Ok(())
}
}
fn raise_pipe() {
let curr = current();
send_signal_to_process(
curr.as_thread().proc_data.proc.pid(),
Some(SignalInfo::new_kernel(Signo::SIGPIPE)),
)
.expect("Failed to send SIGPIPE");
}
impl FileLike for Pipe {
fn read(&self, dst: &mut SealedBufMut) -> AxResult<usize> {
if !self.is_read() {
return Err(AxError::BadFileDescriptor);
}
if dst.remaining_mut() == 0 {
return Ok(0);
}
block_on(poll_io(self, IoEvents::IN, self.nonblocking(), || {
let read = {
let cons = self.shared.buffer.lock();
let (left, right) = cons.as_slices();
let mut count = dst.write(left)?;
if count >= left.len() {
count += dst.write(right)?;
}
unsafe { cons.advance_read_index(count) };
count
};
if read > 0 {
self.shared.poll_tx.wake();
Ok(read)
} else if self.closed() {
Ok(0)
} else {
Err(AxError::WouldBlock)
}
}))
}
fn write(&self, src: &mut SealedBuf) -> AxResult<usize> {
if !self.is_write() {
return Err(AxError::BadFileDescriptor);
}
let size = src.remaining();
if size == 0 {
return Ok(0);
}
let mut total_written = 0;
block_on(poll_io(self, IoEvents::OUT, self.nonblocking(), || {
if self.closed() {
raise_pipe();
return Err(AxError::BrokenPipe);
}
let written = {
let mut prod = self.shared.buffer.lock();
let (left, right) = prod.vacant_slices_mut();
let mut count = src.read(unsafe { left.assume_init_mut() })?;
if count >= left.len() {
count += src.read(unsafe { right.assume_init_mut() })?;
}
unsafe { prod.advance_write_index(count) };
count
};
if written > 0 {
self.shared.poll_rx.wake();
total_written += written;
if total_written == size || self.nonblocking() {
return Ok(total_written);
}
}
Err(AxError::WouldBlock)
}))
}
fn stat(&self) -> AxResult<Kstat> {
Ok(Kstat {
mode: S_IFIFO | if self.is_read() { 0o444 } else { 0o222 },
..Default::default()
})
}
fn path(&self) -> Cow<str> {
format!("pipe:[{}]", self as *const _ as usize).into()
}
fn into_any(self: Arc<Self>) -> Arc<dyn Any + Send + Sync> {
self
}
fn set_nonblocking(&self, nonblocking: bool) -> AxResult {
self.non_blocking.store(nonblocking, Ordering::Release);
Ok(())
}
fn nonblocking(&self) -> bool {
self.non_blocking.load(Ordering::Acquire)
}
fn ioctl(&self, cmd: u32, arg: usize) -> AxResult<usize> {
match cmd {
FIONREAD => {
(arg as *mut u32).vm_write(self.shared.buffer.lock().occupied_len() as u32)?;
Ok(0)
}
_ => Err(AxError::NotATty),
}
}
}
impl Pollable for Pipe {
fn poll(&self) -> IoEvents {
let mut events = IoEvents::empty();
let buf = self.shared.buffer.lock();
if self.read_side {
events.set(IoEvents::IN, buf.occupied_len() > 0);
events.set(IoEvents::HUP, self.closed());
} else {
events.set(IoEvents::OUT, buf.vacant_len() > 0);
}
events
}
fn register(&self, context: &mut Context<'_>, events: IoEvents) {
if events.contains(IoEvents::IN) {
self.shared.poll_rx.register(context.waker());
}
if events.contains(IoEvents::OUT) {
self.shared.poll_tx.register(context.waker());
}
self.shared.poll_close.register(context.waker());
}
}