Skip to content

Commit 366dcd4

Browse files
syscalls::poll: init
Signed-off-by: Andy-Python-Programmer <[email protected]>
1 parent c576fd1 commit 366dcd4

File tree

12 files changed

+224
-40
lines changed

12 files changed

+224
-40
lines changed

patches/mlibc/mlibc.patch

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
From d3bcf4e954ce27b14f5ffdb994486c984d1773ad Mon Sep 17 00:00:00 2001
1+
From a1c97a1acc4f67a2add919e2dbf8a7f35d1a107a Mon Sep 17 00:00:00 2001
22
From: Matt Taylor <[email protected]>
33
Date: Fri, 17 Jun 2022 15:28:34 +0100
44
Subject: [PATCH] abi-bits: add domainname to utsname
@@ -10,8 +10,9 @@ Subject: [PATCH] abi-bits: add domainname to utsname
1010
options/glibc/generic/execinfo.cpp | 6 +-
1111
options/rtdl/generic/linker.cpp | 2 +-
1212
sysdeps/aero/generic/aero.cpp | 10 ++-
13-
sysdeps/aero/include/aero/syscall.h | 1 +
14-
7 files changed, 104 insertions(+), 17 deletions(-)
13+
sysdeps/aero/generic/filesystem.cpp | 19 ++++-
14+
sysdeps/aero/include/aero/syscall.h | 2 +
15+
8 files changed, 122 insertions(+), 19 deletions(-)
1516

1617
diff --git a/ABI_BREAKS.md b/ABI_BREAKS.md
1718
index 0cd3993b..d2a0bb7d 100644
@@ -204,15 +205,49 @@ index 43ddb906..f4fe182f 100644
204205
int sys_seteuid(uid_t euid) UNIMPLEMENTED("sys_seteuid")
205206

