194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood/*
294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood** Copyright 2008, The Android Open Source Project
394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood**
494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood** Licensed under the Apache License, Version 2.0 (the "License");
594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood** you may not use this file except in compliance with the License.
694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood** You may obtain a copy of the License at
794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood**
894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood**     http://www.apache.org/licenses/LICENSE-2.0
994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood**
1094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood** Unless required by applicable law or agreed to in writing, software
1194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood** distributed under the License is distributed on an "AS IS" BASIS,
1294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood** 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 */
2694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood#define TOKEN_MAX     8     /* max number of arguments in buffer */
2794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood#define REPLY_MAX     256   /* largest reply allowed */
2894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
2994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodstatic int do_ping(char **arg, char reply[REPLY_MAX])
3094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
3194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return 0;
3294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
3394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
3494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodstatic int do_install(char **arg, char reply[REPLY_MAX])
3594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
364d3fd4e9988c0eb284dd5104c4dea757f723c716Robert Craig    return install(arg[0], atoi(arg[1]), atoi(arg[2]), arg[3]); /* pkgname, uid, gid, seinfo */
3794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
3894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
3994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodstatic int do_dexopt(char **arg, char reply[REPLY_MAX])
4094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
41b1efac103523efccbe671e76cc0eaaeab810415bCalin Juravle    /* apk_path, uid, is_public, pkgname, instruction_set, vm_safe_mode, should_relocate */
42b1efac103523efccbe671e76cc0eaaeab810415bCalin Juravle    return dexopt(arg[0], atoi(arg[1]), atoi(arg[2]), arg[3], arg[4], atoi(arg[5]), 0);
4394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
4494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
45b1a6c2f95a455aa2829340fcc8d233493855f4d4Narayan Kamathstatic int do_mark_boot_complete(char **arg, char reply[REPLY_MAX])
46b1a6c2f95a455aa2829340fcc8d233493855f4d4Narayan Kamath{
47b1a6c2f95a455aa2829340fcc8d233493855f4d4Narayan Kamath    return mark_boot_complete(arg[0] /* instruction set */);
48b1a6c2f95a455aa2829340fcc8d233493855f4d4Narayan Kamath}
49b1a6c2f95a455aa2829340fcc8d233493855f4d4Narayan Kamath
5094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodstatic int do_move_dex(char **arg, char reply[REPLY_MAX])
5194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
521b4003207750ea8fe8c7b03eb32d80f1df83979eNarayan Kamath    return move_dex(arg[0], arg[1], arg[2]); /* src, dst, instruction_set */
5394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
5494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
5594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodstatic int do_rm_dex(char **arg, char reply[REPLY_MAX])
5694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
571b4003207750ea8fe8c7b03eb32d80f1df83979eNarayan Kamath    return rm_dex(arg[0], arg[1]); /* pkgname, instruction_set */
5894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
5994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
6094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodstatic int do_remove(char **arg, char reply[REPLY_MAX])
6194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
6294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return uninstall(arg[0], atoi(arg[1])); /* pkgname, userid */
6394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
6494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
6594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodstatic int do_rename(char **arg, char reply[REPLY_MAX])
6694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
6794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return renamepkg(arg[0], arg[1]); /* oldpkgname, newpkgname */
6894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
6994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
7094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodstatic int do_fixuid(char **arg, char reply[REPLY_MAX])
7194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
7294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return fix_uid(arg[0], atoi(arg[1]), atoi(arg[2])); /* pkgname, uid, gid */
7394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
7494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
7594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodstatic int do_free_cache(char **arg, char reply[REPLY_MAX]) /* TODO int:free_size */
7694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
7794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return free_cache((int64_t)atoll(arg[0])); /* free_size */
7894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
7994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
8094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodstatic int do_rm_cache(char **arg, char reply[REPLY_MAX])
8194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
8294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return delete_cache(arg[0], atoi(arg[1])); /* pkgname, userid */
8394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
8494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
85c796b681e52fbb792da9a5b4f30e935cc927c1d7Jeff Sharkeystatic int do_rm_code_cache(char **arg, char reply[REPLY_MAX])
86c796b681e52fbb792da9a5b4f30e935cc927c1d7Jeff Sharkey{
87c796b681e52fbb792da9a5b4f30e935cc927c1d7Jeff Sharkey    return delete_code_cache(arg[0], atoi(arg[1])); /* pkgname, userid */
88c796b681e52fbb792da9a5b4f30e935cc927c1d7Jeff Sharkey}
89c796b681e52fbb792da9a5b4f30e935cc927c1d7Jeff Sharkey
9094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodstatic int do_get_size(char **arg, char reply[REPLY_MAX])
9194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
9294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    int64_t codesize = 0;
9394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    int64_t datasize = 0;
9494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    int64_t cachesize = 0;
9594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    int64_t asecsize = 0;
9694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    int res = 0;
9794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
98abe4fe5b46157ecd2a52d28abf938c816c3ce878Jeff Sharkey        /* pkgdir, userid, apkpath */
998b41780d73930b37b6254cc1dac5607c843839c0Dianne Hackborn    res = get_size(arg[0], atoi(arg[1]), arg[2], arg[3], arg[4], arg[5],
1001b4003207750ea8fe8c7b03eb32d80f1df83979eNarayan Kamath            arg[6], &codesize, &datasize, &cachesize, &asecsize);
10194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
10294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    /*
10394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood     * Each int64_t can take up 22 characters printed out. Make sure it
10494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood     * doesn't go over REPLY_MAX in the future.
10594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood     */
10694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    snprintf(reply, REPLY_MAX, "%" PRId64 " %" PRId64 " %" PRId64 " %" PRId64,
10794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            codesize, datasize, cachesize, asecsize);
10894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return res;
10994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
11094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
11194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodstatic int do_rm_user_data(char **arg, char reply[REPLY_MAX])
11294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
11394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return delete_user_data(arg[0], atoi(arg[1])); /* pkgname, userid */
11494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
11594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
11694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodstatic int do_mk_user_data(char **arg, char reply[REPLY_MAX])
11794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
118880d1a957ebcb63fb9d3724e2f91c58b7ff0cd54Robert Craig    return make_user_data(arg[0], atoi(arg[1]), atoi(arg[2]), arg[3]);
119880d1a957ebcb63fb9d3724e2f91c58b7ff0cd54Robert Craig                             /* pkgname, uid, userid, seinfo */
12094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
12194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
1227c8bec01790087748ec7afa69a31789828b751f9Robin Leestatic int do_mk_user_config(char **arg, char reply[REPLY_MAX])
123095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee{
1247c8bec01790087748ec7afa69a31789828b751f9Robin Lee    return make_user_config(atoi(arg[0])); /* userid */
125095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee}
126095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee
12794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodstatic int do_rm_user(char **arg, char reply[REPLY_MAX])
12894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
129abe4fe5b46157ecd2a52d28abf938c816c3ce878Jeff Sharkey    return delete_user(atoi(arg[0])); /* userid */
13094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
13194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
13294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodstatic int do_movefiles(char **arg, char reply[REPLY_MAX])
13394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
13494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return movefiles();
13594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
13694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
13794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodstatic int do_linklib(char **arg, char reply[REPLY_MAX])
13894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
13994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return linklib(arg[0], arg[1], atoi(arg[2]));
14094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
14194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
14263568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstadstatic int do_idmap(char **arg, char reply[REPLY_MAX])
14363568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad{
14463568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad    return idmap(arg[0], arg[1], atoi(arg[2]));
14563568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad}
14663568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad
147da30dc7336f03ca629fe173db1425fdce989119cRobert Craigstatic int do_restorecon_data(char **arg, char reply[REPLY_MAX] __attribute__((unused)))
148e9887e46cea4a095e4219927eadbe4c57bb1a5eeRobert Craig{
149da30dc7336f03ca629fe173db1425fdce989119cRobert Craig    return restorecon_data(arg[0], arg[1], atoi(arg[2]));
150da30dc7336f03ca629fe173db1425fdce989119cRobert Craig                             /* pkgName, seinfo, uid*/
151e9887e46cea4a095e4219927eadbe4c57bb1a5eeRobert Craig}
152e9887e46cea4a095e4219927eadbe4c57bb1a5eeRobert Craig
15343c5d30795faf08ab639b8d88c2eceaf2b648c93Alex Lightstatic int do_patchoat(char **arg, char reply[REPLY_MAX]) {
154b1efac103523efccbe671e76cc0eaaeab810415bCalin Juravle    /* apk_path, uid, is_public, pkgname, instruction_set, vm_safe_mode, should_relocate */
155b1efac103523efccbe671e76cc0eaaeab810415bCalin Juravle    return dexopt(arg[0], atoi(arg[1]), atoi(arg[2]), arg[3], arg[4], 0, 1);
15643c5d30795faf08ab639b8d88c2eceaf2b648c93Alex Light}
15743c5d30795faf08ab639b8d88c2eceaf2b648c93Alex Light
15894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodstruct cmdinfo {
15994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    const char *name;
16094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    unsigned numargs;
16194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    int (*func)(char **arg, char reply[REPLY_MAX]);
16294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood};
16394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
16494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodstruct cmdinfo cmds[] = {
16594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    { "ping",                 0, do_ping },
1664d3fd4e9988c0eb284dd5104c4dea757f723c716Robert Craig    { "install",              4, do_install },
167b1efac103523efccbe671e76cc0eaaeab810415bCalin Juravle    { "dexopt",               6, do_dexopt },
168b1a6c2f95a455aa2829340fcc8d233493855f4d4Narayan Kamath    { "markbootcomplete",     1, do_mark_boot_complete },
1691b4003207750ea8fe8c7b03eb32d80f1df83979eNarayan Kamath    { "movedex",              3, do_move_dex },
1701b4003207750ea8fe8c7b03eb32d80f1df83979eNarayan Kamath    { "rmdex",                2, do_rm_dex },
17194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    { "remove",               2, do_remove },
17294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    { "rename",               2, do_rename },
17394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    { "fixuid",               3, do_fixuid },
17494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    { "freecache",            1, do_free_cache },
17594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    { "rmcache",              2, do_rm_cache },
176c796b681e52fbb792da9a5b4f30e935cc927c1d7Jeff Sharkey    { "rmcodecache",          2, do_rm_code_cache },
1771b4003207750ea8fe8c7b03eb32d80f1df83979eNarayan Kamath    { "getsize",              7, do_get_size },
17894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    { "rmuserdata",           2, do_rm_user_data },
17994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    { "movefiles",            0, do_movefiles },
18094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    { "linklib",              3, do_linklib },
181880d1a957ebcb63fb9d3724e2f91c58b7ff0cd54Robert Craig    { "mkuserdata",           4, do_mk_user_data },
1827c8bec01790087748ec7afa69a31789828b751f9Robin Lee    { "mkuserconfig",         1, do_mk_user_config },
18394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    { "rmuser",               1, do_rm_user },
18463568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad    { "idmap",                3, do_idmap },
185da30dc7336f03ca629fe173db1425fdce989119cRobert Craig    { "restorecondata",       3, do_restorecon_data },
18643c5d30795faf08ab639b8d88c2eceaf2b648c93Alex Light    { "patchoat",             5, do_patchoat },
18794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood};
18894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
18994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodstatic int readx(int s, void *_buf, int count)
19094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
19194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    char *buf = _buf;
19294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    int n = 0, r;
19394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (count < 0) return -1;
19494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    while (n < count) {
19594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        r = read(s, buf + n, count - n);
19694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (r < 0) {
19794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (errno == EINTR) continue;
19894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            ALOGE("read error: %s\n", strerror(errno));
19994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            return -1;
20094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
20194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (r == 0) {
20294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            ALOGE("eof\n");
20394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            return -1; /* EOF */
20494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
20594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        n += r;
20694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
20794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return 0;
20894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
20994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
21094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodstatic int writex(int s, const void *_buf, int count)
21194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
21294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    const char *buf = _buf;
21394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    int n = 0, r;
21494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (count < 0) return -1;
21594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    while (n < count) {
21694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        r = write(s, buf + n, count - n);
21794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (r < 0) {
21894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (errno == EINTR) continue;
21994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            ALOGE("write error: %s\n", strerror(errno));
22094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            return -1;
22194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
22294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        n += r;
22394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
22494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return 0;
22594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
22694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
22794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
22894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood/* Tokenize the command buffer, locate a matching command,
22994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * ensure that the required number of arguments are provided,
23094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * call the function(), return the result.
23194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood */
23294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodstatic int execute(int s, char cmd[BUFFER_MAX])
23394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
23494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    char reply[REPLY_MAX];
23594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    char *arg[TOKEN_MAX+1];
23694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    unsigned i;
23794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    unsigned n = 0;
23894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    unsigned short count;
23994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    int ret = -1;
24094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
2411705fc44fb85c4232637f9f7189c3bdca98a63d5Brian Carlstrom    // ALOGI("execute('%s')\n", cmd);
24294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
24394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        /* default reply is "" */
24494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    reply[0] = 0;
24594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
24694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        /* n is number of args (not counting arg[0]) */
24794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    arg[0] = cmd;
24894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    while (*cmd) {
24994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (isspace(*cmd)) {
25094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            *cmd++ = 0;
25194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            n++;
25294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            arg[n] = cmd;
25394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (n == TOKEN_MAX) {
25494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                ALOGE("too many arguments\n");
25594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                goto done;
25694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
25794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
25894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        cmd++;
25994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
26094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
26194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    for (i = 0; i < sizeof(cmds) / sizeof(cmds[0]); i++) {
26294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (!strcmp(cmds[i].name,arg[0])) {
26394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (n != cmds[i].numargs) {
26494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                ALOGE("%s requires %d arguments (%d given)\n",
26594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                     cmds[i].name, cmds[i].numargs, n);
26694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            } else {
26794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                ret = cmds[i].func(arg + 1, reply);
26894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
26994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            goto done;
27094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
27194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
27294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    ALOGE("unsupported command '%s'\n", arg[0]);
27394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
27494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwooddone:
27594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (reply[0]) {
27694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        n = snprintf(cmd, BUFFER_MAX, "%d %s", ret, reply);
27794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    } else {
27894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        n = snprintf(cmd, BUFFER_MAX, "%d", ret);
27994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
28094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (n > BUFFER_MAX) n = BUFFER_MAX;
28194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    count = n;
28294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
2831705fc44fb85c4232637f9f7189c3bdca98a63d5Brian Carlstrom    // ALOGI("reply: '%s'\n", cmd);
28494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (writex(s, &count, sizeof(count))) return -1;
28594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (writex(s, cmd, count)) return -1;
28694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return 0;
28794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
28894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
28994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood/**
29094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * Initialize all the global variables that are used elsewhere. Returns 0 upon
29194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * success and -1 on error.
29294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood */
29394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodvoid free_globals() {
29494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    size_t i;
29594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
29694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    for (i = 0; i < android_system_dirs.count; i++) {
29794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (android_system_dirs.dirs[i].path != NULL) {
29894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            free(android_system_dirs.dirs[i].path);
29994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
30094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
30194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
30294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    free(android_system_dirs.dirs);
30394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
30494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
30594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodint initialize_globals() {
30694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    // Get the android data directory.
30794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (get_path_from_env(&android_data_dir, "ANDROID_DATA") < 0) {
30894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        return -1;
30994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
31094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
31194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    // Get the android app directory.
31294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (copy_and_append(&android_app_dir, &android_data_dir, APP_SUBDIR) < 0) {
31394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        return -1;
31494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
31594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
31694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    // Get the android protected app directory.
31794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (copy_and_append(&android_app_private_dir, &android_data_dir, PRIVATE_APP_SUBDIR) < 0) {
31894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        return -1;
31994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
32094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
32194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    // Get the android app native library directory.
32294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (copy_and_append(&android_app_lib_dir, &android_data_dir, APP_LIB_SUBDIR) < 0) {
32394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        return -1;
32494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
32594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
32694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    // Get the sd-card ASEC mount point.
32794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (get_path_from_env(&android_asec_dir, "ASEC_MOUNTPOINT") < 0) {
32894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        return -1;
32994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
33094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
33194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    // Get the android media directory.
33294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (copy_and_append(&android_media_dir, &android_data_dir, MEDIA_SUBDIR) < 0) {
33394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        return -1;
33494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
33594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
33694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    // Take note of the system and vendor directories.
337770180a4dd86f8bda6af2e6db4676e99a5bb1548Jeff Sharkey    android_system_dirs.count = 4;
33894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
33994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    android_system_dirs.dirs = calloc(android_system_dirs.count, sizeof(dir_rec_t));
34094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (android_system_dirs.dirs == NULL) {
34194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        ALOGE("Couldn't allocate array for dirs; aborting\n");
34294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        return -1;
34394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
34494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
345770180a4dd86f8bda6af2e6db4676e99a5bb1548Jeff Sharkey    dir_rec_t android_root_dir;
346770180a4dd86f8bda6af2e6db4676e99a5bb1548Jeff Sharkey    if (get_path_from_env(&android_root_dir, "ANDROID_ROOT") < 0) {
347770180a4dd86f8bda6af2e6db4676e99a5bb1548Jeff Sharkey        ALOGE("Missing ANDROID_ROOT; aborting\n");
34894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        return -1;
34994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
35094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
351770180a4dd86f8bda6af2e6db4676e99a5bb1548Jeff Sharkey    android_system_dirs.dirs[0].path = build_string2(android_root_dir.path, APP_SUBDIR);
352770180a4dd86f8bda6af2e6db4676e99a5bb1548Jeff Sharkey    android_system_dirs.dirs[0].len = strlen(android_system_dirs.dirs[0].path);
35394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
354770180a4dd86f8bda6af2e6db4676e99a5bb1548Jeff Sharkey    android_system_dirs.dirs[1].path = build_string2(android_root_dir.path, PRIV_APP_SUBDIR);
35594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    android_system_dirs.dirs[1].len = strlen(android_system_dirs.dirs[1].path);
35694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
357770180a4dd86f8bda6af2e6db4676e99a5bb1548Jeff Sharkey    android_system_dirs.dirs[2].path = "/vendor/app/";
358770180a4dd86f8bda6af2e6db4676e99a5bb1548Jeff Sharkey    android_system_dirs.dirs[2].len = strlen(android_system_dirs.dirs[2].path);
359770180a4dd86f8bda6af2e6db4676e99a5bb1548Jeff Sharkey
360770180a4dd86f8bda6af2e6db4676e99a5bb1548Jeff Sharkey    android_system_dirs.dirs[3].path = "/oem/app/";
361770180a4dd86f8bda6af2e6db4676e99a5bb1548Jeff Sharkey    android_system_dirs.dirs[3].len = strlen(android_system_dirs.dirs[3].path);
362770180a4dd86f8bda6af2e6db4676e99a5bb1548Jeff Sharkey
36394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return 0;
36494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
36594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
36694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodint initialize_directories() {
36794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    int res = -1;
36894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
36994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    // Read current filesystem layout version to handle upgrade paths
37094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    char version_path[PATH_MAX];
37194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    snprintf(version_path, PATH_MAX, "%s.layout_version", android_data_dir.path);
37294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
37394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    int oldVersion;
37494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (fs_read_atomic_int(version_path, &oldVersion) == -1) {
37594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        oldVersion = 0;
37694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
37794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    int version = oldVersion;
37894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
37994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    // /data/user
38094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    char *user_data_dir = build_string2(android_data_dir.path, SECONDARY_USER_PREFIX);
38194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    // /data/data
38294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    char *legacy_data_dir = build_string2(android_data_dir.path, PRIMARY_USER_PREFIX);
38394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    // /data/user/0
38494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    char *primary_data_dir = build_string3(android_data_dir.path, SECONDARY_USER_PREFIX, "0");
38594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (!user_data_dir || !legacy_data_dir || !primary_data_dir) {
38694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        goto fail;
38794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
38894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
38994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    // Make the /data/user directory if necessary
39094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (access(user_data_dir, R_OK) < 0) {
39194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (mkdir(user_data_dir, 0711) < 0) {
39294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            goto fail;
39394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
39494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (chown(user_data_dir, AID_SYSTEM, AID_SYSTEM) < 0) {
39594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            goto fail;
39694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
39794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (chmod(user_data_dir, 0711) < 0) {
39894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            goto fail;
39994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
40094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
40194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    // Make the /data/user/0 symlink to /data/data if necessary
40294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (access(primary_data_dir, R_OK) < 0) {
40394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (symlink(legacy_data_dir, primary_data_dir)) {
40494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            goto fail;
40594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
40694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
40794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
40894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (version == 0) {
40994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        // Introducing multi-user, so migrate /data/media contents into /data/media/0
41094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        ALOGD("Upgrading /data/media for multi-user");
41194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
41294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        // Ensure /data/media
41394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (fs_prepare_dir(android_media_dir.path, 0770, AID_MEDIA_RW, AID_MEDIA_RW) == -1) {
41494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            goto fail;
41594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
41694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
41794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        // /data/media.tmp
41894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        char media_tmp_dir[PATH_MAX];
41994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        snprintf(media_tmp_dir, PATH_MAX, "%smedia.tmp", android_data_dir.path);
42094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
42194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        // Only copy when upgrade not already in progress
42294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (access(media_tmp_dir, F_OK) == -1) {
42394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (rename(android_media_dir.path, media_tmp_dir) == -1) {
42494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                ALOGE("Failed to move legacy media path: %s", strerror(errno));
42594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                goto fail;
42694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
42794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
42894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
42994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        // Create /data/media again
43094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (fs_prepare_dir(android_media_dir.path, 0770, AID_MEDIA_RW, AID_MEDIA_RW) == -1) {
43194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            goto fail;
43294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
43394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
43426288202e7bdf2e897a11bf31a15685d7c20945fStephen Smalley        if (selinux_android_restorecon(android_media_dir.path, 0)) {
43547a351834f202386b01a27d42ec41ceb1f17b754Stephen Smalley            goto fail;
43647a351834f202386b01a27d42ec41ceb1f17b754Stephen Smalley        }
43747a351834f202386b01a27d42ec41ceb1f17b754Stephen Smalley
43894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        // /data/media/0
43994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        char owner_media_dir[PATH_MAX];
44094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        snprintf(owner_media_dir, PATH_MAX, "%s0", android_media_dir.path);
44194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
44294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        // Move any owner data into place
44394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (access(media_tmp_dir, F_OK) == 0) {
44494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (rename(media_tmp_dir, owner_media_dir) == -1) {
44594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                ALOGE("Failed to move owner media path: %s", strerror(errno));
44694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                goto fail;
44794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
44894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
44994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
45094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        // Ensure media directories for any existing users
45194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        DIR *dir;
45294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        struct dirent *dirent;
45394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        char user_media_dir[PATH_MAX];
45494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
45594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        dir = opendir(user_data_dir);
45694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (dir != NULL) {
45794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            while ((dirent = readdir(dir))) {
45894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                if (dirent->d_type == DT_DIR) {
45994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                    const char *name = dirent->d_name;
46094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
46194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                    // skip "." and ".."
46294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                    if (name[0] == '.') {
46394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                        if (name[1] == 0) continue;
46494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                        if ((name[1] == '.') && (name[2] == 0)) continue;
46594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                    }
46694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
46794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                    // /data/media/<user_id>
46894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                    snprintf(user_media_dir, PATH_MAX, "%s%s", android_media_dir.path, name);
46994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                    if (fs_prepare_dir(user_media_dir, 0770, AID_MEDIA_RW, AID_MEDIA_RW) == -1) {
47094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                        goto fail;
47194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                    }
47294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                }
47394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
47494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            closedir(dir);
47594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
47694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
47794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        version = 1;
47894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
47994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
48094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    // /data/media/obb
48194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    char media_obb_dir[PATH_MAX];
48294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    snprintf(media_obb_dir, PATH_MAX, "%sobb", android_media_dir.path);
48394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
48494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (version == 1) {
48594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        // Introducing /data/media/obb for sharing OBB across users; migrate
48694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        // any existing OBB files from owner.
48794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        ALOGD("Upgrading to shared /data/media/obb");
48894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
48994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        // /data/media/0/Android/obb
49094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        char owner_obb_path[PATH_MAX];
49194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        snprintf(owner_obb_path, PATH_MAX, "%s0/Android/obb", android_media_dir.path);
49294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
49394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        // Only move if target doesn't already exist
49494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (access(media_obb_dir, F_OK) != 0 && access(owner_obb_path, F_OK) == 0) {
49594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (rename(owner_obb_path, media_obb_dir) == -1) {
49694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                ALOGE("Failed to move OBB from owner: %s", strerror(errno));
49794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                goto fail;
49894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
49994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
50094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
50194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        version = 2;
50294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
50394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
50494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (ensure_media_user_dirs(0) == -1) {
50594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        ALOGE("Failed to setup media for user 0");
50694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        goto fail;
50794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
50894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (fs_prepare_dir(media_obb_dir, 0770, AID_MEDIA_RW, AID_MEDIA_RW) == -1) {
50994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        goto fail;
51094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
51194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
51207053fcb61436221fac2281394e98ec9d0feab3dRobin Lee    if (ensure_config_user_dirs(0) == -1) {
51307053fcb61436221fac2281394e98ec9d0feab3dRobin Lee        ALOGE("Failed to setup misc for user 0");
51407053fcb61436221fac2281394e98ec9d0feab3dRobin Lee        goto fail;
51507053fcb61436221fac2281394e98ec9d0feab3dRobin Lee    }
51607053fcb61436221fac2281394e98ec9d0feab3dRobin Lee
517095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee    if (version == 2) {
518095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee        ALOGD("Upgrading to /data/misc/user directories");
519095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee
52060fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        char misc_dir[PATH_MAX];
52160fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        snprintf(misc_dir, PATH_MAX, "%smisc", android_data_dir.path);
52260fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee
52360fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        char keychain_added_dir[PATH_MAX];
52460fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        snprintf(keychain_added_dir, PATH_MAX, "%s/keychain/cacerts-added", misc_dir);
52560fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee
52660fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        char keychain_removed_dir[PATH_MAX];
52760fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        snprintf(keychain_removed_dir, PATH_MAX, "%s/keychain/cacerts-removed", misc_dir);
52860fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee
529095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee        DIR *dir;
530095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee        struct dirent *dirent;
531095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee        dir = opendir(user_data_dir);
532095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee        if (dir != NULL) {
533095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee            while ((dirent = readdir(dir))) {
53460fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee                const char *name = dirent->d_name;
535095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee
53660fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee                // skip "." and ".."
53760fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee                if (name[0] == '.') {
53860fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee                    if (name[1] == 0) continue;
53960fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee                    if ((name[1] == '.') && (name[2] == 0)) continue;
54060fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee                }
541095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee
54260fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee                uint32_t user_id = atoi(name);
54360fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee
54460fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee                // /data/misc/user/<user_id>
54560fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee                if (ensure_config_user_dirs(user_id) == -1) {
54660fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee                    goto fail;
54760fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee                }
54860fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee
54960fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee                char misc_added_dir[PATH_MAX];
55060fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee                snprintf(misc_added_dir, PATH_MAX, "%s/user/%s/cacerts-added", misc_dir, name);
55160fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee
55260fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee                char misc_removed_dir[PATH_MAX];
55360fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee                snprintf(misc_removed_dir, PATH_MAX, "%s/user/%s/cacerts-removed", misc_dir, name);
55460fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee
55560fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee                uid_t uid = multiuser_get_uid(user_id, AID_SYSTEM);
55660fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee                gid_t gid = uid;
55760fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee                if (access(keychain_added_dir, F_OK) == 0) {
55860fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee                    if (copy_dir_files(keychain_added_dir, misc_added_dir, uid, gid) != 0) {
55960fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee                        ALOGE("Some files failed to copy");
56060fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee                    }
56160fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee                }
56260fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee                if (access(keychain_removed_dir, F_OK) == 0) {
56360fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee                    if (copy_dir_files(keychain_removed_dir, misc_removed_dir, uid, gid) != 0) {
56460fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee                        ALOGE("Some files failed to copy");
565095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee                    }
566095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee                }
567095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee            }
568095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee            closedir(dir);
56907053fcb61436221fac2281394e98ec9d0feab3dRobin Lee
57060fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee            if (access(keychain_added_dir, F_OK) == 0) {
57160fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee                delete_dir_contents(keychain_added_dir, 1, 0);
57207053fcb61436221fac2281394e98ec9d0feab3dRobin Lee            }
57360fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee            if (access(keychain_removed_dir, F_OK) == 0) {
57460fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee                delete_dir_contents(keychain_removed_dir, 1, 0);
57507053fcb61436221fac2281394e98ec9d0feab3dRobin Lee            }
57607053fcb61436221fac2281394e98ec9d0feab3dRobin Lee        }
577095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee
57807053fcb61436221fac2281394e98ec9d0feab3dRobin Lee        version = 3;
579095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee    }
580095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee
58194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    // Persist layout version if changed
58294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (version != oldVersion) {
58394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (fs_write_atomic_int(version_path, version) == -1) {
58494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            ALOGE("Failed to save version to %s: %s", version_path, strerror(errno));
58594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            goto fail;
58694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
58794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
58894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
58994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    // Success!
59094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    res = 0;
59194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
59294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodfail:
59394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    free(user_data_dir);
59494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    free(legacy_data_dir);
59594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    free(primary_data_dir);
59694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return res;
59794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
59894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
59994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodstatic void drop_privileges() {
60094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (prctl(PR_SET_KEEPCAPS, 1) < 0) {
60194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        ALOGE("prctl(PR_SET_KEEPCAPS) failed: %s\n", strerror(errno));
60294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        exit(1);
60394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
60494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
60594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (setgid(AID_INSTALL) < 0) {
60694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        ALOGE("setgid() can't drop privileges; exiting.\n");
60794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        exit(1);
60894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
60994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
61094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (setuid(AID_INSTALL) < 0) {
61194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        ALOGE("setuid() can't drop privileges; exiting.\n");
61294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        exit(1);
61394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
61494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
61594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    struct __user_cap_header_struct capheader;
61694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    struct __user_cap_data_struct capdata[2];
61794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    memset(&capheader, 0, sizeof(capheader));
61894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    memset(&capdata, 0, sizeof(capdata));
61994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    capheader.version = _LINUX_CAPABILITY_VERSION_3;
62094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    capheader.pid = 0;
62194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
62294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    capdata[CAP_TO_INDEX(CAP_DAC_OVERRIDE)].permitted |= CAP_TO_MASK(CAP_DAC_OVERRIDE);
62394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    capdata[CAP_TO_INDEX(CAP_CHOWN)].permitted        |= CAP_TO_MASK(CAP_CHOWN);
62494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    capdata[CAP_TO_INDEX(CAP_SETUID)].permitted       |= CAP_TO_MASK(CAP_SETUID);
62594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    capdata[CAP_TO_INDEX(CAP_SETGID)].permitted       |= CAP_TO_MASK(CAP_SETGID);
62663568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad    capdata[CAP_TO_INDEX(CAP_FOWNER)].permitted       |= CAP_TO_MASK(CAP_FOWNER);
62794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
62894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    capdata[0].effective = capdata[0].permitted;
62994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    capdata[1].effective = capdata[1].permitted;
63094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    capdata[0].inheritable = 0;
63194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    capdata[1].inheritable = 0;
63294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
63394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (capset(&capheader, &capdata[0]) < 0) {
63494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        ALOGE("capset failed: %s\n", strerror(errno));
63594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        exit(1);
63694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
63794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
63894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
6397abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalleystatic int log_callback(int type, const char *fmt, ...) {
6407abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalley    va_list ap;
6417abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalley    int priority;
6427abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalley
6437abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalley    switch (type) {
6447abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalley    case SELINUX_WARNING:
6457abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalley        priority = ANDROID_LOG_WARN;
6467abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalley        break;
6477abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalley    case SELINUX_INFO:
6487abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalley        priority = ANDROID_LOG_INFO;
6497abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalley        break;
6507abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalley    default:
6517abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalley        priority = ANDROID_LOG_ERROR;
6527abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalley        break;
6537abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalley    }
6547abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalley    va_start(ap, fmt);
6557abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalley    LOG_PRI_VA(priority, "SELinux", fmt, ap);
6567abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalley    va_end(ap);
6577abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalley    return 0;
6587abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalley}
6597abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalley
66094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodint main(const int argc, const char *argv[]) {
66194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    char buf[BUFFER_MAX];
66294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    struct sockaddr addr;
66394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    socklen_t alen;
66494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    int lsocket, s, count;
665bd558d61871218f5b13df2fe4b7cc3b530ee947cStephen Smalley    int selinux_enabled = (is_selinux_enabled() > 0);
66694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
66794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    ALOGI("installd firing up\n");
66894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
6697abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalley    union selinux_callback cb;
6707abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalley    cb.func_log = log_callback;
6717abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalley    selinux_set_callback(SELINUX_CB_LOG, cb);
6727abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalley
67394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (initialize_globals() < 0) {
67494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        ALOGE("Could not initialize globals; exiting.\n");
67594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        exit(1);
67694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
67794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
67894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (initialize_directories() < 0) {
67994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        ALOGE("Could not create directories; exiting.\n");
68094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        exit(1);
68194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
68294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
683bd558d61871218f5b13df2fe4b7cc3b530ee947cStephen Smalley    if (selinux_enabled && selinux_status_open(true) < 0) {
684bd558d61871218f5b13df2fe4b7cc3b530ee947cStephen Smalley        ALOGE("Could not open selinux status; exiting.\n");
685bd558d61871218f5b13df2fe4b7cc3b530ee947cStephen Smalley        exit(1);
686bd558d61871218f5b13df2fe4b7cc3b530ee947cStephen Smalley    }
687bd558d61871218f5b13df2fe4b7cc3b530ee947cStephen Smalley
68894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    drop_privileges();
68994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
69094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    lsocket = android_get_control_socket(SOCKET_PATH);
69194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (lsocket < 0) {
69294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        ALOGE("Failed to get socket from environment: %s\n", strerror(errno));
69394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        exit(1);
69494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
69594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (listen(lsocket, 5)) {
69694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        ALOGE("Listen on socket failed: %s\n", strerror(errno));
69794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        exit(1);
69894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
69994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    fcntl(lsocket, F_SETFD, FD_CLOEXEC);
70094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
70194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    for (;;) {
70294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        alen = sizeof(addr);
70394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        s = accept(lsocket, &addr, &alen);
70494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (s < 0) {
70594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            ALOGE("Accept failed: %s\n", strerror(errno));
70694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            continue;
70794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
70894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        fcntl(s, F_SETFD, FD_CLOEXEC);
70994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
71094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        ALOGI("new connection\n");
71194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        for (;;) {
71294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            unsigned short count;
71394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (readx(s, &count, sizeof(count))) {
71494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                ALOGE("failed to read size\n");
71594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                break;
71694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
71794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if ((count < 1) || (count >= BUFFER_MAX)) {
71894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                ALOGE("invalid size %d\n", count);
71994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                break;
72094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
72194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (readx(s, buf, count)) {
72294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                ALOGE("failed to read command\n");
72394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                break;
72494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
72594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            buf[count] = 0;
726bd558d61871218f5b13df2fe4b7cc3b530ee947cStephen Smalley            if (selinux_enabled && selinux_status_updated() > 0) {
727bd558d61871218f5b13df2fe4b7cc3b530ee947cStephen Smalley                selinux_android_seapp_context_reload();
728bd558d61871218f5b13df2fe4b7cc3b530ee947cStephen Smalley            }
72994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (execute(s, buf)) break;
73094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
73194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        ALOGI("closing connection\n");
73294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        close(s);
73394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
73494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
73594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return 0;
73694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
737