2 * Use vmsplice to fill some user memory into a pipe. vmsplice writes
3 * to stdout, so that must be a pipe.
12 #include <sys/types.h>
16 #define ALIGN(buf) (void *) (((unsigned long) (buf) + align_mask) & ~align_mask)
19 static int align_mask = 65535;
20 static int force_unalign;
21 static int splice_flags;
23 int do_vmsplice(int fd, void *b1, void *b2, int len)
25 struct pollfd pfd = { .fd = fd, .events = POLLOUT, };
26 struct iovec iov[] = {
40 * in a real app you'd be more clever with poll of course,
41 * here we are basically just blocking on output room and
42 * not using the free time for anything interesting.
44 if (poll(&pfd, 1, -1) < 0)
47 written = vmsplice(fd, &iov[idx], 2 - idx, splice_flags);
50 return error("vmsplice");
53 if (written >= iov[idx].iov_len) {
54 int extra = written - iov[idx].iov_len;
57 iov[idx].iov_len -= extra;
58 iov[idx].iov_base += extra;
60 iov[idx].iov_len -= written;
61 iov[idx].iov_base += written;
68 static int usage(char *name)
70 fprintf(stderr, "%s: [-c(lear)] [-u(nalign)] [-g(ift)]| ...\n", name);
74 static int parse_options(int argc, char *argv[])
78 while ((c = getopt(argc, argv, "cug")) != -1) {
89 splice_flags = SPLICE_F_GIFT;
100 int main(int argc, char *argv[])
102 unsigned char *b1, *b2;
104 if (parse_options(argc, argv) < 0)
105 return usage(argv[0]);
107 if (check_output_pipe())
108 return usage(argv[0]);
110 b1 = ALIGN(malloc(SPLICE_SIZE + align_mask));
111 b2 = ALIGN(malloc(SPLICE_SIZE + align_mask));
118 memset(b1, 0xaa, SPLICE_SIZE);
119 memset(b2, 0xbb, SPLICE_SIZE);
122 int half = SPLICE_SIZE / 2;
125 * vmsplice the first half of the buffer into the pipe
127 if (do_vmsplice(STDOUT_FILENO, b1, b2, SPLICE_SIZE))
131 * first half is now in pipe, but we don't quite know when
136 * vmsplice second half
138 if (do_vmsplice(STDOUT_FILENO, b1 + half, b2 + half, SPLICE_SIZE))
142 * We still don't know when we can reuse the second half of
143 * the buffer, but we do now know that all parts of the first
144 * half have been consumed from the pipe - so we can reuse that.
148 * Test option - clear the first half of the buffer, should
152 memset(b1, 0x00, SPLICE_SIZE);
153 memset(b2, 0x00, SPLICE_SIZE);