1 /*
2 * Copyright (c) 2025 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 <cerrno>
17 #include <sys/mount.h>
18 #include "bootstage.h"
19 #include "init_log.h"
20 #include "init_module_engine.h"
21
HandleModuleUpdate(void)22 static void HandleModuleUpdate(void)
23 {
24 if (mount("tmpfs", "/module_update", "tmpfs", MS_NOEXEC | MS_NODEV | MS_NOSUID, "mode=0755") != 0) {
25 INIT_LOGE("mount module_update tmpfs fail, %s", strerror(errno));
26 }
27 if (mount(nullptr, "/module_update", nullptr, MS_SHARED, nullptr) != 0) {
28 INIT_LOGE("mount module_update shared fail, %s", strerror(errno));
29 }
30 pid_t pid = fork();
31 if (pid < 0) {
32 INIT_LOGE("HandleModuleUpdate, fork fail.");
33 } else if (pid == 0) {
34 INIT_LOGI("HandleModuleUpdate, exec chekc_module_update.");
35 int ret = execv("/system/bin/check_module_update_init", nullptr);
36 if (ret != 0) {
37 INIT_LOGE("exec error, ret = [%d], err = [%s].", ret, strerror(errno));
38 }
39 _exit(0);
40 }
41 }
42
CheckModuleUpdate(const HOOK_INFO * hookInfo,void * ctx)43 static int CheckModuleUpdate(const HOOK_INFO *hookInfo, void *ctx)
44 {
45 INIT_LOGI("check_module_update start.");
46 static bool isExecuted = false;
47 if (isExecuted) {
48 INIT_LOGI("check_module_update has been executed.");
49 return 0;
50 }
51 isExecuted = true;
52 HandleModuleUpdate();
53 return 0;
54 }
55
InitBootCompleted(const HOOK_INFO * hookInfo,void * ctx)56 static int InitBootCompleted(const HOOK_INFO *hookInfo, void *ctx)
57 {
58 INIT_LOGI("boot completed, uninstall module_update_init.");
59 AutorunModuleMgrUnInstall("module_update_init");
60 return 0;
61 }
62
MODULE_CONSTRUCTOR(void)63 MODULE_CONSTRUCTOR(void)
64 {
65 INIT_LOGI("module_update_init add hookMgr.");
66 HookMgrAdd(GetBootStageHookMgr(), INIT_MOUNT_STAGE, 0, CheckModuleUpdate);
67 HookMgrAdd(GetBootStageHookMgr(), INIT_BOOT_COMPLETE, 0, InitBootCompleted);
68 }
69
MODULE_DESTRUCTOR(void)70 MODULE_DESTRUCTOR(void)
71 {
72 INIT_LOGI("module_update_init destroy.");
73 HookMgrDel(GetBootStageHookMgr(), INIT_MOUNT_STAGE, CheckModuleUpdate);
74 HookMgrDel(GetBootStageHookMgr(), INIT_BOOT_COMPLETE, InitBootCompleted);
75 }
76