#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];
static const char reboot[] = "reboot";
const char* cmd = reboot;
char* optarg = "";
opterr = 0;
do {
int c;
//#include <unistd.h>
//参数argc和argv:
// 通常是从main的参数直接传递而来,argc是参数的数量,argv是一个常量字符串数组的地址
c = getopt(argc, argv, "p");
if (c == -1) {
break;
}
switch (c) {
//adb reboot p 出发关机
case 'p':
cmd = "shutdown";
break;
case '?':
fprintf(stderr, "usage: %s [-p] [rebootcommand]\n", argv[0]);
//exit(0): 正常执行程序并退出程序。
//exit(1): 非正常执行导致退出程序。
//#define EXIT_FAILURE 1
//#define EXIT_SUCCESS 0
exit(EXIT_FAILURE);
}
} while (1);
//int optind:argv的当前索引值。当getopt函数在while循环中使用时,剩下的字符串为操作数,下标从optind到argc-1
//异常处理
if(argc > optind + 1) {
fprintf(stderr, "%s: too many arguments\n", argv[0]);
exit(EXIT_FAILURE);
}
if (argc > optind)
optarg = argv[optind];
if (!optarg || !optarg[0]) optarg = "shell";
prop_len = snprintf(property_val, sizeof(property_val), "%s,%s", cmd, optarg);
if (prop_len >= sizeof(property_val)) {
fprintf(stderr, "%s command too long: %s\n", cmd, optarg);
exit(EXIT_FAILURE);
}
//#define ANDROID_RB_PROPERTY "sys.powerctl"
//对变量sys.powerctl 进行赋值
//property_set 》<cutils/properties.h> 》properties.cpp
ret = property_set(ANDROID_RB_PROPERTY, property_val);
if (ret < 0) {
perror(cmd);
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"
if (cmd == reboot) {
while (1) {
//#include <unistd.h>
// 让进程暂停
pause();
}
}
fprintf(stderr, "Done\n");
return 0;
}
android reboot 1
最新推荐文章于 2021-05-30 11:27:53 发布