Message ID | YDiRywyld/0OTT5U@coredump.intra.peff.net (mailing list archive) |
---|---|
State | Accepted |
Commit | 2b08101204066cc8221afb9029c07649f67da0a0 |
Headers | show |
Series | [v2] Makefile: add OPEN_RETURNS_EINTR knob | expand |
Jeff King <peff@peff.net> writes: > However, note that we must not enable this on Windows. It doesn't do > anything there, and the macro overrides the existing mingw_open() > redirection. I've added a preemptive #undef here in the mingw header > (which is processed first) to just quietly disable it (we could also > make it an #error, but there is little point in being so aggressive). > ... > diff --git a/compat/open.c b/compat/open.c > new file mode 100644 > index 0000000000..eb3754a23b > --- /dev/null > +++ b/compat/open.c > @@ -0,0 +1,25 @@ > +#include "git-compat-util.h" > + > +#undef open > +int git_open_with_retry(const char *path, int flags, ...) > +{ > + mode_t mode = 0; > + int ret; > + > + /* > + * Also O_TMPFILE would take a mode, but it isn't defined everywhere. > + * And anyway, we don't use it in our code base. > + */ That is being extra careful---I like it very much. > + if (flags & O_CREAT) { > + va_list ap; > + va_start(ap, flags); > + mode = va_arg(ap, int); > + va_end(ap); > + } > + > + do { > + ret = open(path, flags, mode); > + } while (ret < 0 && errno == EINTR); > + > + return ret; > +} Thanks. > diff --git a/git-compat-util.h b/git-compat-util.h > index 838246289c..551cc9f22f 100644 > --- a/git-compat-util.h > +++ b/git-compat-util.h > @@ -788,6 +788,12 @@ int git_vsnprintf(char *str, size_t maxsize, > const char *format, va_list ap); > #endif > > +#ifdef OPEN_RETURNS_EINTR > +#undef open > +#define open git_open_with_retry > +int git_open_with_retry(const char *path, int flag, ...); > +#endif > + > #ifdef __GLIBC_PREREQ > #if __GLIBC_PREREQ(2, 1) > #define HAVE_STRCHRNUL
diff --git a/Makefile b/Makefile index 9b1bde2e0e..c4872ca16e 100644 --- a/Makefile +++ b/Makefile @@ -22,6 +22,9 @@ all:: # when attempting to read from an fopen'ed directory (or even to fopen # it at all). # +# Define OPEN_RETURNS_EINTR if your open() system call may return EINTR +# when a signal is received (as opposed to restarting). +# # Define NO_OPENSSL environment variable if you do not have OpenSSL. # # Define USE_LIBPCRE if you have and want to use libpcre. Various @@ -1538,6 +1541,10 @@ ifdef FREAD_READS_DIRECTORIES COMPAT_CFLAGS += -DFREAD_READS_DIRECTORIES COMPAT_OBJS += compat/fopen.o endif +ifdef OPEN_RETURNS_EINTR + COMPAT_CFLAGS += -DOPEN_RETURNS_EINTR + COMPAT_OBJS += compat/open.o +endif ifdef NO_SYMLINK_HEAD BASIC_CFLAGS += -DNO_SYMLINK_HEAD endif diff --git a/compat/mingw.h b/compat/mingw.h index af8eddd73e..c9a52ad64a 100644 --- a/compat/mingw.h +++ b/compat/mingw.h @@ -227,6 +227,7 @@ int mingw_rmdir(const char *path); int mingw_open (const char *filename, int oflags, ...); #define open mingw_open +#undef OPEN_RETURNS_EINTR int mingw_fgetc(FILE *stream); #define fgetc mingw_fgetc diff --git a/compat/open.c b/compat/open.c new file mode 100644 index 0000000000..eb3754a23b --- /dev/null +++ b/compat/open.c @@ -0,0 +1,25 @@ +#include "git-compat-util.h" + +#undef open +int git_open_with_retry(const char *path, int flags, ...) +{ + mode_t mode = 0; + int ret; + + /* + * Also O_TMPFILE would take a mode, but it isn't defined everywhere. + * And anyway, we don't use it in our code base. + */ + if (flags & O_CREAT) { + va_list ap; + va_start(ap, flags); + mode = va_arg(ap, int); + va_end(ap); + } + + do { + ret = open(path, flags, mode); + } while (ret < 0 && errno == EINTR); + + return ret; +} diff --git a/git-compat-util.h b/git-compat-util.h index 838246289c..551cc9f22f 100644 --- a/git-compat-util.h +++ b/git-compat-util.h @@ -788,6 +788,12 @@ int git_vsnprintf(char *str, size_t maxsize, const char *format, va_list ap); #endif +#ifdef OPEN_RETURNS_EINTR +#undef open +#define open git_open_with_retry +int git_open_with_retry(const char *path, int flag, ...); +#endif + #ifdef __GLIBC_PREREQ #if __GLIBC_PREREQ(2, 1) #define HAVE_STRCHRNUL