Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit eb84ee8d authored by Mårten Kongstad's avatar Mårten Kongstad Committed by Todd Kennedy
Browse files

Remove legacy idmap binary

The idmap tool has been replaced by idmap2. Remove now obsolete code
related to the old binary.

Bug: 118711077
Test: atest OverlayDeviceTests OverlayHostTests
Change-Id: Ie1b8fe6f53ce5308b3a1ee6b9f4719cd830a2ecf
parent 993e82ee
Loading
Loading
Loading
Loading

cmds/idmap/Android.bp

deleted100644 → 0
+0 −38
Original line number Diff line number Diff line
// Copyright (C) 2012 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

cc_binary {
    name: "idmap",

    srcs: [
        "idmap.cpp",
        "create.cpp",
        "scan.cpp",
        "inspect.cpp",
    ],

    shared_libs: [
        "liblog",
        "libutils",
        "libandroidfw",
        "libcutils",
    ],

    cflags: [
        "-Wall",
        "-Werror",
        "-Wunused",
        "-Wunreachable-code",
    ],
}

cmds/idmap/create.cpp

deleted100644 → 0
+0 −233
Original line number Diff line number Diff line
#include "idmap.h"

#include <memory>
#include <androidfw/AssetManager.h>
#include <androidfw/ResourceTypes.h>
#include <androidfw/ZipFileRO.h>
#include <utils/String8.h>

#include <fcntl.h>
#include <sys/file.h>
#include <sys/stat.h>

using namespace android;

namespace {
    int get_zip_entry_crc(const char *zip_path, const char *entry_name, uint32_t *crc)
    {
        std::unique_ptr<ZipFileRO> zip(ZipFileRO::open(zip_path));
        if (zip.get() == NULL) {
            return -1;
        }
        ZipEntryRO entry = zip->findEntryByName(entry_name);
        if (entry == NULL) {
            return -1;
        }
        if (!zip->getEntryInfo(entry, NULL, NULL, NULL, NULL, NULL, crc)) {
            return -1;
        }
        zip->releaseEntry(entry);
        return 0;
    }

    int open_idmap(const char *path)
    {
        int fd = TEMP_FAILURE_RETRY(open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644));
        if (fd == -1) {
            ALOGD("error: open %s: %s\n", path, strerror(errno));
            goto fail;
        }
        if (fchmod(fd, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) < 0) {
            ALOGD("error: fchmod %s: %s\n", path, strerror(errno));
            goto fail;
        }
        if (TEMP_FAILURE_RETRY(flock(fd, LOCK_EX)) != 0) {
            ALOGD("error: flock %s: %s\n", path, strerror(errno));
            goto fail;
        }

