• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <unistd.h>
17 #include <linux/reboot.h>
18 #include <sys/reboot.h>
19 #include <sys/syscall.h>
20 
21 #include "reboot_adp.h"
22 #include "init_cmdexecutor.h"
23 #include "init_module_engine.h"
24 #include "init_utils.h"
25 #include "plugin_adapter.h"
26 #include "securec.h"
27 #ifndef OHOS_LITE
28 #include "init_hisysevent.h"
29 #endif
30 
31 #define BUFF_SIZE 256
32 
DoRoot_(const char * jobName,int type)33 PLUGIN_STATIC int DoRoot_(const char *jobName, int type)
34 {
35     // by job to stop service and unmount
36     if (jobName != NULL) {
37         DoJobNow(jobName);
38     }
39 #ifndef STARTUP_INIT_TEST
40     return reboot(type);
41 #else
42     return 0;
43 #endif
44 }
45 
DoReboot(int id,const char * name,int argc,const char ** argv)46 static int DoReboot(int id, const char *name, int argc, const char **argv)
47 {
48     UNUSED(id);
49     UNUSED(name);
50     UNUSED(argc);
51     UNUSED(argv);
52 #ifndef OHOS_LITE
53     ReportStartupReboot(argv[0]);
54 #endif
55     // clear misc
56     (void)UpdateMiscMessage(NULL, "reboot", NULL, NULL);
57     return DoRoot_("reboot", RB_AUTOBOOT);
58 }
59 
DoRebootPanic(int id,const char * name,int argc,const char ** argv)60 static int DoRebootPanic(int id, const char *name, int argc, const char **argv)
61 {
62     UNUSED(id);
63 #ifndef OHOS_LITE
64     ReportStartupReboot(argv[0]);
65 #endif
66     char str[BUFF_SIZE] = {0};
67     int ret = sprintf_s(str, sizeof(str) - 1, "panic caused by %s:", name);
68     if (ret <= 0) {
69         PLUGIN_LOGW("DoRebootPanic sprintf_s name %s failed!", name);
70     }
71 
72     int len = ret > 0 ? (sizeof(str) - ret) : sizeof(str);
73     char *tmp = str + (sizeof(str) - len);
74     for (int i = 0; i < argc; ++i) {
75         ret = sprintf_s(tmp, len - 1, " %s", argv[i]);
76         if (ret <= 0) {
77             PLUGIN_LOGW("DoRebootPanic sprintf_s arg %s failed!", argv[i]);
78             break;
79         } else {
80             len -= ret;
81             tmp += ret;
82         }
83     }
84     PLUGIN_LOGI("DoRebootPanic %s", str);
85     if (InRescueMode() == 0) {
86         PLUGIN_LOGI("Don't panic in resuce mode!");
87         return 0;
88     }
89     // clear misc
90     (void)UpdateMiscMessage(NULL, "reboot", NULL, NULL);
91     DoJobNow("reboot");
92 #ifndef STARTUP_INIT_TEST
93     FILE *panic = fopen("/proc/sysrq-trigger", "wb");
94     if (panic == NULL) {
95         return reboot(RB_AUTOBOOT);
96     }
97     if (fwrite((void *)"c", 1, 1, panic) != 1) {
98         (void)fclose(panic);
99         PLUGIN_LOGI("fwrite to panic failed");
100         return -1;
101     }
102     (void)fclose(panic);
103 #endif
104     return 0;
105 }
106 
DoRebootShutdown(int id,const char * name,int argc,const char ** argv)107 PLUGIN_STATIC int DoRebootShutdown(int id, const char *name, int argc, const char **argv)
108 {
109     UNUSED(id);
110     UNUSED(name);
111     UNUSED(argc);
112     UNUSED(argv);
113     PLUGIN_CHECK(argc >= 1, return -1, "Invalid parameter");
114 #ifndef OHOS_LITE
115     ReportStartupReboot(argv[0]);
116 #endif
117     // clear misc
118     (void)UpdateMiscMessage(NULL, "reboot", NULL, NULL);
119     const size_t len = strlen("reboot,");
120     const char *cmd = strstr(argv[0], "reboot,");
121     if (cmd != NULL && strlen(cmd) > len) {
122         PLUGIN_LOGI("DoRebootShutdown argv %s", cmd + len);
123         // by job to stop service and unmount
124         DoJobNow("reboot");
125 #ifndef STARTUP_INIT_TEST
126         return syscall(__NR_reboot,
127             LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, LINUX_REBOOT_CMD_POWER_OFF, cmd + len);
128 #else
129         return 0;
130 #endif
131     }
132     return DoRoot_("reboot", RB_POWER_OFF);
133 }
134 
DoRebootUpdater(int id,const char * name,int argc,const char ** argv)135 static int DoRebootUpdater(int id, const char *name, int argc, const char **argv)
136 {
137     UNUSED(id);
138     PLUGIN_LOGI("DoRebootUpdater argc %d %s", argc, name);
139     PLUGIN_CHECK(argc >= 1, return -1, "Invalid parameter");
140     PLUGIN_LOGI("DoRebootUpdater argv %s", argv[0]);
141 #ifndef OHOS_LITE
142     ReportStartupReboot(argv[0]);
143 #endif
144     int ret = UpdateMiscMessage(argv[0], "updater", "updater:", "boot_updater");
145     if (ret == 0) {
146         return DoRoot_("reboot", RB_AUTOBOOT);
147     }
148     return ret;
149 }
150 
DoRebootFlashed(int id,const char * name,int argc,const char ** argv)151 PLUGIN_STATIC int DoRebootFlashed(int id, const char *name, int argc, const char **argv)
152 {
153     UNUSED(id);
154     PLUGIN_LOGI("DoRebootFlashed argc %d %s", argc, name);
155     PLUGIN_CHECK(argc >= 1, return -1, "Invalid parameter");
156     PLUGIN_LOGI("DoRebootFlashd argv %s", argv[0]);
157 #ifndef OHOS_LITE
158     ReportStartupReboot(argv[0]);
159 #endif
160     int ret = UpdateMiscMessage(argv[0], "flash", "flash:", "boot_flash");
161     if (ret == 0) {
162         return DoRoot_("reboot", RB_AUTOBOOT);
163     }
164     return ret;
165 }
166 
DoRebootCharge(int id,const char * name,int argc,const char ** argv)167 PLUGIN_STATIC int DoRebootCharge(int id, const char *name, int argc, const char **argv)
168 {
169     UNUSED(id);
170     UNUSED(name);
171     UNUSED(argc);
172     UNUSED(argv);
173 #ifndef OHOS_LITE
174     ReportStartupReboot(argv[0]);
175 #endif
176     int ret = UpdateMiscMessage(NULL, "charge", "charge:", "boot_charge");
177     if (ret == 0) {
178         return DoRoot_("reboot", RB_AUTOBOOT);
179     }
180     return ret;
181 }
182 
DoRebootSuspend(int id,const char * name,int argc,const char ** argv)183 static int DoRebootSuspend(int id, const char *name, int argc, const char **argv)
184 {
185     UNUSED(id);
186     UNUSED(name);
187     UNUSED(argc);
188     UNUSED(argv);
189 #ifndef OHOS_LITE
190     ReportStartupReboot(argv[0]);
191 #endif
192     return DoRoot_("suspend", RB_AUTOBOOT);
193 }
194 
DoRebootOther(int id,const char * name,int argc,const char ** argv)195 PLUGIN_STATIC int DoRebootOther(int id, const char *name, int argc, const char **argv)
196 {
197     UNUSED(id);
198     UNUSED(name);
199     PLUGIN_CHECK(argc >= 1, return -1, "Invalid parameter argc %d", argc);
200 #ifndef OHOS_LITE
201     ReportStartupReboot(argv[0]);
202 #endif
203     const char *cmd = strstr(argv[0], "reboot,");
204     PLUGIN_CHECK(cmd != NULL, return -1, "Invalid parameter argc %s", argv[0]);
205     PLUGIN_LOGI("DoRebootOther argv %s", argv[0]);
206     DoJobNow("reboot");
207 #ifndef STARTUP_INIT_TEST
208     return syscall(__NR_reboot, LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2,
209         LINUX_REBOOT_CMD_RESTART2, cmd + strlen("reboot,"));
210 #else
211     return 0;
212 #endif
213 }
214 
RebootAdpInit(void)215 static void RebootAdpInit(void)
216 {
217     // add default reboot cmd
218     (void)AddCmdExecutor("reboot", DoReboot);
219     (void)AddCmdExecutor("reboot.other", DoRebootOther);
220     AddRebootCmdExecutor("shutdown", DoRebootShutdown);
221     AddRebootCmdExecutor("flashd", DoRebootFlashed);
222     AddRebootCmdExecutor("updater", DoRebootUpdater);
223     AddRebootCmdExecutor("charge", DoRebootCharge);
224     AddRebootCmdExecutor("suspend", DoRebootSuspend);
225     AddRebootCmdExecutor("panic", DoRebootPanic);
226     (void)AddCmdExecutor("panic", DoRebootPanic);
227 }
228 
MODULE_CONSTRUCTOR(void)229 MODULE_CONSTRUCTOR(void)
230 {
231     PLUGIN_LOGI("Reboot adapter plug-in init now ...");
232     RebootAdpInit();
233 }
234 
MODULE_DESTRUCTOR(void)235 MODULE_DESTRUCTOR(void)
236 {
237     PLUGIN_LOGI("Reboot adapter plug-in exit now ...");
238 }
239