blob: 297b3a14cb084b66813a890947ef3e0172043992 [file] [log] [blame]
Mike Lockwood94afecf2012-10-24 10:45:23 -07001/*
2** Copyright 2008, The Android Open Source Project
3**
Jeff Sharkey19803802015-04-07 12:44:51 -07004** 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
Mike Lockwood94afecf2012-10-24 10:45:23 -07007**
Jeff Sharkey19803802015-04-07 12:44:51 -07008** http://www.apache.org/licenses/LICENSE-2.0
Mike Lockwood94afecf2012-10-24 10:45:23 -07009**
Jeff Sharkey19803802015-04-07 12:44:51 -070010** 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
Mike Lockwood94afecf2012-10-24 10:45:23 -070014** limitations under the License.
15*/
16
Jeff Sharkeye3637242015-04-08 20:56:42 -070017#include "installd.h"
18
19#include <base/logging.h>
20
Nick Kralevichd7471292013-02-28 16:59:13 -080021#include <sys/capability.h>
Elliott Hughes119b7652014-07-18 17:29:15 -070022#include <sys/prctl.h>
Stephen Smalleybd558d62013-04-16 12:16:50 -040023#include <selinux/android.h>
24#include <selinux/avc.h>
Mike Lockwood94afecf2012-10-24 10:45:23 -070025
Mike Lockwood94afecf2012-10-24 10:45:23 -070026#define BUFFER_MAX 1024 /* input buffer for commands */
Fyodor Kupolov88ce4ff2015-03-03 12:25:29 -080027#define TOKEN_MAX 16 /* max number of arguments in buffer */
Mike Lockwood94afecf2012-10-24 10:45:23 -070028#define REPLY_MAX 256 /* largest reply allowed */
29
Jeff Sharkey6fe28a02015-04-09 13:10:03 -070030static char* parse_null(char* arg) {
31 if (strcmp(arg, "!") == 0) {
32 return nullptr;
33 } else {
34 return arg;
35 }
36}
37
Chih-Hung Hsieh99d9fb12014-09-11 14:44:46 -070038static int do_ping(char **arg __unused, char reply[REPLY_MAX] __unused)
Mike Lockwood94afecf2012-10-24 10:45:23 -070039{
40 return 0;
41}
42
Chih-Hung Hsieh99d9fb12014-09-11 14:44:46 -070043static int do_install(char **arg, char reply[REPLY_MAX] __unused)
Mike Lockwood94afecf2012-10-24 10:45:23 -070044{
Jeff Sharkey6fe28a02015-04-09 13:10:03 -070045 return install(parse_null(arg[0]), arg[1], atoi(arg[2]), atoi(arg[3]), arg[4]); /* uuid, pkgname, uid, gid, seinfo */
Mike Lockwood94afecf2012-10-24 10:45:23 -070046}
47
Chih-Hung Hsieh99d9fb12014-09-11 14:44:46 -070048static int do_dexopt(char **arg, char reply[REPLY_MAX] __unused)
Mike Lockwood94afecf2012-10-24 10:45:23 -070049{
Richard Uhlerc92fb622015-03-26 15:47:38 -070050 /* apk_path, uid, is_public, pkgname, instruction_set,
51 * dexopt_needed, vm_safe_mode, debuggable, oat_dir */
52 return dexopt(arg[0], atoi(arg[1]), atoi(arg[2]), arg[3], arg[4], atoi(arg[5]),
53 atoi(arg[6]), atoi(arg[7]), arg[8]);
Mike Lockwood94afecf2012-10-24 10:45:23 -070054}
55
Bernhard Rosenkränzer7fb390d2014-11-23 22:28:26 +010056static int do_mark_boot_complete(char **arg, char reply[REPLY_MAX] __unused)
Narayan Kamath091ea772014-11-10 15:03:46 +000057{
58 return mark_boot_complete(arg[0] /* instruction set */);
59}
60
Bernhard Rosenkränzer7fb390d2014-11-23 22:28:26 +010061static int do_move_dex(char **arg, char reply[REPLY_MAX] __unused)
Mike Lockwood94afecf2012-10-24 10:45:23 -070062{
Narayan Kamath1b400322014-04-11 13:17:00 +010063 return move_dex(arg[0], arg[1], arg[2]); /* src, dst, instruction_set */
Mike Lockwood94afecf2012-10-24 10:45:23 -070064}
65
Chih-Hung Hsieh99d9fb12014-09-11 14:44:46 -070066static int do_rm_dex(char **arg, char reply[REPLY_MAX] __unused)
Mike Lockwood94afecf2012-10-24 10:45:23 -070067{
Narayan Kamath1b400322014-04-11 13:17:00 +010068 return rm_dex(arg[0], arg[1]); /* pkgname, instruction_set */
Mike Lockwood94afecf2012-10-24 10:45:23 -070069}
70
Chih-Hung Hsieh99d9fb12014-09-11 14:44:46 -070071static int do_remove(char **arg, char reply[REPLY_MAX] __unused)
Mike Lockwood94afecf2012-10-24 10:45:23 -070072{
Jeff Sharkey6fe28a02015-04-09 13:10:03 -070073 return uninstall(parse_null(arg[0]), arg[1], atoi(arg[2])); /* uuid, pkgname, userid */
Mike Lockwood94afecf2012-10-24 10:45:23 -070074}
75
Chih-Hung Hsieh99d9fb12014-09-11 14:44:46 -070076static int do_rename(char **arg, char reply[REPLY_MAX] __unused)
Mike Lockwood94afecf2012-10-24 10:45:23 -070077{
78 return renamepkg(arg[0], arg[1]); /* oldpkgname, newpkgname */
79}
80
Chih-Hung Hsieh99d9fb12014-09-11 14:44:46 -070081static int do_fixuid(char **arg, char reply[REPLY_MAX] __unused)
Mike Lockwood94afecf2012-10-24 10:45:23 -070082{
Jeff Sharkey6fe28a02015-04-09 13:10:03 -070083 return fix_uid(parse_null(arg[0]), arg[1], atoi(arg[2]), atoi(arg[3])); /* uuid, pkgname, uid, gid */
Mike Lockwood94afecf2012-10-24 10:45:23 -070084}
85
Chih-Hung Hsieh99d9fb12014-09-11 14:44:46 -070086static int do_free_cache(char **arg, char reply[REPLY_MAX] __unused) /* TODO int:free_size */
Mike Lockwood94afecf2012-10-24 10:45:23 -070087{
Jeff Sharkey6fe28a02015-04-09 13:10:03 -070088 return free_cache(parse_null(arg[0]), (int64_t)atoll(arg[1])); /* uuid, free_size */
Mike Lockwood94afecf2012-10-24 10:45:23 -070089}
90
Chih-Hung Hsieh99d9fb12014-09-11 14:44:46 -070091static int do_rm_cache(char **arg, char reply[REPLY_MAX] __unused)
Mike Lockwood94afecf2012-10-24 10:45:23 -070092{
Jeff Sharkey6fe28a02015-04-09 13:10:03 -070093 return delete_cache(parse_null(arg[0]), arg[1], atoi(arg[2])); /* uuid, pkgname, userid */
Mike Lockwood94afecf2012-10-24 10:45:23 -070094}
95
Dan Albert9e8b5282014-09-12 09:00:50 -070096static int do_rm_code_cache(char **arg, char reply[REPLY_MAX] __unused)
Jeff Sharkeyc796b682014-07-15 21:49:51 -070097{
Jeff Sharkey6fe28a02015-04-09 13:10:03 -070098 return delete_code_cache(parse_null(arg[0]), arg[1], atoi(arg[2])); /* uuid, pkgname, userid */
Jeff Sharkeyc796b682014-07-15 21:49:51 -070099}
100
Mike Lockwood94afecf2012-10-24 10:45:23 -0700101static int do_get_size(char **arg, char reply[REPLY_MAX])
102{
103 int64_t codesize = 0;
104 int64_t datasize = 0;
105 int64_t cachesize = 0;
106 int64_t asecsize = 0;
107 int res = 0;
108
Jeff Sharkey6fe28a02015-04-09 13:10:03 -0700109 /* uuid, pkgdir, userid, apkpath */
110 res = get_size(parse_null(arg[0]), arg[1], atoi(arg[2]), arg[3], arg[4], arg[5], arg[6],
111 arg[7], &codesize, &datasize, &cachesize, &asecsize);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700112
113 /*
114 * Each int64_t can take up 22 characters printed out. Make sure it
115 * doesn't go over REPLY_MAX in the future.
116 */
117 snprintf(reply, REPLY_MAX, "%" PRId64 " %" PRId64 " %" PRId64 " %" PRId64,
118 codesize, datasize, cachesize, asecsize);
119 return res;
120}
121
Chih-Hung Hsieh99d9fb12014-09-11 14:44:46 -0700122static int do_rm_user_data(char **arg, char reply[REPLY_MAX] __unused)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700123{
Jeff Sharkey6fe28a02015-04-09 13:10:03 -0700124 return delete_user_data(parse_null(arg[0]), arg[1], atoi(arg[2])); /* uuid, pkgname, userid */
Mike Lockwood94afecf2012-10-24 10:45:23 -0700125}
126
Jeff Sharkeyd7921182015-04-30 15:58:19 -0700127static int do_mv_complete_app(char **arg, char reply[REPLY_MAX] __unused)
Jeff Sharkeye3637242015-04-08 20:56:42 -0700128{
Jeff Sharkeyd7921182015-04-30 15:58:19 -0700129 // from_uuid, to_uuid, package_name, data_app_name, appid, seinfo
130 return move_complete_app(parse_null(arg[0]), parse_null(arg[1]), arg[2], arg[3], atoi(arg[4]), arg[5]);
Jeff Sharkeye3637242015-04-08 20:56:42 -0700131}
132
Chih-Hung Hsieh99d9fb12014-09-11 14:44:46 -0700133static int do_mk_user_data(char **arg, char reply[REPLY_MAX] __unused)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700134{
Jeff Sharkey6fe28a02015-04-09 13:10:03 -0700135 return make_user_data(parse_null(arg[0]), arg[1], atoi(arg[2]), atoi(arg[3]), arg[4]);
136 /* uuid, pkgname, uid, userid, seinfo */
Mike Lockwood94afecf2012-10-24 10:45:23 -0700137}
138
Chih-Hung Hsieh99d9fb12014-09-11 14:44:46 -0700139static int do_mk_user_config(char **arg, char reply[REPLY_MAX] __unused)
Robin Lee095c7632014-04-25 15:05:19 +0100140{
Robin Lee7c8bec02014-06-10 18:46:26 +0100141 return make_user_config(atoi(arg[0])); /* userid */
Robin Lee095c7632014-04-25 15:05:19 +0100142}
143
Chih-Hung Hsieh99d9fb12014-09-11 14:44:46 -0700144static int do_rm_user(char **arg, char reply[REPLY_MAX] __unused)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700145{
Jeff Sharkey6fe28a02015-04-09 13:10:03 -0700146 return delete_user(parse_null(arg[0]), atoi(arg[1])); /* uuid, userid */
Mike Lockwood94afecf2012-10-24 10:45:23 -0700147}
148
Chih-Hung Hsieh99d9fb12014-09-11 14:44:46 -0700149static int do_movefiles(char **arg __unused, char reply[REPLY_MAX] __unused)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700150{
151 return movefiles();
152}
153
Chih-Hung Hsieh99d9fb12014-09-11 14:44:46 -0700154static int do_linklib(char **arg, char reply[REPLY_MAX] __unused)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700155{
Jeff Sharkey6fe28a02015-04-09 13:10:03 -0700156 return linklib(parse_null(arg[0]), arg[1], arg[2], atoi(arg[3]));
Mike Lockwood94afecf2012-10-24 10:45:23 -0700157}
158
Chih-Hung Hsieh99d9fb12014-09-11 14:44:46 -0700159static int do_idmap(char **arg, char reply[REPLY_MAX] __unused)
Mårten Kongstad63568b12014-01-31 14:42:59 +0100160{
161 return idmap(arg[0], arg[1], atoi(arg[2]));
162}
163
Robert Craigda30dc72014-03-27 10:21:12 -0400164static int do_restorecon_data(char **arg, char reply[REPLY_MAX] __attribute__((unused)))
Robert Craige9887e42014-02-20 10:25:56 -0500165{
Jeff Sharkey6fe28a02015-04-09 13:10:03 -0700166 return restorecon_data(parse_null(arg[0]), arg[1], arg[2], atoi(arg[3]));
167 /* uuid, pkgName, seinfo, uid*/
Robert Craige9887e42014-02-20 10:25:56 -0500168}
169
Fyodor Kupolov88ce4ff2015-03-03 12:25:29 -0800170static int do_create_oat_dir(char **arg, char reply[REPLY_MAX] __unused)
171{
172 /* oat_dir, instruction_set */
173 return create_oat_dir(arg[0], arg[1]);
174}
175
176static int do_rm_package_dir(char **arg, char reply[REPLY_MAX] __unused)
177{
178 /* oat_dir */
179 return rm_package_dir(arg[0]);
Alex Light43c5d302014-07-21 12:23:48 -0700180}
181
Narayan Kamathd845c962015-06-04 13:20:27 +0100182static int do_link_file(char **arg, char reply[REPLY_MAX] __unused)
183{
184 /* relative_path, from_base, to_base */
185 return link_file(arg[0], arg[1], arg[2]);
186}
187
Mike Lockwood94afecf2012-10-24 10:45:23 -0700188struct cmdinfo {
189 const char *name;
190 unsigned numargs;
191 int (*func)(char **arg, char reply[REPLY_MAX]);
192};
193
194struct cmdinfo cmds[] = {
195 { "ping", 0, do_ping },
Jeff Sharkey6fe28a02015-04-09 13:10:03 -0700196 { "install", 5, do_install },
Richard Uhlerc92fb622015-03-26 15:47:38 -0700197 { "dexopt", 9, do_dexopt },
Narayan Kamath091ea772014-11-10 15:03:46 +0000198 { "markbootcomplete", 1, do_mark_boot_complete },
Narayan Kamath1b400322014-04-11 13:17:00 +0100199 { "movedex", 3, do_move_dex },
200 { "rmdex", 2, do_rm_dex },
Jeff Sharkey6fe28a02015-04-09 13:10:03 -0700201 { "remove", 3, do_remove },
Mike Lockwood94afecf2012-10-24 10:45:23 -0700202 { "rename", 2, do_rename },
Jeff Sharkey6fe28a02015-04-09 13:10:03 -0700203 { "fixuid", 4, do_fixuid },
204 { "freecache", 2, do_free_cache },
205 { "rmcache", 3, do_rm_cache },
206 { "rmcodecache", 3, do_rm_code_cache },
207 { "getsize", 8, do_get_size },
208 { "rmuserdata", 3, do_rm_user_data },
Jeff Sharkeyd7921182015-04-30 15:58:19 -0700209 { "mvcompleteapp", 6, do_mv_complete_app },
Mike Lockwood94afecf2012-10-24 10:45:23 -0700210 { "movefiles", 0, do_movefiles },
Jeff Sharkey6fe28a02015-04-09 13:10:03 -0700211 { "linklib", 4, do_linklib },
212 { "mkuserdata", 5, do_mk_user_data },
Robin Lee7c8bec02014-06-10 18:46:26 +0100213 { "mkuserconfig", 1, do_mk_user_config },
Jeff Sharkey6fe28a02015-04-09 13:10:03 -0700214 { "rmuser", 2, do_rm_user },
Mårten Kongstad63568b12014-01-31 14:42:59 +0100215 { "idmap", 3, do_idmap },
Jeff Sharkey6fe28a02015-04-09 13:10:03 -0700216 { "restorecondata", 4, do_restorecon_data },
Fyodor Kupolov88ce4ff2015-03-03 12:25:29 -0800217 { "createoatdir", 2, do_create_oat_dir },
Narayan Kamathd845c962015-06-04 13:20:27 +0100218 { "rmpackagedir", 1, do_rm_package_dir },
219 { "linkfile", 3, do_link_file }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700220};
221
222static int readx(int s, void *_buf, int count)
223{
Jeff Sharkey19803802015-04-07 12:44:51 -0700224 char *buf = (char *) _buf;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700225 int n = 0, r;
226 if (count < 0) return -1;
227 while (n < count) {
228 r = read(s, buf + n, count - n);
229 if (r < 0) {
230 if (errno == EINTR) continue;
231 ALOGE("read error: %s\n", strerror(errno));
232 return -1;
233 }
234 if (r == 0) {
235 ALOGE("eof\n");
236 return -1; /* EOF */
237 }
238 n += r;
239 }
240 return 0;
241}
242
243static int writex(int s, const void *_buf, int count)
244{
Jeff Sharkey19803802015-04-07 12:44:51 -0700245 const char *buf = (const char *) _buf;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700246 int n = 0, r;
247 if (count < 0) return -1;
248 while (n < count) {
249 r = write(s, buf + n, count - n);
250 if (r < 0) {
251 if (errno == EINTR) continue;
252 ALOGE("write error: %s\n", strerror(errno));
253 return -1;
254 }
255 n += r;
256 }
257 return 0;
258}
259
260
261/* Tokenize the command buffer, locate a matching command,
262 * ensure that the required number of arguments are provided,
263 * call the function(), return the result.
264 */
265static int execute(int s, char cmd[BUFFER_MAX])
266{
267 char reply[REPLY_MAX];
268 char *arg[TOKEN_MAX+1];
269 unsigned i;
270 unsigned n = 0;
271 unsigned short count;
272 int ret = -1;
273
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700274 // ALOGI("execute('%s')\n", cmd);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700275
276 /* default reply is "" */
277 reply[0] = 0;
278
279 /* n is number of args (not counting arg[0]) */
280 arg[0] = cmd;
281 while (*cmd) {
282 if (isspace(*cmd)) {
283 *cmd++ = 0;
284 n++;
285 arg[n] = cmd;
286 if (n == TOKEN_MAX) {
287 ALOGE("too many arguments\n");
288 goto done;
289 }
290 }
Serguei Katkov62bb3852014-10-29 19:38:01 +0600291 if (*cmd) {
292 cmd++;
293 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700294 }
295
296 for (i = 0; i < sizeof(cmds) / sizeof(cmds[0]); i++) {
297 if (!strcmp(cmds[i].name,arg[0])) {
298 if (n != cmds[i].numargs) {
299 ALOGE("%s requires %d arguments (%d given)\n",
300 cmds[i].name, cmds[i].numargs, n);
301 } else {
302 ret = cmds[i].func(arg + 1, reply);
303 }
304 goto done;
305 }
306 }
307 ALOGE("unsupported command '%s'\n", arg[0]);
308
309done:
310 if (reply[0]) {
311 n = snprintf(cmd, BUFFER_MAX, "%d %s", ret, reply);
312 } else {
313 n = snprintf(cmd, BUFFER_MAX, "%d", ret);
314 }
315 if (n > BUFFER_MAX) n = BUFFER_MAX;
316 count = n;
317
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700318 // ALOGI("reply: '%s'\n", cmd);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700319 if (writex(s, &count, sizeof(count))) return -1;
320 if (writex(s, cmd, count)) return -1;
321 return 0;
322}
323
324/**
325 * Initialize all the global variables that are used elsewhere. Returns 0 upon
326 * success and -1 on error.
327 */
328void free_globals() {
329 size_t i;
330
331 for (i = 0; i < android_system_dirs.count; i++) {
332 if (android_system_dirs.dirs[i].path != NULL) {
333 free(android_system_dirs.dirs[i].path);
334 }
335 }
336
337 free(android_system_dirs.dirs);
338}
339
340int initialize_globals() {
341 // Get the android data directory.
342 if (get_path_from_env(&android_data_dir, "ANDROID_DATA") < 0) {
343 return -1;
344 }
345
346 // Get the android app directory.
347 if (copy_and_append(&android_app_dir, &android_data_dir, APP_SUBDIR) < 0) {
348 return -1;
349 }
350
351 // Get the android protected app directory.
352 if (copy_and_append(&android_app_private_dir, &android_data_dir, PRIVATE_APP_SUBDIR) < 0) {
353 return -1;
354 }
355
356 // Get the android app native library directory.
357 if (copy_and_append(&android_app_lib_dir, &android_data_dir, APP_LIB_SUBDIR) < 0) {
358 return -1;
359 }
360
361 // Get the sd-card ASEC mount point.
362 if (get_path_from_env(&android_asec_dir, "ASEC_MOUNTPOINT") < 0) {
363 return -1;
364 }
365
366 // Get the android media directory.
367 if (copy_and_append(&android_media_dir, &android_data_dir, MEDIA_SUBDIR) < 0) {
368 return -1;
369 }
370
Jeff Sharkeye23a1322015-04-06 16:19:39 -0700371 // Get the android external app directory.
Jeff Sharkey19803802015-04-07 12:44:51 -0700372 if (get_path_from_string(&android_mnt_expand_dir, "/mnt/expand/") < 0) {
Jeff Sharkeye23a1322015-04-06 16:19:39 -0700373 return -1;
374 }
375
Mike Lockwood94afecf2012-10-24 10:45:23 -0700376 // Take note of the system and vendor directories.
Jeff Sharkey770180a2014-09-08 17:14:26 -0700377 android_system_dirs.count = 4;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700378
Jeff Sharkey19803802015-04-07 12:44:51 -0700379 android_system_dirs.dirs = (dir_rec_t*) calloc(android_system_dirs.count, sizeof(dir_rec_t));
Mike Lockwood94afecf2012-10-24 10:45:23 -0700380 if (android_system_dirs.dirs == NULL) {
381 ALOGE("Couldn't allocate array for dirs; aborting\n");
382 return -1;
383 }
384
Jeff Sharkey770180a2014-09-08 17:14:26 -0700385 dir_rec_t android_root_dir;
386 if (get_path_from_env(&android_root_dir, "ANDROID_ROOT") < 0) {
387 ALOGE("Missing ANDROID_ROOT; aborting\n");
Mike Lockwood94afecf2012-10-24 10:45:23 -0700388 return -1;
389 }
390
Jeff Sharkey770180a2014-09-08 17:14:26 -0700391 android_system_dirs.dirs[0].path = build_string2(android_root_dir.path, APP_SUBDIR);
392 android_system_dirs.dirs[0].len = strlen(android_system_dirs.dirs[0].path);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700393
Jeff Sharkey770180a2014-09-08 17:14:26 -0700394 android_system_dirs.dirs[1].path = build_string2(android_root_dir.path, PRIV_APP_SUBDIR);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700395 android_system_dirs.dirs[1].len = strlen(android_system_dirs.dirs[1].path);
396
Jeff Sharkey19803802015-04-07 12:44:51 -0700397 android_system_dirs.dirs[2].path = strdup("/vendor/app/");
Jeff Sharkey770180a2014-09-08 17:14:26 -0700398 android_system_dirs.dirs[2].len = strlen(android_system_dirs.dirs[2].path);
399
Jeff Sharkey19803802015-04-07 12:44:51 -0700400 android_system_dirs.dirs[3].path = strdup("/oem/app/");
Jeff Sharkey770180a2014-09-08 17:14:26 -0700401 android_system_dirs.dirs[3].len = strlen(android_system_dirs.dirs[3].path);
402
Mike Lockwood94afecf2012-10-24 10:45:23 -0700403 return 0;
404}
405
406int initialize_directories() {
407 int res = -1;
408
409 // Read current filesystem layout version to handle upgrade paths
410 char version_path[PATH_MAX];
411 snprintf(version_path, PATH_MAX, "%s.layout_version", android_data_dir.path);
412
413 int oldVersion;
414 if (fs_read_atomic_int(version_path, &oldVersion) == -1) {
415 oldVersion = 0;
416 }
417 int version = oldVersion;
418
419 // /data/user
420 char *user_data_dir = build_string2(android_data_dir.path, SECONDARY_USER_PREFIX);
421 // /data/data
422 char *legacy_data_dir = build_string2(android_data_dir.path, PRIMARY_USER_PREFIX);
423 // /data/user/0
424 char *primary_data_dir = build_string3(android_data_dir.path, SECONDARY_USER_PREFIX, "0");
425 if (!user_data_dir || !legacy_data_dir || !primary_data_dir) {
426 goto fail;
427 }
428
429 // Make the /data/user directory if necessary
430 if (access(user_data_dir, R_OK) < 0) {
431 if (mkdir(user_data_dir, 0711) < 0) {
432 goto fail;
433 }
434 if (chown(user_data_dir, AID_SYSTEM, AID_SYSTEM) < 0) {
435 goto fail;
436 }
437 if (chmod(user_data_dir, 0711) < 0) {
438 goto fail;
439 }
440 }
441 // Make the /data/user/0 symlink to /data/data if necessary
442 if (access(primary_data_dir, R_OK) < 0) {
443 if (symlink(legacy_data_dir, primary_data_dir)) {
444 goto fail;
445 }
446 }
447
448 if (version == 0) {
449 // Introducing multi-user, so migrate /data/media contents into /data/media/0
450 ALOGD("Upgrading /data/media for multi-user");
451
452 // Ensure /data/media
453 if (fs_prepare_dir(android_media_dir.path, 0770, AID_MEDIA_RW, AID_MEDIA_RW) == -1) {
454 goto fail;
455 }
456
457 // /data/media.tmp
458 char media_tmp_dir[PATH_MAX];
459 snprintf(media_tmp_dir, PATH_MAX, "%smedia.tmp", android_data_dir.path);
460
461 // Only copy when upgrade not already in progress
462 if (access(media_tmp_dir, F_OK) == -1) {
463 if (rename(android_media_dir.path, media_tmp_dir) == -1) {
464 ALOGE("Failed to move legacy media path: %s", strerror(errno));
465 goto fail;
466 }
467 }
468
469 // Create /data/media again
470 if (fs_prepare_dir(android_media_dir.path, 0770, AID_MEDIA_RW, AID_MEDIA_RW) == -1) {
471 goto fail;
472 }
473
Stephen Smalley26288202014-02-07 09:16:46 -0500474 if (selinux_android_restorecon(android_media_dir.path, 0)) {
Stephen Smalley47a35182013-12-17 16:04:20 -0500475 goto fail;
476 }
477
Mike Lockwood94afecf2012-10-24 10:45:23 -0700478 // /data/media/0
479 char owner_media_dir[PATH_MAX];
480 snprintf(owner_media_dir, PATH_MAX, "%s0", android_media_dir.path);
481
482 // Move any owner data into place
483 if (access(media_tmp_dir, F_OK) == 0) {
484 if (rename(media_tmp_dir, owner_media_dir) == -1) {
485 ALOGE("Failed to move owner media path: %s", strerror(errno));
486 goto fail;
487 }
488 }
489
490 // Ensure media directories for any existing users
491 DIR *dir;
492 struct dirent *dirent;
493 char user_media_dir[PATH_MAX];
494
495 dir = opendir(user_data_dir);
496 if (dir != NULL) {
497 while ((dirent = readdir(dir))) {
498 if (dirent->d_type == DT_DIR) {
499 const char *name = dirent->d_name;
500
501 // skip "." and ".."
502 if (name[0] == '.') {
503 if (name[1] == 0) continue;
504 if ((name[1] == '.') && (name[2] == 0)) continue;
505 }
506
507 // /data/media/<user_id>
508 snprintf(user_media_dir, PATH_MAX, "%s%s", android_media_dir.path, name);
509 if (fs_prepare_dir(user_media_dir, 0770, AID_MEDIA_RW, AID_MEDIA_RW) == -1) {
510 goto fail;
511 }
512 }
513 }
514 closedir(dir);
515 }
516
517 version = 1;
518 }
519
520 // /data/media/obb
521 char media_obb_dir[PATH_MAX];
522 snprintf(media_obb_dir, PATH_MAX, "%sobb", android_media_dir.path);
523
524 if (version == 1) {
525 // Introducing /data/media/obb for sharing OBB across users; migrate
526 // any existing OBB files from owner.
527 ALOGD("Upgrading to shared /data/media/obb");
528
529 // /data/media/0/Android/obb
530 char owner_obb_path[PATH_MAX];
531 snprintf(owner_obb_path, PATH_MAX, "%s0/Android/obb", android_media_dir.path);
532
533 // Only move if target doesn't already exist
534 if (access(media_obb_dir, F_OK) != 0 && access(owner_obb_path, F_OK) == 0) {
535 if (rename(owner_obb_path, media_obb_dir) == -1) {
536 ALOGE("Failed to move OBB from owner: %s", strerror(errno));
537 goto fail;
538 }
539 }
540
541 version = 2;
542 }
543
Jeff Sharkey41ea4242015-04-09 11:34:03 -0700544 if (ensure_media_user_dirs(nullptr, 0) == -1) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700545 ALOGE("Failed to setup media for user 0");
546 goto fail;
547 }
548 if (fs_prepare_dir(media_obb_dir, 0770, AID_MEDIA_RW, AID_MEDIA_RW) == -1) {
549 goto fail;
550 }
551
Robin Lee07053fc2014-04-29 19:42:01 +0100552 if (ensure_config_user_dirs(0) == -1) {
553 ALOGE("Failed to setup misc for user 0");
554 goto fail;
555 }
556
Robin Lee095c7632014-04-25 15:05:19 +0100557 if (version == 2) {
558 ALOGD("Upgrading to /data/misc/user directories");
559
Robin Lee60fd3fe2014-10-07 16:55:02 +0100560 char misc_dir[PATH_MAX];
561 snprintf(misc_dir, PATH_MAX, "%smisc", android_data_dir.path);
562
563 char keychain_added_dir[PATH_MAX];
564 snprintf(keychain_added_dir, PATH_MAX, "%s/keychain/cacerts-added", misc_dir);
565
566 char keychain_removed_dir[PATH_MAX];
567 snprintf(keychain_removed_dir, PATH_MAX, "%s/keychain/cacerts-removed", misc_dir);
568
Robin Lee095c7632014-04-25 15:05:19 +0100569 DIR *dir;
570 struct dirent *dirent;
Robin Lee095c7632014-04-25 15:05:19 +0100571 dir = opendir(user_data_dir);
572 if (dir != NULL) {
573 while ((dirent = readdir(dir))) {
Robin Lee60fd3fe2014-10-07 16:55:02 +0100574 const char *name = dirent->d_name;
Robin Lee095c7632014-04-25 15:05:19 +0100575
Robin Lee60fd3fe2014-10-07 16:55:02 +0100576 // skip "." and ".."
577 if (name[0] == '.') {
578 if (name[1] == 0) continue;
579 if ((name[1] == '.') && (name[2] == 0)) continue;
580 }
581
582 uint32_t user_id = atoi(name);
583
584 // /data/misc/user/<user_id>
585 if (ensure_config_user_dirs(user_id) == -1) {
586 goto fail;
587 }
588
589 char misc_added_dir[PATH_MAX];
590 snprintf(misc_added_dir, PATH_MAX, "%s/user/%s/cacerts-added", misc_dir, name);
591
592 char misc_removed_dir[PATH_MAX];
593 snprintf(misc_removed_dir, PATH_MAX, "%s/user/%s/cacerts-removed", misc_dir, name);
594
595 uid_t uid = multiuser_get_uid(user_id, AID_SYSTEM);
596 gid_t gid = uid;
597 if (access(keychain_added_dir, F_OK) == 0) {
598 if (copy_dir_files(keychain_added_dir, misc_added_dir, uid, gid) != 0) {
599 ALOGE("Some files failed to copy");
Robin Lee095c7632014-04-25 15:05:19 +0100600 }
Robin Lee60fd3fe2014-10-07 16:55:02 +0100601 }
602 if (access(keychain_removed_dir, F_OK) == 0) {
603 if (copy_dir_files(keychain_removed_dir, misc_removed_dir, uid, gid) != 0) {
604 ALOGE("Some files failed to copy");
Robin Lee095c7632014-04-25 15:05:19 +0100605 }
606 }
607 }
608 closedir(dir);
Robin Lee095c7632014-04-25 15:05:19 +0100609
Robin Lee60fd3fe2014-10-07 16:55:02 +0100610 if (access(keychain_added_dir, F_OK) == 0) {
611 delete_dir_contents(keychain_added_dir, 1, 0);
Robin Lee07053fc2014-04-29 19:42:01 +0100612 }
Robin Lee60fd3fe2014-10-07 16:55:02 +0100613 if (access(keychain_removed_dir, F_OK) == 0) {
614 delete_dir_contents(keychain_removed_dir, 1, 0);
Robin Lee07053fc2014-04-29 19:42:01 +0100615 }
616 }
617
618 version = 3;
Robin Lee095c7632014-04-25 15:05:19 +0100619 }
620
Mike Lockwood94afecf2012-10-24 10:45:23 -0700621 // Persist layout version if changed
622 if (version != oldVersion) {
623 if (fs_write_atomic_int(version_path, version) == -1) {
624 ALOGE("Failed to save version to %s: %s", version_path, strerror(errno));
625 goto fail;
626 }
627 }
628
629 // Success!
630 res = 0;
631
632fail:
633 free(user_data_dir);
634 free(legacy_data_dir);
635 free(primary_data_dir);
636 return res;
637}
638
Stephen Smalley7abb52b2014-03-26 09:30:37 -0400639static int log_callback(int type, const char *fmt, ...) {
640 va_list ap;
641 int priority;
642
643 switch (type) {
644 case SELINUX_WARNING:
645 priority = ANDROID_LOG_WARN;
646 break;
647 case SELINUX_INFO:
648 priority = ANDROID_LOG_INFO;
649 break;
650 default:
651 priority = ANDROID_LOG_ERROR;
652 break;
653 }
654 va_start(ap, fmt);
655 LOG_PRI_VA(priority, "SELinux", fmt, ap);
656 va_end(ap);
657 return 0;
658}
659
Jeff Sharkeye3637242015-04-08 20:56:42 -0700660int main(const int argc __unused, char *argv[]) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700661 char buf[BUFFER_MAX];
662 struct sockaddr addr;
663 socklen_t alen;
Chih-Hung Hsieh99d9fb12014-09-11 14:44:46 -0700664 int lsocket, s;
Stephen Smalleybd558d62013-04-16 12:16:50 -0400665 int selinux_enabled = (is_selinux_enabled() > 0);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700666
Jeff Sharkeye3637242015-04-08 20:56:42 -0700667 setenv("ANDROID_LOG_TAGS", "*:v", 1);
668 android::base::InitLogging(argv);
669
Mike Lockwood94afecf2012-10-24 10:45:23 -0700670 ALOGI("installd firing up\n");
671
Stephen Smalley7abb52b2014-03-26 09:30:37 -0400672 union selinux_callback cb;
673 cb.func_log = log_callback;
674 selinux_set_callback(SELINUX_CB_LOG, cb);
675
Mike Lockwood94afecf2012-10-24 10:45:23 -0700676 if (initialize_globals() < 0) {
677 ALOGE("Could not initialize globals; exiting.\n");
678 exit(1);
679 }
680
681 if (initialize_directories() < 0) {
682 ALOGE("Could not create directories; exiting.\n");
683 exit(1);
684 }
685
Stephen Smalleybd558d62013-04-16 12:16:50 -0400686 if (selinux_enabled && selinux_status_open(true) < 0) {
687 ALOGE("Could not open selinux status; exiting.\n");
688 exit(1);
689 }
690
Mike Lockwood94afecf2012-10-24 10:45:23 -0700691 lsocket = android_get_control_socket(SOCKET_PATH);
692 if (lsocket < 0) {
693 ALOGE("Failed to get socket from environment: %s\n", strerror(errno));
694 exit(1);
695 }
696 if (listen(lsocket, 5)) {
697 ALOGE("Listen on socket failed: %s\n", strerror(errno));
698 exit(1);
699 }
700 fcntl(lsocket, F_SETFD, FD_CLOEXEC);
701
702 for (;;) {
703 alen = sizeof(addr);
704 s = accept(lsocket, &addr, &alen);
705 if (s < 0) {
706 ALOGE("Accept failed: %s\n", strerror(errno));
707 continue;
708 }
709 fcntl(s, F_SETFD, FD_CLOEXEC);
710
711 ALOGI("new connection\n");
712 for (;;) {
713 unsigned short count;
714 if (readx(s, &count, sizeof(count))) {
715 ALOGE("failed to read size\n");
716 break;
717 }
718 if ((count < 1) || (count >= BUFFER_MAX)) {
719 ALOGE("invalid size %d\n", count);
720 break;
721 }
722 if (readx(s, buf, count)) {
723 ALOGE("failed to read command\n");
724 break;
725 }
726 buf[count] = 0;
Stephen Smalleybd558d62013-04-16 12:16:50 -0400727 if (selinux_enabled && selinux_status_updated() > 0) {
728 selinux_android_seapp_context_reload();
729 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700730 if (execute(s, buf)) break;
731 }
732 ALOGI("closing connection\n");
733 close(s);
734 }
735
736 return 0;
737}