blob: d388977e8e2ff0d3db9e0278d83680bf2a68eded [file] [log] [blame]
Colin Cross13221c92014-02-11 18:04:44 -08001#include "idmap.h"
2
3#include <private/android_filesystem_config.h> // for AID_SYSTEM
4
5#include <stdlib.h>
6#include <string.h>
7
8namespace {
9 const char *usage = "NAME\n\
10 idmap - create or display idmap files\n\
11\n\
12SYNOPSIS \n\
13 idmap --help \n\
14 idmap --fd target overlay fd \n\
15 idmap --path target overlay idmap \n\
Colin Cross13221c92014-02-11 18:04:44 -080016 idmap --inspect idmap \n\
17\n\
18DESCRIPTION \n\
19 Idmap files play an integral part in the runtime resource overlay framework. An idmap \n\
20 file contains a mapping of resource identifiers between overlay package and its target \n\
21 package; this mapping is used during resource lookup. Idmap files also act as control \n\
22 files by their existence: if not present, the corresponding overlay package is ignored \n\
23 when the resource context is created. \n\
24\n\
25 Idmap files are stored in /data/resource-cache. For each pair (target package, overlay \n\
26 package), there exists exactly one idmap file, or none if the overlay should not be used. \n\
27\n\
28NOMENCLATURE \n\
29 target: the original, non-overlay, package. Each target package may be associated with \n\
30 any number of overlay packages. \n\
31\n\
32 overlay: an overlay package. Each overlay package is associated with exactly one target \n\
33 package, specified in the overlay's manifest using the <overlay target=\"...\"/> \n\
34 tag. \n\
35\n\
36OPTIONS \n\
37 --help: display this help \n\
38\n\
39 --fd: create idmap for target package 'target' (path to apk) and overlay package 'overlay' \n\
40 (path to apk); write results to file descriptor 'fd' (integer). This invocation \n\
41 version is intended to be used by a parent process with higher privileges to call \n\
42 idmap in a controlled way: the parent will open a suitable file descriptor, fork, \n\
43 drop its privileges and exec. This tool will continue execution without the extra \n\
44 privileges, but still have write access to a file it could not have opened on its \n\
45 own. \n\
46\n\
47 --path: create idmap for target package 'target' (path to apk) and overlay package \n\
48 'overlay' (path to apk); write results to 'idmap' (path). \n\
49\n\
Colin Cross13221c92014-02-11 18:04:44 -080050 --inspect: decode the binary format of 'idmap' (path) and display the contents in a \n\
51 debug-friendly format. \n\
52\n\
53EXAMPLES \n\
54 Create an idmap file: \n\
55\n\
56 $ adb shell idmap --path /system/app/target.apk \\ \n\
57 /vendor/overlay/overlay.apk \\ \n\
58 /data/resource-cache/vendor@overlay@overlay.apk@idmap \n\
59\n\
60 Display an idmap file: \n\
61\n\
62 $ adb shell idmap --inspect /data/resource-cache/vendor@overlay@overlay.apk@idmap \n\
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -070063 SECTION ENTRY VALUE COMMENT \n\
64 IDMAP HEADER magic 0x706d6469 \n\
65 base crc 0xb65a383f \n\
66 overlay crc 0x7b9675e8 \n\
67 base path .......... /path/to/target.apk \n\
68 overlay path .......... /path/to/overlay.apk \n\
69 DATA HEADER target pkg 0x0000007f \n\
70 types count 0x00000003 \n\
71 DATA BLOCK target type 0x00000002 \n\
72 overlay type 0x00000002 \n\
73 entry count 0x00000001 \n\
74 entry offset 0x00000000 \n\
75 entry 0x00000000 drawable/drawable \n\
76 DATA BLOCK target type 0x00000003 \n\
77 overlay type 0x00000003 \n\
78 entry count 0x00000001 \n\
79 entry offset 0x00000000 \n\
80 entry 0x00000000 xml/integer \n\
81 DATA BLOCK target type 0x00000004 \n\
82 overlay type 0x00000004 \n\
83 entry count 0x00000001 \n\
84 entry offset 0x00000000 \n\
85 entry 0x00000000 raw/lorem_ipsum \n\
Colin Cross13221c92014-02-11 18:04:44 -080086\n\
87 In this example, the overlay package provides three alternative resource values:\n\
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -070088 drawable/drawable, xml/integer, and raw/lorem_ipsum \n\
Colin Cross13221c92014-02-11 18:04:44 -080089\n\
90NOTES \n\
91 This tool and its expected invocation from installd is modelled on dexopt.";
92
Colin Cross13221c92014-02-11 18:04:44 -080093 bool verify_file_readable(const char *path)
94 {
95 return access(path, R_OK) == 0;
96 }
97
98 bool verify_root_or_system()
99 {
100 uid_t uid = getuid();
101 gid_t gid = getgid();
102
103 return (uid == 0 && gid == 0) || (uid == AID_SYSTEM && gid == AID_SYSTEM);
104 }
105
106 int maybe_create_fd(const char *target_apk_path, const char *overlay_apk_path,
107 const char *idmap_str)
108 {
109 // anyone (not just root or system) may do --fd -- the file has
110 // already been opened by someone else on our behalf
111
112 char *endptr;
113 int idmap_fd = strtol(idmap_str, &endptr, 10);
114 if (*endptr != '\0') {
115 fprintf(stderr, "error: failed to parse file descriptor argument %s\n", idmap_str);
116 return -1;
117 }
118
119 if (!verify_file_readable(target_apk_path)) {
120 ALOGD("error: failed to read apk %s: %s\n", target_apk_path, strerror(errno));
121 return -1;
122 }
123
124 if (!verify_file_readable(overlay_apk_path)) {
125 ALOGD("error: failed to read apk %s: %s\n", overlay_apk_path, strerror(errno));
126 return -1;
127 }
128
129 return idmap_create_fd(target_apk_path, overlay_apk_path, idmap_fd);
130 }
131
132 int maybe_create_path(const char *target_apk_path, const char *overlay_apk_path,
133 const char *idmap_path)
134 {
135 if (!verify_root_or_system()) {
136 fprintf(stderr, "error: permission denied: not user root or user system\n");
137 return -1;
138 }
139
140 if (!verify_file_readable(target_apk_path)) {
141 ALOGD("error: failed to read apk %s: %s\n", target_apk_path, strerror(errno));
142 return -1;
143 }
144
145 if (!verify_file_readable(overlay_apk_path)) {
146 ALOGD("error: failed to read apk %s: %s\n", overlay_apk_path, strerror(errno));
147 return -1;
148 }
149
150 return idmap_create_path(target_apk_path, overlay_apk_path, idmap_path);
151 }
152
Colin Cross13221c92014-02-11 18:04:44 -0800153 int maybe_inspect(const char *idmap_path)
154 {
155 // anyone (not just root or system) may do --inspect
156 if (!verify_file_readable(idmap_path)) {
157 ALOGD("error: failed to read idmap %s: %s\n", idmap_path, strerror(errno));
158 return -1;
159 }
160 return idmap_inspect(idmap_path);
161 }
162}
163
164int main(int argc, char **argv)
165{
166#if 0
167 {
168 char buf[1024];
169 buf[0] = '\0';
170 for (int i = 0; i < argc; ++i) {
171 strncat(buf, argv[i], sizeof(buf) - 1);
172 strncat(buf, " ", sizeof(buf) - 1);
173 }
174 ALOGD("%s:%d: uid=%d gid=%d argv=%s\n", __FILE__, __LINE__, getuid(), getgid(), buf);
175 }
176#endif
177
178 if (argc == 2 && !strcmp(argv[1], "--help")) {
179 printf("%s\n", usage);
180 return 0;
181 }
182
183 if (argc == 5 && !strcmp(argv[1], "--fd")) {
184 return maybe_create_fd(argv[2], argv[3], argv[4]);
185 }
186
187 if (argc == 5 && !strcmp(argv[1], "--path")) {
188 return maybe_create_path(argv[2], argv[3], argv[4]);
189 }
190
Colin Cross13221c92014-02-11 18:04:44 -0800191 if (argc == 3 && !strcmp(argv[1], "--inspect")) {
192 return maybe_inspect(argv[2]);
193 }
194
195 fprintf(stderr, "Usage: don't use this (cf dexopt usage).\n");
196 return EXIT_FAILURE;
197}