blob: ab6adfb9475fe454872261beccf44552fcb99d36 [file] [log] [blame]
Mark Salyzynb62286e2015-04-01 07:48:34 -07001#include <dirent.h>
Narayan Kamath4600dd02015-06-16 12:02:57 +01002#include <inttypes.h>
Martin Wallgren0fbb6082015-08-11 15:10:31 +02003#include <sys/file.h>
Mark Salyzynb62286e2015-04-01 07:48:34 -07004#include <sys/stat.h>
5
Colin Cross13221c92014-02-11 18:04:44 -08006#include "idmap.h"
7
Dan Albert0881a742015-11-05 00:57:12 -08008#include <memory>
Colin Cross13221c92014-02-11 18:04:44 -08009#include <androidfw/ResourceTypes.h>
10#include <androidfw/StreamingZipInflater.h>
11#include <androidfw/ZipFileRO.h>
12#include <private/android_filesystem_config.h> // for AID_SYSTEM
13#include <utils/SortedVector.h>
14#include <utils/String16.h>
15#include <utils/String8.h>
16
Colin Cross13221c92014-02-11 18:04:44 -080017#define NO_OVERLAY_TAG (-1000)
18
19using namespace android;
20
21namespace {
22 struct Overlay {
23 Overlay() {}
24 Overlay(const String8& a, const String8& i, int p) :
25 apk_path(a), idmap_path(i), priority(p) {}
26
27 bool operator<(Overlay const& rhs) const
28 {
Mårten Kongstad7197d912015-03-18 15:24:18 +010029 return rhs.priority > priority;
Colin Cross13221c92014-02-11 18:04:44 -080030 }
31
32 String8 apk_path;
33 String8 idmap_path;
34 int priority;
35 };
36
37 bool writePackagesList(const char *filename, const SortedVector<Overlay>& overlayVector)
38 {
Martin Wallgren0fbb6082015-08-11 15:10:31 +020039 // the file is opened for appending so that it doesn't get truncated
40 // before we can guarantee mutual exclusion via the flock
41 FILE* fout = fopen(filename, "a");
Colin Cross13221c92014-02-11 18:04:44 -080042 if (fout == NULL) {
43 return false;
44 }
45
Martin Wallgren0fbb6082015-08-11 15:10:31 +020046 if (TEMP_FAILURE_RETRY(flock(fileno(fout), LOCK_EX)) != 0) {
47 fclose(fout);
48 return false;
49 }
50
51 if (TEMP_FAILURE_RETRY(ftruncate(fileno(fout), 0)) != 0) {
52 TEMP_FAILURE_RETRY(flock(fileno(fout), LOCK_UN));
53 fclose(fout);
54 return false;
55 }
56
Colin Cross13221c92014-02-11 18:04:44 -080057 for (size_t i = 0; i < overlayVector.size(); ++i) {
58 const Overlay& overlay = overlayVector[i];
59 fprintf(fout, "%s %s\n", overlay.apk_path.string(), overlay.idmap_path.string());
60 }
61
Martin Wallgren0fbb6082015-08-11 15:10:31 +020062 TEMP_FAILURE_RETRY(fflush(fout));
63 TEMP_FAILURE_RETRY(flock(fileno(fout), LOCK_UN));
Colin Cross13221c92014-02-11 18:04:44 -080064 fclose(fout);
65
66 // Make file world readable since Zygote (running as root) will read
67 // it when creating the initial AssetManger object
68 const mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH; // 0644
69 if (chmod(filename, mode) == -1) {
70 unlink(filename);
71 return false;
72 }
73
74 return true;
75 }
76
77 String8 flatten_path(const char *path)
78 {
79 String16 tmp(path);
80 tmp.replaceAll('/', '@');
81 return String8(tmp);
82 }
83
Colin Cross13221c92014-02-11 18:04:44 -080084 int parse_overlay_tag(const ResXMLTree& parser, const char *target_package_name)
85 {
86 const size_t N = parser.getAttributeCount();
87 String16 target;
88 int priority = -1;
89 for (size_t i = 0; i < N; ++i) {
90 size_t len;
91 String16 key(parser.getAttributeName(i, &len));
92 if (key == String16("targetPackage")) {
Dan Albert66987492014-11-20 11:41:21 -080093 const char16_t *p = parser.getAttributeStringValue(i, &len);
Andreas Gampecfedceb2014-09-30 21:48:18 -070094 if (p != NULL) {
Colin Cross13221c92014-02-11 18:04:44 -080095 target = String16(p, len);
96 }
97 } else if (key == String16("priority")) {
98 Res_value v;
99 if (parser.getAttributeValue(i, &v) == sizeof(Res_value)) {
100 priority = v.data;
101 if (priority < 0 || priority > 9999) {
102 return -1;
103 }
104 }
105 }
106 }
107 if (target == String16(target_package_name)) {
108 return priority;
109 }
110 return NO_OVERLAY_TAG;
111 }
112
113 int parse_manifest(const void *data, size_t size, const char *target_package_name)
114 {
Adam Lesinskide898ff2014-01-29 18:20:45 -0800115 ResXMLTree parser;
116 parser.setTo(data, size);
Colin Cross13221c92014-02-11 18:04:44 -0800117 if (parser.getError() != NO_ERROR) {
118 ALOGD("%s failed to init xml parser, error=0x%08x\n", __FUNCTION__, parser.getError());
119 return -1;
120 }
121
122 ResXMLParser::event_code_t type;
123 do {
124 type = parser.next();
125 if (type == ResXMLParser::START_TAG) {
126 size_t len;
127 String16 tag(parser.getElementName(&len));
128 if (tag == String16("overlay")) {
129 return parse_overlay_tag(parser, target_package_name);
130 }
131 }
132 } while (type != ResXMLParser::BAD_DOCUMENT && type != ResXMLParser::END_DOCUMENT);
133
134 return NO_OVERLAY_TAG;
135 }
136
137 int parse_apk(const char *path, const char *target_package_name)
138 {
Dan Albert0881a742015-11-05 00:57:12 -0800139 std::unique_ptr<ZipFileRO> zip(ZipFileRO::open(path));
Colin Cross13221c92014-02-11 18:04:44 -0800140 if (zip.get() == NULL) {
141 ALOGW("%s: failed to open zip %s\n", __FUNCTION__, path);
142 return -1;
143 }
144 ZipEntryRO entry;
145 if ((entry = zip->findEntryByName("AndroidManifest.xml")) == NULL) {
146 ALOGW("%s: failed to find entry AndroidManifest.xml\n", __FUNCTION__);
147 return -1;
148 }
Narayan Kamath4600dd02015-06-16 12:02:57 +0100149 uint32_t uncompLen = 0;
150 uint16_t method;
Colin Cross13221c92014-02-11 18:04:44 -0800151 if (!zip->getEntryInfo(entry, &method, &uncompLen, NULL, NULL, NULL, NULL)) {
152 ALOGW("%s: failed to read entry info\n", __FUNCTION__);
153 return -1;
154 }
155 if (method != ZipFileRO::kCompressDeflated) {
Narayan Kamath4600dd02015-06-16 12:02:57 +0100156 ALOGW("%s: cannot handle zip compression method %" PRIu16 "\n", __FUNCTION__, method);
Colin Cross13221c92014-02-11 18:04:44 -0800157 return -1;
158 }
159 FileMap *dataMap = zip->createEntryFileMap(entry);
Andreas Gampecfedceb2014-09-30 21:48:18 -0700160 if (dataMap == NULL) {
Colin Cross13221c92014-02-11 18:04:44 -0800161 ALOGW("%s: failed to create FileMap\n", __FUNCTION__);
162 return -1;
163 }
164 char *buf = new char[uncompLen];
165 if (NULL == buf) {
Narayan Kamath4600dd02015-06-16 12:02:57 +0100166 ALOGW("%s: failed to allocate %" PRIu32 " byte\n", __FUNCTION__, uncompLen);
Narayan Kamath688ff4c2015-02-23 15:47:54 +0000167 delete dataMap;
Colin Cross13221c92014-02-11 18:04:44 -0800168 return -1;
169 }
170 StreamingZipInflater inflater(dataMap, uncompLen);
171 if (inflater.read(buf, uncompLen) < 0) {
Narayan Kamath4600dd02015-06-16 12:02:57 +0100172 ALOGW("%s: failed to inflate %" PRIu32 " byte\n", __FUNCTION__, uncompLen);
Colin Cross13221c92014-02-11 18:04:44 -0800173 delete[] buf;
Narayan Kamath688ff4c2015-02-23 15:47:54 +0000174 delete dataMap;
Colin Cross13221c92014-02-11 18:04:44 -0800175 return -1;
176 }
177
Narayan Kamath4600dd02015-06-16 12:02:57 +0100178 int priority = parse_manifest(buf, static_cast<size_t>(uncompLen), target_package_name);
Colin Cross13221c92014-02-11 18:04:44 -0800179 delete[] buf;
Narayan Kamath688ff4c2015-02-23 15:47:54 +0000180 delete dataMap;
Colin Cross13221c92014-02-11 18:04:44 -0800181 return priority;
182 }
183}
184
Mårten Kongstad83ab0d72015-03-20 12:57:36 +0100185int idmap_scan(const char *target_package_name, const char *target_apk_path,
186 const char *idmap_dir, const android::Vector<const char *> *overlay_dirs)
Colin Cross13221c92014-02-11 18:04:44 -0800187{
188 String8 filename = String8(idmap_dir);
189 filename.appendPath("overlays.list");
Colin Cross13221c92014-02-11 18:04:44 -0800190
Colin Cross13221c92014-02-11 18:04:44 -0800191 SortedVector<Overlay> overlayVector;
Mårten Kongstad83ab0d72015-03-20 12:57:36 +0100192 const size_t N = overlay_dirs->size();
193 for (size_t i = 0; i < N; ++i) {
194 const char *overlay_dir = overlay_dirs->itemAt(i);
195 DIR *dir = opendir(overlay_dir);
196 if (dir == NULL) {
197 return EXIT_FAILURE;
Colin Cross13221c92014-02-11 18:04:44 -0800198 }
199
Mårten Kongstad83ab0d72015-03-20 12:57:36 +0100200 struct dirent *dirent;
201 while ((dirent = readdir(dir)) != NULL) {
202 struct stat st;
203 char overlay_apk_path[PATH_MAX + 1];
204 snprintf(overlay_apk_path, PATH_MAX, "%s/%s", overlay_dir, dirent->d_name);
205 if (stat(overlay_apk_path, &st) < 0) {
206 continue;
207 }
208 if (!S_ISREG(st.st_mode)) {
209 continue;
210 }
211
212 int priority = parse_apk(overlay_apk_path, target_package_name);
213 if (priority < 0) {
214 continue;
215 }
216
217 String8 idmap_path(idmap_dir);
218 idmap_path.appendPath(flatten_path(overlay_apk_path + 1));
219 idmap_path.append("@idmap");
220
221 if (idmap_create_path(target_apk_path, overlay_apk_path, idmap_path.string()) != 0) {
222 ALOGE("error: failed to create idmap for target=%s overlay=%s idmap=%s\n",
223 target_apk_path, overlay_apk_path, idmap_path.string());
224 continue;
225 }
226
227 Overlay overlay(String8(overlay_apk_path), idmap_path, priority);
228 overlayVector.add(overlay);
Colin Cross13221c92014-02-11 18:04:44 -0800229 }
230
Mårten Kongstad83ab0d72015-03-20 12:57:36 +0100231 closedir(dir);
Colin Cross13221c92014-02-11 18:04:44 -0800232 }
233
Colin Cross13221c92014-02-11 18:04:44 -0800234 if (!writePackagesList(filename.string(), overlayVector)) {
235 return EXIT_FAILURE;
236 }
237
238 return EXIT_SUCCESS;
239}