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 = ""; |
| 32 | |
| 33 | static bool get_hit_file(const char* cached_path, std::string ffn) { |
| 34 | return should_hit_cache() |
| 35 | ? !strncmp(cached_path, OTAIO_CACHE_FNAME, strlen(cached_path)) |
| 36 | : !strncmp(cached_path, ffn.c_str(), strlen(cached_path)); |
| 37 | } |
| 38 | |
| 39 | void ota_set_fault_files() { |
| 40 | if (should_fault_inject(OTAIO_READ)) { |
| 41 | read_fault_file_name = fault_fname(OTAIO_READ); |
| 42 | } |
| 43 | if (should_fault_inject(OTAIO_WRITE)) { |
| 44 | write_fault_file_name = fault_fname(OTAIO_WRITE); |
| 45 | } |
| 46 | if (should_fault_inject(OTAIO_FSYNC)) { |
| 47 | fsync_fault_file_name = fault_fname(OTAIO_FSYNC); |
| 48 | } |
| 49 | } |
Jed Estep | a7b9a46 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 50 | |
| 51 | int ota_open(const char* path, int oflags) { |
Jed Estep | a7b9a46 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 52 | // Let the caller handle errors; we do not care if open succeeds or fails |
| 53 | int fd = open(path, oflags); |
Jed Estep | 39c1b5e | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 54 | filename_cache[fd] = path; |
Jed Estep | a7b9a46 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 55 | return fd; |
Jed Estep | a7b9a46 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | int ota_open(const char* path, int oflags, mode_t mode) { |
Jed Estep | a7b9a46 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 59 | int fd = open(path, oflags, mode); |
Jed Estep | 39c1b5e | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 60 | filename_cache[fd] = path; |
| 61 | return fd; } |
Jed Estep | a7b9a46 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 62 | |
| 63 | FILE* ota_fopen(const char* path, const char* mode) { |
Jed Estep | a7b9a46 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 64 | FILE* fh = fopen(path, mode); |
Jed Estep | 39c1b5e | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 65 | filename_cache[(intptr_t)fh] = path; |
Jed Estep | a7b9a46 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 66 | return fh; |
Jed Estep | a7b9a46 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | int ota_close(int fd) { |
Jed Estep | 39c1b5e | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 70 | // descriptors can be reused, so make sure not to leave them in the cache |
| 71 | filename_cache.erase(fd); |
Jed Estep | a7b9a46 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 72 | return close(fd); |
| 73 | } |
| 74 | |
| 75 | int ota_fclose(FILE* fh) { |
Jed Estep | 39c1b5e | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 76 | filename_cache.erase((intptr_t)fh); |
Jed Estep | a7b9a46 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 77 | return fclose(fh); |
| 78 | } |
| 79 | |
| 80 | 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] | 81 | if (should_fault_inject(OTAIO_READ)) { |
| 82 | auto cached = filename_cache.find((intptr_t)stream); |
| 83 | const char* cached_path = cached->second; |
| 84 | if (cached != filename_cache.end() && |
| 85 | get_hit_file(cached_path, read_fault_file_name)) { |
| 86 | read_fault_file_name = ""; |
| 87 | errno = EIO; |
| 88 | return 0; |
| 89 | } |
Jed Estep | a7b9a46 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 90 | } |
Jed Estep | a7b9a46 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 91 | return fread(ptr, size, nitems, stream); |
Jed Estep | a7b9a46 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | ssize_t ota_read(int fd, void* buf, size_t nbyte) { |
Jed Estep | 39c1b5e | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 95 | if (should_fault_inject(OTAIO_READ)) { |
| 96 | auto cached = filename_cache.find(fd); |
| 97 | const char* cached_path = cached->second; |
| 98 | if (cached != filename_cache.end() |
| 99 | && get_hit_file(cached_path, read_fault_file_name)) { |
| 100 | read_fault_file_name = ""; |
| 101 | errno = EIO; |
| 102 | return -1; |
| 103 | } |
Jed Estep | a7b9a46 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 104 | } |
Jed Estep | a7b9a46 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 105 | return read(fd, buf, nbyte); |
Jed Estep | a7b9a46 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | 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] | 109 | if (should_fault_inject(OTAIO_WRITE)) { |
| 110 | auto cached = filename_cache.find((intptr_t)stream); |
| 111 | const char* cached_path = cached->second; |
| 112 | if (cached != filename_cache.end() && |
| 113 | get_hit_file(cached_path, write_fault_file_name)) { |
| 114 | write_fault_file_name = ""; |
| 115 | errno = EIO; |
| 116 | return 0; |
| 117 | } |
Jed Estep | a7b9a46 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 118 | } |
Jed Estep | a7b9a46 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 119 | return fwrite(ptr, size, count, stream); |
Jed Estep | a7b9a46 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | ssize_t ota_write(int fd, const void* buf, size_t nbyte) { |
Jed Estep | 39c1b5e | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 123 | if (should_fault_inject(OTAIO_WRITE)) { |
| 124 | auto cached = filename_cache.find(fd); |
| 125 | const char* cached_path = cached->second; |
| 126 | if (cached != filename_cache.end() && |
| 127 | get_hit_file(cached_path, write_fault_file_name)) { |
| 128 | write_fault_file_name = ""; |
| 129 | errno = EIO; |
| 130 | return -1; |
| 131 | } |
Jed Estep | a7b9a46 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 132 | } |
Jed Estep | a7b9a46 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 133 | return write(fd, buf, nbyte); |
Jed Estep | a7b9a46 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | int ota_fsync(int fd) { |
Jed Estep | 39c1b5e | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 137 | if (should_fault_inject(OTAIO_FSYNC)) { |
| 138 | auto cached = filename_cache.find(fd); |
| 139 | const char* cached_path = cached->second; |
| 140 | if (cached != filename_cache.end() && |
| 141 | get_hit_file(cached_path, fsync_fault_file_name)) { |
| 142 | fsync_fault_file_name = ""; |
| 143 | errno = EIO; |
| 144 | return -1; |
| 145 | } |
Jed Estep | a7b9a46 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 146 | } |
Jed Estep | a7b9a46 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 147 | return fsync(fd); |
Jed Estep | a7b9a46 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 148 | } |
Jed Estep | 39c1b5e | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 149 | |