Implement pipes in userspace (XCC)
[akaros.git] / tools / compilers / gcc-glibc / glibc-2.19-akaros / sysdeps / akaros / pipe2.c
1 /* Copyright (c) 2016 Google Inc.
2  * Barret Rhoden <brho@cs.berkeley.edu>
3  * See LICENSE for details.
4  *
5  * Pipes, now with openat! */
6
7 #include <errno.h>
8 #include <unistd.h>
9 #include <stddef.h>
10 #include <sys/types.h>
11 #include <sys/stat.h>
12 #include <fcntl.h>
13
14 int __pipe2(int pipedes[2], int flags)
15 {
16         int dirfd, dfd, d1fd, old_errno;
17         char old_errstr[MAX_ERRSTR_LEN];
18
19         if (!pipedes) {
20                 __set_errno(EFAULT);
21                 return -1;
22         }
23         dirfd = open("#pipe", O_PATH);
24         if (dirfd < 0)
25                 return -1;
26         dfd = openat(dirfd, "data", O_RDWR | flags);
27         if (dfd < 0) {
28                 save_err(&old_errno, old_errstr);
29                 close(dirfd);
30                 restore_err(&old_errno, old_errstr);
31                 return -1;
32         }
33         d1fd = openat(dirfd, "data1", O_RDWR | flags);
34         if (d1fd < 0) {
35                 save_err(&old_errno, old_errstr);
36                 close(dfd);
37                 close(dirfd);
38                 restore_err(&old_errno, old_errstr);
39                 return -1;
40         }
41         pipedes[0] = dfd;
42         pipedes[1] = d1fd;
43         return 0;
44 }
45 weak_alias(__pipe2, pipe2)