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