1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2025-12-28 16:01:18 +01:00

ethos: Avoid EOF loops on detached stdin

Add a specific case of EOF on stdin to avoid situations where the
message `error reading from stdio. res=0` is repeated forever if stdin
is not a terminal. When ethos is started as a background process with
stdin redirected to /dev/null, e.g. `ethos ... < /dev/null &`, then
reading stdin will always result in a 0 length read (EOF).
If stdin is a tty we close the program on EOF (CTRL+D in the terminal),
otherwise, we stop reading from stdin after EOF was reached, but
continue tunneling traffic as usual.
This commit is contained in:
Joakim Nohlgård 2018-01-13 16:03:04 +01:00
parent 2c324a42c0
commit 369267863c

View File

@ -540,10 +540,13 @@ int main(int argc, char *argv[])
fprintf(stderr, "----> ethos: activating serial pass through.\n");
_send_hello(serial_fd, &serial, LINE_FRAME_TYPE_HELLO);
int stdin_open = 1;
while(1) {
int activity;
FD_ZERO(&readfds);
FD_SET(STDIN_FILENO, &readfds);
if (stdin_open) {
FD_SET(STDIN_FILENO, &readfds);
}
FD_SET(tap_fd, &readfds);
FD_SET(serial_fd, &readfds);
activity = select( max_fd + 1 , &readfds , NULL , NULL , NULL);
@ -605,7 +608,21 @@ int main(int argc, char *argv[])
if (FD_ISSET(STDIN_FILENO, &readfds)) {
ssize_t res = read(STDIN_FILENO, inbuf, sizeof(inbuf));
if (res <= 0) {
if (res == 0) {
fprintf(stderr, "EOF from stdin\n");
if (isatty(STDIN_FILENO)) {
/* EOF from the terminal means good bye! */
fprintf(stderr, "Bye!\n");
break;
}
else {
/* Ignore EOF when stdin is not a terminal */
close(STDIN_FILENO);
stdin_open = 0;
}
continue;
}
if (res < 0) {
fprintf(stderr, "error reading from stdio. res=%zi\n", res);
continue;
}