Jed Estep | a7b9a46 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Tao Bao | 3dc14cb | 2016-11-22 11:37:13 -0800 | [diff] [blame] | 17 | #include "ota_io.h" |
Jed Estep | a7b9a46 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 18 | |
| 19 | #include <errno.h> |
| 20 | #include <fcntl.h> |
| 21 | #include <stdio.h> |
| 22 | #include <sys/stat.h> |
| 23 | #include <unistd.h> |
| 24 | |
Tao Bao | 3dc14cb | 2016-11-22 11:37:13 -0800 | [diff] [blame] | 25 | #include <map> |
| 26 | #include <memory> |
Tianjie Xu | 4bb11c7 | 2017-04-06 23:54:03 -0700 | [diff] [blame] | 27 | #include <mutex> |
Tao Bao | 3dc14cb | 2016-11-22 11:37:13 -0800 | [diff] [blame] | 28 | |
Tianjie Xu | 4bb11c7 | 2017-04-06 23:54:03 -0700 | [diff] [blame] | 29 | #include <android-base/thread_annotations.h> |
Jed Estep | ff6df89 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 30 | #include "config.h" |
Jed Estep | a7b9a46 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 31 | |
Tianjie Xu | 4bb11c7 | 2017-04-06 23:54:03 -0700 | [diff] [blame] | 32 | static std::mutex filename_mutex; |
| 33 | static std::map<intptr_t, const char*> filename_cache GUARDED_BY(filename_mutex); |
Jed Estep | ff6df89 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 34 | static std::string read_fault_file_name = ""; |
| 35 | static std::string write_fault_file_name = ""; |
| 36 | static std::string fsync_fault_file_name = ""; |
| 37 | |
Chih-Hung Hsieh | 23abfd3 | 2016-07-27 10:19:47 -0700 | [diff] [blame] | 38 | static bool get_hit_file(const char* cached_path, const std::string& ffn) { |
Jed Estep | ff6df89 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 39 | return should_hit_cache() |
| 40 | ? !strncmp(cached_path, OTAIO_CACHE_FNAME, strlen(cached_path)) |
| 41 | : !strncmp(cached_path, ffn.c_str(), strlen(cached_path)); |
| 42 | } |
| 43 | |
| 44 | void ota_set_fault_files() { |
| 45 | if (should_fault_inject(OTAIO_READ)) { |
| 46 | read_fault_file_name = fault_fname(OTAIO_READ); |
| 47 | } |
| 48 | if (should_fault_inject(OTAIO_WRITE)) { |
| 49 | write_fault_file_name = fault_fname(OTAIO_WRITE); |
| 50 | } |
| 51 | if (should_fault_inject(OTAIO_FSYNC)) { |
| 52 | fsync_fault_file_name = fault_fname(OTAIO_FSYNC); |
| 53 | } |
| 54 | } |
Jed Estep | f1fc48c | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 55 | |
Tianjie Xu | 3c62b67 | 2016-02-05 18:25:58 -0800 | [diff] [blame] | 56 | bool have_eio_error = false; |
Jed Estep | 39c1b5e | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 57 | |
Jed Estep | a7b9a46 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 58 | int ota_open(const char* path, int oflags) { |
Jed Estep | a7b9a46 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 59 | // Let the caller handle errors; we do not care if open succeeds or fails |
| 60 | int fd = open(path, oflags); |
Tianjie Xu | 4bb11c7 | 2017-04-06 23:54:03 -0700 | [diff] [blame] | 61 | std::lock_guard<std::mutex> lock(filename_mutex); |
Jed Estep | ff6df89 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 62 | filename_cache[fd] = path; |
Jed Estep | a7b9a46 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 63 | return fd; |
Jed Estep | a7b9a46 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | int ota_open(const char* path, int oflags, mode_t mode) { |
Jed Estep | a7b9a46 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 67 | int fd = open(path, oflags, mode); |
Tianjie Xu | 4bb11c7 | 2017-04-06 23:54:03 -0700 | [diff] [blame] | 68 | std::lock_guard<std::mutex> lock(filename_mutex); |
Jed Estep | ff6df89 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 69 | filename_cache[fd] = path; |
Tianjie Xu | 4bb11c7 | 2017-04-06 23:54:03 -0700 | [diff] [blame] | 70 | return fd; |
| 71 | } |
Jed Estep | a7b9a46 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 72 | |
| 73 | FILE* ota_fopen(const char* path, const char* mode) { |
Jed Estep | a7b9a46 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 74 | FILE* fh = fopen(path, mode); |
Tianjie Xu | 4bb11c7 | 2017-04-06 23:54:03 -0700 | [diff] [blame] | 75 | std::lock_guard<std::mutex> lock(filename_mutex); |
Jed Estep | ff6df89 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 76 | filename_cache[(intptr_t)fh] = path; |
Jed Estep | a7b9a46 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 77 | return fh; |
Jed Estep | a7b9a46 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 78 | } |
| 79 | |
Tao Bao | 358c2ec | 2016-11-28 11:48:43 -0800 | [diff] [blame] | 80 | static int __ota_close(int fd) { |
Jed Estep | ff6df89 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 81 | // descriptors can be reused, so make sure not to leave them in the cache |
Tianjie Xu | 4bb11c7 | 2017-04-06 23:54:03 -0700 | [diff] [blame] | 82 | std::lock_guard<std::mutex> lock(filename_mutex); |
Jed Estep | ff6df89 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 83 | filename_cache.erase(fd); |
Jed Estep | a7b9a46 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 84 | return close(fd); |
| 85 | } |
| 86 | |
Tao Bao | 358c2ec | 2016-11-28 11:48:43 -0800 | [diff] [blame] | 87 | void OtaCloser::Close(int fd) { |
| 88 | __ota_close(fd); |
Tao Bao | 3dc14cb | 2016-11-22 11:37:13 -0800 | [diff] [blame] | 89 | } |
| 90 | |
Tao Bao | 358c2ec | 2016-11-28 11:48:43 -0800 | [diff] [blame] | 91 | int ota_close(unique_fd& fd) { |
| 92 | return __ota_close(fd.release()); |
| 93 | } |
| 94 | |
| 95 | static int __ota_fclose(FILE* fh) { |
Tianjie Xu | 4bb11c7 | 2017-04-06 23:54:03 -0700 | [diff] [blame] | 96 | std::lock_guard<std::mutex> lock(filename_mutex); |
Tao Bao | 358c2ec | 2016-11-28 11:48:43 -0800 | [diff] [blame] | 97 | filename_cache.erase(reinterpret_cast<intptr_t>(fh)); |
Jed Estep | a7b9a46 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 98 | return fclose(fh); |
| 99 | } |
| 100 | |
Mikhail Lappo | b49767c | 2017-03-23 21:44:26 +0100 | [diff] [blame] | 101 | void OtaFcloser::operator()(FILE* f) const { |
Tao Bao | 358c2ec | 2016-11-28 11:48:43 -0800 | [diff] [blame] | 102 | __ota_fclose(f); |
| 103 | }; |
| 104 | |
| 105 | int ota_fclose(unique_file& fh) { |
| 106 | return __ota_fclose(fh.release()); |
Tao Bao | 3dc14cb | 2016-11-22 11:37:13 -0800 | [diff] [blame] | 107 | } |
| 108 | |
Jed Estep | a7b9a46 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 109 | size_t ota_fread(void* ptr, size_t size, size_t nitems, FILE* stream) { |
Jed Estep | ff6df89 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 110 | if (should_fault_inject(OTAIO_READ)) { |
Tianjie Xu | 4bb11c7 | 2017-04-06 23:54:03 -0700 | [diff] [blame] | 111 | std::lock_guard<std::mutex> lock(filename_mutex); |
Jed Estep | ff6df89 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 112 | auto cached = filename_cache.find((intptr_t)stream); |
| 113 | const char* cached_path = cached->second; |
| 114 | if (cached != filename_cache.end() && |
| 115 | get_hit_file(cached_path, read_fault_file_name)) { |
| 116 | read_fault_file_name = ""; |
| 117 | errno = EIO; |
Tianjie Xu | 3c62b67 | 2016-02-05 18:25:58 -0800 | [diff] [blame] | 118 | have_eio_error = true; |
Jed Estep | ff6df89 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 119 | return 0; |
Jed Estep | 39c1b5e | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 120 | } |
Jed Estep | a7b9a46 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 121 | } |
Tianjie Xu | 3c62b67 | 2016-02-05 18:25:58 -0800 | [diff] [blame] | 122 | size_t status = fread(ptr, size, nitems, stream); |
| 123 | // If I/O error occurs, set the retry-update flag. |
| 124 | if (status != nitems && errno == EIO) { |
| 125 | have_eio_error = true; |
| 126 | } |
| 127 | return status; |
Jed Estep | a7b9a46 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | ssize_t ota_read(int fd, void* buf, size_t nbyte) { |
Jed Estep | ff6df89 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 131 | if (should_fault_inject(OTAIO_READ)) { |
Tianjie Xu | 4bb11c7 | 2017-04-06 23:54:03 -0700 | [diff] [blame] | 132 | std::lock_guard<std::mutex> lock(filename_mutex); |
Jed Estep | ff6df89 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 133 | auto cached = filename_cache.find(fd); |
| 134 | const char* cached_path = cached->second; |
| 135 | if (cached != filename_cache.end() |
| 136 | && get_hit_file(cached_path, read_fault_file_name)) { |
| 137 | read_fault_file_name = ""; |
| 138 | errno = EIO; |
Tianjie Xu | 3c62b67 | 2016-02-05 18:25:58 -0800 | [diff] [blame] | 139 | have_eio_error = true; |
Jed Estep | ff6df89 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 140 | return -1; |
Jed Estep | 39c1b5e | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 141 | } |
Jed Estep | a7b9a46 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 142 | } |
Tianjie Xu | 3c62b67 | 2016-02-05 18:25:58 -0800 | [diff] [blame] | 143 | ssize_t status = read(fd, buf, nbyte); |
| 144 | if (status == -1 && errno == EIO) { |
| 145 | have_eio_error = true; |
| 146 | } |
| 147 | return status; |
Jed Estep | a7b9a46 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | size_t ota_fwrite(const void* ptr, size_t size, size_t count, FILE* stream) { |
Jed Estep | ff6df89 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 151 | if (should_fault_inject(OTAIO_WRITE)) { |
Tianjie Xu | 4bb11c7 | 2017-04-06 23:54:03 -0700 | [diff] [blame] | 152 | std::lock_guard<std::mutex> lock(filename_mutex); |
Jed Estep | ff6df89 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 153 | auto cached = filename_cache.find((intptr_t)stream); |
| 154 | const char* cached_path = cached->second; |
| 155 | if (cached != filename_cache.end() && |
| 156 | get_hit_file(cached_path, write_fault_file_name)) { |
| 157 | write_fault_file_name = ""; |
| 158 | errno = EIO; |
Tianjie Xu | 3c62b67 | 2016-02-05 18:25:58 -0800 | [diff] [blame] | 159 | have_eio_error = true; |
Jed Estep | ff6df89 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 160 | return 0; |
Jed Estep | 39c1b5e | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 161 | } |
Jed Estep | a7b9a46 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 162 | } |
Tianjie Xu | 3c62b67 | 2016-02-05 18:25:58 -0800 | [diff] [blame] | 163 | size_t status = fwrite(ptr, size, count, stream); |
| 164 | if (status != count && errno == EIO) { |
| 165 | have_eio_error = true; |
| 166 | } |
| 167 | return status; |
Jed Estep | a7b9a46 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | ssize_t ota_write(int fd, const void* buf, size_t nbyte) { |
Jed Estep | ff6df89 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 171 | if (should_fault_inject(OTAIO_WRITE)) { |
Tianjie Xu | 4bb11c7 | 2017-04-06 23:54:03 -0700 | [diff] [blame] | 172 | std::lock_guard<std::mutex> lock(filename_mutex); |
Jed Estep | ff6df89 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 173 | auto cached = filename_cache.find(fd); |
| 174 | const char* cached_path = cached->second; |
| 175 | if (cached != filename_cache.end() && |
| 176 | get_hit_file(cached_path, write_fault_file_name)) { |
| 177 | write_fault_file_name = ""; |
| 178 | errno = EIO; |
Tianjie Xu | 3c62b67 | 2016-02-05 18:25:58 -0800 | [diff] [blame] | 179 | have_eio_error = true; |
Jed Estep | ff6df89 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 180 | return -1; |
Jed Estep | 39c1b5e | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 181 | } |
Jed Estep | a7b9a46 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 182 | } |
Tianjie Xu | 3c62b67 | 2016-02-05 18:25:58 -0800 | [diff] [blame] | 183 | ssize_t status = write(fd, buf, nbyte); |
| 184 | if (status == -1 && errno == EIO) { |
| 185 | have_eio_error = true; |
| 186 | } |
| 187 | return status; |
Jed Estep | a7b9a46 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | int ota_fsync(int fd) { |
Jed Estep | ff6df89 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 191 | if (should_fault_inject(OTAIO_FSYNC)) { |
Tianjie Xu | 4bb11c7 | 2017-04-06 23:54:03 -0700 | [diff] [blame] | 192 | std::lock_guard<std::mutex> lock(filename_mutex); |
Jed Estep | ff6df89 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 193 | auto cached = filename_cache.find(fd); |
| 194 | const char* cached_path = cached->second; |
| 195 | if (cached != filename_cache.end() && |
| 196 | get_hit_file(cached_path, fsync_fault_file_name)) { |
| 197 | fsync_fault_file_name = ""; |
| 198 | errno = EIO; |
Tianjie Xu | 3c62b67 | 2016-02-05 18:25:58 -0800 | [diff] [blame] | 199 | have_eio_error = true; |
Jed Estep | ff6df89 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 200 | return -1; |
Jed Estep | 39c1b5e | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 201 | } |
Jed Estep | a7b9a46 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 202 | } |
Tianjie Xu | 3c62b67 | 2016-02-05 18:25:58 -0800 | [diff] [blame] | 203 | int status = fsync(fd); |
| 204 | if (status == -1 && errno == EIO) { |
| 205 | have_eio_error = true; |
| 206 | } |
| 207 | return status; |
Jed Estep | a7b9a46 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 208 | } |
Jed Estep | ff6df89 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 209 | |