I want to drain a pipe by pushing all its data to a socket using the splice
system call on Linux.
My current attempt is:
ssize_t splice(int i, loff_t* io, int o, loff_t* oo, size_t l, unsigned int flags);
int spliceAll(int i, loff_t* io, int o, loff_t* oo, size_t l, unsigned int flags) {
size_t t = 0; int n = 0;
while (t < l) { if ((n = splice(i, io, o, oo, l - t, flags)) < 0) break; t += n; }
return t == l ? 0 : -1;
}
the user-space buffer equivalent to this is is simply:
int sendAll(int s, void* b, size_t l, int flags) {
size_t t = 0; int n = 0;
while (t < l) { if ((n = send(s, b + t, l - t, flags)) < 0) break; t += n; }
return t == l ? 0 : -1;
}
Assuming non-blocking sockets (and pipes for spliceAll
) AND send
& splice
never returning 0
:
- Is the loop in
spliceAll
correct?
- Are there any other errors I am failing to see in both
spliceAll
andsendAll
?