169 lines
4.0 KiB
C
169 lines
4.0 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"
|
|
#include "kprintf.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, proc->pid)) < 0) {
|
|
ret = E_NOMEMORY;
|
|
goto done;
|
|
}
|
|
|
|
spinlock_acquire(&proc->pipes_spinlock);
|
|
if (proc->pipes[pipenum] != NULL) {
|
|
spinlock_release(&proc->pipes_spinlock);
|
|
ipc_pipefree(pipe);
|
|
dlfree(pipe);
|
|
ret = E_RESOURCEAVAIL;
|
|
goto done;
|
|
}
|
|
proc->pipes[pipenum] = pipe;
|
|
spinlock_release(&proc->pipes_spinlock);
|
|
|
|
ret = E_OK;
|
|
} break;
|
|
case IPCPIPE_DELETE: {
|
|
if (pipenum >= PROC_PIPEHANDLES_MAX) {
|
|
ret = E_NOMEMORY;
|
|
goto done;
|
|
}
|
|
|
|
spinlock_acquire(&proc->pipes_spinlock);
|
|
if (proc->pipes[pipenum] != NULL && proc->pid == proc->pipes[pipenum]->ownerpid) {
|
|
ipc_pipefree(proc->pipes[pipenum]);
|
|
dlfree(proc->pipes[pipenum]);
|
|
}
|
|
proc->pipes[pipenum] = NULL;
|
|
spinlock_release(&proc->pipes_spinlock);
|
|
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;
|
|
}
|
|
|
|
spinlock_acquire(&proc->pipes_spinlock);
|
|
IpcPipe *pipe = proc->pipes[pipenum];
|
|
spinlock_release(&proc->pipes_spinlock);
|
|
|
|
if (pipe == NULL) {
|
|
ret = E_INVALIDARGUMENT;
|
|
goto done;
|
|
}
|
|
|
|
ret = ipc_pipewrite(pipe, buffer, len1);
|
|
} break;
|
|
case IPCPIPE_READ: {
|
|
if (pipenum >= PROC_PIPEHANDLES_MAX) {
|
|
ret = E_INVALIDARGUMENT;
|
|
goto done;
|
|
}
|
|
|
|
uint8_t *const buffer = (uint8_t *const)buffer1;
|
|
if (buffer == NULL) {
|
|
ret = E_INVALIDARGUMENT;
|
|
goto done;
|
|
}
|
|
|
|
spinlock_acquire(&proc->pipes_spinlock);
|
|
IpcPipe *pipe = proc->pipes[pipenum];
|
|
spinlock_release(&proc->pipes_spinlock);
|
|
|
|
if (pipe == NULL) {
|
|
ret = E_INVALIDARGUMENT;
|
|
goto done;
|
|
}
|
|
|
|
ret = ipc_piperead(pipe, buffer, len1);
|
|
} break;
|
|
case IPCPIPE_REPLACE: {
|
|
if (pipenum >= PROC_PIPEHANDLES_MAX) {
|
|
ret = E_NOMEMORY;
|
|
goto done;
|
|
}
|
|
|
|
uint64_t pid2 = buffer1;
|
|
uint64_t pipenum2 = len1;
|
|
|
|
spinlock_acquire(&PROCS.spinlock);
|
|
Proc *proc2 = NULL;
|
|
LL_FINDPROP(PROCS.procs, proc2, pid, pid2);
|
|
spinlock_release(&PROCS.spinlock);
|
|
|
|
if (proc2 == NULL) {
|
|
ret = E_INVALIDARGUMENT;
|
|
goto done;
|
|
}
|
|
|
|
if (pipenum2 >= PROC_PIPEHANDLES_MAX) {
|
|
ret = E_NOMEMORY;
|
|
goto done;
|
|
}
|
|
|
|
spinlock_acquire(&proc2->pipes_spinlock);
|
|
spinlock_acquire(&proc->pipes_spinlock);
|
|
|
|
if (proc->pipes[pipenum] != NULL && proc->pid == proc->pipes[pipenum]->ownerpid) {
|
|
ipc_pipefree(proc->pipes[pipenum]);
|
|
dlfree(proc->pipes[pipenum]);
|
|
}
|
|
proc->pipes[pipenum] = proc2->pipes[pipenum2];
|
|
|
|
spinlock_release(&proc->pipes_spinlock);
|
|
spinlock_release(&proc2->pipes_spinlock);
|
|
} break;
|
|
default: {
|
|
ret = E_INVALIDARGUMENT;
|
|
} break;
|
|
}
|
|
|
|
done:
|
|
return ret;
|
|
}
|