        return fd;
fail:
        if (fd != -1) {
            close(fd);
            unlink(path);
        }
        return -1;
    }

    int write_idmap(int fd, const uint32_t *data, size_t size)
    {
        if (lseek(fd, 0, SEEK_SET) < 0) {
            return -1;
        }
        size_t bytesLeft = size;
        while (bytesLeft > 0) {
            ssize_t w = TEMP_FAILURE_RETRY(write(fd, data + size - bytesLeft, bytesLeft));
            if (w < 0) {
                fprintf(stderr, "error: write: %s\n", strerror(errno));
                return -1;
            }
            bytesLeft -= static_cast<size_t>(w);
        }
        return 0;
    }

    bool is_idmap_stale_fd(const char *target_apk_path, const char *overlay_apk_path, int idmap_fd)
    {
        static const size_t N = ResTable::IDMAP_HEADER_SIZE_BYTES;
        struct stat st;
        if (fstat(idmap_fd, &st) == -1) {
            return true;
        }
        if (st.st_size < static_cast<off_t>(N)) {
            // file is empty or corrupt
            return true;
        }

        char buf[N];
        size_t bytesLeft = N;
        if (lseek(idmap_fd, 0, SEEK_SET) < 0) {
            return true;
        }
        for (;;) {
            ssize_t r = TEMP_FAILURE_RETRY(read(idmap_fd, buf + N - bytesLeft, bytesLeft));
            if (r < 0) {
                return true;
            }
            bytesLeft -= static_cast<size_t>(r);
            if (bytesLeft == 0) {
                break;
            }
            if (r == 0) {
                // "shouldn't happen"
                return true;
            }
        }

        uint32_t version, cached_target_crc, cached_overlay_crc;
        String8 cached_target_path, cached_overlay_path;
        if (!ResTable::getIdmapInfo(buf, N, &version, &cached_target_crc, &cached_overlay_crc,
                    &cached_target_path, &cached_overlay_path)) {
            return true;
        }

        if (version != ResTable::IDMAP_CURRENT_VERSION) {
            return true;
        }

        if (cached_target_path != target_apk_path) {
            return true;
        }
        if (cached_overlay_path != overlay_apk_path) {
            return true;
        }

        uint32_t actual_target_crc, actual_overlay_crc;
        if (get_zip_entry_crc(target_apk_path, AssetManager::RESOURCES_FILENAME,
				&actual_target_crc) == -1) {
            return true;
        }
        if (get_zip_entry_crc(overlay_apk_path, AssetManager::RESOURCES_FILENAME,
				&actual_overlay_crc) == -1) {
            return true;
        }

        return cached_target_crc != actual_target_crc || cached_overlay_crc != actual_overlay_crc;
    }

    bool is_idmap_stale_path(const char *target_apk_path, const char *overlay_apk_path,
            const char *idmap_path)
    {
        struct stat st;
        if (stat(idmap_path, &st) == -1) {
            // non-existing idmap is always stale; on other errors, abort idmap generation
            return errno == ENOENT;
        }

        int idmap_fd = TEMP_FAILURE_RETRY(open(idmap_path, O_RDONLY));
        if (idmap_fd == -1) {
            return false;
        }
        bool is_stale = is_idmap_stale_fd(target_apk_path, overlay_apk_path, idmap_fd);
        close(idmap_fd);
        return is_stale;
    }

    int create_idmap(const char *target_apk_path, const char *overlay_apk_path,
            uint32_t **data, size_t *size)
    {
        uint32_t target_crc, overlay_crc;
        if (get_zip_entry_crc(target_apk_path, AssetManager::RESOURCES_FILENAME,
				&target_crc) == -1) {
            return -1;
        }
        if (get_zip_entry_crc(overlay_apk_path, AssetManager::RESOURCES_FILENAME,
				&overlay_crc) == -1) {
            return -1;
        }

        AssetManager am;
        bool b = am.createIdmap(target_apk_path, overlay_apk_path, target_crc, overlay_crc,
                data, size);
        return b ? 0 : -1;
    }

    int create_and_write_idmap(const char *target_apk_path, const char *overlay_apk_path,
            int fd, bool check_if_stale)
    {
        if (check_if_stale) {
            if (!is_idmap_stale_fd(target_apk_path, overlay_apk_path, fd)) {
                // already up to date -- nothing to do
                return 0;
            }
        }

        uint32_t *data = NULL;
        size_t size;

        if (create_idmap(target_apk_path, overlay_apk_path, &data, &size) == -1) {
            return -1;
        }

        if (write_idmap(fd, data, size) == -1) {
            free(data);
            return -1;
        }

        free(data);
        return 0;
    }
}

int idmap_create_path(const char *target_apk_path, const char *overlay_apk_path,
        const char *idmap_path)
{
    if (!is_idmap_stale_path(target_apk_path, overlay_apk_path, idmap_path)) {
        // already up to date -- nothing to do
        return EXIT_SUCCESS;
    }

    int fd = open_idmap(idmap_path);
    if (fd == -1) {
        return EXIT_FAILURE;
    }

    int r = create_and_write_idmap(target_apk_path, overlay_apk_path, fd, false);
    close(fd);
    if (r != 0) {
        unlink(idmap_path);
    }
    return r == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
}

int idmap_create_fd(const char *target_apk_path, const char *overlay_apk_path, int fd)
{
    return create_and_write_idmap(target_apk_path, overlay_apk_path, fd, true) == 0 ?
        EXIT_SUCCESS : EXIT_FAILURE;
}

