Skip to content

Commit b1c067b

Browse files
epoll_ctl: add add event
Signed-off-by: Andy-Python-Programmer <[email protected]>
1 parent 67fca86 commit b1c067b

File tree

7 files changed

+111
-10
lines changed

7 files changed

+111
-10
lines changed

base-files/server.exe

-32 Bytes
Binary file not shown.

patches/mlibc/mlibc.patch

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
From c11445241f6016b07ed89c0be2617488cfb79265 Mon Sep 17 00:00:00 2001
1+
From 1f388ec59a355c18a21aa23226159d90fcb35912 Mon Sep 17 00:00:00 2001
22
From: Andy-Python-Programmer <[email protected]>
33
Date: Thu, 10 Feb 2022 19:12:25 +1100
44
Subject: [PATCH] yes
@@ -8,10 +8,10 @@ Signed-off-by: Andy-Python-Programmer <[email protected]>
88
.gitignore | 3 ++
99
options/rtdl/generic/linker.cpp | 2 +-
1010
sysdeps/aero/generic/aero.cpp | 12 ++++-
11-
sysdeps/aero/generic/filesystem.cpp | 68 +++++++++++++++++++++++++----
12-
sysdeps/aero/generic/signals.cpp | 8 +++-
11+
sysdeps/aero/generic/filesystem.cpp | 79 ++++++++++++++++++++++++++---
12+
sysdeps/aero/generic/signals.cpp | 8 ++-
1313
sysdeps/aero/include/aero/syscall.h | 12 +++++
14-
6 files changed, 92 insertions(+), 13 deletions(-)
14+
6 files changed, 103 insertions(+), 13 deletions(-)
1515

1616
diff --git a/.gitignore b/.gitignore
1717
index dbb35e8b..20c8d4c3 100644
@@ -68,7 +68,7 @@ index 7de909f5..4281beb9 100644
6868
#endif
6969
} // namespace mlibc
7070
diff --git a/sysdeps/aero/generic/filesystem.cpp b/sysdeps/aero/generic/filesystem.cpp
71-
index 6a13f19c..8679b28a 100644
71+
index 6a13f19c..d34dfce3 100644
7272
--- a/sysdeps/aero/generic/filesystem.cpp
7373
+++ b/sysdeps/aero/generic/filesystem.cpp
7474
@@ -1,3 +1,4 @@
@@ -150,11 +150,12 @@ index 6a13f19c..8679b28a 100644
150150
return 0;
151151
}
152152

