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 #define POWEROFF_REASON_DEV_PATH "/proc/poweroff_reason"
33
WritePowerOffReason(const char * reason)34 static int WritePowerOffReason(const char* reason)
35 {
36 PLUGIN_CHECK(reason != NULL, return -1, "WritePowerOffReason: reason is NULL\n");
37 PLUGIN_CHECK(access(POWEROFF_REASON_DEV_PATH, F_OK) == 0, return -1,
38 "WritePowerOffReason: access %s failed, errno = %d, %s\n",
39 POWEROFF_REASON_DEV_PATH, errno, strerror(errno));
40 int fd = open(POWEROFF_REASON_DEV_PATH, O_RDWR);
41 PLUGIN_CHECK(fd > 0, return -1, "WritePowerOffReason: errno = %d, %s\n", errno, strerror(errno));
42 int writeBytes = strlen(reason);
43 int ret = write(fd, reason, writeBytes);
44 PLUGIN_CHECK(ret == writeBytes, writeBytes = -1, "WritePowerOffReason: write poweroff reason failed\n");
45 close(fd);
46 return writeBytes;
47 }
48
ParseRebootReason(const char * name,int argc,const char ** argv)49 static void ParseRebootReason(const char *name, int argc, const char **argv)
50 {
51 char str[BUFF_SIZE] = {0};
52 int len = sizeof(str);
53 char *tmp = str;
54 int ret;
55 for (int i = 0; i < argc; i++) {
56 if (i != argc - 1) {
57 ret = sprintf_s(tmp, len - 1, "%s ", argv[i]);
58 } else {
59 ret = sprintf_s(tmp, len - 1, "%s", argv[i]);
60 }
61 if (ret <= 0) {
62 PLUGIN_LOGW("ParseRebootReason: sprintf_s arg %s failed!", argv[i]);
63 break;
64 }
65 len -= ret;
66 tmp += ret;
67 }
68 ret = WritePowerOffReason(str);
69 PLUGIN_CHECK(ret >= 0, return, "ParseRebootReason: write poweroff reason failed\n");
70 }
71
DoRoot_(const char * jobName,int type)72 PLUGIN_STATIC int DoRoot_(const char *jobName, int type)
73 {
74 // by job to stop service and unmount
75 if (jobName != NULL) {
76 DoJobNow(jobName);
77 }
78 #ifndef STARTUP_INIT_TEST
79 return reboot(type);
80 #else
81 return 0;
82 #endif
83 }
84
DoReboot(int id,const char * name,int argc,const char ** argv)85 static int DoReboot(int id, const char *name, int argc, const char **argv)
86 {
87 UNUSED(id);
88 #ifndef OHOS_LITE
89 ReportStartupReboot(argv[0]);
90 #endif
91 ParseRebootReason(name, argc, argv);
92 // clear misc
93 (void)UpdateMiscMessage(NULL, "reboot", NULL, NULL);
94 return DoRoot_("reboot", RB_AUTOBOOT);
95 }
96
DoRebootPanic(int id,const char * name,int argc,const char ** argv)97 static int DoRebootPanic(int id, const char *name, int argc, const char **argv)
98 {
99 UNUSED(id);
100 #ifndef OHOS_LITE
101 ReportStartupReboot(argv[0]);
102 #endif
103 char str[BUFF_SIZE] = {0};
104 int ret = sprintf_s(str, sizeof(str) - 1, "panic caused by %s:", name);
105 if (ret <= 0) {
106 PLUGIN_LOGW("DoRebootPanic sprintf_s name %s failed!", name);
107 }
108
109 int len = ret > 0 ? (sizeof(str) - ret) : sizeof(str);
110 char *tmp = str + (sizeof(str) - len);
111 for (int i = 0; i < argc; ++i) {
112 ret = sprintf_s(tmp, len - 1, " %s", argv[i]);
113 if (ret <= 0) {
114 PLUGIN_LOGW("DoRebootPanic sprintf_s arg %s failed!", argv[i]);
115 break;
116 } else {
117 len -= ret;
118 tmp += ret;
119 }
120 }
121 PLUGIN_LOGI("DoRebootPanic %s", str);
122 ParseRebootReason(name, argc, argv);
123 if (InRescueMode() == 0) {
124 PLUGIN_LOGI("Don't panic in resuce mode!");
125 return 0;
126 }
127 // clear misc
128 (void)UpdateMiscMessage(NULL, "reboot", NULL, NULL);
129 DoJobNow("reboot");
130 #ifndef STARTUP_INIT_TEST
131 FILE *panic = fopen("/proc/sysrq-trigger", "wb");
132 if (panic == NULL) {
133 return reboot(RB_AUTOBOOT);
134 }
135 if (fwrite((void *)"c", 1, 1, panic) != 1) {
136 (void)fclose(panic);
137 PLUGIN_LOGI("fwrite to panic failed");
138 return -1;
139 }
140 (void)fclose(panic);
141 #endif
142 return 0;
143 }
144
DoRebootShutdown(int id,const char * name,int argc,const char ** argv)145 PLUGIN_STATIC int DoRebootShutdown(int id, const char *name, int argc, const char **argv)
146 {
147 UNUSED(id);
148 PLUGIN_CHECK(argc >= 1, return -1, "Invalid parameter");
149 #ifndef OHOS_LITE
150 ReportStartupReboot(argv[0]);
151 #endif
152 ParseRebootReason(name, argc, argv);
153 // clear misc
154 (void)UpdateMiscMessage(NULL, "reboot", NULL, NULL);
155 const size_t len = strlen("reboot,");
156 const char *cmd = strstr(argv[0], "reboot,");
157 if (cmd != NULL && strlen(cmd) > len) {
158 PLUGIN_LOGI("DoRebootShutdown argv %s", cmd + len);
159 // by job to stop service and unmount
160 DoJobNow("reboot");
161 #ifndef STARTUP_INIT_TEST
162 return syscall(__NR_reboot,
163 LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, LINUX_REBOOT_CMD_POWER_OFF, cmd + len);
164 #else
165 return 0;
166 #endif
167 }
168 return DoRoot_("reboot", RB_POWER_OFF);
169 }
170
DoRebootUpdater(int id,const char * name,int argc,const char ** argv)171 static int DoRebootUpdater(int id, const char *name, int argc, const char **argv)
172 {
173 UNUSED(id);
174 PLUGIN_LOGI("DoRebootUpdater argc %d %s", argc, name);
175 PLUGIN_CHECK(argc >= 1, return -1, "Invalid parameter");
176 PLUGIN_LOGI("DoRebootUpdater argv %s", argv[0]);
177 #ifndef OHOS_LITE
178 ReportStartupReboot(argv[0]);
179 #endif
180 ParseRebootReason(name, argc, argv);
181 int ret = UpdateMiscMessage(argv[0], "updater", "updater:", "boot_updater");
182 if (ret == 0) {
183 return DoRoot_("reboot", RB_AUTOBOOT);
184 }
185 return ret;
186 }
187
DoRebootPenglai(int id,const char * name,int argc,const char ** argv)188 static int DoRebootPenglai(int id, const char *name, int argc, const char **argv)
189 {
190 UNUSED(id);
191 PLUGIN_LOGI("DoRebootPenglai argc %d %s", argc, name);
192 PLUGIN_CHECK(argc >= 1, return -1, "Invalid parameter");
193 PLUGIN_LOGI("DoRebootPenglai argv %s", argv[0]);
194 #ifndef OHOS_LITE
195 ReportStartupReboot(argv[0]);
196 #endif
197 ParseRebootReason(name, argc, argv);
198 int ret = UpdateMiscMessage(argv[0], "penglai", "penglai:", "boot_penglai");
199 if (ret == 0) {
200 return DoRoot_("reboot", RB_AUTOBOOT);
201 }
202 return ret;
203 }
204
DoRebootFlashed(int id,const char * name,int argc,const char ** argv)205 PLUGIN_STATIC int DoRebootFlashed(int id, const char *name, int argc, const char **argv)
206 {
207 UNUSED(id);
208 PLUGIN_LOGI("DoRebootFlashed argc %d %s", argc, name);
209 PLUGIN_CHECK(argc >= 1, return -1, "Invalid parameter");
210 PLUGIN_LOGI("DoRebootFlashd argv %s", argv[0]);
211 #ifndef OHOS_LITE
212 ReportStartupReboot(argv[0]);
213 #endif
214 ParseRebootReason(name, argc, argv);
215 int ret = UpdateMiscMessage(argv[0], "flash", "flash:", "boot_flash");
216 if (ret == 0) {
217 return DoRoot_("reboot", RB_AUTOBOOT);
218 }
219 return ret;
220 }
221
DoRebootCharge(int id,const char * name,int argc,const char ** argv)222 PLUGIN_STATIC int DoRebootCharge(int id, const char *name, int argc, const char **argv)
223 {
224 UNUSED(id);
225 #ifndef OHOS_LITE
226 ReportStartupReboot(argv[0]);
227 #endif
228 ParseRebootReason(name, argc, argv);
229 int ret = UpdateMiscMessage(NULL, "charge", "charge:", "boot_charge");
230 if (ret == 0) {
231 return DoRoot_("reboot", RB_AUTOBOOT);
232 }
233 return ret;
234 }
235
DoRebootSuspend(int id,const char * name,int argc,const char ** argv)236 static int DoRebootSuspend(int id, const char *name, int argc, const char **argv)
237 {
238 UNUSED(id);
239 #ifndef OHOS_LITE
240 ReportStartupReboot(argv[0]);
241 #endif
242 ParseRebootReason(name, argc, argv);
243 return DoRoot_("suspend", RB_AUTOBOOT);
244 }
245
DoRebootOther(int id,const char * name,int argc,const char ** argv)246 PLUGIN_STATIC int DoRebootOther(int id, const char *name, int argc, const char **argv)
247 {
248 UNUSED(id);
249 PLUGIN_CHECK(argc >= 1, return -1, "Invalid parameter argc %d", argc);
250 #ifndef OHOS_LITE
251 ReportStartupReboot(argv[0]);
252 #endif
253 const char *cmd = strstr(argv[0], "reboot,");
254 PLUGIN_CHECK(cmd != NULL, return -1, "Invalid parameter argc %s", argv[0]);
255 PLUGIN_LOGI("DoRebootOther argv %s", argv[0]);
256 ParseRebootReason(name, argc, argv);
257 // clear misc
258 (void)UpdateMiscMessage(NULL, "reboot", NULL, NULL);
259 DoJobNow("reboot");
260 #ifndef STARTUP_INIT_TEST
261 return syscall(__NR_reboot, LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2,
262 LINUX_REBOOT_CMD_RESTART2, cmd + strlen("reboot,"));
263 #else
264 return 0;
265 #endif
266 }
267
RebootAdpInit(void)268 static void RebootAdpInit(void)
269 {
270 // add default reboot cmd
271 (void)AddCmdExecutor("reboot", DoReboot);
272 (void)AddCmdExecutor("reboot.other", DoRebootOther);
273 AddRebootCmdExecutor("shutdown", DoRebootShutdown);
274 AddRebootCmdExecutor("flashd", DoRebootFlashed);
275 AddRebootCmdExecutor("updater", DoRebootUpdater);
276 AddRebootCmdExecutor("charge", DoRebootCharge);
277 AddRebootCmdExecutor("suspend", DoRebootSuspend);
278 AddRebootCmdExecutor("panic", DoRebootPanic);
279 AddRebootCmdExecutor("penglai", DoRebootPenglai);
280 (void)AddCmdExecutor("panic", DoRebootPanic);
281 }
282
MODULE_CONSTRUCTOR(void)283 MODULE_CONSTRUCTOR(void)
284 {
285 PLUGIN_LOGI("Reboot adapter plug-in init now ...");
286 RebootAdpInit();
287 }
288
MODULE_DESTRUCTOR(void)289 MODULE_DESTRUCTOR(void)
290 {
291 PLUGIN_LOGI("Reboot adapter plug-in exit now ...");
292 }
293