Jed Estep | f1fc48c | 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 | |
| 17 | #if defined (TARGET_INJECT_FAULTS) |
| 18 | #include <map> |
| 19 | #endif |
| 20 | |
| 21 | #include <errno.h> |
| 22 | #include <fcntl.h> |
| 23 | #include <stdio.h> |
| 24 | #include <sys/stat.h> |
| 25 | #include <unistd.h> |
| 26 | |
| 27 | #include "ota_io.h" |
| 28 | |
| 29 | #if defined (TARGET_INJECT_FAULTS) |
| 30 | static std::map<int, const char*> FilenameCache; |
| 31 | static std::string FaultFileName = |
| 32 | #if defined (TARGET_READ_FAULT) |
| 33 | TARGET_READ_FAULT; |
| 34 | #elif defined (TARGET_WRITE_FAULT) |
| 35 | TARGET_WRITE_FAULT; |
| 36 | #elif defined (TARGET_FSYNC_FAULT) |
| 37 | TARGET_FSYNC_FAULT; |
| 38 | #endif // defined (TARGET_READ_FAULT) |
| 39 | #endif // defined (TARGET_INJECT_FAULTS) |
| 40 | |
Tianjie Xu | fa12b97 | 2016-02-05 18:25:58 -0800 | [diff] [blame] | 41 | bool have_eio_error = false; |
| 42 | |
Jed Estep | f1fc48c | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 43 | int ota_open(const char* path, int oflags) { |
| 44 | #if defined (TARGET_INJECT_FAULTS) |
| 45 | // Let the caller handle errors; we do not care if open succeeds or fails |
| 46 | int fd = open(path, oflags); |
| 47 | FilenameCache[fd] = path; |
| 48 | return fd; |
| 49 | #else |
| 50 | return open(path, oflags); |
| 51 | #endif |
| 52 | } |
| 53 | |
| 54 | int ota_open(const char* path, int oflags, mode_t mode) { |
| 55 | #if defined (TARGET_INJECT_FAULTS) |
| 56 | int fd = open(path, oflags, mode); |
| 57 | FilenameCache[fd] = path; |
| 58 | return fd; |
| 59 | #else |
| 60 | return open(path, oflags, mode); |
| 61 | #endif |
| 62 | } |
| 63 | |
| 64 | FILE* ota_fopen(const char* path, const char* mode) { |
| 65 | #if defined (TARGET_INJECT_FAULTS) |
| 66 | FILE* fh = fopen(path, mode); |
| 67 | FilenameCache[(intptr_t)fh] = path; |
| 68 | return fh; |
| 69 | #else |
| 70 | return fopen(path, mode); |
| 71 | #endif |
| 72 | } |
| 73 | |
| 74 | int ota_close(int fd) { |
| 75 | #if defined (TARGET_INJECT_FAULTS) |
| 76 | // descriptors can be reused, so make sure not to leave them in the cahce |
| 77 | FilenameCache.erase(fd); |
| 78 | #endif |
| 79 | return close(fd); |
| 80 | } |
| 81 | |
| 82 | int ota_fclose(FILE* fh) { |
| 83 | #if defined (TARGET_INJECT_FAULTS) |
| 84 | FilenameCache.erase((intptr_t)fh); |
| 85 | #endif |
| 86 | return fclose(fh); |
| 87 | } |
| 88 | |
| 89 | size_t ota_fread(void* ptr, size_t size, size_t nitems, FILE* stream) { |
| 90 | #if defined (TARGET_READ_FAULT) |
| 91 | if (FilenameCache.find((intptr_t)stream) != FilenameCache.end() |
| 92 | && FilenameCache[(intptr_t)stream] == FaultFileName) { |
| 93 | FaultFileName = ""; |
| 94 | errno = EIO; |
Tianjie Xu | fa12b97 | 2016-02-05 18:25:58 -0800 | [diff] [blame] | 95 | have_eio_error = true; |
Jed Estep | f1fc48c | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 96 | return 0; |
| 97 | } else { |
Tianjie Xu | fa12b97 | 2016-02-05 18:25:58 -0800 | [diff] [blame] | 98 | size_t status = fread(ptr, size, nitems, stream); |
| 99 | // If I/O error occurs, set the retry-update flag. |
| 100 | if (status != nitems && errno == EIO) { |
| 101 | have_eio_error = true; |
| 102 | } |
| 103 | return status; |
Jed Estep | f1fc48c | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 104 | } |
| 105 | #else |
Tianjie Xu | fa12b97 | 2016-02-05 18:25:58 -0800 | [diff] [blame] | 106 | size_t status = fread(ptr, size, nitems, stream); |
| 107 | if (status != nitems && errno == EIO) { |
| 108 | have_eio_error = true; |
| 109 | } |
| 110 | return status; |
Jed Estep | f1fc48c | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 111 | #endif |
| 112 | } |
| 113 | |
| 114 | ssize_t ota_read(int fd, void* buf, size_t nbyte) { |
| 115 | #if defined (TARGET_READ_FAULT) |
| 116 | if (FilenameCache.find(fd) != FilenameCache.end() |
| 117 | && FilenameCache[fd] == FaultFileName) { |
| 118 | FaultFileName = ""; |
| 119 | errno = EIO; |
Tianjie Xu | fa12b97 | 2016-02-05 18:25:58 -0800 | [diff] [blame] | 120 | have_eio_error = true; |
Jed Estep | f1fc48c | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 121 | return -1; |
| 122 | } else { |
Tianjie Xu | fa12b97 | 2016-02-05 18:25:58 -0800 | [diff] [blame] | 123 | ssize_t status = read(fd, buf, nbyte); |
| 124 | if (status == -1 && errno == EIO) { |
| 125 | have_eio_error = true; |
| 126 | } |
| 127 | return status; |
Jed Estep | f1fc48c | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 128 | } |
| 129 | #else |
Tianjie Xu | fa12b97 | 2016-02-05 18:25:58 -0800 | [diff] [blame] | 130 | ssize_t status = read(fd, buf, nbyte); |
| 131 | if (status == -1 && errno == EIO) { |
| 132 | have_eio_error = true; |
| 133 | } |
| 134 | return status; |
Jed Estep | f1fc48c | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 135 | #endif |
| 136 | } |
| 137 | |
| 138 | size_t ota_fwrite(const void* ptr, size_t size, size_t count, FILE* stream) { |
| 139 | #if defined (TARGET_WRITE_FAULT) |
| 140 | if (FilenameCache.find((intptr_t)stream) != FilenameCache.end() |
| 141 | && FilenameCache[(intptr_t)stream] == FaultFileName) { |
| 142 | FaultFileName = ""; |
| 143 | errno = EIO; |
Tianjie Xu | fa12b97 | 2016-02-05 18:25:58 -0800 | [diff] [blame] | 144 | have_eio_error = true; |
Jed Estep | f1fc48c | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 145 | return 0; |
| 146 | } else { |
Tianjie Xu | fa12b97 | 2016-02-05 18:25:58 -0800 | [diff] [blame] | 147 | size_t status = fwrite(ptr, size, count, stream); |
| 148 | if (status != count && errno == EIO) { |
| 149 | have_eio_error = true; |
| 150 | } |
| 151 | return status; |
Jed Estep | f1fc48c | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 152 | } |
| 153 | #else |
Tianjie Xu | fa12b97 | 2016-02-05 18:25:58 -0800 | [diff] [blame] | 154 | size_t status = fwrite(ptr, size, count, stream); |
| 155 | if (status != count && errno == EIO) { |
| 156 | have_eio_error = true; |
| 157 | } |
| 158 | return status; |
Jed Estep | f1fc48c | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 159 | #endif |
| 160 | } |
| 161 | |
| 162 | ssize_t ota_write(int fd, const void* buf, size_t nbyte) { |
| 163 | #if defined (TARGET_WRITE_FAULT) |
| 164 | if (FilenameCache.find(fd) != FilenameCache.end() |
| 165 | && FilenameCache[fd] == FaultFileName) { |
| 166 | FaultFileName = ""; |
| 167 | errno = EIO; |
Tianjie Xu | fa12b97 | 2016-02-05 18:25:58 -0800 | [diff] [blame] | 168 | have_eio_error = true; |
Jed Estep | f1fc48c | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 169 | return -1; |
| 170 | } else { |
Tianjie Xu | fa12b97 | 2016-02-05 18:25:58 -0800 | [diff] [blame] | 171 | ssize_t status = write(fd, buf, nbyte); |
| 172 | if (status == -1 && errno == EIO) { |
| 173 | have_eio_error = true; |
| 174 | } |
| 175 | return status; |
Jed Estep | f1fc48c | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 176 | } |
| 177 | #else |
Tianjie Xu | fa12b97 | 2016-02-05 18:25:58 -0800 | [diff] [blame] | 178 | ssize_t status = write(fd, buf, nbyte); |
| 179 | if (status == -1 && errno == EIO) { |
| 180 | have_eio_error = true; |
| 181 | } |
| 182 | return status; |
Jed Estep | f1fc48c | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 183 | #endif |
| 184 | } |
| 185 | |
| 186 | int ota_fsync(int fd) { |
| 187 | #if defined (TARGET_FSYNC_FAULT) |
| 188 | if (FilenameCache.find(fd) != FilenameCache.end() |
| 189 | && FilenameCache[fd] == FaultFileName) { |
| 190 | FaultFileName = ""; |
| 191 | errno = EIO; |
Tianjie Xu | fa12b97 | 2016-02-05 18:25:58 -0800 | [diff] [blame] | 192 | have_eio_error = true; |
Greg Kaiser | f4300bc | 2016-03-14 12:15:24 -0700 | [diff] [blame^] | 193 | return -1; |
Jed Estep | f1fc48c | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 194 | } else { |
Tianjie Xu | fa12b97 | 2016-02-05 18:25:58 -0800 | [diff] [blame] | 195 | int status = fsync(fd); |
| 196 | if (status == -1 && errno == EIO) { |
| 197 | have_eio_error = true; |
| 198 | } |
| 199 | return status; |
Jed Estep | f1fc48c | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 200 | } |
| 201 | #else |
Tianjie Xu | fa12b97 | 2016-02-05 18:25:58 -0800 | [diff] [blame] | 202 | int status = fsync(fd); |
| 203 | if (status == -1 && errno == EIO) { |
| 204 | have_eio_error = true; |
| 205 | } |
| 206 | return status; |
Jed Estep | f1fc48c | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 207 | #endif |
| 208 | } |