Andrew Geissler | d159c7f | 2021-09-02 21:05:58 -0500 | [diff] [blame] | 1 | From 682fb48c137b687477008b68863c2a0b73ed47d1 Mon Sep 17 00:00:00 2001 |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 2 | From: Fabio Berton <fabio.berton@ossystems.com.br> |
| 3 | Date: Fri, 9 Sep 2016 16:00:42 -0300 |
| 4 | Subject: [PATCH] handle read-only files |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 5 | |
| 6 | Patch from: |
| 7 | https://github.com/darealshinji/patchelf/commit/40e66392bc4b96e9b4eda496827d26348a503509 |
| 8 | |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 9 | Upstream-Status: Denied [https://github.com/NixOS/patchelf/pull/89] |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 10 | |
| 11 | Signed-off-by: Fabio Berton <fabio.berton@ossystems.com.br> |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 12 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 13 | --- |
| 14 | src/patchelf.cc | 16 +++++++++++++++- |
| 15 | 1 file changed, 15 insertions(+), 1 deletion(-) |
| 16 | |
Andrew Geissler | 7e0e3c0 | 2022-02-25 20:34:39 +0000 | [diff] [blame] | 17 | Index: git/src/patchelf.cc |
| 18 | =================================================================== |
| 19 | --- git.orig/src/patchelf.cc |
| 20 | +++ git/src/patchelf.cc |
| 21 | @@ -534,9 +534,19 @@ void ElfFile<ElfFileParamNames>::sortShd |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 22 | |
Andrew Geissler | d159c7f | 2021-09-02 21:05:58 -0500 | [diff] [blame] | 23 | static void writeFile(const std::string & fileName, const FileContents & contents) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 24 | { |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 25 | + struct stat st; |
| 26 | + int fd; |
| 27 | + |
Andrew Geissler | d25ed32 | 2020-06-27 00:28:28 -0500 | [diff] [blame] | 28 | debug("writing %s\n", fileName.c_str()); |
| 29 | |
| 30 | - int fd = open(fileName.c_str(), O_CREAT | O_TRUNC | O_WRONLY, 0777); |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 31 | + if (stat(fileName.c_str(), &st) != 0) |
| 32 | + error("stat"); |
| 33 | + |
| 34 | + if (chmod(fileName.c_str(), 0600) != 0) |
| 35 | + error("chmod"); |
| 36 | + |
Andrew Geissler | d25ed32 | 2020-06-27 00:28:28 -0500 | [diff] [blame] | 37 | + fd = open(fileName.c_str(), O_CREAT | O_TRUNC | O_WRONLY, 0777); |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 38 | + |
| 39 | if (fd == -1) |
| 40 | error("open"); |
| 41 | |
Andrew Geissler | 7e0e3c0 | 2022-02-25 20:34:39 +0000 | [diff] [blame] | 42 | @@ -551,8 +561,6 @@ static void writeFile(const std::string |
| 43 | bytesWritten += portion; |
| 44 | } |
| 45 | |
| 46 | - if (close(fd) >= 0) |
| 47 | - return; |
| 48 | /* |
| 49 | * Just ignore EINTR; a retry loop is the wrong thing to do. |
| 50 | * |
| 51 | @@ -561,9 +569,11 @@ static void writeFile(const std::string |
| 52 | * http://utcc.utoronto.ca/~cks/space/blog/unix/CloseEINTR |
| 53 | * https://sites.google.com/site/michaelsafyan/software-engineering/checkforeintrwheninvokingclosethinkagain |
| 54 | */ |
| 55 | - if (errno == EINTR) |
| 56 | - return; |
| 57 | - error("close"); |
| 58 | + if ((close(fd) < 0) && errno != EINTR) |
| 59 | + error("close"); |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 60 | + |
| 61 | + if (chmod(fileName.c_str(), st.st_mode) != 0) |
| 62 | + error("chmod"); |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | |