Compare commits

...

4 Commits

Author SHA1 Message Date
207d901de8
kernel+libc: Add the poll() syscall
All checks were successful
continuous-integration/drone/push Build is passing
2023-08-02 14:48:20 +02:00
df77fc8de8
libluna: Remove make_array() and destroy_array()
Placement new on arrays is a bit unreliable and could cause out-of-bounds data accesses.
2023-08-02 14:47:58 +02:00
b1fb6dee8a
login: Create a new process group to log in 2023-08-02 14:47:13 +02:00
aac8280e8a
libc+libos: Properly propagate errors through fgetc() and File::getchar()
This restores proper ^C behavior in the shell.
2023-08-02 14:46:47 +02:00
13 changed files with 118 additions and 30 deletions

View File

@ -26,11 +26,12 @@ Result<int> luna_main(int argc, char** argv)
String name;
setpgid(0, 0);
signal(SIGTTOU, SIG_IGN);
if (isatty(STDIN_FILENO)) tcsetpgrp(STDIN_FILENO, getpgid(0));
if (username.is_empty())
{
signal(SIGTTOU, SIG_IGN);
if (isatty(STDIN_FILENO)) tcsetpgrp(STDIN_FILENO, getpgid(0));
auto input = os::File::standard_input();

View File

@ -42,6 +42,7 @@ set(SOURCES
src/sys/resource.cpp
src/sys/signal.cpp
src/sys/socket.cpp
src/sys/poll.cpp
src/fs/VFS.cpp
src/fs/Pipe.cpp
src/fs/Mount.cpp

View File

@ -754,8 +754,8 @@ Result<u64> ATADevice::read(u8* buf, usize offset, usize size) const
ScopedKMutexLock<100> lock(m_drive->channel()->lock());
// FIXME: Don't always allocate this if we don't need it.
auto* temp = TRY(make_array<u8>(block_size));
auto guard = make_scope_guard([temp] { delete[] temp; });
auto* temp = (u8*)TRY(malloc_impl(block_size));
auto guard = make_scope_guard([temp] { free_impl(temp); });
if (offset % block_size)
{

View File

@ -41,8 +41,8 @@ namespace GPT
u32 partition_index = 1;
auto* table = TRY(make_array<PartitionEntry>(header.num_partitions));
auto guard = make_scope_guard([table] { delete[] table; });
auto* table = (PartitionEntry*)TRY(calloc_impl(header.num_partitions, sizeof(PartitionEntry)));
auto guard = make_scope_guard([table] { free_impl(table); });
nread = TRY(device->read((u8*)table, partition_table_start, sizeof(PartitionEntry) * header.num_partitions));
check(nread == sizeof(PartitionEntry) * header.num_partitions);

View File

@ -91,8 +91,8 @@ namespace Ext2
const usize inode_size = m_metadata.size;
const usize block_size = m_fs->m_block_size;
u8* const buf = TRY(make_array<u8>(block_size));
auto guard = make_scope_guard([buf] { delete[] buf; });
u8* const buf = (u8*)TRY(calloc_impl(block_size, 1));
auto guard = make_scope_guard([buf] { free_impl(buf); });
m_entries.clear();

70
kernel/src/sys/poll.cpp Normal file
View File

@ -0,0 +1,70 @@
#include "fs/VFS.h"
#include "memory/MemoryManager.h"
#include "sys/Syscall.h"
#include "thread/Scheduler.h"
#include <bits/poll.h>
Result<u64> sys_poll(Registers* regs, SyscallArgs args)
{
struct pollfd* fds = (struct pollfd*)args[0];
nfds_t nfds = (nfds_t)args[1];
int timeout = (int)args[2];
struct pollfd* kfds = (struct pollfd*)TRY(malloc_impl(nfds * sizeof(pollfd)));
auto guard = make_scope_guard([kfds] { free_impl(kfds); });
if (!MemoryManager::copy_from_user(fds, kfds, nfds * sizeof(pollfd))) return err(EFAULT);
Vector<SharedPtr<VFS::Inode>> inodes;
auto* current = Scheduler::current();
for (nfds_t i = 0; i < nfds; i++)
{
int fd = kfds[i].fd;
auto maybe_inode = current->resolve_fd(fd);
if (maybe_inode.has_error())
{
kfds[i].revents |= POLLNVAL;
TRY(inodes.try_append({}));
}
else
TRY(inodes.try_append(maybe_inode.release_value()->description->inode));
}
bool infinite = timeout < 0;
int fds_with_events;
do {
fds_with_events = 0;
for (nfds_t i = 0; i < nfds; i++)
{
auto& inode = inodes[i];
if (!inode) continue;
if (kfds[i].events & POLLIN && !inode->will_block_if_read())
{
fds_with_events++;
kfds[i].revents |= POLLIN;
}
}
if (!fds_with_events && (timeout > 0 || infinite))
{
kernel_sleep(10);
timeout -= (int)current->sleep_ticks_left;
if (current->interrupted)
{
guard.deactivate();
free_impl(kfds);
if (current->will_invoke_signal_handler()) return err(EINTR);
current->process_pending_signals(regs);
return err(EINTR);
}
continue;
}
} while (false);
MemoryManager::copy_to_user(fds, kfds, nfds * sizeof(pollfd));
return fds_with_events;
}

View File

@ -3,7 +3,8 @@
#ifndef _BITS_FIXED_SIZE_TYPES_H
#define _BITS_FIXED_SIZE_TYPES_H
#if !defined(_SYS_TYPES_H) && !defined(_BITS_SETJMP_TYPES_H) && !defined(_BITS_MAKEDEV_H) && !defined(_BITS_TERMIOS_H)
#if !defined(_SYS_TYPES_H) && !defined(_BITS_SETJMP_TYPES_H) && !defined(_BITS_MAKEDEV_H) && \
!defined(_BITS_TERMIOS_H) && !defined(_BITS_POLL_H)
#error "Never include bits/fixed-size-types.h, use the standard <stdint.h> header instead."
#endif

21
libc/include/bits/poll.h Normal file
View File

@ -0,0 +1,21 @@
/* bits/poll.h: The pollfd data structure. */
#ifndef _BITS_POLL_H
#define _BITS_POLL_H
#include <bits/fixed-size-types.h>
#define POLLIN 0
#define POLLERR 1
#define POLLNVAL 2
typedef __u64_t nfds_t;
struct pollfd
{
int fd;
short events;
short revents;
};
#endif

View File

@ -432,7 +432,12 @@ extern "C"
{
u8 value;
ssize_t rc = read_from_buffer(stream, &value, 1);
if (rc <= 0) return EOF;
if (rc < 0)
{
stream->_err = 1;
return EOF;
}
else if (rc == 0) { return EOF; }
return value;
}

View File

@ -10,19 +10,7 @@ template <typename T, class... Args> [[nodiscard]] Result<T*> make(Args... args)
return result;
}
template <typename T> [[nodiscard]] Result<T*> make_array(usize count)
{
T* const result = (T*)TRY(calloc_impl(count, sizeof(T)));
new (result) T[count];
return result;
}
template <typename T> void destroy(T* item)
{
delete item;
}
template <typename T> void destroy_array(T* item)
{
delete[] item;
}

View File

@ -7,7 +7,7 @@
_e(fstatat) _e(chdir) _e(getcwd) _e(unlinkat) _e(uname) _e(sethostname) _e(dup2) _e(pipe) _e(mount) \
_e(umount) _e(pstat) _e(getrusage) _e(symlinkat) _e(readlinkat) _e(umask) _e(linkat) _e(faccessat) \
_e(pivot_root) _e(sigreturn) _e(sigaction) _e(kill) _e(sigprocmask) _e(setpgid) _e(isatty) \
_e(getpgid) _e(socket) _e(bind) _e(connect) _e(listen) _e(accept)
_e(getpgid) _e(socket) _e(bind) _e(connect) _e(listen) _e(accept) _e(poll)
enum Syscalls
{

View File

@ -149,9 +149,9 @@ namespace os
int current;
while (true)
{
current = fgetc(m_file);
current = TRY(getchar());
if (current == -1) break;
if (current == EOF) break;
TRY(data.try_append((char)current));
@ -208,7 +208,9 @@ namespace os
Result<int> File::getchar()
{
return fgetc(m_file);
int rc = fgetc(m_file);
if (rc == EOF && ferror(m_file)) return err(errno);
return rc;
}
void File::set_close_on_exec()

View File

@ -82,12 +82,11 @@ namespace os::FileSystem
TRY(stat(path, st, false));
if (!S_ISLNK(st.st_mode)) return String {};
char* buf = TRY(make_array<char>(st.st_size + 1));
auto guard = make_scope_guard([buf] { delete[] buf; });
char* buf = (char*)TRY(calloc_impl(st.st_size + 1, 1));
auto guard = make_scope_guard([buf] { free_impl(buf); });
usize nread = TRY(
Result<usize>::from_syscall(syscall(SYS_readlinkat, path.dirfd(), path.name().chars(), buf, st.st_size)));
buf[nread] = '\0';
guard.deactivate();
return String { buf, nread };