summaryrefslogtreecommitdiff
path: root/kernel
diff options
context:
space:
mode:
authorMiguel <m.i@gmx.at>2018-09-21 01:43:25 +0200
committerMiguel <m.i@gmx.at>2018-09-21 01:43:25 +0200
commitace0646608c393d8952b14536090c302bed2ee85 (patch)
tree5d96e0d0a66c27818b677af3a84ef52af0260be1 /kernel
parentaeefdb37d1fc1c0eb7953b9c196cab09460bc167 (diff)
piperei working finally :)
Diffstat (limited to 'kernel')
-rw-r--r--kernel/syscalls.c85
1 files changed, 61 insertions, 24 deletions
diff --git a/kernel/syscalls.c b/kernel/syscalls.c
index dfcdaae..ee100e2 100644
--- a/kernel/syscalls.c
+++ b/kernel/syscalls.c
@@ -28,22 +28,28 @@
//TODO move to process.c and implement per process //
static fd fds[MAX_PID][MAX_FD];
-static uint32_t next_fd[MAX_PID];
+static bool open_fd[MAX_PID][MAX_FD];
void fd_init_std_streams(uint32_t pid)
{
if(pid==0)
{
//stdin / stdout /stderr
- fds[0][next_fd[0]++]=fd_from_ringbuffer();
- fds[0][next_fd[0]++]=fd_from_fb_term();
- fds[0][next_fd[0]++]=fd_from_fb_term();
+ fds[0][0]=fd_from_ringbuffer();
+ fds[0][1]=fd_from_fb_term();
+ fds[0][2]=fd_from_fb_term();
+ open_fd[0][0]=true;
+ open_fd[0][1]=true;
+ open_fd[0][2]=true;
}
else
{
fds[pid][0]=fds[0][0];
fds[pid][1]=fds[0][1];
fds[pid][2]=fds[0][2];
+ open_fd[pid][0]=true;
+ open_fd[pid][1]=true;
+ open_fd[pid][2]=true;
}
}
@@ -248,22 +254,38 @@ int syscall_execve(const char *name, char *const argv[], char *const env[], int
return 0;
}
+/** helper */
+int nextfd(int pid)
+{
+ for(int i=0;i<MAX_FD;i++)
+ if(!open_fd[pid][i])
+ {
+ open_fd[pid][i]=true;
+ return i;
+ }
+
+ return -1;
+}
int syscall_open(char *name, int flags, int mode,uint32_t pid)
{
- fds[pid][next_fd[pid]]=mount_file_open(name);
- if(*(uint32_t *)fds[pid][next_fd[pid]].data==0)return -1;
-
- next_fd[pid]++;
- return next_fd[pid]-1;
+ uint32_t fdn=nextfd(pid);
+ fds[pid][fdn]=mount_file_open(name);
+ if(*(uint32_t *)fds[pid][fdn].data==0)return -1;
+ return fdn;
}
// pid_t fork(void);
uint32_t syscall_fork(int none1, int none2, int none3, int pid)
{
uint32_t newpid=task_fork(pid);
- fds[newpid][0]=fds[pid][0];
- fds[newpid][1]=fds[pid][1];
- fds[newpid][2]=fds[pid][2];
+
+ for(int i=0;i<MAX_FD;i++)
+ {
+ if(!open_fd[pid][i])continue;
+ fds[newpid][i]=fd_dupl(&fds[pid][i]);
+ open_fd[newpid][i]=true;
+ }
+
return newpid;
}
@@ -282,6 +304,15 @@ uint32_t syscall_exit(int status, uint32_t none1, uint32_t none2,int pid)
{
fixme("free allll mem");
klog("pid %d exited with %d",pid,status);
+ for(int i=0;i<MAX_FD;i++)
+ {
+ if(open_fd[pid][i])
+ {
+ fd_close(&fds[pid][i]);
+ open_fd[pid][i]=false;
+ }
+
+ }
task_exit(pid);
return 0;
}
@@ -289,15 +320,20 @@ uint32_t syscall_exit(int status, uint32_t none1, uint32_t none2,int pid)
// int close (int fd) (TODO; close or not to close open filedescirptors?)
int syscall_close(int file,int none1,int none2,int pid)
{
- if(file<3)return 0;
- fd_close(&fds[pid][file]);
+ if(open_fd[pid][file])
+ {
+ fd_close(&fds[pid][file]);
+ open_fd[pid][file]=false;
+ }
+
return 0;
}
// TODO: check if file is a termminal!
int syscall_isatty(int file,int none1,int none2,int pid)
{
- return 1;
+ if(fds[pid][file].type==FD_TYPE_FIFO)return 1;
+ return 0;
}
// TODO: per process basis!
@@ -324,22 +360,23 @@ uint32_t syscall_pipe(uint32_t *addr,int none1, int none2, uint32_t pid)
fd pipfds[2];
int ret=fds_from_pipe(pipfds);
- fds[pid][next_fd[pid]]=pipfds[0] ;
- *addr=next_fd[pid];
- addr++;
+ int fd1=nextfd(pid);
+ int fd2=nextfd(pid);
- next_fd[pid]++;
+ fds[pid][fd1]=pipfds[0] ;
+ *addr=fd1;
+ addr++;
- fds[pid][next_fd[pid]]=pipfds[1] ;
- *addr=next_fd[pid];
- next_fd[pid]++;
+ fds[pid][fd2]=pipfds[1] ;
+ *addr=fd2;
return ret;
}
uint32_t syscall_dup2(uint32_t oldfd,int newfd, int none2, uint32_t pid)
{
- fds[pid][newfd]=fds[pid][oldfd];
+ fd_close(&fds[pid][newfd]);
+ fds[pid][newfd]=fd_dupl(&fds[pid][oldfd]);
return newfd;
}
@@ -350,7 +387,7 @@ uint32_t syscall_generic_test(uint32_t nr,uint32_t p1, uint32_t p2, uint32_t p3,
case SYSCALL_WAIT :
return !task_runs(p1);
case SYSCALL_READ :
- return fd_has(&fds[pid][p1]);
+ return fd_has(&fds[pid][p1])||fd_eof(&fds[pid][p1]);
}
return 1;//other syscalls never block for now.