Skip to content

Commit

Permalink
fs+console: implement poll for system_console
Browse files Browse the repository at this point in the history
  • Loading branch information
mosmeh committed Jun 30, 2024
1 parent 6a2b489 commit ccfd5e2
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 11 deletions.
6 changes: 6 additions & 0 deletions kernel/console/system_console.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,17 @@ static int system_console_device_ioctl(file_description* desc, int request,
return file_description_ioctl(active_console, request, user_argp);
}

static short system_console_device_poll(file_description* desc, short events) {
(void)desc;
return file_description_poll(active_console, events);
}

static struct inode* system_console_device_get(void) {
static file_ops fops = {
.read = system_console_device_read,
.write = system_console_device_write,
.ioctl = system_console_device_ioctl,
.poll = system_console_device_poll,
};
static struct inode inode = {
.fops = &fops, .mode = S_IFCHR, .rdev = makedev(5, 1), .ref_count = 1};
Expand Down
14 changes: 14 additions & 0 deletions kernel/fs/fs.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <kernel/api/dirent.h>
#include <kernel/api/fcntl.h>
#include <kernel/api/stdio.h>
#include <kernel/api/sys/poll.h>
#include <kernel/lock.h>
#include <kernel/memory/memory.h>
#include <kernel/panic.h>
Expand Down Expand Up @@ -267,6 +268,19 @@ int file_description_getdents(file_description* desc,
return inode->fops->getdents(desc, callback, ctx);
}

NODISCARD short file_description_poll(file_description* desc, short events) {
struct inode* inode = desc->inode;
if (!inode->fops->poll)
return events & (POLLIN | POLLOUT);
short revents = inode->fops->poll(desc, events);
ASSERT(revents >= 0);
if (!(events & POLLIN))
ASSERT(!(revents & POLLIN));
if (!(events & POLLOUT))
ASSERT(!(revents & POLLOUT));
return revents;
}

int file_description_block(file_description* desc,
bool (*should_unblock)(file_description*)) {
if ((desc->flags & O_NONBLOCK) && !should_unblock(desc))
Expand Down
1 change: 1 addition & 0 deletions kernel/fs/fs.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ NODISCARD int file_description_ioctl(file_description*, int request,
void* user_argp);
NODISCARD int file_description_getdents(file_description*, getdents_callback_fn,
void* ctx);
NODISCARD short file_description_poll(file_description*, short events);

NODISCARD int file_description_block(file_description*,
bool (*should_unblock)(file_description*));
Expand Down
12 changes: 1 addition & 11 deletions kernel/syscall/poll.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,8 @@ static bool poll_should_unblock(struct poll_blocker* blocker) {
if (!desc)
continue;

file_ops* fops = desc->inode->fops;
struct pollfd* fd = blocker->fds + i;
if (fops->poll) {
fd->revents = fops->poll(desc, fd->events);
ASSERT(fd->revents >= 0);
if (!(fd->events & POLLIN))
ASSERT(!(fd->revents & POLLIN));
if (!(fd->events & POLLOUT))
ASSERT(!(fd->revents & POLLOUT));
} else {
fd->revents = fd->events & (POLLIN | POLLOUT);
}
fd->revents = file_description_poll(desc, fd->events);
if (fd->revents)
++blocker->num_events;
}
Expand Down

0 comments on commit ccfd5e2

Please sign in to comment.