installd.cpp revision 76e767ca14bcbb4bc809cd1279ece82a3aabe8a4
194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood/* 294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood** Copyright 2008, The Android Open Source Project 394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood** 419803807cd7ae01868fcfa50305f4a7dd13765e2Jeff Sharkey** Licensed under the Apache License, Version 2.0 (the "License"); 519803807cd7ae01868fcfa50305f4a7dd13765e2Jeff Sharkey** you may not use this file except in compliance with the License. 619803807cd7ae01868fcfa50305f4a7dd13765e2Jeff Sharkey** You may obtain a copy of the License at 794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood** 819803807cd7ae01868fcfa50305f4a7dd13765e2Jeff Sharkey** http://www.apache.org/licenses/LICENSE-2.0 994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood** 1019803807cd7ae01868fcfa50305f4a7dd13765e2Jeff Sharkey** Unless required by applicable law or agreed to in writing, software 1119803807cd7ae01868fcfa50305f4a7dd13765e2Jeff Sharkey** distributed under the License is distributed on an "AS IS" BASIS, 1219803807cd7ae01868fcfa50305f4a7dd13765e2Jeff Sharkey** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1319803807cd7ae01868fcfa50305f4a7dd13765e2Jeff Sharkey** See the License for the specific language governing permissions and 1494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood** limitations under the License. 1594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood*/ 1694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 17e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey#include "installd.h" 18e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey 19e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey#include <base/logging.h> 20e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey 21d747129e1e8876f5a50f47156ec50b6969a638e4Nick Kralevich#include <sys/capability.h> 22119b765a053f650b4b47256245ce836f8c403d7fElliott Hughes#include <sys/prctl.h> 23bd558d61871218f5b13df2fe4b7cc3b530ee947cStephen Smalley#include <selinux/android.h> 24bd558d61871218f5b13df2fe4b7cc3b530ee947cStephen Smalley#include <selinux/avc.h> 2594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 2694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood#define BUFFER_MAX 1024 /* input buffer for commands */ 2788ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov#define TOKEN_MAX 16 /* max number of arguments in buffer */ 2894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood#define REPLY_MAX 256 /* largest reply allowed */ 2994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 306fe28a06012250da85f808a0869f87e06e0bcce9Jeff Sharkeystatic char* parse_null(char* arg) { 316fe28a06012250da85f808a0869f87e06e0bcce9Jeff Sharkey if (strcmp(arg, "!") == 0) { 326fe28a06012250da85f808a0869f87e06e0bcce9Jeff Sharkey return nullptr; 336fe28a06012250da85f808a0869f87e06e0bcce9Jeff Sharkey } else { 346fe28a06012250da85f808a0869f87e06e0bcce9Jeff Sharkey return arg; 356fe28a06012250da85f808a0869f87e06e0bcce9Jeff Sharkey } 366fe28a06012250da85f808a0869f87e06e0bcce9Jeff Sharkey} 376fe28a06012250da85f808a0869f87e06e0bcce9Jeff Sharkey 3899d9fb15b4a1eb534261ae81b2cf25aec4447bd0Chih-Hung Hsiehstatic int do_ping(char **arg __unused, char reply[REPLY_MAX] __unused) 3994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{ 4094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return 0; 4194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 4294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 4399d9fb15b4a1eb534261ae81b2cf25aec4447bd0Chih-Hung Hsiehstatic int do_install(char **arg, char reply[REPLY_MAX] __unused) 4494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{ 456fe28a06012250da85f808a0869f87e06e0bcce9Jeff Sharkey return install(parse_null(arg[0]), arg[1], atoi(arg[2]), atoi(arg[3]), arg[4]); /* uuid, pkgname, uid, gid, seinfo */ 4694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 4794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 4899d9fb15b4a1eb534261ae81b2cf25aec4447bd0Chih-Hung Hsiehstatic int do_dexopt(char **arg, char reply[REPLY_MAX] __unused) 4994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{ 5076e767ca14bcbb4bc809cd1279ece82a3aabe8a4Todd Kennedy /* apk_path, uid, pkgname, instruction_set, dexopt_needed, oat_dir, dexopt_flags */ 5176e767ca14bcbb4bc809cd1279ece82a3aabe8a4Todd Kennedy return dexopt(arg[0], atoi(arg[1]), arg[2], arg[3], atoi(arg[4]), 5276e767ca14bcbb4bc809cd1279ece82a3aabe8a4Todd Kennedy arg[5], atoi(arg[6])); 5394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 5494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 557fb390d10c896c861ba6205f2232669586ac5085Bernhard Rosenkränzerstatic int do_mark_boot_complete(char **arg, char reply[REPLY_MAX] __unused) 56091ea779d4e575856c04d51d82f45cc8a6155b5eNarayan Kamath{ 57091ea779d4e575856c04d51d82f45cc8a6155b5eNarayan Kamath return mark_boot_complete(arg[0] /* instruction set */); 58091ea779d4e575856c04d51d82f45cc8a6155b5eNarayan Kamath} 59091ea779d4e575856c04d51d82f45cc8a6155b5eNarayan Kamath 607fb390d10c896c861ba6205f2232669586ac5085Bernhard Rosenkränzerstatic int do_move_dex(char **arg, char reply[REPLY_MAX] __unused) 6194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{ 621b4003207750ea8fe8c7b03eb32d80f1df83979eNarayan Kamath return move_dex(arg[0], arg[1], arg[2]); /* src, dst, instruction_set */ 6394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 6494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 6599d9fb15b4a1eb534261ae81b2cf25aec4447bd0Chih-Hung Hsiehstatic int do_rm_dex(char **arg, char reply[REPLY_MAX] __unused) 6694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{ 671b4003207750ea8fe8c7b03eb32d80f1df83979eNarayan Kamath return rm_dex(arg[0], arg[1]); /* pkgname, instruction_set */ 6894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 6994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 7099d9fb15b4a1eb534261ae81b2cf25aec4447bd0Chih-Hung Hsiehstatic int do_remove(char **arg, char reply[REPLY_MAX] __unused) 7194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{ 726fe28a06012250da85f808a0869f87e06e0bcce9Jeff Sharkey return uninstall(parse_null(arg[0]), arg[1], atoi(arg[2])); /* uuid, pkgname, userid */ 7394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 7494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 7599d9fb15b4a1eb534261ae81b2cf25aec4447bd0Chih-Hung Hsiehstatic int do_rename(char **arg, char reply[REPLY_MAX] __unused) 7694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{ 7794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return renamepkg(arg[0], arg[1]); /* oldpkgname, newpkgname */ 7894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 7994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 8099d9fb15b4a1eb534261ae81b2cf25aec4447bd0Chih-Hung Hsiehstatic int do_fixuid(char **arg, char reply[REPLY_MAX] __unused) 8194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{ 826fe28a06012250da85f808a0869f87e06e0bcce9Jeff Sharkey return fix_uid(parse_null(arg[0]), arg[1], atoi(arg[2]), atoi(arg[3])); /* uuid, pkgname, uid, gid */ 8394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 8494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 8599d9fb15b4a1eb534261ae81b2cf25aec4447bd0Chih-Hung Hsiehstatic int do_free_cache(char **arg, char reply[REPLY_MAX] __unused) /* TODO int:free_size */ 8694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{ 876fe28a06012250da85f808a0869f87e06e0bcce9Jeff Sharkey return free_cache(parse_null(arg[0]), (int64_t)atoll(arg[1])); /* uuid, free_size */ 8894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 8994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 9099d9fb15b4a1eb534261ae81b2cf25aec4447bd0Chih-Hung Hsiehstatic int do_rm_cache(char **arg, char reply[REPLY_MAX] __unused) 9194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{ 926fe28a06012250da85f808a0869f87e06e0bcce9Jeff Sharkey return delete_cache(parse_null(arg[0]), arg[1], atoi(arg[2])); /* uuid, pkgname, userid */ 9394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 9494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 959e8b528926506b3fa9ca7df52fef5f56ec6149c7Dan Albertstatic int do_rm_code_cache(char **arg, char reply[REPLY_MAX] __unused) 96c796b681e52fbb792da9a5b4f30e935cc927c1d7Jeff Sharkey{ 976fe28a06012250da85f808a0869f87e06e0bcce9Jeff Sharkey return delete_code_cache(parse_null(arg[0]), arg[1], atoi(arg[2])); /* uuid, pkgname, userid */ 98c796b681e52fbb792da9a5b4f30e935cc927c1d7Jeff Sharkey} 99c796b681e52fbb792da9a5b4f30e935cc927c1d7Jeff Sharkey 10094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodstatic int do_get_size(char **arg, char reply[REPLY_MAX]) 10194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{ 10294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood int64_t codesize = 0; 10394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood int64_t datasize = 0; 10494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood int64_t cachesize = 0; 10594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood int64_t asecsize = 0; 10694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood int res = 0; 10794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 1086fe28a06012250da85f808a0869f87e06e0bcce9Jeff Sharkey /* uuid, pkgdir, userid, apkpath */ 1096fe28a06012250da85f808a0869f87e06e0bcce9Jeff Sharkey res = get_size(parse_null(arg[0]), arg[1], atoi(arg[2]), arg[3], arg[4], arg[5], arg[6], 1106fe28a06012250da85f808a0869f87e06e0bcce9Jeff Sharkey arg[7], &codesize, &datasize, &cachesize, &asecsize); 11194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 11294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood /* 11394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * Each int64_t can take up 22 characters printed out. Make sure it 11494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * doesn't go over REPLY_MAX in the future. 11594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood */ 11694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood snprintf(reply, REPLY_MAX, "%" PRId64 " %" PRId64 " %" PRId64 " %" PRId64, 11794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood codesize, datasize, cachesize, asecsize); 11894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return res; 11994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 12094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 12199d9fb15b4a1eb534261ae81b2cf25aec4447bd0Chih-Hung Hsiehstatic int do_rm_user_data(char **arg, char reply[REPLY_MAX] __unused) 12294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{ 1236fe28a06012250da85f808a0869f87e06e0bcce9Jeff Sharkey return delete_user_data(parse_null(arg[0]), arg[1], atoi(arg[2])); /* uuid, pkgname, userid */ 12494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 12594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 126e36372423000a906bafae68844ebc6c42d09335aJeff Sharkeystatic int do_mv_user_data(char **arg, char reply[REPLY_MAX] __unused) 127e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey{ 128e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey // from_uuid, to_uuid, pkgname, appid, seinfo 129e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey return move_user_data(parse_null(arg[0]), parse_null(arg[1]), arg[2], atoi(arg[3]), arg[4]); 130e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey} 131e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey 13299d9fb15b4a1eb534261ae81b2cf25aec4447bd0Chih-Hung Hsiehstatic int do_mk_user_data(char **arg, char reply[REPLY_MAX] __unused) 13394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{ 1346fe28a06012250da85f808a0869f87e06e0bcce9Jeff Sharkey return make_user_data(parse_null(arg[0]), arg[1], atoi(arg[2]), atoi(arg[3]), arg[4]); 1356fe28a06012250da85f808a0869f87e06e0bcce9Jeff Sharkey /* uuid, pkgname, uid, userid, seinfo */ 13694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 13794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 13899d9fb15b4a1eb534261ae81b2cf25aec4447bd0Chih-Hung Hsiehstatic int do_mk_user_config(char **arg, char reply[REPLY_MAX] __unused) 139095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee{ 1407c8bec01790087748ec7afa69a31789828b751f9Robin Lee return make_user_config(atoi(arg[0])); /* userid */ 141095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee} 142095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee 14399d9fb15b4a1eb534261ae81b2cf25aec4447bd0Chih-Hung Hsiehstatic int do_rm_user(char **arg, char reply[REPLY_MAX] __unused) 14494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{ 1456fe28a06012250da85f808a0869f87e06e0bcce9Jeff Sharkey return delete_user(parse_null(arg[0]), atoi(arg[1])); /* uuid, userid */ 14694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 14794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 14899d9fb15b4a1eb534261ae81b2cf25aec4447bd0Chih-Hung Hsiehstatic int do_movefiles(char **arg __unused, char reply[REPLY_MAX] __unused) 14994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{ 15094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return movefiles(); 15194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 15294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 15399d9fb15b4a1eb534261ae81b2cf25aec4447bd0Chih-Hung Hsiehstatic int do_linklib(char **arg, char reply[REPLY_MAX] __unused) 15494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{ 1556fe28a06012250da85f808a0869f87e06e0bcce9Jeff Sharkey return linklib(parse_null(arg[0]), arg[1], arg[2], atoi(arg[3])); 15694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 15794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 15899d9fb15b4a1eb534261ae81b2cf25aec4447bd0Chih-Hung Hsiehstatic int do_idmap(char **arg, char reply[REPLY_MAX] __unused) 15963568b1430d741f40ca008391c854ef1cc880138Mårten Kongstad{ 16063568b1430d741f40ca008391c854ef1cc880138Mårten Kongstad return idmap(arg[0], arg[1], atoi(arg[2])); 16163568b1430d741f40ca008391c854ef1cc880138Mårten Kongstad} 16263568b1430d741f40ca008391c854ef1cc880138Mårten Kongstad 163da30dc7336f03ca629fe173db1425fdce989119cRobert Craigstatic int do_restorecon_data(char **arg, char reply[REPLY_MAX] __attribute__((unused))) 164e9887e46cea4a095e4219927eadbe4c57bb1a5eeRobert Craig{ 1656fe28a06012250da85f808a0869f87e06e0bcce9Jeff Sharkey return restorecon_data(parse_null(arg[0]), arg[1], arg[2], atoi(arg[3])); 1666fe28a06012250da85f808a0869f87e06e0bcce9Jeff Sharkey /* uuid, pkgName, seinfo, uid*/ 167e9887e46cea4a095e4219927eadbe4c57bb1a5eeRobert Craig} 168e9887e46cea4a095e4219927eadbe4c57bb1a5eeRobert Craig 16988ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolovstatic int do_create_oat_dir(char **arg, char reply[REPLY_MAX] __unused) 17088ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov{ 17188ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov /* oat_dir, instruction_set */ 17288ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov return create_oat_dir(arg[0], arg[1]); 17388ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov} 17488ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov 17588ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolovstatic int do_rm_package_dir(char **arg, char reply[REPLY_MAX] __unused) 17688ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov{ 17788ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov /* oat_dir */ 17888ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov return rm_package_dir(arg[0]); 17943c5d30795faf08ab639b8d88c2eceaf2b648c93Alex Light} 18043c5d30795faf08ab639b8d88c2eceaf2b648c93Alex Light 18194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodstruct cmdinfo { 18294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood const char *name; 18394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood unsigned numargs; 18494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood int (*func)(char **arg, char reply[REPLY_MAX]); 18594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}; 18694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 18794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodstruct cmdinfo cmds[] = { 18894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood { "ping", 0, do_ping }, 1896fe28a06012250da85f808a0869f87e06e0bcce9Jeff Sharkey { "install", 5, do_install }, 19076e767ca14bcbb4bc809cd1279ece82a3aabe8a4Todd Kennedy { "dexopt", 7, do_dexopt }, 191091ea779d4e575856c04d51d82f45cc8a6155b5eNarayan Kamath { "markbootcomplete", 1, do_mark_boot_complete }, 1921b4003207750ea8fe8c7b03eb32d80f1df83979eNarayan Kamath { "movedex", 3, do_move_dex }, 1931b4003207750ea8fe8c7b03eb32d80f1df83979eNarayan Kamath { "rmdex", 2, do_rm_dex }, 1946fe28a06012250da85f808a0869f87e06e0bcce9Jeff Sharkey { "remove", 3, do_remove }, 19594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood { "rename", 2, do_rename }, 1966fe28a06012250da85f808a0869f87e06e0bcce9Jeff Sharkey { "fixuid", 4, do_fixuid }, 1976fe28a06012250da85f808a0869f87e06e0bcce9Jeff Sharkey { "freecache", 2, do_free_cache }, 1986fe28a06012250da85f808a0869f87e06e0bcce9Jeff Sharkey { "rmcache", 3, do_rm_cache }, 1996fe28a06012250da85f808a0869f87e06e0bcce9Jeff Sharkey { "rmcodecache", 3, do_rm_code_cache }, 2006fe28a06012250da85f808a0869f87e06e0bcce9Jeff Sharkey { "getsize", 8, do_get_size }, 2016fe28a06012250da85f808a0869f87e06e0bcce9Jeff Sharkey { "rmuserdata", 3, do_rm_user_data }, 202e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey { "mvuserdata", 5, do_mv_user_data }, 20394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood { "movefiles", 0, do_movefiles }, 2046fe28a06012250da85f808a0869f87e06e0bcce9Jeff Sharkey { "linklib", 4, do_linklib }, 2056fe28a06012250da85f808a0869f87e06e0bcce9Jeff Sharkey { "mkuserdata", 5, do_mk_user_data }, 2067c8bec01790087748ec7afa69a31789828b751f9Robin Lee { "mkuserconfig", 1, do_mk_user_config }, 2076fe28a06012250da85f808a0869f87e06e0bcce9Jeff Sharkey { "rmuser", 2, do_rm_user }, 20863568b1430d741f40ca008391c854ef1cc880138Mårten Kongstad { "idmap", 3, do_idmap }, 2096fe28a06012250da85f808a0869f87e06e0bcce9Jeff Sharkey { "restorecondata", 4, do_restorecon_data }, 21088ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov { "createoatdir", 2, do_create_oat_dir }, 21188ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov { "rmpackagedir", 1, do_rm_package_dir}, 21294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}; 21394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 21494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodstatic int readx(int s, void *_buf, int count) 21594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{ 21619803807cd7ae01868fcfa50305f4a7dd13765e2Jeff Sharkey char *buf = (char *) _buf; 21794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood int n = 0, r; 21894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (count < 0) return -1; 21994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood while (n < count) { 22094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood r = read(s, buf + n, count - n); 22194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (r < 0) { 22294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (errno == EINTR) continue; 22394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGE("read error: %s\n", strerror(errno)); 22494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return -1; 22594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 22694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (r == 0) { 22794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGE("eof\n"); 22894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return -1; /* EOF */ 22994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 23094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood n += r; 23194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 23294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return 0; 23394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 23494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 23594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodstatic int writex(int s, const void *_buf, int count) 23694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{ 23719803807cd7ae01868fcfa50305f4a7dd13765e2Jeff Sharkey const char *buf = (const char *) _buf; 23894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood int n = 0, r; 23994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (count < 0) return -1; 24094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood while (n < count) { 24194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood r = write(s, buf + n, count - n); 24294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (r < 0) { 24394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (errno == EINTR) continue; 24494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGE("write error: %s\n", strerror(errno)); 24594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return -1; 24694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 24794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood n += r; 24894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 24994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return 0; 25094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 25194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 25294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 25394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood/* Tokenize the command buffer, locate a matching command, 25494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * ensure that the required number of arguments are provided, 25594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * call the function(), return the result. 25694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood */ 25794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodstatic int execute(int s, char cmd[BUFFER_MAX]) 25894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{ 25994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood char reply[REPLY_MAX]; 26094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood char *arg[TOKEN_MAX+1]; 26194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood unsigned i; 26294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood unsigned n = 0; 26394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood unsigned short count; 26494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood int ret = -1; 26594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 2661705fc44fb85c4232637f9f7189c3bdca98a63d5Brian Carlstrom // ALOGI("execute('%s')\n", cmd); 26794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 26894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood /* default reply is "" */ 26994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood reply[0] = 0; 27094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 27194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood /* n is number of args (not counting arg[0]) */ 27294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood arg[0] = cmd; 27394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood while (*cmd) { 27494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (isspace(*cmd)) { 27594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood *cmd++ = 0; 27694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood n++; 27794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood arg[n] = cmd; 27894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (n == TOKEN_MAX) { 27994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGE("too many arguments\n"); 28094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood goto done; 28194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 28294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 28362bb385728121b456782d205f6e27d0c18be9c0bSerguei Katkov if (*cmd) { 28462bb385728121b456782d205f6e27d0c18be9c0bSerguei Katkov cmd++; 28562bb385728121b456782d205f6e27d0c18be9c0bSerguei Katkov } 28694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 28794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 28894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood for (i = 0; i < sizeof(cmds) / sizeof(cmds[0]); i++) { 28994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (!strcmp(cmds[i].name,arg[0])) { 29094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (n != cmds[i].numargs) { 29194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGE("%s requires %d arguments (%d given)\n", 29294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood cmds[i].name, cmds[i].numargs, n); 29394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } else { 29494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ret = cmds[i].func(arg + 1, reply); 29594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 29694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood goto done; 29794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 29894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 29994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGE("unsupported command '%s'\n", arg[0]); 30094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 30194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwooddone: 30294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (reply[0]) { 30394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood n = snprintf(cmd, BUFFER_MAX, "%d %s", ret, reply); 30494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } else { 30594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood n = snprintf(cmd, BUFFER_MAX, "%d", ret); 30694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 30794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (n > BUFFER_MAX) n = BUFFER_MAX; 30894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood count = n; 30994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 3101705fc44fb85c4232637f9f7189c3bdca98a63d5Brian Carlstrom // ALOGI("reply: '%s'\n", cmd); 31194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (writex(s, &count, sizeof(count))) return -1; 31294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (writex(s, cmd, count)) return -1; 31394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return 0; 31494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 31594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 31694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood/** 31794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * Initialize all the global variables that are used elsewhere. Returns 0 upon 31894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * success and -1 on error. 31994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood */ 32094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodvoid free_globals() { 32194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood size_t i; 32294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 32394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood for (i = 0; i < android_system_dirs.count; i++) { 32494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (android_system_dirs.dirs[i].path != NULL) { 32594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood free(android_system_dirs.dirs[i].path); 32694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 32794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 32894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 32994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood free(android_system_dirs.dirs); 33094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 33194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 33294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodint initialize_globals() { 33394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood // Get the android data directory. 33494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (get_path_from_env(&android_data_dir, "ANDROID_DATA") < 0) { 33594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return -1; 33694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 33794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 33894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood // Get the android app directory. 33994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (copy_and_append(&android_app_dir, &android_data_dir, APP_SUBDIR) < 0) { 34094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return -1; 34194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 34294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 34394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood // Get the android protected app directory. 34494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (copy_and_append(&android_app_private_dir, &android_data_dir, PRIVATE_APP_SUBDIR) < 0) { 34594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return -1; 34694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 34794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 34894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood // Get the android app native library directory. 34994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (copy_and_append(&android_app_lib_dir, &android_data_dir, APP_LIB_SUBDIR) < 0) { 35094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return -1; 35194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 35294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 35394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood // Get the sd-card ASEC mount point. 35494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (get_path_from_env(&android_asec_dir, "ASEC_MOUNTPOINT") < 0) { 35594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return -1; 35694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 35794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 35894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood // Get the android media directory. 35994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (copy_and_append(&android_media_dir, &android_data_dir, MEDIA_SUBDIR) < 0) { 36094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return -1; 36194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 36294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 363e23a13299a4f6f2488935b4786cdbb46f46e3d3cJeff Sharkey // Get the android external app directory. 36419803807cd7ae01868fcfa50305f4a7dd13765e2Jeff Sharkey if (get_path_from_string(&android_mnt_expand_dir, "/mnt/expand/") < 0) { 365e23a13299a4f6f2488935b4786cdbb46f46e3d3cJeff Sharkey return -1; 366e23a13299a4f6f2488935b4786cdbb46f46e3d3cJeff Sharkey } 367e23a13299a4f6f2488935b4786cdbb46f46e3d3cJeff Sharkey 36894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood // Take note of the system and vendor directories. 369770180a4dd86f8bda6af2e6db4676e99a5bb1548Jeff Sharkey android_system_dirs.count = 4; 37094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 37119803807cd7ae01868fcfa50305f4a7dd13765e2Jeff Sharkey android_system_dirs.dirs = (dir_rec_t*) calloc(android_system_dirs.count, sizeof(dir_rec_t)); 37294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (android_system_dirs.dirs == NULL) { 37394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGE("Couldn't allocate array for dirs; aborting\n"); 37494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return -1; 37594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 37694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 377770180a4dd86f8bda6af2e6db4676e99a5bb1548Jeff Sharkey dir_rec_t android_root_dir; 378770180a4dd86f8bda6af2e6db4676e99a5bb1548Jeff Sharkey if (get_path_from_env(&android_root_dir, "ANDROID_ROOT") < 0) { 379770180a4dd86f8bda6af2e6db4676e99a5bb1548Jeff Sharkey ALOGE("Missing ANDROID_ROOT; aborting\n"); 38094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return -1; 38194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 38294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 383770180a4dd86f8bda6af2e6db4676e99a5bb1548Jeff Sharkey android_system_dirs.dirs[0].path = build_string2(android_root_dir.path, APP_SUBDIR); 384770180a4dd86f8bda6af2e6db4676e99a5bb1548Jeff Sharkey android_system_dirs.dirs[0].len = strlen(android_system_dirs.dirs[0].path); 38594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 386770180a4dd86f8bda6af2e6db4676e99a5bb1548Jeff Sharkey android_system_dirs.dirs[1].path = build_string2(android_root_dir.path, PRIV_APP_SUBDIR); 38794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood android_system_dirs.dirs[1].len = strlen(android_system_dirs.dirs[1].path); 38894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 38919803807cd7ae01868fcfa50305f4a7dd13765e2Jeff Sharkey android_system_dirs.dirs[2].path = strdup("/vendor/app/"); 390770180a4dd86f8bda6af2e6db4676e99a5bb1548Jeff Sharkey android_system_dirs.dirs[2].len = strlen(android_system_dirs.dirs[2].path); 391770180a4dd86f8bda6af2e6db4676e99a5bb1548Jeff Sharkey 39219803807cd7ae01868fcfa50305f4a7dd13765e2Jeff Sharkey android_system_dirs.dirs[3].path = strdup("/oem/app/"); 393770180a4dd86f8bda6af2e6db4676e99a5bb1548Jeff Sharkey android_system_dirs.dirs[3].len = strlen(android_system_dirs.dirs[3].path); 394770180a4dd86f8bda6af2e6db4676e99a5bb1548Jeff Sharkey 39594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return 0; 39694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 39794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 39894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodint initialize_directories() { 39994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood int res = -1; 40094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 40194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood // Read current filesystem layout version to handle upgrade paths 40294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood char version_path[PATH_MAX]; 40394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood snprintf(version_path, PATH_MAX, "%s.layout_version", android_data_dir.path); 40494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 40594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood int oldVersion; 40694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (fs_read_atomic_int(version_path, &oldVersion) == -1) { 40794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood oldVersion = 0; 40894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 40994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood int version = oldVersion; 41094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 41194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood // /data/user 41294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood char *user_data_dir = build_string2(android_data_dir.path, SECONDARY_USER_PREFIX); 41394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood // /data/data 41494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood char *legacy_data_dir = build_string2(android_data_dir.path, PRIMARY_USER_PREFIX); 41594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood // /data/user/0 41694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood char *primary_data_dir = build_string3(android_data_dir.path, SECONDARY_USER_PREFIX, "0"); 41794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (!user_data_dir || !legacy_data_dir || !primary_data_dir) { 41894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood goto fail; 41994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 42094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 42194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood // Make the /data/user directory if necessary 42294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (access(user_data_dir, R_OK) < 0) { 42394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (mkdir(user_data_dir, 0711) < 0) { 42494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood goto fail; 42594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 42694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (chown(user_data_dir, AID_SYSTEM, AID_SYSTEM) < 0) { 42794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood goto fail; 42894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 42994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (chmod(user_data_dir, 0711) < 0) { 43094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood goto fail; 43194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 43294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 43394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood // Make the /data/user/0 symlink to /data/data if necessary 43494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (access(primary_data_dir, R_OK) < 0) { 43594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (symlink(legacy_data_dir, primary_data_dir)) { 43694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood goto fail; 43794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 43894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 43994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 44094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (version == 0) { 44194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood // Introducing multi-user, so migrate /data/media contents into /data/media/0 44294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGD("Upgrading /data/media for multi-user"); 44394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 44494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood // Ensure /data/media 44594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (fs_prepare_dir(android_media_dir.path, 0770, AID_MEDIA_RW, AID_MEDIA_RW) == -1) { 44694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood goto fail; 44794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 44894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 44994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood // /data/media.tmp 45094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood char media_tmp_dir[PATH_MAX]; 45194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood snprintf(media_tmp_dir, PATH_MAX, "%smedia.tmp", android_data_dir.path); 45294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 45394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood // Only copy when upgrade not already in progress 45494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (access(media_tmp_dir, F_OK) == -1) { 45594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (rename(android_media_dir.path, media_tmp_dir) == -1) { 45694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGE("Failed to move legacy media path: %s", strerror(errno)); 45794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood goto fail; 45894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 45994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 46094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 46194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood // Create /data/media again 46294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (fs_prepare_dir(android_media_dir.path, 0770, AID_MEDIA_RW, AID_MEDIA_RW) == -1) { 46394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood goto fail; 46494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 46594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 46626288202e7bdf2e897a11bf31a15685d7c20945fStephen Smalley if (selinux_android_restorecon(android_media_dir.path, 0)) { 46747a351834f202386b01a27d42ec41ceb1f17b754Stephen Smalley goto fail; 46847a351834f202386b01a27d42ec41ceb1f17b754Stephen Smalley } 46947a351834f202386b01a27d42ec41ceb1f17b754Stephen Smalley 47094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood // /data/media/0 47194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood char owner_media_dir[PATH_MAX]; 47294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood snprintf(owner_media_dir, PATH_MAX, "%s0", android_media_dir.path); 47394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 47494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood // Move any owner data into place 47594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (access(media_tmp_dir, F_OK) == 0) { 47694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (rename(media_tmp_dir, owner_media_dir) == -1) { 47794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGE("Failed to move owner media path: %s", strerror(errno)); 47894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood goto fail; 47994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 48094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 48194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 48294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood // Ensure media directories for any existing users 48394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood DIR *dir; 48494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood struct dirent *dirent; 48594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood char user_media_dir[PATH_MAX]; 48694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 48794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood dir = opendir(user_data_dir); 48894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (dir != NULL) { 48994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood while ((dirent = readdir(dir))) { 49094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (dirent->d_type == DT_DIR) { 49194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood const char *name = dirent->d_name; 49294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 49394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood // skip "." and ".." 49494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (name[0] == '.') { 49594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (name[1] == 0) continue; 49694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if ((name[1] == '.') && (name[2] == 0)) continue; 49794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 49894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 49994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood // /data/media/<user_id> 50094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood snprintf(user_media_dir, PATH_MAX, "%s%s", android_media_dir.path, name); 50194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (fs_prepare_dir(user_media_dir, 0770, AID_MEDIA_RW, AID_MEDIA_RW) == -1) { 50294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood goto fail; 50394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 50494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 50594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 50694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood closedir(dir); 50794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 50894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 50994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood version = 1; 51094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 51194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 51294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood // /data/media/obb 51394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood char media_obb_dir[PATH_MAX]; 51494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood snprintf(media_obb_dir, PATH_MAX, "%sobb", android_media_dir.path); 51594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 51694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (version == 1) { 51794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood // Introducing /data/media/obb for sharing OBB across users; migrate 51894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood // any existing OBB files from owner. 51994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGD("Upgrading to shared /data/media/obb"); 52094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 52194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood // /data/media/0/Android/obb 52294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood char owner_obb_path[PATH_MAX]; 52394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood snprintf(owner_obb_path, PATH_MAX, "%s0/Android/obb", android_media_dir.path); 52494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 52594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood // Only move if target doesn't already exist 52694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (access(media_obb_dir, F_OK) != 0 && access(owner_obb_path, F_OK) == 0) { 52794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (rename(owner_obb_path, media_obb_dir) == -1) { 52894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGE("Failed to move OBB from owner: %s", strerror(errno)); 52994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood goto fail; 53094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 53194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 53294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 53394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood version = 2; 53494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 53594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 53641ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey if (ensure_media_user_dirs(nullptr, 0) == -1) { 53794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGE("Failed to setup media for user 0"); 53894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood goto fail; 53994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 54094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (fs_prepare_dir(media_obb_dir, 0770, AID_MEDIA_RW, AID_MEDIA_RW) == -1) { 54194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood goto fail; 54294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 54394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 54407053fcb61436221fac2281394e98ec9d0feab3dRobin Lee if (ensure_config_user_dirs(0) == -1) { 54507053fcb61436221fac2281394e98ec9d0feab3dRobin Lee ALOGE("Failed to setup misc for user 0"); 54607053fcb61436221fac2281394e98ec9d0feab3dRobin Lee goto fail; 54707053fcb61436221fac2281394e98ec9d0feab3dRobin Lee } 54807053fcb61436221fac2281394e98ec9d0feab3dRobin Lee 549095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee if (version == 2) { 550095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee ALOGD("Upgrading to /data/misc/user directories"); 551095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee 55260fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee char misc_dir[PATH_MAX]; 55360fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee snprintf(misc_dir, PATH_MAX, "%smisc", android_data_dir.path); 55460fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee 55560fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee char keychain_added_dir[PATH_MAX]; 55660fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee snprintf(keychain_added_dir, PATH_MAX, "%s/keychain/cacerts-added", misc_dir); 55760fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee 55860fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee char keychain_removed_dir[PATH_MAX]; 55960fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee snprintf(keychain_removed_dir, PATH_MAX, "%s/keychain/cacerts-removed", misc_dir); 56060fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee 561095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee DIR *dir; 562095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee struct dirent *dirent; 563095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee dir = opendir(user_data_dir); 564095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee if (dir != NULL) { 565095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee while ((dirent = readdir(dir))) { 56660fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee const char *name = dirent->d_name; 567095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee 56860fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee // skip "." and ".." 56960fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee if (name[0] == '.') { 57060fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee if (name[1] == 0) continue; 57160fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee if ((name[1] == '.') && (name[2] == 0)) continue; 57260fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee } 573095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee 57460fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee uint32_t user_id = atoi(name); 57560fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee 57660fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee // /data/misc/user/<user_id> 57760fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee if (ensure_config_user_dirs(user_id) == -1) { 57860fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee goto fail; 57960fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee } 58060fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee 58160fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee char misc_added_dir[PATH_MAX]; 58260fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee snprintf(misc_added_dir, PATH_MAX, "%s/user/%s/cacerts-added", misc_dir, name); 58360fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee 58460fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee char misc_removed_dir[PATH_MAX]; 58560fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee snprintf(misc_removed_dir, PATH_MAX, "%s/user/%s/cacerts-removed", misc_dir, name); 58660fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee 58760fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee uid_t uid = multiuser_get_uid(user_id, AID_SYSTEM); 58860fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee gid_t gid = uid; 58960fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee if (access(keychain_added_dir, F_OK) == 0) { 59060fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee if (copy_dir_files(keychain_added_dir, misc_added_dir, uid, gid) != 0) { 59160fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee ALOGE("Some files failed to copy"); 59260fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee } 59360fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee } 59460fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee if (access(keychain_removed_dir, F_OK) == 0) { 59560fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee if (copy_dir_files(keychain_removed_dir, misc_removed_dir, uid, gid) != 0) { 59660fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee ALOGE("Some files failed to copy"); 597095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee } 598095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee } 599095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee } 600095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee closedir(dir); 60107053fcb61436221fac2281394e98ec9d0feab3dRobin Lee 60260fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee if (access(keychain_added_dir, F_OK) == 0) { 60360fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee delete_dir_contents(keychain_added_dir, 1, 0); 60407053fcb61436221fac2281394e98ec9d0feab3dRobin Lee } 60560fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee if (access(keychain_removed_dir, F_OK) == 0) { 60660fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee delete_dir_contents(keychain_removed_dir, 1, 0); 60707053fcb61436221fac2281394e98ec9d0feab3dRobin Lee } 60807053fcb61436221fac2281394e98ec9d0feab3dRobin Lee } 609095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee 61007053fcb61436221fac2281394e98ec9d0feab3dRobin Lee version = 3; 611095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee } 612095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee 61394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood // Persist layout version if changed 61494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (version != oldVersion) { 61594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (fs_write_atomic_int(version_path, version) == -1) { 61694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGE("Failed to save version to %s: %s", version_path, strerror(errno)); 61794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood goto fail; 61894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 61994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 62094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 62194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood // Success! 62294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood res = 0; 62394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 62494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodfail: 62594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood free(user_data_dir); 62694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood free(legacy_data_dir); 62794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood free(primary_data_dir); 62894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return res; 62994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 63094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 6317abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalleystatic int log_callback(int type, const char *fmt, ...) { 6327abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalley va_list ap; 6337abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalley int priority; 6347abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalley 6357abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalley switch (type) { 6367abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalley case SELINUX_WARNING: 6377abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalley priority = ANDROID_LOG_WARN; 6387abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalley break; 6397abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalley case SELINUX_INFO: 6407abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalley priority = ANDROID_LOG_INFO; 6417abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalley break; 6427abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalley default: 6437abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalley priority = ANDROID_LOG_ERROR; 6447abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalley break; 6457abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalley } 6467abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalley va_start(ap, fmt); 6477abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalley LOG_PRI_VA(priority, "SELinux", fmt, ap); 6487abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalley va_end(ap); 6497abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalley return 0; 6507abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalley} 6517abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalley 652e36372423000a906bafae68844ebc6c42d09335aJeff Sharkeyint main(const int argc __unused, char *argv[]) { 65394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood char buf[BUFFER_MAX]; 65494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood struct sockaddr addr; 65594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood socklen_t alen; 65699d9fb15b4a1eb534261ae81b2cf25aec4447bd0Chih-Hung Hsieh int lsocket, s; 657bd558d61871218f5b13df2fe4b7cc3b530ee947cStephen Smalley int selinux_enabled = (is_selinux_enabled() > 0); 65894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 659e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey setenv("ANDROID_LOG_TAGS", "*:v", 1); 660e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey android::base::InitLogging(argv); 661e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey 66294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGI("installd firing up\n"); 66394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 6647abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalley union selinux_callback cb; 6657abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalley cb.func_log = log_callback; 6667abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalley selinux_set_callback(SELINUX_CB_LOG, cb); 6677abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalley 66894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (initialize_globals() < 0) { 66994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGE("Could not initialize globals; exiting.\n"); 67094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood exit(1); 67194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 67294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 67394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (initialize_directories() < 0) { 67494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGE("Could not create directories; exiting.\n"); 67594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood exit(1); 67694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 67794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 678bd558d61871218f5b13df2fe4b7cc3b530ee947cStephen Smalley if (selinux_enabled && selinux_status_open(true) < 0) { 679bd558d61871218f5b13df2fe4b7cc3b530ee947cStephen Smalley ALOGE("Could not open selinux status; exiting.\n"); 680bd558d61871218f5b13df2fe4b7cc3b530ee947cStephen Smalley exit(1); 681bd558d61871218f5b13df2fe4b7cc3b530ee947cStephen Smalley } 682bd558d61871218f5b13df2fe4b7cc3b530ee947cStephen Smalley 68394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood lsocket = android_get_control_socket(SOCKET_PATH); 68494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (lsocket < 0) { 68594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGE("Failed to get socket from environment: %s\n", strerror(errno)); 68694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood exit(1); 68794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 68894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (listen(lsocket, 5)) { 68994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGE("Listen on socket failed: %s\n", strerror(errno)); 69094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood exit(1); 69194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 69294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood fcntl(lsocket, F_SETFD, FD_CLOEXEC); 69394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 69494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood for (;;) { 69594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood alen = sizeof(addr); 69694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood s = accept(lsocket, &addr, &alen); 69794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (s < 0) { 69894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGE("Accept failed: %s\n", strerror(errno)); 69994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood continue; 70094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 70194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood fcntl(s, F_SETFD, FD_CLOEXEC); 70294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 70394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGI("new connection\n"); 70494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood for (;;) { 70594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood unsigned short count; 70694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (readx(s, &count, sizeof(count))) { 70794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGE("failed to read size\n"); 70894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood break; 70994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 71094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if ((count < 1) || (count >= BUFFER_MAX)) { 71194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGE("invalid size %d\n", count); 71294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood break; 71394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 71494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (readx(s, buf, count)) { 71594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGE("failed to read command\n"); 71694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood break; 71794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 71894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood buf[count] = 0; 719bd558d61871218f5b13df2fe4b7cc3b530ee947cStephen Smalley if (selinux_enabled && selinux_status_updated() > 0) { 720bd558d61871218f5b13df2fe4b7cc3b530ee947cStephen Smalley selinux_android_seapp_context_reload(); 721bd558d61871218f5b13df2fe4b7cc3b530ee947cStephen Smalley } 72294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (execute(s, buf)) break; 72394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 72494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGI("closing connection\n"); 72594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood close(s); 72694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 72794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 72894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return 0; 72994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 730