153-
@@ -267,4 +297,26 @@ int sys_pipe(int *fds, int flags) {
153+
@@ -267,4 +297,37 @@ int sys_pipe(int *fds, int flags) {
154154

155155
return 0;
156156
}
157157
+
158+
+// epoll API syscalls:
158159
+int sys_epoll_create(int flags, int *fd) {
159160
+ auto result = syscall(SYS_EPOLL_CREATE, flags);
160161
+
@@ -166,6 +167,16 @@ index 6a13f19c..8679b28a 100644
166167
+ return 0;
167168
+}
168169
+
170+
+int sys_epoll_ctl(int epfd, int mode, int fd, struct epoll_event *ev) {
171+
+ auto result = syscall(SYS_EPOLL_CTL, epfd, mode, fd, ev);
172+
+
173+
+ if (result < 0) {
174+
+ return -result;
175+
+ }
176+
+
177+
+ return 0;
178+
+}
179+
+
169180
+int sys_eventfd_create(unsigned int initval, int flags, int *fd) {
170181
+ auto result = syscall(SYS_EVENT_FD, initval, flags);
171182
+

src/aero_kernel/src/fs/epoll.rs

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

20+
use aero_syscall::{prelude::EPollEvent, AeroSyscallError};
2021
use alloc::sync::Arc;
22+
use hashbrown::HashMap;
23+
24+
use crate::utils::sync::Mutex;
2125

2226
use super::inode::INodeInterface;
2327

24-
pub struct EPoll {}
28+
pub struct EPoll {
29+
events: Mutex<HashMap<usize, EPollEvent>>,
30+
}
2531

2632
impl EPoll {
2733
pub fn new() -> Arc<Self> {
28-
Arc::new(Self {})
34+
Arc::new(Self {
35+
events: Mutex::new(HashMap::new()),
36+
})
37+
}
38+
39+
/// Adds an event to the epoll instance.
40+
///
41+
/// ## Errors
42+
/// * `EEXIST`: The event already exists at `fd`.
43+
pub fn add_event(&self, fd: usize, event: EPollEvent) -> Result<(), AeroSyscallError> {
44+
let mut events = self.events.lock_irq();
45+
46+
if events.get(&fd).is_some() {
47+
return Err(AeroSyscallError::EEXIST);
48+
}
49+
50+
events.insert(fd, event);
51+
Ok(())
2952
}
3053
}
3154

55+
unsafe impl Send for EPoll {}
56+
unsafe impl Sync for EPoll {}
57+
3258
impl INodeInterface for EPoll {}

src/aero_kernel/src/syscall/fs.rs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,13 @@ use aero_syscall::{AeroSyscallError, OpenFlags, Stat};
2323
use crate::fs::epoll::EPoll;
2424
use crate::fs::eventfd::EventFd;
2525
use crate::fs::file_table::DuplicateHint;
26-
use crate::fs::inode::DirEntry;
26+
use crate::fs::inode::{DirEntry, INodeInterface};
2727
use crate::fs::pipe::Pipe;
2828
use crate::fs::{self, lookup_path, LookupMode};
2929
use crate::userland::scheduler;
3030

3131
use crate::fs::Path;
32+
use crate::utils::downcast;
3233

3334
#[aero_proc::syscall]
3435
pub fn write(fd: usize, buffer: &[u8]) -> Result<usize, AeroSyscallError> {
@@ -275,6 +276,8 @@ pub fn pipe(fds: &mut [usize; 2], flags: usize) -> Result<usize, AeroSyscallErro
275276

276277
#[aero_proc::syscall]
277278
pub fn unlink(fd: usize, path: &Path, flags: usize) -> Result<usize, AeroSyscallError> {
279+
log::debug!("{path:?}");
280+
278281
// TODO: Make use of the open flags.
279282
let _flags = OpenFlags::from_bits(flags).ok_or(AeroSyscallError::EINVAL)?;
280283
let name = path.container();
@@ -404,6 +407,35 @@ pub fn epoll_create(flags: usize) -> Result<usize, AeroSyscallError> {
404407
.open_file(entry, OpenFlags::O_RDWR)?)
405408
}
406409

410+
/// Used to add, modify, or remove entries in the interest list of the
411+
/// epoll instance referred to by the file descriptor. It requests that
412+
/// the operation be performed for the target file descriptor.
413+
#[aero_proc::syscall]
414+
pub fn epoll_ctl(
415+
epfd: usize,
416+
mode: usize,
417+
fd: usize,
418+
event: &mut EPollEvent,
419+
) -> Result<usize, AeroSyscallError> {
420+
let current_task = scheduler::get_scheduler().current_task();
421+
let epfd = current_task
422+
.file_table
423+
.get_handle(epfd)
424+
.ok_or(AeroSyscallError::EBADFD)?;
425+
426+
match mode {
427+
EPOLL_CTL_ADD => {
428+
let epoll = downcast::<dyn INodeInterface, EPoll>(&epfd.inode())
429+
.ok_or(AeroSyscallError::EINVAL)?;
430+
431+
epoll.add_event(fd, event.clone())?;
432+
Ok(0)
433+
}
434+
435+
_ => unreachable!("epoll_ctl: unknown mode {mode}"),
436+
}
437+
}
438+
407439
#[aero_proc::syscall]
408440
pub fn event_fd(_initval: usize, flags: usize) -> Result<usize, AeroSyscallError> {
409441
let flags = EventFdFlags::from_bits(flags).ok_or(AeroSyscallError::EINVAL)?;

src/aero_kernel/src/syscall/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ pub fn generic_do_syscall(
206206

207207
// epoll calls:
208208
SYS_EPOLL_CREATE => fs::epoll_create(b),
209+
SYS_EPOLL_CTL => fs::epoll_ctl(b, c, d, e),
209210

210211
SYS_SOCKET => net::socket(b, c, d),
211212
SYS_BIND => net::bind(b, c, d),

src/aero_syscall/src/consts.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,27 @@ bitflags::bitflags! {
105105
}
106106
}
107107

108+
pub const EPOLL_CTL_ADD: usize = 1;
109+
pub const EPOLL_CTL_DEL: usize = 2;
110+
pub const EPOLL_CTL_MOD: usize = 3;
111+
112+
// structures and uninons for the epoll API:
113+
#[derive(Copy, Clone)]
114+
#[repr(C)]
115+
pub union EPollData {
116+
pub ptr: *mut u8,
117+
pub fd: i32,
118+
pub u32: u32,
119+
pub u64: u64,
120+
}
121+
122+
#[derive(Copy, Clone)]
123+
#[repr(C)]
124+
pub struct EPollEvent {
125+
pub events: u32,
126+
pub data: EPollData,
127+
}
128+
108129
// constants for event fd:
109130
bitflags::bitflags! {
110131
// mlibc/options/linux/include/sys/eventfd.h

userland/apps/aero_shell/src/main.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,17 @@ fn repl(history: &mut Vec<String>) -> Result<(), AeroSyscallError> {
203203

204204
let argv = argv.as_slice();
205205

206-
match sys_exec(cmd, argv, &["TERM=linux"]) {
206+
match sys_exec(
207+
cmd,
208+
argv,
209+
&[
210+
"TERM=linux",
211+
// The `XDG_RUNTIME_DIR` enviornment variable tells the tells any program you
212+
// run where to find a user-specific directory in which it can store small
213+
// temporary files.
214+
"XDG_RUNTIME_DIR=temp",
215+
],
216+
) {
207217
Ok(_) => core::unreachable!(),
208218
Err(AeroSyscallError::EISDIR) => error!("{}: is a directory", cmd),
209219
Err(AeroSyscallError::ENOENT) => error!("{}: command not found", cmd),

0 commit comments

Comments
 (0)