int idmap_verify_fd(const char *target_apk_path, const char *overlay_apk_path, int fd)
{
    return !is_idmap_stale_fd(target_apk_path, overlay_apk_path, fd) ?
            EXIT_SUCCESS : EXIT_FAILURE;
}

cmds/idmap/idmap.cpp

deleted100644 → 0
+0 −283
Original line number Diff line number Diff line
#include "idmap.h"

#include <private/android_filesystem_config.h> // for AID_SYSTEM

#include <stdlib.h>
#include <string.h>

namespace {
    const char *usage = "NAME\n\
      idmap - create or display idmap files\n\
\n\
SYNOPSIS \n\
      idmap --help \n\
      idmap --fd target overlay fd \n\
      idmap --path target overlay idmap \n\
      idmap --scan target-package-name-to-look-for path-to-target-apk dir-to-hold-idmaps \\\
                   dir-to-scan [additional-dir-to-scan [additional-dir-to-scan [...]]]\n\
      idmap --inspect idmap \n\
      idmap --verify target overlay fd \n\
\n\
DESCRIPTION \n\
      Idmap files play an integral part in the runtime resource overlay framework. An idmap \n\
      file contains a mapping of resource identifiers between overlay package and its target \n\
      package; this mapping is used during resource lookup. Idmap files also act as control \n\
      files by their existence: if not present, the corresponding overlay package is ignored \n\
      when the resource context is created. \n\
\n\
      Idmap files are stored in /data/resource-cache. For each pair (target package, overlay \n\
      package), there exists exactly one idmap file, or none if the overlay should not be used. \n\
\n\
NOMENCLATURE \n\
      target: the original, non-overlay, package. Each target package may be associated with \n\
              any number of overlay packages. \n\
\n\
      overlay: an overlay package. Each overlay package is associated with exactly one target \n\
               package, specified in the overlay's manifest using the <overlay target=\"...\"/> \n\
               tag. \n\
\n\
OPTIONS \n\
      --help: display this help \n\
\n\
      --fd: create idmap for target package 'target' (path to apk) and overlay package 'overlay' \n\
            (path to apk); write results to file descriptor 'fd' (integer). This invocation \n\
            version is intended to be used by a parent process with higher privileges to call \n\
            idmap in a controlled way: the parent will open a suitable file descriptor, fork, \n\
            drop its privileges and exec. This tool will continue execution without the extra \n\
            privileges, but still have write access to a file it could not have opened on its \n\
            own. \n\
\n\
      --path: create idmap for target package 'target' (path to apk) and overlay package \n\
              'overlay' (path to apk); write results to 'idmap' (path). \n\
\n\
      --scan: non-recursively search directory 'dir-to-scan' (path) for static overlay packages \n\
              with target package 'target-package-name-to-look-for' (package name) present at\n\
              'path-to-target-apk' (path to apk). For each overlay package found, create an\n\
              idmap file in 'dir-to-hold-idmaps' (path). \n\
\n\
      --inspect: decode the binary format of 'idmap' (path) and display the contents in a \n\
                 debug-friendly format. \n\
\n\
      --verify: verify if idmap corresponding to file descriptor 'fd' (integer) is made from \n\
                target package 'target' (path to apk) and overlay package 'overlay'. \n\
\n\
EXAMPLES \n\
      Create an idmap file: \n\
\n\
      $ adb shell idmap --path /system/app/target.apk \\ \n\
                               /vendor/overlay/overlay.apk \\ \n\
                               /data/resource-cache/vendor@overlay@overlay.apk@idmap \n\
\n\
      Display an idmap file: \n\
\n\
      $ adb shell idmap --inspect /data/resource-cache/vendor@overlay@overlay.apk@idmap \n\
      SECTION      ENTRY        VALUE      COMMENT \n\
      IDMAP HEADER magic        0x706d6469 \n\
                   base crc     0xb65a383f \n\
                   overlay crc  0x7b9675e8 \n\
                   base path    .......... /path/to/target.apk \n\
                   overlay path .......... /path/to/overlay.apk \n\
      DATA HEADER  target pkg   0x0000007f \n\
                   types count  0x00000003 \n\
      DATA BLOCK   target type  0x00000002 \n\
                   overlay type 0x00000002 \n\
                   entry count  0x00000001 \n\
                   entry offset 0x00000000 \n\
                   entry        0x00000000 drawable/drawable \n\
      DATA BLOCK   target type  0x00000003 \n\
                   overlay type 0x00000003 \n\
                   entry count  0x00000001 \n\
                   entry offset 0x00000000 \n\
                   entry        0x00000000 xml/integer \n\
      DATA BLOCK   target type  0x00000004 \n\
                   overlay type 0x00000004 \n\
                   entry count  0x00000001 \n\
                   entry offset 0x00000000 \n\
                   entry        0x00000000 raw/lorem_ipsum \n\
\n\
      In this example, the overlay package provides three alternative resource values:\n\
      drawable/drawable, xml/integer, and raw/lorem_ipsum \n\
\n\
NOTES \n\
      This tool and its expected invocation from installd is modelled on dexopt.";

