blob: dc3418a6a375d46000194234d79bd22d4873954e [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) {
Greg Kaiser00087b72016-03-14 13:29:10 -0700131 /* demand that we are an absolute path */
132 if ((src == nullptr) || (src[0] != '/') || strstr(src,"..")) {
Andreas Gampe02d0de52015-11-11 20:43:16 -0800133 return false;
134 }
135
Greg Kaiser00087b72016-03-14 13:29:10 -0700136 size_t srclen = strlen(src);
137
Andreas Gampe02d0de52015-11-11 20:43:16 -0800138 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,
Janis Danisevskiseecb2d22016-01-12 14:45:55 +0000193 appid_t appid, const char* seinfo, int target_sdk_version */
194 return create_app_data(parse_null(arg[0]), arg[1], atoi(arg[2]), atoi(arg[3]),
195 atoi(arg[4]), arg[5], atoi(arg[6]));
Jeff Sharkeyc7d1b222016-01-11 13:07:09 -0700196}
197
198static int do_restorecon_app_data(char **arg, char reply[REPLY_MAX] ATTRIBUTE_UNUSED) {
199 /* const char* uuid, const char* pkgName, userid_t userid, int flags,
200 appid_t appid, const char* seinfo */
201 return restorecon_app_data(parse_null(arg[0]), arg[1], atoi(arg[2]), atoi(arg[3]), atoi(arg[4]), arg[5]);
202}
203
Jeff Sharkeycc6281c2016-02-06 19:46:09 -0700204static int do_migrate_app_data(char **arg, char reply[REPLY_MAX] ATTRIBUTE_UNUSED) {
205 /* const char *uuid, const char *pkgname, userid_t userid, int flags */
206 return migrate_app_data(parse_null(arg[0]), arg[1], atoi(arg[2]), atoi(arg[3]));
207}
208
Jeff Sharkeyc7d1b222016-01-11 13:07:09 -0700209static int do_clear_app_data(char **arg, char reply[REPLY_MAX] ATTRIBUTE_UNUSED) {
210 /* const char *uuid, const char *pkgname, userid_t userid, int flags */
211 return clear_app_data(parse_null(arg[0]), arg[1], atoi(arg[2]), atoi(arg[3]));
212}
213
214static int do_destroy_app_data(char **arg, char reply[REPLY_MAX] ATTRIBUTE_UNUSED) {
215 /* const char *uuid, const char *pkgname, userid_t userid, int flags */
216 return destroy_app_data(parse_null(arg[0]), arg[1], atoi(arg[2]), atoi(arg[3]));
Mike Lockwood94afecf2012-10-24 10:45:23 -0700217}
218
Andreas Gampe01ad5982016-03-09 16:27:29 -0800219// We use otapreopt_chroot to get into the chroot.
220static constexpr const char* kOtaPreopt = "/system/bin/otapreopt_chroot";
Andreas Gampe73dae112015-11-19 14:12:14 -0800221
Andreas Gampe01ad5982016-03-09 16:27:29 -0800222static int do_ota_dexopt(char **arg, char reply[REPLY_MAX] ATTRIBUTE_UNUSED) {
223 // Time to fork and run otapreopt.
224
225 // Check that the tool exists.
226 struct stat s;
227 if (stat(kOtaPreopt, &s) != 0) {
228 LOG(ERROR) << "Otapreopt chroot tool not found.";
229 return -1;
Andreas Gampe73dae112015-11-19 14:12:14 -0800230 }
Andreas Gampe01ad5982016-03-09 16:27:29 -0800231
232 pid_t pid = fork();
233 if (pid == 0) {
234 const char* argv[1 + 9 + 1];
235 argv[0] = kOtaPreopt;
236 for (size_t i = 1; i <= 9; ++i) {
237 argv[i] = arg[i - 1];
238 }
239 argv[10] = nullptr;
240
241 execv(argv[0], (char * const *)argv);
242 PLOG(ERROR) << "execv(OTAPREOPT_CHROOT) failed";
243 exit(99);
244 } else {
245 int res = wait_child(pid);
246 if (res == 0) {
247 ALOGV("DexInv: --- END OTAPREOPT (success) ---\n");
248 } else {
249 ALOGE("DexInv: --- END OTAPREOPT --- status=0x%04x, process failed\n", res);
250 }
251 return res;
252 }
Andreas Gampe73dae112015-11-19 14:12:14 -0800253}
254
255static int do_dexopt(char **arg, char reply[REPLY_MAX])
Mike Lockwood94afecf2012-10-24 10:45:23 -0700256{
Andreas Gampe73dae112015-11-19 14:12:14 -0800257 int dexopt_flags = atoi(arg[6]);
258 if ((dexopt_flags & DEXOPT_OTA) != 0) {
259 return do_ota_dexopt(arg, reply);
260 }
Andreas Gampe4d0f8252016-03-20 11:30:28 -0700261 return dexopt(arg[0], // apk_path
262 atoi(arg[1]), // uid
263 arg[2], // pkgname
264 arg[3], // instruction_set
265 atoi(arg[4]), // dexopt_needed
266 arg[5], // oat_dir
267 dexopt_flags,
268 arg[7], // compiler_filter
269 parse_null(arg[8])); // volume_uuid
270}
271
272static int do_merge_profiles(char **arg, char reply[REPLY_MAX])
273{
274 uid_t uid = static_cast<uid_t>(atoi(arg[0]));
275 const char* pkgname = arg[1];
276 if (merge_profiles(uid, pkgname)) {
277 strncpy(reply, "true", REPLY_MAX);
278 } else {
279 strncpy(reply, "false", REPLY_MAX);
280 }
281 return 0;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700282}
283
Andreas Gampe02d0de52015-11-11 20:43:16 -0800284static int do_mark_boot_complete(char **arg, char reply[REPLY_MAX] ATTRIBUTE_UNUSED)
Narayan Kamath091ea772014-11-10 15:03:46 +0000285{
286 return mark_boot_complete(arg[0] /* instruction set */);
287}
288
Andreas Gampe02d0de52015-11-11 20:43:16 -0800289static int do_rm_dex(char **arg, char reply[REPLY_MAX] ATTRIBUTE_UNUSED)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700290{
Narayan Kamath1b400322014-04-11 13:17:00 +0100291 return rm_dex(arg[0], arg[1]); /* pkgname, instruction_set */
Mike Lockwood94afecf2012-10-24 10:45:23 -0700292}
293
Andreas Gampe02d0de52015-11-11 20:43:16 -0800294static int do_free_cache(char **arg, char reply[REPLY_MAX] ATTRIBUTE_UNUSED) /* TODO int:free_size */
Mike Lockwood94afecf2012-10-24 10:45:23 -0700295{
Jeff Sharkey6fe28a02015-04-09 13:10:03 -0700296 return free_cache(parse_null(arg[0]), (int64_t)atoll(arg[1])); /* uuid, free_size */
Mike Lockwood94afecf2012-10-24 10:45:23 -0700297}
298
Jeff Sharkeyc7d1b222016-01-11 13:07:09 -0700299static int do_get_app_size(char **arg, char reply[REPLY_MAX]) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700300 int64_t codesize = 0;
301 int64_t datasize = 0;
302 int64_t cachesize = 0;
303 int64_t asecsize = 0;
304 int res = 0;
305
Jeff Sharkeyc7d1b222016-01-11 13:07:09 -0700306 /* const char *uuid, const char *pkgname, userid_t userid, int flags,
307 const char *apkpath, const char *libdirpath, const char *fwdlock_apkpath,
308 const char *asecpath, const char *instruction_set */
309 res = get_app_size(parse_null(arg[0]), arg[1], atoi(arg[2]), atoi(arg[3]), arg[4], arg[5],
310 arg[6], arg[7], arg[8], &codesize, &datasize, &cachesize, &asecsize);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700311
312 /*
313 * Each int64_t can take up 22 characters printed out. Make sure it
314 * doesn't go over REPLY_MAX in the future.
315 */
316 snprintf(reply, REPLY_MAX, "%" PRId64 " %" PRId64 " %" PRId64 " %" PRId64,
317 codesize, datasize, cachesize, asecsize);
318 return res;
319}
320
Jeff Sharkeyc7d1b222016-01-11 13:07:09 -0700321static int do_move_complete_app(char **arg, char reply[REPLY_MAX] ATTRIBUTE_UNUSED) {
322 /* const char* from_uuid, const char *to_uuid, const char *package_name,
Janis Danisevskiseecb2d22016-01-12 14:45:55 +0000323 const char *data_app_name, appid_t appid, const char* seinfo,
324 int target_sdk_version */
325 return move_complete_app(parse_null(arg[0]), parse_null(arg[1]), arg[2], arg[3],
326 atoi(arg[4]), arg[5], atoi(arg[6]));
Mike Lockwood94afecf2012-10-24 10:45:23 -0700327}
328
Andreas Gampe02d0de52015-11-11 20:43:16 -0800329static int do_mk_user_config(char **arg, char reply[REPLY_MAX] ATTRIBUTE_UNUSED)
Robin Lee095c7632014-04-25 15:05:19 +0100330{
Robin Lee7c8bec02014-06-10 18:46:26 +0100331 return make_user_config(atoi(arg[0])); /* userid */
Robin Lee095c7632014-04-25 15:05:19 +0100332}
333
Andreas Gampe02d0de52015-11-11 20:43:16 -0800334static int do_rm_user(char **arg, char reply[REPLY_MAX] ATTRIBUTE_UNUSED)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700335{
Jeff Sharkey6fe28a02015-04-09 13:10:03 -0700336 return delete_user(parse_null(arg[0]), atoi(arg[1])); /* uuid, userid */
Mike Lockwood94afecf2012-10-24 10:45:23 -0700337}
338
Andreas Gampe02d0de52015-11-11 20:43:16 -0800339static int do_linklib(char **arg, char reply[REPLY_MAX] ATTRIBUTE_UNUSED)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700340{
Jeff Sharkey6fe28a02015-04-09 13:10:03 -0700341 return linklib(parse_null(arg[0]), arg[1], arg[2], atoi(arg[3]));
Mike Lockwood94afecf2012-10-24 10:45:23 -0700342}
343
Andreas Gampe02d0de52015-11-11 20:43:16 -0800344static int do_idmap(char **arg, char reply[REPLY_MAX] ATTRIBUTE_UNUSED)
MÃ¥rten Kongstad63568b12014-01-31 14:42:59 +0100345{
346 return idmap(arg[0], arg[1], atoi(arg[2]));
347}
348
Andreas Gampe02d0de52015-11-11 20:43:16 -0800349static int do_create_oat_dir(char **arg, char reply[REPLY_MAX] ATTRIBUTE_UNUSED)
Fyodor Kupolov88ce4ff2015-03-03 12:25:29 -0800350{
351 /* oat_dir, instruction_set */
352 return create_oat_dir(arg[0], arg[1]);
353}
354
Andreas Gampe02d0de52015-11-11 20:43:16 -0800355static int do_rm_package_dir(char **arg, char reply[REPLY_MAX] ATTRIBUTE_UNUSED)
Fyodor Kupolov88ce4ff2015-03-03 12:25:29 -0800356{
357 /* oat_dir */
358 return rm_package_dir(arg[0]);
Alex Light43c5d302014-07-21 12:23:48 -0700359}
360
David Brazdild828d682016-03-08 12:50:20 +0000361static int do_rm_profiles(char **arg, char reply[REPLY_MAX] ATTRIBUTE_UNUSED)
362{
363 /* package_name */
364 return rm_profiles(arg[0]);
365}
366
Andreas Gampe02d0de52015-11-11 20:43:16 -0800367static int do_link_file(char **arg, char reply[REPLY_MAX] ATTRIBUTE_UNUSED)
Narayan Kamathd845c962015-06-04 13:20:27 +0100368{
369 /* relative_path, from_base, to_base */
370 return link_file(arg[0], arg[1], arg[2]);
371}
372
Andreas Gampee65b4fa2016-03-01 11:46:45 -0800373static int do_move_ab(char **arg, char reply[REPLY_MAX] ATTRIBUTE_UNUSED) {
374 // apk_path, instruction_set, oat_dir
375 return move_ab(arg[0], arg[1], arg[2]);
376}
377
Mike Lockwood94afecf2012-10-24 10:45:23 -0700378struct cmdinfo {
379 const char *name;
380 unsigned numargs;
381 int (*func)(char **arg, char reply[REPLY_MAX]);
382};
383
384struct cmdinfo cmds[] = {
385 { "ping", 0, do_ping },
Jeff Sharkeyc7d1b222016-01-11 13:07:09 -0700386
Janis Danisevskiseecb2d22016-01-12 14:45:55 +0000387 { "create_app_data", 7, do_create_app_data },
Jeff Sharkeyc7d1b222016-01-11 13:07:09 -0700388 { "restorecon_app_data", 6, do_restorecon_app_data },
Jeff Sharkeycc6281c2016-02-06 19:46:09 -0700389 { "migrate_app_data", 4, do_migrate_app_data },
Jeff Sharkeyc7d1b222016-01-11 13:07:09 -0700390 { "clear_app_data", 4, do_clear_app_data },
391 { "destroy_app_data", 4, do_destroy_app_data },
Janis Danisevskiseecb2d22016-01-12 14:45:55 +0000392 { "move_complete_app", 7, do_move_complete_app },
Jeff Sharkeyc7d1b222016-01-11 13:07:09 -0700393 { "get_app_size", 9, do_get_app_size },
394
Calin Juravle60a794d2015-12-24 12:36:41 +0200395 { "dexopt", 9, do_dexopt },
Narayan Kamath091ea772014-11-10 15:03:46 +0000396 { "markbootcomplete", 1, do_mark_boot_complete },
Narayan Kamath1b400322014-04-11 13:17:00 +0100397 { "rmdex", 2, do_rm_dex },
Jeff Sharkey6fe28a02015-04-09 13:10:03 -0700398 { "freecache", 2, do_free_cache },
Jeff Sharkey6fe28a02015-04-09 13:10:03 -0700399 { "linklib", 4, do_linklib },
Robin Lee7c8bec02014-06-10 18:46:26 +0100400 { "mkuserconfig", 1, do_mk_user_config },
Jeff Sharkey6fe28a02015-04-09 13:10:03 -0700401 { "rmuser", 2, do_rm_user },
MÃ¥rten Kongstad63568b12014-01-31 14:42:59 +0100402 { "idmap", 3, do_idmap },
Fyodor Kupolov88ce4ff2015-03-03 12:25:29 -0800403 { "createoatdir", 2, do_create_oat_dir },
Narayan Kamathd845c962015-06-04 13:20:27 +0100404 { "rmpackagedir", 1, do_rm_package_dir },
David Brazdild828d682016-03-08 12:50:20 +0000405 { "rmprofiles", 1, do_rm_profiles },
Jeff Sharkeyc7d1b222016-01-11 13:07:09 -0700406 { "linkfile", 3, do_link_file },
Andreas Gampee65b4fa2016-03-01 11:46:45 -0800407 { "move_ab", 3, do_move_ab },
Andreas Gampe4d0f8252016-03-20 11:30:28 -0700408 { "merge_profiles", 2, do_merge_profiles },
Mike Lockwood94afecf2012-10-24 10:45:23 -0700409};
410
411static int readx(int s, void *_buf, int count)
412{
Jeff Sharkey19803802015-04-07 12:44:51 -0700413 char *buf = (char *) _buf;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700414 int n = 0, r;
415 if (count < 0) return -1;
416 while (n < count) {
417 r = read(s, buf + n, count - n);
418 if (r < 0) {
419 if (errno == EINTR) continue;
420 ALOGE("read error: %s\n", strerror(errno));
421 return -1;
422 }
423 if (r == 0) {
424 ALOGE("eof\n");
425 return -1; /* EOF */
426 }
427 n += r;
428 }
429 return 0;
430}
431
432static int writex(int s, const void *_buf, int count)
433{
Jeff Sharkey19803802015-04-07 12:44:51 -0700434 const char *buf = (const char *) _buf;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700435 int n = 0, r;
436 if (count < 0) return -1;
437 while (n < count) {
438 r = write(s, buf + n, count - n);
439 if (r < 0) {
440 if (errno == EINTR) continue;
441 ALOGE("write error: %s\n", strerror(errno));
442 return -1;
443 }
444 n += r;
445 }
446 return 0;
447}
448
449
450/* Tokenize the command buffer, locate a matching command,
451 * ensure that the required number of arguments are provided,
452 * call the function(), return the result.
453 */
454static int execute(int s, char cmd[BUFFER_MAX])
455{
456 char reply[REPLY_MAX];
457 char *arg[TOKEN_MAX+1];
458 unsigned i;
459 unsigned n = 0;
460 unsigned short count;
461 int ret = -1;
462
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700463 // ALOGI("execute('%s')\n", cmd);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700464
465 /* default reply is "" */
466 reply[0] = 0;
467
468 /* n is number of args (not counting arg[0]) */
469 arg[0] = cmd;
470 while (*cmd) {
471 if (isspace(*cmd)) {
472 *cmd++ = 0;
473 n++;
474 arg[n] = cmd;
475 if (n == TOKEN_MAX) {
476 ALOGE("too many arguments\n");
477 goto done;
478 }
479 }
Serguei Katkov62bb3852014-10-29 19:38:01 +0600480 if (*cmd) {
481 cmd++;
482 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700483 }
484
485 for (i = 0; i < sizeof(cmds) / sizeof(cmds[0]); i++) {
486 if (!strcmp(cmds[i].name,arg[0])) {
487 if (n != cmds[i].numargs) {
488 ALOGE("%s requires %d arguments (%d given)\n",
489 cmds[i].name, cmds[i].numargs, n);
490 } else {
491 ret = cmds[i].func(arg + 1, reply);
492 }
493 goto done;
494 }
495 }
496 ALOGE("unsupported command '%s'\n", arg[0]);
497
498done:
499 if (reply[0]) {
500 n = snprintf(cmd, BUFFER_MAX, "%d %s", ret, reply);
501 } else {
502 n = snprintf(cmd, BUFFER_MAX, "%d", ret);
503 }
504 if (n > BUFFER_MAX) n = BUFFER_MAX;
505 count = n;
506
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700507 // ALOGI("reply: '%s'\n", cmd);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700508 if (writex(s, &count, sizeof(count))) return -1;
509 if (writex(s, cmd, count)) return -1;
510 return 0;
511}
512
Andreas Gampe02d0de52015-11-11 20:43:16 -0800513bool initialize_globals() {
514 const char* data_path = getenv("ANDROID_DATA");
515 if (data_path == nullptr) {
516 ALOGE("Could not find ANDROID_DATA");
517 return false;
518 }
519 const char* root_path = getenv("ANDROID_ROOT");
520 if (root_path == nullptr) {
521 ALOGE("Could not find ANDROID_ROOT");
522 return false;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700523 }
524
Andreas Gampe02d0de52015-11-11 20:43:16 -0800525 return init_globals_from_data_and_root(data_path, root_path);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700526}
527
Andreas Gampe02d0de52015-11-11 20:43:16 -0800528static int initialize_directories() {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700529 int res = -1;
530
531 // Read current filesystem layout version to handle upgrade paths
532 char version_path[PATH_MAX];
533 snprintf(version_path, PATH_MAX, "%s.layout_version", android_data_dir.path);
534
535 int oldVersion;
536 if (fs_read_atomic_int(version_path, &oldVersion) == -1) {
537 oldVersion = 0;
538 }
539 int version = oldVersion;
540
Jeff Sharkeye02657d2016-01-13 09:37:46 -0700541 if (version < 2) {
542 SLOGD("Assuming that device has multi-user storage layout; upgrade no longer supported");
Mike Lockwood94afecf2012-10-24 10:45:23 -0700543 version = 2;
544 }
545
Robin Lee07053fc2014-04-29 19:42:01 +0100546 if (ensure_config_user_dirs(0) == -1) {
547 ALOGE("Failed to setup misc for user 0");
548 goto fail;
549 }
550
Robin Lee095c7632014-04-25 15:05:19 +0100551 if (version == 2) {
552 ALOGD("Upgrading to /data/misc/user directories");
553
Robin Lee60fd3fe2014-10-07 16:55:02 +0100554 char misc_dir[PATH_MAX];
555 snprintf(misc_dir, PATH_MAX, "%smisc", android_data_dir.path);
556
557 char keychain_added_dir[PATH_MAX];
558 snprintf(keychain_added_dir, PATH_MAX, "%s/keychain/cacerts-added", misc_dir);
559
560 char keychain_removed_dir[PATH_MAX];
561 snprintf(keychain_removed_dir, PATH_MAX, "%s/keychain/cacerts-removed", misc_dir);
562
Robin Lee095c7632014-04-25 15:05:19 +0100563 DIR *dir;
564 struct dirent *dirent;
Jeff Sharkeye02657d2016-01-13 09:37:46 -0700565 dir = opendir("/data/user");
Robin Lee095c7632014-04-25 15:05:19 +0100566 if (dir != NULL) {
567 while ((dirent = readdir(dir))) {
Robin Lee60fd3fe2014-10-07 16:55:02 +0100568 const char *name = dirent->d_name;
Robin Lee095c7632014-04-25 15:05:19 +0100569
Robin Lee60fd3fe2014-10-07 16:55:02 +0100570 // skip "." and ".."
571 if (name[0] == '.') {
572 if (name[1] == 0) continue;
573 if ((name[1] == '.') && (name[2] == 0)) continue;
574 }
575
576 uint32_t user_id = atoi(name);
577
578 // /data/misc/user/<user_id>
579 if (ensure_config_user_dirs(user_id) == -1) {
580 goto fail;
581 }
582
583 char misc_added_dir[PATH_MAX];
584 snprintf(misc_added_dir, PATH_MAX, "%s/user/%s/cacerts-added", misc_dir, name);
585
586 char misc_removed_dir[PATH_MAX];
587 snprintf(misc_removed_dir, PATH_MAX, "%s/user/%s/cacerts-removed", misc_dir, name);
588
589 uid_t uid = multiuser_get_uid(user_id, AID_SYSTEM);
590 gid_t gid = uid;
591 if (access(keychain_added_dir, F_OK) == 0) {
592 if (copy_dir_files(keychain_added_dir, misc_added_dir, uid, gid) != 0) {
593 ALOGE("Some files failed to copy");
Robin Lee095c7632014-04-25 15:05:19 +0100594 }
Robin Lee60fd3fe2014-10-07 16:55:02 +0100595 }
596 if (access(keychain_removed_dir, F_OK) == 0) {
597 if (copy_dir_files(keychain_removed_dir, misc_removed_dir, uid, gid) != 0) {
598 ALOGE("Some files failed to copy");
Robin Lee095c7632014-04-25 15:05:19 +0100599 }
600 }
601 }
602 closedir(dir);
Robin Lee095c7632014-04-25 15:05:19 +0100603
Robin Lee60fd3fe2014-10-07 16:55:02 +0100604 if (access(keychain_added_dir, F_OK) == 0) {
605 delete_dir_contents(keychain_added_dir, 1, 0);
Robin Lee07053fc2014-04-29 19:42:01 +0100606 }
Robin Lee60fd3fe2014-10-07 16:55:02 +0100607 if (access(keychain_removed_dir, F_OK) == 0) {
608 delete_dir_contents(keychain_removed_dir, 1, 0);
Robin Lee07053fc2014-04-29 19:42:01 +0100609 }
610 }
611
612 version = 3;
Robin Lee095c7632014-04-25 15:05:19 +0100613 }
614
Mike Lockwood94afecf2012-10-24 10:45:23 -0700615 // Persist layout version if changed
616 if (version != oldVersion) {
617 if (fs_write_atomic_int(version_path, version) == -1) {
618 ALOGE("Failed to save version to %s: %s", version_path, strerror(errno));
619 goto fail;
620 }
621 }
622
623 // Success!
624 res = 0;
625
626fail:
Mike Lockwood94afecf2012-10-24 10:45:23 -0700627 return res;
628}
629
Stephen Smalley7abb52b2014-03-26 09:30:37 -0400630static int log_callback(int type, const char *fmt, ...) {
631 va_list ap;
632 int priority;
633
634 switch (type) {
635 case SELINUX_WARNING:
636 priority = ANDROID_LOG_WARN;
637 break;
638 case SELINUX_INFO:
639 priority = ANDROID_LOG_INFO;
640 break;
641 default:
642 priority = ANDROID_LOG_ERROR;
643 break;
644 }
645 va_start(ap, fmt);
646 LOG_PRI_VA(priority, "SELinux", fmt, ap);
647 va_end(ap);
648 return 0;
649}
650
Andreas Gampe02d0de52015-11-11 20:43:16 -0800651static int installd_main(const int argc ATTRIBUTE_UNUSED, char *argv[]) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700652 char buf[BUFFER_MAX];
653 struct sockaddr addr;
654 socklen_t alen;
Chih-Hung Hsieh99d9fb12014-09-11 14:44:46 -0700655 int lsocket, s;
Stephen Smalleybd558d62013-04-16 12:16:50 -0400656 int selinux_enabled = (is_selinux_enabled() > 0);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700657
Jeff Sharkeye3637242015-04-08 20:56:42 -0700658 setenv("ANDROID_LOG_TAGS", "*:v", 1);
659 android::base::InitLogging(argv);
660
Mike Lockwood94afecf2012-10-24 10:45:23 -0700661 ALOGI("installd firing up\n");
662
Stephen Smalley7abb52b2014-03-26 09:30:37 -0400663 union selinux_callback cb;
664 cb.func_log = log_callback;
665 selinux_set_callback(SELINUX_CB_LOG, cb);
666
Andreas Gampe02d0de52015-11-11 20:43:16 -0800667 if (!initialize_globals()) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700668 ALOGE("Could not initialize globals; exiting.\n");
669 exit(1);
670 }
671
672 if (initialize_directories() < 0) {
673 ALOGE("Could not create directories; exiting.\n");
674 exit(1);
675 }
676
Stephen Smalleybd558d62013-04-16 12:16:50 -0400677 if (selinux_enabled && selinux_status_open(true) < 0) {
678 ALOGE("Could not open selinux status; exiting.\n");
679 exit(1);
680 }
681
Mike Lockwood94afecf2012-10-24 10:45:23 -0700682 lsocket = android_get_control_socket(SOCKET_PATH);
683 if (lsocket < 0) {
684 ALOGE("Failed to get socket from environment: %s\n", strerror(errno));
685 exit(1);
686 }
687 if (listen(lsocket, 5)) {
688 ALOGE("Listen on socket failed: %s\n", strerror(errno));
689 exit(1);
690 }
691 fcntl(lsocket, F_SETFD, FD_CLOEXEC);
692
693 for (;;) {
694 alen = sizeof(addr);
695 s = accept(lsocket, &addr, &alen);
696 if (s < 0) {
697 ALOGE("Accept failed: %s\n", strerror(errno));
698 continue;
699 }
700 fcntl(s, F_SETFD, FD_CLOEXEC);
701
702 ALOGI("new connection\n");
703 for (;;) {
704 unsigned short count;
705 if (readx(s, &count, sizeof(count))) {
706 ALOGE("failed to read size\n");
707 break;
708 }
709 if ((count < 1) || (count >= BUFFER_MAX)) {
710 ALOGE("invalid size %d\n", count);
711 break;
712 }
713 if (readx(s, buf, count)) {
714 ALOGE("failed to read command\n");
715 break;
716 }
717 buf[count] = 0;
Stephen Smalleybd558d62013-04-16 12:16:50 -0400718 if (selinux_enabled && selinux_status_updated() > 0) {
719 selinux_android_seapp_context_reload();
720 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700721 if (execute(s, buf)) break;
722 }
723 ALOGI("closing connection\n");
724 close(s);
725 }
726
727 return 0;
728}
Andreas Gampe02d0de52015-11-11 20:43:16 -0800729
730} // namespace installd
731} // namespace android
732
733int main(const int argc, char *argv[]) {
734 return android::installd::installd_main(argc, argv);
735}