Incorporate upstream changes from Dasynq.
[oweals/dinit.git] / src / dasynq / dasynq-util.h
1 #ifndef DASYNQ_UTIL_H_INCLUDED
2 #define DASYNQ_UTIL_H_INCLUDED 1
3
4 #include <dasynq-config.h>
5 #include <unistd.h>
6
7 namespace dasynq {
8
9 // Define pipe2, if it's not present in the sytem library. pipe2 is like pipe with an additional flags
10 // argument which can set file/descriptor flags atomically. The emulated version that we generate cannot
11 // do this atomically, of course.
12
13 #if DASYNQ_HAVE_PIPE2 == 0
14 inline int pipe2(int filedes[2], int flags)
15 {
16     if (pipe(filedes) == -1) {
17         return -1;
18     }
19
20     if (flags & O_CLOEXEC) {
21         fcntl(filedes[0], F_SETFD, FD_CLOEXEC);
22         fcntl(filedes[1], F_SETFD, FD_CLOEXEC);
23     }
24
25     if (flags & O_NONBLOCK) {
26         fcntl(filedes[0], F_SETFL, O_NONBLOCK);
27         fcntl(filedes[1], F_SETFL, O_NONBLOCK);
28     }
29
30     return 0;
31 }
32 #else
33
34 using ::pipe2;
35
36 #endif
37
38
39 }
40
41 #endif