summaryrefslogtreecommitdiff
path: root/fs/fd.c
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 /fs/fd.c
parentaeefdb37d1fc1c0eb7953b9c196cab09460bc167 (diff)
piperei working finally :)
Diffstat (limited to 'fs/fd.c')
-rw-r--r--fs/fd.c124
1 files changed, 97 insertions, 27 deletions
diff --git a/fs/fd.c b/fs/fd.c
index f8981a5..1215679 100644
--- a/fs/fd.c
+++ b/fs/fd.c
@@ -33,6 +33,20 @@ bool fd_close(fd* f)
return f->close(f->data);
}
+fd fd_dupl(fd* f)
+{
+ return f->dupl(f);
+}
+
+static fd default_dupl(fd *f)
+{
+ return *f;
+}
+void default_close(void* x)
+{
+ // nothin
+}
+
//----
void* ext2_init(char *path)
{
@@ -109,11 +123,44 @@ void sysfs_close(uint32_t *data)
kbfree(data); // free memory holding ringbuffer information
}
-void pipe_close(uint32_t *data)
+void pipe_r_close(uint8_t *data)
{
- ringbuffer *r=data;
- ringbuffer_free(data); // free ringbuffer buffer
- kbfree(data); // free memory holding ringbuffer information
+ uint8_t *mem=data+sizeof(ringbuffer);
+ *mem-=1;
+ klog("closing read end of pipe, ref count=%d",*mem);
+ /*
+ if(*mem==0) // TODO : check other end too! and close on w_close
+ {
+ ringbuffer *r=data;
+ ringbuffer_free(data); // free ringbuffer buffer
+ kbfree(data); // free memory holding ringbuffer information
+ }
+ */
+}
+
+void pipe_w_close(uint8_t *data)
+{
+ uint8_t *mem=data+sizeof(ringbuffer);
+ mem+=4;
+ *mem-=1;
+ klog("closing write end of pipe, ref count=%d",*mem);
+}
+
+static fd pipe_r_dupl(fd *f)
+{
+ uint8_t *mem=f->data+sizeof(ringbuffer);
+ *mem+=1;
+ klog("duplicating read end of pipe, ref count=%d",*mem);
+ return *f;
+}
+
+static fd pipe_w_dupl(fd *f)
+{
+ uint8_t *mem=f->data+sizeof(ringbuffer);
+ mem+=4;
+ *mem+=1;
+ klog("duplicating write end of pipe, ref count=%d",*mem);
+ return *f;
}
void get_sysfs_data(ringbuffer *r,char *format_string, ...)
@@ -135,19 +182,6 @@ bool fifo_eof(uint32_t* data)
return false;
}
-// TODO remove and use fd_from_ringbuffer?
-fd fd_from_fifo(fifo* fif)
-{
- fd f;
- f.type=FD_TYPE_FIFO_BUFFERED;
- f.data=fif;
- f.read=fifo_get;
- f.write=fifo_put;
- f.eof=fifo_eof;
-// f.close=fd_close; //TODO.. etc also otehr and cleanups
- f.has=fifo_has;
- return f;
-}
uint8_t pipe_get(uint32_t *data)
{
@@ -164,11 +198,19 @@ bool pipe_has(uint32_t *data)
return ringbuffer_has(data);
}
-bool pipe_eof(uint32_t *data)
+bool pipe_eof(uint8_t *data)
{
- return 0;
+
+ uint8_t *mem=data+sizeof(ringbuffer);
+ mem+=4;
+
+ //we assume EOF if there is no data anymore AND no writers!
+ return (!ringbuffer_has(data))&&(*mem==0);
}
+
+/////
+
int fds_from_pipe(fd pipefds[2])
{
fd read;
@@ -177,24 +219,36 @@ int fds_from_pipe(fd pipefds[2])
read.type=FD_TYPE_PIPE;
wrt.type=FD_TYPE_PIPE;
- read.data=kballoc(1);
- wrt.data=read.data;
+ uint8_t *mem=kballoc(1);
+
+ read.data=mem;
+ wrt.data=mem;
ringbuffer r=ringbuffer_init(1);
- memcpy(read.data,&r,sizeof(ringbuffer));
+ memcpy(mem,&r,sizeof(ringbuffer));
+ mem+=sizeof(ringbuffer);
+ *mem=1;
+ mem+=4;
+ *mem=1;
read.read=pipe_get;
+ wrt.read=0;
+
wrt.write=pipe_put;
+ read.write=0;
read.has=pipe_has;
- wrt.has=pipe_has;
+ wrt.has=0;
read.eof=pipe_eof;
- wrt.eof=pipe_eof;
+ wrt.eof=0;
+
+ read.close=pipe_r_close;
+ wrt.close=pipe_w_close;
- read.close=0;
- wrt.close=0;
+ read.dupl=pipe_r_dupl;
+ wrt.dupl=pipe_w_dupl;
pipefds[0]=read;
pipefds[1]=wrt;
@@ -212,6 +266,7 @@ fd fd_from_path(char* path)
f.close=ext2_close;
f.eof=ext2_eof;
f.has=ext2_has;
+ f.dupl=default_dupl;
return f;
}
@@ -237,6 +292,21 @@ fd fd_from_sysfs(void(*g)(ringbuffer *r,void (*f)(ringbuffer *r,char *fmt, ...))
f.close=sysfs_close;
f.has=sysfs_has;
f.eof=sysfs_eof;
-
+ f.dupl=default_dupl;
+ f.close=default_close;
+ return f;
+}
+
+fd fd_from_fifo(fifo* fif)
+{
+ fd f;
+ f.type=FD_TYPE_FIFO;
+ f.data=fif;
+ f.read=fifo_get;
+ f.write=fifo_put;
+ f.eof=fifo_eof;
+ f.has=fifo_has;
+ f.dupl=default_dupl;
+ f.close=default_close;
return f;
}