206207
gid_t sys_getgid() {
208+
diff --git a/sysdeps/aero/generic/filesystem.cpp b/sysdeps/aero/generic/filesystem.cpp
209+
index a3e2aca2..a56438f0 100644
210+
--- a/sysdeps/aero/generic/filesystem.cpp
211+
+++ b/sysdeps/aero/generic/filesystem.cpp
212+
@@ -158,11 +158,26 @@ int sys_tcsetattr(int fd, int optional_action, const struct termios *attr) {
213+
return 0;
214+
}
215+
216+
-int sys_poll(struct pollfd *fds, nfds_t count, int timeout, int *num_events) {
217+
- mlibc::infoLogger() << "sys_poll() is not implemented" << frg::endlog;
218+
+int sys_ppoll(struct pollfd *fds, int nfds, const struct timespec *timeout,
219+
+ const sigset_t *sigmask, int *num_events) {
220+
+ auto result = syscall(SYS_POLL, fds, nfds, timeout, sigmask);
221+
+
222+
+ if (result < 0) {
223+
+ return -result;
224+
+ }
225+
+
226+
+ *num_events = result;
227+
return 0;
228+
}
229+
230+
+int sys_poll(struct pollfd *fds, nfds_t count, int timeout, int *num_events) {
231+
+ struct timespec ts;
232+
+ ts.tv_sec = timeout / 1000;
233+
+ ts.tv_nsec = (timeout % 1000) * 1000000;
234+
+
235+
+ return sys_ppoll(fds, count, &ts, NULL, num_events);
236+
+}
237+
+
238+
int sys_mkdir(const char *path, mode_t) {
239+
auto result = syscall(SYS_MKDIR, path, strlen(path));
240+
207241
diff --git a/sysdeps/aero/include/aero/syscall.h b/sysdeps/aero/include/aero/syscall.h
208-
index 12f8dc61..cf57bd3d 100644
242+
index 12f8dc61..fcc219a9 100644
209243
--- a/sysdeps/aero/include/aero/syscall.h
210244
+++ b/sysdeps/aero/include/aero/syscall.h
211-
@@ -64,6 +64,7 @@
245+
@@ -64,6 +64,8 @@
212246
#define SYS_FUTEX_WAIT 57
213247
#define SYS_FUTEX_WAKE 58
214248
#define SYS_LINK 59
215249
+#define SYS_BACKTRACE 60
250+
+#define SYS_POLL 61
216251

217252
// Invalid syscall used to trigger a log error in the kernel (as a hint)
218253
// so, that we can implement the syscall in the kernel.

src/aero_kernel/src/drivers/tty.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,13 @@
1717
* along with Aero. If not, see <https://www.gnu.org/licenses/>.
1818
*/
1919

20-
use aero_syscall::prelude::EPollEventFlags;
2120
use alloc::string::String;
2221
use alloc::sync::{Arc, Weak};
2322
use alloc::vec::Vec;
2423

2524
use crate::fs;
2625
use crate::fs::devfs;
27-
use crate::fs::inode::{self, PollTable};
26+
use crate::fs::inode::{self, PollFlags, PollTable};
2827
use crate::rendy;
2928

3029
use crate::fs::inode::INodeInterface;
@@ -258,12 +257,12 @@ impl INodeInterface for Tty {
258257
Ok(buffer.len())
259258
}
260259

261-
fn poll(&self, table: Option<&mut PollTable>) -> fs::Result<EPollEventFlags> {
260+
fn poll(&self, table: Option<&mut PollTable>) -> fs::Result<PollFlags> {
262261
table.map(|e| e.insert(&self.block_queue));
263-
let mut events = EPollEventFlags::default();
262+
let mut events = PollFlags::empty();
264263

265264
if self.stdin.lock_irq().is_complete() {
266-
events.insert(EPollEventFlags::IN);
265+
events.insert(PollFlags::IN);
267266
}
268267

269268
Ok(events)

src/aero_kernel/src/fs/devfs.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ use crate::mem::paging::*;
3535
use crate::rendy::RendyInfo;
3636

3737
use super::cache::{DirCacheItem, INodeCacheItem};
38-
use super::inode::{INodeInterface, PollTable};
38+
use super::inode::{INodeInterface, PollFlags, PollTable};
3939
use super::ramfs::RamFs;
4040
use super::FileSystemError;
4141
use super::{FileSystem, Result, MOUNT_MANAGER};
@@ -129,7 +129,7 @@ impl INodeInterface for DevINode {
129129
self.0.inode().ioctl(command, arg)
130130
}
131131

132-
fn poll(&self, table: Option<&mut PollTable>) -> Result<EPollEventFlags> {
132+
fn poll(&self, table: Option<&mut PollTable>) -> Result<PollFlags> {
133133
self.0.inode().poll(table)
134134
}
135135
}

src/aero_kernel/src/fs/epoll.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
* along with Aero. If not, see <https://www.gnu.org/licenses/>.
1818
*/
1919

20-
use aero_syscall::prelude::EPollEvent;
20+
use aero_syscall::prelude::{EPollEvent, EPollEventFlags};
2121
use aero_syscall::SyscallError;
2222

2323
use alloc::sync::Arc;
@@ -134,7 +134,7 @@ impl EPoll {
134134
.ok_or(FileSystemError::NotSupported)?; // EINVAL
135135

136136
let flags = epoll_event.events;
137-
let ready = fd.inode().poll(None)?;
137+
let ready: EPollEventFlags = fd.inode().poll(None)?.into();
138138

139139
if ready.contains(flags) {
140140
// The registered event is ready; increment the number of ready events
@@ -161,7 +161,7 @@ impl EPoll {
161161
scheduler::get_scheduler().inner.await_io()?;
162162

163163
for fd in fds.iter() {
164-
let ready = fd.inode().poll(None)?;
164+
let ready: EPollEventFlags = fd.inode().poll(None)?.into();
165165
let flags = table
166166
.get(&fd.fd)
167167
.ok_or(FileSystemError::NotSupported)?
@@ -170,6 +170,7 @@ impl EPoll {
170170
if ready.contains(flags) {
171171
// The event is ready; break out of the search loop and set ready
172172
// events to 1.
173+
ret_events[0].events = ready & flags;
173174
n = 1;
174175
break 'search;
175176
}

src/aero_kernel/src/fs/eventfd.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,9 @@
1717
* along with Aero. If not, see <https://www.gnu.org/licenses/>.
1818
*/
1919

20-
use aero_syscall::prelude::EPollEventFlags;
2120
use alloc::sync::Arc;
2221

23-
use super::inode::{INodeInterface, PollTable};
22+
use super::inode::{INodeInterface, PollFlags, PollTable};
2423
use crate::utils::sync::{BlockQueue, Mutex};
2524

2625
pub struct EventFd {
@@ -50,22 +49,22 @@ impl INodeInterface for EventFd {
5049
unimplemented!()
5150
}
5251

53-
fn poll(&self, table: Option<&mut PollTable>) -> super::Result<EPollEventFlags> {
52+
fn poll(&self, table: Option<&mut PollTable>) -> super::Result<PollFlags> {
5453
let count = self.count.lock();
55-
let mut events = EPollEventFlags::default();
54+
let mut events = PollFlags::empty();
5655

5756
table.map(|e| e.insert(&self.wq)); // listen for changes
5857

5958
if *count > 0 {
60-
events.insert(EPollEventFlags::IN);
59+
events.insert(PollFlags::IN);
6160
}
6261

6362
if *count == usize::MAX {
64-
events.insert(EPollEventFlags::ERR);
63+
events.insert(PollFlags::ERR);
6564
}
6665

6766
if *count < (usize::MAX - 1) {
68-
events.insert(EPollEventFlags::OUT); // possible to write a value of at least "1" without blocking.
67+
events.insert(PollFlags::OUT); // possible to write a value of at least "1" without blocking.
6968
}
7069

7170
Ok(events)

src/aero_kernel/src/fs/inode.rs

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
use core::sync::atomic::{AtomicUsize, Ordering};
2121

22-
use aero_syscall::prelude::EPollEventFlags;
22+
use aero_syscall::prelude::{EPollEventFlags, PollEventFlags};
2323
use aero_syscall::{MMapFlags, OpenFlags};
2424

2525
use alloc::string::String;
@@ -66,6 +66,53 @@ impl Drop for PollTable {
6666
}
6767
}
6868

69+
bitflags::bitflags! {
70+
pub struct PollFlags: usize {
71+
/// The associated file is available for read operations.
72+
const IN = 1 << 1;
73+
/// The associated file is available for write operations.
74+
const OUT = 1 << 2;
75+
/// Error condition happened on the associated file descriptor.
76+
const ERR = 1 << 3;
77+
}
78+
}
79+
80+
impl From<PollFlags> for EPollEventFlags {
81+
fn from(poll: PollFlags) -> Self {
82+
let mut flags = Self::empty();
83+
84+
if poll.contains(PollFlags::IN) {
85+
flags |= Self::IN;
86+
}
87+
if poll.contains(PollFlags::OUT) {
88+
flags |= Self::OUT;
89+
}
90+
if poll.contains(PollFlags::ERR) {
91+
flags |= Self::ERR;
92+
}
93+
94+
flags
95+
}
96+
}
97+
98+
impl From<PollFlags> for PollEventFlags {
99+
fn from(poll: PollFlags) -> Self {
100+
let mut flags = Self::empty();
101+
102+
if poll.contains(PollFlags::IN) {
103+
flags |= Self::IN;
104+
}
105+
if poll.contains(PollFlags::OUT) {
106+
flags |= Self::OUT;
107+
}
108+
if poll.contains(PollFlags::ERR) {
109+
flags |= Self::ERR;
110+
}
111+
112+
flags
113+
}
114+
}
115+
69116
/// An inode describes a file. An inode structure holds metadata of the
70117
/// inode which includes its type, size, the number of links referring to it,
71118
/// and the list of blocks holding the file's content. For example device files,
@@ -185,7 +232,7 @@ pub trait INodeInterface: Send + Sync {
185232
Err(FileSystemError::NotSocket)
186233
}
187234

188-
fn poll(&self, _table: Option<&mut PollTable>) -> Result<EPollEventFlags> {
235+
fn poll(&self, _table: Option<&mut PollTable>) -> Result<PollFlags> {
189236
Err(FileSystemError::NotSupported)
190237
}
191238

src/aero_kernel/src/fs/ramfs.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
2424
use core::sync::atomic::{AtomicUsize, Ordering};
2525

26-
use aero_syscall::prelude::EPollEventFlags;
2726
use aero_syscall::MMapFlags;
2827
use alloc::collections::BTreeMap;
2928
use alloc::string::{String, ToString};
@@ -38,7 +37,7 @@ use crate::utils::sync::Mutex;
3837
use super::cache::{self, CacheWeak};
3938
use super::cache::{CachedINode, DirCacheItem, INodeCacheItem, INodeCacheWeakItem};
4039
use super::devfs::DevINode;
41-
use super::inode::{DirEntry, FileType, INodeInterface, PollTable};
40+
use super::inode::{DirEntry, FileType, INodeInterface, PollFlags, PollTable};
4241
use super::inode::{FileContents, Metadata};
4342
use super::{FileSystem, FileSystemError, Result};
4443

@@ -393,7 +392,7 @@ impl INodeInterface for LockedRamINode {
393392
}
394393
}
395394

396-
fn poll(&self, table: Option<&mut PollTable>) -> Result<EPollEventFlags> {
395+
fn poll(&self, table: Option<&mut PollTable>) -> Result<PollFlags> {
397396
let this = self.0.read();
398397

399398
match &this.contents {

src/aero_kernel/src/socket/unix.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,13 @@
1919

2020
use aero_syscall::SocketAddrUnix;
2121

22-
use aero_syscall::prelude::EPollEventFlags;
2322
use alloc::string::String;
2423
use alloc::sync::{Arc, Weak};
2524
use alloc::vec::Vec;
2625
use spin::RwLock;
2726

2827
use crate::fs;
29-
use crate::fs::inode::{DirEntry, FileType, INodeInterface, Metadata, PollTable};
28+
use crate::fs::inode::{DirEntry, FileType, INodeInterface, Metadata, PollFlags, PollTable};
3029
use crate::fs::{FileSystemError, Path, Result};
3130
use crate::utils::sync::BlockQueue;
3231

@@ -65,7 +64,7 @@ impl UnixSocketBacklog {
6564
}
6665

6766
pub fn len(&self) -> usize {
68-
self.backlog.as_ref().map(|e| e.len()).unwrap()
67+
self.backlog.as_ref().map(|e| e.len()).unwrap_or_default()
6968
}
7069

7170
pub fn update_capacity(&mut self, capacity: usize) {
@@ -164,13 +163,13 @@ impl INodeInterface for UnixSocket {
164163
Ok(())
165164
}
166165

167-
fn poll(&self, table: Option<&mut PollTable>) -> Result<EPollEventFlags> {
166+
fn poll(&self, table: Option<&mut PollTable>) -> Result<PollFlags> {
168167
table.map(|e| e.insert(&self.wq));
169168

170-
let mut events = EPollEventFlags::default();
169+
let mut events = PollFlags::empty();
171170

172171
if self.inner.read().backlog.len() > 0 {
173-
events.insert(EPollEventFlags::OUT);
172+
events.insert(PollFlags::OUT);
174173
}
175174

176175
Ok(events)

0 commit comments

Comments
 (0)