美文网首页
sys.powerctl 属性的说明

sys.powerctl 属性的说明

作者: 小磊长江大 | 来源:发表于2018-12-29 17:40 被阅读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
    

    相关文章

      网友评论

          本文标题:sys.powerctl 属性的说明

          本文链接:https://www.haomeiwen.com/subject/abeplqtx.html