blob: 31fd7038ad3525aca818463fb25fdf06933a2bed [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
Andreas Gampe02d0de52015-11-11 20:43:16 -080048namespace android {
49namespace installd {
50
51// Check that installd-deps sizes match cutils sizes.
52static_assert(kPropertyKeyMax == PROPERTY_KEY_MAX, "Size mismatch.");
53static_assert(kPropertyValueMax == PROPERTY_VALUE_MAX, "Size mismatch.");
54
55////////////////////////
56// Plug-in functions. //
57////////////////////////
58
59int get_property(const char *key, char *value, const char *default_value) {
60 return property_get(key, value, default_value);
61}
62
63// Compute the output path of
64bool calculate_oat_file_path(char path[PKG_PATH_MAX],
65 const char *oat_dir,
66 const char *apk_path,
67 const char *instruction_set) {
68 char *file_name_start;
69 char *file_name_end;
70
71 file_name_start = strrchr(apk_path, '/');
72 if (file_name_start == NULL) {
73 ALOGE("apk_path '%s' has no '/'s in it\n", apk_path);
74 return false;
75 }
76 file_name_end = strrchr(apk_path, '.');
77 if (file_name_end < file_name_start) {
78 ALOGE("apk_path '%s' has no extension\n", apk_path);
79 return false;
80 }
81
82 // Calculate file_name
83 int file_name_len = file_name_end - file_name_start - 1;
84 char file_name[file_name_len + 1];
85 memcpy(file_name, file_name_start + 1, file_name_len);
86 file_name[file_name_len] = '\0';
87
88 // <apk_parent_dir>/oat/<isa>/<file_name>.odex
89 snprintf(path, PKG_PATH_MAX, "%s/%s/%s.odex", oat_dir, instruction_set, file_name);
90 return true;
91}
92
93/*
94 * Computes the odex file for the given apk_path and instruction_set.
95 * /system/framework/whatever.jar -> /system/framework/oat/<isa>/whatever.odex
96 *
97 * Returns false if it failed to determine the odex file path.
98 */
99bool calculate_odex_file_path(char path[PKG_PATH_MAX],
100 const char *apk_path,
101 const char *instruction_set) {
102 if (strlen(apk_path) + strlen("oat/") + strlen(instruction_set)
103 + strlen("/") + strlen("odex") + 1 > PKG_PATH_MAX) {
104 ALOGE("apk_path '%s' may be too long to form odex file path.\n", apk_path);
105 return false;
106 }
107
108 strcpy(path, apk_path);
109 char *end = strrchr(path, '/');
110 if (end == NULL) {
111 ALOGE("apk_path '%s' has no '/'s in it?!\n", apk_path);
112 return false;
113 }
114 const char *apk_end = apk_path + (end - path); // strrchr(apk_path, '/');
115
116 strcpy(end + 1, "oat/"); // path = /system/framework/oat/\0
117 strcat(path, instruction_set); // path = /system/framework/oat/<isa>\0
118 strcat(path, apk_end); // path = /system/framework/oat/<isa>/whatever.jar\0
119 end = strrchr(path, '.');
120 if (end == NULL) {
121 ALOGE("apk_path '%s' has no extension.\n", apk_path);
122 return false;
123 }
124 strcpy(end + 1, "odex");
125 return true;
126}
127
128bool create_cache_path(char path[PKG_PATH_MAX],
129 const char *src,
130 const char *instruction_set) {
131 size_t srclen = strlen(src);
132
133 /* demand that we are an absolute path */
134 if ((src == 0) || (src[0] != '/') || strstr(src,"..")) {
135 return false;
136 }
137
138 if (srclen > PKG_PATH_MAX) { // XXX: PKG_NAME_MAX?
139 return false;
140 }
141
142 size_t dstlen =
143 android_data_dir.len +
144 strlen(DALVIK_CACHE) +
145 1 +
146 strlen(instruction_set) +
147 srclen +
148 strlen(DALVIK_CACHE_POSTFIX) + 2;
149
150 if (dstlen > PKG_PATH_MAX) {
151 return false;
152 }
153
154 sprintf(path,"%s%s/%s/%s%s",
155 android_data_dir.path,
156 DALVIK_CACHE,
157 instruction_set,
158 src + 1, /* skip the leading / */
159 DALVIK_CACHE_POSTFIX);
160
161 char* tmp =
162 path +
163 android_data_dir.len +
164 strlen(DALVIK_CACHE) +
165 1 +
166 strlen(instruction_set) + 1;
167
168 for(; *tmp; tmp++) {
169 if (*tmp == '/') {
170 *tmp = '@';
171 }
172 }
173
174 return true;
175}
176
177
Jeff Sharkey6fe28a02015-04-09 13:10:03 -0700178static char* parse_null(char* arg) {
179 if (strcmp(arg, "!") == 0) {
180 return nullptr;
181 } else {
182 return arg;
183 }
184}
185
Andreas Gampe02d0de52015-11-11 20:43:16 -0800186static int do_ping(char **arg ATTRIBUTE_UNUSED, char reply[REPLY_MAX] ATTRIBUTE_UNUSED)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700187{
188 return 0;
189}
190
Jeff Sharkeyc7d1b222016-01-11 13:07:09 -0700191static int do_create_app_data(char **arg, char reply[REPLY_MAX] ATTRIBUTE_UNUSED) {
192 /* const char *uuid, const char *pkgname, userid_t userid, int flags,
193 appid_t appid, const char* seinfo */
194 return create_app_data(parse_null(arg[0]), arg[1], atoi(arg[2]), atoi(arg[3]), atoi(arg[4]), arg[5]);
195}
196
197static int do_restorecon_app_data(char **arg, char reply[REPLY_MAX] ATTRIBUTE_UNUSED) {
198 /* const char* uuid, const char* pkgName, userid_t userid, int flags,
199 appid_t appid, const char* seinfo */
200 return restorecon_app_data(parse_null(arg[0]), arg[1], atoi(arg[2]), atoi(arg[3]), atoi(arg[4]), arg[5]);
201}
202
203static int do_clear_app_data(char **arg, char reply[REPLY_MAX] ATTRIBUTE_UNUSED) {
204 /* const char *uuid, const char *pkgname, userid_t userid, int flags */
205 return clear_app_data(parse_null(arg[0]), arg[1], atoi(arg[2]), atoi(arg[3]));
206}
207
208static int do_destroy_app_data(char **arg, char reply[REPLY_MAX] ATTRIBUTE_UNUSED) {
209 /* const char *uuid, const char *pkgname, userid_t userid, int flags */
210 return destroy_app_data(parse_null(arg[0]), arg[1], atoi(arg[2]), atoi(arg[3]));
Mike Lockwood94afecf2012-10-24 10:45:23 -0700211}
212
Andreas Gampe02d0de52015-11-11 20:43:16 -0800213static int do_dexopt(char **arg, char reply[REPLY_MAX] ATTRIBUTE_UNUSED)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700214{
Todd Kennedy76e767c2015-09-25 07:47:47 -0700215 /* apk_path, uid, pkgname, instruction_set, dexopt_needed, oat_dir, dexopt_flags */
216 return dexopt(arg[0], atoi(arg[1]), arg[2], arg[3], atoi(arg[4]),
217 arg[5], atoi(arg[6]));
Mike Lockwood94afecf2012-10-24 10:45:23 -0700218}
219
Andreas Gampe02d0de52015-11-11 20:43:16 -0800220static int do_mark_boot_complete(char **arg, char reply[REPLY_MAX] ATTRIBUTE_UNUSED)
Narayan Kamath091ea772014-11-10 15:03:46 +0000221{
222 return mark_boot_complete(arg[0] /* instruction set */);
223}
224
Andreas Gampe02d0de52015-11-11 20:43:16 -0800225static int do_rm_dex(char **arg, char reply[REPLY_MAX] ATTRIBUTE_UNUSED)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700226{
Narayan Kamath1b400322014-04-11 13:17:00 +0100227 return rm_dex(arg[0], arg[1]); /* pkgname, instruction_set */
Mike Lockwood94afecf2012-10-24 10:45:23 -0700228}
229
Andreas Gampe02d0de52015-11-11 20:43:16 -0800230static int do_free_cache(char **arg, char reply[REPLY_MAX] ATTRIBUTE_UNUSED) /* TODO int:free_size */
Mike Lockwood94afecf2012-10-24 10:45:23 -0700231{
Jeff Sharkey6fe28a02015-04-09 13:10:03 -0700232 return free_cache(parse_null(arg[0]), (int64_t)atoll(arg[1])); /* uuid, free_size */
Mike Lockwood94afecf2012-10-24 10:45:23 -0700233}
234
Jeff Sharkeyc7d1b222016-01-11 13:07:09 -0700235static int do_get_app_size(char **arg, char reply[REPLY_MAX]) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700236 int64_t codesize = 0;
237 int64_t datasize = 0;
238 int64_t cachesize = 0;
239 int64_t asecsize = 0;
240 int res = 0;
241
Jeff Sharkeyc7d1b222016-01-11 13:07:09 -0700242 /* const char *uuid, const char *pkgname, userid_t userid, int flags,
243 const char *apkpath, const char *libdirpath, const char *fwdlock_apkpath,
244 const char *asecpath, const char *instruction_set */
245 res = get_app_size(parse_null(arg[0]), arg[1], atoi(arg[2]), atoi(arg[3]), arg[4], arg[5],
246 arg[6], arg[7], arg[8], &codesize, &datasize, &cachesize, &asecsize);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700247
248 /*
249 * Each int64_t can take up 22 characters printed out. Make sure it
250 * doesn't go over REPLY_MAX in the future.
251 */
252 snprintf(reply, REPLY_MAX, "%" PRId64 " %" PRId64 " %" PRId64 " %" PRId64,
253 codesize, datasize, cachesize, asecsize);
254 return res;
255}
256
Jeff Sharkeyc7d1b222016-01-11 13:07:09 -0700257static int do_move_complete_app(char **arg, char reply[REPLY_MAX] ATTRIBUTE_UNUSED) {
258 /* const char* from_uuid, const char *to_uuid, const char *package_name,
259 const char *data_app_name, appid_t appid, const char* seinfo */
260 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 -0700261}
262
Andreas Gampe02d0de52015-11-11 20:43:16 -0800263static int do_mk_user_config(char **arg, char reply[REPLY_MAX] ATTRIBUTE_UNUSED)
Robin Lee095c7632014-04-25 15:05:19 +0100264{
Robin Lee7c8bec02014-06-10 18:46:26 +0100265 return make_user_config(atoi(arg[0])); /* userid */
Robin Lee095c7632014-04-25 15:05:19 +0100266}
267
Andreas Gampe02d0de52015-11-11 20:43:16 -0800268static int do_rm_user(char **arg, char reply[REPLY_MAX] ATTRIBUTE_UNUSED)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700269{
Jeff Sharkey6fe28a02015-04-09 13:10:03 -0700270 return delete_user(parse_null(arg[0]), atoi(arg[1])); /* uuid, userid */
Mike Lockwood94afecf2012-10-24 10:45:23 -0700271}
272
Andreas Gampe02d0de52015-11-11 20:43:16 -0800273static int do_movefiles(char **arg ATTRIBUTE_UNUSED, char reply[REPLY_MAX] ATTRIBUTE_UNUSED)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700274{
275 return movefiles();
276}
277
Andreas Gampe02d0de52015-11-11 20:43:16 -0800278static int do_linklib(char **arg, char reply[REPLY_MAX] ATTRIBUTE_UNUSED)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700279{
Jeff Sharkey6fe28a02015-04-09 13:10:03 -0700280 return linklib(parse_null(arg[0]), arg[1], arg[2], atoi(arg[3]));
Mike Lockwood94afecf2012-10-24 10:45:23 -0700281}
282
Andreas Gampe02d0de52015-11-11 20:43:16 -0800283static int do_idmap(char **arg, char reply[REPLY_MAX] ATTRIBUTE_UNUSED)
MÃ¥rten Kongstad63568b12014-01-31 14:42:59 +0100284{
285 return idmap(arg[0], arg[1], atoi(arg[2]));
286}
287
Andreas Gampe02d0de52015-11-11 20:43:16 -0800288static int do_create_oat_dir(char **arg, char reply[REPLY_MAX] ATTRIBUTE_UNUSED)
Fyodor Kupolov88ce4ff2015-03-03 12:25:29 -0800289{
290 /* oat_dir, instruction_set */
291 return create_oat_dir(arg[0], arg[1]);
292}
293
Andreas Gampe02d0de52015-11-11 20:43:16 -0800294static int do_rm_package_dir(char **arg, char reply[REPLY_MAX] ATTRIBUTE_UNUSED)
Fyodor Kupolov88ce4ff2015-03-03 12:25:29 -0800295{
296 /* oat_dir */
297 return rm_package_dir(arg[0]);
Alex Light43c5d302014-07-21 12:23:48 -0700298}
299
Andreas Gampe02d0de52015-11-11 20:43:16 -0800300static int do_link_file(char **arg, char reply[REPLY_MAX] ATTRIBUTE_UNUSED)
Narayan Kamathd845c962015-06-04 13:20:27 +0100301{
302 /* relative_path, from_base, to_base */
303 return link_file(arg[0], arg[1], arg[2]);
304}
305
Mike Lockwood94afecf2012-10-24 10:45:23 -0700306struct cmdinfo {
307 const char *name;
308 unsigned numargs;
309 int (*func)(char **arg, char reply[REPLY_MAX]);
310};
311
312struct cmdinfo cmds[] = {
313 { "ping", 0, do_ping },
Jeff Sharkeyc7d1b222016-01-11 13:07:09 -0700314
315 { "create_app_data", 6, do_create_app_data },
316 { "restorecon_app_data", 6, do_restorecon_app_data },
317 { "clear_app_data", 4, do_clear_app_data },
318 { "destroy_app_data", 4, do_destroy_app_data },
319 { "move_complete_app", 6, do_move_complete_app },
320 { "get_app_size", 9, do_get_app_size },
321
Todd Kennedy76e767c2015-09-25 07:47:47 -0700322 { "dexopt", 7, do_dexopt },
Narayan Kamath091ea772014-11-10 15:03:46 +0000323 { "markbootcomplete", 1, do_mark_boot_complete },
Narayan Kamath1b400322014-04-11 13:17:00 +0100324 { "rmdex", 2, do_rm_dex },
Jeff Sharkey6fe28a02015-04-09 13:10:03 -0700325 { "freecache", 2, do_free_cache },
Mike Lockwood94afecf2012-10-24 10:45:23 -0700326 { "movefiles", 0, do_movefiles },
Jeff Sharkey6fe28a02015-04-09 13:10:03 -0700327 { "linklib", 4, do_linklib },
Robin Lee7c8bec02014-06-10 18:46:26 +0100328 { "mkuserconfig", 1, do_mk_user_config },
Jeff Sharkey6fe28a02015-04-09 13:10:03 -0700329 { "rmuser", 2, do_rm_user },
MÃ¥rten Kongstad63568b12014-01-31 14:42:59 +0100330 { "idmap", 3, do_idmap },
Fyodor Kupolov88ce4ff2015-03-03 12:25:29 -0800331 { "createoatdir", 2, do_create_oat_dir },
Narayan Kamathd845c962015-06-04 13:20:27 +0100332 { "rmpackagedir", 1, do_rm_package_dir },
Jeff Sharkeyc7d1b222016-01-11 13:07:09 -0700333 { "linkfile", 3, do_link_file },
Mike Lockwood94afecf2012-10-24 10:45:23 -0700334};
335
336static int readx(int s, void *_buf, int count)
337{
Jeff Sharkey19803802015-04-07 12:44:51 -0700338 char *buf = (char *) _buf;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700339 int n = 0, r;
340 if (count < 0) return -1;
341 while (n < count) {
342 r = read(s, buf + n, count - n);
343 if (r < 0) {
344 if (errno == EINTR) continue;
345 ALOGE("read error: %s\n", strerror(errno));
346 return -1;
347 }
348 if (r == 0) {
349 ALOGE("eof\n");
350 return -1; /* EOF */
351 }
352 n += r;
353 }
354 return 0;
355}
356
357static int writex(int s, const void *_buf, int count)
358{
Jeff Sharkey19803802015-04-07 12:44:51 -0700359 const char *buf = (const char *) _buf;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700360 int n = 0, r;
361 if (count < 0) return -1;
362 while (n < count) {
363 r = write(s, buf + n, count - n);
364 if (r < 0) {
365 if (errno == EINTR) continue;
366 ALOGE("write error: %s\n", strerror(errno));
367 return -1;
368 }
369 n += r;
370 }
371 return 0;
372}
373
374
375/* Tokenize the command buffer, locate a matching command,
376 * ensure that the required number of arguments are provided,
377 * call the function(), return the result.
378 */
379static int execute(int s, char cmd[BUFFER_MAX])
380{
381 char reply[REPLY_MAX];
382 char *arg[TOKEN_MAX+1];
383 unsigned i;
384 unsigned n = 0;
385 unsigned short count;
386 int ret = -1;
387
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700388 // ALOGI("execute('%s')\n", cmd);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700389
390 /* default reply is "" */
391 reply[0] = 0;
392
393 /* n is number of args (not counting arg[0]) */
394 arg[0] = cmd;
395 while (*cmd) {
396 if (isspace(*cmd)) {
397 *cmd++ = 0;
398 n++;
399 arg[n] = cmd;
400 if (n == TOKEN_MAX) {
401 ALOGE("too many arguments\n");
402 goto done;
403 }
404 }
Serguei Katkov62bb3852014-10-29 19:38:01 +0600405 if (*cmd) {
406 cmd++;
407 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700408 }
409
410 for (i = 0; i < sizeof(cmds) / sizeof(cmds[0]); i++) {
411 if (!strcmp(cmds[i].name,arg[0])) {
412 if (n != cmds[i].numargs) {
413 ALOGE("%s requires %d arguments (%d given)\n",
414 cmds[i].name, cmds[i].numargs, n);
415 } else {
416 ret = cmds[i].func(arg + 1, reply);
417 }
418 goto done;
419 }
420 }
421 ALOGE("unsupported command '%s'\n", arg[0]);
422
423done:
424 if (reply[0]) {
425 n = snprintf(cmd, BUFFER_MAX, "%d %s", ret, reply);
426 } else {
427 n = snprintf(cmd, BUFFER_MAX, "%d", ret);
428 }
429 if (n > BUFFER_MAX) n = BUFFER_MAX;
430 count = n;
431
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700432 // ALOGI("reply: '%s'\n", cmd);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700433 if (writex(s, &count, sizeof(count))) return -1;
434 if (writex(s, cmd, count)) return -1;
435 return 0;
436}
437
Andreas Gampe02d0de52015-11-11 20:43:16 -0800438bool initialize_globals() {
439 const char* data_path = getenv("ANDROID_DATA");
440 if (data_path == nullptr) {
441 ALOGE("Could not find ANDROID_DATA");
442 return false;
443 }
444 const char* root_path = getenv("ANDROID_ROOT");
445 if (root_path == nullptr) {
446 ALOGE("Could not find ANDROID_ROOT");
447 return false;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700448 }
449
Andreas Gampe02d0de52015-11-11 20:43:16 -0800450 return init_globals_from_data_and_root(data_path, root_path);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700451}
452
Andreas Gampe02d0de52015-11-11 20:43:16 -0800453static int initialize_directories() {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700454 int res = -1;
455
456 // Read current filesystem layout version to handle upgrade paths
457 char version_path[PATH_MAX];
458 snprintf(version_path, PATH_MAX, "%s.layout_version", android_data_dir.path);
459
460 int oldVersion;
461 if (fs_read_atomic_int(version_path, &oldVersion) == -1) {
462 oldVersion = 0;
463 }
464 int version = oldVersion;
465
Jeff Sharkeye02657d2016-01-13 09:37:46 -0700466 if (version < 2) {
467 SLOGD("Assuming that device has multi-user storage layout; upgrade no longer supported");
Mike Lockwood94afecf2012-10-24 10:45:23 -0700468 version = 2;
469 }
470
Robin Lee07053fc2014-04-29 19:42:01 +0100471 if (ensure_config_user_dirs(0) == -1) {
472 ALOGE("Failed to setup misc for user 0");
473 goto fail;
474 }
475
Robin Lee095c7632014-04-25 15:05:19 +0100476 if (version == 2) {
477 ALOGD("Upgrading to /data/misc/user directories");
478
Robin Lee60fd3fe2014-10-07 16:55:02 +0100479 char misc_dir[PATH_MAX];
480 snprintf(misc_dir, PATH_MAX, "%smisc", android_data_dir.path);
481
482 char keychain_added_dir[PATH_MAX];
483 snprintf(keychain_added_dir, PATH_MAX, "%s/keychain/cacerts-added", misc_dir);
484
485 char keychain_removed_dir[PATH_MAX];
486 snprintf(keychain_removed_dir, PATH_MAX, "%s/keychain/cacerts-removed", misc_dir);
487
Robin Lee095c7632014-04-25 15:05:19 +0100488 DIR *dir;
489 struct dirent *dirent;
Jeff Sharkeye02657d2016-01-13 09:37:46 -0700490 dir = opendir("/data/user");
Robin Lee095c7632014-04-25 15:05:19 +0100491 if (dir != NULL) {
492 while ((dirent = readdir(dir))) {
Robin Lee60fd3fe2014-10-07 16:55:02 +0100493 const char *name = dirent->d_name;
Robin Lee095c7632014-04-25 15:05:19 +0100494
Robin Lee60fd3fe2014-10-07 16:55:02 +0100495 // skip "." and ".."
496 if (name[0] == '.') {
497 if (name[1] == 0) continue;
498 if ((name[1] == '.') && (name[2] == 0)) continue;
499 }
500
501 uint32_t user_id = atoi(name);
502
503 // /data/misc/user/<user_id>
504 if (ensure_config_user_dirs(user_id) == -1) {
505 goto fail;
506 }
507
508 char misc_added_dir[PATH_MAX];
509 snprintf(misc_added_dir, PATH_MAX, "%s/user/%s/cacerts-added", misc_dir, name);
510
511 char misc_removed_dir[PATH_MAX];
512 snprintf(misc_removed_dir, PATH_MAX, "%s/user/%s/cacerts-removed", misc_dir, name);
513
514 uid_t uid = multiuser_get_uid(user_id, AID_SYSTEM);
515 gid_t gid = uid;
516 if (access(keychain_added_dir, F_OK) == 0) {
517 if (copy_dir_files(keychain_added_dir, misc_added_dir, uid, gid) != 0) {
518 ALOGE("Some files failed to copy");
Robin Lee095c7632014-04-25 15:05:19 +0100519 }
Robin Lee60fd3fe2014-10-07 16:55:02 +0100520 }
521 if (access(keychain_removed_dir, F_OK) == 0) {
522 if (copy_dir_files(keychain_removed_dir, misc_removed_dir, uid, gid) != 0) {
523 ALOGE("Some files failed to copy");
Robin Lee095c7632014-04-25 15:05:19 +0100524 }
525 }
526 }
527 closedir(dir);
Robin Lee095c7632014-04-25 15:05:19 +0100528
Robin Lee60fd3fe2014-10-07 16:55:02 +0100529 if (access(keychain_added_dir, F_OK) == 0) {
530 delete_dir_contents(keychain_added_dir, 1, 0);
Robin Lee07053fc2014-04-29 19:42:01 +0100531 }
Robin Lee60fd3fe2014-10-07 16:55:02 +0100532 if (access(keychain_removed_dir, F_OK) == 0) {
533 delete_dir_contents(keychain_removed_dir, 1, 0);
Robin Lee07053fc2014-04-29 19:42:01 +0100534 }
535 }
536
537 version = 3;
Robin Lee095c7632014-04-25 15:05:19 +0100538 }
539
Mike Lockwood94afecf2012-10-24 10:45:23 -0700540 // Persist layout version if changed
541 if (version != oldVersion) {
542 if (fs_write_atomic_int(version_path, version) == -1) {
543 ALOGE("Failed to save version to %s: %s", version_path, strerror(errno));
544 goto fail;
545 }
546 }
547
548 // Success!
549 res = 0;
550
551fail:
Mike Lockwood94afecf2012-10-24 10:45:23 -0700552 return res;
553}
554
Stephen Smalley7abb52b2014-03-26 09:30:37 -0400555static int log_callback(int type, const char *fmt, ...) {
556 va_list ap;
557 int priority;
558
559 switch (type) {
560 case SELINUX_WARNING:
561 priority = ANDROID_LOG_WARN;
562 break;
563 case SELINUX_INFO:
564 priority = ANDROID_LOG_INFO;
565 break;
566 default:
567 priority = ANDROID_LOG_ERROR;
568 break;
569 }
570 va_start(ap, fmt);
571 LOG_PRI_VA(priority, "SELinux", fmt, ap);
572 va_end(ap);
573 return 0;
574}
575
Andreas Gampe02d0de52015-11-11 20:43:16 -0800576static int installd_main(const int argc ATTRIBUTE_UNUSED, char *argv[]) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700577 char buf[BUFFER_MAX];
578 struct sockaddr addr;
579 socklen_t alen;
Chih-Hung Hsieh99d9fb12014-09-11 14:44:46 -0700580 int lsocket, s;
Stephen Smalleybd558d62013-04-16 12:16:50 -0400581 int selinux_enabled = (is_selinux_enabled() > 0);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700582
Jeff Sharkeye3637242015-04-08 20:56:42 -0700583 setenv("ANDROID_LOG_TAGS", "*:v", 1);
584 android::base::InitLogging(argv);
585
Mike Lockwood94afecf2012-10-24 10:45:23 -0700586 ALOGI("installd firing up\n");
587
Stephen Smalley7abb52b2014-03-26 09:30:37 -0400588 union selinux_callback cb;
589 cb.func_log = log_callback;
590 selinux_set_callback(SELINUX_CB_LOG, cb);
591
Andreas Gampe02d0de52015-11-11 20:43:16 -0800592 if (!initialize_globals()) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700593 ALOGE("Could not initialize globals; exiting.\n");
594 exit(1);
595 }
596
597 if (initialize_directories() < 0) {
598 ALOGE("Could not create directories; exiting.\n");
599 exit(1);
600 }
601
Stephen Smalleybd558d62013-04-16 12:16:50 -0400602 if (selinux_enabled && selinux_status_open(true) < 0) {
603 ALOGE("Could not open selinux status; exiting.\n");
604 exit(1);
605 }
606
Mike Lockwood94afecf2012-10-24 10:45:23 -0700607 lsocket = android_get_control_socket(SOCKET_PATH);
608 if (lsocket < 0) {
609 ALOGE("Failed to get socket from environment: %s\n", strerror(errno));
610 exit(1);
611 }
612 if (listen(lsocket, 5)) {
613 ALOGE("Listen on socket failed: %s\n", strerror(errno));
614 exit(1);
615 }
616 fcntl(lsocket, F_SETFD, FD_CLOEXEC);
617
618 for (;;) {
619 alen = sizeof(addr);
620 s = accept(lsocket, &addr, &alen);
621 if (s < 0) {
622 ALOGE("Accept failed: %s\n", strerror(errno));
623 continue;
624 }
625 fcntl(s, F_SETFD, FD_CLOEXEC);
626
627 ALOGI("new connection\n");
628 for (;;) {
629 unsigned short count;
630 if (readx(s, &count, sizeof(count))) {
631 ALOGE("failed to read size\n");
632 break;
633 }
634 if ((count < 1) || (count >= BUFFER_MAX)) {
635 ALOGE("invalid size %d\n", count);
636 break;
637 }
638 if (readx(s, buf, count)) {
639 ALOGE("failed to read command\n");
640 break;
641 }
642 buf[count] = 0;
Stephen Smalleybd558d62013-04-16 12:16:50 -0400643 if (selinux_enabled && selinux_status_updated() > 0) {
644 selinux_android_seapp_context_reload();
645 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700646 if (execute(s, buf)) break;
647 }
648 ALOGI("closing connection\n");
649 close(s);
650 }
651
652 return 0;
653}
Andreas Gampe02d0de52015-11-11 20:43:16 -0800654
655} // namespace installd
656} // namespace android
657
658int main(const int argc, char *argv[]) {
659 return android::installd::installd_main(argc, argv);
660}