Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

5
  • Re. calling select() to see if a write would block, wouldn't select() be capable of waiting for a change in the "writable" status of the fd, the same way it watches for other fds to become readable? All the SSH server would really need to know here is if the program ever reads. But does select() actually tell if someone is actively calling read() on the fd? Or just that there's an fd open for reading, which might not mean the program is actually calling read() on it? Commented Jan 26, 2022 at 12:49
  • @ilkkachu The job of select is to tell whether read/write would block. Not whether there has been a time in the past when it didn't block. If the application tries reading from stdin for a while, then times out, and then sshd calls select, sshd will not see that it can write without blocking. Commented Jan 26, 2022 at 13:08
  • no, of course not. But often the program (sshd here) using select() would spend most of its time blocking on the select() call, and so would be immediately notified when the fd becomes available. Commented Jan 26, 2022 at 14:07
  • Your second paragraph makes it seem like using select would work when the consumer does blocking reads, but not when it does non-blocking reads. However, even when the consumer only does blocking reads, select wouldn't work at the start, since writing would only block after the pipe buffer has filled. That means that in a typical pipe, before the producer has written anything, select would always return saying that writing wouldn't block, regardless of whether the consumer is trying to slurp, read intermittently, or not read at all. Commented Jan 26, 2022 at 22:17
  • Also, I agree with ilkkachu. If the pipe buffer weren't an issue (e.g. if it were possible to set its size to 0), then intermittent reads shouldn't be a problem, since sshd could via an alternate thread call select and stay blocked, while it starts the client-specified command. select would only return once the command tries to do any sort of read. Again, this is in the hypothetical scenario where the pipe buffer size could be set to 0. Right now, it must be at least equal to the page size. Commented Jan 26, 2022 at 22:53