blob: faae5275d418133f529b9a30c8faea53c8fc283a [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>
Tianjie Xu4bb11c72017-04-06 23:54:03 -070027#include <mutex>
Tao Bao3dc14cb2016-11-22 11:37:13 -080028
Tianjie Xu4bb11c72017-04-06 23:54:03 -070029#include <android-base/thread_annotations.h>
Jed Estepff6df892015-12-15 16:04:53 -080030#include "config.h"
Jed Estepa7b9a462015-12-15 16:04:53 -080031
Tianjie Xu4bb11c72017-04-06 23:54:03 -070032static std::mutex filename_mutex;
33static std::map<intptr_t, const char*> filename_cache GUARDED_BY(filename_mutex);
Jed Estepff6df892015-12-15 16:04:53 -080034static std::string read_fault_file_name = "";
35static std::string write_fault_file_name = "";
36static std::string fsync_fault_file_name = "";
37
Chih-Hung Hsieh23abfd32016-07-27 10:19:47 -070038static bool get_hit_file(const char* cached_path, const std::string& ffn) {
Jed Estepff6df892015-12-15 16:04:53 -080039 return should_hit_cache()
40 ? !strncmp(cached_path, OTAIO_CACHE_FNAME, strlen(cached_path))
41 : !strncmp(cached_path, ffn.c_str(), strlen(cached_path));
42}
43
44void ota_set_fault_files() {
45 if (should_fault_inject(OTAIO_READ)) {
46 read_fault_file_name = fault_fname(OTAIO_READ);
47 }
48 if (should_fault_inject(OTAIO_WRITE)) {
49 write_fault_file_name = fault_fname(OTAIO_WRITE);
50 }
51 if (should_fault_inject(OTAIO_FSYNC)) {
52 fsync_fault_file_name = fault_fname(OTAIO_FSYNC);
53 }
54}
Jed Estepf1fc48c2015-12-15 16:04:53 -080055
Tianjie Xu3c62b672016-02-05 18:25:58 -080056bool have_eio_error = false;
Jed Estep39c1b5e2015-12-15 16:04:53 -080057
Jed Estepa7b9a462015-12-15 16:04:53 -080058int ota_open(const char* path, int oflags) {
Jed Estepa7b9a462015-12-15 16:04:53 -080059 // Let the caller handle errors; we do not care if open succeeds or fails
60 int fd = open(path, oflags);
Tianjie Xu4bb11c72017-04-06 23:54:03 -070061 std::lock_guard<std::mutex> lock(filename_mutex);
Jed Estepff6df892015-12-15 16:04:53 -080062 filename_cache[fd] = path;
Jed Estepa7b9a462015-12-15 16:04:53 -080063 return fd;
Jed Estepa7b9a462015-12-15 16:04:53 -080064}
65
66int ota_open(const char* path, int oflags, mode_t mode) {
Jed Estepa7b9a462015-12-15 16:04:53 -080067 int fd = open(path, oflags, mode);
Tianjie Xu4bb11c72017-04-06 23:54:03 -070068 std::lock_guard<std::mutex> lock(filename_mutex);
Jed Estepff6df892015-12-15 16:04:53 -080069 filename_cache[fd] = path;
Tianjie Xu4bb11c72017-04-06 23:54:03 -070070 return fd;
71}
Jed Estepa7b9a462015-12-15 16:04:53 -080072
73FILE* ota_fopen(const char* path, const char* mode) {
Jed Estepa7b9a462015-12-15 16:04:53 -080074 FILE* fh = fopen(path, mode);
Tianjie Xu4bb11c72017-04-06 23:54:03 -070075 std::lock_guard<std::mutex> lock(filename_mutex);
Jed Estepff6df892015-12-15 16:04:53 -080076 filename_cache[(intptr_t)fh] = path;
Jed Estepa7b9a462015-12-15 16:04:53 -080077 return fh;
Jed Estepa7b9a462015-12-15 16:04:53 -080078}
79
Tao Bao358c2ec2016-11-28 11:48:43 -080080static int __ota_close(int fd) {
Jed Estepff6df892015-12-15 16:04:53 -080081 // descriptors can be reused, so make sure not to leave them in the cache
Tianjie Xu4bb11c72017-04-06 23:54:03 -070082 std::lock_guard<std::mutex> lock(filename_mutex);
Jed Estepff6df892015-12-15 16:04:53 -080083 filename_cache.erase(fd);
Jed Estepa7b9a462015-12-15 16:04:53 -080084 return close(fd);
85}
86
Tao Bao358c2ec2016-11-28 11:48:43 -080087void OtaCloser::Close(int fd) {
88 __ota_close(fd);
Tao Bao3dc14cb2016-11-22 11:37:13 -080089}
90
Tao Bao358c2ec2016-11-28 11:48:43 -080091int ota_close(unique_fd& fd) {
92 return __ota_close(fd.release());
93}
94
95static int __ota_fclose(FILE* fh) {
Tianjie Xu4bb11c72017-04-06 23:54:03 -070096 std::lock_guard<std::mutex> lock(filename_mutex);
Tao Bao358c2ec2016-11-28 11:48:43 -080097 filename_cache.erase(reinterpret_cast<intptr_t>(fh));
Jed Estepa7b9a462015-12-15 16:04:53 -080098 return fclose(fh);
99}
100
Mikhail Lappob49767c2017-03-23 21:44:26 +0100101void OtaFcloser::operator()(FILE* f) const {
Tao Bao358c2ec2016-11-28 11:48:43 -0800102 __ota_fclose(f);
103};
104
105int ota_fclose(unique_file& fh) {
106 return __ota_fclose(fh.release());
Tao Bao3dc14cb2016-11-22 11:37:13 -0800107}
108
Jed Estepa7b9a462015-12-15 16:04:53 -0800109size_t ota_fread(void* ptr, size_t size, size_t nitems, FILE* stream) {
Jed Estepff6df892015-12-15 16:04:53 -0800110 if (should_fault_inject(OTAIO_READ)) {
Tianjie Xu4bb11c72017-04-06 23:54:03 -0700111 std::lock_guard<std::mutex> lock(filename_mutex);
Jed Estepff6df892015-12-15 16:04:53 -0800112 auto cached = filename_cache.find((intptr_t)stream);
113 const char* cached_path = cached->second;
114 if (cached != filename_cache.end() &&
115 get_hit_file(cached_path, read_fault_file_name)) {
116 read_fault_file_name = "";
117 errno = EIO;
Tianjie Xu3c62b672016-02-05 18:25:58 -0800118 have_eio_error = true;
Jed Estepff6df892015-12-15 16:04:53 -0800119 return 0;
Jed Estep39c1b5e2015-12-15 16:04:53 -0800120 }
Jed Estepa7b9a462015-12-15 16:04:53 -0800121 }
Tianjie Xu3c62b672016-02-05 18:25:58 -0800122 size_t status = fread(ptr, size, nitems, stream);
123 // If I/O error occurs, set the retry-update flag.
124 if (status != nitems && errno == EIO) {
125 have_eio_error = true;
126 }
127 return status;
Jed Estepa7b9a462015-12-15 16:04:53 -0800128}
129
130ssize_t ota_read(int fd, void* buf, size_t nbyte) {
Jed Estepff6df892015-12-15 16:04:53 -0800131 if (should_fault_inject(OTAIO_READ)) {
Tianjie Xu4bb11c72017-04-06 23:54:03 -0700132 std::lock_guard<std::mutex> lock(filename_mutex);
Jed Estepff6df892015-12-15 16:04:53 -0800133 auto cached = filename_cache.find(fd);
134 const char* cached_path = cached->second;
135 if (cached != filename_cache.end()
136 && get_hit_file(cached_path, read_fault_file_name)) {
137 read_fault_file_name = "";
138 errno = EIO;
Tianjie Xu3c62b672016-02-05 18:25:58 -0800139 have_eio_error = true;
Jed Estepff6df892015-12-15 16:04:53 -0800140 return -1;
Jed Estep39c1b5e2015-12-15 16:04:53 -0800141 }
Jed Estepa7b9a462015-12-15 16:04:53 -0800142 }
Tianjie Xu3c62b672016-02-05 18:25:58 -0800143 ssize_t status = read(fd, buf, nbyte);
144 if (status == -1 && errno == EIO) {
145 have_eio_error = true;
146 }
147 return status;
Jed Estepa7b9a462015-12-15 16:04:53 -0800148}
149
150size_t ota_fwrite(const void* ptr, size_t size, size_t count, FILE* stream) {
Jed Estepff6df892015-12-15 16:04:53 -0800151 if (should_fault_inject(OTAIO_WRITE)) {
Tianjie Xu4bb11c72017-04-06 23:54:03 -0700152 std::lock_guard<std::mutex> lock(filename_mutex);
Jed Estepff6df892015-12-15 16:04:53 -0800153 auto cached = filename_cache.find((intptr_t)stream);
154 const char* cached_path = cached->second;
155 if (cached != filename_cache.end() &&
156 get_hit_file(cached_path, write_fault_file_name)) {
157 write_fault_file_name = "";
158 errno = EIO;
Tianjie Xu3c62b672016-02-05 18:25:58 -0800159 have_eio_error = true;
Jed Estepff6df892015-12-15 16:04:53 -0800160 return 0;
Jed Estep39c1b5e2015-12-15 16:04:53 -0800161 }
Jed Estepa7b9a462015-12-15 16:04:53 -0800162 }
Tianjie Xu3c62b672016-02-05 18:25:58 -0800163 size_t status = fwrite(ptr, size, count, stream);
164 if (status != count && errno == EIO) {
165 have_eio_error = true;
166 }
167 return status;
Jed Estepa7b9a462015-12-15 16:04:53 -0800168}
169
170ssize_t ota_write(int fd, const void* buf, size_t nbyte) {
Jed Estepff6df892015-12-15 16:04:53 -0800171 if (should_fault_inject(OTAIO_WRITE)) {
Tianjie Xu4bb11c72017-04-06 23:54:03 -0700172 std::lock_guard<std::mutex> lock(filename_mutex);
Jed Estepff6df892015-12-15 16:04:53 -0800173 auto cached = filename_cache.find(fd);
174 const char* cached_path = cached->second;
175 if (cached != filename_cache.end() &&
176 get_hit_file(cached_path, write_fault_file_name)) {
177 write_fault_file_name = "";
178 errno = EIO;
Tianjie Xu3c62b672016-02-05 18:25:58 -0800179 have_eio_error = true;
Jed Estepff6df892015-12-15 16:04:53 -0800180 return -1;
Jed Estep39c1b5e2015-12-15 16:04:53 -0800181 }
Jed Estepa7b9a462015-12-15 16:04:53 -0800182 }
Tianjie Xu3c62b672016-02-05 18:25:58 -0800183 ssize_t status = write(fd, buf, nbyte);
184 if (status == -1 && errno == EIO) {
185 have_eio_error = true;
186 }
187 return status;
Jed Estepa7b9a462015-12-15 16:04:53 -0800188}
189
190int ota_fsync(int fd) {
Jed Estepff6df892015-12-15 16:04:53 -0800191 if (should_fault_inject(OTAIO_FSYNC)) {
Tianjie Xu4bb11c72017-04-06 23:54:03 -0700192 std::lock_guard<std::mutex> lock(filename_mutex);
Jed Estepff6df892015-12-15 16:04:53 -0800193 auto cached = filename_cache.find(fd);
194 const char* cached_path = cached->second;
195 if (cached != filename_cache.end() &&
196 get_hit_file(cached_path, fsync_fault_file_name)) {
197 fsync_fault_file_name = "";
198 errno = EIO;
Tianjie Xu3c62b672016-02-05 18:25:58 -0800199 have_eio_error = true;
Jed Estepff6df892015-12-15 16:04:53 -0800200 return -1;
Jed Estep39c1b5e2015-12-15 16:04:53 -0800201 }
Jed Estepa7b9a462015-12-15 16:04:53 -0800202 }
Tianjie Xu3c62b672016-02-05 18:25:58 -0800203 int status = fsync(fd);
204 if (status == -1 && errno == EIO) {
205 have_eio_error = true;
206 }
207 return status;
Jed Estepa7b9a462015-12-15 16:04:53 -0800208}
Jed Estepff6df892015-12-15 16:04:53 -0800209