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