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