installd.cpp revision a5e161b1207ef447a51e99856097d69d4a6111e1
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*/
16a5e161b1207ef447a51e99856097d69d4a6111e1Mark Salyzyn#define LOG_TAG "installd"
1794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
1802d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include <fcntl.h>
19bd558d61871218f5b13df2fe4b7cc3b530ee947cStephen Smalley#include <selinux/android.h>
20bd558d61871218f5b13df2fe4b7cc3b530ee947cStephen Smalley#include <selinux/avc.h>
2102d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include <sys/capability.h>
22c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey#include <sys/fsuid.h>
2302d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include <sys/prctl.h>
2402d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include <sys/stat.h>
2502d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe
2602d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include <android-base/logging.h>
2702d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include <cutils/fs.h>
2802d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include <cutils/properties.h>
29a5e161b1207ef447a51e99856097d69d4a6111e1Mark Salyzyn#include <log/log.h>              // TODO: Move everything to base::logging.
3002d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include <private/android_filesystem_config.h>
3102d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe
32f3e30b936ef103dc0f3d8697e0f86ba82b49609eJeff Sharkey#include "InstalldNativeService.h"
33f3e30b936ef103dc0f3d8697e0f86ba82b49609eJeff Sharkey#include "globals.h"
34f3e30b936ef103dc0f3d8697e0f86ba82b49609eJeff Sharkey#include "installd_constants.h"
35f3e30b936ef103dc0f3d8697e0f86ba82b49609eJeff Sharkey#include "installd_deps.h"  // Need to fill in requirements of commands.
36f3e30b936ef103dc0f3d8697e0f86ba82b49609eJeff Sharkey#include "utils.h"
3702d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe
3802d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampenamespace android {
3902d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampenamespace installd {
4002d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe
4102d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe// Check that installd-deps sizes match cutils sizes.
4202d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampestatic_assert(kPropertyKeyMax == PROPERTY_KEY_MAX, "Size mismatch.");
4302d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampestatic_assert(kPropertyValueMax == PROPERTY_VALUE_MAX, "Size mismatch.");
4402d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe
4502d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe////////////////////////
4602d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe// Plug-in functions. //
4702d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe////////////////////////
4802d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe
4902d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampeint get_property(const char *key, char *value, const char *default_value) {
5002d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    return property_get(key, value, default_value);
5102d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe}
5202d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe
5302d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe// Compute the output path of
5402d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampebool calculate_oat_file_path(char path[PKG_PATH_MAX],
5502d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe                             const char *oat_dir,
5602d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe                             const char *apk_path,
5702d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe                             const char *instruction_set) {
58632350257cd93e34aa1e8b354c1535864b314c38Dan Austin    const char *file_name_start;
59632350257cd93e34aa1e8b354c1535864b314c38Dan Austin    const char *file_name_end;
6002d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe
6102d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    file_name_start = strrchr(apk_path, '/');
6202d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    if (file_name_start == NULL) {
63a5e161b1207ef447a51e99856097d69d4a6111e1Mark Salyzyn        SLOGE("apk_path '%s' has no '/'s in it\n", apk_path);
6402d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe        return false;
6502d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    }
6602d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    file_name_end = strrchr(apk_path, '.');
6702d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    if (file_name_end < file_name_start) {
68a5e161b1207ef447a51e99856097d69d4a6111e1Mark Salyzyn        SLOGE("apk_path '%s' has no extension\n", apk_path);
6902d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe        return false;
7002d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    }
7102d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe
7202d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    // Calculate file_name
7302d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    int file_name_len = file_name_end - file_name_start - 1;
7402d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    char file_name[file_name_len + 1];
7502d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    memcpy(file_name, file_name_start + 1, file_name_len);
7602d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    file_name[file_name_len] = '\0';
7702d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe
7802d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    // <apk_parent_dir>/oat/<isa>/<file_name>.odex
7902d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    snprintf(path, PKG_PATH_MAX, "%s/%s/%s.odex", oat_dir, instruction_set, file_name);
8002d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    return true;
8102d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe}
8202d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe
8302d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe/*
8402d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe * Computes the odex file for the given apk_path and instruction_set.
8502d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe * /system/framework/whatever.jar -> /system/framework/oat/<isa>/whatever.odex
8602d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe *
8702d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe * Returns false if it failed to determine the odex file path.
8802d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe */
8902d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampebool calculate_odex_file_path(char path[PKG_PATH_MAX],
9002d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe                              const char *apk_path,
9102d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe                              const char *instruction_set) {
9202d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    if (strlen(apk_path) + strlen("oat/") + strlen(instruction_set)
9302d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe            + strlen("/") + strlen("odex") + 1 > PKG_PATH_MAX) {
94a5e161b1207ef447a51e99856097d69d4a6111e1Mark Salyzyn        SLOGE("apk_path '%s' may be too long to form odex file path.\n", apk_path);
9502d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe        return false;
9602d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    }
9702d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe
9802d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    strcpy(path, apk_path);
9902d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    char *end = strrchr(path, '/');
10002d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    if (end == NULL) {
101a5e161b1207ef447a51e99856097d69d4a6111e1Mark Salyzyn        SLOGE("apk_path '%s' has no '/'s in it?!\n", apk_path);
10202d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe        return false;
10302d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    }
10402d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    const char *apk_end = apk_path + (end - path); // strrchr(apk_path, '/');
10502d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe
10602d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    strcpy(end + 1, "oat/");       // path = /system/framework/oat/\0
10702d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    strcat(path, instruction_set); // path = /system/framework/oat/<isa>\0
10802d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    strcat(path, apk_end);         // path = /system/framework/oat/<isa>/whatever.jar\0
10902d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    end = strrchr(path, '.');
11002d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    if (end == NULL) {
111a5e161b1207ef447a51e99856097d69d4a6111e1Mark Salyzyn        SLOGE("apk_path '%s' has no extension.\n", apk_path);
11202d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe        return false;
11302d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    }
11402d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    strcpy(end + 1, "odex");
11502d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    return true;
11602d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe}
11702d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe
11802d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampebool create_cache_path(char path[PKG_PATH_MAX],
11902d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe                       const char *src,
12002d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe                       const char *instruction_set) {
12100087b70401bd09dfeaab9171d006e833e3cd844Greg Kaiser    /* demand that we are an absolute path */
12200087b70401bd09dfeaab9171d006e833e3cd844Greg Kaiser    if ((src == nullptr) || (src[0] != '/') || strstr(src,"..")) {
12302d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe        return false;
12402d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    }
12502d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe
12600087b70401bd09dfeaab9171d006e833e3cd844Greg Kaiser    size_t srclen = strlen(src);
12700087b70401bd09dfeaab9171d006e833e3cd844Greg Kaiser
12802d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    if (srclen > PKG_PATH_MAX) {        // XXX: PKG_NAME_MAX?
12902d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe        return false;
13002d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    }
13102d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe
13202d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    size_t dstlen =
13302d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe        android_data_dir.len +
13402d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe        strlen(DALVIK_CACHE) +
13502d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe        1 +
13602d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe        strlen(instruction_set) +
13702d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe        srclen +
13802d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe        strlen(DALVIK_CACHE_POSTFIX) + 2;
13902d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe
14002d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    if (dstlen > PKG_PATH_MAX) {
14102d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe        return false;
14202d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    }
14302d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe
144249c1796a2e62f8751348e5bafce9f40e6538cbaDavid Brazdil    sprintf(path,"%s%s/%s/%s",
14502d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe            android_data_dir.path,
14602d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe            DALVIK_CACHE,
14702d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe            instruction_set,
148249c1796a2e62f8751348e5bafce9f40e6538cbaDavid Brazdil            src + 1 /* skip the leading / */);
14902d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe
15002d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    char* tmp =
15102d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe            path +
15202d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe            android_data_dir.len +
15302d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe            strlen(DALVIK_CACHE) +
15402d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe            1 +
15502d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe            strlen(instruction_set) + 1;
15602d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe
15702d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    for(; *tmp; tmp++) {
15802d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe        if (*tmp == '/') {
15902d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe            *tmp = '@';
16002d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe        }
16102d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    }
16202d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe
163249c1796a2e62f8751348e5bafce9f40e6538cbaDavid Brazdil    strcat(path, DALVIK_CACHE_POSTFIX);
16402d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    return true;
16502d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe}
16602d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe
167d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampestatic bool initialize_globals() {
16802d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    const char* data_path = getenv("ANDROID_DATA");
16902d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    if (data_path == nullptr) {
170a5e161b1207ef447a51e99856097d69d4a6111e1Mark Salyzyn        SLOGE("Could not find ANDROID_DATA");
17102d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe        return false;
17294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
17302d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    const char* root_path = getenv("ANDROID_ROOT");
17402d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    if (root_path == nullptr) {
175a5e161b1207ef447a51e99856097d69d4a6111e1Mark Salyzyn        SLOGE("Could not find ANDROID_ROOT");
17602d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe        return false;
17794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
17894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
17902d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    return init_globals_from_data_and_root(data_path, root_path);
18094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
18194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
18202d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampestatic int initialize_directories() {
18394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    int res = -1;
18494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
18594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    // Read current filesystem layout version to handle upgrade paths
18694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    char version_path[PATH_MAX];
18794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    snprintf(version_path, PATH_MAX, "%s.layout_version", android_data_dir.path);
18894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
18994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    int oldVersion;
19094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (fs_read_atomic_int(version_path, &oldVersion) == -1) {
19194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        oldVersion = 0;
19294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
19394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    int version = oldVersion;
19494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
195e02657d627a85628ec6f0f398fb85283b8d91bfaJeff Sharkey    if (version < 2) {
196e02657d627a85628ec6f0f398fb85283b8d91bfaJeff Sharkey        SLOGD("Assuming that device has multi-user storage layout; upgrade no longer supported");
19794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        version = 2;
19894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
19994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
20007053fcb61436221fac2281394e98ec9d0feab3dRobin Lee    if (ensure_config_user_dirs(0) == -1) {
201a5e161b1207ef447a51e99856097d69d4a6111e1Mark Salyzyn        SLOGE("Failed to setup misc for user 0");
20207053fcb61436221fac2281394e98ec9d0feab3dRobin Lee        goto fail;
20307053fcb61436221fac2281394e98ec9d0feab3dRobin Lee    }
20407053fcb61436221fac2281394e98ec9d0feab3dRobin Lee
205095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee    if (version == 2) {
206a5e161b1207ef447a51e99856097d69d4a6111e1Mark Salyzyn        SLOGD("Upgrading to /data/misc/user directories");
207095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee
20860fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        char misc_dir[PATH_MAX];
20960fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        snprintf(misc_dir, PATH_MAX, "%smisc", android_data_dir.path);
21060fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee
21160fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        char keychain_added_dir[PATH_MAX];
21260fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        snprintf(keychain_added_dir, PATH_MAX, "%s/keychain/cacerts-added", misc_dir);
21360fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee
21460fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        char keychain_removed_dir[PATH_MAX];
21560fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        snprintf(keychain_removed_dir, PATH_MAX, "%s/keychain/cacerts-removed", misc_dir);
21660fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee
217095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee        DIR *dir;
218095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee        struct dirent *dirent;
219e02657d627a85628ec6f0f398fb85283b8d91bfaJeff Sharkey        dir = opendir("/data/user");
220095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee        if (dir != NULL) {
221095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee            while ((dirent = readdir(dir))) {
22260fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee                const char *name = dirent->d_name;
223095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee
22460fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee                // skip "." and ".."
22560fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee                if (name[0] == '.') {
22660fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee                    if (name[1] == 0) continue;
22760fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee                    if ((name[1] == '.') && (name[2] == 0)) continue;
22860fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee                }
229095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee
23060fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee                uint32_t user_id = atoi(name);
23160fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee
23260fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee                // /data/misc/user/<user_id>
23360fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee                if (ensure_config_user_dirs(user_id) == -1) {
23460fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee                    goto fail;
23560fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee                }
23660fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee
23760fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee                char misc_added_dir[PATH_MAX];
23860fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee                snprintf(misc_added_dir, PATH_MAX, "%s/user/%s/cacerts-added", misc_dir, name);
23960fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee
24060fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee                char misc_removed_dir[PATH_MAX];
24160fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee                snprintf(misc_removed_dir, PATH_MAX, "%s/user/%s/cacerts-removed", misc_dir, name);
24260fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee
24360fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee                uid_t uid = multiuser_get_uid(user_id, AID_SYSTEM);
24460fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee                gid_t gid = uid;
24560fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee                if (access(keychain_added_dir, F_OK) == 0) {
24660fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee                    if (copy_dir_files(keychain_added_dir, misc_added_dir, uid, gid) != 0) {
247a5e161b1207ef447a51e99856097d69d4a6111e1Mark Salyzyn                        SLOGE("Some files failed to copy");
24860fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee                    }
24960fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee                }
25060fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee                if (access(keychain_removed_dir, F_OK) == 0) {
25160fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee                    if (copy_dir_files(keychain_removed_dir, misc_removed_dir, uid, gid) != 0) {
252a5e161b1207ef447a51e99856097d69d4a6111e1Mark Salyzyn                        SLOGE("Some files failed to copy");
253095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee                    }
254095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee                }
255095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee            }
256095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee            closedir(dir);
25707053fcb61436221fac2281394e98ec9d0feab3dRobin Lee
25860fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee            if (access(keychain_added_dir, F_OK) == 0) {
25960fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee                delete_dir_contents(keychain_added_dir, 1, 0);
26007053fcb61436221fac2281394e98ec9d0feab3dRobin Lee            }
26160fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee            if (access(keychain_removed_dir, F_OK) == 0) {
26260fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee                delete_dir_contents(keychain_removed_dir, 1, 0);
26307053fcb61436221fac2281394e98ec9d0feab3dRobin Lee            }
26407053fcb61436221fac2281394e98ec9d0feab3dRobin Lee        }
265095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee
26607053fcb61436221fac2281394e98ec9d0feab3dRobin Lee        version = 3;
267095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee    }
268095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee
26994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    // Persist layout version if changed
27094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (version != oldVersion) {
27194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (fs_write_atomic_int(version_path, version) == -1) {
272a5e161b1207ef447a51e99856097d69d4a6111e1Mark Salyzyn            SLOGE("Failed to save version to %s: %s", version_path, strerror(errno));
27394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            goto fail;
27494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
27594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
27694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
27794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    // Success!
27894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    res = 0;
27994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
28094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodfail:
28194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return res;
28294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
28394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
2847abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalleystatic int log_callback(int type, const char *fmt, ...) {
2857abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalley    va_list ap;
2867abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalley    int priority;
2877abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalley
2887abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalley    switch (type) {
2897abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalley    case SELINUX_WARNING:
2907abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalley        priority = ANDROID_LOG_WARN;
2917abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalley        break;
2927abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalley    case SELINUX_INFO:
2937abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalley        priority = ANDROID_LOG_INFO;
2947abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalley        break;
2957abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalley    default:
2967abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalley        priority = ANDROID_LOG_ERROR;
2977abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalley        break;
2987abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalley    }
2997abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalley    va_start(ap, fmt);
3007abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalley    LOG_PRI_VA(priority, "SELinux", fmt, ap);
3017abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalley    va_end(ap);
3027abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalley    return 0;
3037abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalley}
3047abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalley
30502d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampestatic int installd_main(const int argc ATTRIBUTE_UNUSED, char *argv[]) {
3066c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey    int ret;
307bd558d61871218f5b13df2fe4b7cc3b530ee947cStephen Smalley    int selinux_enabled = (is_selinux_enabled() > 0);
30894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
309e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey    setenv("ANDROID_LOG_TAGS", "*:v", 1);
310e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey    android::base::InitLogging(argv);
311e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey
312a5e161b1207ef447a51e99856097d69d4a6111e1Mark Salyzyn    SLOGI("installd firing up");
31394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
3147abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalley    union selinux_callback cb;
3157abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalley    cb.func_log = log_callback;
3167abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalley    selinux_set_callback(SELINUX_CB_LOG, cb);
3177abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalley
31802d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    if (!initialize_globals()) {
319a5e161b1207ef447a51e99856097d69d4a6111e1Mark Salyzyn        SLOGE("Could not initialize globals; exiting.\n");
32094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        exit(1);
32194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
32294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
32394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (initialize_directories() < 0) {
324a5e161b1207ef447a51e99856097d69d4a6111e1Mark Salyzyn        SLOGE("Could not create directories; exiting.\n");
32594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        exit(1);
32694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
32794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
328bd558d61871218f5b13df2fe4b7cc3b530ee947cStephen Smalley    if (selinux_enabled && selinux_status_open(true) < 0) {
329a5e161b1207ef447a51e99856097d69d4a6111e1Mark Salyzyn        SLOGE("Could not open selinux status; exiting.\n");
330bd558d61871218f5b13df2fe4b7cc3b530ee947cStephen Smalley        exit(1);
331bd558d61871218f5b13df2fe4b7cc3b530ee947cStephen Smalley    }
332bd558d61871218f5b13df2fe4b7cc3b530ee947cStephen Smalley
3339087400f3c82b97aa17a74329c7e65c0a5ff4062Jeff Sharkey    if ((ret = InstalldNativeService::start()) != android::OK) {
334a5e161b1207ef447a51e99856097d69d4a6111e1Mark Salyzyn        SLOGE("Unable to start InstalldNativeService: %d", ret);
3359087400f3c82b97aa17a74329c7e65c0a5ff4062Jeff Sharkey        exit(1);
3369087400f3c82b97aa17a74329c7e65c0a5ff4062Jeff Sharkey    }
3379087400f3c82b97aa17a74329c7e65c0a5ff4062Jeff Sharkey
3386c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey    IPCThreadState::self()->joinThreadPool();
3396c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey
3406c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey    LOG(INFO) << "installd shutting down";
34194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
34294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return 0;
34394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
34402d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe
34502d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe}  // namespace installd
34602d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe}  // namespace android
34702d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe
34802d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampeint main(const int argc, char *argv[]) {
34902d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    return android::installd::installd_main(argc, argv);
35002d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe}
351