    bool verify_directory_readable(const char *path)
    {
        return access(path, R_OK | X_OK) == 0;
    }

    bool verify_directory_writable(const char *path)
    {
        return access(path, W_OK) == 0;
    }

    bool verify_file_readable(const char *path)
    {
        return access(path, R_OK) == 0;
    }

    bool verify_root_or_system()
    {
        uid_t uid = getuid();
        gid_t gid = getgid();

        return (uid == 0 && gid == 0) || (uid == AID_SYSTEM && gid == AID_SYSTEM);
    }

    int maybe_create_fd(const char *target_apk_path, const char *overlay_apk_path,
            const char *idmap_str)
    {
        // anyone (not just root or system) may do --fd -- the file has
        // already been opened by someone else on our behalf

        char *endptr;
        int idmap_fd = strtol(idmap_str, &endptr, 10);
        if (*endptr != '\0') {
            fprintf(stderr, "error: failed to parse file descriptor argument %s\n", idmap_str);
            return -1;
        }

        if (!verify_file_readable(target_apk_path)) {
            ALOGD("error: failed to read apk %s: %s\n", target_apk_path, strerror(errno));
            return -1;
        }

        if (!verify_file_readable(overlay_apk_path)) {
            ALOGD("error: failed to read apk %s: %s\n", overlay_apk_path, strerror(errno));
            return -1;
        }

        return idmap_create_fd(target_apk_path, overlay_apk_path, idmap_fd);
    }

    int maybe_create_path(const char *target_apk_path, const char *overlay_apk_path,
            const char *idmap_path)
    {
        if (!verify_root_or_system()) {
            fprintf(stderr, "error: permission denied: not user root or user system\n");
            return -1;
        }

        if (!verify_file_readable(target_apk_path)) {
            ALOGD("error: failed to read apk %s: %s\n", target_apk_path, strerror(errno));
            return -1;
        }

        if (!verify_file_readable(overlay_apk_path)) {
            ALOGD("error: failed to read apk %s: %s\n", overlay_apk_path, strerror(errno));
            return -1;
        }

        return idmap_create_path(target_apk_path, overlay_apk_path, idmap_path);
    }

    int maybe_verify_fd(const char *target_apk_path, const char *overlay_apk_path,
            const char *idmap_str)
    {
        char *endptr;
        int idmap_fd = strtol(idmap_str, &endptr, 10);
        if (*endptr != '\0') {
            fprintf(stderr, "error: failed to parse file descriptor argument %s\n", idmap_str);
            return -1;
        }

        if (!verify_file_readable(target_apk_path)) {
            ALOGD("error: failed to read apk %s: %s\n", target_apk_path, strerror(errno));
            return -1;
        }

        if (!verify_file_readable(overlay_apk_path)) {
            ALOGD("error: failed to read apk %s: %s\n", overlay_apk_path, strerror(errno));
            return -1;
        }

        return idmap_verify_fd(target_apk_path, overlay_apk_path, idmap_fd);
    }

