Subject: Re: When does read block?

Re: When does read block?

From: Peter Stuge <>
Date: Thu, 17 May 2012 20:01:50 +0200

Seth Willits wrote:
> The documentation nor the examples have big conceptual overviews

The SSH RFCs have that. libssh2 introduces few if any concepts not
documented really well in the brief and easy-to-read RFCs.

> The ssh_exec example does not read at all from stderr and it *only*
> looks for read returning 0 to determine the remote side has finished
> executing. According to the ssh_exec example, if there is possibly
> going to be more data, you'll get EAGAIN, if not, you'll get 0.

Sure, the examples have room for improvement. Please send patches! :)

Since there are so many examples I think you will quickly discover
that they are similar in some places and not so similar in other.

Because there are many very small examples I think it's wise to not
trust every example to be a perfect demonstration of every aspect it
uses, but rather to view examples as a showcase of the particular
thing they each show, and nothing else.

> I wondered if (in non-blocking land) 0 on read was effectively an
> eof, and the eof didn't happen every time because of some
> difference in how the remotely executed command behaved.

All of this is described with extreme clarity in the SSH RFCs. You
really should look at them if you want to use the protocol, even if
you do not intend to implement the protocol yourself but instead use
a library. Make sure to read at least RFC 4251.

> The reason I never got an eof in ssh_exec though turns out to
> simply be because the code (which I was relying on for correct
> usage) was not reading from stderr, and there was still data on
> it. ssh_exec doesn't read from stderr, nor does echo, and the
> documentation doesn't say anywhere that you need to, in order to
> reach eof.

Common sense, a bit of systems programming experience on any
UNIX-like system, and reading the SSH RFCs says it though.

> That may be second nature to you, but as a newcomer, I have no idea
> how libssh2 is supposed to be used, and I wrongly (in this case)
> assumed the examples would show me, since the documentation doesn't
> cover high level concepts.

I hope you find the FAQ helpful.

> And a quick note, the documentation says for libssh2_channel_eof:
> "Check if the remote host has sent an EOF status for the selected
> stream." What selected stream?

The channel you pass as parameter. Please send commits to improve the

Received on 2012-05-17