Skip to content

Commit

Permalink
posix: add unnamed pipes
Browse files Browse the repository at this point in the history
RTOS-299
  • Loading branch information
HBuczynski committed Dec 15, 2022
1 parent c47aaf1 commit 54c5764
Show file tree
Hide file tree
Showing 6 changed files with 257 additions and 9 deletions.
2 changes: 1 addition & 1 deletion posix/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
# Author: Pawel Pisarczyk
#

OBJS += $(addprefix $(PREFIX_O)posix/, posix.o inet.o unix.o fdpass.o)
OBJS += $(addprefix $(PREFIX_O)posix/, posix.o inet.o unix.o fdpass.o pipe.o)

207 changes: 207 additions & 0 deletions posix/pipe.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
/*
* Phoenix-RTOS
*
* Operating system kernel
*
* Unnamed pipes
*
* Copyright 2022 Phoenix Systems
* Author: Hubert Buczynski
*
* This file is part of Phoenix-RTOS.
*
* %LICENSE%
*/

#include "pipe.h"

#include "../usrv.h"
#include "../lib/cbuffer.h"
#include "../include/posix.h"


#define SIZE_PIPE_BUFF (4 * SIZE_PAGE)

typedef struct {
rbnode_t linkage;
oid_t oid;

lock_t lock;
void *data;
cbuffer_t cbuff;
} pipe_t;


static struct {
rbtree_t pipes;
unsigned int cnt;
lock_t lock;
} pipe_common;


static int pipe_cmp(rbnode_t *n1, rbnode_t *n2)
{
pipe_t *p1 = lib_treeof(pipe_t, linkage, n1);
pipe_t *p2 = lib_treeof(pipe_t, linkage, n2);
int res;

if (p1->oid.id < p2->oid.id) {
res = -1;
}
else if (p1->oid.id > p2->oid.id) {
res = 1;
}
else {
res = 0;
}

return res;
}


static pipe_t *pipe_getPipe(const oid_t *oid)
{
pipe_t p;
p.oid = *oid;

return lib_treeof(pipe_t, linkage, lib_rbFind(&pipe_common.pipes, &p.linkage));
}


static int pipe_destroy(oid_t oid)
{
pipe_t *pipe = pipe_getPipe(&oid);
if (pipe == NULL) {
return -EINVAL;
}

proc_lockSet(&pipe_common.lock);
lib_rbRemove(&pipe_common.pipes, &pipe->linkage);
proc_lockClear(&pipe_common.lock);

proc_lockSet(&pipe->lock);
_cbuffer_free(&pipe->cbuff);
vm_kfree(pipe->data);
proc_lockClear(&pipe->lock);

proc_lockDone(&pipe->lock);
vm_kfree(pipe);

return EOK;
}


static int pipe_create(oid_t *oid)
{
int res;
pipe_t *p;

p = vm_kmalloc(sizeof(pipe_t));
if (p == NULL) {
return -ENOMEM;
}

p->data = vm_kmalloc(SIZE_PIPE_BUFF);
if (p->data == NULL) {
vm_kfree(p);
return -ENOMEM;
}

res = proc_lockInit(&p->lock);
if (res < 0) {
vm_kfree(p->data);
vm_kfree(p);
return res;
}

res = _cbuffer_init(&p->cbuff, p->data, SIZE_PIPE_BUFF);
if (res < 0) {
proc_lockClear(&p->lock);
vm_kfree(p->data);
vm_kfree(p);
return res;
}

p->oid.port = USRV_PORT;
p->oid.id = (id_t)(++pipe_common.cnt << USRV_ID_BITS) | USRV_ID_PIPES;

proc_lockSet(&pipe_common.lock);
lib_rbInsert(&pipe_common.pipes, &p->linkage);
proc_lockClear(&pipe_common.lock);

return EOK;
}


static int pipe_read(const oid_t *oid, void *buf, size_t sz, unsigned mode)
{
pipe_t *pipe = pipe_getPipe(oid);

if (pipe == NULL) {
return -EINVAL;
}


return 0;
}


static int pipe_write(const oid_t *oid, const void *buf, size_t sz, unsigned mode)
{
pipe_t *pipe = pipe_getPipe(oid);

if (pipe == NULL) {
return -EINVAL;
}


if (mode & O_NONBLOCK) {

}

return 0;
}


