blob: 63ef18e2648134dfb770271a8273d07196620a69 [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 Baod33b2f82017-09-28 21:29:11 -070017#include "otafault/ota_io.h"
Jed Estepa7b9a462015-12-15 16:04:53 -080018
19#include <errno.h>
20#include <fcntl.h>
Tao Baoac27a7a2017-09-28 17:43:53 -070021#include <stdint.h>
Jed Estepa7b9a462015-12-15 16:04:53 -080022#include <stdio.h>
23#include <sys/stat.h>
Tao Baoac27a7a2017-09-28 17:43:53 -070024#include <sys/types.h>
Jed Estepa7b9a462015-12-15 16:04:53 -080025#include <unistd.h>
26
Tao Bao3dc14cb2016-11-22 11:37:13 -080027#include <map>
Tianjie Xu4bb11c72017-04-06 23:54:03 -070028#include <mutex>
Tao Baof0ca4f82017-10-03 17:21:11 -070029#include <string>
Tao Bao3dc14cb2016-11-22 11:37:13 -080030
Tianjie Xu4bb11c72017-04-06 23:54:03 -070031#include <android-base/thread_annotations.h>
Tao Baoac27a7a2017-09-28 17:43:53 -070032
Tao Baod33b2f82017-09-28 21:29:11 -070033#include "otafault/config.h"
Jed Estepa7b9a462015-12-15 16:04:53 -080034
Tianjie Xu4bb11c72017-04-06 23:54:03 -070035static std::mutex filename_mutex;
36static std::map<intptr_t, const char*> filename_cache GUARDED_BY(filename_mutex);
Jed Estepff6df892015-12-15 16:04:53 -080037static std::string read_fault_file_name = "";
38static std::string write_fault_file_name = "";
39static std::string fsync_fault_file_name = "";
40
Chih-Hung Hsieh23abfd32016-07-27 10:19:47 -070041static bool get_hit_file(const char* cached_path, const std::string& ffn) {
Jed Estepff6df892015-12-15 16:04:53 -080042 return should_hit_cache()
43 ? !strncmp(cached_path, OTAIO_CACHE_FNAME, strlen(cached_path))
44 : !strncmp(cached_path, ffn.c_str(), strlen(cached_path));
45}
46
47void ota_set_fault_files() {
48 if (should_fault_inject(OTAIO_READ)) {
49 read_fault_file_name = fault_fname(OTAIO_READ);
50 }
51 if (should_fault_inject(OTAIO_WRITE)) {
52 write_fault_file_name = fault_fname(OTAIO_WRITE);
53 }
54 if (should_fault_inject(OTAIO_FSYNC)) {
55 fsync_fault_file_name = fault_fname(OTAIO_FSYNC);
56 }
57}
Jed Estepf1fc48c2015-12-15 16:04:53 -080058
Tianjie Xu3c62b672016-02-05 18:25:58 -080059bool have_eio_error = false;
Jed Estep39c1b5e2015-12-15 16:04:53 -080060
Jed Estepa7b9a462015-12-15 16:04:53 -080061int ota_open(const char* path, int oflags) {
Jed Estepa7b9a462015-12-15 16:04:53 -080062 // Let the caller handle errors; we do not care if open succeeds or fails
63 int fd = open(path, oflags);
Tianjie Xu4bb11c72017-04-06 23:54:03 -070064 std::lock_guard<std::mutex> lock(filename_mutex);
Jed Estepff6df892015-12-15 16:04:53 -080065 filename_cache[fd] = path;
Jed Estepa7b9a462015-12-15 16:04:53 -080066 return fd;
Jed Estepa7b9a462015-12-15 16:04:53 -080067}
68
69int ota_open(const char* path, int oflags, mode_t mode) {
Jed Estepa7b9a462015-12-15 16:04:53 -080070 int fd = open(path, oflags, mode);
Tianjie Xu4bb11c72017-04-06 23:54:03 -070071 std::lock_guard<std::mutex> lock(filename_mutex);
Jed Estepff6df892015-12-15 16:04:53 -080072 filename_cache[fd] = path;
Tianjie Xu4bb11c72017-04-06 23:54:03 -070073 return fd;
74}
Jed Estepa7b9a462015-12-15 16:04:53 -080075
76FILE* ota_fopen(const char* path, const char* mode) {
Jed Estepa7b9a462015-12-15 16:04:53 -080077 FILE* fh = fopen(path, mode);
Tianjie Xu4bb11c72017-04-06 23:54:03 -070078 std::lock_guard<std::mutex> lock(filename_mutex);
Jed Estepff6df892015-12-15 16:04:53 -080079 filename_cache[(intptr_t)fh] = path;
Jed Estepa7b9a462015-12-15 16:04:53 -080080 return fh;
Jed Estepa7b9a462015-12-15 16:04:53 -080081}
82
Tao Bao358c2ec2016-11-28 11:48:43 -080083static int __ota_close(int fd) {
Jed Estepff6df892015-12-15 16:04:53 -080084 // descriptors can be reused, so make sure not to leave them in the cache
Tianjie Xu4bb11c72017-04-06 23:54:03 -070085 std::lock_guard<std::mutex> lock(filename_mutex);
Jed Estepff6df892015-12-15 16:04:53 -080086 filename_cache.erase(fd);
Jed Estepa7b9a462015-12-15 16:04:53 -080087 return close(fd);
88}
89
Tao Bao358c2ec2016-11-28 11:48:43 -080090void OtaCloser::Close(int fd) {
91 __ota_close(fd);
Tao Bao3dc14cb2016-11-22 11:37:13 -080092}
93
Tao Bao358c2ec2016-11-28 11:48:43 -080094int ota_close(unique_fd& fd) {
95 return __ota_close(fd.release());
96}
97
98static int __ota_fclose(FILE* fh) {
Tianjie Xu4bb11c72017-04-06 23:54:03 -070099 std::lock_guard<std::mutex> lock(filename_mutex);
Tao Bao358c2ec2016-11-28 11:48:43 -0800100 filename_cache.erase(reinterpret_cast<intptr_t>(fh));
Jed Estepa7b9a462015-12-15 16:04:53 -0800101 return fclose(fh);
102}
103
Mikhail Lappob49767c2017-03-23 21:44:26 +0100104void OtaFcloser::operator()(FILE* f) const {
Tao Bao358c2ec2016-11-28 11:48:43 -0800105 __ota_fclose(f);
106};
107
108int ota_fclose(unique_file& fh) {
109 return __ota_fclose(fh.release());
Tao Bao3dc14cb2016-11-22 11:37:13 -0800110}
111
Jed Estepa7b9a462015-12-15 16:04:53 -0800112size_t ota_fread(void* ptr, size_t size, size_t nitems, FILE* stream) {
Jed Estepff6df892015-12-15 16:04:53 -0800113 if (should_fault_inject(OTAIO_READ)) {
Tianjie Xu4bb11c72017-04-06 23:54:03 -0700114 std::lock_guard<std::mutex> lock(filename_mutex);
Jed Estepff6df892015-12-15 16:04:53 -0800115 auto cached = filename_cache.find((intptr_t)stream);
116 const char* cached_path = cached->second;
117 if (cached != filename_cache.end() &&
118 get_hit_file(cached_path, read_fault_file_name)) {
119 read_fault_file_name = "";
120 errno = EIO;
Tianjie Xu3c62b672016-02-05 18:25:58 -0800121 have_eio_error = true;
Jed Estepff6df892015-12-15 16:04:53 -0800122 return 0;
Jed Estep39c1b5e2015-12-15 16:04:53 -0800123 }
Jed Estepa7b9a462015-12-15 16:04:53 -0800124 }
Tianjie Xu3c62b672016-02-05 18:25:58 -0800125 size_t status = fread(ptr, size, nitems, stream);
126 // If I/O error occurs, set the retry-update flag.
127 if (status != nitems && errno == EIO) {
128 have_eio_error = true;
129 }
130 return status;
Jed Estepa7b9a462015-12-15 16:04:53 -0800131}
132
133ssize_t ota_read(int fd, void* buf, size_t nbyte) {
Jed Estepff6df892015-12-15 16:04:53 -0800134 if (should_fault_inject(OTAIO_READ)) {
Tianjie Xu4bb11c72017-04-06 23:54:03 -0700135 std::lock_guard<std::mutex> lock(filename_mutex);
Jed Estepff6df892015-12-15 16:04:53 -0800136 auto cached = filename_cache.find(fd);
137 const char* cached_path = cached->second;
138 if (cached != filename_cache.end()
139 && get_hit_file(cached_path, read_fault_file_name)) {
140 read_fault_file_name = "";
141 errno = EIO;
Tianjie Xu3c62b672016-02-05 18:25:58 -0800142 have_eio_error = true;
Jed Estepff6df892015-12-15 16:04:53 -0800143 return -1;
Jed Estep39c1b5e2015-12-15 16:04:53 -0800144 }
Jed Estepa7b9a462015-12-15 16:04:53 -0800145 }
Tianjie Xu3c62b672016-02-05 18:25:58 -0800146 ssize_t status = read(fd, buf, nbyte);
147 if (status == -1 && errno == EIO) {
148 have_eio_error = true;
149 }
150 return status;
Jed Estepa7b9a462015-12-15 16:04:53 -0800151}
152
153size_t ota_fwrite(const void* ptr, size_t size, size_t count, FILE* stream) {
Jed Estepff6df892015-12-15 16:04:53 -0800154 if (should_fault_inject(OTAIO_WRITE)) {
Tianjie Xu4bb11c72017-04-06 23:54:03 -0700155 std::lock_guard<std::mutex> lock(filename_mutex);
Jed Estepff6df892015-12-15 16:04:53 -0800156 auto cached = filename_cache.find((intptr_t)stream);
157 const char* cached_path = cached->second;
158 if (cached != filename_cache.end() &&
159 get_hit_file(cached_path, write_fault_file_name)) {
160 write_fault_file_name = "";
161 errno = EIO;
Tianjie Xu3c62b672016-02-05 18:25:58 -0800162 have_eio_error = true;
Jed Estepff6df892015-12-15 16:04:53 -0800163 return 0;
Jed Estep39c1b5e2015-12-15 16:04:53 -0800164 }
Jed Estepa7b9a462015-12-15 16:04:53 -0800165 }
Tianjie Xu3c62b672016-02-05 18:25:58 -0800166 size_t status = fwrite(ptr, size, count, stream);
167 if (status != count && errno == EIO) {
168 have_eio_error = true;
169 }
170 return status;
Jed Estepa7b9a462015-12-15 16:04:53 -0800171}
172
173ssize_t ota_write(int fd, const void* buf, size_t nbyte) {
Jed Estepff6df892015-12-15 16:04:53 -0800174 if (should_fault_inject(OTAIO_WRITE)) {
Tianjie Xu4bb11c72017-04-06 23:54:03 -0700175 std::lock_guard<std::mutex> lock(filename_mutex);
Jed Estepff6df892015-12-15 16:04:53 -0800176 auto cached = filename_cache.find(fd);
177 const char* cached_path = cached->second;
178 if (cached != filename_cache.end() &&
179 get_hit_file(cached_path, write_fault_file_name)) {
180 write_fault_file_name = "";
181 errno = EIO;
Tianjie Xu3c62b672016-02-05 18:25:58 -0800182 have_eio_error = true;
Jed Estepff6df892015-12-15 16:04:53 -0800183 return -1;
Jed Estep39c1b5e2015-12-15 16:04:53 -0800184 }
Jed Estepa7b9a462015-12-15 16:04:53 -0800185 }
Tianjie Xu3c62b672016-02-05 18:25:58 -0800186 ssize_t status = write(fd, buf, nbyte);
187 if (status == -1 && errno == EIO) {
188 have_eio_error = true;
189 }
190 return status;
Jed Estepa7b9a462015-12-15 16:04:53 -0800191}
192
193int ota_fsync(int fd) {
Jed Estepff6df892015-12-15 16:04:53 -0800194 if (should_fault_inject(OTAIO_FSYNC)) {
Tianjie Xu4bb11c72017-04-06 23:54:03 -0700195 std::lock_guard<std::mutex> lock(filename_mutex);
Jed Estepff6df892015-12-15 16:04:53 -0800196 auto cached = filename_cache.find(fd);
197 const char* cached_path = cached->second;
198 if (cached != filename_cache.end() &&
199 get_hit_file(cached_path, fsync_fault_file_name)) {
200 fsync_fault_file_name = "";
201 errno = EIO;
Tianjie Xu3c62b672016-02-05 18:25:58 -0800202 have_eio_error = true;
Jed Estepff6df892015-12-15 16:04:53 -0800203 return -1;
Jed Estep39c1b5e2015-12-15 16:04:53 -0800204 }
Jed Estepa7b9a462015-12-15 16:04:53 -0800205 }
Tianjie Xu3c62b672016-02-05 18:25:58 -0800206 int status = fsync(fd);
207 if (status == -1 && errno == EIO) {
208 have_eio_error = true;
209 }
210 return status;
Jed Estepa7b9a462015-12-15 16:04:53 -0800211}
Jed Estepff6df892015-12-15 16:04:53 -0800212