Skip to content

Commit

Permalink
Handle EINTR on read
Browse files Browse the repository at this point in the history
It seems read(2) for files may be interrupted on Mac when
ckati is running under a debugger.
  • Loading branch information
shinh committed Apr 11, 2016
1 parent 84ddbd2 commit 706c27f
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
3 changes: 2 additions & 1 deletion file.cc
Expand Up @@ -21,6 +21,7 @@
#include <sys/types.h>
#include <unistd.h>

#include "fileutil.h"
#include "log.h"
#include "parser.h"
#include "stmt.h"
Expand All @@ -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());
Expand Down
2 changes: 1 addition & 1 deletion fileutil.cc
Expand Up @@ -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)
Expand Down
10 changes: 10 additions & 0 deletions fileutil.h
Expand Up @@ -15,6 +15,8 @@
#ifndef FILEUTIL_H_
#define FILEUTIL_H_

#include <errno.h>

#include <memory>
#include <string>
#include <unordered_map>
Expand Down Expand Up @@ -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_

0 comments on commit 706c27f

Please sign in to comment.