blob: 3a9b54f5b20acda43251a6842c644d7d1285cb55 [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{
Todd Kennedy76e767c2015-09-25 07:47:47 -070050 /* apk_path, uid, pkgname, instruction_set, dexopt_needed, oat_dir, dexopt_flags */
51 return dexopt(arg[0], atoi(arg[1]), arg[2], arg[3], atoi(arg[4]),
52 arg[5], atoi(arg[6]));
Mike Lockwood94afecf2012-10-24 10:45:23 -070053}
54
Bernhard Rosenkränzer7fb390d2014-11-23 22:28:26 +010055static int do_mark_boot_complete(char **arg, char reply[REPLY_MAX] __unused)
Narayan Kamath091ea772014-11-10 15:03:46 +000056{
57 return mark_boot_complete(arg[0] /* instruction set */);
58}
59
Bernhard Rosenkränzer7fb390d2014-11-23 22:28:26 +010060static int do_move_dex(char **arg, char reply[REPLY_MAX] __unused)
Mike Lockwood94afecf2012-10-24 10:45:23 -070061{
Narayan Kamath1b400322014-04-11 13:17:00 +010062 return move_dex(arg[0], arg[1], arg[2]); /* src, dst, instruction_set */
Mike Lockwood94afecf2012-10-24 10:45:23 -070063}
64
Chih-Hung Hsieh99d9fb12014-09-11 14:44:46 -070065static int do_rm_dex(char **arg, char reply[REPLY_MAX] __unused)
Mike Lockwood94afecf2012-10-24 10:45:23 -070066{
Narayan Kamath1b400322014-04-11 13:17:00 +010067 return rm_dex(arg[0], arg[1]); /* pkgname, instruction_set */
Mike Lockwood94afecf2012-10-24 10:45:23 -070068}
69
Chih-Hung Hsieh99d9fb12014-09-11 14:44:46 -070070static int do_remove(char **arg, char reply[REPLY_MAX] __unused)
Mike Lockwood94afecf2012-10-24 10:45:23 -070071{
Jeff Sharkey6fe28a02015-04-09 13:10:03 -070072 return uninstall(parse_null(arg[0]), arg[1], atoi(arg[2])); /* uuid, pkgname, userid */
Mike Lockwood94afecf2012-10-24 10:45:23 -070073}
74
Chih-Hung Hsieh99d9fb12014-09-11 14:44:46 -070075static int do_rename(char **arg, char reply[REPLY_MAX] __unused)
Mike Lockwood94afecf2012-10-24 10:45:23 -070076{
77 return renamepkg(arg[0], arg[1]); /* oldpkgname, newpkgname */
78}
79
Chih-Hung Hsieh99d9fb12014-09-11 14:44:46 -070080static int do_fixuid(char **arg, char reply[REPLY_MAX] __unused)
Mike Lockwood94afecf2012-10-24 10:45:23 -070081{
Jeff Sharkey6fe28a02015-04-09 13:10:03 -070082 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 -070083}
84
Chih-Hung Hsieh99d9fb12014-09-11 14:44:46 -070085static int do_free_cache(char **arg, char reply[REPLY_MAX] __unused) /* TODO int:free_size */
Mike Lockwood94afecf2012-10-24 10:45:23 -070086{
Jeff Sharkey6fe28a02015-04-09 13:10:03 -070087 return free_cache(parse_null(arg[0]), (int64_t)atoll(arg[1])); /* uuid, free_size */
Mike Lockwood94afecf2012-10-24 10:45:23 -070088}
89
Chih-Hung Hsieh99d9fb12014-09-11 14:44:46 -070090static int do_rm_cache(char **arg, char reply[REPLY_MAX] __unused)
Mike Lockwood94afecf2012-10-24 10:45:23 -070091{
Jeff Sharkey6fe28a02015-04-09 13:10:03 -070092 return delete_cache(parse_null(arg[0]), arg[1], atoi(arg[2])); /* uuid, pkgname, userid */
Mike Lockwood94afecf2012-10-24 10:45:23 -070093}
94
Dan Albert9e8b5282014-09-12 09:00:50 -070095static int do_rm_code_cache(char **arg, char reply[REPLY_MAX] __unused)
Jeff Sharkeyc796b682014-07-15 21:49:51 -070096{
Jeff Sharkey6fe28a02015-04-09 13:10:03 -070097 return delete_code_cache(parse_null(arg[0]), arg[1], atoi(arg[2])); /* uuid, pkgname, userid */
Jeff Sharkeyc796b682014-07-15 21:49:51 -070098}
99
Mike Lockwood94afecf2012-10-24 10:45:23 -0700100static int do_get_size(char **arg, char reply[REPLY_MAX])
101{
102 int64_t codesize = 0;
103 int64_t datasize = 0;
104 int64_t cachesize = 0;
105 int64_t asecsize = 0;
106 int res = 0;
107
Jeff Sharkey6fe28a02015-04-09 13:10:03 -0700108 /* uuid, pkgdir, userid, apkpath */
109 res = get_size(parse_null(arg[0]), arg[1], atoi(arg[2]), arg[3], arg[4], arg[5], arg[6],
110 arg[7], &codesize, &datasize, &cachesize, &asecsize);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700111
112 /*
113 * Each int64_t can take up 22 characters printed out. Make sure it
114 * doesn't go over REPLY_MAX in the future.
115 */
116 snprintf(reply, REPLY_MAX, "%" PRId64 " %" PRId64 " %" PRId64 " %" PRId64,
117 codesize, datasize, cachesize, asecsize);
118 return res;
119}
120
Chih-Hung Hsieh99d9fb12014-09-11 14:44:46 -0700121static int do_rm_user_data(char **arg, char reply[REPLY_MAX] __unused)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700122{
Jeff Sharkey6fe28a02015-04-09 13:10:03 -0700123 return delete_user_data(parse_null(arg[0]), arg[1], atoi(arg[2])); /* uuid, pkgname, userid */
Mike Lockwood94afecf2012-10-24 10:45:23 -0700124}
125
Jeff Sharkeye3637242015-04-08 20:56:42 -0700126static int do_mv_user_data(char **arg, char reply[REPLY_MAX] __unused)
127{
128 // from_uuid, to_uuid, pkgname, appid, seinfo
129 return move_user_data(parse_null(arg[0]), parse_null(arg[1]), arg[2], atoi(arg[3]), arg[4]);
130}
131
Chih-Hung Hsieh99d9fb12014-09-11 14:44:46 -0700132static int do_mk_user_data(char **arg, char reply[REPLY_MAX] __unused)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700133{
Jeff Sharkey6fe28a02015-04-09 13:10:03 -0700134 return make_user_data(parse_null(arg[0]), arg[1], atoi(arg[2]), atoi(arg[3]), arg[4]);
135 /* uuid, pkgname, uid, userid, seinfo */
Mike Lockwood94afecf2012-10-24 10:45:23 -0700136}
137
Chih-Hung Hsieh99d9fb12014-09-11 14:44:46 -0700138static int do_mk_user_config(char **arg, char reply[REPLY_MAX] __unused)
Robin Lee095c7632014-04-25 15:05:19 +0100139{
Robin Lee7c8bec02014-06-10 18:46:26 +0100140 return make_user_config(atoi(arg[0])); /* userid */
Robin Lee095c7632014-04-25 15:05:19 +0100141}
142
Chih-Hung Hsieh99d9fb12014-09-11 14:44:46 -0700143static int do_rm_user(char **arg, char reply[REPLY_MAX] __unused)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700144{
Jeff Sharkey6fe28a02015-04-09 13:10:03 -0700145 return delete_user(parse_null(arg[0]), atoi(arg[1])); /* uuid, userid */
Mike Lockwood94afecf2012-10-24 10:45:23 -0700146}
147
Chih-Hung Hsieh99d9fb12014-09-11 14:44:46 -0700148static int do_movefiles(char **arg __unused, char reply[REPLY_MAX] __unused)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700149{
150 return movefiles();
151}
152
Chih-Hung Hsieh99d9fb12014-09-11 14:44:46 -0700153static int do_linklib(char **arg, char reply[REPLY_MAX] __unused)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700154{
Jeff Sharkey6fe28a02015-04-09 13:10:03 -0700155 return linklib(parse_null(arg[0]), arg[1], arg[2], atoi(arg[3]));
Mike Lockwood94afecf2012-10-24 10:45:23 -0700156}
157
Chih-Hung Hsieh99d9fb12014-09-11 14:44:46 -0700158static int do_idmap(char **arg, char reply[REPLY_MAX] __unused)
Mårten Kongstad63568b12014-01-31 14:42:59 +0100159{
160 return idmap(arg[0], arg[1], atoi(arg[2]));
161}
162
Robert Craigda30dc72014-03-27 10:21:12 -0400163static int do_restorecon_data(char **arg, char reply[REPLY_MAX] __attribute__((unused)))
Robert Craige9887e42014-02-20 10:25:56 -0500164{
Jeff Sharkey6fe28a02015-04-09 13:10:03 -0700165 return restorecon_data(parse_null(arg[0]), arg[1], arg[2], atoi(arg[3]));
166 /* uuid, pkgName, seinfo, uid*/
Robert Craige9887e42014-02-20 10:25:56 -0500167}
168
Fyodor Kupolov88ce4ff2015-03-03 12:25:29 -0800169static int do_create_oat_dir(char **arg, char reply[REPLY_MAX] __unused)
170{
171 /* oat_dir, instruction_set */
172 return create_oat_dir(arg[0], arg[1]);
173}
174
175static int do_rm_package_dir(char **arg, char reply[REPLY_MAX] __unused)
176{
177 /* oat_dir */
178 return rm_package_dir(arg[0]);
Alex Light43c5d302014-07-21 12:23:48 -0700179}
180
Mike Lockwood94afecf2012-10-24 10:45:23 -0700181struct cmdinfo {
182 const char *name;
183 unsigned numargs;
184 int (*func)(char **arg, char reply[REPLY_MAX]);
185};
186
187struct cmdinfo cmds[] = {
188 { "ping", 0, do_ping },
Jeff Sharkey6fe28a02015-04-09 13:10:03 -0700189 { "install", 5, do_install },
Todd Kennedy76e767c2015-09-25 07:47:47 -0700190 { "dexopt", 7, do_dexopt },
Narayan Kamath091ea772014-11-10 15:03:46 +0000191 { "markbootcomplete", 1, do_mark_boot_complete },
Narayan Kamath1b400322014-04-11 13:17:00 +0100192 { "movedex", 3, do_move_dex },
193 { "rmdex", 2, do_rm_dex },
Jeff Sharkey6fe28a02015-04-09 13:10:03 -0700194 { "remove", 3, do_remove },
Mike Lockwood94afecf2012-10-24 10:45:23 -0700195 { "rename", 2, do_rename },
Jeff Sharkey6fe28a02015-04-09 13:10:03 -0700196 { "fixuid", 4, do_fixuid },
197 { "freecache", 2, do_free_cache },
198 { "rmcache", 3, do_rm_cache },
199 { "rmcodecache", 3, do_rm_code_cache },
200 { "getsize", 8, do_get_size },
201 { "rmuserdata", 3, do_rm_user_data },
Jeff Sharkeye3637242015-04-08 20:56:42 -0700202 { "mvuserdata", 5, do_mv_user_data },
Mike Lockwood94afecf2012-10-24 10:45:23 -0700203 { "movefiles", 0, do_movefiles },
Jeff Sharkey6fe28a02015-04-09 13:10:03 -0700204 { "linklib", 4, do_linklib },
205 { "mkuserdata", 5, do_mk_user_data },
Robin Lee7c8bec02014-06-10 18:46:26 +0100206 { "mkuserconfig", 1, do_mk_user_config },
Jeff Sharkey6fe28a02015-04-09 13:10:03 -0700207 { "rmuser", 2, do_rm_user },
Mårten Kongstad63568b12014-01-31 14:42:59 +0100208 { "idmap", 3, do_idmap },
Jeff Sharkey6fe28a02015-04-09 13:10:03 -0700209 { "restorecondata", 4, do_restorecon_data },
Fyodor Kupolov88ce4ff2015-03-03 12:25:29 -0800210 { "createoatdir", 2, do_create_oat_dir },
211 { "rmpackagedir", 1, do_rm_package_dir},
Mike Lockwood94afecf2012-10-24 10:45:23 -0700212};
213
214static int readx(int s, void *_buf, int count)
215{
Jeff Sharkey19803802015-04-07 12:44:51 -0700216 char *buf = (char *) _buf;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700217 int n = 0, r;
218 if (count < 0) return -1;
219 while (n < count) {
220 r = read(s, buf + n, count - n);
221 if (r < 0) {
222 if (errno == EINTR) continue;
223 ALOGE("read error: %s\n", strerror(errno));
224 return -1;
225 }
226 if (r == 0) {
227 ALOGE("eof\n");
228 return -1; /* EOF */
229 }
230 n += r;
231 }
232 return 0;
233}
234
235static int writex(int s, const void *_buf, int count)
236{
Jeff Sharkey19803802015-04-07 12:44:51 -0700237 const char *buf = (const char *) _buf;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700238 int n = 0, r;
239 if (count < 0) return -1;
240 while (n < count) {
241 r = write(s, buf + n, count - n);
242 if (r < 0) {
243 if (errno == EINTR) continue;
244 ALOGE("write error: %s\n", strerror(errno));
245 return -1;
246 }
247 n += r;
248 }
249 return 0;
250}
251
252
253/* Tokenize the command buffer, locate a matching command,
254 * ensure that the required number of arguments are provided,
255 * call the function(), return the result.
256 */
257static int execute(int s, char cmd[BUFFER_MAX])
258{
259 char reply[REPLY_MAX];
260 char *arg[TOKEN_MAX+1];
261 unsigned i;
262 unsigned n = 0;
263 unsigned short count;
264 int ret = -1;
265
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700266 // ALOGI("execute('%s')\n", cmd);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700267
268 /* default reply is "" */
269 reply[0] = 0;
270
271 /* n is number of args (not counting arg[0]) */
272 arg[0] = cmd;
273 while (*cmd) {
274 if (isspace(*cmd)) {
275 *cmd++ = 0;
276 n++;
277 arg[n] = cmd;
278 if (n == TOKEN_MAX) {
279 ALOGE("too many arguments\n");
280 goto done;
281 }
282 }
Serguei Katkov62bb3852014-10-29 19:38:01 +0600283 if (*cmd) {
284 cmd++;
285 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700286 }
287
288 for (i = 0; i < sizeof(cmds) / sizeof(cmds[0]); i++) {
289 if (!strcmp(cmds[i].name,arg[0])) {
290 if (n != cmds[i].numargs) {
291 ALOGE("%s requires %d arguments (%d given)\n",
292 cmds[i].name, cmds[i].numargs, n);
293 } else {
294 ret = cmds[i].func(arg + 1, reply);
295 }
296 goto done;
297 }
298 }
299 ALOGE("unsupported command '%s'\n", arg[0]);
300
301done:
302 if (reply[0]) {
303 n = snprintf(cmd, BUFFER_MAX, "%d %s", ret, reply);
304 } else {
305 n = snprintf(cmd, BUFFER_MAX, "%d", ret);
306 }
307 if (n > BUFFER_MAX) n = BUFFER_MAX;
308 count = n;
309
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700310 // ALOGI("reply: '%s'\n", cmd);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700311 if (writex(s, &count, sizeof(count))) return -1;
312 if (writex(s, cmd, count)) return -1;
313 return 0;
314}
315
316/**
317 * Initialize all the global variables that are used elsewhere. Returns 0 upon
318 * success and -1 on error.
319 */
320void free_globals() {
321 size_t i;
322
323 for (i = 0; i < android_system_dirs.count; i++) {
324 if (android_system_dirs.dirs[i].path != NULL) {
325 free(android_system_dirs.dirs[i].path);
326 }
327 }
328
329 free(android_system_dirs.dirs);
330}
331
332int initialize_globals() {
333 // Get the android data directory.
334 if (get_path_from_env(&android_data_dir, "ANDROID_DATA") < 0) {
335 return -1;
336 }
337
338 // Get the android app directory.
339 if (copy_and_append(&android_app_dir, &android_data_dir, APP_SUBDIR) < 0) {
340 return -1;
341 }
342
343 // Get the android protected app directory.
344 if (copy_and_append(&android_app_private_dir, &android_data_dir, PRIVATE_APP_SUBDIR) < 0) {
345 return -1;
346 }
347
348 // Get the android app native library directory.
349 if (copy_and_append(&android_app_lib_dir, &android_data_dir, APP_LIB_SUBDIR) < 0) {
350 return -1;
351 }
352
353 // Get the sd-card ASEC mount point.
354 if (get_path_from_env(&android_asec_dir, "ASEC_MOUNTPOINT") < 0) {
355 return -1;
356 }
357
358 // Get the android media directory.
359 if (copy_and_append(&android_media_dir, &android_data_dir, MEDIA_SUBDIR) < 0) {
360 return -1;
361 }
362
Jeff Sharkeye23a1322015-04-06 16:19:39 -0700363 // Get the android external app directory.
Jeff Sharkey19803802015-04-07 12:44:51 -0700364 if (get_path_from_string(&android_mnt_expand_dir, "/mnt/expand/") < 0) {
Jeff Sharkeye23a1322015-04-06 16:19:39 -0700365 return -1;
366 }
367
Mike Lockwood94afecf2012-10-24 10:45:23 -0700368 // Take note of the system and vendor directories.
Jeff Sharkey770180a2014-09-08 17:14:26 -0700369 android_system_dirs.count = 4;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700370
Jeff Sharkey19803802015-04-07 12:44:51 -0700371 android_system_dirs.dirs = (dir_rec_t*) calloc(android_system_dirs.count, sizeof(dir_rec_t));
Mike Lockwood94afecf2012-10-24 10:45:23 -0700372 if (android_system_dirs.dirs == NULL) {
373 ALOGE("Couldn't allocate array for dirs; aborting\n");
374 return -1;
375 }
376
Jeff Sharkey770180a2014-09-08 17:14:26 -0700377 dir_rec_t android_root_dir;
378 if (get_path_from_env(&android_root_dir, "ANDROID_ROOT") < 0) {
379 ALOGE("Missing ANDROID_ROOT; aborting\n");
Mike Lockwood94afecf2012-10-24 10:45:23 -0700380 return -1;
381 }
382
Jeff Sharkey770180a2014-09-08 17:14:26 -0700383 android_system_dirs.dirs[0].path = build_string2(android_root_dir.path, APP_SUBDIR);
384 android_system_dirs.dirs[0].len = strlen(android_system_dirs.dirs[0].path);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700385
Jeff Sharkey770180a2014-09-08 17:14:26 -0700386 android_system_dirs.dirs[1].path = build_string2(android_root_dir.path, PRIV_APP_SUBDIR);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700387 android_system_dirs.dirs[1].len = strlen(android_system_dirs.dirs[1].path);
388
Jeff Sharkey19803802015-04-07 12:44:51 -0700389 android_system_dirs.dirs[2].path = strdup("/vendor/app/");
Jeff Sharkey770180a2014-09-08 17:14:26 -0700390 android_system_dirs.dirs[2].len = strlen(android_system_dirs.dirs[2].path);
391
Jeff Sharkey19803802015-04-07 12:44:51 -0700392 android_system_dirs.dirs[3].path = strdup("/oem/app/");
Jeff Sharkey770180a2014-09-08 17:14:26 -0700393 android_system_dirs.dirs[3].len = strlen(android_system_dirs.dirs[3].path);
394
Mike Lockwood94afecf2012-10-24 10:45:23 -0700395 return 0;
396}
397
398int initialize_directories() {
399 int res = -1;
400
401 // Read current filesystem layout version to handle upgrade paths
402 char version_path[PATH_MAX];
403 snprintf(version_path, PATH_MAX, "%s.layout_version", android_data_dir.path);
404
405 int oldVersion;
406 if (fs_read_atomic_int(version_path, &oldVersion) == -1) {
407 oldVersion = 0;
408 }
409 int version = oldVersion;
410
411 // /data/user
412 char *user_data_dir = build_string2(android_data_dir.path, SECONDARY_USER_PREFIX);
413 // /data/data
414 char *legacy_data_dir = build_string2(android_data_dir.path, PRIMARY_USER_PREFIX);
415 // /data/user/0
416 char *primary_data_dir = build_string3(android_data_dir.path, SECONDARY_USER_PREFIX, "0");
417 if (!user_data_dir || !legacy_data_dir || !primary_data_dir) {
418 goto fail;
419 }
420
421 // Make the /data/user directory if necessary
422 if (access(user_data_dir, R_OK) < 0) {
423 if (mkdir(user_data_dir, 0711) < 0) {
424 goto fail;
425 }
426 if (chown(user_data_dir, AID_SYSTEM, AID_SYSTEM) < 0) {
427 goto fail;
428 }
429 if (chmod(user_data_dir, 0711) < 0) {
430 goto fail;
431 }
432 }
433 // Make the /data/user/0 symlink to /data/data if necessary
434 if (access(primary_data_dir, R_OK) < 0) {
435 if (symlink(legacy_data_dir, primary_data_dir)) {
436 goto fail;
437 }
438 }
439
440 if (version == 0) {
441 // Introducing multi-user, so migrate /data/media contents into /data/media/0
442 ALOGD("Upgrading /data/media for multi-user");
443
444 // Ensure /data/media
445 if (fs_prepare_dir(android_media_dir.path, 0770, AID_MEDIA_RW, AID_MEDIA_RW) == -1) {
446 goto fail;
447 }
448
449 // /data/media.tmp
450 char media_tmp_dir[PATH_MAX];
451 snprintf(media_tmp_dir, PATH_MAX, "%smedia.tmp", android_data_dir.path);
452
453 // Only copy when upgrade not already in progress
454 if (access(media_tmp_dir, F_OK) == -1) {
455 if (rename(android_media_dir.path, media_tmp_dir) == -1) {
456 ALOGE("Failed to move legacy media path: %s", strerror(errno));
457 goto fail;
458 }
459 }
460
461 // Create /data/media again
462 if (fs_prepare_dir(android_media_dir.path, 0770, AID_MEDIA_RW, AID_MEDIA_RW) == -1) {
463 goto fail;
464 }
465
Stephen Smalley26288202014-02-07 09:16:46 -0500466 if (selinux_android_restorecon(android_media_dir.path, 0)) {
Stephen Smalley47a35182013-12-17 16:04:20 -0500467 goto fail;
468 }
469
Mike Lockwood94afecf2012-10-24 10:45:23 -0700470 // /data/media/0
471 char owner_media_dir[PATH_MAX];
472 snprintf(owner_media_dir, PATH_MAX, "%s0", android_media_dir.path);
473
474 // Move any owner data into place
475 if (access(media_tmp_dir, F_OK) == 0) {
476 if (rename(media_tmp_dir, owner_media_dir) == -1) {
477 ALOGE("Failed to move owner media path: %s", strerror(errno));
478 goto fail;
479 }
480 }
481
482 // Ensure media directories for any existing users
483 DIR *dir;
484 struct dirent *dirent;
485 char user_media_dir[PATH_MAX];
486
487 dir = opendir(user_data_dir);
488 if (dir != NULL) {
489 while ((dirent = readdir(dir))) {
490 if (dirent->d_type == DT_DIR) {
491 const char *name = dirent->d_name;
492
493 // skip "." and ".."
494 if (name[0] == '.') {
495 if (name[1] == 0) continue;
496 if ((name[1] == '.') && (name[2] == 0)) continue;
497 }
498
499 // /data/media/<user_id>
500 snprintf(user_media_dir, PATH_MAX, "%s%s", android_media_dir.path, name);
501 if (fs_prepare_dir(user_media_dir, 0770, AID_MEDIA_RW, AID_MEDIA_RW) == -1) {
502 goto fail;
503 }
504 }
505 }
506 closedir(dir);
507 }
508
509 version = 1;
510 }
511
512 // /data/media/obb
513 char media_obb_dir[PATH_MAX];
514 snprintf(media_obb_dir, PATH_MAX, "%sobb", android_media_dir.path);
515
516 if (version == 1) {
517 // Introducing /data/media/obb for sharing OBB across users; migrate
518 // any existing OBB files from owner.
519 ALOGD("Upgrading to shared /data/media/obb");
520
521 // /data/media/0/Android/obb
522 char owner_obb_path[PATH_MAX];
523 snprintf(owner_obb_path, PATH_MAX, "%s0/Android/obb", android_media_dir.path);
524
525 // Only move if target doesn't already exist
526 if (access(media_obb_dir, F_OK) != 0 && access(owner_obb_path, F_OK) == 0) {
527 if (rename(owner_obb_path, media_obb_dir) == -1) {
528 ALOGE("Failed to move OBB from owner: %s", strerror(errno));
529 goto fail;
530 }
531 }
532
533 version = 2;
534 }
535
Jeff Sharkey41ea4242015-04-09 11:34:03 -0700536 if (ensure_media_user_dirs(nullptr, 0) == -1) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700537 ALOGE("Failed to setup media for user 0");
538 goto fail;
539 }
540 if (fs_prepare_dir(media_obb_dir, 0770, AID_MEDIA_RW, AID_MEDIA_RW) == -1) {
541 goto fail;
542 }
543
Robin Lee07053fc2014-04-29 19:42:01 +0100544 if (ensure_config_user_dirs(0) == -1) {
545 ALOGE("Failed to setup misc for user 0");
546 goto fail;
547 }
548
Robin Lee095c7632014-04-25 15:05:19 +0100549 if (version == 2) {
550 ALOGD("Upgrading to /data/misc/user directories");
551
Robin Lee60fd3fe2014-10-07 16:55:02 +0100552 char misc_dir[PATH_MAX];
553 snprintf(misc_dir, PATH_MAX, "%smisc", android_data_dir.path);
554
555 char keychain_added_dir[PATH_MAX];
556 snprintf(keychain_added_dir, PATH_MAX, "%s/keychain/cacerts-added", misc_dir);
557
558 char keychain_removed_dir[PATH_MAX];
559 snprintf(keychain_removed_dir, PATH_MAX, "%s/keychain/cacerts-removed", misc_dir);
560
Robin Lee095c7632014-04-25 15:05:19 +0100561 DIR *dir;
562 struct dirent *dirent;
Robin Lee095c7632014-04-25 15:05:19 +0100563 dir = opendir(user_data_dir);
564 if (dir != NULL) {
565 while ((dirent = readdir(dir))) {
Robin Lee60fd3fe2014-10-07 16:55:02 +0100566 const char *name = dirent->d_name;
Robin Lee095c7632014-04-25 15:05:19 +0100567
Robin Lee60fd3fe2014-10-07 16:55:02 +0100568 // skip "." and ".."
569 if (name[0] == '.') {
570 if (name[1] == 0) continue;
571 if ((name[1] == '.') && (name[2] == 0)) continue;
572 }
573
574 uint32_t user_id = atoi(name);
575
576 // /data/misc/user/<user_id>
577 if (ensure_config_user_dirs(user_id) == -1) {
578 goto fail;
579 }
580
581 char misc_added_dir[PATH_MAX];
582 snprintf(misc_added_dir, PATH_MAX, "%s/user/%s/cacerts-added", misc_dir, name);
583
584 char misc_removed_dir[PATH_MAX];
585 snprintf(misc_removed_dir, PATH_MAX, "%s/user/%s/cacerts-removed", misc_dir, name);
586
587 uid_t uid = multiuser_get_uid(user_id, AID_SYSTEM);
588 gid_t gid = uid;
589 if (access(keychain_added_dir, F_OK) == 0) {
590 if (copy_dir_files(keychain_added_dir, misc_added_dir, uid, gid) != 0) {
591 ALOGE("Some files failed to copy");
Robin Lee095c7632014-04-25 15:05:19 +0100592 }
Robin Lee60fd3fe2014-10-07 16:55:02 +0100593 }
594 if (access(keychain_removed_dir, F_OK) == 0) {
595 if (copy_dir_files(keychain_removed_dir, misc_removed_dir, uid, gid) != 0) {
596 ALOGE("Some files failed to copy");
Robin Lee095c7632014-04-25 15:05:19 +0100597 }
598 }
599 }
600 closedir(dir);
Robin Lee095c7632014-04-25 15:05:19 +0100601
Robin Lee60fd3fe2014-10-07 16:55:02 +0100602 if (access(keychain_added_dir, F_OK) == 0) {
603 delete_dir_contents(keychain_added_dir, 1, 0);
Robin Lee07053fc2014-04-29 19:42:01 +0100604 }
Robin Lee60fd3fe2014-10-07 16:55:02 +0100605 if (access(keychain_removed_dir, F_OK) == 0) {
606 delete_dir_contents(keychain_removed_dir, 1, 0);
Robin Lee07053fc2014-04-29 19:42:01 +0100607 }
608 }
609
610 version = 3;
Robin Lee095c7632014-04-25 15:05:19 +0100611 }
612
Mike Lockwood94afecf2012-10-24 10:45:23 -0700613 // Persist layout version if changed
614 if (version != oldVersion) {
615 if (fs_write_atomic_int(version_path, version) == -1) {
616 ALOGE("Failed to save version to %s: %s", version_path, strerror(errno));
617 goto fail;
618 }
619 }
620
621 // Success!
622 res = 0;
623
624fail:
625 free(user_data_dir);
626 free(legacy_data_dir);
627 free(primary_data_dir);
628 return res;
629}
630
Stephen Smalley7abb52b2014-03-26 09:30:37 -0400631static int log_callback(int type, const char *fmt, ...) {
632 va_list ap;
633 int priority;
634
635 switch (type) {
636 case SELINUX_WARNING:
637 priority = ANDROID_LOG_WARN;
638 break;
639 case SELINUX_INFO:
640 priority = ANDROID_LOG_INFO;
641 break;
642 default:
643 priority = ANDROID_LOG_ERROR;
644 break;
645 }
646 va_start(ap, fmt);
647 LOG_PRI_VA(priority, "SELinux", fmt, ap);
648 va_end(ap);
649 return 0;
650}
651
Jeff Sharkeye3637242015-04-08 20:56:42 -0700652int main(const int argc __unused, char *argv[]) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700653 char buf[BUFFER_MAX];
654 struct sockaddr addr;
655 socklen_t alen;
Chih-Hung Hsieh99d9fb12014-09-11 14:44:46 -0700656 int lsocket, s;
Stephen Smalleybd558d62013-04-16 12:16:50 -0400657 int selinux_enabled = (is_selinux_enabled() > 0);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700658
Jeff Sharkeye3637242015-04-08 20:56:42 -0700659 setenv("ANDROID_LOG_TAGS", "*:v", 1);
660 android::base::InitLogging(argv);
661
Mike Lockwood94afecf2012-10-24 10:45:23 -0700662 ALOGI("installd firing up\n");
663
Stephen Smalley7abb52b2014-03-26 09:30:37 -0400664 union selinux_callback cb;
665 cb.func_log = log_callback;
666 selinux_set_callback(SELINUX_CB_LOG, cb);
667
Mike Lockwood94afecf2012-10-24 10:45:23 -0700668 if (initialize_globals() < 0) {
669 ALOGE("Could not initialize globals; exiting.\n");
670 exit(1);
671 }
672
673 if (initialize_directories() < 0) {
674 ALOGE("Could not create directories; exiting.\n");
675 exit(1);
676 }
677
Stephen Smalleybd558d62013-04-16 12:16:50 -0400678 if (selinux_enabled && selinux_status_open(true) < 0) {
679 ALOGE("Could not open selinux status; exiting.\n");
680 exit(1);
681 }
682
Mike Lockwood94afecf2012-10-24 10:45:23 -0700683 lsocket = android_get_control_socket(SOCKET_PATH);
684 if (lsocket < 0) {
685 ALOGE("Failed to get socket from environment: %s\n", strerror(errno));
686 exit(1);
687 }
688 if (listen(lsocket, 5)) {
689 ALOGE("Listen on socket failed: %s\n", strerror(errno));
690 exit(1);
691 }
692 fcntl(lsocket, F_SETFD, FD_CLOEXEC);
693
694 for (;;) {
695 alen = sizeof(addr);
696 s = accept(lsocket, &addr, &alen);
697 if (s < 0) {
698 ALOGE("Accept failed: %s\n", strerror(errno));
699 continue;
700 }
701 fcntl(s, F_SETFD, FD_CLOEXEC);
702
703 ALOGI("new connection\n");
704 for (;;) {
705 unsigned short count;
706 if (readx(s, &count, sizeof(count))) {
707 ALOGE("failed to read size\n");
708 break;
709 }
710 if ((count < 1) || (count >= BUFFER_MAX)) {
711 ALOGE("invalid size %d\n", count);
712 break;
713 }
714 if (readx(s, buf, count)) {
715 ALOGE("failed to read command\n");
716 break;
717 }
718 buf[count] = 0;
Stephen Smalleybd558d62013-04-16 12:16:50 -0400719 if (selinux_enabled && selinux_status_updated() > 0) {
720 selinux_android_seapp_context_reload();
721 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700722 if (execute(s, buf)) break;
723 }
724 ALOGI("closing connection\n");
725 close(s);
726 }
727
728 return 0;
729}