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> |
| 27 | |
Jed Estep | ff6df89 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 28 | #include "config.h" |
Jed Estep | a7b9a46 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 29 | |
Jed Estep | ff6df89 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 30 | static std::map<intptr_t, const char*> filename_cache; |
| 31 | static std::string read_fault_file_name = ""; |
| 32 | static std::string write_fault_file_name = ""; |
| 33 | static std::string fsync_fault_file_name = ""; |
| 34 | |
Chih-Hung Hsieh | 23abfd3 | 2016-07-27 10:19:47 -0700 | [diff] [blame] | 35 | 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] | 36 | return should_hit_cache() |
| 37 | ? !strncmp(cached_path, OTAIO_CACHE_FNAME, strlen(cached_path)) |
| 38 | : !strncmp(cached_path, ffn.c_str(), strlen(cached_path)); |
| 39 | } |
| 40 | |
| 41 | void ota_set_fault_files() { |
| 42 | if (should_fault_inject(OTAIO_READ)) { |
| 43 | read_fault_file_name = fault_fname(OTAIO_READ); |
| 44 | } |
| 45 | if (should_fault_inject(OTAIO_WRITE)) { |
| 46 | write_fault_file_name = fault_fname(OTAIO_WRITE); |
| 47 | } |
| 48 | if (should_fault_inject(OTAIO_FSYNC)) { |
| 49 | fsync_fault_file_name = fault_fname(OTAIO_FSYNC); |
| 50 | } |
| 51 | } |
Jed Estep | f1fc48c | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 52 | |
Tianjie Xu | 3c62b67 | 2016-02-05 18:25:58 -0800 | [diff] [blame] | 53 | bool have_eio_error = false; |
Jed Estep | 39c1b5e | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 54 | |
Jed Estep | a7b9a46 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 55 | int ota_open(const char* path, int oflags) { |
Jed Estep | a7b9a46 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 56 | // Let the caller handle errors; we do not care if open succeeds or fails |
| 57 | int fd = open(path, oflags); |
Jed Estep | ff6df89 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 58 | filename_cache[fd] = path; |
Jed Estep | a7b9a46 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 59 | return fd; |
Jed Estep | a7b9a46 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | int ota_open(const char* path, int oflags, mode_t mode) { |
Jed Estep | a7b9a46 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 63 | int fd = open(path, oflags, mode); |
Jed Estep | ff6df89 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 64 | filename_cache[fd] = path; |
| 65 | return fd; } |
Jed Estep | a7b9a46 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 66 | |
| 67 | FILE* ota_fopen(const char* path, const char* mode) { |
Jed Estep | a7b9a46 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 68 | FILE* fh = fopen(path, mode); |
Jed Estep | ff6df89 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 69 | filename_cache[(intptr_t)fh] = path; |
Jed Estep | a7b9a46 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 70 | return fh; |
Jed Estep | a7b9a46 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | int ota_close(int fd) { |
Jed Estep | ff6df89 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 74 | // descriptors can be reused, so make sure not to leave them in the cache |
| 75 | filename_cache.erase(fd); |
Jed Estep | a7b9a46 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 76 | return close(fd); |
| 77 | } |
| 78 | |
Tao Bao | 3dc14cb | 2016-11-22 11:37:13 -0800 | [diff] [blame] | 79 | int ota_close(unique_fd& fd) { |
| 80 | return ota_close(fd.release()); |
| 81 | } |
| 82 | |
Jed Estep | a7b9a46 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 83 | int ota_fclose(FILE* fh) { |
Jed Estep | ff6df89 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 84 | filename_cache.erase((intptr_t)fh); |
Jed Estep | a7b9a46 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 85 | return fclose(fh); |
| 86 | } |
| 87 | |
Tao Bao | 3dc14cb | 2016-11-22 11:37:13 -0800 | [diff] [blame] | 88 | int ota_fclose(std::unique_ptr<FILE, int (*)(FILE*)>& fh) { |
| 89 | return ota_fclose(fh.release()); |
| 90 | } |
| 91 | |
Jed Estep | a7b9a46 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 92 | 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] | 93 | if (should_fault_inject(OTAIO_READ)) { |
| 94 | auto cached = filename_cache.find((intptr_t)stream); |
| 95 | const char* cached_path = cached->second; |
| 96 | if (cached != filename_cache.end() && |
| 97 | get_hit_file(cached_path, read_fault_file_name)) { |
| 98 | read_fault_file_name = ""; |
| 99 | errno = EIO; |
Tianjie Xu | 3c62b67 | 2016-02-05 18:25:58 -0800 | [diff] [blame] | 100 | have_eio_error = true; |
Jed Estep | ff6df89 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 101 | return 0; |
Jed Estep | 39c1b5e | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 102 | } |
Jed Estep | a7b9a46 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 103 | } |
Tianjie Xu | 3c62b67 | 2016-02-05 18:25:58 -0800 | [diff] [blame] | 104 | size_t status = fread(ptr, size, nitems, stream); |
| 105 | // If I/O error occurs, set the retry-update flag. |
| 106 | if (status != nitems && errno == EIO) { |
| 107 | have_eio_error = true; |
| 108 | } |
| 109 | return status; |
Jed Estep | a7b9a46 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | ssize_t ota_read(int fd, void* buf, size_t nbyte) { |
Jed Estep | ff6df89 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 113 | if (should_fault_inject(OTAIO_READ)) { |
| 114 | auto cached = filename_cache.find(fd); |
| 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 -1; |
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 | ssize_t status = read(fd, buf, nbyte); |
| 125 | if (status == -1 && errno == EIO) { |
| 126 | have_eio_error = true; |
| 127 | } |
| 128 | return status; |
Jed Estep | a7b9a46 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | 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] | 132 | if (should_fault_inject(OTAIO_WRITE)) { |
| 133 | auto cached = filename_cache.find((intptr_t)stream); |
| 134 | const char* cached_path = cached->second; |
| 135 | if (cached != filename_cache.end() && |
| 136 | get_hit_file(cached_path, write_fault_file_name)) { |
| 137 | write_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 0; |
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 | size_t status = fwrite(ptr, size, count, stream); |
| 144 | if (status != count && 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 | ssize_t ota_write(int fd, const void* buf, size_t nbyte) { |
Jed Estep | ff6df89 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 151 | if (should_fault_inject(OTAIO_WRITE)) { |
| 152 | auto cached = filename_cache.find(fd); |
| 153 | const char* cached_path = cached->second; |
| 154 | if (cached != filename_cache.end() && |
| 155 | get_hit_file(cached_path, write_fault_file_name)) { |
| 156 | write_fault_file_name = ""; |
| 157 | errno = EIO; |
Tianjie Xu | 3c62b67 | 2016-02-05 18:25:58 -0800 | [diff] [blame] | 158 | have_eio_error = true; |
Jed Estep | ff6df89 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 159 | return -1; |
Jed Estep | 39c1b5e | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 160 | } |
Jed Estep | a7b9a46 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 161 | } |
Tianjie Xu | 3c62b67 | 2016-02-05 18:25:58 -0800 | [diff] [blame] | 162 | ssize_t status = write(fd, buf, nbyte); |
| 163 | if (status == -1 && errno == EIO) { |
| 164 | have_eio_error = true; |
| 165 | } |
| 166 | return status; |
Jed Estep | a7b9a46 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | int ota_fsync(int fd) { |
Jed Estep | ff6df89 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 170 | if (should_fault_inject(OTAIO_FSYNC)) { |
| 171 | auto cached = filename_cache.find(fd); |
| 172 | const char* cached_path = cached->second; |
| 173 | if (cached != filename_cache.end() && |
| 174 | get_hit_file(cached_path, fsync_fault_file_name)) { |
| 175 | fsync_fault_file_name = ""; |
| 176 | errno = EIO; |
Tianjie Xu | 3c62b67 | 2016-02-05 18:25:58 -0800 | [diff] [blame] | 177 | have_eio_error = true; |
Jed Estep | ff6df89 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 178 | return -1; |
Jed Estep | 39c1b5e | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 179 | } |
Jed Estep | a7b9a46 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 180 | } |
Tianjie Xu | 3c62b67 | 2016-02-05 18:25:58 -0800 | [diff] [blame] | 181 | int status = fsync(fd); |
| 182 | if (status == -1 && errno == EIO) { |
| 183 | have_eio_error = true; |
| 184 | } |
| 185 | return status; |
Jed Estep | a7b9a46 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 186 | } |
Jed Estep | ff6df89 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 187 | |