summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorAndrew Gregory <andrew.gregory.8@gmail.com>2014-08-06 16:36:00 -0400
committerAllan McRae <allan@archlinux.org>2014-09-23 21:43:16 +1000
commitc792262b137a5f2daddac22f82e7d8d98d0d7d31 (patch)
treed3177e6c722a96da72503a912d4c5fded0ef78e8 /src
parent9c066dff439ba453f4c362e1875b794cf3f362ed (diff)
downloadpacman-c792262b137a5f2daddac22f82e7d8d98d0d7d31.tar.xz
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 <andrew.gregory.8@gmail.com>
Diffstat (limited to 'src')
-rw-r--r--src/common/util-common.c25
-rw-r--r--src/common/util-common.h3
-rw-r--r--src/pacman/ini.c2
-rw-r--r--src/pacman/util.c8
4 files changed, 33 insertions, 5 deletions
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 <http://www.gnu.org/licenses/>.
*/
+#include <errno.h>
#include <stdlib.h>
#include <string.h>
@@ -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.
diff --git a/src/common/util-common.h b/src/common/util-common.h
index 576702fa..ca8db5ab 100644
--- a/src/common/util-common.h
+++ b/src/common/util-common.h
@@ -20,6 +20,7 @@
#ifndef _PM_UTIL_COMMON_H
#define _PM_UTIL_COMMON_H
+#include <stdio.h>
#include <sys/stat.h> /* struct stat */
const char *mbasename(const char *path);
@@ -27,6 +28,8 @@ char *mdirname(const char *path);
int llstat(char *path, struct stat *buf);
+char *safe_fgets(char *s, int size, FILE *stream);
+
#ifndef HAVE_STRNDUP
char *strndup(const char *s, size_t n);
#endif
diff --git a/src/pacman/ini.c b/src/pacman/ini.c
index 2a3ef0ed..2eb32302 100644
--- a/src/pacman/ini.c
+++ b/src/pacman/ini.c
@@ -66,7 +66,7 @@ static int _parse_ini(const char *file, ini_parser_fn cb, void *data,
goto cleanup;
}
- while(fgets(line, PATH_MAX, fp)) {
+ while(safe_fgets(line, PATH_MAX, fp)) {
char *key, *value, *ptr;
size_t line_len;
diff --git a/src/pacman/util.c b/src/pacman/util.c
index 6a095fdb..2671e54c 100644
--- a/src/pacman/util.c
+++ b/src/pacman/util.c
@@ -1403,7 +1403,7 @@ int multiselect_question(char *array, int count)
flush_term_input(fileno(stdin));
- if(fgets(response, response_len, stdin)) {
+ if(safe_fgets(response, response_len, stdin)) {
const size_t response_incr = 64;
size_t len;
/* handle buffer not being large enough to read full line case */
@@ -1416,7 +1416,7 @@ int multiselect_question(char *array, int count)
lastchar = response + response_len - 1;
/* sentinel byte */
*lastchar = 1;
- if(fgets(response + response_len - response_incr - 1,
+ if(safe_fgets(response + response_len - response_incr - 1,
response_incr + 1, stdin) == 0) {
free(response);
return -1;
@@ -1467,7 +1467,7 @@ int select_question(int count)
flush_term_input(fileno(stdin));
- if(fgets(response, sizeof(response), stdin)) {
+ if(safe_fgets(response, sizeof(response), stdin)) {
size_t len = strtrim(response);
if(len > 0) {
int n;
@@ -1521,7 +1521,7 @@ static int question(short preset, const char *format, va_list args)
flush_term_input(fd_in);
- if(fgets(response, sizeof(response), stdin)) {
+ if(safe_fgets(response, sizeof(response), stdin)) {
size_t len = strtrim(response);
if(len == 0) {
return preset;