From: Matthias Kruk Date: Tue, 28 Jul 2020 13:22:15 +0000 (+0900) Subject: libc: Add pipe() function for use in userspace processes X-Git-Url: https://git.corax.cc/?a=commitdiff_plain;h=18a1b2d0ecf082b2cb30147aa03d74f9275755d2;p=corax libc: Add pipe() function for use in userspace processes --- diff --git a/include/unistd.h b/include/unistd.h index 5011b68..09e2937 100644 --- a/include/unistd.h +++ b/include/unistd.h @@ -13,4 +13,6 @@ extern unsigned int sleep(unsigned int); extern int brk(void*); extern void* sbrk(int); +extern int pipe(int pipefd[2]); + #endif /* _UNISTD_H */ diff --git a/libc/unistd.c b/libc/unistd.c new file mode 100644 index 0000000..f0947d0 --- /dev/null +++ b/libc/unistd.c @@ -0,0 +1,48 @@ +#include +#include +#include +#include +#include +#include + +int pipe(int pipefd[2]) +{ + struct cxmsg msg; + struct cxio_pipe *data; + int ret_val; + + data = (struct cxio_pipe*)msg.cm_data; + data->flags = 0; + + msg.cm_len = sizeof(*data); + msg.cm_type = CXIO_PIPE; + + ret_val = cxsendrecv(PID_IO, &msg); + + if(!ret_val) { + struct cxio_response *resp; + + resp = (struct cxio_response*)msg.cm_data; + + ret_val = resp->retval; + errno = resp->error; + + if(!ret_val) { + /* + * Call has succeeded, but we should still check if the amount + * of data returned by the IO process has the expected size. + */ + if(resp->extralen == sizeof(int[2])) { + memcpy(pipefd, resp->extradata, resp->extralen); + } else { + ret_val = -1; + errno = EFAULT; + } + } + } else { + ret_val = -1; + errno = EFAULT; + } + + return(ret_val); +}