installd.cpp revision 19803807cd7ae01868fcfa50305f4a7dd13765e2
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 17d747129e1e8876f5a50f47156ec50b6969a638e4Nick Kralevich#include <sys/capability.h> 18119b765a053f650b4b47256245ce836f8c403d7fElliott Hughes#include <sys/prctl.h> 19bd558d61871218f5b13df2fe4b7cc3b530ee947cStephen Smalley#include <selinux/android.h> 20bd558d61871218f5b13df2fe4b7cc3b530ee947cStephen Smalley#include <selinux/avc.h> 2194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 2294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood#include "installd.h" 2394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 2494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 2594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood#define BUFFER_MAX 1024 /* input buffer for commands */ 2688ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov#define TOKEN_MAX 16 /* max number of arguments in buffer */ 2794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood#define REPLY_MAX 256 /* largest reply allowed */ 2894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 2999d9fb15b4a1eb534261ae81b2cf25aec4447bd0Chih-Hung Hsiehstatic int do_ping(char **arg __unused, char reply[REPLY_MAX] __unused) 3094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{ 3194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return 0; 3294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 3394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 3499d9fb15b4a1eb534261ae81b2cf25aec4447bd0Chih-Hung Hsiehstatic int do_install(char **arg, char reply[REPLY_MAX] __unused) 3594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{ 364d3fd4e9988c0eb284dd5104c4dea757f723c716Robert Craig return install(arg[0], atoi(arg[1]), atoi(arg[2]), arg[3]); /* pkgname, uid, gid, seinfo */ 3794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 3894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 3999d9fb15b4a1eb534261ae81b2cf25aec4447bd0Chih-Hung Hsiehstatic int do_dexopt(char **arg, char reply[REPLY_MAX] __unused) 4094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{ 4188ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov /* apk_path, uid, is_public, pkgname, instruction_set, vm_safe_mode, should_relocate 4288ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov debuggable, outputPath */ 4319803807cd7ae01868fcfa50305f4a7dd13765e2Jeff Sharkey return dexopt(arg[0], atoi(arg[1]), atoi(arg[2]), arg[3], arg[4], atoi(arg[5]), 0, 4488ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov atoi(arg[6]), arg[7]); 4594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 4694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 477fb390d10c896c861ba6205f2232669586ac5085Bernhard Rosenkränzerstatic int do_mark_boot_complete(char **arg, char reply[REPLY_MAX] __unused) 48091ea779d4e575856c04d51d82f45cc8a6155b5eNarayan Kamath{ 49091ea779d4e575856c04d51d82f45cc8a6155b5eNarayan Kamath return mark_boot_complete(arg[0] /* instruction set */); 50091ea779d4e575856c04d51d82f45cc8a6155b5eNarayan Kamath} 51091ea779d4e575856c04d51d82f45cc8a6155b5eNarayan Kamath 527fb390d10c896c861ba6205f2232669586ac5085Bernhard Rosenkränzerstatic int do_move_dex(char **arg, char reply[REPLY_MAX] __unused) 5394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{ 541b4003207750ea8fe8c7b03eb32d80f1df83979eNarayan Kamath return move_dex(arg[0], arg[1], arg[2]); /* src, dst, instruction_set */ 5594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 5694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 5799d9fb15b4a1eb534261ae81b2cf25aec4447bd0Chih-Hung Hsiehstatic int do_rm_dex(char **arg, char reply[REPLY_MAX] __unused) 5894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{ 591b4003207750ea8fe8c7b03eb32d80f1df83979eNarayan Kamath return rm_dex(arg[0], arg[1]); /* pkgname, instruction_set */ 6094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 6194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 6299d9fb15b4a1eb534261ae81b2cf25aec4447bd0Chih-Hung Hsiehstatic int do_remove(char **arg, char reply[REPLY_MAX] __unused) 6394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{ 6494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return uninstall(arg[0], atoi(arg[1])); /* pkgname, userid */ 6594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 6694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 6799d9fb15b4a1eb534261ae81b2cf25aec4447bd0Chih-Hung Hsiehstatic int do_rename(char **arg, char reply[REPLY_MAX] __unused) 6894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{ 6994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return renamepkg(arg[0], arg[1]); /* oldpkgname, newpkgname */ 7094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 7194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 7299d9fb15b4a1eb534261ae81b2cf25aec4447bd0Chih-Hung Hsiehstatic int do_fixuid(char **arg, char reply[REPLY_MAX] __unused) 7394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{ 7494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return fix_uid(arg[0], atoi(arg[1]), atoi(arg[2])); /* pkgname, uid, gid */ 7594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 7694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 7799d9fb15b4a1eb534261ae81b2cf25aec4447bd0Chih-Hung Hsiehstatic int do_free_cache(char **arg, char reply[REPLY_MAX] __unused) /* TODO int:free_size */ 7894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{ 7994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return free_cache((int64_t)atoll(arg[0])); /* free_size */ 8094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 8194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 8299d9fb15b4a1eb534261ae81b2cf25aec4447bd0Chih-Hung Hsiehstatic int do_rm_cache(char **arg, char reply[REPLY_MAX] __unused) 8394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{ 8494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return delete_cache(arg[0], atoi(arg[1])); /* pkgname, userid */ 8594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 8694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 879e8b528926506b3fa9ca7df52fef5f56ec6149c7Dan Albertstatic int do_rm_code_cache(char **arg, char reply[REPLY_MAX] __unused) 88c796b681e52fbb792da9a5b4f30e935cc927c1d7Jeff Sharkey{ 89c796b681e52fbb792da9a5b4f30e935cc927c1d7Jeff Sharkey return delete_code_cache(arg[0], atoi(arg[1])); /* pkgname, userid */ 90c796b681e52fbb792da9a5b4f30e935cc927c1d7Jeff Sharkey} 91c796b681e52fbb792da9a5b4f30e935cc927c1d7Jeff Sharkey 9294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodstatic int do_get_size(char **arg, char reply[REPLY_MAX]) 9394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{ 9494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood int64_t codesize = 0; 9594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood int64_t datasize = 0; 9694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood int64_t cachesize = 0; 9794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood int64_t asecsize = 0; 9894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood int res = 0; 9994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 100abe4fe5b46157ecd2a52d28abf938c816c3ce878Jeff Sharkey /* pkgdir, userid, apkpath */ 1018b41780d73930b37b6254cc1dac5607c843839c0Dianne Hackborn res = get_size(arg[0], atoi(arg[1]), arg[2], arg[3], arg[4], arg[5], 1021b4003207750ea8fe8c7b03eb32d80f1df83979eNarayan Kamath arg[6], &codesize, &datasize, &cachesize, &asecsize); 10394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 10494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood /* 10594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * Each int64_t can take up 22 characters printed out. Make sure it 10694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * doesn't go over REPLY_MAX in the future. 10794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood */ 10894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood snprintf(reply, REPLY_MAX, "%" PRId64 " %" PRId64 " %" PRId64 " %" PRId64, 10994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood codesize, datasize, cachesize, asecsize); 11094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return res; 11194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 11294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 11399d9fb15b4a1eb534261ae81b2cf25aec4447bd0Chih-Hung Hsiehstatic int do_rm_user_data(char **arg, char reply[REPLY_MAX] __unused) 11494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{ 11594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return delete_user_data(arg[0], atoi(arg[1])); /* pkgname, userid */ 11694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 11794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 11899d9fb15b4a1eb534261ae81b2cf25aec4447bd0Chih-Hung Hsiehstatic int do_mk_user_data(char **arg, char reply[REPLY_MAX] __unused) 11994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{ 120880d1a957ebcb63fb9d3724e2f91c58b7ff0cd54Robert Craig return make_user_data(arg[0], atoi(arg[1]), atoi(arg[2]), arg[3]); 121880d1a957ebcb63fb9d3724e2f91c58b7ff0cd54Robert Craig /* pkgname, uid, userid, seinfo */ 12294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 12394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 12499d9fb15b4a1eb534261ae81b2cf25aec4447bd0Chih-Hung Hsiehstatic int do_mk_user_config(char **arg, char reply[REPLY_MAX] __unused) 125095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee{ 1267c8bec01790087748ec7afa69a31789828b751f9Robin Lee return make_user_config(atoi(arg[0])); /* userid */ 127095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee} 128095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee 12999d9fb15b4a1eb534261ae81b2cf25aec4447bd0Chih-Hung Hsiehstatic int do_rm_user(char **arg, char reply[REPLY_MAX] __unused) 13094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{ 131abe4fe5b46157ecd2a52d28abf938c816c3ce878Jeff Sharkey return delete_user(atoi(arg[0])); /* userid */ 13294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 13394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 13499d9fb15b4a1eb534261ae81b2cf25aec4447bd0Chih-Hung Hsiehstatic int do_movefiles(char **arg __unused, char reply[REPLY_MAX] __unused) 13594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{ 13694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return movefiles(); 13794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 13894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 13999d9fb15b4a1eb534261ae81b2cf25aec4447bd0Chih-Hung Hsiehstatic int do_linklib(char **arg, char reply[REPLY_MAX] __unused) 14094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{ 14194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return linklib(arg[0], arg[1], atoi(arg[2])); 14294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 14394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 14499d9fb15b4a1eb534261ae81b2cf25aec4447bd0Chih-Hung Hsiehstatic int do_idmap(char **arg, char reply[REPLY_MAX] __unused) 14563568b1430d741f40ca008391c854ef1cc880138Mårten Kongstad{ 14663568b1430d741f40ca008391c854ef1cc880138Mårten Kongstad return idmap(arg[0], arg[1], atoi(arg[2])); 14763568b1430d741f40ca008391c854ef1cc880138Mårten Kongstad} 14863568b1430d741f40ca008391c854ef1cc880138Mårten Kongstad 149da30dc7336f03ca629fe173db1425fdce989119cRobert Craigstatic int do_restorecon_data(char **arg, char reply[REPLY_MAX] __attribute__((unused))) 150e9887e46cea4a095e4219927eadbe4c57bb1a5eeRobert Craig{ 151da30dc7336f03ca629fe173db1425fdce989119cRobert Craig return restorecon_data(arg[0], arg[1], atoi(arg[2])); 152da30dc7336f03ca629fe173db1425fdce989119cRobert Craig /* pkgName, seinfo, uid*/ 153e9887e46cea4a095e4219927eadbe4c57bb1a5eeRobert Craig} 154e9887e46cea4a095e4219927eadbe4c57bb1a5eeRobert Craig 15599d9fb15b4a1eb534261ae81b2cf25aec4447bd0Chih-Hung Hsiehstatic int do_patchoat(char **arg, char reply[REPLY_MAX] __unused) { 156598c25e23f7c97470e09a2316513ddf2efdfb670Andreas Gampe /* apk_path, uid, is_public, pkgname, instruction_set, vm_safe_mode, should_relocate, 15788ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov debuggable, outputPath */ 15888ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov return dexopt(arg[0], atoi(arg[1]), atoi(arg[2]), arg[3], arg[4], 0, 1, 0, "!"); 15988ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov} 16088ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov 16188ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolovstatic int do_create_oat_dir(char **arg, char reply[REPLY_MAX] __unused) 16288ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov{ 16388ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov /* oat_dir, instruction_set */ 16488ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov return create_oat_dir(arg[0], arg[1]); 16588ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov} 16688ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov 16788ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolovstatic int do_rm_package_dir(char **arg, char reply[REPLY_MAX] __unused) 16888ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov{ 16988ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov /* oat_dir */ 17088ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov return rm_package_dir(arg[0]); 17143c5d30795faf08ab639b8d88c2eceaf2b648c93Alex Light} 17243c5d30795faf08ab639b8d88c2eceaf2b648c93Alex Light 17394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodstruct cmdinfo { 17494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood const char *name; 17594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood unsigned numargs; 17694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood int (*func)(char **arg, char reply[REPLY_MAX]); 17794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}; 17894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 17994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodstruct cmdinfo cmds[] = { 18094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood { "ping", 0, do_ping }, 1814d3fd4e9988c0eb284dd5104c4dea757f723c716Robert Craig { "install", 4, do_install }, 18288ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov { "dexopt", 8, do_dexopt }, 183091ea779d4e575856c04d51d82f45cc8a6155b5eNarayan Kamath { "markbootcomplete", 1, do_mark_boot_complete }, 1841b4003207750ea8fe8c7b03eb32d80f1df83979eNarayan Kamath { "movedex", 3, do_move_dex }, 1851b4003207750ea8fe8c7b03eb32d80f1df83979eNarayan Kamath { "rmdex", 2, do_rm_dex }, 18694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood { "remove", 2, do_remove }, 18794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood { "rename", 2, do_rename }, 18894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood { "fixuid", 3, do_fixuid }, 18994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood { "freecache", 1, do_free_cache }, 19094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood { "rmcache", 2, do_rm_cache }, 191c796b681e52fbb792da9a5b4f30e935cc927c1d7Jeff Sharkey { "rmcodecache", 2, do_rm_code_cache }, 1921b4003207750ea8fe8c7b03eb32d80f1df83979eNarayan Kamath { "getsize", 7, do_get_size }, 19394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood { "rmuserdata", 2, do_rm_user_data }, 19494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood { "movefiles", 0, do_movefiles }, 19594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood { "linklib", 3, do_linklib }, 196880d1a957ebcb63fb9d3724e2f91c58b7ff0cd54Robert Craig { "mkuserdata", 4, do_mk_user_data }, 1977c8bec01790087748ec7afa69a31789828b751f9Robin Lee { "mkuserconfig", 1, do_mk_user_config }, 19894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood { "rmuser", 1, do_rm_user }, 19963568b1430d741f40ca008391c854ef1cc880138Mårten Kongstad { "idmap", 3, do_idmap }, 200da30dc7336f03ca629fe173db1425fdce989119cRobert Craig { "restorecondata", 3, do_restorecon_data }, 20143c5d30795faf08ab639b8d88c2eceaf2b648c93Alex Light { "patchoat", 5, do_patchoat }, 20288ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov { "createoatdir", 2, do_create_oat_dir }, 20388ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov { "rmpackagedir", 1, do_rm_package_dir}, 20494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}; 20594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 20694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodstatic int readx(int s, void *_buf, int count) 20794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{ 20819803807cd7ae01868fcfa50305f4a7dd13765e2Jeff Sharkey char *buf = (char *) _buf; 20994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood int n = 0, r; 21094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (count < 0) return -1; 21194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood while (n < count) { 21294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood r = read(s, buf + n, count - n); 21394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (r < 0) { 21494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (errno == EINTR) continue; 21594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGE("read error: %s\n", strerror(errno)); 21694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return -1; 21794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 21894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (r == 0) { 21994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGE("eof\n"); 22094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return -1; /* EOF */ 22194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 22294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood n += r; 22394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 22494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return 0; 22594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 22694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 22794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodstatic int writex(int s, const void *_buf, int count) 22894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{ 22919803807cd7ae01868fcfa50305f4a7dd13765e2Jeff Sharkey const char *buf = (const char *) _buf; 23094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood int n = 0, r; 23194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (count < 0) return -1; 23294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood while (n < count) { 23394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood r = write(s, buf + n, count - n); 23494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (r < 0) { 23594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (errno == EINTR) continue; 23694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGE("write error: %s\n", strerror(errno)); 23794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return -1; 23894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 23994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood n += r; 24094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 24194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return 0; 24294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 24394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 24494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 24594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood/* Tokenize the command buffer, locate a matching command, 24694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * ensure that the required number of arguments are provided, 24794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * call the function(), return the result. 24894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood */ 24994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodstatic int execute(int s, char cmd[BUFFER_MAX]) 25094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{ 25194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood char reply[REPLY_MAX]; 25294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood char *arg[TOKEN_MAX+1]; 25394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood unsigned i; 25494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood unsigned n = 0; 25594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood unsigned short count; 25694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood int ret = -1; 25794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 2581705fc44fb85c4232637f9f7189c3bdca98a63d5Brian Carlstrom // ALOGI("execute('%s')\n", cmd); 25994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 26094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood /* default reply is "" */ 26194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood reply[0] = 0; 26294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 26394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood /* n is number of args (not counting arg[0]) */ 26494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood arg[0] = cmd; 26594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood while (*cmd) { 26694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (isspace(*cmd)) { 26794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood *cmd++ = 0; 26894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood n++; 26994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood arg[n] = cmd; 27094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (n == TOKEN_MAX) { 27194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGE("too many arguments\n"); 27294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood goto done; 27394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 27494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 27562bb385728121b456782d205f6e27d0c18be9c0bSerguei Katkov if (*cmd) { 27662bb385728121b456782d205f6e27d0c18be9c0bSerguei Katkov cmd++; 27762bb385728121b456782d205f6e27d0c18be9c0bSerguei Katkov } 27894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 27994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 28094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood for (i = 0; i < sizeof(cmds) / sizeof(cmds[0]); i++) { 28194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (!strcmp(cmds[i].name,arg[0])) { 28294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (n != cmds[i].numargs) { 28394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGE("%s requires %d arguments (%d given)\n", 28494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood cmds[i].name, cmds[i].numargs, n); 28594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } else { 28694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ret = cmds[i].func(arg + 1, reply); 28794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 28894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood goto done; 28994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 29094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 29194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGE("unsupported command '%s'\n", arg[0]); 29294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 29394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwooddone: 29494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (reply[0]) { 29594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood n = snprintf(cmd, BUFFER_MAX, "%d %s", ret, reply); 29694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } else { 29794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood n = snprintf(cmd, BUFFER_MAX, "%d", ret); 29894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 29994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (n > BUFFER_MAX) n = BUFFER_MAX; 30094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood count = n; 30194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 3021705fc44fb85c4232637f9f7189c3bdca98a63d5Brian Carlstrom // ALOGI("reply: '%s'\n", cmd); 30394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (writex(s, &count, sizeof(count))) return -1; 30494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (writex(s, cmd, count)) return -1; 30594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return 0; 30694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 30794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 30894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood/** 30994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * Initialize all the global variables that are used elsewhere. Returns 0 upon 31094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * success and -1 on error. 31194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood */ 31294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodvoid free_globals() { 31394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood size_t i; 31494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 31594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood for (i = 0; i < android_system_dirs.count; i++) { 31694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (android_system_dirs.dirs[i].path != NULL) { 31794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood free(android_system_dirs.dirs[i].path); 31894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 31994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 32094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 32194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood free(android_system_dirs.dirs); 32294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 32394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 32494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodint initialize_globals() { 32594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood // Get the android data directory. 32694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (get_path_from_env(&android_data_dir, "ANDROID_DATA") < 0) { 32794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return -1; 32894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 32994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 33094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood // Get the android app directory. 33194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (copy_and_append(&android_app_dir, &android_data_dir, APP_SUBDIR) < 0) { 33294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return -1; 33394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 33494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 33594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood // Get the android protected app directory. 33694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (copy_and_append(&android_app_private_dir, &android_data_dir, PRIVATE_APP_SUBDIR) < 0) { 33794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return -1; 33894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 33994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 34094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood // Get the android app native library directory. 34194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (copy_and_append(&android_app_lib_dir, &android_data_dir, APP_LIB_SUBDIR) < 0) { 34294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return -1; 34394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 34494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 34594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood // Get the sd-card ASEC mount point. 34694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (get_path_from_env(&android_asec_dir, "ASEC_MOUNTPOINT") < 0) { 34794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return -1; 34894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 34994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 35094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood // Get the android media directory. 35194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (copy_and_append(&android_media_dir, &android_data_dir, MEDIA_SUBDIR) < 0) { 35294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return -1; 35394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 35494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 355e23a13299a4f6f2488935b4786cdbb46f46e3d3cJeff Sharkey // Get the android external app directory. 35619803807cd7ae01868fcfa50305f4a7dd13765e2Jeff Sharkey if (get_path_from_string(&android_mnt_expand_dir, "/mnt/expand/") < 0) { 357e23a13299a4f6f2488935b4786cdbb46f46e3d3cJeff Sharkey return -1; 358e23a13299a4f6f2488935b4786cdbb46f46e3d3cJeff Sharkey } 359e23a13299a4f6f2488935b4786cdbb46f46e3d3cJeff Sharkey 36094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood // Take note of the system and vendor directories. 361770180a4dd86f8bda6af2e6db4676e99a5bb1548Jeff Sharkey android_system_dirs.count = 4; 36294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 36319803807cd7ae01868fcfa50305f4a7dd13765e2Jeff Sharkey android_system_dirs.dirs = (dir_rec_t*) calloc(android_system_dirs.count, sizeof(dir_rec_t)); 36494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (android_system_dirs.dirs == NULL) { 36594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGE("Couldn't allocate array for dirs; aborting\n"); 36694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return -1; 36794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 36894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 369770180a4dd86f8bda6af2e6db4676e99a5bb1548Jeff Sharkey dir_rec_t android_root_dir; 370770180a4dd86f8bda6af2e6db4676e99a5bb1548Jeff Sharkey if (get_path_from_env(&android_root_dir, "ANDROID_ROOT") < 0) { 371770180a4dd86f8bda6af2e6db4676e99a5bb1548Jeff Sharkey ALOGE("Missing ANDROID_ROOT; aborting\n"); 37294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return -1; 37394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 37494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 375770180a4dd86f8bda6af2e6db4676e99a5bb1548Jeff Sharkey android_system_dirs.dirs[0].path = build_string2(android_root_dir.path, APP_SUBDIR); 376770180a4dd86f8bda6af2e6db4676e99a5bb1548Jeff Sharkey android_system_dirs.dirs[0].len = strlen(android_system_dirs.dirs[0].path); 37794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 378770180a4dd86f8bda6af2e6db4676e99a5bb1548Jeff Sharkey android_system_dirs.dirs[1].path = build_string2(android_root_dir.path, PRIV_APP_SUBDIR); 37994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood android_system_dirs.dirs[1].len = strlen(android_system_dirs.dirs[1].path); 38094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 38119803807cd7ae01868fcfa50305f4a7dd13765e2Jeff Sharkey android_system_dirs.dirs[2].path = strdup("/vendor/app/"); 382770180a4dd86f8bda6af2e6db4676e99a5bb1548Jeff Sharkey android_system_dirs.dirs[2].len = strlen(android_system_dirs.dirs[2].path); 383770180a4dd86f8bda6af2e6db4676e99a5bb1548Jeff Sharkey 38419803807cd7ae01868fcfa50305f4a7dd13765e2Jeff Sharkey android_system_dirs.dirs[3].path = strdup("/oem/app/"); 385770180a4dd86f8bda6af2e6db4676e99a5bb1548Jeff Sharkey android_system_dirs.dirs[3].len = strlen(android_system_dirs.dirs[3].path); 386770180a4dd86f8bda6af2e6db4676e99a5bb1548Jeff Sharkey 38794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return 0; 38894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 38994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 39094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodint initialize_directories() { 39194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood int res = -1; 39294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 39394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood // Read current filesystem layout version to handle upgrade paths 39494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood char version_path[PATH_MAX]; 39594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood snprintf(version_path, PATH_MAX, "%s.layout_version", android_data_dir.path); 39694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 39794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood int oldVersion; 39894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (fs_read_atomic_int(version_path, &oldVersion) == -1) { 39994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood oldVersion = 0; 40094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 40194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood int version = oldVersion; 40294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 40394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood // /data/user 40494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood char *user_data_dir = build_string2(android_data_dir.path, SECONDARY_USER_PREFIX); 40594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood // /data/data 40694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood char *legacy_data_dir = build_string2(android_data_dir.path, PRIMARY_USER_PREFIX); 40794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood // /data/user/0 40894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood char *primary_data_dir = build_string3(android_data_dir.path, SECONDARY_USER_PREFIX, "0"); 40994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (!user_data_dir || !legacy_data_dir || !primary_data_dir) { 41094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood goto fail; 41194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 41294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 41394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood // Make the /data/user directory if necessary 41494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (access(user_data_dir, R_OK) < 0) { 41594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (mkdir(user_data_dir, 0711) < 0) { 41694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood goto fail; 41794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 41894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (chown(user_data_dir, AID_SYSTEM, AID_SYSTEM) < 0) { 41994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood goto fail; 42094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 42194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (chmod(user_data_dir, 0711) < 0) { 42294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood goto fail; 42394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 42494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 42594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood // Make the /data/user/0 symlink to /data/data if necessary 42694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (access(primary_data_dir, R_OK) < 0) { 42794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (symlink(legacy_data_dir, primary_data_dir)) { 42894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood goto fail; 42994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 43094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 43194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 43294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (version == 0) { 43394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood // Introducing multi-user, so migrate /data/media contents into /data/media/0 43494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGD("Upgrading /data/media for multi-user"); 43594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 43694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood // Ensure /data/media 43794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (fs_prepare_dir(android_media_dir.path, 0770, AID_MEDIA_RW, AID_MEDIA_RW) == -1) { 43894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood goto fail; 43994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 44094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 44194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood // /data/media.tmp 44294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood char media_tmp_dir[PATH_MAX]; 44394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood snprintf(media_tmp_dir, PATH_MAX, "%smedia.tmp", android_data_dir.path); 44494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 44594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood // Only copy when upgrade not already in progress 44694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (access(media_tmp_dir, F_OK) == -1) { 44794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (rename(android_media_dir.path, media_tmp_dir) == -1) { 44894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGE("Failed to move legacy media path: %s", strerror(errno)); 44994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood goto fail; 45094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 45194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 45294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 45394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood // Create /data/media again 45494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (fs_prepare_dir(android_media_dir.path, 0770, AID_MEDIA_RW, AID_MEDIA_RW) == -1) { 45594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood goto fail; 45694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 45794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 45826288202e7bdf2e897a11bf31a15685d7c20945fStephen Smalley if (selinux_android_restorecon(android_media_dir.path, 0)) { 45947a351834f202386b01a27d42ec41ceb1f17b754Stephen Smalley goto fail; 46047a351834f202386b01a27d42ec41ceb1f17b754Stephen Smalley } 46147a351834f202386b01a27d42ec41ceb1f17b754Stephen Smalley 46294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood // /data/media/0 46394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood char owner_media_dir[PATH_MAX]; 46494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood snprintf(owner_media_dir, PATH_MAX, "%s0", android_media_dir.path); 46594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 46694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood // Move any owner data into place 46794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (access(media_tmp_dir, F_OK) == 0) { 46894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (rename(media_tmp_dir, owner_media_dir) == -1) { 46994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGE("Failed to move owner media path: %s", strerror(errno)); 47094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood goto fail; 47194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 47294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 47394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 47494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood // Ensure media directories for any existing users 47594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood DIR *dir; 47694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood struct dirent *dirent; 47794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood char user_media_dir[PATH_MAX]; 47894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 47994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood dir = opendir(user_data_dir); 48094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (dir != NULL) { 48194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood while ((dirent = readdir(dir))) { 48294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (dirent->d_type == DT_DIR) { 48394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood const char *name = dirent->d_name; 48494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 48594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood // skip "." and ".." 48694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (name[0] == '.') { 48794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (name[1] == 0) continue; 48894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if ((name[1] == '.') && (name[2] == 0)) continue; 48994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 49094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 49194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood // /data/media/<user_id> 49294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood snprintf(user_media_dir, PATH_MAX, "%s%s", android_media_dir.path, name); 49394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (fs_prepare_dir(user_media_dir, 0770, AID_MEDIA_RW, AID_MEDIA_RW) == -1) { 49494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood goto fail; 49594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 49694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 49794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 49894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood closedir(dir); 49994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 50094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 50194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood version = 1; 50294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 50394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 50494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood // /data/media/obb 50594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood char media_obb_dir[PATH_MAX]; 50694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood snprintf(media_obb_dir, PATH_MAX, "%sobb", android_media_dir.path); 50794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 50894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (version == 1) { 50994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood // Introducing /data/media/obb for sharing OBB across users; migrate 51094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood // any existing OBB files from owner. 51194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGD("Upgrading to shared /data/media/obb"); 51294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 51394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood // /data/media/0/Android/obb 51494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood char owner_obb_path[PATH_MAX]; 51594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood snprintf(owner_obb_path, PATH_MAX, "%s0/Android/obb", android_media_dir.path); 51694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 51794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood // Only move if target doesn't already exist 51894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (access(media_obb_dir, F_OK) != 0 && access(owner_obb_path, F_OK) == 0) { 51994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (rename(owner_obb_path, media_obb_dir) == -1) { 52094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGE("Failed to move OBB from owner: %s", strerror(errno)); 52194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood goto fail; 52294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 52394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 52494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 52594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood version = 2; 52694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 52794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 52894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (ensure_media_user_dirs(0) == -1) { 52994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGE("Failed to setup media for user 0"); 53094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood goto fail; 53194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 53294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (fs_prepare_dir(media_obb_dir, 0770, AID_MEDIA_RW, AID_MEDIA_RW) == -1) { 53394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood goto fail; 53494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 53594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 53607053fcb61436221fac2281394e98ec9d0feab3dRobin Lee if (ensure_config_user_dirs(0) == -1) { 53707053fcb61436221fac2281394e98ec9d0feab3dRobin Lee ALOGE("Failed to setup misc for user 0"); 53807053fcb61436221fac2281394e98ec9d0feab3dRobin Lee goto fail; 53907053fcb61436221fac2281394e98ec9d0feab3dRobin Lee } 54007053fcb61436221fac2281394e98ec9d0feab3dRobin Lee 541095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee if (version == 2) { 542095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee ALOGD("Upgrading to /data/misc/user directories"); 543095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee 54460fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee char misc_dir[PATH_MAX]; 54560fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee snprintf(misc_dir, PATH_MAX, "%smisc", android_data_dir.path); 54660fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee 54760fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee char keychain_added_dir[PATH_MAX]; 54860fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee snprintf(keychain_added_dir, PATH_MAX, "%s/keychain/cacerts-added", misc_dir); 54960fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee 55060fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee char keychain_removed_dir[PATH_MAX]; 55160fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee snprintf(keychain_removed_dir, PATH_MAX, "%s/keychain/cacerts-removed", misc_dir); 55260fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee 553095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee DIR *dir; 554095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee struct dirent *dirent; 555095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee dir = opendir(user_data_dir); 556095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee if (dir != NULL) { 557095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee while ((dirent = readdir(dir))) { 55860fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee const char *name = dirent->d_name; 559095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee 56060fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee // skip "." and ".." 56160fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee if (name[0] == '.') { 56260fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee if (name[1] == 0) continue; 56360fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee if ((name[1] == '.') && (name[2] == 0)) continue; 56460fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee } 565095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee 56660fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee uint32_t user_id = atoi(name); 56760fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee 56860fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee // /data/misc/user/<user_id> 56960fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee if (ensure_config_user_dirs(user_id) == -1) { 57060fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee goto fail; 57160fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee } 57260fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee 57360fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee char misc_added_dir[PATH_MAX]; 57460fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee snprintf(misc_added_dir, PATH_MAX, "%s/user/%s/cacerts-added", misc_dir, name); 57560fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee 57660fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee char misc_removed_dir[PATH_MAX]; 57760fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee snprintf(misc_removed_dir, PATH_MAX, "%s/user/%s/cacerts-removed", misc_dir, name); 57860fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee 57960fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee uid_t uid = multiuser_get_uid(user_id, AID_SYSTEM); 58060fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee gid_t gid = uid; 58160fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee if (access(keychain_added_dir, F_OK) == 0) { 58260fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee if (copy_dir_files(keychain_added_dir, misc_added_dir, uid, gid) != 0) { 58360fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee ALOGE("Some files failed to copy"); 58460fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee } 58560fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee } 58660fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee if (access(keychain_removed_dir, F_OK) == 0) { 58760fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee if (copy_dir_files(keychain_removed_dir, misc_removed_dir, uid, gid) != 0) { 58860fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee ALOGE("Some files failed to copy"); 589095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee } 590095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee } 591095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee } 592095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee closedir(dir); 59307053fcb61436221fac2281394e98ec9d0feab3dRobin Lee 59460fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee if (access(keychain_added_dir, F_OK) == 0) { 59560fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee delete_dir_contents(keychain_added_dir, 1, 0); 59607053fcb61436221fac2281394e98ec9d0feab3dRobin Lee } 59760fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee if (access(keychain_removed_dir, F_OK) == 0) { 59860fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee delete_dir_contents(keychain_removed_dir, 1, 0); 59907053fcb61436221fac2281394e98ec9d0feab3dRobin Lee } 60007053fcb61436221fac2281394e98ec9d0feab3dRobin Lee } 601095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee 60207053fcb61436221fac2281394e98ec9d0feab3dRobin Lee version = 3; 603095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee } 604095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee 60594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood // Persist layout version if changed 60694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (version != oldVersion) { 60794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (fs_write_atomic_int(version_path, version) == -1) { 60894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGE("Failed to save version to %s: %s", version_path, strerror(errno)); 60994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood goto fail; 61094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 61194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 61294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 61394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood // Success! 61494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood res = 0; 61594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 61694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodfail: 61794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood free(user_data_dir); 61894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood free(legacy_data_dir); 61994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood free(primary_data_dir); 62094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return res; 62194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 62294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 62394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodstatic void drop_privileges() { 62494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (prctl(PR_SET_KEEPCAPS, 1) < 0) { 62594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGE("prctl(PR_SET_KEEPCAPS) failed: %s\n", strerror(errno)); 62694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood exit(1); 62794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 62894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 62994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (setgid(AID_INSTALL) < 0) { 63094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGE("setgid() can't drop privileges; exiting.\n"); 63194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood exit(1); 63294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 63394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 63494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (setuid(AID_INSTALL) < 0) { 63594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGE("setuid() can't drop privileges; exiting.\n"); 63694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood exit(1); 63794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 63894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 63994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood struct __user_cap_header_struct capheader; 64094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood struct __user_cap_data_struct capdata[2]; 64194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood memset(&capheader, 0, sizeof(capheader)); 64294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood memset(&capdata, 0, sizeof(capdata)); 64394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood capheader.version = _LINUX_CAPABILITY_VERSION_3; 64494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood capheader.pid = 0; 64594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 64694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood capdata[CAP_TO_INDEX(CAP_DAC_OVERRIDE)].permitted |= CAP_TO_MASK(CAP_DAC_OVERRIDE); 64794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood capdata[CAP_TO_INDEX(CAP_CHOWN)].permitted |= CAP_TO_MASK(CAP_CHOWN); 64894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood capdata[CAP_TO_INDEX(CAP_SETUID)].permitted |= CAP_TO_MASK(CAP_SETUID); 64994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood capdata[CAP_TO_INDEX(CAP_SETGID)].permitted |= CAP_TO_MASK(CAP_SETGID); 65063568b1430d741f40ca008391c854ef1cc880138Mårten Kongstad capdata[CAP_TO_INDEX(CAP_FOWNER)].permitted |= CAP_TO_MASK(CAP_FOWNER); 65194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 65294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood capdata[0].effective = capdata[0].permitted; 65394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood capdata[1].effective = capdata[1].permitted; 65494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood capdata[0].inheritable = 0; 65594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood capdata[1].inheritable = 0; 65694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 65794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (capset(&capheader, &capdata[0]) < 0) { 65894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGE("capset failed: %s\n", strerror(errno)); 65994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood exit(1); 66094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 66194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 66294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 6637abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalleystatic int log_callback(int type, const char *fmt, ...) { 6647abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalley va_list ap; 6657abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalley int priority; 6667abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalley 6677abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalley switch (type) { 6687abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalley case SELINUX_WARNING: 6697abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalley priority = ANDROID_LOG_WARN; 6707abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalley break; 6717abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalley case SELINUX_INFO: 6727abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalley priority = ANDROID_LOG_INFO; 6737abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalley break; 6747abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalley default: 6757abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalley priority = ANDROID_LOG_ERROR; 6767abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalley break; 6777abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalley } 6787abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalley va_start(ap, fmt); 6797abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalley LOG_PRI_VA(priority, "SELinux", fmt, ap); 6807abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalley va_end(ap); 6817abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalley return 0; 6827abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalley} 6837abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalley 68499d9fb15b4a1eb534261ae81b2cf25aec4447bd0Chih-Hung Hsiehint main(const int argc __unused, const char *argv[] __unused) { 68594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood char buf[BUFFER_MAX]; 68694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood struct sockaddr addr; 68794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood socklen_t alen; 68899d9fb15b4a1eb534261ae81b2cf25aec4447bd0Chih-Hung Hsieh int lsocket, s; 689bd558d61871218f5b13df2fe4b7cc3b530ee947cStephen Smalley int selinux_enabled = (is_selinux_enabled() > 0); 69094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 69194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGI("installd firing up\n"); 69294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 6937abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalley union selinux_callback cb; 6947abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalley cb.func_log = log_callback; 6957abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalley selinux_set_callback(SELINUX_CB_LOG, cb); 6967abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalley 69794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (initialize_globals() < 0) { 69894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGE("Could not initialize globals; exiting.\n"); 69994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood exit(1); 70094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 70194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 70294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (initialize_directories() < 0) { 70394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGE("Could not create directories; exiting.\n"); 70494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood exit(1); 70594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 70694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 707bd558d61871218f5b13df2fe4b7cc3b530ee947cStephen Smalley if (selinux_enabled && selinux_status_open(true) < 0) { 708bd558d61871218f5b13df2fe4b7cc3b530ee947cStephen Smalley ALOGE("Could not open selinux status; exiting.\n"); 709bd558d61871218f5b13df2fe4b7cc3b530ee947cStephen Smalley exit(1); 710bd558d61871218f5b13df2fe4b7cc3b530ee947cStephen Smalley } 711bd558d61871218f5b13df2fe4b7cc3b530ee947cStephen Smalley 71294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood drop_privileges(); 71394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 71494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood lsocket = android_get_control_socket(SOCKET_PATH); 71594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (lsocket < 0) { 71694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGE("Failed to get socket from environment: %s\n", strerror(errno)); 71794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood exit(1); 71894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 71994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (listen(lsocket, 5)) { 72094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGE("Listen on socket failed: %s\n", strerror(errno)); 72194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood exit(1); 72294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 72394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood fcntl(lsocket, F_SETFD, FD_CLOEXEC); 72494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 72594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood for (;;) { 72694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood alen = sizeof(addr); 72794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood s = accept(lsocket, &addr, &alen); 72894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (s < 0) { 72994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGE("Accept failed: %s\n", strerror(errno)); 73094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood continue; 73194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 73294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood fcntl(s, F_SETFD, FD_CLOEXEC); 73394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 73494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGI("new connection\n"); 73594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood for (;;) { 73694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood unsigned short count; 73794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (readx(s, &count, sizeof(count))) { 73894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGE("failed to read size\n"); 73994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood break; 74094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 74194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if ((count < 1) || (count >= BUFFER_MAX)) { 74294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGE("invalid size %d\n", count); 74394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood break; 74494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 74594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (readx(s, buf, count)) { 74694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGE("failed to read command\n"); 74794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood break; 74894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 74994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood buf[count] = 0; 750bd558d61871218f5b13df2fe4b7cc3b530ee947cStephen Smalley if (selinux_enabled && selinux_status_updated() > 0) { 751bd558d61871218f5b13df2fe4b7cc3b530ee947cStephen Smalley selinux_android_seapp_context_reload(); 752bd558d61871218f5b13df2fe4b7cc3b530ee947cStephen Smalley } 75394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (execute(s, buf)) break; 75494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 75594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGI("closing connection\n"); 75694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood close(s); 75794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 75894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 75994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return 0; 76094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 761