blob: 30e4f8b417ffb09ecee8081fac6f21a6599038d5 [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
Elliott Hughese4ec9eb2015-12-04 15:39:32 -080019#include <android-base/logging.h>
Jeff Sharkeye3637242015-04-08 20:56:42 -070020
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 Sharkey31f08982015-07-07 13:31:37 -0700126static int do_cp_complete_app(char **arg, char reply[REPLY_MAX] __unused)
Jeff Sharkeye3637242015-04-08 20:56:42 -0700127{
Jeff Sharkeyd7921182015-04-30 15:58:19 -0700128 // from_uuid, to_uuid, package_name, data_app_name, appid, seinfo
Jeff Sharkey31f08982015-07-07 13:31:37 -0700129 return copy_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 -0700130}
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
Narayan Kamathd845c962015-06-04 13:20:27 +0100181static int do_link_file(char **arg, char reply[REPLY_MAX] __unused)
182{
183 /* relative_path, from_base, to_base */
184 return link_file(arg[0], arg[1], arg[2]);
185}
186
Mike Lockwood94afecf2012-10-24 10:45:23 -0700187struct cmdinfo {
188 const char *name;
189 unsigned numargs;
190 int (*func)(char **arg, char reply[REPLY_MAX]);
191};
192
193struct cmdinfo cmds[] = {
194 { "ping", 0, do_ping },
Jeff Sharkey6fe28a02015-04-09 13:10:03 -0700195 { "install", 5, do_install },
Todd Kennedy76e767c2015-09-25 07:47:47 -0700196 { "dexopt", 7, do_dexopt },
Narayan Kamath091ea772014-11-10 15:03:46 +0000197 { "markbootcomplete", 1, do_mark_boot_complete },
Narayan Kamath1b400322014-04-11 13:17:00 +0100198 { "movedex", 3, do_move_dex },
199 { "rmdex", 2, do_rm_dex },
Jeff Sharkey6fe28a02015-04-09 13:10:03 -0700200 { "remove", 3, do_remove },
Mike Lockwood94afecf2012-10-24 10:45:23 -0700201 { "rename", 2, do_rename },
Jeff Sharkey6fe28a02015-04-09 13:10:03 -0700202 { "fixuid", 4, do_fixuid },
203 { "freecache", 2, do_free_cache },
204 { "rmcache", 3, do_rm_cache },
205 { "rmcodecache", 3, do_rm_code_cache },
206 { "getsize", 8, do_get_size },
207 { "rmuserdata", 3, do_rm_user_data },
Jeff Sharkey31f08982015-07-07 13:31:37 -0700208 { "cpcompleteapp", 6, do_cp_complete_app },
Mike Lockwood94afecf2012-10-24 10:45:23 -0700209 { "movefiles", 0, do_movefiles },
Jeff Sharkey6fe28a02015-04-09 13:10:03 -0700210 { "linklib", 4, do_linklib },
211 { "mkuserdata", 5, do_mk_user_data },
Robin Lee7c8bec02014-06-10 18:46:26 +0100212 { "mkuserconfig", 1, do_mk_user_config },
Jeff Sharkey6fe28a02015-04-09 13:10:03 -0700213 { "rmuser", 2, do_rm_user },
Mårten Kongstad63568b12014-01-31 14:42:59 +0100214 { "idmap", 3, do_idmap },
Jeff Sharkey6fe28a02015-04-09 13:10:03 -0700215 { "restorecondata", 4, do_restorecon_data },
Fyodor Kupolov88ce4ff2015-03-03 12:25:29 -0800216 { "createoatdir", 2, do_create_oat_dir },
Narayan Kamathd845c962015-06-04 13:20:27 +0100217 { "rmpackagedir", 1, do_rm_package_dir },
218 { "linkfile", 3, do_link_file }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700219};
220
221static int readx(int s, void *_buf, int count)
222{
Jeff Sharkey19803802015-04-07 12:44:51 -0700223 char *buf = (char *) _buf;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700224 int n = 0, r;
225 if (count < 0) return -1;
226 while (n < count) {
227 r = read(s, buf + n, count - n);
228 if (r < 0) {
229 if (errno == EINTR) continue;
230 ALOGE("read error: %s\n", strerror(errno));
231 return -1;
232 }
233 if (r == 0) {
234 ALOGE("eof\n");
235 return -1; /* EOF */
236 }
237 n += r;
238 }
239 return 0;
240}
241
242static int writex(int s, const void *_buf, int count)
243{
Jeff Sharkey19803802015-04-07 12:44:51 -0700244 const char *buf = (const char *) _buf;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700245 int n = 0, r;
246 if (count < 0) return -1;
247 while (n < count) {
248 r = write(s, buf + n, count - n);
249 if (r < 0) {
250 if (errno == EINTR) continue;
251 ALOGE("write error: %s\n", strerror(errno));
252 return -1;
253 }
254 n += r;
255 }
256 return 0;
257}
258
259
260/* Tokenize the command buffer, locate a matching command,
261 * ensure that the required number of arguments are provided,
262 * call the function(), return the result.
263 */
264static int execute(int s, char cmd[BUFFER_MAX])
265{
266 char reply[REPLY_MAX];
267 char *arg[TOKEN_MAX+1];
268 unsigned i;
269 unsigned n = 0;
270 unsigned short count;
271 int ret = -1;
272
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700273 // ALOGI("execute('%s')\n", cmd);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700274
275 /* default reply is "" */
276 reply[0] = 0;
277
278 /* n is number of args (not counting arg[0]) */
279 arg[0] = cmd;
280 while (*cmd) {
281 if (isspace(*cmd)) {
282 *cmd++ = 0;
283 n++;
284 arg[n] = cmd;
285 if (n == TOKEN_MAX) {
286 ALOGE("too many arguments\n");
287 goto done;
288 }
289 }
Serguei Katkov62bb3852014-10-29 19:38:01 +0600290 if (*cmd) {
291 cmd++;
292 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700293 }
294
295 for (i = 0; i < sizeof(cmds) / sizeof(cmds[0]); i++) {
296 if (!strcmp(cmds[i].name,arg[0])) {
297 if (n != cmds[i].numargs) {
298 ALOGE("%s requires %d arguments (%d given)\n",
299 cmds[i].name, cmds[i].numargs, n);
300 } else {
301 ret = cmds[i].func(arg + 1, reply);
302 }
303 goto done;
304 }
305 }
306 ALOGE("unsupported command '%s'\n", arg[0]);
307
308done:
309 if (reply[0]) {
310 n = snprintf(cmd, BUFFER_MAX, "%d %s", ret, reply);
311 } else {
312 n = snprintf(cmd, BUFFER_MAX, "%d", ret);
313 }
314 if (n > BUFFER_MAX) n = BUFFER_MAX;
315 count = n;
316
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700317 // ALOGI("reply: '%s'\n", cmd);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700318 if (writex(s, &count, sizeof(count))) return -1;
319 if (writex(s, cmd, count)) return -1;
320 return 0;
321}
322
323/**
324 * Initialize all the global variables that are used elsewhere. Returns 0 upon
325 * success and -1 on error.
326 */
327void free_globals() {
328 size_t i;
329
330 for (i = 0; i < android_system_dirs.count; i++) {
331 if (android_system_dirs.dirs[i].path != NULL) {
332 free(android_system_dirs.dirs[i].path);
333 }
334 }
335
336 free(android_system_dirs.dirs);
337}
338
339int initialize_globals() {
340 // Get the android data directory.
341 if (get_path_from_env(&android_data_dir, "ANDROID_DATA") < 0) {
342 return -1;
343 }
344
345 // Get the android app directory.
346 if (copy_and_append(&android_app_dir, &android_data_dir, APP_SUBDIR) < 0) {
347 return -1;
348 }
349
350 // Get the android protected app directory.
351 if (copy_and_append(&android_app_private_dir, &android_data_dir, PRIVATE_APP_SUBDIR) < 0) {
352 return -1;
353 }
354
355 // Get the android app native library directory.
356 if (copy_and_append(&android_app_lib_dir, &android_data_dir, APP_LIB_SUBDIR) < 0) {
357 return -1;
358 }
359
360 // Get the sd-card ASEC mount point.
361 if (get_path_from_env(&android_asec_dir, "ASEC_MOUNTPOINT") < 0) {
362 return -1;
363 }
364
365 // Get the android media directory.
366 if (copy_and_append(&android_media_dir, &android_data_dir, MEDIA_SUBDIR) < 0) {
367 return -1;
368 }
369
Jeff Sharkeye23a1322015-04-06 16:19:39 -0700370 // Get the android external app directory.
Jeff Sharkey19803802015-04-07 12:44:51 -0700371 if (get_path_from_string(&android_mnt_expand_dir, "/mnt/expand/") < 0) {
Jeff Sharkeye23a1322015-04-06 16:19:39 -0700372 return -1;
373 }
374
Mike Lockwood94afecf2012-10-24 10:45:23 -0700375 // Take note of the system and vendor directories.
Jeff Sharkey770180a2014-09-08 17:14:26 -0700376 android_system_dirs.count = 4;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700377
Jeff Sharkey19803802015-04-07 12:44:51 -0700378 android_system_dirs.dirs = (dir_rec_t*) calloc(android_system_dirs.count, sizeof(dir_rec_t));
Mike Lockwood94afecf2012-10-24 10:45:23 -0700379 if (android_system_dirs.dirs == NULL) {
380 ALOGE("Couldn't allocate array for dirs; aborting\n");
381 return -1;
382 }
383
Jeff Sharkey770180a2014-09-08 17:14:26 -0700384 dir_rec_t android_root_dir;
385 if (get_path_from_env(&android_root_dir, "ANDROID_ROOT") < 0) {
386 ALOGE("Missing ANDROID_ROOT; aborting\n");
Mike Lockwood94afecf2012-10-24 10:45:23 -0700387 return -1;
388 }
389
Jeff Sharkey770180a2014-09-08 17:14:26 -0700390 android_system_dirs.dirs[0].path = build_string2(android_root_dir.path, APP_SUBDIR);
391 android_system_dirs.dirs[0].len = strlen(android_system_dirs.dirs[0].path);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700392
Jeff Sharkey770180a2014-09-08 17:14:26 -0700393 android_system_dirs.dirs[1].path = build_string2(android_root_dir.path, PRIV_APP_SUBDIR);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700394 android_system_dirs.dirs[1].len = strlen(android_system_dirs.dirs[1].path);
395
Jeff Sharkey19803802015-04-07 12:44:51 -0700396 android_system_dirs.dirs[2].path = strdup("/vendor/app/");
Jeff Sharkey770180a2014-09-08 17:14:26 -0700397 android_system_dirs.dirs[2].len = strlen(android_system_dirs.dirs[2].path);
398
Jeff Sharkey19803802015-04-07 12:44:51 -0700399 android_system_dirs.dirs[3].path = strdup("/oem/app/");
Jeff Sharkey770180a2014-09-08 17:14:26 -0700400 android_system_dirs.dirs[3].len = strlen(android_system_dirs.dirs[3].path);
401
Mike Lockwood94afecf2012-10-24 10:45:23 -0700402 return 0;
403}
404
405int initialize_directories() {
406 int res = -1;
407
408 // Read current filesystem layout version to handle upgrade paths
409 char version_path[PATH_MAX];
410 snprintf(version_path, PATH_MAX, "%s.layout_version", android_data_dir.path);
411
412 int oldVersion;
413 if (fs_read_atomic_int(version_path, &oldVersion) == -1) {
414 oldVersion = 0;
415 }
416 int version = oldVersion;
417
418 // /data/user
419 char *user_data_dir = build_string2(android_data_dir.path, SECONDARY_USER_PREFIX);
420 // /data/data
421 char *legacy_data_dir = build_string2(android_data_dir.path, PRIMARY_USER_PREFIX);
422 // /data/user/0
423 char *primary_data_dir = build_string3(android_data_dir.path, SECONDARY_USER_PREFIX, "0");
424 if (!user_data_dir || !legacy_data_dir || !primary_data_dir) {
425 goto fail;
426 }
427
428 // Make the /data/user directory if necessary
429 if (access(user_data_dir, R_OK) < 0) {
430 if (mkdir(user_data_dir, 0711) < 0) {
431 goto fail;
432 }
433 if (chown(user_data_dir, AID_SYSTEM, AID_SYSTEM) < 0) {
434 goto fail;
435 }
436 if (chmod(user_data_dir, 0711) < 0) {
437 goto fail;
438 }
439 }
440 // Make the /data/user/0 symlink to /data/data if necessary
441 if (access(primary_data_dir, R_OK) < 0) {
442 if (symlink(legacy_data_dir, primary_data_dir)) {
443 goto fail;
444 }
445 }
446
447 if (version == 0) {
448 // Introducing multi-user, so migrate /data/media contents into /data/media/0
449 ALOGD("Upgrading /data/media for multi-user");
450
451 // Ensure /data/media
452 if (fs_prepare_dir(android_media_dir.path, 0770, AID_MEDIA_RW, AID_MEDIA_RW) == -1) {
453 goto fail;
454 }
455
456 // /data/media.tmp
457 char media_tmp_dir[PATH_MAX];
458 snprintf(media_tmp_dir, PATH_MAX, "%smedia.tmp", android_data_dir.path);
459
460 // Only copy when upgrade not already in progress
461 if (access(media_tmp_dir, F_OK) == -1) {
462 if (rename(android_media_dir.path, media_tmp_dir) == -1) {
463 ALOGE("Failed to move legacy media path: %s", strerror(errno));
464 goto fail;
465 }
466 }
467
468 // Create /data/media again
469 if (fs_prepare_dir(android_media_dir.path, 0770, AID_MEDIA_RW, AID_MEDIA_RW) == -1) {
470 goto fail;
471 }
472
Stephen Smalley26288202014-02-07 09:16:46 -0500473 if (selinux_android_restorecon(android_media_dir.path, 0)) {
Stephen Smalley47a35182013-12-17 16:04:20 -0500474 goto fail;
475 }
476
Mike Lockwood94afecf2012-10-24 10:45:23 -0700477 // /data/media/0
478 char owner_media_dir[PATH_MAX];
479 snprintf(owner_media_dir, PATH_MAX, "%s0", android_media_dir.path);
480
481 // Move any owner data into place
482 if (access(media_tmp_dir, F_OK) == 0) {
483 if (rename(media_tmp_dir, owner_media_dir) == -1) {
484 ALOGE("Failed to move owner media path: %s", strerror(errno));
485 goto fail;
486 }
487 }
488
489 // Ensure media directories for any existing users
490 DIR *dir;
491 struct dirent *dirent;
492 char user_media_dir[PATH_MAX];
493
494 dir = opendir(user_data_dir);
495 if (dir != NULL) {
496 while ((dirent = readdir(dir))) {
497 if (dirent->d_type == DT_DIR) {
498 const char *name = dirent->d_name;
499
500 // skip "." and ".."
501 if (name[0] == '.') {
502 if (name[1] == 0) continue;
503 if ((name[1] == '.') && (name[2] == 0)) continue;
504 }
505
506 // /data/media/<user_id>
507 snprintf(user_media_dir, PATH_MAX, "%s%s", android_media_dir.path, name);
508 if (fs_prepare_dir(user_media_dir, 0770, AID_MEDIA_RW, AID_MEDIA_RW) == -1) {
509 goto fail;
510 }
511 }
512 }
513 closedir(dir);
514 }
515
516 version = 1;
517 }
518
519 // /data/media/obb
520 char media_obb_dir[PATH_MAX];
521 snprintf(media_obb_dir, PATH_MAX, "%sobb", android_media_dir.path);
522
523 if (version == 1) {
524 // Introducing /data/media/obb for sharing OBB across users; migrate
525 // any existing OBB files from owner.
526 ALOGD("Upgrading to shared /data/media/obb");
527
528 // /data/media/0/Android/obb
529 char owner_obb_path[PATH_MAX];
530 snprintf(owner_obb_path, PATH_MAX, "%s0/Android/obb", android_media_dir.path);
531
532 // Only move if target doesn't already exist
533 if (access(media_obb_dir, F_OK) != 0 && access(owner_obb_path, F_OK) == 0) {
534 if (rename(owner_obb_path, media_obb_dir) == -1) {
535 ALOGE("Failed to move OBB from owner: %s", strerror(errno));
536 goto fail;
537 }
538 }
539
540 version = 2;
541 }
542
Jeff Sharkey41ea4242015-04-09 11:34:03 -0700543 if (ensure_media_user_dirs(nullptr, 0) == -1) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700544 ALOGE("Failed to setup media for user 0");
545 goto fail;
546 }
547 if (fs_prepare_dir(media_obb_dir, 0770, AID_MEDIA_RW, AID_MEDIA_RW) == -1) {
548 goto fail;
549 }
550
Robin Lee07053fc2014-04-29 19:42:01 +0100551 if (ensure_config_user_dirs(0) == -1) {
552 ALOGE("Failed to setup misc for user 0");
553 goto fail;
554 }
555
Robin Lee095c7632014-04-25 15:05:19 +0100556 if (version == 2) {
557 ALOGD("Upgrading to /data/misc/user directories");
558
Robin Lee60fd3fe2014-10-07 16:55:02 +0100559 char misc_dir[PATH_MAX];
560 snprintf(misc_dir, PATH_MAX, "%smisc", android_data_dir.path);
561
562 char keychain_added_dir[PATH_MAX];
563 snprintf(keychain_added_dir, PATH_MAX, "%s/keychain/cacerts-added", misc_dir);
564
565 char keychain_removed_dir[PATH_MAX];
566 snprintf(keychain_removed_dir, PATH_MAX, "%s/keychain/cacerts-removed", misc_dir);
567
Robin Lee095c7632014-04-25 15:05:19 +0100568 DIR *dir;
569 struct dirent *dirent;
Robin Lee095c7632014-04-25 15:05:19 +0100570 dir = opendir(user_data_dir);
571 if (dir != NULL) {
572 while ((dirent = readdir(dir))) {
Robin Lee60fd3fe2014-10-07 16:55:02 +0100573 const char *name = dirent->d_name;
Robin Lee095c7632014-04-25 15:05:19 +0100574
Robin Lee60fd3fe2014-10-07 16:55:02 +0100575 // skip "." and ".."
576 if (name[0] == '.') {
577 if (name[1] == 0) continue;
578 if ((name[1] == '.') && (name[2] == 0)) continue;
579 }
580
581 uint32_t user_id = atoi(name);
582
583 // /data/misc/user/<user_id>
584 if (ensure_config_user_dirs(user_id) == -1) {
585 goto fail;
586 }
587
588 char misc_added_dir[PATH_MAX];
589 snprintf(misc_added_dir, PATH_MAX, "%s/user/%s/cacerts-added", misc_dir, name);
590
591 char misc_removed_dir[PATH_MAX];
592 snprintf(misc_removed_dir, PATH_MAX, "%s/user/%s/cacerts-removed", misc_dir, name);
593
594 uid_t uid = multiuser_get_uid(user_id, AID_SYSTEM);
595 gid_t gid = uid;
596 if (access(keychain_added_dir, F_OK) == 0) {
597 if (copy_dir_files(keychain_added_dir, misc_added_dir, uid, gid) != 0) {
598 ALOGE("Some files failed to copy");
Robin Lee095c7632014-04-25 15:05:19 +0100599 }
Robin Lee60fd3fe2014-10-07 16:55:02 +0100600 }
601 if (access(keychain_removed_dir, F_OK) == 0) {
602 if (copy_dir_files(keychain_removed_dir, misc_removed_dir, uid, gid) != 0) {
603 ALOGE("Some files failed to copy");
Robin Lee095c7632014-04-25 15:05:19 +0100604 }
605 }
606 }
607 closedir(dir);
Robin Lee095c7632014-04-25 15:05:19 +0100608
Robin Lee60fd3fe2014-10-07 16:55:02 +0100609 if (access(keychain_added_dir, F_OK) == 0) {
610 delete_dir_contents(keychain_added_dir, 1, 0);
Robin Lee07053fc2014-04-29 19:42:01 +0100611 }
Robin Lee60fd3fe2014-10-07 16:55:02 +0100612 if (access(keychain_removed_dir, F_OK) == 0) {
613 delete_dir_contents(keychain_removed_dir, 1, 0);
Robin Lee07053fc2014-04-29 19:42:01 +0100614 }
615 }
616
617 version = 3;
Robin Lee095c7632014-04-25 15:05:19 +0100618 }
619
Mike Lockwood94afecf2012-10-24 10:45:23 -0700620 // Persist layout version if changed
621 if (version != oldVersion) {
622 if (fs_write_atomic_int(version_path, version) == -1) {
623 ALOGE("Failed to save version to %s: %s", version_path, strerror(errno));
624 goto fail;
625 }
626 }
627
628 // Success!
629 res = 0;
630
631fail:
632 free(user_data_dir);
633 free(legacy_data_dir);
634 free(primary_data_dir);
635 return res;
636}
637
Stephen Smalley7abb52b2014-03-26 09:30:37 -0400638static int log_callback(int type, const char *fmt, ...) {
639 va_list ap;
640 int priority;
641
642 switch (type) {
643 case SELINUX_WARNING:
644 priority = ANDROID_LOG_WARN;
645 break;
646 case SELINUX_INFO:
647 priority = ANDROID_LOG_INFO;
648 break;
649 default:
650 priority = ANDROID_LOG_ERROR;
651 break;
652 }
653 va_start(ap, fmt);
654 LOG_PRI_VA(priority, "SELinux", fmt, ap);
655 va_end(ap);
656 return 0;
657}
658
Jeff Sharkeye3637242015-04-08 20:56:42 -0700659int main(const int argc __unused, char *argv[]) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700660 char buf[BUFFER_MAX];
661 struct sockaddr addr;
662 socklen_t alen;
Chih-Hung Hsieh99d9fb12014-09-11 14:44:46 -0700663 int lsocket, s;
Stephen Smalleybd558d62013-04-16 12:16:50 -0400664 int selinux_enabled = (is_selinux_enabled() > 0);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700665
Jeff Sharkeye3637242015-04-08 20:56:42 -0700666 setenv("ANDROID_LOG_TAGS", "*:v", 1);
667 android::base::InitLogging(argv);
668
Mike Lockwood94afecf2012-10-24 10:45:23 -0700669 ALOGI("installd firing up\n");
670
Stephen Smalley7abb52b2014-03-26 09:30:37 -0400671 union selinux_callback cb;
672 cb.func_log = log_callback;
673 selinux_set_callback(SELINUX_CB_LOG, cb);
674
Mike Lockwood94afecf2012-10-24 10:45:23 -0700675 if (initialize_globals() < 0) {
676 ALOGE("Could not initialize globals; exiting.\n");
677 exit(1);
678 }
679
680 if (initialize_directories() < 0) {
681 ALOGE("Could not create directories; exiting.\n");
682 exit(1);
683 }
684
Stephen Smalleybd558d62013-04-16 12:16:50 -0400685 if (selinux_enabled && selinux_status_open(true) < 0) {
686 ALOGE("Could not open selinux status; exiting.\n");
687 exit(1);
688 }
689
Mike Lockwood94afecf2012-10-24 10:45:23 -0700690 lsocket = android_get_control_socket(SOCKET_PATH);
691 if (lsocket < 0) {
692 ALOGE("Failed to get socket from environment: %s\n", strerror(errno));
693 exit(1);
694 }
695 if (listen(lsocket, 5)) {
696 ALOGE("Listen on socket failed: %s\n", strerror(errno));
697 exit(1);
698 }
699 fcntl(lsocket, F_SETFD, FD_CLOEXEC);
700
701 for (;;) {
702 alen = sizeof(addr);
703 s = accept(lsocket, &addr, &alen);
704 if (s < 0) {
705 ALOGE("Accept failed: %s\n", strerror(errno));
706 continue;
707 }
708 fcntl(s, F_SETFD, FD_CLOEXEC);
709
710 ALOGI("new connection\n");
711 for (;;) {
712 unsigned short count;
713 if (readx(s, &count, sizeof(count))) {
714 ALOGE("failed to read size\n");
715 break;
716 }
717 if ((count < 1) || (count >= BUFFER_MAX)) {
718 ALOGE("invalid size %d\n", count);
719 break;
720 }
721 if (readx(s, buf, count)) {
722 ALOGE("failed to read command\n");
723 break;
724 }
725 buf[count] = 0;
Stephen Smalleybd558d62013-04-16 12:16:50 -0400726 if (selinux_enabled && selinux_status_updated() > 0) {
727 selinux_android_seapp_context_reload();
728 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700729 if (execute(s, buf)) break;
730 }
731 ALOGI("closing connection\n");
732 close(s);
733 }
734
735 return 0;
736}