    int maybe_scan(const char *target_package_name, const char *target_apk_path,
            const char *idmap_dir, const android::Vector<const char *> *overlay_dirs)
    {
        if (!verify_root_or_system()) {
            fprintf(stderr, "error: permission denied: not user root or user system\n");
            return -1;
        }

        if (!verify_file_readable(target_apk_path)) {
            ALOGD("error: failed to read apk %s: %s\n", target_apk_path, strerror(errno));
            return -1;
        }

        if (!verify_directory_writable(idmap_dir)) {
            ALOGD("error: no write access to %s: %s\n", idmap_dir, strerror(errno));
            return -1;
        }

        const size_t N = overlay_dirs->size();
        for (size_t i = 0; i < N; i++) {
            const char *dir = overlay_dirs->itemAt(i);
            if (!verify_directory_readable(dir)) {
                ALOGD("error: no read access to %s: %s\n", dir, strerror(errno));
                return -1;
            }
        }

        return idmap_scan(target_package_name, target_apk_path, idmap_dir, overlay_dirs);
    }

    int maybe_inspect(const char *idmap_path)
    {
        // anyone (not just root or system) may do --inspect
        if (!verify_file_readable(idmap_path)) {
            ALOGD("error: failed to read idmap %s: %s\n", idmap_path, strerror(errno));
            return -1;
        }
        return idmap_inspect(idmap_path);
    }
}

int main(int argc, char **argv)
{
#if 0
    {
        char buf[1024];
        buf[0] = '\0';
        for (int i = 0; i < argc; ++i) {
            strncat(buf, argv[i], sizeof(buf) - 1);
            strncat(buf, " ", sizeof(buf) - 1);
        }
        ALOGD("%s:%d: uid=%d gid=%d argv=%s\n", __FILE__, __LINE__, getuid(), getgid(), buf);
    }
#endif

    if (argc == 2 && !strcmp(argv[1], "--help")) {
        printf("%s\n", usage);
        return 0;
    }

    if (argc == 5 && !strcmp(argv[1], "--fd")) {
        return maybe_create_fd(argv[2], argv[3], argv[4]);
    }

    if (argc == 5 && !strcmp(argv[1], "--path")) {
        return maybe_create_path(argv[2], argv[3], argv[4]);
    }

    if (argc == 5 && !strcmp(argv[1], "--verify")) {
        return maybe_verify_fd(argv[2], argv[3], argv[4]);
    }

    if (argc >= 6 && !strcmp(argv[1], "--scan")) {
        android::Vector<const char *> v;
        for (int i = 5; i < argc; i++) {
            v.push(argv[i]);
        }
        return maybe_scan(argv[2], argv[3], argv[4], &v);
    }

    if (argc == 3 && !strcmp(argv[1], "--inspect")) {
        return maybe_inspect(argv[2]);
    }

    fprintf(stderr, "Usage: don't use this (cf dexopt usage).\n");
    return EXIT_FAILURE;
}

cmds/idmap/idmap.h

deleted100644 → 0
+0 −38
Original line number Diff line number Diff line

#ifndef _IDMAP_H_
#define _IDMAP_H_

#define LOG_TAG "idmap"

#include <utils/Log.h>
#include <utils/Vector.h>

#include <errno.h>
#include <stdio.h>

#ifndef TEMP_FAILURE_RETRY
// Used to retry syscalls that can return EINTR.
#define TEMP_FAILURE_RETRY(exp) ({         \
    typeof (exp) _rc;                      \
    do {                                   \
        _rc = (exp);                       \
    } while (_rc == -1 && errno == EINTR); \
    _rc; })
#endif

int idmap_create_path(const char *target_apk_path, const char *overlay_apk_path,
        const char *idmap_path);

int idmap_create_fd(const char *target_apk_path, const char *overlay_apk_path, int fd);

int idmap_verify_fd(const char *target_apk_path, const char *overlay_apk_path, int fd);

// Regarding target_package_name: the idmap_scan implementation should
// be able to extract this from the manifest in target_apk_path,
// simplifying the external API.
int idmap_scan(const char *target_package_name, const char *target_apk_path,
        const char *idmap_dir, const android::Vector<const char *> *overlay_dirs);

int idmap_inspect(const char *idmap_path);

#endif // _IDMAP_H_

cmds/idmap/inspect.cpp

deleted100644 → 0
+0 −313

File deleted.

Preview size limit exceeded, changes collapsed.

Loading