android reboot 1

#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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值