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;
21 static int do_vmsplice_unmap(int fd, unsigned char *buf, int len)
28 return svmsplice(fd, &iov, 1, SPLICE_F_UNMAP);
31 static int do_vmsplice(int fd, unsigned char **buf, int len)
33 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);
55 return error("vmsplice");
62 iov.iov_len -= written;
63 iov.iov_base += written;
70 static int usage(char *name)
72 fprintf(stderr, "| %s [-d(ump)] [-a(ascii)] [-m(ap)] [-z(eromap)]\n", name);
76 static int parse_options(int argc, char *argv[])
80 while ((c = getopt(argc, argv, "admz")) != -1) {
91 splice_flags |= SPLICE_F_MOVE;
106 static void hexdump(unsigned char *buf, int len)
110 for (i = 0; i < len; i++)
111 printf("%02x", buf[i]);
114 static void asciidump(unsigned char *buf, int len)
118 for (i = 0; i < len; i++)
119 printf("%c", buf[i]);
122 int main(int argc, char *argv[])
127 if (parse_options(argc, argv) < 0)
128 return usage(argv[0]);
130 if (check_input_pipe())
131 return usage(argv[0]);
133 if (do_zeromap && !(splice_flags & SPLICE_F_MOVE)) {
134 fprintf(stderr, "zero map only valid for -m(ove)\n");
135 return usage(argv[0]);
140 memset(buf, 0, 4096);
144 ret = do_vmsplice(STDIN_FILENO, &buf, 4096);
153 if (splice_flags & SPLICE_F_MOVE) {
154 ret = do_vmsplice_unmap(STDIN_FILENO, buf, 4096);