From e3a8099343aac9d94f411638ad84632d4b620132 Mon Sep 17 00:00:00 2001 From: Miguel Date: Mon, 15 Oct 2018 16:29:50 +0200 Subject: cleanup sys/ etc --- kernel/syscalls.c | 139 +++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 117 insertions(+), 22 deletions(-) (limited to 'kernel/syscalls.c') diff --git a/kernel/syscalls.c b/kernel/syscalls.c index 1c1d2c7..e73f89c 100644 --- a/kernel/syscalls.c +++ b/kernel/syscalls.c @@ -31,6 +31,8 @@ //TODO move to process.c and implement per process // static fd fds[MAX_PID][MAX_FD]; +//static uint32_t opendir_pos[MAX_PID][MAX_FD]; +//static char opendir_name[MAX_PID][MAX_FD][256]; static int tty[MAX_PID]; // keep track of /dev/tty fd for each process :P static bool open_fd[MAX_PID][MAX_FD]; @@ -55,6 +57,19 @@ void fd_init_std_streams(uint32_t pid) } // +/** helper */ +int nextfd(int pid) +{ + for(int i=0;i0) + { + if(fd_eof(&fds[pid][file]))return ret; + *buf++=fd_read(&fds[pid][file]); + ret++; + } + return ret; } + /** + * Monitor multiple descriptors + * ============================ + * + * nfds is the MAX file descriptor number +1. + * + * It is possible to provide 3 different sets of filedescriptors + * (they can be NULL as well) + * + * - readfds - reading + * - writefds - writing + * - exceptfds - exceptions (all cleared unless exception occurs) + * + * The call returns the total number of descriptors contained in all + * sets after returning. + * + * This call will block until time (timeout) runs out + * OR a file descriptor becomes ready. + */ + int syscall_select(int maxxfd,struct timeval *tv, fd_set **fd_sets, uint32_t pid, bool test) { int ret=0; @@ -251,15 +313,47 @@ int syscall_select(int maxxfd,struct timeval *tv, fd_set **fd_sets, uint32_t pid } -//TODO: replace with dirent! -int syscall_readdir(const char *name,fs_dirent *dirs,int *pos,uint32_t pid) +//TODO: use fd and inode (instead of path)... now this is a crazy trick how it works.. +//is it that bad??? yes it was bad and vim crashed so we changed to even more hacky approach now.. :( :( +//ok it was other reason but we should not use it ... +//since userspace might zero or reuse dirent* and corrupt our DIR* ! TODO TODO TODO danger! +struct dirent *syscall_opendir(const char *name,struct dirent *dirs,int none,uint32_t pid) { - int ret=mount_read_dir(name, dirs, pos); + strcpy(dirs->dirname,name); + if(dirs->dirname[strlen(dirs->dirname)-1]=='/')dirs->dirname[strlen(dirs->dirname)-1]=0; + klog("syscall to opendir(%s)",dirs->dirname); + + // test if exists.... + struct dirent testdirent; + uint32_t testpos; + int ret=mount_read_dir(dirs->dirname, &testdirent, &testpos); if(ret==-1) { + // no it does not! set_errno(ENOENT); + return NULL; } - return ret; + + dirs->pos=0; + + /* + uint32_t fdn=nextfd(pid); + strcpy(opendir_name[pid][fdn],dirs->dirname); + opendir_pos[pid][fdn]=0; + */ + + return dirs; +} + +struct dirent *syscall_readdir(struct dirent *dirs,int none1, int none2,uint32_t pid) +{ + // this dirent will get filled with next entry... + int ret=mount_read_dir(dirs->dirname, dirs, &dirs->pos); + + // no more entries + if(ret==0)return NULL; + + return dirs; } /** execve helper */ @@ -327,22 +421,10 @@ int syscall_execve(const char *name, char *const argv[], char *const env[], int return 0; } -/** helper */ -int nextfd(int pid) -{ - for(int i=0;imax_addy)kpanic("can not set sbrk . TODO: dynamic allocation!"); fixme("fake syscall_sbrk(0x%08X) return %08X",incr,oldalloc); return oldalloc; } @@ -568,7 +654,9 @@ uint32_t syscall_generic_test(uint32_t nr,uint32_t p1, uint32_t p2, uint32_t p3, uint32_t syscall_generic(uint32_t nr,uint32_t p1, uint32_t p2, uint32_t p3, uint32_t pid) { -// klog("processing syscall [%s] for pid:%d",syscall_get_name(nr),pid); +#ifdef LOG_SYSCALLS + klog("processing syscall [%s] for pid:%d",syscall_get_name(nr),pid); +#endif switch(nr){ case SYSCALL_EXIT : @@ -617,6 +705,8 @@ uint32_t syscall_generic(uint32_t nr,uint32_t p1, uint32_t p2, uint32_t p3, uint return syscall_gettimeofday(p1,p2,p3,pid); case SYSCALL_READDIR : return syscall_readdir(p1,p2,p3,pid); + case SYSCALL_OPENDIR : + return syscall_opendir(p1,p2,p3,pid); case SYSCALL_KILL : // return task_kill(p1,p2,p3); return -1; @@ -634,6 +724,11 @@ uint32_t syscall_generic(uint32_t nr,uint32_t p1, uint32_t p2, uint32_t p3, uint return syscall_tcgetattr(p1,p2,p3,pid); case SYSCALL_TCSETATTR: return syscall_tcsetattr(p1,p2,p3,pid); + case SYSCALL_UNIMPLEMENTED: + if(!strcmp(p1,"fcntl")){ + klog("todo: quickfix hack for fcntl"); + } + } klog("unknown syscall %s / %d we just return 0",p1,nr); return 0; -- cgit v1.2.3