nelhage / reptyr

Reparent a running program to a new terminal
MIT License
5.82k stars 215 forks source link

Retry syscalls on EINTR #37

Closed worr closed 10 years ago

worr commented 10 years ago

Most syscalls that can return EINTR aren't retried if they return EINTR. This diff just adds a simple pattern to retry whenever EINTR is returned.

nelhage commented 10 years ago

I believe that on Linux we can't get EINTR until we set up the SIGWINCH signal handler -- EINTR can only happen if a signal arrives and is handled by a userspace signal handler (c.f. http://www.gnu.org/software/libc/manual/html_node/Interrupted-Primitives.html). And there's a handful of things that are retried by this patch (e.g. sscanf) that can't ever (again, AFAIK) return EINTR.

If we do need to go down this route, I think I'd rather adopt something like glibc's TEMP_FAILURE_RETRY macro, instead of open-coding all the do / while loops inline everywhere.

worr commented 10 years ago

Wow, yeah. My notion of EINTR semantics was totally wrong. Thanks for the information.

I believe that tcsetattr(3) can be safely restarted on EINTR. Would you accept a PR for that?

nelhage commented 10 years ago

Hm. I see no mention in termios(3) of those functions being allowed to return EINTR, and, in general, I don't expect non-long-blocking functions to be ever return EINTR.

worr commented 10 years ago

I was going off of the POSIX docs, since the Linux manpages don't specify which values for errno are valid: http://pubs.opengroup.org/onlinepubs/009695399/functions/tcsetattr.html

nelhage commented 10 years ago

Hm, fair enough. Ok, I'd take that patch.