reboot.c 4.3 KB
Newer Older
C
cheng_jinsong 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * Copyright (c) 2022 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.
 */
15 16 17

#include <unistd.h>
#include <linux/reboot.h>
C
cheng_jinsong 已提交
18
#include <sys/reboot.h>
19
#include <sys/syscall.h>
C
cheng_jinsong 已提交
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34

#include "reboot_adp.h"
#include "init_cmdexecutor.h"
#include "init_module_engine.h"
#include "plugin_adapter.h"
#include "securec.h"

static int DoRoot_(const char *jobName, int type)
{
    // by job to stop service and unmount
    if (jobName != NULL) {
        DoJobNow(jobName);
    }
#ifndef STARTUP_INIT_TEST
    return reboot(type);
C
cheng_jinsong 已提交
35 36
#else
    return 0;
C
cheng_jinsong 已提交
37 38 39 40 41
#endif
}

static int DoReboot(int id, const char *name, int argc, const char **argv)
{
C
cheng_jinsong 已提交
42 43 44 45
    UNUSED(id);
    UNUSED(name);
    UNUSED(argc);
    UNUSED(argv);
C
cheng_jinsong 已提交
46 47 48 49 50 51 52
    // clear misc
    (void)UpdateMiscMessage(NULL, "reboot", NULL, NULL);
    return DoRoot_("reboot", RB_AUTOBOOT);
}

static int DoRebootShutdown(int id, const char *name, int argc, const char **argv)
{
C
cheng_jinsong 已提交
53 54 55 56
    UNUSED(id);
    UNUSED(name);
    UNUSED(argc);
    UNUSED(argv);
C
cheng_jinsong 已提交
57 58 59 60 61 62 63
    // clear misc
    (void)UpdateMiscMessage(NULL, "reboot", NULL, NULL);
    return DoRoot_("reboot", RB_POWER_OFF);
}

static int DoRebootUpdater(int id, const char *name, int argc, const char **argv)
{
C
cheng_jinsong 已提交
64
    UNUSED(id);
C
cheng_jinsong 已提交
65 66 67 68 69 70 71 72 73 74 75 76
    PLUGIN_LOGI("DoRebootUpdater argc %d %s", argc, name);
    PLUGIN_CHECK(argc >= 1, return -1, "Invalid parameter");
    PLUGIN_LOGI("DoRebootUpdater argv %s", argv[0]);
    int ret = UpdateMiscMessage(argv[0], "updater", "updater:", "boot_updater");
    if (ret == 0) {
        return DoRoot_("reboot", RB_AUTOBOOT);
    }
    return ret;
}

static int DoRebootFlashed(int id, const char *name, int argc, const char **argv)
{
C
cheng_jinsong 已提交
77
    UNUSED(id);
C
cheng_jinsong 已提交
78 79 80 81 82 83 84 85 86 87 88 89
    PLUGIN_LOGI("DoRebootFlashed argc %d %s", argc, name);
    PLUGIN_CHECK(argc >= 1, return -1, "Invalid parameter");
    PLUGIN_LOGI("DoRebootFlashd argv %s", argv[0]);
    int ret = UpdateMiscMessage(argv[0], "flash", "flash:", "boot_flash");
    if (ret == 0) {
        return DoRoot_("reboot", RB_AUTOBOOT);
    }
    return ret;
}

static int DoRebootCharge(int id, const char *name, int argc, const char **argv)
{
C
cheng_jinsong 已提交
90 91 92 93
    UNUSED(id);
    UNUSED(name);
    UNUSED(argc);
    UNUSED(argv);
C
cheng_jinsong 已提交
94 95 96 97 98 99 100
    int ret = UpdateMiscMessage(NULL, "charge", "charge:", "boot_charge");
    if (ret == 0) {
        return DoRoot_("reboot", RB_AUTOBOOT);
    }
    return ret;
}

C
cheng_jinsong 已提交
101
static int DoRebootSuspend(int id, const char *name, int argc, const char **argv)
C
cheng_jinsong 已提交
102
{
C
cheng_jinsong 已提交
103 104 105 106 107
    UNUSED(id);
    UNUSED(name);
    UNUSED(argc);
    UNUSED(argv);
    return DoRoot_("suspend", RB_AUTOBOOT);
C
cheng_jinsong 已提交
108 109
}

110 111 112 113 114 115 116 117 118 119 120
static int DoRebootOther(int id, const char *name, int argc, const char **argv)
{
    UNUSED(id);
    UNUSED(name);
    PLUGIN_CHECK(argc >= 1, return -1, "Invalid parameter argc %d", argc);
    const char *cmd = strstr(argv[0], "reboot,");
    PLUGIN_CHECK(cmd != NULL, return -1, "Invalid parameter argc %s", argv[0]);
    PLUGIN_LOGI("DoRebootOther argv %s", argv[0]);
    return syscall(__NR_reboot, LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, LINUX_REBOOT_CMD_RESTART2, cmd + strlen("reboot,"));
}

C
cheng_jinsong 已提交
121 122
static void RebootAdpInit(void)
{
C
cheng_jinsong 已提交
123 124 125
    // sample {"reboot,shutdown", "reboot.shutdown", "reboot.shutdown"},
    // add default reboot cmd
    (void)AddCmdExecutor("reboot", DoReboot);
126
    (void)AddCmdExecutor("reboot.other", DoRebootOther);
C
cheng_jinsong 已提交
127 128 129 130 131
    AddRebootCmdExecutor("shutdown", DoRebootShutdown);
    AddRebootCmdExecutor("flashd", DoRebootFlashed);
    AddRebootCmdExecutor("updater", DoRebootUpdater);
    AddRebootCmdExecutor("charge", DoRebootCharge);
    AddRebootCmdExecutor("suspend", DoRebootSuspend);
C
cheng_jinsong 已提交
132 133 134 135
}

MODULE_CONSTRUCTOR(void)
{
C
fix log  
cheng_jinsong 已提交
136
    PLUGIN_LOGI("Reboot adapter plug-in init now ...");
C
cheng_jinsong 已提交
137 138 139 140 141
    RebootAdpInit();
}

MODULE_DESTRUCTOR(void)
{
C
fix log  
cheng_jinsong 已提交
142
    PLUGIN_LOGI("Reboot adapter plug-in exit now ...");
C
cheng_jinsong 已提交
143
}