blob: 3a861812f48fa1857fb427c2a20bcb95e1894836 [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 Sharkeye3637242015-04-08 20:56:42 -0700127static int do_mv_user_data(char **arg, char reply[REPLY_MAX] __unused)
128{
129 // from_uuid, to_uuid, pkgname, appid, seinfo
130 return move_user_data(parse_null(arg[0]), parse_null(arg[1]), arg[2], atoi(arg[3]), arg[4]);
131}
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
Mike Lockwood94afecf2012-10-24 10:45:23 -0700182struct cmdinfo {
183 const char *name;
184 unsigned numargs;
185 int (*func)(char **arg, char reply[REPLY_MAX]);
186};
187
188struct cmdinfo cmds[] = {
189 { "ping", 0, do_ping },
Jeff Sharkey6fe28a02015-04-09 13:10:03 -0700190 { "install", 5, do_install },
Richard Uhlerc92fb622015-03-26 15:47:38 -0700191 { "dexopt", 9, do_dexopt },
Narayan Kamath091ea772014-11-10 15:03:46 +0000192 { "markbootcomplete", 1, do_mark_boot_complete },
Narayan Kamath1b400322014-04-11 13:17:00 +0100193 { "movedex", 3, do_move_dex },
194 { "rmdex", 2, do_rm_dex },
Jeff Sharkey6fe28a02015-04-09 13:10:03 -0700195 { "remove", 3, do_remove },
Mike Lockwood94afecf2012-10-24 10:45:23 -0700196 { "rename", 2, do_rename },
Jeff Sharkey6fe28a02015-04-09 13:10:03 -0700197 { "fixuid", 4, do_fixuid },
198 { "freecache", 2, do_free_cache },
199 { "rmcache", 3, do_rm_cache },
200 { "rmcodecache", 3, do_rm_code_cache },
201 { "getsize", 8, do_get_size },
202 { "rmuserdata", 3, do_rm_user_data },
Jeff Sharkeye3637242015-04-08 20:56:42 -0700203 { "mvuserdata", 5, do_mv_user_data },
Mike Lockwood94afecf2012-10-24 10:45:23 -0700204 { "movefiles", 0, do_movefiles },
Jeff Sharkey6fe28a02015-04-09 13:10:03 -0700205 { "linklib", 4, do_linklib },
206 { "mkuserdata", 5, do_mk_user_data },
Robin Lee7c8bec02014-06-10 18:46:26 +0100207 { "mkuserconfig", 1, do_mk_user_config },
Jeff Sharkey6fe28a02015-04-09 13:10:03 -0700208 { "rmuser", 2, do_rm_user },
Mårten Kongstad63568b12014-01-31 14:42:59 +0100209 { "idmap", 3, do_idmap },
Jeff Sharkey6fe28a02015-04-09 13:10:03 -0700210 { "restorecondata", 4, do_restorecon_data },
Fyodor Kupolov88ce4ff2015-03-03 12:25:29 -0800211 { "createoatdir", 2, do_create_oat_dir },
212 { "rmpackagedir", 1, do_rm_package_dir},
Mike Lockwood94afecf2012-10-24 10:45:23 -0700213};
214
215static int readx(int s, void *_buf, int count)
216{
Jeff Sharkey19803802015-04-07 12:44:51 -0700217 char *buf = (char *) _buf;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700218 int n = 0, r;
219 if (count < 0) return -1;
220 while (n < count) {
221 r = read(s, buf + n, count - n);
222 if (r < 0) {
223 if (errno == EINTR) continue;
224 ALOGE("read error: %s\n", strerror(errno));
225 return -1;
226 }
227 if (r == 0) {
228 ALOGE("eof\n");
229 return -1; /* EOF */
230 }
231 n += r;
232 }
233 return 0;
234}
235
236static int writex(int s, const void *_buf, int count)
237{
Jeff Sharkey19803802015-04-07 12:44:51 -0700238 const char *buf = (const char *) _buf;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700239 int n = 0, r;
240 if (count < 0) return -1;
241 while (n < count) {
242 r = write(s, buf + n, count - n);
243 if (r < 0) {
244 if (errno == EINTR) continue;
245 ALOGE("write error: %s\n", strerror(errno));
246 return -1;
247 }
248 n += r;
249 }
250 return 0;
251}
252
253
254/* Tokenize the command buffer, locate a matching command,
255 * ensure that the required number of arguments are provided,
256 * call the function(), return the result.
257 */
258static int execute(int s, char cmd[BUFFER_MAX])
259{
260 char reply[REPLY_MAX];
261 char *arg[TOKEN_MAX+1];
262 unsigned i;
263 unsigned n = 0;
264 unsigned short count;
265 int ret = -1;
266
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700267 // ALOGI("execute('%s')\n", cmd);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700268
269 /* default reply is "" */
270 reply[0] = 0;
271
272 /* n is number of args (not counting arg[0]) */
273 arg[0] = cmd;
274 while (*cmd) {
275 if (isspace(*cmd)) {
276 *cmd++ = 0;
277 n++;
278 arg[n] = cmd;
279 if (n == TOKEN_MAX) {
280 ALOGE("too many arguments\n");
281 goto done;
282 }
283 }
Serguei Katkov62bb3852014-10-29 19:38:01 +0600284 if (*cmd) {
285 cmd++;
286 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700287 }
288
289 for (i = 0; i < sizeof(cmds) / sizeof(cmds[0]); i++) {
290 if (!strcmp(cmds[i].name,arg[0])) {
291 if (n != cmds[i].numargs) {
292 ALOGE("%s requires %d arguments (%d given)\n",
293 cmds[i].name, cmds[i].numargs, n);
294 } else {
295 ret = cmds[i].func(arg + 1, reply);
296 }
297 goto done;
298 }
299 }
300 ALOGE("unsupported command '%s'\n", arg[0]);
301
302done:
303 if (reply[0]) {
304 n = snprintf(cmd, BUFFER_MAX, "%d %s", ret, reply);
305 } else {
306 n = snprintf(cmd, BUFFER_MAX, "%d", ret);
307 }
308 if (n > BUFFER_MAX) n = BUFFER_MAX;
309 count = n;
310
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700311 // ALOGI("reply: '%s'\n", cmd);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700312 if (writex(s, &count, sizeof(count))) return -1;
313 if (writex(s, cmd, count)) return -1;
314 return 0;
315}
316
317/**
318 * Initialize all the global variables that are used elsewhere. Returns 0 upon
319 * success and -1 on error.
320 */
321void free_globals() {
322 size_t i;
323
324 for (i = 0; i < android_system_dirs.count; i++) {
325 if (android_system_dirs.dirs[i].path != NULL) {
326 free(android_system_dirs.dirs[i].path);
327 }
328 }
329
330 free(android_system_dirs.dirs);
331}
332
333int initialize_globals() {
334 // Get the android data directory.
335 if (get_path_from_env(&android_data_dir, "ANDROID_DATA") < 0) {
336 return -1;
337 }
338
339 // Get the android app directory.
340 if (copy_and_append(&android_app_dir, &android_data_dir, APP_SUBDIR) < 0) {
341 return -1;
342 }
343
344 // Get the android protected app directory.
345 if (copy_and_append(&android_app_private_dir, &android_data_dir, PRIVATE_APP_SUBDIR) < 0) {
346 return -1;
347 }
348
349 // Get the android app native library directory.
350 if (copy_and_append(&android_app_lib_dir, &android_data_dir, APP_LIB_SUBDIR) < 0) {
351 return -1;
352 }
353
354 // Get the sd-card ASEC mount point.
355 if (get_path_from_env(&android_asec_dir, "ASEC_MOUNTPOINT") < 0) {
356 return -1;
357 }
358
359 // Get the android media directory.
360 if (copy_and_append(&android_media_dir, &android_data_dir, MEDIA_SUBDIR) < 0) {
361 return -1;
362 }
363
Jeff Sharkeye23a1322015-04-06 16:19:39 -0700364 // Get the android external app directory.
Jeff Sharkey19803802015-04-07 12:44:51 -0700365 if (get_path_from_string(&android_mnt_expand_dir, "/mnt/expand/") < 0) {
Jeff Sharkeye23a1322015-04-06 16:19:39 -0700366 return -1;
367 }
368
Mike Lockwood94afecf2012-10-24 10:45:23 -0700369 // Take note of the system and vendor directories.
Jeff Sharkey770180a2014-09-08 17:14:26 -0700370 android_system_dirs.count = 4;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700371
Jeff Sharkey19803802015-04-07 12:44:51 -0700372 android_system_dirs.dirs = (dir_rec_t*) calloc(android_system_dirs.count, sizeof(dir_rec_t));
Mike Lockwood94afecf2012-10-24 10:45:23 -0700373 if (android_system_dirs.dirs == NULL) {
374 ALOGE("Couldn't allocate array for dirs; aborting\n");
375 return -1;
376 }
377
Jeff Sharkey770180a2014-09-08 17:14:26 -0700378 dir_rec_t android_root_dir;
379 if (get_path_from_env(&android_root_dir, "ANDROID_ROOT") < 0) {
380 ALOGE("Missing ANDROID_ROOT; aborting\n");
Mike Lockwood94afecf2012-10-24 10:45:23 -0700381 return -1;
382 }
383
Jeff Sharkey770180a2014-09-08 17:14:26 -0700384 android_system_dirs.dirs[0].path = build_string2(android_root_dir.path, APP_SUBDIR);
385 android_system_dirs.dirs[0].len = strlen(android_system_dirs.dirs[0].path);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700386
Jeff Sharkey770180a2014-09-08 17:14:26 -0700387 android_system_dirs.dirs[1].path = build_string2(android_root_dir.path, PRIV_APP_SUBDIR);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700388 android_system_dirs.dirs[1].len = strlen(android_system_dirs.dirs[1].path);
389
Jeff Sharkey19803802015-04-07 12:44:51 -0700390 android_system_dirs.dirs[2].path = strdup("/vendor/app/");
Jeff Sharkey770180a2014-09-08 17:14:26 -0700391 android_system_dirs.dirs[2].len = strlen(android_system_dirs.dirs[2].path);
392
Jeff Sharkey19803802015-04-07 12:44:51 -0700393 android_system_dirs.dirs[3].path = strdup("/oem/app/");
Jeff Sharkey770180a2014-09-08 17:14:26 -0700394 android_system_dirs.dirs[3].len = strlen(android_system_dirs.dirs[3].path);
395
Mike Lockwood94afecf2012-10-24 10:45:23 -0700396 return 0;
397}
398
399int initialize_directories() {
400 int res = -1;
401
402 // Read current filesystem layout version to handle upgrade paths
403 char version_path[PATH_MAX];
404 snprintf(version_path, PATH_MAX, "%s.layout_version", android_data_dir.path);
405
406 int oldVersion;
407 if (fs_read_atomic_int(version_path, &oldVersion) == -1) {
408 oldVersion = 0;
409 }
410 int version = oldVersion;
411
412 // /data/user
413 char *user_data_dir = build_string2(android_data_dir.path, SECONDARY_USER_PREFIX);
414 // /data/data
415 char *legacy_data_dir = build_string2(android_data_dir.path, PRIMARY_USER_PREFIX);
416 // /data/user/0
417 char *primary_data_dir = build_string3(android_data_dir.path, SECONDARY_USER_PREFIX, "0");
418 if (!user_data_dir || !legacy_data_dir || !primary_data_dir) {
419 goto fail;
420 }
421
422 // Make the /data/user directory if necessary
423 if (access(user_data_dir, R_OK) < 0) {
424 if (mkdir(user_data_dir, 0711) < 0) {
425 goto fail;
426 }
427 if (chown(user_data_dir, AID_SYSTEM, AID_SYSTEM) < 0) {
428 goto fail;
429 }
430 if (chmod(user_data_dir, 0711) < 0) {
431 goto fail;
432 }
433 }
434 // Make the /data/user/0 symlink to /data/data if necessary
435 if (access(primary_data_dir, R_OK) < 0) {
436 if (symlink(legacy_data_dir, primary_data_dir)) {
437 goto fail;
438 }
439 }
440
441 if (version == 0) {
442 // Introducing multi-user, so migrate /data/media contents into /data/media/0
443 ALOGD("Upgrading /data/media for multi-user");
444
445 // Ensure /data/media
446 if (fs_prepare_dir(android_media_dir.path, 0770, AID_MEDIA_RW, AID_MEDIA_RW) == -1) {
447 goto fail;
448 }
449
450 // /data/media.tmp
451 char media_tmp_dir[PATH_MAX];
452 snprintf(media_tmp_dir, PATH_MAX, "%smedia.tmp", android_data_dir.path);
453
454 // Only copy when upgrade not already in progress
455 if (access(media_tmp_dir, F_OK) == -1) {
456 if (rename(android_media_dir.path, media_tmp_dir) == -1) {
457 ALOGE("Failed to move legacy media path: %s", strerror(errno));
458 goto fail;
459 }
460 }
461
462 // Create /data/media again
463 if (fs_prepare_dir(android_media_dir.path, 0770, AID_MEDIA_RW, AID_MEDIA_RW) == -1) {
464 goto fail;
465 }
466
Stephen Smalley26288202014-02-07 09:16:46 -0500467 if (selinux_android_restorecon(android_media_dir.path, 0)) {
Stephen Smalley47a35182013-12-17 16:04:20 -0500468 goto fail;
469 }
470
Mike Lockwood94afecf2012-10-24 10:45:23 -0700471 // /data/media/0
472 char owner_media_dir[PATH_MAX];
473 snprintf(owner_media_dir, PATH_MAX, "%s0", android_media_dir.path);
474
475 // Move any owner data into place
476 if (access(media_tmp_dir, F_OK) == 0) {
477 if (rename(media_tmp_dir, owner_media_dir) == -1) {
478 ALOGE("Failed to move owner media path: %s", strerror(errno));
479 goto fail;
480 }
481 }
482
483 // Ensure media directories for any existing users
484 DIR *dir;
485 struct dirent *dirent;
486 char user_media_dir[PATH_MAX];
487
488 dir = opendir(user_data_dir);
489 if (dir != NULL) {
490 while ((dirent = readdir(dir))) {
491 if (dirent->d_type == DT_DIR) {
492 const char *name = dirent->d_name;
493
494 // skip "." and ".."
495 if (name[0] == '.') {
496 if (name[1] == 0) continue;
497 if ((name[1] == '.') && (name[2] == 0)) continue;
498 }
499
500 // /data/media/<user_id>
501 snprintf(user_media_dir, PATH_MAX, "%s%s", android_media_dir.path, name);
502 if (fs_prepare_dir(user_media_dir, 0770, AID_MEDIA_RW, AID_MEDIA_RW) == -1) {
503 goto fail;
504 }
505 }
506 }
507 closedir(dir);
508 }
509
510 version = 1;
511 }
512
513 // /data/media/obb
514 char media_obb_dir[PATH_MAX];
515 snprintf(media_obb_dir, PATH_MAX, "%sobb", android_media_dir.path);
516
517 if (version == 1) {
518 // Introducing /data/media/obb for sharing OBB across users; migrate
519 // any existing OBB files from owner.
520 ALOGD("Upgrading to shared /data/media/obb");
521
522 // /data/media/0/Android/obb
523 char owner_obb_path[PATH_MAX];
524 snprintf(owner_obb_path, PATH_MAX, "%s0/Android/obb", android_media_dir.path);
525
526 // Only move if target doesn't already exist
527 if (access(media_obb_dir, F_OK) != 0 && access(owner_obb_path, F_OK) == 0) {
528 if (rename(owner_obb_path, media_obb_dir) == -1) {
529 ALOGE("Failed to move OBB from owner: %s", strerror(errno));
530 goto fail;
531 }
532 }
533
534 version = 2;
535 }
536
Jeff Sharkey41ea4242015-04-09 11:34:03 -0700537 if (ensure_media_user_dirs(nullptr, 0) == -1) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700538 ALOGE("Failed to setup media for user 0");
539 goto fail;
540 }
541 if (fs_prepare_dir(media_obb_dir, 0770, AID_MEDIA_RW, AID_MEDIA_RW) == -1) {
542 goto fail;
543 }
544
Robin Lee07053fc2014-04-29 19:42:01 +0100545 if (ensure_config_user_dirs(0) == -1) {
546 ALOGE("Failed to setup misc for user 0");
547 goto fail;
548 }
549
Robin Lee095c7632014-04-25 15:05:19 +0100550 if (version == 2) {
551 ALOGD("Upgrading to /data/misc/user directories");
552
Robin Lee60fd3fe2014-10-07 16:55:02 +0100553 char misc_dir[PATH_MAX];
554 snprintf(misc_dir, PATH_MAX, "%smisc", android_data_dir.path);
555
556 char keychain_added_dir[PATH_MAX];
557 snprintf(keychain_added_dir, PATH_MAX, "%s/keychain/cacerts-added", misc_dir);
558
559 char keychain_removed_dir[PATH_MAX];
560 snprintf(keychain_removed_dir, PATH_MAX, "%s/keychain/cacerts-removed", misc_dir);
561
Robin Lee095c7632014-04-25 15:05:19 +0100562 DIR *dir;
563 struct dirent *dirent;
Robin Lee095c7632014-04-25 15:05:19 +0100564 dir = opendir(user_data_dir);
565 if (dir != NULL) {
566 while ((dirent = readdir(dir))) {
Robin Lee60fd3fe2014-10-07 16:55:02 +0100567 const char *name = dirent->d_name;
Robin Lee095c7632014-04-25 15:05:19 +0100568
Robin Lee60fd3fe2014-10-07 16:55:02 +0100569 // skip "." and ".."
570 if (name[0] == '.') {
571 if (name[1] == 0) continue;
572 if ((name[1] == '.') && (name[2] == 0)) continue;
573 }
574
575 uint32_t user_id = atoi(name);
576
577 // /data/misc/user/<user_id>
578 if (ensure_config_user_dirs(user_id) == -1) {
579 goto fail;
580 }
581
582 char misc_added_dir[PATH_MAX];
583 snprintf(misc_added_dir, PATH_MAX, "%s/user/%s/cacerts-added", misc_dir, name);
584
585 char misc_removed_dir[PATH_MAX];
586 snprintf(misc_removed_dir, PATH_MAX, "%s/user/%s/cacerts-removed", misc_dir, name);
587
588 uid_t uid = multiuser_get_uid(user_id, AID_SYSTEM);
589 gid_t gid = uid;
590 if (access(keychain_added_dir, F_OK) == 0) {
591 if (copy_dir_files(keychain_added_dir, misc_added_dir, uid, gid) != 0) {
592 ALOGE("Some files failed to copy");
Robin Lee095c7632014-04-25 15:05:19 +0100593 }
Robin Lee60fd3fe2014-10-07 16:55:02 +0100594 }
595 if (access(keychain_removed_dir, F_OK) == 0) {
596 if (copy_dir_files(keychain_removed_dir, misc_removed_dir, uid, gid) != 0) {
597 ALOGE("Some files failed to copy");
Robin Lee095c7632014-04-25 15:05:19 +0100598 }
599 }
600 }
601 closedir(dir);
Robin Lee095c7632014-04-25 15:05:19 +0100602
Robin Lee60fd3fe2014-10-07 16:55:02 +0100603 if (access(keychain_added_dir, F_OK) == 0) {
604 delete_dir_contents(keychain_added_dir, 1, 0);
Robin Lee07053fc2014-04-29 19:42:01 +0100605 }
Robin Lee60fd3fe2014-10-07 16:55:02 +0100606 if (access(keychain_removed_dir, F_OK) == 0) {
607 delete_dir_contents(keychain_removed_dir, 1, 0);
Robin Lee07053fc2014-04-29 19:42:01 +0100608 }
609 }
610
611 version = 3;
Robin Lee095c7632014-04-25 15:05:19 +0100612 }
613
Mike Lockwood94afecf2012-10-24 10:45:23 -0700614 // Persist layout version if changed
615 if (version != oldVersion) {
616 if (fs_write_atomic_int(version_path, version) == -1) {
617 ALOGE("Failed to save version to %s: %s", version_path, strerror(errno));
618 goto fail;
619 }
620 }
621
622 // Success!
623 res = 0;
624
625fail:
626 free(user_data_dir);
627 free(legacy_data_dir);
628 free(primary_data_dir);
629 return res;
630}
631
Stephen Smalley7abb52b2014-03-26 09:30:37 -0400632static int log_callback(int type, const char *fmt, ...) {
633 va_list ap;
634 int priority;
635
636 switch (type) {
637 case SELINUX_WARNING:
638 priority = ANDROID_LOG_WARN;
639 break;
640 case SELINUX_INFO:
641 priority = ANDROID_LOG_INFO;
642 break;
643 default:
644 priority = ANDROID_LOG_ERROR;
645 break;
646 }
647 va_start(ap, fmt);
648 LOG_PRI_VA(priority, "SELinux", fmt, ap);
649 va_end(ap);
650 return 0;
651}
652
Jeff Sharkeye3637242015-04-08 20:56:42 -0700653int main(const int argc __unused, char *argv[]) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700654 char buf[BUFFER_MAX];
655 struct sockaddr addr;
656 socklen_t alen;
Chih-Hung Hsieh99d9fb12014-09-11 14:44:46 -0700657 int lsocket, s;
Stephen Smalleybd558d62013-04-16 12:16:50 -0400658 int selinux_enabled = (is_selinux_enabled() > 0);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700659
Jeff Sharkeye3637242015-04-08 20:56:42 -0700660 setenv("ANDROID_LOG_TAGS", "*:v", 1);
661 android::base::InitLogging(argv);
662
Mike Lockwood94afecf2012-10-24 10:45:23 -0700663 ALOGI("installd firing up\n");
664
Stephen Smalley7abb52b2014-03-26 09:30:37 -0400665 union selinux_callback cb;
666 cb.func_log = log_callback;
667 selinux_set_callback(SELINUX_CB_LOG, cb);
668
Mike Lockwood94afecf2012-10-24 10:45:23 -0700669 if (initialize_globals() < 0) {
670 ALOGE("Could not initialize globals; exiting.\n");
671 exit(1);
672 }
673
674 if (initialize_directories() < 0) {
675 ALOGE("Could not create directories; exiting.\n");
676 exit(1);
677 }
678
Stephen Smalleybd558d62013-04-16 12:16:50 -0400679 if (selinux_enabled && selinux_status_open(true) < 0) {
680 ALOGE("Could not open selinux status; exiting.\n");
681 exit(1);
682 }
683
Mike Lockwood94afecf2012-10-24 10:45:23 -0700684 lsocket = android_get_control_socket(SOCKET_PATH);
685 if (lsocket < 0) {
686 ALOGE("Failed to get socket from environment: %s\n", strerror(errno));
687 exit(1);
688 }
689 if (listen(lsocket, 5)) {
690 ALOGE("Listen on socket failed: %s\n", strerror(errno));
691 exit(1);
692 }
693 fcntl(lsocket, F_SETFD, FD_CLOEXEC);
694
695 for (;;) {
696 alen = sizeof(addr);
697 s = accept(lsocket, &addr, &alen);
698 if (s < 0) {
699 ALOGE("Accept failed: %s\n", strerror(errno));
700 continue;
701 }
702 fcntl(s, F_SETFD, FD_CLOEXEC);
703
704 ALOGI("new connection\n");
705 for (;;) {
706 unsigned short count;
707 if (readx(s, &count, sizeof(count))) {
708 ALOGE("failed to read size\n");
709 break;
710 }
711 if ((count < 1) || (count >= BUFFER_MAX)) {
712 ALOGE("invalid size %d\n", count);
713 break;
714 }
715 if (readx(s, buf, count)) {
716 ALOGE("failed to read command\n");
717 break;
718 }
719 buf[count] = 0;
Stephen Smalleybd558d62013-04-16 12:16:50 -0400720 if (selinux_enabled && selinux_status_updated() > 0) {
721 selinux_android_seapp_context_reload();
722 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700723 if (execute(s, buf)) break;
724 }
725 ALOGI("closing connection\n");
726 close(s);
727 }
728
729 return 0;
730}