sys.powerctl 属性的说明

2018-12-29  本文已影响0人  小磊长江大

1.代码片段1

system/core/init/builtins.c +785

int do_powerctl(int nargs, char **args)
{
    char command[PROP_VALUE_MAX];
    int res; 
    int len = 0; 
    int cmd = 0; 
    char *reboot_target;

    res = expand_props(command, args[1], sizeof(command));
    if (res) {
        ERROR("powerctl: cannot expand '%s'\n", args[1]);
        return -EINVAL;
    }    

    if (strncmp(command, "shutdown", 8) == 0) { 
        cmd = ANDROID_RB_POWEROFF;
        len = 8; 
    } else if (strncmp(command, "reboot", 6) == 0) { 
        cmd = ANDROID_RB_RESTART2;
        len = 6; 
    } else {
        ERROR("powerctl: unrecognized command '%s'\n", command);
        return -EINVAL;
    }    

    if (command[len] == ',') {
        reboot_target = &command[len + 1];
    } else if (command[len] == '\0') {
        reboot_target = "";
    } else {
        ERROR("powerctl: unrecognized reboot target '%s'\n", &command[len]);
        return -EINVAL;
    }    

    return android_reboot(cmd, 0, reboot_target);
}

2.代码片段2

system/core/libcutils/android_reboot.c +104

int android_reboot(int cmd, int flags, char *arg)
{
    int ret,fd;

    sync();
    fd = open("/sys/class/remount/need_remount", O_WRONLY);
    if (fd < 0) {
        return -1; 
    }   
    write(fd, "1", 1); 
    close(fd);
    remount_ro();

    switch (cmd) {
        case ANDROID_RB_RESTART:
            ret = reboot(RB_AUTOBOOT);
            break;

        case ANDROID_RB_POWEROFF:
            ret = reboot(RB_POWER_OFF);
            break;

        case ANDROID_RB_RESTART2:
            ret = __reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2,
                           LINUX_REBOOT_CMD_RESTART2, arg);
            break;

        default:
            ret = -1; 
    }   

    return ret;
}

3.分析说明

首先我们了解

        SystemProperties.set("sys.powerctl", "reboot,silence");

会调用init.rc 中

on property:sys.powerctl=*
powerctl ${sys.powerctl}

即执行

powerctl reboot,silence

查找powerctl命令定义system/core/init/keywords.h +77

KEYWORD(powerctl,    COMMAND, 1, do_powerctl)

即知晓实际调用的是

do_powerctl
上一篇下一篇

猜你喜欢

热点阅读