天天看點

Android Reboot 指令執行過程

我們平時在序列槽操作執行reboot時,系統的重新開機,與預期效果一緻,但是很多人以為是reboot這個bin檔案做了實際的動作,但事實是他隻是解析了你在reboot後面所追加的參數,發送指令,那麼最終是誰執行的呢?

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <cutils/properties.h>
#include <cutils/android_reboot.h>
#include <unistd.h>

int main(int argc, char *argv[])
{
    int ret;
    size_t prop_len;
    char property_val[PROPERTY_VALUE_MAX];
    const char *cmd = "reboot";
    char *optarg = "";

    opterr = 0;
    do {
        int c;

        c = getopt(argc, argv, "p");

        if (c == -1) {
            break;
        }

        switch (c) {
        case 'p':
            cmd = "shutdown";
            break;
        case '?':
            fprintf(stderr, "usage: %s [-p] [rebootcommand]\n", argv[0]);
            exit(EXIT_FAILURE);
        }
    } while (1);

    if(argc > optind + 1) {
        fprintf(stderr, "%s: too many arguments\n", argv[0]);
        exit(EXIT_FAILURE);
    }

    if (argc > optind)
        optarg = argv[optind];

    prop_len = snprintf(property_val, sizeof(property_val), "%s,%s", cmd, optarg);
    if (prop_len >= sizeof(property_val)) {
        fprintf(stderr, "reboot command too long: %s\n", optarg);
        exit(EXIT_FAILURE);
    }

    ret = property_set(ANDROID_RB_PROPERTY, property_val);
    if(ret < 0) {
        perror("reboot");
        exit(EXIT_FAILURE);
    }

    // Don't return early. Give the reboot command time to take effect
    // to avoid messing up scripts which do "adb shell reboot && adb wait-for-device"
    while(1) { pause(); }

    fprintf(stderr, "Done\n");
    return 0;
}
           

以上便是reboot的所有代碼,短小精悍,最重要的代碼:

ret = property_set(ANDROID_RB_PROPERTY, property_val);

這個則是在init程序中觸發的:

on property:sys.powerctl=*

  powerctl ${sys.powerctl}

在init中調用

int do_powerctl(int nargs, char **args)   

return android_reboot(cmd, 0, reboot_target);

然後是:

/system/core/libcutils/android_reboot.c 中的執行android_reboot();

case ANDROID_RB_RESTART2:                                                                                                                       

121            ret = __reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2,

122                            LINUX_REBOOT_CMD_RESTART2, arg); //arg = recovery

這個在bionic/libc/include/sys/reboot.h中定義的。說明這是一個标準的系統調用

extern int __reboot(int, int, int, void *);

具體到系統調用階段的我們不再贅述。

繼續閱讀