[PATCH] Add vmsplice
[splice.git] / vmsplice.c
1 /*
2  * Use vmsplice to fill some user memory into a pipe. vmsplice writes
3  * to stdout, so that must be a pipe.
4  */
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <unistd.h>
8 #include <fcntl.h>
9 #include <limits.h>
10 #include <sys/poll.h>
11 #include <sys/stat.h>
12 #include <sys/types.h>
13
14 #include "splice.h"
15
16 #define ALIGN_BUF
17
18 #ifdef ALIGN_BUF
19 #define ALIGN_MASK      (65535) /* 64k-1, should just be PAGE_SIZE - 1 */
20 #define ALIGN(buf)      (void *) (((unsigned long) (buf) + ALIGN_MASK) & ~ALIGN_MASK)
21 #else
22 #define ALIGN_MASK      (0)
23 #define ALIGN(buf)      (buf)
24 #endif
25
26 int do_vmsplice(int fd, void *buffer, int len)
27 {
28         struct pollfd pfd = { .fd = fd, .events = POLLOUT, };
29         int written;
30
31         while (len) {
32                 /*
33                  * in a real app you'd be more clever with poll of course,
34                  * here we are basically just blocking on output room and
35                  * not using the free time for anything interesting.
36                  */
37                 if (poll(&pfd, 1, -1) < 0)
38                         return error("poll");
39
40                 written = vmsplice(fd, buffer, min(SPLICE_SIZE, len), 0);
41
42                 if (written <= 0)
43                         return error("vmsplice");
44
45                 len -= written;
46         }
47
48         return 0;
49 }
50
51 int main(int argc, char *argv[])
52 {
53         unsigned char *buffer;
54         struct stat sb;
55         long page_size;
56         int i, ret;
57
58         if (fstat(STDOUT_FILENO, &sb) < 0)
59                 return error("stat");
60         if (!S_ISFIFO(sb.st_mode)) {
61                 fprintf(stderr, "stdout must be a pipe\n");
62                 return 1;
63         }
64
65         ret = fcntl(STDOUT_FILENO, F_GETPSZ);
66         if (ret < 0)
67                 return error("F_GETPSZ");
68
69         page_size = sysconf(_SC_PAGESIZE);
70         if (page_size < 0)
71                 return error("_SC_PAGESIZE");
72
73         fprintf(stderr, "Pipe size: %d pages / %ld bytes\n", ret, ret * page_size);
74
75         buffer = ALIGN(malloc(2 * SPLICE_SIZE + ALIGN_MASK));
76         for (i = 0; i < 2 * SPLICE_SIZE; i++)
77                 buffer[i] = (i & 0xff);
78
79         do {
80                 /*
81                  * vmsplice the first half of the buffer into the pipe
82                  */
83                 if (do_vmsplice(STDOUT_FILENO, buffer, SPLICE_SIZE))
84                         break;
85
86                 /*
87                  * first half is now in pipe, but we don't quite know when
88                  * we can reuse it.
89                  */
90
91                 /*
92                  * vmsplice second half
93                  */
94                 if (do_vmsplice(STDOUT_FILENO, buffer + SPLICE_SIZE, SPLICE_SIZE))
95                         break;
96
97                 /*
98                  * We still don't know when we can reuse the second half of
99                  * the buffer, but we do now know that all parts of the first
100                  * half have been consumed from the pipe - so we can reuse that.
101                  */
102         } while (0);
103
104         return 0;
105 }