aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShinichiro Hamaji <shinichiro.hamaji@gmail.com>2016-04-11 18:35:06 +0900
committerShinichiro Hamaji <shinichiro.hamaji@gmail.com>2016-04-11 18:35:06 +0900
commit706c27ff12c09b899dce19992feac315cae6073b (patch)
treeb7479166ff0a2a086cd25f6ea0f0718cdaa43651
parent84ddbd225143d94d22a3085557d7d161b295fc2d (diff)
downloadkati-706c27ff12c09b899dce19992feac315cae6073b.tar.gz
Handle EINTR on read
It seems read(2) for files may be interrupted on Mac when ckati is running under a debugger.
-rw-r--r--file.cc3
-rw-r--r--fileutil.cc2
-rw-r--r--fileutil.h10
3 files changed, 13 insertions, 2 deletions
diff --git a/file.cc b/file.cc
index eca09dd..9ef6708 100644
--- a/file.cc
+++ b/file.cc
@@ -21,6 +21,7 @@
#include <sys/types.h>
#include <unistd.h>
+#include "fileutil.h"
#include "log.h"
#include "parser.h"
#include "stmt.h"
@@ -41,7 +42,7 @@ Makefile::Makefile(const string& filename)
mtime_ = st.st_mtime;
buf_.resize(len);
exists_ = true;
- ssize_t r = read(fd, &buf_[0], len);
+ ssize_t r = HANDLE_EINTR(read(fd, &buf_[0], len));
if (r != static_cast<ssize_t>(len)) {
if (r < 0)
PERROR("read failed for %s", filename.c_str());
diff --git a/fileutil.cc b/fileutil.cc
index a2a0d09..c116f31 100644
--- a/fileutil.cc
+++ b/fileutil.cc
@@ -84,7 +84,7 @@ int RunCommand(const string& shell, const string& cmd,
while (true) {
char buf[4096];
- ssize_t r = read(pipefd[0], buf, 4096);
+ ssize_t r = HANDLE_EINTR(read(pipefd[0], buf, 4096));
if (r < 0)
PERROR("read failed");
if (r == 0)
diff --git a/fileutil.h b/fileutil.h
index 5a4740d..7cc3c8e 100644
--- a/fileutil.h
+++ b/fileutil.h
@@ -15,6 +15,8 @@
#ifndef FILEUTIL_H_
#define FILEUTIL_H_
+#include <errno.h>
+
#include <memory>
#include <string>
#include <unordered_map>
@@ -46,4 +48,12 @@ const unordered_map<string, vector<string>*>& GetAllGlobCache();
void ClearGlobCache();
+#define HANDLE_EINTR(x) ({ \
+ int r; \
+ do { \
+ r = (x); \
+ } while (r == -1 && errno == EINTR); \
+ r; \
+ })
+
#endif // FILEUTIL_H_