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