2 * Use vmsplice to splice data from a pipe to user space memory.
12 #include <sys/types.h>
18 static int do_zeromap;
19 static int splice_flags;
20 static unsigned int splice_size = SPLICE_SIZE;
22 static int do_vmsplice_unmap(int fd, unsigned char *buf, int len)
29 return svmsplice(fd, &iov, 1, SPLICE_F_UNMAP);
32 static int do_vmsplice(int fd, void **buf, int len)
34 struct pollfd pfd = { .fd = fd, .events = POLLIN, };
44 * in a real app you'd be more clever with poll of course,
45 * here we are basically just blocking on output room and
46 * not using the free time for anything interesting.
48 if (poll(&pfd, 1, -1) < 0)
51 written = svmsplice(fd, &iov, 1, splice_flags);
57 return error("vmsplice");
64 iov.iov_len -= written;
65 iov.iov_base += written;
72 static int usage(char *name)
74 fprintf(stderr, "| %s [-s(ize)] [-d(ump)] [-a(ascii)] [-m(ap)] [-z(eromap)] [-n non-block]\n", name);
78 static int parse_options(int argc, char *argv[])
82 while ((c = getopt(argc, argv, "admzs:n")) != -1) {
93 splice_flags |= SPLICE_F_MOVE;
101 splice_size = atoi(optarg);
105 splice_flags |= SPLICE_F_NONBLOCK;
113 if (do_zeromap && !(splice_flags & SPLICE_F_MOVE)) {
114 fprintf(stderr, "zero map only valid for -m(ove)\n");
121 static void hexdump(unsigned char *buf, int len)
125 for (i = 0; i < len; i++)
126 printf("%02x", buf[i]);
129 static void asciidump(unsigned char *buf, int len)
133 for (i = 0; i < len; i++)
134 printf("%c", buf[i]);
137 int main(int argc, char *argv[])
142 if (parse_options(argc, argv) < 0)
143 return usage(argv[0]);
145 if (check_input_pipe())
146 return usage(argv[0]);
149 buf = malloc(splice_size);
150 memset(buf, 0, splice_size);
154 ret = do_vmsplice(STDIN_FILENO, &buf, splice_size);
165 if (splice_flags & SPLICE_F_MOVE) {
166 ret = do_vmsplice_unmap(STDIN_FILENO, buf, ret);