]> git.corax.cc Git - corax/commitdiff
libc: Add pipe() function for use in userspace processes
authorMatthias Kruk <m@m10k.eu>
Tue, 28 Jul 2020 13:22:15 +0000 (22:22 +0900)
committerMatthias Kruk <m@m10k.eu>
Tue, 28 Jul 2020 13:22:15 +0000 (22:22 +0900)
include/unistd.h
libc/unistd.c [new file with mode: 0644]

index 5011b687ba1c389c1d572450b46040e623037772..09e2937f9d3f8d108f59379f6148bb3aca973646 100644 (file)
@@ -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 (file)
index 0000000..f0947d0
--- /dev/null
@@ -0,0 +1,48 @@
+#include <unistd.h>
+#include <corax/ipc.h>
+#include <corax/ipc/io.h>
+#include <crxstd.h>
+#include <string.h>
+#include <errno.h>
+
+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);
+}