Merge pull request #3 from jD91mZM2/master

Convert into edge-triggered model
This commit is contained in:
Jeremy Soller
2018-05-31 11:57:38 -06:00
committed by GitHub
10 changed files with 148 additions and 109 deletions
Generated
+4 -4
View File
@@ -2,13 +2,13 @@
name = "ptyd"
version = "0.1.0"
dependencies = [
"redox_syscall 0.1.37 (registry+https://github.com/rust-lang/crates.io-index)",
"redox_syscall 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)",
"redox_termios 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "redox_syscall"
version = "0.1.37"
version = "0.1.40"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
@@ -16,9 +16,9 @@ name = "redox_termios"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"redox_syscall 0.1.37 (registry+https://github.com/rust-lang/crates.io-index)",
"redox_syscall 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)",
]
[metadata]
"checksum redox_syscall 0.1.37 (registry+https://github.com/rust-lang/crates.io-index)" = "0d92eecebad22b767915e4d529f89f28ee96dbbf5a4810d2b844373f136417fd"
"checksum redox_syscall 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)" = "c214e91d3ecf43e9a4e41e578973adeb14b474f2bee858742d127af75a0112b1"
"checksum redox_termios 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7e891cfe48e9100a70a3b6eb652fef28920c117d366339687bd5576160db0f76"
+1 -1
View File
@@ -3,5 +3,5 @@ name = "ptyd"
version = "0.1.0"
[dependencies]
redox_syscall = "0.1"
redox_syscall = "0.1.40"
redox_termios = "0.1"
+26 -25
View File
@@ -7,8 +7,7 @@ use std::fs::File;
use std::io::{Read, Write};
use syscall::data::Packet;
use syscall::error::EWOULDBLOCK;
use syscall::scheme::SchemeMut;
use syscall::scheme::SchemeBlockMut;
mod master;
mod pgrp;
@@ -34,42 +33,44 @@ fn main(){
let mut packet = Packet::default();
socket.read(&mut packet).expect("pty: failed to read events from pty scheme");
let a = packet.a;
scheme.handle(&mut packet);
if packet.a == (-EWOULDBLOCK) as usize {
if let Some(a) = scheme.handle(&mut packet) {
packet.a = a;
todo.push(packet);
} else {
socket.write(&packet).expect("pty: failed to write responses to pty scheme");
} else {
todo.push(packet);
}
let mut i = 0;
while i < todo.len() {
let a = todo[i].a;
scheme.handle(&mut todo[i]);
if todo[i].a == (-EWOULDBLOCK) as usize {
todo[i].a = a;
i += 1;
} else {
let packet = todo.remove(i);
if let Some(a) = scheme.handle(&mut todo[i]) {
let mut packet = todo.remove(i);
packet.a = a;
socket.write(&packet).expect("pty: failed to write responses to pty scheme");
} else {
i += 1;
}
}
for (id, handle) in scheme.handles.iter() {
for (id, handle) in scheme.handles.iter_mut() {
if let Some(count) = handle.fevent_count() {
socket.write(&Packet {
id: 0,
pid: 0,
uid: 0,
gid: 0,
a: syscall::number::SYS_FEVENT,
b: *id,
c: syscall::flag::EVENT_READ,
d: count
}).expect("pty: failed to write event");
post_fevent(&mut socket, *id, syscall::EVENT_READ, count);
}
if handle.fevent_writable() {
post_fevent(&mut socket, *id, syscall::EVENT_WRITE, 1);
}
}
}
}
}
fn post_fevent(socket: &mut File, id: usize, flags: usize, count: usize) {
socket.write(&Packet {
id: 0,
pid: 0,
uid: 0,
gid: 0,
a: syscall::number::SYS_FEVENT,
b: id,
c: flags,
d: count
}).expect("pty: failed to write event");
}
+29 -18
View File
@@ -1,7 +1,7 @@
use std::cell::RefCell;
use std::rc::{Rc, Weak};
use syscall::error::{Error, Result, EINVAL, EWOULDBLOCK};
use syscall::error::{Error, Result, EINVAL, EAGAIN};
use syscall::flag::{F_GETFL, F_SETFL, O_ACCMODE, O_NONBLOCK};
use pty::Pty;
@@ -12,6 +12,8 @@ use resource::Resource;
pub struct PtyMaster {
pty: Rc<RefCell<Pty>>,
flags: usize,
notified_read: bool,
notified_write: bool
}
impl PtyMaster {
@@ -19,6 +21,8 @@ impl PtyMaster {
PtyMaster {
pty: pty,
flags: flags,
notified_read: false,
notified_write: false
}
}
}
@@ -40,7 +44,7 @@ impl Resource for PtyMaster {
self.pty.borrow_mut().path(buf)
}
fn read(&self, buf: &mut [u8]) -> Result<usize> {
fn read(&self, buf: &mut [u8]) -> Result<Option<usize>> {
let mut pty = self.pty.borrow_mut();
if let Some(packet) = pty.miso.pop_front() {
@@ -55,24 +59,24 @@ impl Resource for PtyMaster {
pty.miso.push_front(packet[i..].to_vec());
}
Ok(i)
Ok(Some(i))
} else if self.flags & O_NONBLOCK == O_NONBLOCK || Rc::weak_count(&self.pty) == 0 {
Ok(0)
Err(Error::new(EAGAIN))
} else {
Err(Error::new(EWOULDBLOCK))
Ok(None)
}
}
fn write(&self, buf: &[u8]) -> Result<usize> {
fn write(&self, buf: &[u8]) -> Result<Option<usize>> {
let mut pty = self.pty.borrow_mut();
if pty.mosi.len() >= 64 {
return Err(Error::new(EWOULDBLOCK));
return Ok(None);
}
pty.input(buf);
Ok(buf.len())
Ok(Some(buf.len()))
}
fn sync(&self) -> Result<usize> {
@@ -90,22 +94,29 @@ impl Resource for PtyMaster {
}
}
fn fevent(&self) -> Result<()> {
fn fevent(&mut self) -> Result<()> {
self.notified_read = false; // resend
self.notified_write = false;
Ok(())
}
fn fevent_count(&self) -> Option<usize> {
{
let pty = self.pty.borrow();
if let Some(data) = pty.miso.front() {
return Some(data.len());
fn fevent_count(&mut self) -> Option<usize> {
let pty = self.pty.borrow();
if let Some(data) = pty.miso.front() {
if !self.notified_read {
self.notified_read = true;
Some(data.len())
} else {
None
}
}
if Rc::weak_count(&self.pty) == 0 {
Some(0)
} else {
self.notified_read = false;
None
}
}
fn fevent_writable(&mut self) -> bool {
let notified = self.notified_write;
self.notified_write = true;
!notified
}
}
+10 -7
View File
@@ -45,7 +45,7 @@ impl Resource for PtyPgrp {
}
}
fn read(&self, buf: &mut [u8]) -> Result<usize> {
fn read(&self, buf: &mut [u8]) -> Result<Option<usize>> {
if let Some(pty_lock) = self.pty.upgrade() {
let pty = pty_lock.borrow();
let pgrp: &[u8] = unsafe {
@@ -60,13 +60,13 @@ impl Resource for PtyPgrp {
buf[i] = pgrp[i];
i += 1;
}
Ok(i)
Ok(Some(i))
} else {
Ok(0)
Ok(Some(0))
}
}
fn write(&self, buf: &[u8]) -> Result<usize> {
fn write(&self, buf: &[u8]) -> Result<Option<usize>> {
if let Some(pty_lock) = self.pty.upgrade() {
let mut pty = pty_lock.borrow_mut();
let pgrp: &mut [u8] = unsafe {
@@ -81,7 +81,7 @@ impl Resource for PtyPgrp {
pgrp[i] = buf[i];
i += 1;
}
Ok(i)
Ok(Some(i))
} else {
Err(Error::new(EPIPE))
}
@@ -102,11 +102,14 @@ impl Resource for PtyPgrp {
}
}
fn fevent(&self) -> Result<()> {
fn fevent(&mut self) -> Result<()> {
Err(Error::new(EBADF))
}
fn fevent_count(&self) -> Option<usize> {
fn fevent_count(&mut self) -> Option<usize> {
None
}
fn fevent_writable(&mut self) -> bool {
false
}
}
+5 -4
View File
@@ -11,10 +11,11 @@ pub trait Resource {
fn flags(&self) -> usize;
fn path(&self, buf: &mut [u8]) -> Result<usize>;
fn read(&self, buf: &mut [u8]) -> Result<usize>;
fn write(&self, buf: &[u8]) -> Result<usize>;
fn read(&self, buf: &mut [u8]) -> Result<Option<usize>>;
fn write(&self, buf: &[u8]) -> Result<Option<usize>>;
fn sync(&self) -> Result<usize>;
fn fcntl(&mut self, cmd: usize, arg: usize) -> Result<usize>;
fn fevent(&self) -> Result<()>;
fn fevent_count(&self) -> Option<usize>;
fn fevent(&mut self) -> Result<()>;
fn fevent_count(&mut self) -> Option<usize>;
fn fevent_writable(&mut self) -> bool;
}
+24 -24
View File
@@ -6,7 +6,7 @@ use std::str;
use syscall::data::Stat;
use syscall::error::{Error, Result, EBADF, EINVAL, ENOENT};
use syscall::flag::MODE_CHR;
use syscall::scheme::SchemeMut;
use syscall::scheme::SchemeBlockMut;
use master::PtyMaster;
use pgrp::PtyPgrp;
@@ -30,8 +30,8 @@ impl PtyScheme {
}
}
impl SchemeMut for PtyScheme {
fn open(&mut self, path: &[u8], flags: usize, _uid: u32, _gid: u32) -> Result<usize> {
impl SchemeBlockMut for PtyScheme {
fn open(&mut self, path: &[u8], flags: usize, _uid: u32, _gid: u32) -> Result<Option<usize>> {
let path = str::from_utf8(path).or(Err(Error::new(EINVAL)))?.trim_matches('/');
if path.is_empty() {
@@ -41,7 +41,7 @@ impl SchemeMut for PtyScheme {
let pty = Rc::new(RefCell::new(Pty::new(id)));
self.handles.insert(id, Box::new(PtyMaster::new(pty, flags)));
Ok(id)
Ok(Some(id))
} else {
let master_id = path.parse::<usize>().or(Err(Error::new(EINVAL)))?;
let pty = {
@@ -54,11 +54,11 @@ impl SchemeMut for PtyScheme {
self.handles.insert(id, Box::new(PtySlave::new(pty, flags)));
Ok(id)
Ok(Some(id))
}
}
fn dup(&mut self, old_id: usize, buf: &[u8]) -> Result<usize> {
fn dup(&mut self, old_id: usize, buf: &[u8]) -> Result<Option<usize>> {
let handle: Box<Resource> = {
let old_handle = self.handles.get(&old_id).ok_or(Error::new(EBADF))?;
@@ -79,35 +79,35 @@ impl SchemeMut for PtyScheme {
self.next_id += 1;
self.handles.insert(id, handle);
Ok(id)
Ok(Some(id))
}
fn read(&mut self, id: usize, buf: &mut [u8]) -> Result<usize> {
fn read(&mut self, id: usize, buf: &mut [u8]) -> Result<Option<usize>> {
let handle = self.handles.get(&id).ok_or(Error::new(EBADF))?;
handle.read(buf)
}
fn write(&mut self, id: usize, buf: &[u8]) -> Result<usize> {
fn write(&mut self, id: usize, buf: &[u8]) -> Result<Option<usize>> {
let handle = self.handles.get(&id).ok_or(Error::new(EBADF))?;
handle.write(buf)
}
fn fcntl(&mut self, id: usize, cmd: usize, arg: usize) -> Result<usize> {
fn fcntl(&mut self, id: usize, cmd: usize, arg: usize) -> Result<Option<usize>> {
let handle = self.handles.get_mut(&id).ok_or(Error::new(EBADF))?;
handle.fcntl(cmd, arg)
handle.fcntl(cmd, arg).map(Some)
}
fn fevent(&mut self, id: usize, _flags: usize) -> Result<usize> {
fn fevent(&mut self, id: usize, _flags: usize) -> Result<Option<usize>> {
let handle = self.handles.get_mut(&id).ok_or(Error::new(EBADF))?;
handle.fevent().and(Ok(Some(id)))
}
fn fpath(&mut self, id: usize, buf: &mut [u8]) -> Result<Option<usize>> {
let handle = self.handles.get(&id).ok_or(Error::new(EBADF))?;
handle.fevent().and(Ok(id))
handle.path(buf).map(Some)
}
fn fpath(&mut self, id: usize, buf: &mut [u8]) -> Result<usize> {
let handle = self.handles.get(&id).ok_or(Error::new(EBADF))?;
handle.path(buf)
}
fn fstat(&mut self, id: usize, stat: &mut Stat) -> Result<usize> {
fn fstat(&mut self, id: usize, stat: &mut Stat) -> Result<Option<usize>> {
let _handle = self.handles.get(&id).ok_or(Error::new(EBADF))?;
*stat = Stat {
@@ -115,17 +115,17 @@ impl SchemeMut for PtyScheme {
..Default::default()
};
Ok(0)
Ok(Some(0))
}
fn fsync(&mut self, id: usize) -> Result<usize> {
fn fsync(&mut self, id: usize) -> Result<Option<usize>> {
let handle = self.handles.get(&id).ok_or(Error::new(EBADF))?;
handle.sync()
handle.sync().map(Some)
}
fn close(&mut self, id: usize) -> Result<usize> {
fn close(&mut self, id: usize) -> Result<Option<usize>> {
drop(self.handles.remove(&id));
Ok(0)
Ok(Some(0))
}
}
+29 -12
View File
@@ -1,7 +1,7 @@
use std::cell::RefCell;
use std::rc::Weak;
use syscall::error::{Error, Result, EINVAL, EPIPE, EWOULDBLOCK};
use syscall::error::{Error, Result, EINVAL, EPIPE, EAGAIN};
use syscall::flag::{F_GETFL, F_SETFL, O_ACCMODE, O_NONBLOCK};
use pty::Pty;
@@ -12,6 +12,8 @@ use resource::Resource;
pub struct PtySlave {
pty: Weak<RefCell<Pty>>,
flags: usize,
notified_read: bool,
notified_write: bool
}
impl PtySlave {
@@ -19,6 +21,8 @@ impl PtySlave {
PtySlave {
pty: pty,
flags: flags,
notified_read: false,
notified_write: false
}
}
}
@@ -44,7 +48,7 @@ impl Resource for PtySlave {
}
}
fn read(&self, buf: &mut [u8]) -> Result<usize> {
fn read(&self, buf: &mut [u8]) -> Result<Option<usize>> {
if let Some(pty_lock) = self.pty.upgrade() {
let mut pty = pty_lock.borrow_mut();
@@ -60,28 +64,28 @@ impl Resource for PtySlave {
pty.mosi.push_front(packet[i..].to_vec());
}
Ok(i)
Ok(Some(i))
} else if self.flags & O_NONBLOCK == O_NONBLOCK {
Ok(0)
Err(Error::new(EAGAIN))
} else {
Err(Error::new(EWOULDBLOCK))
Ok(None)
}
} else {
Ok(0)
Ok(Some(0))
}
}
fn write(&self, buf: &[u8]) -> Result<usize> {
fn write(&self, buf: &[u8]) -> Result<Option<usize>> {
if let Some(pty_lock) = self.pty.upgrade() {
let mut pty = pty_lock.borrow_mut();
if pty.miso.len() >= 64 {
return Err(Error::new(EWOULDBLOCK));
return Ok(None);
}
pty.output(buf);
Ok(buf.len())
Ok(Some(buf.len()))
} else {
Err(Error::new(EPIPE))
}
@@ -110,20 +114,33 @@ impl Resource for PtySlave {
}
}
fn fevent(&self) -> Result<()> {
fn fevent(&mut self) -> Result<()> {
self.notified_read = false; // resend
self.notified_write = false;
Ok(())
}
fn fevent_count(&self) -> Option<usize> {
fn fevent_count(&mut self) -> Option<usize> {
if let Some(pty_lock) = self.pty.upgrade() {
let pty = pty_lock.borrow();
if let Some(data) = pty.mosi.front() {
Some(data.len())
if !self.notified_read {
self.notified_read = true;
Some(data.len())
} else {
None
}
} else {
self.notified_read = false;
None
}
} else {
Some(0)
}
}
fn fevent_writable(&mut self) -> bool {
let notified = self.notified_write;
self.notified_write = true;
!notified
}
}
+10 -7
View File
@@ -45,7 +45,7 @@ impl Resource for PtyTermios {
}
}
fn read(&self, buf: &mut [u8]) -> Result<usize> {
fn read(&self, buf: &mut [u8]) -> Result<Option<usize>> {
if let Some(pty_lock) = self.pty.upgrade() {
let pty = pty_lock.borrow();
let termios: &[u8] = pty.termios.deref();
@@ -55,13 +55,13 @@ impl Resource for PtyTermios {
buf[i] = termios[i];
i += 1;
}
Ok(i)
Ok(Some(i))
} else {
Ok(0)
Ok(Some(0))
}
}
fn write(&self, buf: &[u8]) -> Result<usize> {
fn write(&self, buf: &[u8]) -> Result<Option<usize>> {
if let Some(pty_lock) = self.pty.upgrade() {
let mut pty = pty_lock.borrow_mut();
let termios: &mut [u8] = pty.termios.deref_mut();
@@ -71,7 +71,7 @@ impl Resource for PtyTermios {
termios[i] = buf[i];
i += 1;
}
Ok(i)
Ok(Some(i))
} else {
Err(Error::new(EPIPE))
}
@@ -92,11 +92,14 @@ impl Resource for PtyTermios {
}
}
fn fevent(&self) -> Result<()> {
fn fevent(&mut self) -> Result<()> {
Err(Error::new(EBADF))
}
fn fevent_count(&self) -> Option<usize> {
fn fevent_count(&mut self) -> Option<usize> {
None
}
fn fevent_writable(&mut self) -> bool {
false
}
}
+10 -7
View File
@@ -45,7 +45,7 @@ impl Resource for PtyWinsize {
}
}
fn read(&self, buf: &mut [u8]) -> Result<usize> {
fn read(&self, buf: &mut [u8]) -> Result<Option<usize>> {
if let Some(pty_lock) = self.pty.upgrade() {
let pty = pty_lock.borrow();
let winsize: &[u8] = pty.winsize.deref();
@@ -55,13 +55,13 @@ impl Resource for PtyWinsize {
buf[i] = winsize[i];
i += 1;
}
Ok(i)
Ok(Some(i))
} else {
Ok(0)
Ok(Some(0))
}
}
fn write(&self, buf: &[u8]) -> Result<usize> {
fn write(&self, buf: &[u8]) -> Result<Option<usize>> {
if let Some(pty_lock) = self.pty.upgrade() {
let mut pty = pty_lock.borrow_mut();
let winsize: &mut [u8] = pty.winsize.deref_mut();
@@ -71,7 +71,7 @@ impl Resource for PtyWinsize {
winsize[i] = buf[i];
i += 1;
}
Ok(i)
Ok(Some(i))
} else {
Err(Error::new(EPIPE))
}
@@ -92,11 +92,14 @@ impl Resource for PtyWinsize {
}
}
fn fevent(&self) -> Result<()> {
fn fevent(&mut self) -> Result<()> {
Err(Error::new(EBADF))
}
fn fevent_count(&self) -> Option<usize> {
fn fevent_count(&mut self) -> Option<usize> {
None
}
fn fevent_writable(&mut self) -> bool {
false
}
}