summaryrefslogtreecommitdiff
path: root/fs
diff options
context:
space:
mode:
Diffstat (limited to 'fs')
-rw-r--r--fs/fd.c124
-rw-r--r--fs/fd.h4
2 files changed, 100 insertions, 28 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;
}
diff --git a/fs/fd.h b/fs/fd.h
index 64a53ed..21037c8 100644
--- a/fs/fd.h
+++ b/fs/fd.h
@@ -10,7 +10,7 @@
#include "ringbuffer.h"
enum FD_TYPE{
- FD_TYPE_FIFO_BUFFERED=1,
+ FD_TYPE_FIFO=1,
FD_TYPE_EXT2_FILE=2,
FD_TYPE_EXT2_DIR=3,
FD_TYPE_SYSFS=4,
@@ -24,6 +24,7 @@ typedef struct fd_struct
bool (*has)(struct fd_struct*);
bool (*eof)(struct fd_struct*);
bool (*close)(struct fd_struct*);
+ struct fd_struct (*dupl)(struct fd_struct *);
uint8_t type;
void *data; // opaque data
@@ -34,6 +35,7 @@ bool fd_has(fd*);
bool fd_eof(fd*);
bool fd_write(fd*,uint8_t);
bool fd_close(fd*);
+fd fd_dupl(fd*);
fd fd_from_fifo(fifo* f);
fd fd_from_path(char *path);