Get Pipe buffer size

Writting a new implementation for Monkey,  I was looking for some way to know “how many bytes are ready to be readen from a Pipe”?, obviously I want to get that information before to read them. As I coudn’t find any information I wrote to Linus Torvalds requiring him a new syscall to do it… Today I got his response:

int bytes, err;
err = ioctl(fd, FIONREAD, &bytes);

is the traditional UNIX way to query how many bytes are readable right now
in a pipe (or a socket or tty, for that matter).

NOTE! There is _not_ a way to query how much you could write to a pipe,
though. Although normally I think you should be able to rely on being able
to write PIPE_BUF characters if a poll() or select() says “writable”. But
you may be able to write more.

Hope this tip help someone… thanks Linus!

sendfile() and splice()

Last night searching for information about how to use splice() described as “an innovative I/O method which was added in Linux-2.6.17, based in a in-kernel buffer that the user has control over, where splice() moves data to/from the buffer from/to an arbitrary file descriptor with splice(), while “tee()” copies the data in one buffer to another”, I realized that’s don’t exist good examples about how to use it as a replacement for sendfile(),  two hours ago reading about some kernel commits I found that Linux-2.6.23 has changed the original sendfile() implementation by splice() internally:

“For the particular case of sending the data from a file descriptor to a fd socket, there’s been always the sendfile() syscall. splice() however is a generic mechanism, not just limited to what sendfile(). In other words, sendfile() is just a small subset of what splice can do, splice obsoletes it. In Linux 2.6.23, the sendfile() mechanism implementation is killed, but the API and its functionality is not removed, it’s instead implemented internally with the splice() mechanisms.”

Resume: don’t waste time using splice in your code to send files through sockets…it’s already done!