From c792262b137a5f2daddac22f82e7d8d98d0d7d31 Mon Sep 17 00:00:00 2001 From: Andrew Gregory Date: Wed, 6 Aug 2014 16:36:00 -0400 Subject: wrap fgets to retry on EINTR The read() underlying fgets() can be interrupted by a signal handler causing fgets() to return NULL. Before we started handling SIGWINCH, the odds of interrupting a read were low and typically resulted in termination anyway. Replace all fgets calls with a wrapper that retries in EINTR. Signed-off-by: Andrew Gregory --- src/common/util-common.c | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'src/common/util-common.c') diff --git a/src/common/util-common.c b/src/common/util-common.c index 3316eaec..ab74e7c6 100644 --- a/src/common/util-common.c +++ b/src/common/util-common.c @@ -17,6 +17,7 @@ * along with this program. If not, see . */ +#include #include #include @@ -102,6 +103,30 @@ int llstat(char *path, struct stat *buf) return ret; } +/** Wrapper around fgets() which properly handles EINTR + * @param s string to read into + * @param size maximum length to read + * @param stream stream to read from + * @return value returned by fgets() + */ +char *safe_fgets(char *s, int size, FILE *stream) +{ + char *ret; + int errno_save = errno, ferror_save = ferror(stream); + while((ret = fgets(s, size, stream)) == NULL && !feof(stream)) { + if(errno == EINTR) { + /* clear any errors we set and try again */ + errno = errno_save; + if(!ferror_save) { + clearerr(stream); + } + } else { + break; + } + } + return ret; +} + #ifndef HAVE_STRNDUP /* A quick and dirty implementation derived from glibc */ /** Determines the length of a fixed-size string. -- cgit v1.2.3-24-g4f1b