void pipe_msgHandler(msg_t *msg, oid_t oid, unsigned long int rid)
{
switch (msg->type) {
case mtOpen:
/* TODO: increase refs in pipes */
break;

case mtCreate:
msg->o.create.err = pipe_create(&msg->o.create.oid);
break;

case mtRead:
msg->o.io.err = pipe_read(&msg->i.io.oid, msg->o.data, msg->o.size, msg->i.io.mode);
break;

case mtWrite:
msg->o.io.err = pipe_write(&msg->i.io.oid, msg->i.data, msg->i.size, msg->i.io.mode);
break;

case mtClose:
/* TODO: manage refs ine pipes and destroy pipe */
break;

case mtDevCtl:
/* TODO: handle fcntl */
break;

default:
break;
}

proc_respond(oid.port, msg, rid);
}


void pipe_init(void)
{
pipe_common.cnt = 0;

proc_lockInit(&pipe_common.lock);
lib_rbInit(&pipe_common.pipes, pipe_cmp, NULL);
}
28 changes: 28 additions & 0 deletions posix/pipe.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Phoenix-RTOS
*
* Operating system kernel
*
* Unnamed pipes
*
* Copyright 2022 Phoenix Systems
* Author: Hubert Buczynski
*
* This file is part of Phoenix-RTOS.
*
* %LICENSE%
*/

#ifndef _POSIX_PIPE_H_
#define _POSIX_PIPE_H_

#include "../proc/msg.h"


extern void pipe_msgHandler(msg_t *msg, oid_t oid, unsigned long int rid);


extern void pipe_init(void);


#endif
18 changes: 14 additions & 4 deletions posix/posix.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@
#include "../proc/proc.h"

#include "posix.h"
#include "pipe.h"
#include "posix_private.h"
#include "../lib/cbuffer.h"
#include "../usrv.h"

#ifdef CPU_STM32
#define MAX_FD_COUNT 8
Expand Down Expand Up @@ -795,12 +797,19 @@ int posix_pipe(int fildes[2])

hal_memset(&oid, 0, sizeof(oid));

if ((res = proc_lookup("/dev/posix/pipes", NULL, &pipesrv)) < 0) {
pinfo_put(p);
return res == -EINTR ? res : -ENOSYS;
res = proc_lookup("/dev/posix/pipes", NULL, &pipesrv);
if (res < 0) {
/* Use anonymous pipe from kernel */
pipesrv.port = USRV_PORT;
pipesrv.id = USRV_ID_PIPES;

}
else {
/* Use anonymous pipes from external server */
}

if ((res = proc_create(pipesrv.port, pxBufferedPipe, O_RDONLY | O_WRONLY, oid, pipesrv, NULL, &oid)) < 0) {
res = proc_create(pipesrv.port, pxBufferedPipe, O_RDONLY | O_WRONLY, oid, pipesrv, NULL, &oid);
if (res < 0) {
pinfo_put(p);
return res;
}
Expand Down Expand Up @@ -833,6 +842,7 @@ int posix_pipe(int fildes[2])
vm_kfree(fi);

pinfo_put(p);
/* FIXME: destroy pipe */
return -EMFILE;
}

Expand Down
5 changes: 1 addition & 4 deletions usrv.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@
#include "proc/ports.h"


#define USRV_ID_LOG 0


static struct {
oid_t oid;
} usrv_common;
Expand Down Expand Up @@ -92,7 +89,7 @@ static void usrv_msgthr(void *arg)
continue;
}

switch (oid.id) {
switch (oid.id & 0xf) {
case USRV_ID_LOG:
log_msgHandler(&msg, oid, rid);
break;
Expand Down
6 changes: 6 additions & 0 deletions usrv.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@
#ifndef _USRV_H_
#define _USRV_H_

#define USRV_PORT 0
#define USRV_ID_LOG 0
#define USRV_ID_PIPES 1

/* LSB number for unit identifier in oid.id */
#define USRV_ID_BITS 4

extern void _usrv_init(void);

Expand Down

0 comments on commit 54c5764

Please sign in to comment.