我们平时在串口操作执行reboot时,系统的重启,与预期效果一致,但是很多人以为是reboot这个bin文件做了实际的动作,但事实是他只是解析了你在reboot后面所追加的参数,发送命令,那么最终是谁执行的呢?
#include
#include
#include
#include
#include
#include
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 *);
具体到系统调用阶段的我们不再赘述。