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 | d33b2f8 | 2017-09-28 21:29:11 -0700 | [diff] [blame] | 17 | #include "otafault/ota_io.h" |
Jed Estep | a7b9a46 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 18 | |
| 19 | #include <errno.h> |
| 20 | #include <fcntl.h> |
Tao Bao | ac27a7a | 2017-09-28 17:43:53 -0700 | [diff] [blame] | 21 | #include <stdint.h> |
Jed Estep | a7b9a46 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 22 | #include <stdio.h> |
| 23 | #include <sys/stat.h> |
Tao Bao | ac27a7a | 2017-09-28 17:43:53 -0700 | [diff] [blame] | 24 | #include <sys/types.h> |
Jed Estep | a7b9a46 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 25 | #include <unistd.h> |
| 26 | |
Tao Bao | 3dc14cb | 2016-11-22 11:37:13 -0800 | [diff] [blame] | 27 | #include <map> |
Tianjie Xu | 4bb11c7 | 2017-04-06 23:54:03 -0700 | [diff] [blame] | 28 | #include <mutex> |
Tao Bao | 3dc14cb | 2016-11-22 11:37:13 -0800 | [diff] [blame] | 29 | |
Tianjie Xu | 4bb11c7 | 2017-04-06 23:54:03 -0700 | [diff] [blame] | 30 | #include <android-base/thread_annotations.h> |
Tao Bao | ac27a7a | 2017-09-28 17:43:53 -0700 | [diff] [blame] | 31 | |
Tao Bao | d33b2f8 | 2017-09-28 21:29:11 -0700 | [diff] [blame] | 32 | #include "otafault/config.h" |
Jed Estep | a7b9a46 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 33 | |
Tianjie Xu | 4bb11c7 | 2017-04-06 23:54:03 -0700 | [diff] [blame] | 34 | static std::mutex filename_mutex; |
| 35 | 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] | 36 | static std::string read_fault_file_name = ""; |
| 37 | static std::string write_fault_file_name = ""; |
| 38 | static std::string fsync_fault_file_name = ""; |
| 39 | |
Chih-Hung Hsieh | 23abfd3 | 2016-07-27 10:19:47 -0700 | [diff] [blame] | 40 | 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] | 41 | return should_hit_cache() |
| 42 | ? !strncmp(cached_path, OTAIO_CACHE_FNAME, strlen(cached_path)) |
| 43 | : !strncmp(cached_path, ffn.c_str(), strlen(cached_path)); |
| 44 | } |
| 45 | |
| 46 | void ota_set_fault_files() { |
| 47 | if (should_fault_inject(OTAIO_READ)) { |
| 48 | read_fault_file_name = fault_fname(OTAIO_READ); |
| 49 | } |
| 50 | if (should_fault_inject(OTAIO_WRITE)) { |
| 51 | write_fault_file_name = fault_fname(OTAIO_WRITE); |
| 52 | } |
| 53 | if (should_fault_inject(OTAIO_FSYNC)) { |
| 54 | fsync_fault_file_name = fault_fname(OTAIO_FSYNC); |
| 55 | } |
| 56 | } |
Jed Estep | f1fc48c | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 57 | |
Tianjie Xu | 3c62b67 | 2016-02-05 18:25:58 -0800 | [diff] [blame] | 58 | bool have_eio_error = false; |
Jed Estep | 39c1b5e | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 59 | |
Jed Estep | a7b9a46 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 60 | int ota_open(const char* path, int oflags) { |
Jed Estep | a7b9a46 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 61 | // Let the caller handle errors; we do not care if open succeeds or fails |
| 62 | int fd = open(path, oflags); |
Tianjie Xu | 4bb11c7 | 2017-04-06 23:54:03 -0700 | [diff] [blame] | 63 | std::lock_guard<std::mutex> lock(filename_mutex); |
Jed Estep | ff6df89 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 64 | filename_cache[fd] = path; |
Jed Estep | a7b9a46 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 65 | return fd; |
Jed Estep | a7b9a46 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | int ota_open(const char* path, int oflags, mode_t mode) { |
Jed Estep | a7b9a46 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 69 | int fd = open(path, oflags, mode); |
Tianjie Xu | 4bb11c7 | 2017-04-06 23:54:03 -0700 | [diff] [blame] | 70 | std::lock_guard<std::mutex> lock(filename_mutex); |
Jed Estep | ff6df89 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 71 | filename_cache[fd] = path; |
Tianjie Xu | 4bb11c7 | 2017-04-06 23:54:03 -0700 | [diff] [blame] | 72 | return fd; |
| 73 | } |
Jed Estep | a7b9a46 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 74 | |
| 75 | FILE* ota_fopen(const char* path, const char* mode) { |
Jed Estep | a7b9a46 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 76 | FILE* fh = fopen(path, mode); |
Tianjie Xu | 4bb11c7 | 2017-04-06 23:54:03 -0700 | [diff] [blame] | 77 | std::lock_guard<std::mutex> lock(filename_mutex); |
Jed Estep | ff6df89 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 78 | filename_cache[(intptr_t)fh] = path; |
Jed Estep | a7b9a46 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 79 | return fh; |
Jed Estep | a7b9a46 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 80 | } |
| 81 | |
Tao Bao | 358c2ec | 2016-11-28 11:48:43 -0800 | [diff] [blame] | 82 | static int __ota_close(int fd) { |
Jed Estep | ff6df89 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 83 | // 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] | 84 | std::lock_guard<std::mutex> lock(filename_mutex); |
Jed Estep | ff6df89 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 85 | filename_cache.erase(fd); |
Jed Estep | a7b9a46 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 86 | return close(fd); |
| 87 | } |
| 88 | |
Tao Bao | 358c2ec | 2016-11-28 11:48:43 -0800 | [diff] [blame] | 89 | void OtaCloser::Close(int fd) { |
| 90 | __ota_close(fd); |
Tao Bao | 3dc14cb | 2016-11-22 11:37:13 -0800 | [diff] [blame] | 91 | } |
| 92 | |
Tao Bao | 358c2ec | 2016-11-28 11:48:43 -0800 | [diff] [blame] | 93 | int ota_close(unique_fd& fd) { |
| 94 | return __ota_close(fd.release()); |
| 95 | } |
| 96 | |
| 97 | static int __ota_fclose(FILE* fh) { |
Tianjie Xu | 4bb11c7 | 2017-04-06 23:54:03 -0700 | [diff] [blame] | 98 | std::lock_guard<std::mutex> lock(filename_mutex); |
Tao Bao | 358c2ec | 2016-11-28 11:48:43 -0800 | [diff] [blame] | 99 | filename_cache.erase(reinterpret_cast<intptr_t>(fh)); |
Jed Estep | a7b9a46 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 100 | return fclose(fh); |
| 101 | } |
| 102 | |
Mikhail Lappo | b49767c | 2017-03-23 21:44:26 +0100 | [diff] [blame] | 103 | void OtaFcloser::operator()(FILE* f) const { |
Tao Bao | 358c2ec | 2016-11-28 11:48:43 -0800 | [diff] [blame] | 104 | __ota_fclose(f); |
| 105 | }; |
| 106 | |
| 107 | int ota_fclose(unique_file& fh) { |
| 108 | return __ota_fclose(fh.release()); |
Tao Bao | 3dc14cb | 2016-11-22 11:37:13 -0800 | [diff] [blame] | 109 | } |
| 110 | |
Jed Estep | a7b9a46 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 111 | 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] | 112 | if (should_fault_inject(OTAIO_READ)) { |
Tianjie Xu | 4bb11c7 | 2017-04-06 23:54:03 -0700 | [diff] [blame] | 113 | std::lock_guard<std::mutex> lock(filename_mutex); |
Jed Estep | ff6df89 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 114 | auto cached = filename_cache.find((intptr_t)stream); |
| 115 | const char* cached_path = cached->second; |
| 116 | if (cached != filename_cache.end() && |
| 117 | get_hit_file(cached_path, read_fault_file_name)) { |
| 118 | read_fault_file_name = ""; |
| 119 | errno = EIO; |
Tianjie Xu | 3c62b67 | 2016-02-05 18:25:58 -0800 | [diff] [blame] | 120 | have_eio_error = true; |
Jed Estep | ff6df89 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 121 | return 0; |
Jed Estep | 39c1b5e | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 122 | } |
Jed Estep | a7b9a46 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 123 | } |
Tianjie Xu | 3c62b67 | 2016-02-05 18:25:58 -0800 | [diff] [blame] | 124 | size_t status = fread(ptr, size, nitems, stream); |
| 125 | // If I/O error occurs, set the retry-update flag. |
| 126 | if (status != nitems && errno == EIO) { |
| 127 | have_eio_error = true; |
| 128 | } |
| 129 | return status; |
Jed Estep | a7b9a46 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | ssize_t ota_read(int fd, void* buf, size_t nbyte) { |
Jed Estep | ff6df89 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 133 | if (should_fault_inject(OTAIO_READ)) { |
Tianjie Xu | 4bb11c7 | 2017-04-06 23:54:03 -0700 | [diff] [blame] | 134 | std::lock_guard<std::mutex> lock(filename_mutex); |
Jed Estep | ff6df89 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 135 | auto cached = filename_cache.find(fd); |
| 136 | const char* cached_path = cached->second; |
| 137 | if (cached != filename_cache.end() |
| 138 | && get_hit_file(cached_path, read_fault_file_name)) { |
| 139 | read_fault_file_name = ""; |
| 140 | errno = EIO; |
Tianjie Xu | 3c62b67 | 2016-02-05 18:25:58 -0800 | [diff] [blame] | 141 | have_eio_error = true; |
Jed Estep | ff6df89 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 142 | return -1; |
Jed Estep | 39c1b5e | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 143 | } |
Jed Estep | a7b9a46 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 144 | } |
Tianjie Xu | 3c62b67 | 2016-02-05 18:25:58 -0800 | [diff] [blame] | 145 | ssize_t status = read(fd, buf, nbyte); |
| 146 | if (status == -1 && errno == EIO) { |
| 147 | have_eio_error = true; |
| 148 | } |
| 149 | return status; |
Jed Estep | a7b9a46 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | 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] | 153 | if (should_fault_inject(OTAIO_WRITE)) { |
Tianjie Xu | 4bb11c7 | 2017-04-06 23:54:03 -0700 | [diff] [blame] | 154 | std::lock_guard<std::mutex> lock(filename_mutex); |
Jed Estep | ff6df89 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 155 | auto cached = filename_cache.find((intptr_t)stream); |
| 156 | const char* cached_path = cached->second; |
| 157 | if (cached != filename_cache.end() && |
| 158 | get_hit_file(cached_path, write_fault_file_name)) { |
| 159 | write_fault_file_name = ""; |
| 160 | errno = EIO; |
Tianjie Xu | 3c62b67 | 2016-02-05 18:25:58 -0800 | [diff] [blame] | 161 | have_eio_error = true; |
Jed Estep | ff6df89 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 162 | return 0; |
Jed Estep | 39c1b5e | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 163 | } |
Jed Estep | a7b9a46 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 164 | } |
Tianjie Xu | 3c62b67 | 2016-02-05 18:25:58 -0800 | [diff] [blame] | 165 | size_t status = fwrite(ptr, size, count, stream); |
| 166 | if (status != count && errno == EIO) { |
| 167 | have_eio_error = true; |
| 168 | } |
| 169 | return status; |
Jed Estep | a7b9a46 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | ssize_t ota_write(int fd, const void* buf, size_t nbyte) { |
Jed Estep | ff6df89 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 173 | if (should_fault_inject(OTAIO_WRITE)) { |
Tianjie Xu | 4bb11c7 | 2017-04-06 23:54:03 -0700 | [diff] [blame] | 174 | std::lock_guard<std::mutex> lock(filename_mutex); |
Jed Estep | ff6df89 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 175 | auto cached = filename_cache.find(fd); |
| 176 | const char* cached_path = cached->second; |
| 177 | if (cached != filename_cache.end() && |
| 178 | get_hit_file(cached_path, write_fault_file_name)) { |
| 179 | write_fault_file_name = ""; |
| 180 | errno = EIO; |
Tianjie Xu | 3c62b67 | 2016-02-05 18:25:58 -0800 | [diff] [blame] | 181 | have_eio_error = true; |
Jed Estep | ff6df89 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 182 | return -1; |
Jed Estep | 39c1b5e | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 183 | } |
Jed Estep | a7b9a46 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 184 | } |
Tianjie Xu | 3c62b67 | 2016-02-05 18:25:58 -0800 | [diff] [blame] | 185 | ssize_t status = write(fd, buf, nbyte); |
| 186 | if (status == -1 && errno == EIO) { |
| 187 | have_eio_error = true; |
| 188 | } |
| 189 | return status; |
Jed Estep | a7b9a46 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | int ota_fsync(int fd) { |
Jed Estep | ff6df89 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 193 | if (should_fault_inject(OTAIO_FSYNC)) { |
Tianjie Xu | 4bb11c7 | 2017-04-06 23:54:03 -0700 | [diff] [blame] | 194 | std::lock_guard<std::mutex> lock(filename_mutex); |
Jed Estep | ff6df89 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 195 | auto cached = filename_cache.find(fd); |
| 196 | const char* cached_path = cached->second; |
| 197 | if (cached != filename_cache.end() && |
| 198 | get_hit_file(cached_path, fsync_fault_file_name)) { |
| 199 | fsync_fault_file_name = ""; |
| 200 | errno = EIO; |
Tianjie Xu | 3c62b67 | 2016-02-05 18:25:58 -0800 | [diff] [blame] | 201 | have_eio_error = true; |
Jed Estep | ff6df89 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 202 | return -1; |
Jed Estep | 39c1b5e | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 203 | } |
Jed Estep | a7b9a46 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 204 | } |
Tianjie Xu | 3c62b67 | 2016-02-05 18:25:58 -0800 | [diff] [blame] | 205 | int status = fsync(fd); |
| 206 | if (status == -1 && errno == EIO) { |
| 207 | have_eio_error = true; |
| 208 | } |
| 209 | return status; |
Jed Estep | a7b9a46 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 210 | } |
Jed Estep | ff6df89 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 211 | |