Commit | Line | Data |
---|---|---|
d8525fbd JA |
1 | #ifndef SPLICE_H |
2 | #define SPLICE_H | |
3 | ||
a05d2973 | 4 | #include <errno.h> |
001a41ec | 5 | #include <sys/uio.h> |
76797253 | 6 | #include <sys/stat.h> |
af14506c | 7 | #include <linux/unistd.h> |
001a41ec | 8 | |
d8525fbd | 9 | #if defined(__i386__) |
a05d2973 JA |
10 | #define __NR_sys_splice 313 |
11 | #define __NR_sys_tee 315 | |
12 | #define __NR_sys_vmsplice 316 | |
d8525fbd | 13 | #elif defined(__x86_64__) |
a05d2973 JA |
14 | #define __NR_sys_splice 275 |
15 | #define __NR_sys_tee 276 | |
16 | #define __NR_sys_vmsplice 278 | |
d8525fbd | 17 | #elif defined(__powerpc__) || defined(__powerpc64__) |
a05d2973 JA |
18 | #define __NR_sys_splice 283 |
19 | #define __NR_sys_tee 284 | |
20 | #define __NR_sys_vmsplice 285 | |
d8525fbd | 21 | #elif defined(__ia64__) |
a05d2973 JA |
22 | #define __NR_sys_splice 1297 |
23 | #define __NR_sys_tee 1301 | |
24 | #define __NR_sys_vmsplice 1302 | |
d8525fbd JA |
25 | #else |
26 | #error unsupported arch | |
27 | #endif | |
28 | ||
5d4d8cbd JA |
29 | #ifndef SPLICE_F_MOVE |
30 | ||
d8525fbd JA |
31 | #define SPLICE_F_MOVE (0x01) /* move pages instead of copying */ |
32 | #define SPLICE_F_NONBLOCK (0x02) /* don't block on the pipe splicing (but */ | |
33 | /* we may still block on the fd we splice */ | |
34 | /* from/to, of course */ | |
35 | #define SPLICE_F_MORE (0x04) /* expect more data */ | |
96897528 | 36 | #define SPLICE_F_GIFT (0x08) /* pages passed in are a gift */ |
d8525fbd | 37 | |
13b72067 JA |
38 | #endif /* SPLICE_F_MOVE defined */ |
39 | ||
40 | static inline int ssplice(int fdin, loff_t *off_in, int fdout, loff_t *off_out, | |
ae118771 | 41 | size_t len, unsigned int flags) |
d8525fbd | 42 | { |
1233e72a BP |
43 | |
44 | return syscall(__NR_sys_splice, fdin, off_in, fdout, off_out, len, flags); | |
d8525fbd JA |
45 | } |
46 | ||
13b72067 | 47 | static inline int stee(int fdin, int fdout, size_t len, unsigned int flags) |
d8525fbd | 48 | { |
1233e72a | 49 | return syscall(__NR_sys_tee, fdin, fdout, len, flags); |
d8525fbd JA |
50 | } |
51 | ||
13b72067 JA |
52 | static inline int svmsplice(int fd, const struct iovec *iov, |
53 | unsigned long nr_segs, unsigned int flags) | |
7ee0f27f | 54 | { |
1233e72a | 55 | return syscall(__NR_sys_vmsplice, fd, iov, nr_segs, flags); |
7ee0f27f JA |
56 | } |
57 | ||
d8525fbd JA |
58 | #define SPLICE_SIZE (64*1024) |
59 | ||
60 | #define BUG_ON(c) assert(!(c)) | |
61 | ||
62 | #define min(x,y) ({ \ | |
63 | typeof(x) _x = (x); \ | |
64 | typeof(y) _y = (y); \ | |
65 | (void) (&_x == &_y); \ | |
66 | _x < _y ? _x : _y; }) | |
67 | ||
68 | #define max(x,y) ({ \ | |
69 | typeof(x) _x = (x); \ | |
70 | typeof(y) _y = (y); \ | |
71 | (void) (&_x == &_y); \ | |
72 | _x > _y ? _x : _y; }) | |
73 | ||
74 | static inline int error(const char *n) | |
75 | { | |
76 | perror(n); | |
77 | return -1; | |
78 | } | |
79 | ||
76797253 JA |
80 | static int __check_pipe(int pfd) |
81 | { | |
82 | struct stat sb; | |
83 | ||
84 | if (fstat(pfd, &sb) < 0) | |
85 | return error("stat"); | |
86 | if (!S_ISFIFO(sb.st_mode)) | |
87 | return 1; | |
88 | ||
89 | return 0; | |
90 | } | |
91 | ||
92 | static inline int check_input_pipe(void) | |
93 | { | |
94 | if (!__check_pipe(STDIN_FILENO)) | |
95 | return 0; | |
96 | ||
97 | fprintf(stderr, "stdin must be a pipe\n"); | |
98 | return 1; | |
99 | } | |
100 | ||
101 | static inline int check_output_pipe(void) | |
102 | { | |
103 | if (!__check_pipe(STDOUT_FILENO)) | |
104 | return 0; | |
105 | ||
106 | fprintf(stderr, "stdout must be a pipe\n"); | |
107 | return 1; | |
108 | } | |
109 | ||
d8525fbd | 110 | #endif |