blob: 1308973a5c313f55a8f19bda1bfff680b5c89b24 [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 Bao3dc14cb2016-11-22 11:37:13 -080029
Tianjie Xu4bb11c72017-04-06 23:54:03 -070030#include <android-base/thread_annotations.h>
Tao Baoac27a7a2017-09-28 17:43:53 -070031
Tao Baod33b2f82017-09-28 21:29:11 -070032#include "otafault/config.h"
Jed Estepa7b9a462015-12-15 16:04:53 -080033
Tianjie Xu4bb11c72017-04-06 23:54:03 -070034static std::mutex filename_mutex;
35static std::map<intptr_t, const char*> filename_cache GUARDED_BY(filename_mutex);
Jed Estepff6df892015-12-15 16:04:53 -080036static std::string read_fault_file_name = "";
37static std::string write_fault_file_name = "";
38static std::string fsync_fault_file_name = "";
39
Chih-Hung Hsieh23abfd32016-07-27 10:19:47 -070040static bool get_hit_file(const char* cached_path, const std::string& ffn) {
Jed Estepff6df892015-12-15 16:04:53 -080041 return should_hit_cache()
42 ? !strncmp(cached_path, OTAIO_CACHE_FNAME, strlen(cached_path))
43 : !strncmp(cached_path, ffn.c_str(), strlen(cached_path));
44}
45
46void ota_set_fault_files() {
47 if (should_fault_inject(OTAIO_READ)) {
48 read_fault_file_name = fault_fname(OTAIO_READ);
49 }
50 if (should_fault_inject(OTAIO_WRITE)) {
51 write_fault_file_name = fault_fname(OTAIO_WRITE);
52 }
53 if (should_fault_inject(OTAIO_FSYNC)) {
54 fsync_fault_file_name = fault_fname(OTAIO_FSYNC);
55 }
56}
Jed Estepf1fc48c2015-12-15 16:04:53 -080057
Tianjie Xu3c62b672016-02-05 18:25:58 -080058bool have_eio_error = false;
Jed Estep39c1b5e2015-12-15 16:04:53 -080059
Jed Estepa7b9a462015-12-15 16:04:53 -080060int ota_open(const char* path, int oflags) {
Jed Estepa7b9a462015-12-15 16:04:53 -080061 // Let the caller handle errors; we do not care if open succeeds or fails
62 int fd = open(path, oflags);
Tianjie Xu4bb11c72017-04-06 23:54:03 -070063 std::lock_guard<std::mutex> lock(filename_mutex);
Jed Estepff6df892015-12-15 16:04:53 -080064 filename_cache[fd] = path;
Jed Estepa7b9a462015-12-15 16:04:53 -080065 return fd;
Jed Estepa7b9a462015-12-15 16:04:53 -080066}
67
68int ota_open(const char* path, int oflags, mode_t mode) {
Jed Estepa7b9a462015-12-15 16:04:53 -080069 int fd = open(path, oflags, mode);
Tianjie Xu4bb11c72017-04-06 23:54:03 -070070 std::lock_guard<std::mutex> lock(filename_mutex);
Jed Estepff6df892015-12-15 16:04:53 -080071 filename_cache[fd] = path;
Tianjie Xu4bb11c72017-04-06 23:54:03 -070072 return fd;
73}
Jed Estepa7b9a462015-12-15 16:04:53 -080074
75FILE* ota_fopen(const char* path, const char* mode) {
Jed Estepa7b9a462015-12-15 16:04:53 -080076 FILE* fh = fopen(path, mode);
Tianjie Xu4bb11c72017-04-06 23:54:03 -070077 std::lock_guard<std::mutex> lock(filename_mutex);
Jed Estepff6df892015-12-15 16:04:53 -080078 filename_cache[(intptr_t)fh] = path;
Jed Estepa7b9a462015-12-15 16:04:53 -080079 return fh;
Jed Estepa7b9a462015-12-15 16:04:53 -080080}
81
Tao Bao358c2ec2016-11-28 11:48:43 -080082static int __ota_close(int fd) {
Jed Estepff6df892015-12-15 16:04:53 -080083 // descriptors can be reused, so make sure not to leave them in the cache
Tianjie Xu4bb11c72017-04-06 23:54:03 -070084 std::lock_guard<std::mutex> lock(filename_mutex);
Jed Estepff6df892015-12-15 16:04:53 -080085 filename_cache.erase(fd);
Jed Estepa7b9a462015-12-15 16:04:53 -080086 return close(fd);
87}
88
Tao Bao358c2ec2016-11-28 11:48:43 -080089void OtaCloser::Close(int fd) {
90 __ota_close(fd);
Tao Bao3dc14cb2016-11-22 11:37:13 -080091}
92
Tao Bao358c2ec2016-11-28 11:48:43 -080093int ota_close(unique_fd& fd) {
94 return __ota_close(fd.release());
95}
96
97static int __ota_fclose(FILE* fh) {
Tianjie Xu4bb11c72017-04-06 23:54:03 -070098 std::lock_guard<std::mutex> lock(filename_mutex);
Tao Bao358c2ec2016-11-28 11:48:43 -080099 filename_cache.erase(reinterpret_cast<intptr_t>(fh));
Jed Estepa7b9a462015-12-15 16:04:53 -0800100 return fclose(fh);
101}
102
Mikhail Lappob49767c2017-03-23 21:44:26 +0100103void OtaFcloser::operator()(FILE* f) const {
Tao Bao358c2ec2016-11-28 11:48:43 -0800104 __ota_fclose(f);
105};
106
107int ota_fclose(unique_file& fh) {
108 return __ota_fclose(fh.release());
Tao Bao3dc14cb2016-11-22 11:37:13 -0800109}
110
Jed Estepa7b9a462015-12-15 16:04:53 -0800111size_t ota_fread(void* ptr, size_t size, size_t nitems, FILE* stream) {
Jed Estepff6df892015-12-15 16:04:53 -0800112 if (should_fault_inject(OTAIO_READ)) {
Tianjie Xu4bb11c72017-04-06 23:54:03 -0700113 std::lock_guard<std::mutex> lock(filename_mutex);
Jed Estepff6df892015-12-15 16:04:53 -0800114 auto cached = filename_cache.find((intptr_t)stream);
115 const char* cached_path = cached->second;
116 if (cached != filename_cache.end() &&
117 get_hit_file(cached_path, read_fault_file_name)) {
118 read_fault_file_name = "";
119 errno = EIO;
Tianjie Xu3c62b672016-02-05 18:25:58 -0800120 have_eio_error = true;
Jed Estepff6df892015-12-15 16:04:53 -0800121 return 0;
Jed Estep39c1b5e2015-12-15 16:04:53 -0800122 }
Jed Estepa7b9a462015-12-15 16:04:53 -0800123 }
Tianjie Xu3c62b672016-02-05 18:25:58 -0800124 size_t status = fread(ptr, size, nitems, stream);
125 // If I/O error occurs, set the retry-update flag.
126 if (status != nitems && errno == EIO) {
127 have_eio_error = true;
128 }
129 return status;
Jed Estepa7b9a462015-12-15 16:04:53 -0800130}
131
132ssize_t ota_read(int fd, void* buf, size_t nbyte) {
Jed Estepff6df892015-12-15 16:04:53 -0800133 if (should_fault_inject(OTAIO_READ)) {
Tianjie Xu4bb11c72017-04-06 23:54:03 -0700134 std::lock_guard<std::mutex> lock(filename_mutex);
Jed Estepff6df892015-12-15 16:04:53 -0800135 auto cached = filename_cache.find(fd);
136 const char* cached_path = cached->second;
137 if (cached != filename_cache.end()
138 && get_hit_file(cached_path, read_fault_file_name)) {
139 read_fault_file_name = "";
140 errno = EIO;
Tianjie Xu3c62b672016-02-05 18:25:58 -0800141 have_eio_error = true;
Jed Estepff6df892015-12-15 16:04:53 -0800142 return -1;
Jed Estep39c1b5e2015-12-15 16:04:53 -0800143 }
Jed Estepa7b9a462015-12-15 16:04:53 -0800144 }
Tianjie Xu3c62b672016-02-05 18:25:58 -0800145 ssize_t status = read(fd, buf, nbyte);
146 if (status == -1 && errno == EIO) {
147 have_eio_error = true;
148 }
149 return status;
Jed Estepa7b9a462015-12-15 16:04:53 -0800150}
151
152size_t ota_fwrite(const void* ptr, size_t size, size_t count, FILE* stream) {
Jed Estepff6df892015-12-15 16:04:53 -0800153 if (should_fault_inject(OTAIO_WRITE)) {
Tianjie Xu4bb11c72017-04-06 23:54:03 -0700154 std::lock_guard<std::mutex> lock(filename_mutex);
Jed Estepff6df892015-12-15 16:04:53 -0800155 auto cached = filename_cache.find((intptr_t)stream);
156 const char* cached_path = cached->second;
157 if (cached != filename_cache.end() &&
158 get_hit_file(cached_path, write_fault_file_name)) {
159 write_fault_file_name = "";
160 errno = EIO;
Tianjie Xu3c62b672016-02-05 18:25:58 -0800161 have_eio_error = true;
Jed Estepff6df892015-12-15 16:04:53 -0800162 return 0;
Jed Estep39c1b5e2015-12-15 16:04:53 -0800163 }
Jed Estepa7b9a462015-12-15 16:04:53 -0800164 }
Tianjie Xu3c62b672016-02-05 18:25:58 -0800165 size_t status = fwrite(ptr, size, count, stream);
166 if (status != count && errno == EIO) {
167 have_eio_error = true;
168 }
169 return status;
Jed Estepa7b9a462015-12-15 16:04:53 -0800170}
171
172ssize_t ota_write(int fd, const void* buf, size_t nbyte) {
Jed Estepff6df892015-12-15 16:04:53 -0800173 if (should_fault_inject(OTAIO_WRITE)) {
Tianjie Xu4bb11c72017-04-06 23:54:03 -0700174 std::lock_guard<std::mutex> lock(filename_mutex);
Jed Estepff6df892015-12-15 16:04:53 -0800175 auto cached = filename_cache.find(fd);
176 const char* cached_path = cached->second;
177 if (cached != filename_cache.end() &&
178 get_hit_file(cached_path, write_fault_file_name)) {
179 write_fault_file_name = "";
180 errno = EIO;
Tianjie Xu3c62b672016-02-05 18:25:58 -0800181 have_eio_error = true;
Jed Estepff6df892015-12-15 16:04:53 -0800182 return -1;
Jed Estep39c1b5e2015-12-15 16:04:53 -0800183 }
Jed Estepa7b9a462015-12-15 16:04:53 -0800184 }
Tianjie Xu3c62b672016-02-05 18:25:58 -0800185 ssize_t status = write(fd, buf, nbyte);
186 if (status == -1 && errno == EIO) {
187 have_eio_error = true;
188 }
189 return status;
Jed Estepa7b9a462015-12-15 16:04:53 -0800190}
191
192int ota_fsync(int fd) {
Jed Estepff6df892015-12-15 16:04:53 -0800193 if (should_fault_inject(OTAIO_FSYNC)) {
Tianjie Xu4bb11c72017-04-06 23:54:03 -0700194 std::lock_guard<std::mutex> lock(filename_mutex);
Jed Estepff6df892015-12-15 16:04:53 -0800195 auto cached = filename_cache.find(fd);
196 const char* cached_path = cached->second;
197 if (cached != filename_cache.end() &&
198 get_hit_file(cached_path, fsync_fault_file_name)) {
199 fsync_fault_file_name = "";
200 errno = EIO;
Tianjie Xu3c62b672016-02-05 18:25:58 -0800201 have_eio_error = true;
Jed Estepff6df892015-12-15 16:04:53 -0800202 return -1;
Jed Estep39c1b5e2015-12-15 16:04:53 -0800203 }
Jed Estepa7b9a462015-12-15 16:04:53 -0800204 }
Tianjie Xu3c62b672016-02-05 18:25:58 -0800205 int status = fsync(fd);
206 if (status == -1 && errno == EIO) {
207 have_eio_error = true;
208 }
209 return status;
Jed Estepa7b9a462015-12-15 16:04:53 -0800210}
Jed Estepff6df892015-12-15 16:04:53 -0800211