installd.cpp revision f3e30b936ef103dc0f3d8697e0f86ba82b49609e
194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood/*
294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood** Copyright 2008, The Android Open Source Project
394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood**
419803807cd7ae01868fcfa50305f4a7dd13765e2Jeff Sharkey** Licensed under the Apache License, Version 2.0 (the "License");
519803807cd7ae01868fcfa50305f4a7dd13765e2Jeff Sharkey** you may not use this file except in compliance with the License.
619803807cd7ae01868fcfa50305f4a7dd13765e2Jeff Sharkey** You may obtain a copy of the License at
794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood**
819803807cd7ae01868fcfa50305f4a7dd13765e2Jeff Sharkey**     http://www.apache.org/licenses/LICENSE-2.0
994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood**
1019803807cd7ae01868fcfa50305f4a7dd13765e2Jeff Sharkey** Unless required by applicable law or agreed to in writing, software
1119803807cd7ae01868fcfa50305f4a7dd13765e2Jeff Sharkey** distributed under the License is distributed on an "AS IS" BASIS,
1219803807cd7ae01868fcfa50305f4a7dd13765e2Jeff Sharkey** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1319803807cd7ae01868fcfa50305f4a7dd13765e2Jeff Sharkey** See the License for the specific language governing permissions and
1494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood** limitations under the License.
1594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood*/
1694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
1702d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include <fcntl.h>
18bd558d61871218f5b13df2fe4b7cc3b530ee947cStephen Smalley#include <selinux/android.h>
19bd558d61871218f5b13df2fe4b7cc3b530ee947cStephen Smalley#include <selinux/avc.h>
2002d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include <sys/capability.h>
21c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey#include <sys/fsuid.h>
2202d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include <sys/prctl.h>
2302d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include <sys/stat.h>
2402d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe
2502d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include <android-base/logging.h>
2602d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include <cutils/fs.h>
2702d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include <cutils/log.h>               // TODO: Move everything to base::logging.
2802d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include <cutils/properties.h>
2902d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include <private/android_filesystem_config.h>
3002d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe
31f3e30b936ef103dc0f3d8697e0f86ba82b49609eJeff Sharkey#include "InstalldNativeService.h"
32f3e30b936ef103dc0f3d8697e0f86ba82b49609eJeff Sharkey#include "globals.h"
33f3e30b936ef103dc0f3d8697e0f86ba82b49609eJeff Sharkey#include "installd_constants.h"
34f3e30b936ef103dc0f3d8697e0f86ba82b49609eJeff Sharkey#include "installd_deps.h"  // Need to fill in requirements of commands.
35f3e30b936ef103dc0f3d8697e0f86ba82b49609eJeff Sharkey#include "utils.h"
3602d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe
3702d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#ifndef LOG_TAG
3802d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#define LOG_TAG "installd"
3902d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#endif
4094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
4102d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampenamespace android {
4202d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampenamespace installd {
4302d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe
4402d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe// Check that installd-deps sizes match cutils sizes.
4502d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampestatic_assert(kPropertyKeyMax == PROPERTY_KEY_MAX, "Size mismatch.");
4602d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampestatic_assert(kPropertyValueMax == PROPERTY_VALUE_MAX, "Size mismatch.");
4702d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe
4802d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe////////////////////////
4902d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe// Plug-in functions. //
5002d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe////////////////////////
5102d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe
5202d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampeint get_property(const char *key, char *value, const char *default_value) {
5302d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    return property_get(key, value, default_value);
5402d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe}
5502d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe
5602d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe// Compute the output path of
5702d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampebool calculate_oat_file_path(char path[PKG_PATH_MAX],
5802d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe                             const char *oat_dir,
5902d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe                             const char *apk_path,
6002d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe                             const char *instruction_set) {
61632350257cd93e34aa1e8b354c1535864b314c38Dan Austin    const char *file_name_start;
62632350257cd93e34aa1e8b354c1535864b314c38Dan Austin    const char *file_name_end;
6302d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe
6402d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    file_name_start = strrchr(apk_path, '/');
6502d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    if (file_name_start == NULL) {
6602d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe        ALOGE("apk_path '%s' has no '/'s in it\n", apk_path);
6702d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe        return false;
6802d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    }
6902d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    file_name_end = strrchr(apk_path, '.');
7002d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    if (file_name_end < file_name_start) {
7102d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe        ALOGE("apk_path '%s' has no extension\n", apk_path);
7202d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe        return false;
7302d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    }
7402d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe
7502d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    // Calculate file_name
7602d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    int file_name_len = file_name_end - file_name_start - 1;
7702d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    char file_name[file_name_len + 1];
7802d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    memcpy(file_name, file_name_start + 1, file_name_len);
7902d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    file_name[file_name_len] = '\0';
8002d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe
8102d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    // <apk_parent_dir>/oat/<isa>/<file_name>.odex
8202d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    snprintf(path, PKG_PATH_MAX, "%s/%s/%s.odex", oat_dir, instruction_set, file_name);
8302d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    return true;
8402d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe}
8502d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe
8602d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe/*
8702d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe * Computes the odex file for the given apk_path and instruction_set.
8802d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe * /system/framework/whatever.jar -> /system/framework/oat/<isa>/whatever.odex
8902d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe *
9002d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe * Returns false if it failed to determine the odex file path.
9102d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe */
9202d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampebool calculate_odex_file_path(char path[PKG_PATH_MAX],
9302d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe                              const char *apk_path,
9402d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe                              const char *instruction_set) {
9502d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    if (strlen(apk_path) + strlen("oat/") + strlen(instruction_set)
9602d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe            + strlen("/") + strlen("odex") + 1 > PKG_PATH_MAX) {
9702d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe        ALOGE("apk_path '%s' may be too long to form odex file path.\n", apk_path);
9802d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe        return false;
9902d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    }
10002d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe
10102d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    strcpy(path, apk_path);
10202d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    char *end = strrchr(path, '/');
10302d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    if (end == NULL) {
10402d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe        ALOGE("apk_path '%s' has no '/'s in it?!\n", apk_path);
10502d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe        return false;
10602d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    }
10702d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    const char *apk_end = apk_path + (end - path); // strrchr(apk_path, '/');
10802d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe
10902d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    strcpy(end + 1, "oat/");       // path = /system/framework/oat/\0
11002d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    strcat(path, instruction_set); // path = /system/framework/oat/<isa>\0
11102d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    strcat(path, apk_end);         // path = /system/framework/oat/<isa>/whatever.jar\0
11202d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    end = strrchr(path, '.');
11302d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    if (end == NULL) {
11402d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe        ALOGE("apk_path '%s' has no extension.\n", apk_path);
11502d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe        return false;
11602d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    }
11702d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    strcpy(end + 1, "odex");
11802d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    return true;
11902d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe}
12002d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe
12102d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampebool create_cache_path(char path[PKG_PATH_MAX],
12202d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe                       const char *src,
12302d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe                       const char *instruction_set) {
12400087b70401bd09dfeaab9171d006e833e3cd844Greg Kaiser    /* demand that we are an absolute path */
12500087b70401bd09dfeaab9171d006e833e3cd844Greg Kaiser    if ((src == nullptr) || (src[0] != '/') || strstr(src,"..")) {
12602d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe        return false;
12702d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    }
12802d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe
12900087b70401bd09dfeaab9171d006e833e3cd844Greg Kaiser    size_t srclen = strlen(src);
13000087b70401bd09dfeaab9171d006e833e3cd844Greg Kaiser
13102d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    if (srclen > PKG_PATH_MAX) {        // XXX: PKG_NAME_MAX?
13202d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe        return false;
13302d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    }
13402d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe
13502d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    size_t dstlen =
13602d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe        android_data_dir.len +
13702d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe        strlen(DALVIK_CACHE) +
13802d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe        1 +
13902d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe        strlen(instruction_set) +
14002d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe        srclen +
14102d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe        strlen(DALVIK_CACHE_POSTFIX) + 2;
14202d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe
14302d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    if (dstlen > PKG_PATH_MAX) {
14402d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe        return false;
14502d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    }
14602d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe
147249c1796a2e62f8751348e5bafce9f40e6538cbaDavid Brazdil    sprintf(path,"%s%s/%s/%s",
14802d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe            android_data_dir.path,
14902d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe            DALVIK_CACHE,
15002d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe            instruction_set,
151249c1796a2e62f8751348e5bafce9f40e6538cbaDavid Brazdil            src + 1 /* skip the leading / */);
15202d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe
15302d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    char* tmp =
15402d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe            path +
15502d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe            android_data_dir.len +
15602d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe            strlen(DALVIK_CACHE) +
15702d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe            1 +
15802d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe            strlen(instruction_set) + 1;
15902d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe
16002d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    for(; *tmp; tmp++) {
16102d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe        if (*tmp == '/') {
16202d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe            *tmp = '@';
16302d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe        }
16402d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    }
16502d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe
166249c1796a2e62f8751348e5bafce9f40e6538cbaDavid Brazdil    strcat(path, DALVIK_CACHE_POSTFIX);
16702d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    return true;
16802d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe}
16902d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe
170d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampestatic bool initialize_globals() {
17102d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    const char* data_path = getenv("ANDROID_DATA");
17202d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    if (data_path == nullptr) {
17302d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe        ALOGE("Could not find ANDROID_DATA");
17402d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe        return false;
17594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
17602d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    const char* root_path = getenv("ANDROID_ROOT");
17702d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    if (root_path == nullptr) {
17802d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe        ALOGE("Could not find ANDROID_ROOT");
17902d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe        return false;
18094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
18194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
18202d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    return init_globals_from_data_and_root(data_path, root_path);
18394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
18494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
18502d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampestatic int initialize_directories() {
18694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    int res = -1;
18794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
18894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    // Read current filesystem layout version to handle upgrade paths
18994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    char version_path[PATH_MAX];
19094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    snprintf(version_path, PATH_MAX, "%s.layout_version", android_data_dir.path);
19194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
19294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    int oldVersion;
19394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (fs_read_atomic_int(version_path, &oldVersion) == -1) {
19494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        oldVersion = 0;
19594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
19694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    int version = oldVersion;
19794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
198e02657d627a85628ec6f0f398fb85283b8d91bfaJeff Sharkey    if (version < 2) {
199e02657d627a85628ec6f0f398fb85283b8d91bfaJeff Sharkey        SLOGD("Assuming that device has multi-user storage layout; upgrade no longer supported");
20094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        version = 2;
20194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
20294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
20307053fcb61436221fac2281394e98ec9d0feab3dRobin Lee    if (ensure_config_user_dirs(0) == -1) {
20407053fcb61436221fac2281394e98ec9d0feab3dRobin Lee        ALOGE("Failed to setup misc for user 0");
20507053fcb61436221fac2281394e98ec9d0feab3dRobin Lee        goto fail;
20607053fcb61436221fac2281394e98ec9d0feab3dRobin Lee    }
20707053fcb61436221fac2281394e98ec9d0feab3dRobin Lee
208095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee    if (version == 2) {
209095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee        ALOGD("Upgrading to /data/misc/user directories");
210095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee
21160fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        char misc_dir[PATH_MAX];
21260fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        snprintf(misc_dir, PATH_MAX, "%smisc", android_data_dir.path);
21360fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee
21460fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        char keychain_added_dir[PATH_MAX];
21560fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        snprintf(keychain_added_dir, PATH_MAX, "%s/keychain/cacerts-added", misc_dir);
21660fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee
21760fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        char keychain_removed_dir[PATH_MAX];
21860fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        snprintf(keychain_removed_dir, PATH_MAX, "%s/keychain/cacerts-removed", misc_dir);
21960fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee
220095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee        DIR *dir;
221095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee        struct dirent *dirent;
222e02657d627a85628ec6f0f398fb85283b8d91bfaJeff Sharkey        dir = opendir("/data/user");
223095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee        if (dir != NULL) {
224095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee            while ((dirent = readdir(dir))) {
22560fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee                const char *name = dirent->d_name;
226095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee
22760fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee                // skip "." and ".."
22860fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee                if (name[0] == '.') {
22960fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee                    if (name[1] == 0) continue;
23060fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee                    if ((name[1] == '.') && (name[2] == 0)) continue;
23160fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee                }
232095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee
23360fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee                uint32_t user_id = atoi(name);
23460fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee
23560fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee                // /data/misc/user/<user_id>
23660fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee                if (ensure_config_user_dirs(user_id) == -1) {
23760fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee                    goto fail;
23860fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee                }
23960fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee
24060fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee                char misc_added_dir[PATH_MAX];
24160fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee                snprintf(misc_added_dir, PATH_MAX, "%s/user/%s/cacerts-added", misc_dir, name);
24260fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee
24360fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee                char misc_removed_dir[PATH_MAX];
24460fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee                snprintf(misc_removed_dir, PATH_MAX, "%s/user/%s/cacerts-removed", misc_dir, name);
24560fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee
24660fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee                uid_t uid = multiuser_get_uid(user_id, AID_SYSTEM);
24760fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee                gid_t gid = uid;
24860fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee                if (access(keychain_added_dir, F_OK) == 0) {
24960fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee                    if (copy_dir_files(keychain_added_dir, misc_added_dir, uid, gid) != 0) {
25060fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee                        ALOGE("Some files failed to copy");
25160fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee                    }
25260fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee                }
25360fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee                if (access(keychain_removed_dir, F_OK) == 0) {
25460fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee                    if (copy_dir_files(keychain_removed_dir, misc_removed_dir, uid, gid) != 0) {
25560fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee                        ALOGE("Some files failed to copy");
256095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee                    }
257095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee                }
258095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee            }
259095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee            closedir(dir);
26007053fcb61436221fac2281394e98ec9d0feab3dRobin Lee
26160fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee            if (access(keychain_added_dir, F_OK) == 0) {
26260fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee                delete_dir_contents(keychain_added_dir, 1, 0);
26307053fcb61436221fac2281394e98ec9d0feab3dRobin Lee            }
26460fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee            if (access(keychain_removed_dir, F_OK) == 0) {
26560fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee                delete_dir_contents(keychain_removed_dir, 1, 0);
26607053fcb61436221fac2281394e98ec9d0feab3dRobin Lee            }
26707053fcb61436221fac2281394e98ec9d0feab3dRobin Lee        }
268095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee
26907053fcb61436221fac2281394e98ec9d0feab3dRobin Lee        version = 3;
270095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee    }
271095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee
27294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    // Persist layout version if changed
27394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (version != oldVersion) {
27494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (fs_write_atomic_int(version_path, version) == -1) {
27594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            ALOGE("Failed to save version to %s: %s", version_path, strerror(errno));
27694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            goto fail;
27794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
27894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
27994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
28094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    // Success!
28194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    res = 0;
28294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
28394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodfail:
28494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return res;
28594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
28694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
2877abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalleystatic int log_callback(int type, const char *fmt, ...) {
2887abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalley    va_list ap;
2897abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalley    int priority;
2907abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalley
2917abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalley    switch (type) {
2927abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalley    case SELINUX_WARNING:
2937abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalley        priority = ANDROID_LOG_WARN;
2947abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalley        break;
2957abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalley    case SELINUX_INFO:
2967abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalley        priority = ANDROID_LOG_INFO;
2977abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalley        break;
2987abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalley    default:
2997abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalley        priority = ANDROID_LOG_ERROR;
3007abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalley        break;
3017abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalley    }
3027abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalley    va_start(ap, fmt);
3037abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalley    LOG_PRI_VA(priority, "SELinux", fmt, ap);
3047abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalley    va_end(ap);
3057abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalley    return 0;
3067abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalley}
3077abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalley
30802d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampestatic int installd_main(const int argc ATTRIBUTE_UNUSED, char *argv[]) {
3096c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey    int ret;
310bd558d61871218f5b13df2fe4b7cc3b530ee947cStephen Smalley    int selinux_enabled = (is_selinux_enabled() > 0);
31194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
312e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey    setenv("ANDROID_LOG_TAGS", "*:v", 1);
313e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey    android::base::InitLogging(argv);
314e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey
3156c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey    LOG(INFO) << "installd firing up";
31694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
3177abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalley    union selinux_callback cb;
3187abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalley    cb.func_log = log_callback;
3197abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalley    selinux_set_callback(SELINUX_CB_LOG, cb);
3207abb52bcafa2f7b422dfe22c5ea275c2fa9e6201Stephen Smalley
32102d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    if (!initialize_globals()) {
32294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        ALOGE("Could not initialize globals; exiting.\n");
32394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        exit(1);
32494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
32594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
32694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (initialize_directories() < 0) {
32794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        ALOGE("Could not create directories; exiting.\n");
32894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        exit(1);
32994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
33094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
331bd558d61871218f5b13df2fe4b7cc3b530ee947cStephen Smalley    if (selinux_enabled && selinux_status_open(true) < 0) {
332bd558d61871218f5b13df2fe4b7cc3b530ee947cStephen Smalley        ALOGE("Could not open selinux status; exiting.\n");
333bd558d61871218f5b13df2fe4b7cc3b530ee947cStephen Smalley        exit(1);
334bd558d61871218f5b13df2fe4b7cc3b530ee947cStephen Smalley    }
335bd558d61871218f5b13df2fe4b7cc3b530ee947cStephen Smalley
3369087400f3c82b97aa17a74329c7e65c0a5ff4062Jeff Sharkey    if ((ret = InstalldNativeService::start()) != android::OK) {
3379087400f3c82b97aa17a74329c7e65c0a5ff4062Jeff Sharkey        ALOGE("Unable to start InstalldNativeService: %d", ret);
3389087400f3c82b97aa17a74329c7e65c0a5ff4062Jeff Sharkey        exit(1);
3399087400f3c82b97aa17a74329c7e65c0a5ff4062Jeff Sharkey    }
3409087400f3c82b97aa17a74329c7e65c0a5ff4062Jeff Sharkey
3416c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey    IPCThreadState::self()->joinThreadPool();
3426c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey
3436c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey    LOG(INFO) << "installd shutting down";
34494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
34594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return 0;
34694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
34702d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe
34802d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe}  // namespace installd
34902d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe}  // namespace android
35002d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe
35102d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampeint main(const int argc, char *argv[]) {
35202d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    return android::installd::installd_main(argc, argv);
35302d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe}
354