Skip to content

Commit 8386499

Browse files
authored
Merge pull request #300 from NixOS/eintr
handle EINTR correctly on write
2 parents d6c7c90 + 5162634 commit 8386499

File tree

1 file changed

+21
-7
lines changed

1 file changed

+21
-7
lines changed

src/patchelf.cc

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040

4141
#include "elf.h"
4242

43-
#ifndef PACKAGE_STRING \
43+
#ifndef PACKAGE_STRING
4444
#define PACKAGE_STRING "patchelf"
4545
#endif
4646

@@ -544,14 +544,28 @@ static void writeFile(const std::string & fileName, const FileContents & content
544544

545545
size_t bytesWritten = 0;
546546
ssize_t portion;
547-
while ((portion = write(fd, contents->data() + bytesWritten, contents->size() - bytesWritten)) > 0)
547+
while (bytesWritten < contents->size()) {
548+
if ((portion = write(fd, contents->data() + bytesWritten, contents->size() - bytesWritten)) < 0) {
549+
if (errno == EINTR)
550+
continue;
551+
error("write");
552+
}
548553
bytesWritten += portion;
554+
}
549555

550-
if (bytesWritten != contents->size())
551-
error("write");
552-
553-
if (close(fd) != 0)
554-
error("close");
556+
if (close(fd) >= 0)
557+
return;
558+
/*
559+
* Just ignore EINTR; a retry loop is the wrong thing to do.
560+
*
561+
* http://lkml.indiana.edu/hypermail/linux/kernel/0509.1/0877.html
562+
* https://bugzilla.gnome.org/show_bug.cgi?id=682819
563+
* http://utcc.utoronto.ca/~cks/space/blog/unix/CloseEINTR
564+
* https://sites.google.com/site/michaelsafyan/software-engineering/checkforeintrwheninvokingclosethinkagain
565+
*/
566+
if (errno == EINTR)
567+
return;
568+
error("close");
555569
}
556570

557571

0 commit comments

Comments
 (0)