blob: 3a89bb5dd1a7da7ce85f9c87337491f43f61c404 [file] [log] [blame]
Jed Estepa7b9a462015-12-15 16:04:53 -08001/*
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 Bao3dc14cb2016-11-22 11:37:13 -080017#include "ota_io.h"
Jed Estepa7b9a462015-12-15 16:04:53 -080018
19#include <errno.h>
20#include <fcntl.h>
21#include <stdio.h>
22#include <sys/stat.h>
23#include <unistd.h>
24
Tao Bao3dc14cb2016-11-22 11:37:13 -080025#include <map>
26#include <memory>
27
Jed Estepff6df892015-12-15 16:04:53 -080028#include "config.h"
Jed Estepa7b9a462015-12-15 16:04:53 -080029
Jed Estepff6df892015-12-15 16:04:53 -080030static std::map<intptr_t, const char*> filename_cache;
31static std::string read_fault_file_name = "";
32static std::string write_fault_file_name = "";
33static std::string fsync_fault_file_name = "";
34
Chih-Hung Hsieh23abfd32016-07-27 10:19:47 -070035static bool get_hit_file(const char* cached_path, const std::string& ffn) {
Jed Estepff6df892015-12-15 16:04:53 -080036 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
41void 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 Estepf1fc48c2015-12-15 16:04:53 -080052
Tianjie Xu3c62b672016-02-05 18:25:58 -080053bool have_eio_error = false;
Jed Estep39c1b5e2015-12-15 16:04:53 -080054
Jed Estepa7b9a462015-12-15 16:04:53 -080055int ota_open(const char* path, int oflags) {
Jed Estepa7b9a462015-12-15 16:04:53 -080056 // Let the caller handle errors; we do not care if open succeeds or fails
57 int fd = open(path, oflags);
Jed Estepff6df892015-12-15 16:04:53 -080058 filename_cache[fd] = path;
Jed Estepa7b9a462015-12-15 16:04:53 -080059 return fd;
Jed Estepa7b9a462015-12-15 16:04:53 -080060}
61
62int ota_open(const char* path, int oflags, mode_t mode) {
Jed Estepa7b9a462015-12-15 16:04:53 -080063 int fd = open(path, oflags, mode);
Jed Estepff6df892015-12-15 16:04:53 -080064 filename_cache[fd] = path;
65 return fd; }
Jed Estepa7b9a462015-12-15 16:04:53 -080066
67FILE* ota_fopen(const char* path, const char* mode) {
Jed Estepa7b9a462015-12-15 16:04:53 -080068 FILE* fh = fopen(path, mode);
Jed Estepff6df892015-12-15 16:04:53 -080069 filename_cache[(intptr_t)fh] = path;
Jed Estepa7b9a462015-12-15 16:04:53 -080070 return fh;
Jed Estepa7b9a462015-12-15 16:04:53 -080071}
72
Tao Bao358c2ec2016-11-28 11:48:43 -080073static int __ota_close(int fd) {
Jed Estepff6df892015-12-15 16:04:53 -080074 // descriptors can be reused, so make sure not to leave them in the cache
75 filename_cache.erase(fd);
Jed Estepa7b9a462015-12-15 16:04:53 -080076 return close(fd);
77}
78
Tao Bao358c2ec2016-11-28 11:48:43 -080079void OtaCloser::Close(int fd) {
80 __ota_close(fd);
Tao Bao3dc14cb2016-11-22 11:37:13 -080081}
82
Tao Bao358c2ec2016-11-28 11:48:43 -080083int ota_close(unique_fd& fd) {
84 return __ota_close(fd.release());
85}
86
87static int __ota_fclose(FILE* fh) {
88 filename_cache.erase(reinterpret_cast<intptr_t>(fh));
Jed Estepa7b9a462015-12-15 16:04:53 -080089 return fclose(fh);
90}
91
Mikhail Lappob49767c2017-03-23 21:44:26 +010092void OtaFcloser::operator()(FILE* f) const {
Tao Bao358c2ec2016-11-28 11:48:43 -080093 __ota_fclose(f);
94};
95
96int ota_fclose(unique_file& fh) {
97 return __ota_fclose(fh.release());
Tao Bao3dc14cb2016-11-22 11:37:13 -080098}
99
Jed Estepa7b9a462015-12-15 16:04:53 -0800100size_t ota_fread(void* ptr, size_t size, size_t nitems, FILE* stream) {
Jed Estepff6df892015-12-15 16:04:53 -0800101 if (should_fault_inject(OTAIO_READ)) {
102 auto cached = filename_cache.find((intptr_t)stream);
103 const char* cached_path = cached->second;
104 if (cached != filename_cache.end() &&
105 get_hit_file(cached_path, read_fault_file_name)) {
106 read_fault_file_name = "";
107 errno = EIO;
Tianjie Xu3c62b672016-02-05 18:25:58 -0800108 have_eio_error = true;
Jed Estepff6df892015-12-15 16:04:53 -0800109 return 0;
Jed Estep39c1b5e2015-12-15 16:04:53 -0800110 }
Jed Estepa7b9a462015-12-15 16:04:53 -0800111 }
Tianjie Xu3c62b672016-02-05 18:25:58 -0800112 size_t status = fread(ptr, size, nitems, stream);
113 // If I/O error occurs, set the retry-update flag.
114 if (status != nitems && errno == EIO) {
115 have_eio_error = true;
116 }
117 return status;
Jed Estepa7b9a462015-12-15 16:04:53 -0800118}
119
120ssize_t ota_read(int fd, void* buf, size_t nbyte) {
Jed Estepff6df892015-12-15 16:04:53 -0800121 if (should_fault_inject(OTAIO_READ)) {
122 auto cached = filename_cache.find(fd);
123 const char* cached_path = cached->second;
124 if (cached != filename_cache.end()
125 && get_hit_file(cached_path, read_fault_file_name)) {
126 read_fault_file_name = "";
127 errno = EIO;
Tianjie Xu3c62b672016-02-05 18:25:58 -0800128 have_eio_error = true;
Jed Estepff6df892015-12-15 16:04:53 -0800129 return -1;
Jed Estep39c1b5e2015-12-15 16:04:53 -0800130 }
Jed Estepa7b9a462015-12-15 16:04:53 -0800131 }
Tianjie Xu3c62b672016-02-05 18:25:58 -0800132 ssize_t status = read(fd, buf, nbyte);
133 if (status == -1 && errno == EIO) {
134 have_eio_error = true;
135 }
136 return status;
Jed Estepa7b9a462015-12-15 16:04:53 -0800137}
138
139size_t ota_fwrite(const void* ptr, size_t size, size_t count, FILE* stream) {
Jed Estepff6df892015-12-15 16:04:53 -0800140 if (should_fault_inject(OTAIO_WRITE)) {
141 auto cached = filename_cache.find((intptr_t)stream);
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 Xu3c62b672016-02-05 18:25:58 -0800147 have_eio_error = true;
Jed Estepff6df892015-12-15 16:04:53 -0800148 return 0;
Jed Estep39c1b5e2015-12-15 16:04:53 -0800149 }
Jed Estepa7b9a462015-12-15 16:04:53 -0800150 }
Tianjie Xu3c62b672016-02-05 18:25:58 -0800151 size_t status = fwrite(ptr, size, count, stream);
152 if (status != count && errno == EIO) {
153 have_eio_error = true;
154 }
155 return status;
Jed Estepa7b9a462015-12-15 16:04:53 -0800156}
157
158ssize_t ota_write(int fd, const void* buf, size_t nbyte) {
Jed Estepff6df892015-12-15 16:04:53 -0800159 if (should_fault_inject(OTAIO_WRITE)) {
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, write_fault_file_name)) {
164 write_fault_file_name = "";
165 errno = EIO;
Tianjie Xu3c62b672016-02-05 18:25:58 -0800166 have_eio_error = true;
Jed Estepff6df892015-12-15 16:04:53 -0800167 return -1;
Jed Estep39c1b5e2015-12-15 16:04:53 -0800168 }
Jed Estepa7b9a462015-12-15 16:04:53 -0800169 }
Tianjie Xu3c62b672016-02-05 18:25:58 -0800170 ssize_t status = write(fd, buf, nbyte);
171 if (status == -1 && errno == EIO) {
172 have_eio_error = true;
173 }
174 return status;
Jed Estepa7b9a462015-12-15 16:04:53 -0800175}
176
177int ota_fsync(int fd) {
Jed Estepff6df892015-12-15 16:04:53 -0800178 if (should_fault_inject(OTAIO_FSYNC)) {
179 auto cached = filename_cache.find(fd);
180 const char* cached_path = cached->second;
181 if (cached != filename_cache.end() &&
182 get_hit_file(cached_path, fsync_fault_file_name)) {
183 fsync_fault_file_name = "";
184 errno = EIO;
Tianjie Xu3c62b672016-02-05 18:25:58 -0800185 have_eio_error = true;
Jed Estepff6df892015-12-15 16:04:53 -0800186 return -1;
Jed Estep39c1b5e2015-12-15 16:04:53 -0800187 }
Jed Estepa7b9a462015-12-15 16:04:53 -0800188 }
Tianjie Xu3c62b672016-02-05 18:25:58 -0800189 int status = fsync(fd);
190 if (status == -1 && errno == EIO) {
191 have_eio_error = true;
192 }
193 return status;
Jed Estepa7b9a462015-12-15 16:04:53 -0800194}
Jed Estepff6df892015-12-15 16:04:53 -0800195