Compare commits

...

2 Commits

Author SHA1 Message Date
7e655e320a
kernel+libc: Add the lseek() syscall, and fseek, ftell, rewind, fsetpos, and fgetpos
All checks were successful
continuous-integration/drone/push Build is passing
2023-03-12 13:15:24 +01:00
292433dc39
kernel+libc: Add the write() system call, and fwrite() 2023-03-12 11:37:41 +01:00
8 changed files with 154 additions and 5 deletions

View File

@ -3,6 +3,9 @@
#include "memory/MemoryManager.h"
#include "sys/Syscall.h"
#include "thread/Scheduler.h"
#include <bits/seek.h>
#include <luna/SafeArithmetic.h>
#include <sys/types.h>
Result<u64> sys_read(Registers*, SyscallArgs args)
{
@ -26,3 +29,57 @@ Result<u64> sys_read(Registers*, SyscallArgs args)
return nread;
}
Result<u64> sys_write(Registers*, SyscallArgs args)
{
int fd = (int)args[0];
const u8* buf = (const u8*)args[1];
usize size = (usize)args[2];
if (!MemoryManager::validate_user_read(buf, size)) return err(EFAULT);
if (fd < 0 || fd >= FD_MAX) return err(EBADF);
Thread* current = Scheduler::current();
Option<FileDescriptor>& descriptor = current->fd_table->fds[fd];
if (!descriptor.has_value()) return err(EBADF);
usize nwritten = TRY(descriptor->inode->write(buf, descriptor->offset, size));
descriptor->offset += nwritten;
return nwritten;
}
Result<u64> sys_lseek(Registers*, SyscallArgs args)
{
int fd = (int)args[0];
off_t offset = (long)args[1];
int whence = (int)args[2];
if (fd < 0 || fd >= FD_MAX) return err(EBADF);
Thread* current = Scheduler::current();
Option<FileDescriptor>& descriptor = current->fd_table->fds[fd];
if (!descriptor.has_value()) return err(EBADF);
off_t new_offset;
switch (whence)
{
case SEEK_SET: new_offset = offset; break;
case SEEK_CUR: new_offset = TRY(safe_add((long)descriptor->offset, offset)); break;
case SEEK_END: todo();
default: return err(EINVAL);
}
if (new_offset < 0) return err(EINVAL);
descriptor->offset = (usize)new_offset;
return (u64)new_offset;
}

10
libc/include/bits/seek.h Normal file
View File

@ -0,0 +1,10 @@
/* bits/seek.h: SEEK_* constants for lseek() and fseek(). */
#ifndef _BITS_SEEK_H
#define _BITS_SEEK_H
#define SEEK_SET 0
#define SEEK_CUR 1
#define SEEK_END 2
#endif

View File

@ -3,6 +3,7 @@
#ifndef _STDIO_H
#define _STDIO_H
#include <bits/seek.h>
#include <stdarg.h>
#include <sys/types.h>
@ -13,7 +14,6 @@ typedef struct
int _eof;
} FILE;
#define SEEK_SET 0
#define EOF -1
extern FILE* stderr;
@ -35,10 +35,23 @@ extern "C"
/* Read arbitrarily sized items from a stream. */
size_t fread(void* buf, size_t size, size_t nmemb, FILE* stream);
size_t fwrite(const void*, size_t, size_t, FILE*);
/* Write arbitrarily sized items to a stream. */
size_t fwrite(const void* buf, size_t size, size_t nmemb, FILE* stream);
int fseek(FILE*, long, int);
long ftell(FILE*);
/* Move the file position. Clears the end-of-file indicator on success. */
int fseek(FILE* stream, long offset, int whence);
/* Return the current file position. */
long ftell(FILE* stream);
/* Rewind the file position and clear the error and end-of-file indicators. */
void rewind(FILE* stream);
/* Save the current file position. */
int fgetpos(FILE* stream, fpos_t* pos);
/* Restore a file position. */
int fsetpos(FILE* stream, const fpos_t* pos);
/* Return whether the error indicator was set in stream. */
int ferror(FILE* stream);

View File

@ -13,5 +13,8 @@ typedef __i64_t ssize_t;
typedef __i64_t time_t;
typedef __u16_t mode_t;
typedef __u64_t useconds_t;
typedef __i64_t off_t;
typedef off_t fpos_t;
#endif

View File

@ -6,6 +6,7 @@
#define __need_NULL
#include <stddef.h>
#include <bits/seek.h>
#include <stdint.h>
#include <sys/types.h>
@ -38,6 +39,12 @@ extern "C"
/* Read bytes from a file descriptor. */
ssize_t read(int fd, void* buf, size_t size);
/* Write bytes to a file descriptor. */
ssize_t write(int fd, const void* buf, size_t size);
/* Modify a file descriptor's offset. */
off_t lseek(int fd, off_t offset, int whence);
#ifdef __cplusplus
}
#endif

View File

@ -66,6 +66,53 @@ extern "C"
return (size_t)nread / size;
}
size_t fwrite(const void* buf, size_t size, size_t nmemb, FILE* stream)
{
if (size * nmemb == 0) return 0;
ssize_t nwrite = write(stream->_fd, buf, size * nmemb);
return (size_t)nwrite / size;
}
int fseek(FILE* stream, long offset, int whence)
{
long result = lseek(stream->_fd, offset, whence);
if (result < 0) return -1;
// man fseek(3): A successful call to the fseek() function clears the end-of-file indicator for the stream.
stream->_eof = 0;
return 0;
}
long ftell(FILE* stream)
{
return lseek(stream->_fd, 0, SEEK_CUR);
}
void rewind(FILE* stream)
{
lseek(stream->_fd, 0, SEEK_SET);
clearerr(stream);
}
int fgetpos(FILE* stream, fpos_t* pos)
{
long offset = ftell(stream);
if (offset < 0) return -1;
*pos = offset;
return 0;
}
int fsetpos(FILE* stream, const fpos_t* pos)
{
return fseek(stream, *pos, SEEK_SET);
}
int ferror(FILE* stream)
{
return stream->_err;

View File

@ -54,4 +54,16 @@ extern "C"
long rc = syscall(SYS_read, fd, buf, size);
__errno_return(rc, ssize_t);
}
ssize_t write(int fd, const void* buf, size_t size)
{
long rc = syscall(SYS_write, fd, buf, size);
__errno_return(rc, ssize_t);
}
off_t lseek(int fd, off_t offset, int whence)
{
long rc = syscall(SYS_lseek, fd, offset, whence);
__errno_return(rc, off_t);
}
}

View File

@ -2,7 +2,7 @@
#define enumerate_syscalls(_e) \
_e(exit) _e(console_write) _e(clock_gettime) _e(allocate_memory) _e(deallocate_memory) _e(usleep) _e(open) \
_e(close) _e(read) _e(getpid)
_e(close) _e(read) _e(getpid) _e(write) _e(lseek)
enum Syscalls
{