2 * Use vmsplice to fill some user memory into a pipe. vmsplice writes
3 * to stdout, so that must be a pipe.
13 #include <sys/types.h>
17 #define ALIGN(buf) (void *) (((unsigned long) (buf) + align_mask) & ~align_mask)
20 static int align_mask = 65535;
21 static int force_unalign;
22 static int splice_flags;
24 int do_vmsplice(int fd, void *b1, void *b2, int len)
26 struct pollfd pfd = { .fd = fd, .events = POLLOUT, };
27 struct iovec iov[] = {
41 * in a real app you'd be more clever with poll of course,
42 * here we are basically just blocking on output room and
43 * not using the free time for anything interesting.
45 if (poll(&pfd, 1, -1) < 0)
48 written = svmsplice(fd, &iov[idx], 2 - idx, splice_flags);
51 return error("vmsplice");
54 if ((size_t) written >= iov[idx].iov_len) {
55 int extra = written - iov[idx].iov_len;
58 iov[idx].iov_len -= extra;
59 iov[idx].iov_base += extra;
61 iov[idx].iov_len -= written;
62 iov[idx].iov_base += written;
69 static int usage(char *name)
71 fprintf(stderr, "%s: [-c(lear)] [-u(nalign)] [-g(ift)]| ...\n", name);
75 static int parse_options(int argc, char *argv[])
79 while ((c = getopt(argc, argv, "cug")) != -1) {
90 splice_flags = SPLICE_F_GIFT;
101 int main(int argc, char *argv[])
103 unsigned char *b1, *b2;
105 if (parse_options(argc, argv) < 0)
106 return usage(argv[0]);
108 if (check_output_pipe())
109 return usage(argv[0]);
111 b1 = ALIGN(malloc(SPLICE_SIZE + align_mask));
112 b2 = ALIGN(malloc(SPLICE_SIZE + align_mask));
119 memset(b1, 0xaa, SPLICE_SIZE);
120 memset(b2, 0xbb, SPLICE_SIZE);
123 int half = SPLICE_SIZE / 2;
126 * vmsplice the first half of the buffer into the pipe
128 if (do_vmsplice(STDOUT_FILENO, b1, b2, SPLICE_SIZE))
132 * first half is now in pipe, but we don't quite know when
137 * vmsplice second half
139 if (do_vmsplice(STDOUT_FILENO, b1 + half, b2 + half, SPLICE_SIZE))
143 * We still don't know when we can reuse the second half of
144 * the buffer, but we do now know that all parts of the first
145 * half have been consumed from the pipe - so we can reuse that.
149 * Test option - clear the first half of the buffer, should
153 memset(b1, 0x00, SPLICE_SIZE);
154 memset(b2, 0x00, SPLICE_SIZE);