blob: 67874a8b9c02c5aa825f8700aa75beb479812984 [file] [log] [blame]
Jaekyun Seok7de2f9c2017-03-02 12:45:10 +09001#include <dirent.h>
2#include <inttypes.h>
3#include <sys/file.h>
4#include <sys/stat.h>
5
6#include "idmap.h"
7
8#include <memory>
9#include <androidfw/ResourceTypes.h>
10#include <androidfw/StreamingZipInflater.h>
11#include <androidfw/ZipFileRO.h>
Jaekyun Seok04342892017-03-02 15:24:19 +090012#include <cutils/jstring.h>
Jaekyun Seok7de2f9c2017-03-02 12:45:10 +090013#include <private/android_filesystem_config.h> // for AID_SYSTEM
14#include <utils/SortedVector.h>
15#include <utils/String16.h>
16#include <utils/String8.h>
17
18#define NO_OVERLAY_TAG (-1000)
19
20using namespace android;
21
22namespace {
23 struct Overlay {
24 Overlay() {}
25 Overlay(const String8& a, const String8& i, int p) :
26 apk_path(a), idmap_path(i), priority(p) {}
27
28 bool operator<(Overlay const& rhs) const
29 {
30 return rhs.priority > priority;
31 }
32
33 String8 apk_path;
34 String8 idmap_path;
35 int priority;
36 };
37
38 bool writePackagesList(const char *filename, const SortedVector<Overlay>& overlayVector)
39 {
40 // the file is opened for appending so that it doesn't get truncated
41 // before we can guarantee mutual exclusion via the flock
42 FILE* fout = fopen(filename, "a");
43 if (fout == NULL) {
44 return false;
45 }
46
47 if (TEMP_FAILURE_RETRY(flock(fileno(fout), LOCK_EX)) != 0) {
48 fclose(fout);
49 return false;
50 }
51
52 if (TEMP_FAILURE_RETRY(ftruncate(fileno(fout), 0)) != 0) {
53 TEMP_FAILURE_RETRY(flock(fileno(fout), LOCK_UN));
54 fclose(fout);
55 return false;
56 }
57
58 for (size_t i = 0; i < overlayVector.size(); ++i) {
59 const Overlay& overlay = overlayVector[i];
60 fprintf(fout, "%s %s\n", overlay.apk_path.string(), overlay.idmap_path.string());
61 }
62
63 TEMP_FAILURE_RETRY(fflush(fout));
64 TEMP_FAILURE_RETRY(flock(fileno(fout), LOCK_UN));
65 fclose(fout);
66
67 // Make file world readable since Zygote (running as root) will read
68 // it when creating the initial AssetManger object
69 const mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH; // 0644
70 if (chmod(filename, mode) == -1) {
71 unlink(filename);
72 return false;
73 }
74
75 return true;
76 }
77
78 String8 flatten_path(const char *path)
79 {
80 String16 tmp(path);
81 tmp.replaceAll('/', '@');
82 return String8(tmp);
83 }
84
Jaekyun Seok04342892017-03-02 15:24:19 +090085 int parse_overlay_tag(const ResXMLTree& parser, const char *target_package_name,
86 bool* is_static_overlay)
Jaekyun Seok7de2f9c2017-03-02 12:45:10 +090087 {
88 const size_t N = parser.getAttributeCount();
89 String16 target;
90 int priority = -1;
91 for (size_t i = 0; i < N; ++i) {
92 size_t len;
93 String16 key(parser.getAttributeName(i, &len));
94 if (key == String16("targetPackage")) {
95 const char16_t *p = parser.getAttributeStringValue(i, &len);
96 if (p != NULL) {
97 target = String16(p, len);
98 }
99 } else if (key == String16("priority")) {
100 Res_value v;
101 if (parser.getAttributeValue(i, &v) == sizeof(Res_value)) {
102 priority = v.data;
103 if (priority < 0 || priority > 9999) {
104 return -1;
105 }
106 }
Jaekyun Seok04342892017-03-02 15:24:19 +0900107 } else if (key == String16("isStatic")) {
108 Res_value v;
109 if (parser.getAttributeValue(i, &v) == sizeof(Res_value)) {
110 *is_static_overlay = (v.data != 0);
111 }
Jaekyun Seok7de2f9c2017-03-02 12:45:10 +0900112 }
113 }
114 if (target == String16(target_package_name)) {
115 return priority;
116 }
117 return NO_OVERLAY_TAG;
118 }
119
Jaekyun Seok04342892017-03-02 15:24:19 +0900120 String16 parse_package_name(const ResXMLTree& parser)
121 {
122 const size_t N = parser.getAttributeCount();
123 String16 package_name;
124 for (size_t i = 0; i < N; ++i) {
125 size_t len;
126 String16 key(parser.getAttributeName(i, &len));
127 if (key == String16("package")) {
128 const char16_t *p = parser.getAttributeStringValue(i, &len);
129 if (p != NULL) {
130 package_name = String16(p, len);
131 }
132 }
133 }
134 return package_name;
135 }
136
137 bool isValidStaticOverlayPackage(const String16& package_name) {
138 // TODO(b/35742444): Need to support selection method based on a package name.
139 return package_name.size() > 0;
140 }
141
Jaekyun Seok7de2f9c2017-03-02 12:45:10 +0900142 int parse_manifest(const void *data, size_t size, const char *target_package_name)
143 {
144 ResXMLTree parser;
145 parser.setTo(data, size);
146 if (parser.getError() != NO_ERROR) {
147 ALOGD("%s failed to init xml parser, error=0x%08x\n", __FUNCTION__, parser.getError());
148 return -1;
149 }
150
151 ResXMLParser::event_code_t type;
Jaekyun Seok04342892017-03-02 15:24:19 +0900152 String16 package_name;
153 bool is_static_overlay = false;
154 int priority = NO_OVERLAY_TAG;
Jaekyun Seok7de2f9c2017-03-02 12:45:10 +0900155 do {
156 type = parser.next();
157 if (type == ResXMLParser::START_TAG) {
158 size_t len;
159 String16 tag(parser.getElementName(&len));
Jaekyun Seok04342892017-03-02 15:24:19 +0900160 if (tag == String16("manifest")) {
161 package_name = parse_package_name(parser);
162 } else if (tag == String16("overlay")) {
163 priority = parse_overlay_tag(parser, target_package_name, &is_static_overlay);
164 break;
Jaekyun Seok7de2f9c2017-03-02 12:45:10 +0900165 }
166 }
167 } while (type != ResXMLParser::BAD_DOCUMENT && type != ResXMLParser::END_DOCUMENT);
168
Jaekyun Seok04342892017-03-02 15:24:19 +0900169 if (is_static_overlay && isValidStaticOverlayPackage(package_name)) {
170 return priority;
171 }
Jaekyun Seok7de2f9c2017-03-02 12:45:10 +0900172 return NO_OVERLAY_TAG;
173 }
174
175 int parse_apk(const char *path, const char *target_package_name)
176 {
177 std::unique_ptr<ZipFileRO> zip(ZipFileRO::open(path));
178 if (zip.get() == NULL) {
179 ALOGW("%s: failed to open zip %s\n", __FUNCTION__, path);
180 return -1;
181 }
182 ZipEntryRO entry;
183 if ((entry = zip->findEntryByName("AndroidManifest.xml")) == NULL) {
184 ALOGW("%s: failed to find entry AndroidManifest.xml\n", __FUNCTION__);
185 return -1;
186 }
187 uint32_t uncompLen = 0;
188 uint16_t method;
189 if (!zip->getEntryInfo(entry, &method, &uncompLen, NULL, NULL, NULL, NULL)) {
190 ALOGW("%s: failed to read entry info\n", __FUNCTION__);
191 return -1;
192 }
193 if (method != ZipFileRO::kCompressDeflated) {
194 ALOGW("%s: cannot handle zip compression method %" PRIu16 "\n", __FUNCTION__, method);
195 return -1;
196 }
197 FileMap *dataMap = zip->createEntryFileMap(entry);
198 if (dataMap == NULL) {
199 ALOGW("%s: failed to create FileMap\n", __FUNCTION__);
200 return -1;
201 }
202 char *buf = new char[uncompLen];
203 if (NULL == buf) {
204 ALOGW("%s: failed to allocate %" PRIu32 " byte\n", __FUNCTION__, uncompLen);
205 delete dataMap;
206 return -1;
207 }
208 StreamingZipInflater inflater(dataMap, uncompLen);
209 if (inflater.read(buf, uncompLen) < 0) {
210 ALOGW("%s: failed to inflate %" PRIu32 " byte\n", __FUNCTION__, uncompLen);
211 delete[] buf;
212 delete dataMap;
213 return -1;
214 }
215
216 int priority = parse_manifest(buf, static_cast<size_t>(uncompLen), target_package_name);
217 delete[] buf;
218 delete dataMap;
219 return priority;
220 }
221}
222
223int idmap_scan(const char *target_package_name, const char *target_apk_path,
224 const char *idmap_dir, const android::Vector<const char *> *overlay_dirs)
225{
226 String8 filename = String8(idmap_dir);
227 filename.appendPath("overlays.list");
228
229 SortedVector<Overlay> overlayVector;
230 const size_t N = overlay_dirs->size();
231 for (size_t i = 0; i < N; ++i) {
232 const char *overlay_dir = overlay_dirs->itemAt(i);
233 DIR *dir = opendir(overlay_dir);
234 if (dir == NULL) {
235 return EXIT_FAILURE;
236 }
237
238 struct dirent *dirent;
239 while ((dirent = readdir(dir)) != NULL) {
240 struct stat st;
241 char overlay_apk_path[PATH_MAX + 1];
242 snprintf(overlay_apk_path, PATH_MAX, "%s/%s", overlay_dir, dirent->d_name);
243 if (stat(overlay_apk_path, &st) < 0) {
244 continue;
245 }
246 if (!S_ISREG(st.st_mode)) {
247 continue;
248 }
249
250 int priority = parse_apk(overlay_apk_path, target_package_name);
251 if (priority < 0) {
252 continue;
253 }
254
255 String8 idmap_path(idmap_dir);
256 idmap_path.appendPath(flatten_path(overlay_apk_path + 1));
257 idmap_path.append("@idmap");
258
259 if (idmap_create_path(target_apk_path, overlay_apk_path, idmap_path.string()) != 0) {
260 ALOGE("error: failed to create idmap for target=%s overlay=%s idmap=%s\n",
261 target_apk_path, overlay_apk_path, idmap_path.string());
262 continue;
263 }
264
265 Overlay overlay(String8(overlay_apk_path), idmap_path, priority);
266 overlayVector.add(overlay);
267 }
268
269 closedir(dir);
270 }
271
272 if (!writePackagesList(filename.string(), overlayVector)) {
273 return EXIT_FAILURE;
274 }
275
276 return EXIT_SUCCESS;
277}
278