init_reboot.c 2.2 KB
Newer Older
Z
zhong_ning 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * Copyright (c) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
S
sun_fan 已提交
15
#include "init_reboot.h"
Z
zhong_ning 已提交
16 17 18

#include <stdio.h>
#include <string.h>
Z
zhong_ning 已提交
19 20
#include <sys/types.h>
#include <unistd.h>
Z
zhong_ning 已提交
21
#include "init_log.h"
Z
zhong_ning 已提交
22 23
#include "securec.h"
#include "sys_param.h"
Z
zhong_ning 已提交
24

S
sun_fan 已提交
25
#define SYS_POWER_CTRL "sys.powerctrl="
Z
zhong_ning 已提交
26 27 28
#define MAX_REBOOT_NAME_SIZE  100
#define MAX_REBOOT_VAUE_SIZE  500

S
sun_fan 已提交
29
int DoReboot(const char *cmdContent)
Z
zhong_ning 已提交
30
{
Z
zhong_ning 已提交
31 32 33 34 35 36
    uid_t uid1 = getuid();
    uid_t uid2 = geteuid();
    if (uid1 != 0 || uid2 != 0) {
        INIT_LOGE("uid1=%d, uid2=%d, user MUST be root, error!", uid1, uid2);
        return -1;
    }
S
sun_fan 已提交
37
    char value[MAX_REBOOT_VAUE_SIZE];
38
    if (cmdContent == NULL || strlen(cmdContent) == 0) {
S
sun_fan 已提交
39
        if (snprintf_s(value, MAX_REBOOT_NAME_SIZE, strlen("reboot") + 1, "%s", "reboot") < 0) {
40 41 42 43 44 45 46 47
            INIT_LOGE("DoReboot api error, MAX_REBOOT_NAME_SIZE is not enough");
            return -1;
        }
        if (SystemSetParameter("sys.powerctrl", value) != 0) {
            INIT_LOGE("DoReboot Api SystemSetParameter error");
            return -1;
        }
        return 0;
Z
zhong_ning 已提交
48 49
    }
    int length = strlen(cmdContent);
50
    if (length > MAX_REBOOT_VAUE_SIZE) {
Z
zhong_ning 已提交
51
        INIT_LOGE("DoReboot api error, cmdContent = %s, length = %d.", cmdContent, length);
Z
zhong_ning 已提交
52 53
        return -1;
    }
S
sun_fan 已提交
54
    if (snprintf_s(value, MAX_REBOOT_NAME_SIZE, MAX_REBOOT_NAME_SIZE - 1, "%s%s", "reboot,", cmdContent) < 0) {
Z
zhong_ning 已提交
55
        INIT_LOGE("DoReboot api error, MAX_REBOOT_NAME_SIZE is not enough");
Z
zhong_ning 已提交
56 57
        return -1;
    }
S
sun_fan 已提交
58
    if (SystemSetParameter("sys.powerctrl", value) != 0) {
Z
zhong_ning 已提交
59
        INIT_LOGE("DoReboot Api SystemSetParameter error");
Z
zhong_ning 已提交
60 61 62 63 64
        return -1;
    }
    return 0;
}