77 lines
1.5 KiB
C
77 lines
1.5 KiB
C
#include <stddef.h>
|
|
#include <stdint.h>
|
|
#include "ipcpipe.h"
|
|
#include "ipc/pipe/pipe.h"
|
|
#include "sysdefs/ipcpipe.h"
|
|
#include "dlmalloc/malloc.h"
|
|
#include "proc/proc.h"
|
|
#include "spinlock/spinlock.h"
|
|
#include "errors.h"
|
|
#include "util/util.h"
|
|
|
|
int32_t SYSCALL5(sys_ipcpipe, pid1, pipenum1, cmd1, buffer1, len1) {
|
|
uint64_t pid = pid1;
|
|
uint64_t pipenum = pipenum1;
|
|
uint64_t cmd = cmd1;
|
|
int32_t ret = E_OK;
|
|
|
|
if (pid == -1) {
|
|
pid = PROCS.current->pid;
|
|
}
|
|
|
|
spinlock_acquire(&PROCS.spinlock);
|
|
Proc *proc = NULL;
|
|
LL_FINDPROP(PROCS.procs, proc, pid, pid);
|
|
spinlock_release(&PROCS.spinlock);
|
|
|
|
if (proc == NULL) {
|
|
ret = E_INVALIDARGUMENT;
|
|
goto done;
|
|
}
|
|
|
|
switch (cmd) {
|
|
case IPCPIPE_MAKE: {
|
|
if (pipenum >= PROC_PIPEHANDLES_MAX) {
|
|
ret = E_NOMEMORY;
|
|
goto done;
|
|
}
|
|
|
|
IpcPipe *pipe = dlmalloc(sizeof(*pipe));
|
|
if (pipe == NULL) {
|
|
ret = E_NOMEMORY;
|
|
goto done;
|
|
}
|
|
|
|
if ((ret = ipc_pipeinit(pipe)) < 0) {
|
|
goto done;
|
|
}
|
|
|
|
proc->pipes[pipenum] = pipe;
|
|
|
|
ret = E_OK;
|
|
} break;
|
|
case IPCPIPE_WRITE: {
|
|
if (pipenum >= PROC_PIPEHANDLES_MAX) {
|
|
ret = E_INVALIDARGUMENT;
|
|
goto done;
|
|
}
|
|
|
|
const uint8_t *const buffer = (const uint8_t *const)buffer1;
|
|
if (buffer == NULL) {
|
|
ret = E_INVALIDARGUMENT;
|
|
goto done;
|
|
}
|
|
|
|
IpcPipe *pipe = proc->pipes[pipenum];
|
|
|
|
ret = ipc_pipewrite(pipe, buffer, len1);
|
|
} break;
|
|
default: {
|
|
ret = E_INVALIDARGUMENT;
|
|
} break;
|
|
}
|
|
|
|
done:
|
|
return ret;
|
|
}
|