syscalls: make more system calls use ustring functions

This commit is contained in:
Matheus
2022-12-23 21:37:14 -03:00
parent 1ead72d897
commit f882b1ad10
7 changed files with 55 additions and 33 deletions
+10 -1
View File
@@ -7,6 +7,7 @@
#include <arch/interrupt.h> #include <arch/interrupt.h>
#include <arch/timekeeper.h> #include <arch/timekeeper.h>
#include <time.h> #include <time.h>
#include <kernel/ustring.h>
#define MAXNFDS 4096 #define MAXNFDS 4096
@@ -35,6 +36,13 @@ syscallret syscall_poll(pollfd *fds, size_t nfds, int timeoutms){
return retv; return retv;
} }
retv.errno = u_memcpy(ilist, fds, nfds*sizeof(pollfd));
if(retv.errno){
free(ilist);
return retv;
}
for(size_t fd = 0; fd < nfds; ++fd){ for(size_t fd = 0; fd < nfds; ++fd){
ilist[fd].fd = fds[fd].fd; ilist[fd].fd = fds[fd].fd;
ilist[fd].events = fds[fd].events; ilist[fd].events = fds[fd].events;
@@ -115,8 +123,9 @@ syscallret syscall_poll(pollfd *fds, size_t nfds, int timeoutms){
if(ilist[i].fd < 0) if(ilist[i].fd < 0)
continue; continue;
if(u_memcpy(&fds[i].revents, &ilist[i].revents, sizeof(ilist[i].revents)))
continue;
fds[i].revents = ilist[i].revents;
if(ilist[i].revents) if(ilist[i].revents)
++eventcount; ++eventcount;
+5 -7
View File
@@ -45,15 +45,13 @@ syscallret syscall_read(int ifd, void* buff, size_t count){
return retv; return retv;
} }
fd->offset += readc; retv.errno = u_memcpy(buff, kbuff, readc);
retv.ret = retv.errno ? (uint64_t)-1 : readc;
if(retv.errno == 0)
fd->offset += readc;
fd_release(fd); fd_release(fd);
memcpy(buff, kbuff, count); // XXX user version for safety
retv.errno = 0;
retv.ret = readc;
free(kbuff); free(kbuff);
return retv; return retv;
+4 -1
View File
@@ -84,7 +84,10 @@ syscallret syscall_recvmsg(int ifd, msghdr *msg, int flags){
size_t iovlen = msg->msg_iov[i].iov_len; size_t iovlen = msg->msg_iov[i].iov_len;
void* iovbuf = msg->msg_iov[i].iov_base; void* iovbuf = msg->msg_iov[i].iov_base;
memcpy(iovbuf, buffptr, iovlen); retv.errno = u_memcpy(iovbuf, buffptr, iovlen);
if(retv.errno)
goto _return;
buffptr += iovlen; buffptr += iovlen;
} }
+17 -15
View File
@@ -7,6 +7,7 @@
#include <arch/cls.h> #include <arch/cls.h>
#include <kernel/alloc.h> #include <kernel/alloc.h>
#include <string.h> #include <string.h>
#include <kernel/ustring.h>
syscallret syscall_stat(const char* path, stat* st){ syscallret syscall_stat(const char* path, stat* st){
syscallret retv; syscallret retv;
@@ -17,36 +18,37 @@ syscallret syscall_stat(const char* path, stat* st){
return retv; return retv;
} }
char* kpath = alloc(strlen(path)+1); // XXX use proper user strlen func size_t len;
if(!kpath){ retv.errno = u_strlen(path, &len);
retv.errno = ENOMEM;
if(retv.errno)
return retv; return retv;
}
strcpy(kpath, path); // XXX use user strcpy func char kpath[len+1];
retv.errno = u_strcpy(kpath, path);
if(retv.errno)
return retv;
vnode_t* file; vnode_t* file;
proc_t* proc = arch_getcls()->thread->proc; proc_t* proc = arch_getcls()->thread->proc;
int ret = vfs_open(&file, *kpath == '/' ? proc->root : proc->cwd, kpath); retv.errno = vfs_open(&file, *kpath == '/' ? proc->root : proc->cwd, kpath);
if(ret){ if(retv.errno)
retv.errno = ret; return retv;
goto _ret;
}
stat stbuff = file->st; stat stbuff = file->st;
vfs_close(file); vfs_close(file);
memcpy(st, &stbuff, sizeof(stat)); // XXX use user memcpy retv.errno = u_memcpy(st, &stbuff, sizeof(stat));
retv.ret = retv.errno ? -1 : 0;
retv.ret = 0;
retv.errno = 0;
_ret:
free(kpath);
return retv; return retv;
} }
+8 -2
View File
@@ -9,6 +9,7 @@
#include <arch/spinlock.h> #include <arch/spinlock.h>
#include <kernel/alloc.h> #include <kernel/alloc.h>
#include <arch/interrupt.h> #include <arch/interrupt.h>
#include <kernel/ustring.h>
#define UNSUPPORTED "astral: waitpid: Unsupported waitpid options!\n" #define UNSUPPORTED "astral: waitpid: Unsupported waitpid options!\n"
#define UNSUPPORTED_PG "astral: waitpid: Process group waits are not supported yet.\n" #define UNSUPPORTED_PG "astral: waitpid: Process group waits are not supported yet.\n"
@@ -94,9 +95,14 @@ syscallret syscall_waitpid(pid_t pid, int *status, int options){
proc->child = child->sibling; proc->child = child->sibling;
} }
if(status)
*status = child->status; // XXX safer way of doing this
if(status){
retv.errno = u_memcpy(status, &child->status, sizeof(*status));
if(retv.errno)
return retv;
}
int threadc = proc->threadcount; int threadc = proc->threadcount;
// free all the threads // free all the threads
+5 -1
View File
@@ -39,7 +39,11 @@ syscallret syscall_write(int ifd, void* buff, size_t count){
return retv; return retv;
} }
memcpy(kbuff, buff, count); // XXX use a more proper user copy retv.errno = u_memcpy(kbuff, buff, count);
if(retv.errno)
goto _ret;
if(fd->flags & O_APPEND) if(fd->flags & O_APPEND)
fd->offset = fd->node->st.st_size; fd->offset = fd->node->st.st_size;
+2 -2
View File
@@ -33,9 +33,9 @@ int u_memcpy(void* dest, const void* src, size_t len){
} }
extern void arch_u_strcpy_cont(); extern void arch_u_strcpy_cont();
extern int arch_u_strcpy(void* dest, const void* src); extern int arch_u_strcpy(char* dest, const char* src);
int u_strcpy(void* dest, const void* src){ int u_strcpy(char* dest, const char* src){
arch_getcls()->thread->umemopfailaddr = arch_u_strcpy_cont; arch_getcls()->thread->umemopfailaddr = arch_u_strcpy_cont;