Voxia OS v0.0.1
Hobby Project Operating System Targeting x86-64
Loading...
Searching...
No Matches
writev.c
Go to the documentation of this file.
1#include "hal/cpu/core.h"
2#include "libk/serial.h"
3#include "str.h"
4#include "vfs/vnode.h"
5#include <sys/err_no.h>
6#include <sys/fd.h>
7#include <sys/syscall.h>
8
9long syscall_writev(int fd, const struct iovec* iov, int iovcnt) {
10 // TODO: handle is fd is not found
11
12 auto curr_procc = get_current_core_data()->active_thread->process;
13 auto fdt = (struct fdtable*)curr_procc->fdtable;
14 auto curr_fd = fdt->fds[fd];
15
16 if (fd < 0 || fd > (int)fdt->max_fds) {
17 LOG2_ERROR("writev", "fd %d is invalid, max fd %d", fd,
18 fdt->max_fds);
19 return -EBADF;
20 }
21
22 auto ops = (vops_file_t*)curr_fd->ops;
23 if (!ops) {
24 LOG2_ERROR("writev", "fd %d ops is missing", fd);
25 return -EBADF;
26 }
27
28 if (!curr_fd->vnode) {
29 LOG2_ERROR("writev", "fd %d vnode is missing", fd);
30 return -EBADF;
31 }
32
33 if (!iov) {
34 LOG2_ERROR("writev", "empty iovec");
35 return -EINVAL;
36 }
37
38 if (!ops->write) {
39 LOG2_ERROR("writev", "fd %d `write` ops is missing", fd);
40 return -ENOTTY;
41 }
42
43 auto iovec_ =
44 (struct iovec*)kalloc(sizeof(struct iovec) * (size_t)iovcnt);
45 memcopy(iovec_, (void*)iov, sizeof(struct iovec) * (size_t)iovcnt);
46
47 long total_read = 0;
48 for (int i = 0; i < iovcnt; i++) {
49 auto iov_ = &iovec_[i];
50 if (!iov->iov_len || !iov->iov_base)
51 continue;
52
53 serial2_printf("write: from 0x%x\n", iov_->iov_base);
54 auto write_count =
55 ops->write(curr_fd->vnode, iov_->iov_base,
56 (size_t)iov_->iov_len, (size_t)total_read);
57
58 if (write_count < 0)
59 return -1;
60
61 total_read += write_count;
62 }
63 return total_read;
64}
each_core_data * get_current_core_data(void)
Definition core.c:54
void * ops
Definition dev.h:2
#define ENOTTY
Definition err_no.h:26
#define EBADF
Definition err_no.h:12
struct fdtable * fdt
Definition fd.h:1
void serial2_printf(const char *fmt,...)
#define EINVAL
void * kalloc(size_t size)
#define LOG2_ERROR(mod, fmt,...)
Definition serial.h:38
void memcopy(void *dest, void *src, size_t size)
Definition fd.h:20
void * iov_base
Definition syscall.h:44
int iov_len
Definition syscall.h:45
long syscall_writev(int fd, const struct iovec *iov, int iovcnt)
Definition writev.c:9