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 */ | |
32 | ||
33 | static inline int splice(int fdin, loff_t *off_in, int fdout, loff_t *off_out, | |
34 | size_t len, unsigned long flags) | |
35 | { | |
36 | return syscall(__NR_splice, fdin, off_in, fdout, off_out, len, flags); | |
d8525fbd JA |
37 | } |
38 | ||
39 | static inline int tee(int fdin, int fdout, size_t len, unsigned int flags) | |
40 | { | |
41 | return syscall(__NR_tee, fdin, fdout, len, flags); | |
42 | } | |
43 | ||
001a41ec JA |
44 | static inline int vmsplice(int fd, const struct iovec *iov, |
45 | unsigned long nr_segs, unsigned int flags) | |
7ee0f27f | 46 | { |
001a41ec | 47 | return syscall(__NR_vmsplice, fd, iov, nr_segs, flags); |
7ee0f27f JA |
48 | } |
49 | ||
d8525fbd JA |
50 | #define SPLICE_SIZE (64*1024) |
51 | ||
52 | #define BUG_ON(c) assert(!(c)) | |
53 | ||
54 | #define min(x,y) ({ \ | |
55 | typeof(x) _x = (x); \ | |
56 | typeof(y) _y = (y); \ | |
57 | (void) (&_x == &_y); \ | |
58 | _x < _y ? _x : _y; }) | |
59 | ||
60 | #define max(x,y) ({ \ | |
61 | typeof(x) _x = (x); \ | |
62 | typeof(y) _y = (y); \ | |
63 | (void) (&_x == &_y); \ | |
64 | _x > _y ? _x : _y; }) | |
65 | ||
66 | static inline int error(const char *n) | |
67 | { | |
68 | perror(n); | |
69 | return -1; | |
70 | } | |
71 | ||
76797253 JA |
72 | static int __check_pipe(int pfd) |
73 | { | |
74 | struct stat sb; | |
75 | ||
76 | if (fstat(pfd, &sb) < 0) | |
77 | return error("stat"); | |
78 | if (!S_ISFIFO(sb.st_mode)) | |
79 | return 1; | |
80 | ||
81 | return 0; | |
82 | } | |
83 | ||
84 | static inline int check_input_pipe(void) | |
85 | { | |
86 | if (!__check_pipe(STDIN_FILENO)) | |
87 | return 0; | |
88 | ||
89 | fprintf(stderr, "stdin must be a pipe\n"); | |
90 | return 1; | |
91 | } | |
92 | ||
93 | static inline int check_output_pipe(void) | |
94 | { | |
95 | if (!__check_pipe(STDOUT_FILENO)) | |
96 | return 0; | |
97 | ||
98 | fprintf(stderr, "stdout must be a pipe\n"); | |
99 | return 1; | |
100 | } | |
101 | ||
d8525fbd | 102 | #endif |