Skip to main content

strat9_kernel/syscall/
poll.rs

1/*
2 * This file is part of strat9-os.
3 *
4 * strat9-os is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * strat9-os is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with strat9-os. If not, see <https://www.gnu.org/licenses/>.
16 *
17 * Website: https://www.strat9-os.org
18 * Repository: https://github.com/strat9-os/strat9-os
19 *
20 * Copyright (C) 2023-26 Guillaume Gielly
21 */
22
23use crate::{
24    memory::{UserSliceRead, UserSliceWrite},
25    process::current_task_clone,
26    syscall::error::SyscallError,
27};
28
29const POLLIN: i16 = 0x0001;
30const POLLOUT: i16 = 0x0004;
31const POLLNVAL: i16 = 0x0020;
32
33const POLLFD_SIZE: usize = 8; // sizeof(pollfd) = i32 + i16 + i16
34
35/// Reads pollfd.
36fn read_pollfd(buf: &[u8], i: usize) -> (i32, i16) {
37    let off = i * POLLFD_SIZE;
38    let fd = i32::from_le_bytes([buf[off], buf[off + 1], buf[off + 2], buf[off + 3]]);
39    let events = i16::from_le_bytes([buf[off + 4], buf[off + 5]]);
40    (fd, events)
41}
42
43/// Writes revents.
44fn write_revents(buf: &mut [u8], i: usize, revents: i16) {
45    let off = i * POLLFD_SIZE + 6;
46    let bytes = revents.to_le_bytes();
47    buf[off] = bytes[0];
48    buf[off + 1] = bytes[1];
49}
50
51/// Performs the sys poll operation.
52pub fn sys_poll(fds_ptr: u64, nfds: u64, _timeout_ms: u64) -> Result<u64, SyscallError> {
53    if nfds == 0 {
54        return Ok(0);
55    }
56    if nfds > 1024 {
57        return Err(SyscallError::InvalidArgument);
58    }
59    let n = nfds as usize;
60    let byte_len = n * POLLFD_SIZE;
61
62    let task = current_task_clone().ok_or(SyscallError::Fault)?;
63    let fd_table = unsafe { &*task.process.fd_table.get() };
64
65    let reader = UserSliceRead::new(fds_ptr, byte_len)?;
66    let mut buf = alloc::vec![0u8; byte_len];
67    reader.copy_to(&mut buf);
68
69    let mut ready_count = 0u64;
70    for i in 0..n {
71        let (fd, events) = read_pollfd(&buf, i);
72        let revents = if fd < 0 {
73            0i16
74        } else {
75            match fd_table.get(fd as u32) {
76                Ok(_) => {
77                    let mut r = 0i16;
78                    if events & POLLIN != 0 {
79                        r |= POLLIN;
80                    }
81                    if events & POLLOUT != 0 {
82                        r |= POLLOUT;
83                    }
84                    r
85                }
86                Err(_) => POLLNVAL,
87            }
88        };
89        write_revents(&mut buf, i, revents);
90        if revents != 0 {
91            ready_count += 1;
92        }
93    }
94
95    let writer = UserSliceWrite::new(fds_ptr, byte_len)?;
96    writer.copy_from(&buf);
97
98    Ok(ready_count)
99}