OSDN Git Service

am f5e67474: Allow overlays to configure ConnectivityService\'s network sampling...
[android-x86/frameworks-base.git] / cmds / idmap / idmap.cpp
1 #include "idmap.h"
2
3 #include <private/android_filesystem_config.h> // for AID_SYSTEM
4
5 #include <stdlib.h>
6 #include <string.h>
7
8 namespace {
9     const char *usage = "NAME\n\
10       idmap - create or display idmap files\n\
11 \n\
12 SYNOPSIS \n\
13       idmap --help \n\
14       idmap --fd target overlay fd \n\
15       idmap --path target overlay idmap \n\
16       idmap --scan dir-to-scan target-to-look-for target dir-to-hold-idmaps \n\
17       idmap --inspect idmap \n\
18 \n\
19 DESCRIPTION \n\
20       Idmap files play an integral part in the runtime resource overlay framework. An idmap \n\
21       file contains a mapping of resource identifiers between overlay package and its target \n\
22       package; this mapping is used during resource lookup. Idmap files also act as control \n\
23       files by their existence: if not present, the corresponding overlay package is ignored \n\
24       when the resource context is created. \n\
25 \n\
26       Idmap files are stored in /data/resource-cache. For each pair (target package, overlay \n\
27       package), there exists exactly one idmap file, or none if the overlay should not be used. \n\
28 \n\
29 NOMENCLATURE \n\
30       target: the original, non-overlay, package. Each target package may be associated with \n\
31               any number of overlay packages. \n\
32 \n\
33       overlay: an overlay package. Each overlay package is associated with exactly one target \n\
34                package, specified in the overlay's manifest using the <overlay target=\"...\"/> \n\
35                tag. \n\
36 \n\
37 OPTIONS \n\
38       --help: display this help \n\
39 \n\
40       --fd: create idmap for target package 'target' (path to apk) and overlay package 'overlay' \n\
41             (path to apk); write results to file descriptor 'fd' (integer). This invocation \n\
42             version is intended to be used by a parent process with higher privileges to call \n\
43             idmap in a controlled way: the parent will open a suitable file descriptor, fork, \n\
44             drop its privileges and exec. This tool will continue execution without the extra \n\
45             privileges, but still have write access to a file it could not have opened on its \n\
46             own. \n\
47 \n\
48       --path: create idmap for target package 'target' (path to apk) and overlay package \n\
49               'overlay' (path to apk); write results to 'idmap' (path). \n\
50 \n\
51       --scan: non-recursively search directory 'dir-to-scan' (path) for overlay packages with \n\
52               target package 'target-to-look-for' (package name) present at 'target' (path to \n\
53               apk). For each overlay package found, create an idmap file in 'dir-to-hold-idmaps' \n\
54               (path). \n\
55 \n\
56       --inspect: decode the binary format of 'idmap' (path) and display the contents in a \n\
57                  debug-friendly format. \n\
58 \n\
59 EXAMPLES \n\
60       Create an idmap file: \n\
61 \n\
62       $ adb shell idmap --path /system/app/target.apk \\ \n\
63                                /vendor/overlay/overlay.apk \\ \n\
64                                /data/resource-cache/vendor@overlay@overlay.apk@idmap \n\
65 \n\
66       Display an idmap file: \n\
67 \n\
68       $ adb shell idmap --inspect /data/resource-cache/vendor@overlay@overlay.apk@idmap \n\
69       SECTION      ENTRY        VALUE      OFFSET    COMMENT \n\
70       IDMAP HEADER magic        0x706d6469 0x0 \n\
71                    base crc     0x484aa77f 0x1 \n\
72                    overlay crc  0x03c66fa5 0x2 \n\
73                    base path    .......... 0x03-0x42 /system/app/target.apk \n\
74                    overlay path .......... 0x43-0x82 /vendor/overlay/overlay.apk \n\
75       DATA HEADER  types count  0x00000003 0x83 \n\
76                    padding      0x00000000 0x84 \n\
77                    type offset  0x00000004 0x85      absolute offset 0x87, xml \n\
78                    type offset  0x00000007 0x86      absolute offset 0x8a, string \n\
79       DATA BLOCK   entry count  0x00000001 0x87 \n\
80                    entry offset 0x00000000 0x88 \n\
81                    entry        0x7f020000 0x89      xml/integer \n\
82       DATA BLOCK   entry count  0x00000002 0x8a \n\
83                    entry offset 0x00000000 0x8b \n\
84                    entry        0x7f030000 0x8c      string/str \n\
85                    entry        0x7f030001 0x8d      string/str2 \n\
86 \n\
87       In this example, the overlay package provides three alternative resource values:\n\
88       xml/integer, string/str and string/str2.\n\
89 \n\
90 NOTES \n\
91       This tool and its expected invocation from installd is modelled on dexopt.";
92
93     bool verify_directory_readable(const char *path)
94     {
95         return access(path, R_OK | X_OK) == 0;
96     }
97
98     bool verify_directory_writable(const char *path)
99     {
100         return access(path, W_OK) == 0;
101     }
102
103     bool verify_file_readable(const char *path)
104     {
105         return access(path, R_OK) == 0;
106     }
107
108     bool verify_root_or_system()
109     {
110         uid_t uid = getuid();
111         gid_t gid = getgid();
112
113         return (uid == 0 && gid == 0) || (uid == AID_SYSTEM && gid == AID_SYSTEM);
114     }
115
116     int maybe_create_fd(const char *target_apk_path, const char *overlay_apk_path,
117             const char *idmap_str)
118     {
119         // anyone (not just root or system) may do --fd -- the file has
120         // already been opened by someone else on our behalf
121
122         char *endptr;
123         int idmap_fd = strtol(idmap_str, &endptr, 10);
124         if (*endptr != '\0') {
125             fprintf(stderr, "error: failed to parse file descriptor argument %s\n", idmap_str);
126             return -1;
127         }
128
129         if (!verify_file_readable(target_apk_path)) {
130             ALOGD("error: failed to read apk %s: %s\n", target_apk_path, strerror(errno));
131             return -1;
132         }
133
134         if (!verify_file_readable(overlay_apk_path)) {
135             ALOGD("error: failed to read apk %s: %s\n", overlay_apk_path, strerror(errno));
136             return -1;
137         }
138
139         return idmap_create_fd(target_apk_path, overlay_apk_path, idmap_fd);
140     }
141
142     int maybe_create_path(const char *target_apk_path, const char *overlay_apk_path,
143             const char *idmap_path)
144     {
145         if (!verify_root_or_system()) {
146             fprintf(stderr, "error: permission denied: not user root or user system\n");
147             return -1;
148         }
149
150         if (!verify_file_readable(target_apk_path)) {
151             ALOGD("error: failed to read apk %s: %s\n", target_apk_path, strerror(errno));
152             return -1;
153         }
154
155         if (!verify_file_readable(overlay_apk_path)) {
156             ALOGD("error: failed to read apk %s: %s\n", overlay_apk_path, strerror(errno));
157             return -1;
158         }
159
160         return idmap_create_path(target_apk_path, overlay_apk_path, idmap_path);
161     }
162
163     int maybe_scan(const char *overlay_dir, const char *target_package_name,
164             const char *target_apk_path, const char *idmap_dir)
165     {
166         if (!verify_root_or_system()) {
167             fprintf(stderr, "error: permission denied: not user root or user system\n");
168             return -1;
169         }
170
171         if (!verify_directory_readable(overlay_dir)) {
172             ALOGD("error: no read access to %s: %s\n", overlay_dir, strerror(errno));
173             return -1;
174         }
175
176         if (!verify_file_readable(target_apk_path)) {
177             ALOGD("error: failed to read apk %s: %s\n", target_apk_path, strerror(errno));
178             return -1;
179         }
180
181         if (!verify_directory_writable(idmap_dir)) {
182             ALOGD("error: no write access to %s: %s\n", idmap_dir, strerror(errno));
183             return -1;
184         }
185
186         return idmap_scan(overlay_dir, target_package_name, target_apk_path, idmap_dir);
187     }
188
189     int maybe_inspect(const char *idmap_path)
190     {
191         // anyone (not just root or system) may do --inspect
192         if (!verify_file_readable(idmap_path)) {
193             ALOGD("error: failed to read idmap %s: %s\n", idmap_path, strerror(errno));
194             return -1;
195         }
196         return idmap_inspect(idmap_path);
197     }
198 }
199
200 int main(int argc, char **argv)
201 {
202 #if 0
203     {
204         char buf[1024];
205         buf[0] = '\0';
206         for (int i = 0; i < argc; ++i) {
207             strncat(buf, argv[i], sizeof(buf) - 1);
208             strncat(buf, " ", sizeof(buf) - 1);
209         }
210         ALOGD("%s:%d: uid=%d gid=%d argv=%s\n", __FILE__, __LINE__, getuid(), getgid(), buf);
211     }
212 #endif
213
214     if (argc == 2 && !strcmp(argv[1], "--help")) {
215         printf("%s\n", usage);
216         return 0;
217     }
218
219     if (argc == 5 && !strcmp(argv[1], "--fd")) {
220         return maybe_create_fd(argv[2], argv[3], argv[4]);
221     }
222
223     if (argc == 5 && !strcmp(argv[1], "--path")) {
224         return maybe_create_path(argv[2], argv[3], argv[4]);
225     }
226
227     if (argc == 6 && !strcmp(argv[1], "--scan")) {
228         return maybe_scan(argv[2], argv[3], argv[4], argv[5]);
229     }
230
231     if (argc == 3 && !strcmp(argv[1], "--inspect")) {
232         return maybe_inspect(argv[2]);
233     }
234
235     fprintf(stderr, "Usage: don't use this (cf dexopt usage).\n");
236     return EXIT_FAILURE;
237 }