Skip to content

Commit

Permalink
Let signals interrupt fgets unless SA_RESTART set
Browse files Browse the repository at this point in the history
See investigation in #1130.

fgets internally calls readv. readv is a @restartable function that
understands SA_RESTART. If SA_RESTART is set, readv already handles
restarting the system call and eventually the string is transparently
returned to the fgets caller.

When SA_RESTART is not set, -1 EINTR should bubble up to the fgets
caller. However, until this commit, fgets itself would detect EINTR and
keep retrying until it read an entire line.

This commit fixes this behaviour so that fgets understands SA_RESTART.

I hereby assign copyright for this commit to Justine Tunney.

Signed-off-by: Cadence Ember <cadence@disroot.org>
  • Loading branch information
cloudrac3r committed Apr 26, 2024
1 parent b9d6e6e commit 9df3689
Showing 1 changed file with 1 addition and 5 deletions.
6 changes: 1 addition & 5 deletions libc/stdio/fgets_unlocked.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,7 @@ char *fgets_unlocked(char *s, int size, FILE *f) {
if (t) break;
} else {
if ((c = fgetc_unlocked(f)) == -1) {
if (ferror_unlocked(f) == EINTR) {
continue;
} else {
break;
}
break;
}
*p++ = c & 255;
if (c == '\n') break;
Expand Down

0 comments on commit 9df3689

Please sign in to comment.