blob: 62b5bb032a5dde16411679ce5e89f7f326a8f439 [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
Andreas Gampe02d0de52015-11-11 20:43:16 -080017#include <fcntl.h>
Stephen Smalleybd558d62013-04-16 12:16:50 -040018#include <selinux/android.h>
19#include <selinux/avc.h>
Andreas Gampe02d0de52015-11-11 20:43:16 -080020#include <sys/capability.h>
Jeff Sharkeyc7d1b222016-01-11 13:07:09 -070021#include <sys/fsuid.h>
Andreas Gampe02d0de52015-11-11 20:43:16 -080022#include <sys/prctl.h>
23#include <sys/socket.h>
24#include <sys/stat.h>
25
26#include <android-base/logging.h>
27#include <cutils/fs.h>
28#include <cutils/log.h> // TODO: Move everything to base::logging.
29#include <cutils/properties.h>
30#include <cutils/sockets.h>
31#include <private/android_filesystem_config.h>
32
33#include <commands.h>
34#include <globals.h>
35#include <installd_constants.h>
36#include <installd_deps.h> // Need to fill in requirements of commands.
37#include <utils.h>
38
39#ifndef LOG_TAG
40#define LOG_TAG "installd"
41#endif
42#define SOCKET_PATH "installd"
Mike Lockwood94afecf2012-10-24 10:45:23 -070043
Mike Lockwood94afecf2012-10-24 10:45:23 -070044#define BUFFER_MAX 1024 /* input buffer for commands */
Fyodor Kupolov88ce4ff2015-03-03 12:25:29 -080045#define TOKEN_MAX 16 /* max number of arguments in buffer */
Mike Lockwood94afecf2012-10-24 10:45:23 -070046#define REPLY_MAX 256 /* largest reply allowed */
47
Jeff Sharkeyc7d1b222016-01-11 13:07:09 -070048#define DEBUG_FBE 0
Andreas Gampe02d0de52015-11-11 20:43:16 -080049
50namespace android {
51namespace installd {
52
53// Check that installd-deps sizes match cutils sizes.
54static_assert(kPropertyKeyMax == PROPERTY_KEY_MAX, "Size mismatch.");
55static_assert(kPropertyValueMax == PROPERTY_VALUE_MAX, "Size mismatch.");
56
57////////////////////////
58// Plug-in functions. //
59////////////////////////
60
61int get_property(const char *key, char *value, const char *default_value) {
62 return property_get(key, value, default_value);
63}
64
65// Compute the output path of
66bool calculate_oat_file_path(char path[PKG_PATH_MAX],
67 const char *oat_dir,
68 const char *apk_path,
69 const char *instruction_set) {
70 char *file_name_start;
71 char *file_name_end;
72
73 file_name_start = strrchr(apk_path, '/');
74 if (file_name_start == NULL) {
75 ALOGE("apk_path '%s' has no '/'s in it\n", apk_path);
76 return false;
77 }
78 file_name_end = strrchr(apk_path, '.');
79 if (file_name_end < file_name_start) {
80 ALOGE("apk_path '%s' has no extension\n", apk_path);
81 return false;
82 }
83
84 // Calculate file_name
85 int file_name_len = file_name_end - file_name_start - 1;
86 char file_name[file_name_len + 1];
87 memcpy(file_name, file_name_start + 1, file_name_len);
88 file_name[file_name_len] = '\0';
89
90 // <apk_parent_dir>/oat/<isa>/<file_name>.odex
91 snprintf(path, PKG_PATH_MAX, "%s/%s/%s.odex", oat_dir, instruction_set, file_name);
92 return true;
93}
94
95/*
96 * Computes the odex file for the given apk_path and instruction_set.
97 * /system/framework/whatever.jar -> /system/framework/oat/<isa>/whatever.odex
98 *
99 * Returns false if it failed to determine the odex file path.
100 */
101bool calculate_odex_file_path(char path[PKG_PATH_MAX],
102 const char *apk_path,
103 const char *instruction_set) {
104 if (strlen(apk_path) + strlen("oat/") + strlen(instruction_set)
105 + strlen("/") + strlen("odex") + 1 > PKG_PATH_MAX) {
106 ALOGE("apk_path '%s' may be too long to form odex file path.\n", apk_path);
107 return false;
108 }
109
110 strcpy(path, apk_path);
111 char *end = strrchr(path, '/');
112 if (end == NULL) {
113 ALOGE("apk_path '%s' has no '/'s in it?!\n", apk_path);
114 return false;
115 }
116 const char *apk_end = apk_path + (end - path); // strrchr(apk_path, '/');
117
118 strcpy(end + 1, "oat/"); // path = /system/framework/oat/\0
119 strcat(path, instruction_set); // path = /system/framework/oat/<isa>\0
120 strcat(path, apk_end); // path = /system/framework/oat/<isa>/whatever.jar\0
121 end = strrchr(path, '.');
122 if (end == NULL) {
123 ALOGE("apk_path '%s' has no extension.\n", apk_path);
124 return false;
125 }
126 strcpy(end + 1, "odex");
127 return true;
128}
129
130bool create_cache_path(char path[PKG_PATH_MAX],
131 const char *src,
132 const char *instruction_set) {
133 size_t srclen = strlen(src);
134
135 /* demand that we are an absolute path */
136 if ((src == 0) || (src[0] != '/') || strstr(src,"..")) {
137 return false;
138 }
139
140 if (srclen > PKG_PATH_MAX) { // XXX: PKG_NAME_MAX?
141 return false;
142 }
143
144 size_t dstlen =
145 android_data_dir.len +
146 strlen(DALVIK_CACHE) +
147 1 +
148 strlen(instruction_set) +
149 srclen +
150 strlen(DALVIK_CACHE_POSTFIX) + 2;
151
152 if (dstlen > PKG_PATH_MAX) {
153 return false;
154 }
155
156 sprintf(path,"%s%s/%s/%s%s",
157 android_data_dir.path,
158 DALVIK_CACHE,
159 instruction_set,
160 src + 1, /* skip the leading / */
161 DALVIK_CACHE_POSTFIX);
162
163 char* tmp =
164 path +
165 android_data_dir.len +
166 strlen(DALVIK_CACHE) +
167 1 +
168 strlen(instruction_set) + 1;
169
170 for(; *tmp; tmp++) {
171 if (*tmp == '/') {
172 *tmp = '@';
173 }
174 }
175
176 return true;
177}
178
179
Jeff Sharkey6fe28a02015-04-09 13:10:03 -0700180static char* parse_null(char* arg) {
181 if (strcmp(arg, "!") == 0) {
182 return nullptr;
183 } else {
184 return arg;
185 }
186}
187
Andreas Gampe02d0de52015-11-11 20:43:16 -0800188static int do_ping(char **arg ATTRIBUTE_UNUSED, char reply[REPLY_MAX] ATTRIBUTE_UNUSED)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700189{
190 return 0;
191}
192
Jeff Sharkeyc7d1b222016-01-11 13:07:09 -0700193static int do_create_app_data(char **arg, char reply[REPLY_MAX] ATTRIBUTE_UNUSED) {
194 /* const char *uuid, const char *pkgname, userid_t userid, int flags,
195 appid_t appid, const char* seinfo */
196 return create_app_data(parse_null(arg[0]), arg[1], atoi(arg[2]), atoi(arg[3]), atoi(arg[4]), arg[5]);
197}
198
199static int do_restorecon_app_data(char **arg, char reply[REPLY_MAX] ATTRIBUTE_UNUSED) {
200 /* const char* uuid, const char* pkgName, userid_t userid, int flags,
201 appid_t appid, const char* seinfo */
202 return restorecon_app_data(parse_null(arg[0]), arg[1], atoi(arg[2]), atoi(arg[3]), atoi(arg[4]), arg[5]);
203}
204
205static int do_clear_app_data(char **arg, char reply[REPLY_MAX] ATTRIBUTE_UNUSED) {
206 /* const char *uuid, const char *pkgname, userid_t userid, int flags */
207 return clear_app_data(parse_null(arg[0]), arg[1], atoi(arg[2]), atoi(arg[3]));
208}
209
210static int do_destroy_app_data(char **arg, char reply[REPLY_MAX] ATTRIBUTE_UNUSED) {
211 /* const char *uuid, const char *pkgname, userid_t userid, int flags */
212 return destroy_app_data(parse_null(arg[0]), arg[1], atoi(arg[2]), atoi(arg[3]));
Mike Lockwood94afecf2012-10-24 10:45:23 -0700213}
214
Andreas Gampe02d0de52015-11-11 20:43:16 -0800215static int do_dexopt(char **arg, char reply[REPLY_MAX] ATTRIBUTE_UNUSED)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700216{
Calin Juravle60a794d2015-12-24 12:36:41 +0200217 /* apk_path, uid, pkgname, instruction_set, dexopt_needed, oat_dir, dexopt_flags, volume_uuid,
218 use_profiles */
Todd Kennedy76e767c2015-09-25 07:47:47 -0700219 return dexopt(arg[0], atoi(arg[1]), arg[2], arg[3], atoi(arg[4]),
Calin Juravle60a794d2015-12-24 12:36:41 +0200220 arg[5], atoi(arg[6]), parse_null(arg[7]), (atoi(arg[8]) == 0 ? false : true));
Mike Lockwood94afecf2012-10-24 10:45:23 -0700221}
222
Andreas Gampe02d0de52015-11-11 20:43:16 -0800223static int do_mark_boot_complete(char **arg, char reply[REPLY_MAX] ATTRIBUTE_UNUSED)
Narayan Kamath091ea772014-11-10 15:03:46 +0000224{
225 return mark_boot_complete(arg[0] /* instruction set */);
226}
227
Andreas Gampe02d0de52015-11-11 20:43:16 -0800228static int do_rm_dex(char **arg, char reply[REPLY_MAX] ATTRIBUTE_UNUSED)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700229{
Narayan Kamath1b400322014-04-11 13:17:00 +0100230 return rm_dex(arg[0], arg[1]); /* pkgname, instruction_set */
Mike Lockwood94afecf2012-10-24 10:45:23 -0700231}
232
Andreas Gampe02d0de52015-11-11 20:43:16 -0800233static int do_free_cache(char **arg, char reply[REPLY_MAX] ATTRIBUTE_UNUSED) /* TODO int:free_size */
Mike Lockwood94afecf2012-10-24 10:45:23 -0700234{
Jeff Sharkey6fe28a02015-04-09 13:10:03 -0700235 return free_cache(parse_null(arg[0]), (int64_t)atoll(arg[1])); /* uuid, free_size */
Mike Lockwood94afecf2012-10-24 10:45:23 -0700236}
237
Jeff Sharkeyc7d1b222016-01-11 13:07:09 -0700238static int do_get_app_size(char **arg, char reply[REPLY_MAX]) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700239 int64_t codesize = 0;
240 int64_t datasize = 0;
241 int64_t cachesize = 0;
242 int64_t asecsize = 0;
243 int res = 0;
244
Jeff Sharkeyc7d1b222016-01-11 13:07:09 -0700245 /* const char *uuid, const char *pkgname, userid_t userid, int flags,
246 const char *apkpath, const char *libdirpath, const char *fwdlock_apkpath,
247 const char *asecpath, const char *instruction_set */
248 res = get_app_size(parse_null(arg[0]), arg[1], atoi(arg[2]), atoi(arg[3]), arg[4], arg[5],
249 arg[6], arg[7], arg[8], &codesize, &datasize, &cachesize, &asecsize);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700250
251 /*
252 * Each int64_t can take up 22 characters printed out. Make sure it
253 * doesn't go over REPLY_MAX in the future.
254 */
255 snprintf(reply, REPLY_MAX, "%" PRId64 " %" PRId64 " %" PRId64 " %" PRId64,
256 codesize, datasize, cachesize, asecsize);
257 return res;
258}
259
Jeff Sharkeyc7d1b222016-01-11 13:07:09 -0700260static int do_move_complete_app(char **arg, char reply[REPLY_MAX] ATTRIBUTE_UNUSED) {
261 /* const char* from_uuid, const char *to_uuid, const char *package_name,
262 const char *data_app_name, appid_t appid, const char* seinfo */
263 return move_complete_app(parse_null(arg[0]), parse_null(arg[1]), arg[2], arg[3], atoi(arg[4]), arg[5]);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700264}
265
Andreas Gampe02d0de52015-11-11 20:43:16 -0800266static int do_mk_user_config(char **arg, char reply[REPLY_MAX] ATTRIBUTE_UNUSED)
Robin Lee095c7632014-04-25 15:05:19 +0100267{
Robin Lee7c8bec02014-06-10 18:46:26 +0100268 return make_user_config(atoi(arg[0])); /* userid */
Robin Lee095c7632014-04-25 15:05:19 +0100269}
270
Andreas Gampe02d0de52015-11-11 20:43:16 -0800271static int do_rm_user(char **arg, char reply[REPLY_MAX] ATTRIBUTE_UNUSED)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700272{
Jeff Sharkey6fe28a02015-04-09 13:10:03 -0700273 return delete_user(parse_null(arg[0]), atoi(arg[1])); /* uuid, userid */
Mike Lockwood94afecf2012-10-24 10:45:23 -0700274}
275
Andreas Gampe02d0de52015-11-11 20:43:16 -0800276static int do_movefiles(char **arg ATTRIBUTE_UNUSED, char reply[REPLY_MAX] ATTRIBUTE_UNUSED)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700277{
278 return movefiles();
279}
280
Andreas Gampe02d0de52015-11-11 20:43:16 -0800281static int do_linklib(char **arg, char reply[REPLY_MAX] ATTRIBUTE_UNUSED)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700282{
Jeff Sharkey6fe28a02015-04-09 13:10:03 -0700283 return linklib(parse_null(arg[0]), arg[1], arg[2], atoi(arg[3]));
Mike Lockwood94afecf2012-10-24 10:45:23 -0700284}
285
Andreas Gampe02d0de52015-11-11 20:43:16 -0800286static int do_idmap(char **arg, char reply[REPLY_MAX] ATTRIBUTE_UNUSED)
MÃ¥rten Kongstad63568b12014-01-31 14:42:59 +0100287{
288 return idmap(arg[0], arg[1], atoi(arg[2]));
289}
290
Andreas Gampe02d0de52015-11-11 20:43:16 -0800291static int do_create_oat_dir(char **arg, char reply[REPLY_MAX] ATTRIBUTE_UNUSED)
Fyodor Kupolov88ce4ff2015-03-03 12:25:29 -0800292{
293 /* oat_dir, instruction_set */
294 return create_oat_dir(arg[0], arg[1]);
295}
296
Andreas Gampe02d0de52015-11-11 20:43:16 -0800297static int do_rm_package_dir(char **arg, char reply[REPLY_MAX] ATTRIBUTE_UNUSED)
Fyodor Kupolov88ce4ff2015-03-03 12:25:29 -0800298{
299 /* oat_dir */
300 return rm_package_dir(arg[0]);
Alex Light43c5d302014-07-21 12:23:48 -0700301}
302
Andreas Gampe02d0de52015-11-11 20:43:16 -0800303static int do_link_file(char **arg, char reply[REPLY_MAX] ATTRIBUTE_UNUSED)
Narayan Kamathd845c962015-06-04 13:20:27 +0100304{
305 /* relative_path, from_base, to_base */
306 return link_file(arg[0], arg[1], arg[2]);
307}
308
Mike Lockwood94afecf2012-10-24 10:45:23 -0700309struct cmdinfo {
310 const char *name;
311 unsigned numargs;
312 int (*func)(char **arg, char reply[REPLY_MAX]);
313};
314
315struct cmdinfo cmds[] = {
316 { "ping", 0, do_ping },
Jeff Sharkeyc7d1b222016-01-11 13:07:09 -0700317
318 { "create_app_data", 6, do_create_app_data },
319 { "restorecon_app_data", 6, do_restorecon_app_data },
320 { "clear_app_data", 4, do_clear_app_data },
321 { "destroy_app_data", 4, do_destroy_app_data },
322 { "move_complete_app", 6, do_move_complete_app },
323 { "get_app_size", 9, do_get_app_size },
324
Calin Juravle60a794d2015-12-24 12:36:41 +0200325 { "dexopt", 9, do_dexopt },
Narayan Kamath091ea772014-11-10 15:03:46 +0000326 { "markbootcomplete", 1, do_mark_boot_complete },
Narayan Kamath1b400322014-04-11 13:17:00 +0100327 { "rmdex", 2, do_rm_dex },
Jeff Sharkey6fe28a02015-04-09 13:10:03 -0700328 { "freecache", 2, do_free_cache },
Mike Lockwood94afecf2012-10-24 10:45:23 -0700329 { "movefiles", 0, do_movefiles },
Jeff Sharkey6fe28a02015-04-09 13:10:03 -0700330 { "linklib", 4, do_linklib },
Robin Lee7c8bec02014-06-10 18:46:26 +0100331 { "mkuserconfig", 1, do_mk_user_config },
Jeff Sharkey6fe28a02015-04-09 13:10:03 -0700332 { "rmuser", 2, do_rm_user },
MÃ¥rten Kongstad63568b12014-01-31 14:42:59 +0100333 { "idmap", 3, do_idmap },
Fyodor Kupolov88ce4ff2015-03-03 12:25:29 -0800334 { "createoatdir", 2, do_create_oat_dir },
Narayan Kamathd845c962015-06-04 13:20:27 +0100335 { "rmpackagedir", 1, do_rm_package_dir },
Jeff Sharkeyc7d1b222016-01-11 13:07:09 -0700336 { "linkfile", 3, do_link_file },
Mike Lockwood94afecf2012-10-24 10:45:23 -0700337};
338
339static int readx(int s, void *_buf, int count)
340{
Jeff Sharkey19803802015-04-07 12:44:51 -0700341 char *buf = (char *) _buf;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700342 int n = 0, r;
343 if (count < 0) return -1;
344 while (n < count) {
345 r = read(s, buf + n, count - n);
346 if (r < 0) {
347 if (errno == EINTR) continue;
348 ALOGE("read error: %s\n", strerror(errno));
349 return -1;
350 }
351 if (r == 0) {
352 ALOGE("eof\n");
353 return -1; /* EOF */
354 }
355 n += r;
356 }
357 return 0;
358}
359
360static int writex(int s, const void *_buf, int count)
361{
Jeff Sharkey19803802015-04-07 12:44:51 -0700362 const char *buf = (const char *) _buf;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700363 int n = 0, r;
364 if (count < 0) return -1;
365 while (n < count) {
366 r = write(s, buf + n, count - n);
367 if (r < 0) {
368 if (errno == EINTR) continue;
369 ALOGE("write error: %s\n", strerror(errno));
370 return -1;
371 }
372 n += r;
373 }
374 return 0;
375}
376
377
378/* Tokenize the command buffer, locate a matching command,
379 * ensure that the required number of arguments are provided,
380 * call the function(), return the result.
381 */
382static int execute(int s, char cmd[BUFFER_MAX])
383{
384 char reply[REPLY_MAX];
385 char *arg[TOKEN_MAX+1];
386 unsigned i;
387 unsigned n = 0;
388 unsigned short count;
389 int ret = -1;
390
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700391 // ALOGI("execute('%s')\n", cmd);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700392
393 /* default reply is "" */
394 reply[0] = 0;
395
396 /* n is number of args (not counting arg[0]) */
397 arg[0] = cmd;
398 while (*cmd) {
399 if (isspace(*cmd)) {
400 *cmd++ = 0;
401 n++;
402 arg[n] = cmd;
403 if (n == TOKEN_MAX) {
404 ALOGE("too many arguments\n");
405 goto done;
406 }
407 }
Serguei Katkov62bb3852014-10-29 19:38:01 +0600408 if (*cmd) {
409 cmd++;
410 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700411 }
412
413 for (i = 0; i < sizeof(cmds) / sizeof(cmds[0]); i++) {
414 if (!strcmp(cmds[i].name,arg[0])) {
415 if (n != cmds[i].numargs) {
416 ALOGE("%s requires %d arguments (%d given)\n",
417 cmds[i].name, cmds[i].numargs, n);
418 } else {
419 ret = cmds[i].func(arg + 1, reply);
420 }
421 goto done;
422 }
423 }
424 ALOGE("unsupported command '%s'\n", arg[0]);
425
426done:
427 if (reply[0]) {
428 n = snprintf(cmd, BUFFER_MAX, "%d %s", ret, reply);
429 } else {
430 n = snprintf(cmd, BUFFER_MAX, "%d", ret);
431 }
432 if (n > BUFFER_MAX) n = BUFFER_MAX;
433 count = n;
434
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700435 // ALOGI("reply: '%s'\n", cmd);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700436 if (writex(s, &count, sizeof(count))) return -1;
437 if (writex(s, cmd, count)) return -1;
438 return 0;
439}
440
Andreas Gampe02d0de52015-11-11 20:43:16 -0800441bool initialize_globals() {
442 const char* data_path = getenv("ANDROID_DATA");
443 if (data_path == nullptr) {
444 ALOGE("Could not find ANDROID_DATA");
445 return false;
446 }
447 const char* root_path = getenv("ANDROID_ROOT");
448 if (root_path == nullptr) {
449 ALOGE("Could not find ANDROID_ROOT");
450 return false;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700451 }
452
Andreas Gampe02d0de52015-11-11 20:43:16 -0800453 return init_globals_from_data_and_root(data_path, root_path);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700454}
455
Andreas Gampe02d0de52015-11-11 20:43:16 -0800456static int initialize_directories() {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700457 int res = -1;
458
459 // Read current filesystem layout version to handle upgrade paths
460 char version_path[PATH_MAX];
461 snprintf(version_path, PATH_MAX, "%s.layout_version", android_data_dir.path);
462
463 int oldVersion;
464 if (fs_read_atomic_int(version_path, &oldVersion) == -1) {
465 oldVersion = 0;
466 }
467 int version = oldVersion;
468
469 // /data/user
470 char *user_data_dir = build_string2(android_data_dir.path, SECONDARY_USER_PREFIX);
471 // /data/data
472 char *legacy_data_dir = build_string2(android_data_dir.path, PRIMARY_USER_PREFIX);
473 // /data/user/0
474 char *primary_data_dir = build_string3(android_data_dir.path, SECONDARY_USER_PREFIX, "0");
475 if (!user_data_dir || !legacy_data_dir || !primary_data_dir) {
476 goto fail;
477 }
478
479 // Make the /data/user directory if necessary
480 if (access(user_data_dir, R_OK) < 0) {
481 if (mkdir(user_data_dir, 0711) < 0) {
482 goto fail;
483 }
484 if (chown(user_data_dir, AID_SYSTEM, AID_SYSTEM) < 0) {
485 goto fail;
486 }
487 if (chmod(user_data_dir, 0711) < 0) {
488 goto fail;
489 }
490 }
491 // Make the /data/user/0 symlink to /data/data if necessary
492 if (access(primary_data_dir, R_OK) < 0) {
493 if (symlink(legacy_data_dir, primary_data_dir)) {
494 goto fail;
495 }
496 }
497
498 if (version == 0) {
499 // Introducing multi-user, so migrate /data/media contents into /data/media/0
500 ALOGD("Upgrading /data/media for multi-user");
501
502 // Ensure /data/media
503 if (fs_prepare_dir(android_media_dir.path, 0770, AID_MEDIA_RW, AID_MEDIA_RW) == -1) {
504 goto fail;
505 }
506
507 // /data/media.tmp
508 char media_tmp_dir[PATH_MAX];
509 snprintf(media_tmp_dir, PATH_MAX, "%smedia.tmp", android_data_dir.path);
510
511 // Only copy when upgrade not already in progress
512 if (access(media_tmp_dir, F_OK) == -1) {
513 if (rename(android_media_dir.path, media_tmp_dir) == -1) {
514 ALOGE("Failed to move legacy media path: %s", strerror(errno));
515 goto fail;
516 }
517 }
518
519 // Create /data/media again
520 if (fs_prepare_dir(android_media_dir.path, 0770, AID_MEDIA_RW, AID_MEDIA_RW) == -1) {
521 goto fail;
522 }
523
Stephen Smalley26288202014-02-07 09:16:46 -0500524 if (selinux_android_restorecon(android_media_dir.path, 0)) {
Stephen Smalley47a35182013-12-17 16:04:20 -0500525 goto fail;
526 }
527
Mike Lockwood94afecf2012-10-24 10:45:23 -0700528 // /data/media/0
529 char owner_media_dir[PATH_MAX];
530 snprintf(owner_media_dir, PATH_MAX, "%s0", android_media_dir.path);
531
532 // Move any owner data into place
533 if (access(media_tmp_dir, F_OK) == 0) {
534 if (rename(media_tmp_dir, owner_media_dir) == -1) {
535 ALOGE("Failed to move owner media path: %s", strerror(errno));
536 goto fail;
537 }
538 }
539
540 // Ensure media directories for any existing users
541 DIR *dir;
542 struct dirent *dirent;
543 char user_media_dir[PATH_MAX];
544
545 dir = opendir(user_data_dir);
546 if (dir != NULL) {
547 while ((dirent = readdir(dir))) {
548 if (dirent->d_type == DT_DIR) {
549 const char *name = dirent->d_name;
550
551 // skip "." and ".."
552 if (name[0] == '.') {
553 if (name[1] == 0) continue;
554 if ((name[1] == '.') && (name[2] == 0)) continue;
555 }
556
557 // /data/media/<user_id>
558 snprintf(user_media_dir, PATH_MAX, "%s%s", android_media_dir.path, name);
559 if (fs_prepare_dir(user_media_dir, 0770, AID_MEDIA_RW, AID_MEDIA_RW) == -1) {
560 goto fail;
561 }
562 }
563 }
564 closedir(dir);
565 }
566
567 version = 1;
568 }
569
570 // /data/media/obb
571 char media_obb_dir[PATH_MAX];
572 snprintf(media_obb_dir, PATH_MAX, "%sobb", android_media_dir.path);
573
574 if (version == 1) {
575 // Introducing /data/media/obb for sharing OBB across users; migrate
576 // any existing OBB files from owner.
577 ALOGD("Upgrading to shared /data/media/obb");
578
579 // /data/media/0/Android/obb
580 char owner_obb_path[PATH_MAX];
581 snprintf(owner_obb_path, PATH_MAX, "%s0/Android/obb", android_media_dir.path);
582
583 // Only move if target doesn't already exist
584 if (access(media_obb_dir, F_OK) != 0 && access(owner_obb_path, F_OK) == 0) {
585 if (rename(owner_obb_path, media_obb_dir) == -1) {
586 ALOGE("Failed to move OBB from owner: %s", strerror(errno));
587 goto fail;
588 }
589 }
590
591 version = 2;
592 }
593
Jeff Sharkey41ea4242015-04-09 11:34:03 -0700594 if (ensure_media_user_dirs(nullptr, 0) == -1) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700595 ALOGE("Failed to setup media for user 0");
596 goto fail;
597 }
598 if (fs_prepare_dir(media_obb_dir, 0770, AID_MEDIA_RW, AID_MEDIA_RW) == -1) {
599 goto fail;
600 }
601
Robin Lee07053fc2014-04-29 19:42:01 +0100602 if (ensure_config_user_dirs(0) == -1) {
603 ALOGE("Failed to setup misc for user 0");
604 goto fail;
605 }
606
Robin Lee095c7632014-04-25 15:05:19 +0100607 if (version == 2) {
608 ALOGD("Upgrading to /data/misc/user directories");
609
Robin Lee60fd3fe2014-10-07 16:55:02 +0100610 char misc_dir[PATH_MAX];
611 snprintf(misc_dir, PATH_MAX, "%smisc", android_data_dir.path);
612
613 char keychain_added_dir[PATH_MAX];
614 snprintf(keychain_added_dir, PATH_MAX, "%s/keychain/cacerts-added", misc_dir);
615
616 char keychain_removed_dir[PATH_MAX];
617 snprintf(keychain_removed_dir, PATH_MAX, "%s/keychain/cacerts-removed", misc_dir);
618
Robin Lee095c7632014-04-25 15:05:19 +0100619 DIR *dir;
620 struct dirent *dirent;
Robin Lee095c7632014-04-25 15:05:19 +0100621 dir = opendir(user_data_dir);
622 if (dir != NULL) {
623 while ((dirent = readdir(dir))) {
Robin Lee60fd3fe2014-10-07 16:55:02 +0100624 const char *name = dirent->d_name;
Robin Lee095c7632014-04-25 15:05:19 +0100625
Robin Lee60fd3fe2014-10-07 16:55:02 +0100626 // skip "." and ".."
627 if (name[0] == '.') {
628 if (name[1] == 0) continue;
629 if ((name[1] == '.') && (name[2] == 0)) continue;
630 }
631
632 uint32_t user_id = atoi(name);
633
634 // /data/misc/user/<user_id>
635 if (ensure_config_user_dirs(user_id) == -1) {
636 goto fail;
637 }
638
639 char misc_added_dir[PATH_MAX];
640 snprintf(misc_added_dir, PATH_MAX, "%s/user/%s/cacerts-added", misc_dir, name);
641
642 char misc_removed_dir[PATH_MAX];
643 snprintf(misc_removed_dir, PATH_MAX, "%s/user/%s/cacerts-removed", misc_dir, name);
644
645 uid_t uid = multiuser_get_uid(user_id, AID_SYSTEM);
646 gid_t gid = uid;
647 if (access(keychain_added_dir, F_OK) == 0) {
648 if (copy_dir_files(keychain_added_dir, misc_added_dir, uid, gid) != 0) {
649 ALOGE("Some files failed to copy");
Robin Lee095c7632014-04-25 15:05:19 +0100650 }
Robin Lee60fd3fe2014-10-07 16:55:02 +0100651 }
652 if (access(keychain_removed_dir, F_OK) == 0) {
653 if (copy_dir_files(keychain_removed_dir, misc_removed_dir, uid, gid) != 0) {
654 ALOGE("Some files failed to copy");
Robin Lee095c7632014-04-25 15:05:19 +0100655 }
656 }
657 }
658 closedir(dir);
Robin Lee095c7632014-04-25 15:05:19 +0100659
Robin Lee60fd3fe2014-10-07 16:55:02 +0100660 if (access(keychain_added_dir, F_OK) == 0) {
661 delete_dir_contents(keychain_added_dir, 1, 0);
Robin Lee07053fc2014-04-29 19:42:01 +0100662 }
Robin Lee60fd3fe2014-10-07 16:55:02 +0100663 if (access(keychain_removed_dir, F_OK) == 0) {
664 delete_dir_contents(keychain_removed_dir, 1, 0);
Robin Lee07053fc2014-04-29 19:42:01 +0100665 }
666 }
667
668 version = 3;
Robin Lee095c7632014-04-25 15:05:19 +0100669 }
670
Mike Lockwood94afecf2012-10-24 10:45:23 -0700671 // Persist layout version if changed
672 if (version != oldVersion) {
673 if (fs_write_atomic_int(version_path, version) == -1) {
674 ALOGE("Failed to save version to %s: %s", version_path, strerror(errno));
675 goto fail;
676 }
677 }
678
679 // Success!
680 res = 0;
681
682fail:
683 free(user_data_dir);
684 free(legacy_data_dir);
685 free(primary_data_dir);
686 return res;
687}
688
Stephen Smalley7abb52b2014-03-26 09:30:37 -0400689static int log_callback(int type, const char *fmt, ...) {
690 va_list ap;
691 int priority;
692
693 switch (type) {
694 case SELINUX_WARNING:
695 priority = ANDROID_LOG_WARN;
696 break;
697 case SELINUX_INFO:
698 priority = ANDROID_LOG_INFO;
699 break;
700 default:
701 priority = ANDROID_LOG_ERROR;
702 break;
703 }
704 va_start(ap, fmt);
705 LOG_PRI_VA(priority, "SELinux", fmt, ap);
706 va_end(ap);
707 return 0;
708}
709
Andreas Gampe02d0de52015-11-11 20:43:16 -0800710static int installd_main(const int argc ATTRIBUTE_UNUSED, char *argv[]) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700711 char buf[BUFFER_MAX];
712 struct sockaddr addr;
713 socklen_t alen;
Chih-Hung Hsieh99d9fb12014-09-11 14:44:46 -0700714 int lsocket, s;
Stephen Smalleybd558d62013-04-16 12:16:50 -0400715 int selinux_enabled = (is_selinux_enabled() > 0);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700716
Jeff Sharkeye3637242015-04-08 20:56:42 -0700717 setenv("ANDROID_LOG_TAGS", "*:v", 1);
718 android::base::InitLogging(argv);
719
Mike Lockwood94afecf2012-10-24 10:45:23 -0700720 ALOGI("installd firing up\n");
721
Stephen Smalley7abb52b2014-03-26 09:30:37 -0400722 union selinux_callback cb;
723 cb.func_log = log_callback;
724 selinux_set_callback(SELINUX_CB_LOG, cb);
725
Andreas Gampe02d0de52015-11-11 20:43:16 -0800726 if (!initialize_globals()) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700727 ALOGE("Could not initialize globals; exiting.\n");
728 exit(1);
729 }
730
731 if (initialize_directories() < 0) {
732 ALOGE("Could not create directories; exiting.\n");
733 exit(1);
734 }
735
Stephen Smalleybd558d62013-04-16 12:16:50 -0400736 if (selinux_enabled && selinux_status_open(true) < 0) {
737 ALOGE("Could not open selinux status; exiting.\n");
738 exit(1);
739 }
740
Mike Lockwood94afecf2012-10-24 10:45:23 -0700741 lsocket = android_get_control_socket(SOCKET_PATH);
742 if (lsocket < 0) {
743 ALOGE("Failed to get socket from environment: %s\n", strerror(errno));
744 exit(1);
745 }
746 if (listen(lsocket, 5)) {
747 ALOGE("Listen on socket failed: %s\n", strerror(errno));
748 exit(1);
749 }
750 fcntl(lsocket, F_SETFD, FD_CLOEXEC);
751
Jeff Sharkeyc7d1b222016-01-11 13:07:09 -0700752 // Perform all filesystem access as system so that FBE emulation mode
753 // can block access using chmod 000.
754#if DEBUG_FBE
755 setfsuid(AID_SYSTEM);
756#endif
757
Mike Lockwood94afecf2012-10-24 10:45:23 -0700758 for (;;) {
759 alen = sizeof(addr);
760 s = accept(lsocket, &addr, &alen);
761 if (s < 0) {
762 ALOGE("Accept failed: %s\n", strerror(errno));
763 continue;
764 }
765 fcntl(s, F_SETFD, FD_CLOEXEC);
766
767 ALOGI("new connection\n");
768 for (;;) {
769 unsigned short count;
770 if (readx(s, &count, sizeof(count))) {
771 ALOGE("failed to read size\n");
772 break;
773 }
774 if ((count < 1) || (count >= BUFFER_MAX)) {
775 ALOGE("invalid size %d\n", count);
776 break;
777 }
778 if (readx(s, buf, count)) {
779 ALOGE("failed to read command\n");
780 break;
781 }
782 buf[count] = 0;
Stephen Smalleybd558d62013-04-16 12:16:50 -0400783 if (selinux_enabled && selinux_status_updated() > 0) {
784 selinux_android_seapp_context_reload();
785 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700786 if (execute(s, buf)) break;
787 }
788 ALOGI("closing connection\n");
789 close(s);
790 }
791
792 return 0;
793}
Andreas Gampe02d0de52015-11-11 20:43:16 -0800794
795} // namespace installd
796} // namespace android
797
798int main(const int argc, char *argv[]) {
799 return android::installd::installd_main(argc, argv);
800}