Файлы System/bin Android 12. Справочник.


  Все     Команда     Скрипт     Служба     Приложение  

run-as - исходный текст
#define PROGNAME  "run-as"
#define LOG_TAG   PROGNAME
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <errno.h>
#include <unistd.h>
#include <time.h>
#include <stdarg.h>
#include <private/android_filesystem_config.h>
#include "package.h"

static void
usage(void)
{
    const char*  str = "Usage: " PROGNAME " <package-name> <command> [<args>]\n\n";
    write(1, str, strlen(str));
    exit(1);
}
static void
panic(const char* format, ...)
{
    va_list args;
    fprintf(stderr, "%s: ", PROGNAME);
    va_start(args, format);
    vfprintf(stderr, format, args);
    va_end(args);
    exit(1);
}
int main(int argc, char **argv)
{
    const char* pkgname;
    int myuid, uid, gid;
    PackageInfo info;
    /* check arguments */
    if (argc < 2)
        usage();
    /* check userid of caller - must be 'shell' or 'root' */
    myuid = getuid();
    if (myuid != AID_SHELL && myuid != AID_ROOT) {
        panic("only 'shell' or 'root' users can run this program\n");
    }
    /* retrieve package information from system */
    pkgname = argv[1];
    if (get_package_info(pkgname, &info) < 0) {
        panic("Package '%s' is unknown\n", pkgname);
        return 1;
    }
    /* reject system packages */
    if (info.uid < AID_APP) {
        panic("Package '%s' is not an application\n", pkgname);
        return 1;
    }
    /* reject any non-debuggable package */
    if (!info.isDebuggable) {
        panic("Package '%s' is not debuggable\n", pkgname);
        return 1;
    }
    /* check that the data directory path is valid */
    if (check_data_path(info.dataDir, info.uid) < 0) {
        panic("Package '%s' has corrupt installation\n", pkgname);
        return 1;
    }
    /* then move to it */
    {
        int ret;
        do {
            ret = chdir(info.dataDir);
        } while (ret < 0 && errno == EINTR);
        if (ret < 0) {
            panic("Could not cd to package's data directory: %s\n", strerror(errno));
            return 1;
        }
    }
    /* Ensure that we change all real/effective/saved IDs at the
     * same time to avoid nasty surprises.
     */
    uid = gid = info.uid;
    if(setresgid(gid,gid,gid) || setresuid(uid,uid,uid)) {
        panic("Permission denied\n");
        return 1;
    }
    /* User specified command for exec. */
    if (argc >= 3 ) {
        if (execvp(argv[2], argv+2) < 0) {
            panic("exec failed for %s Error:%s\n", argv[2], strerror(errno));
            return -errno;
        }
    }
    /* Default exec shell. */
    execlp("/system/bin/sh", "sh", NULL);
    panic("exec failed\n");
    return 1;
}