• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 #include "ota_io.h"
18 
19 #include <errno.h>
20 #include <fcntl.h>
21 #include <stdio.h>
22 #include <sys/stat.h>
23 #include <unistd.h>
24 
25 #include <map>
26 #include <memory>
27 #include <mutex>
28 
29 #include <android-base/thread_annotations.h>
30 #include "config.h"
31 
32 static std::mutex filename_mutex;
33 static std::map<intptr_t, const char*> filename_cache GUARDED_BY(filename_mutex);
34 static std::string read_fault_file_name = "";
35 static std::string write_fault_file_name = "";
36 static std::string fsync_fault_file_name = "";
37 
get_hit_file(const char * cached_path,const std::string & ffn)38 static bool get_hit_file(const char* cached_path, const std::string& ffn) {
39     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 
ota_set_fault_files()44 void 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 }
55 
56 bool have_eio_error = false;
57 
ota_open(const char * path,int oflags)58 int ota_open(const char* path, int oflags) {
59     // Let the caller handle errors; we do not care if open succeeds or fails
60     int fd = open(path, oflags);
61     std::lock_guard<std::mutex> lock(filename_mutex);
62     filename_cache[fd] = path;
63     return fd;
64 }
65 
ota_open(const char * path,int oflags,mode_t mode)66 int ota_open(const char* path, int oflags, mode_t mode) {
67     int fd = open(path, oflags, mode);
68     std::lock_guard<std::mutex> lock(filename_mutex);
69     filename_cache[fd] = path;
70     return fd;
71 }
72 
ota_fopen(const char * path,const char * mode)73 FILE* ota_fopen(const char* path, const char* mode) {
74     FILE* fh = fopen(path, mode);
75     std::lock_guard<std::mutex> lock(filename_mutex);
76     filename_cache[(intptr_t)fh] = path;
77     return fh;
78 }
79 
__ota_close(int fd)80 static int __ota_close(int fd) {
81     // descriptors can be reused, so make sure not to leave them in the cache
82     std::lock_guard<std::mutex> lock(filename_mutex);
83     filename_cache.erase(fd);
84     return close(fd);
85 }
86 
Close(int fd)87 void OtaCloser::Close(int fd) {
88     __ota_close(fd);
89 }
90 
ota_close(unique_fd & fd)91 int ota_close(unique_fd& fd) {
92     return __ota_close(fd.release());
93 }
94 
__ota_fclose(FILE * fh)95 static int __ota_fclose(FILE* fh) {
96     std::lock_guard<std::mutex> lock(filename_mutex);
97     filename_cache.erase(reinterpret_cast<intptr_t>(fh));
98     return fclose(fh);
99 }
100 
operator ()(FILE * f) const101 void OtaFcloser::operator()(FILE* f) const {
102     __ota_fclose(f);
103 };
104 
ota_fclose(unique_file & fh)105 int ota_fclose(unique_file& fh) {
106   return __ota_fclose(fh.release());
107 }
108 
ota_fread(void * ptr,size_t size,size_t nitems,FILE * stream)109 size_t ota_fread(void* ptr, size_t size, size_t nitems, FILE* stream) {
110     if (should_fault_inject(OTAIO_READ)) {
111         std::lock_guard<std::mutex> lock(filename_mutex);
112         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;
118             have_eio_error = true;
119             return 0;
120         }
121     }
122     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;
128 }
129 
ota_read(int fd,void * buf,size_t nbyte)130 ssize_t ota_read(int fd, void* buf, size_t nbyte) {
131     if (should_fault_inject(OTAIO_READ)) {
132         std::lock_guard<std::mutex> lock(filename_mutex);
133         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;
139             have_eio_error = true;
140             return -1;
141         }
142     }
143     ssize_t status = read(fd, buf, nbyte);
144     if (status == -1 && errno == EIO) {
145         have_eio_error = true;
146     }
147     return status;
148 }
149 
ota_fwrite(const void * ptr,size_t size,size_t count,FILE * stream)150 size_t ota_fwrite(const void* ptr, size_t size, size_t count, FILE* stream) {
151     if (should_fault_inject(OTAIO_WRITE)) {
152         std::lock_guard<std::mutex> lock(filename_mutex);
153         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;
159             have_eio_error = true;
160             return 0;
161         }
162     }
163     size_t status = fwrite(ptr, size, count, stream);
164     if (status != count && errno == EIO) {
165         have_eio_error = true;
166     }
167     return status;
168 }
169 
ota_write(int fd,const void * buf,size_t nbyte)170 ssize_t ota_write(int fd, const void* buf, size_t nbyte) {
171     if (should_fault_inject(OTAIO_WRITE)) {
172         std::lock_guard<std::mutex> lock(filename_mutex);
173         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;
179             have_eio_error = true;
180             return -1;
181         }
182     }
183     ssize_t status = write(fd, buf, nbyte);
184     if (status == -1 && errno == EIO) {
185         have_eio_error = true;
186     }
187     return status;
188 }
189 
ota_fsync(int fd)190 int ota_fsync(int fd) {
191     if (should_fault_inject(OTAIO_FSYNC)) {
192         std::lock_guard<std::mutex> lock(filename_mutex);
193         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;
199             have_eio_error = true;
200             return -1;
201         }
202     }
203     int status = fsync(fd);
204     if (status == -1 && errno == EIO) {
205         have_eio_error = true;
206     }
207     return status;
208 }
209 
210