提交 3900746c 编写于 作者: Z zhong_ning

updater_start_l1

上级 38b39d75
...@@ -10,60 +10,110 @@ ...@@ -10,60 +10,110 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
import("//build/lite/config/component/lite_component.gni") if (defined(ohos_lite)) {
import("//build/lite/config/component/lite_component.gni")
lite_component("init_lite") { lite_component("init_lite") {
features = [ ":init" ] features = [ ":init" ]
} }
# feature: init # feature: init
executable("init") { executable("init") {
defines = [ defines = [
"_GNU_SOURCE", #syscall function need this macro definition "_GNU_SOURCE", #syscall function need this macro definition
] "OHOS_LITE=1",
sources = [ ]
"src/init_adapter.c", sources = [
"src/init_cmds.c", "src/init_adapter.c",
"src/init_jobs.c", "src/init_cmds.c",
"src/init_read_cfg.c", "src/init_jobs.c",
"src/init_service.c", "src/init_read_cfg.c",
"src/init_service_manager.c", "src/init_service.c",
"src/init_signal_handler.c", "src/init_service_manager.c",
"src/main.c", "src/init_signal_handler.c",
] "src/main.c",
]
include_dirs = [ include_dirs = [
"include", "include",
"//third_party/cJSON", "//third_party/cJSON",
"//third_party/bounds_checking_function/include", "//third_party/bounds_checking_function/include",
"//base/startup/syspara_lite/interfaces/kits", "//base/startup/syspara_lite/interfaces/kits",
] ]
cflags = [ "-Wall" ] cflags = [ "-Wall" ]
deps = [ deps = [
"//base/startup/syspara_lite/frameworks/parameter:parameter", "//base/startup/syspara_lite/frameworks/parameter:parameter",
"//build/lite/config/component/cJSON:cjson_shared", "//build/lite/config/component/cJSON:cjson_shared",
"//third_party/bounds_checking_function:libsec_shared", "//third_party/bounds_checking_function:libsec_shared",
] ]
ldflags = []
if (ohos_kernel_type == "liteos_a") {
include_dirs += [ "//kernel/liteos_a/syscall" ]
}
if (ohos_kernel_type == "linux") {
defines += [ "NEED_EXEC_RCS_LINUX" ]
ldflags += [
"-lm",
"-lpthread",
]
}
}
ldflags = [] if (ohos_build_type == "debug") {
group("unittest") {
deps = [ "//base/startup/init_lite/services/test/unittest/common:unittest" ]
}
}
} else {
import("//build/ohos.gni")
if (ohos_kernel_type == "liteos_a") { ohos_executable("updaterueventd") {
include_dirs += [ "//kernel/liteos_a/syscall" ] sources = [
"src/uevent.c",
"src/list.c",
]
include_dirs = [
"include",
"//third_party/bounds_checking_function/include",
]
deps = [
"//third_party/bounds_checking_function:libsec_static",
]
install_enable = true
part_name = "updater"
} }
if (ohos_kernel_type == "linux") { ohos_executable("updaterinit") {
defines += [ "NEED_EXEC_RCS_LINUX" ] sources = [
ldflags += [ "src/main.c",
"-lm", "src/init_cmds.c",
"-lpthread", "src/init_jobs.c",
] "src/init_read_cfg.c",
"src/init_adapter.c",
"src/init_service.c",
"src/init_service_manager.c",
"src/init_signal_handler.c",
"src/device.c",
]
defines = [ "OHOS_LITE=0" ]
include_dirs = [
"include",
"//third_party/cJSON",
"//third_party/bounds_checking_function/include",
]
deps = [
"//third_party/bounds_checking_function:libsec_static",
"//third_party/cJSON:cjson_static",
]
install_enable = true
part_name = "updater"
} }
}
if (ohos_build_type == "debug") { ohos_prebuilt_etc("init.cfg") {
group("unittest") { source = "//device/hisilicon/hi3516dv300/updater/init.cfg"
deps = [ "//base/startup/init_lite/services/test/unittest/common:unittest" ] relative_install_dir = "init"
subsystem_name = "updater"
} }
} }
/*
* Copyright (c) 2020 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.
*/
#ifndef BASE_STARTUP_INITLITE_DEVICE_H
#define BASE_STARTUP_INITLITE_DEVICE_H
#ifdef __cplusplus
#if __cplusplus
extern "C" {
#endif
#endif
void MountBasicFs();
void CreateDeviceNode();
#ifdef __cplusplus
#if __cplusplus
}
#endif
#endif
#endif // BASE_STARTUP_INITLITE_DEVICE_H
/*
* Copyright (c) 2020 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.
*/
#ifndef BASE_STARTUP_INITLITE_LIST_H
#define BASE_STARTUP_INITLITE_LIST_H
#ifdef __cplusplus
#if __cplusplus
extern "C" {
#endif
#endif
#include <stddef.h>
struct ListNode {
struct ListNode *next;
struct ListNode *prev;
};
void ListInit(struct ListNode *list);
void ListAddTail(struct ListNode *list, struct ListNode *item);
void ListRemove(struct ListNode *item);
#ifdef __cplusplus
#if __cplusplus
}
#endif
#endif
#endif // BASE_STARTUP_INITLITE_LIST_H
/*
* Copyright (c) 2020 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.
*/
#include <sys/sysmacros.h>
#include <sys/stat.h>
#include <sys/mount.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
void MountBasicFs()
{
if (mount("tmpfs", "/dev", "tmpfs", MS_NOSUID, "mode=0755") != 0) {
printf("Mount tmpfs failed. %s\n", strerror(errno));
}
if (mount("proc", "/proc", "proc", 0, "hidepid=2") != 0) {
printf("Mount procfs failed. %s\n", strerror(errno));
}
if (mount("sysfs", "/sys", "sysfs", 0, NULL) != 0) {
printf("Mount sysfs failed. %s\n", strerror(errno));
}
}
void CreateDeviceNode()
{
if (mknod("/dev/kmsg", S_IFCHR | 0600, makedev(1, 11)) != 0) {
printf("Create /dev/kmsg device node failed. %s\n", strerror(errno));
}
if (mknod("/dev/null", S_IFCHR | 0666, makedev(1, 3)) != 0) {
printf("Create /dev/null device node failed. %s\n", strerror(errno));
}
if (mknod("/dev/random", S_IFCHR | 0666, makedev(1, 8)) != 0) {
printf("Create /dev/random device node failed. %s\n", strerror(errno));
}
if (mknod("/dev/urandom", S_IFCHR | 0666, makedev(1, 9)) != 0) {
printf("Create /dev/urandom device node failed. %s\n", strerror(errno));
}
}
...@@ -24,11 +24,14 @@ ...@@ -24,11 +24,14 @@
#include <sys/mount.h> #include <sys/mount.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <unistd.h> #include <unistd.h>
#if !OHOS_LITE
#include <sys/syscall.h>
#include <fcntl.h>
#include <linux/module.h>
#endif
#include "init_service_manager.h" #include "init_service_manager.h"
#include "securec.h" #include "securec.h"
#define MODE_LEN 4 // for chmod mode, format 0xxx #define MODE_LEN 4 // for chmod mode, format 0xxx
#define DEFAULT_DIR_MODE 0755 // mkdir, default mode #define DEFAULT_DIR_MODE 0755 // mkdir, default mode
#define SPACES_CNT_IN_CMD_MAX 10 // mount, max number of spaces in cmdline #define SPACES_CNT_IN_CMD_MAX 10 // mount, max number of spaces in cmdline
...@@ -48,6 +51,7 @@ static const char* g_supportedCmds[] = { ...@@ -48,6 +51,7 @@ static const char* g_supportedCmds[] = {
"chown ", "chown ",
"mount ", "mount ",
"loadcfg ", "loadcfg ",
"insmod ",
}; };
void ParseCmdLine(const char* cmdStr, CmdLine* resCmd) void ParseCmdLine(const char* cmdStr, CmdLine* resCmd)
...@@ -172,14 +176,14 @@ static void DoChown(const char* cmdContent) ...@@ -172,14 +176,14 @@ static void DoChown(const char* cmdContent)
static char* CopySubStr(const char* srcStr, size_t startPos, size_t endPos) static char* CopySubStr(const char* srcStr, size_t startPos, size_t endPos)
{ {
if (endPos <= startPos) { if (endPos <= startPos) {
printf("[Init] DoMount, invalid params<%lu, %lu> for %s.\n", endPos, startPos, srcStr); printf("[Init] DoMount, invalid params<%zu, %zu> for %s.\n", endPos, startPos, srcStr);
return NULL; return NULL;
} }
size_t mallocLen = endPos - startPos + 1; size_t mallocLen = endPos - startPos + 1;
char* retStr = (char*)malloc(mallocLen); char* retStr = (char*)malloc(mallocLen);
if (retStr == NULL) { if (retStr == NULL) {
printf("[Init] DoMount, malloc failed! malloc size %lu, for %s.\n", mallocLen, srcStr); printf("[Init] DoMount, malloc failed! malloc size %zu, for %s.\n", mallocLen, srcStr);
return NULL; return NULL;
} }
...@@ -316,6 +320,86 @@ static void DoMount(const char* cmdContent) ...@@ -316,6 +320,86 @@ static void DoMount(const char* cmdContent)
free(target); free(target);
} }
#if !OHOS_LITE
// format insmod <ko name> [-f] [options]
static void DoInsmod(const char *cmdContent)
{
#define OPTIONS_SIZE (128u)
char *p = NULL;
char *line = NULL;
char *restPtr = NULL;
char *fileName = NULL;
int flags = 0;
int fd = -1;
char options[OPTIONS_SIZE] = {0};
size_t count = strlen(cmdContent);
if (count > OPTIONS_SIZE) {
printf("[Init], options too long, maybe lost some of options\n");
}
line = (char *)malloc(count + 1);
if (line == NULL) {
printf("[Init] Allocate memory failed.\n");
return;
}
if (memcpy_s(line, count, cmdContent, count) != EOK) {
printf("[Init] memcpy failed\n");
}
line[count] = '\0';
do {
if ((p = strtok_r(line, " ", &restPtr)) == NULL) {
printf("[Init] debug, cannot get filename\n");
free(line);
return;
}
fileName = p;
printf("[Init] debug, fileName is [%s]\n", fileName);
if ((p = strtok_r(NULL, " ", &restPtr)) == NULL) {
break;
}
if (!strcmp(p, "-f")) {
flags = MODULE_INIT_IGNORE_VERMAGIC | MODULE_INIT_IGNORE_MODVERSIONS;
}
} while (0);
if (flags != 0) { // '-f' option
p = restPtr; // grab all rest of contents.
} else { // no '-f' option, should combine p and resetPtr
if (p != NULL) {
if (restPtr != NULL) {
if (snprintf_s(options, sizeof(options), OPTIONS_SIZE -1, "%s %s", p, restPtr) == -1) {
return;
}
} else {
if (strncpy_s(options, OPTIONS_SIZE - 1, p, strlen(p)) != 0) {
return;
}
}
}
}
// Open ko files
fd = open(fileName, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
if (fd < 0) {
printf("[Init] failed to open %s: %d\n", fileName, errno);
goto out;
}
int rc = syscall(__NR_finit_module, fd, options, flags);
if (rc == -1) {
printf("[Init] finit_module for %s failed: %d\n", fileName, errno);
}
out:
if (fd > 0) {
close(fd);
}
if (line != NULL) {
free(line);
}
}
#endif
static bool CheckValidCfg(const char *path) static bool CheckValidCfg(const char *path)
{ {
size_t cfgCnt = sizeof(g_supportCfg) / sizeof(g_supportCfg[0]); size_t cfgCnt = sizeof(g_supportCfg) / sizeof(g_supportCfg[0]);
...@@ -402,7 +486,13 @@ void DoCmd(const CmdLine* curCmd) ...@@ -402,7 +486,13 @@ void DoCmd(const CmdLine* curCmd)
DoMount(curCmd->cmdContent); DoMount(curCmd->cmdContent);
} else if (strncmp(curCmd->name, "loadcfg ", strlen("loadcfg ")) == 0) { } else if (strncmp(curCmd->name, "loadcfg ", strlen("loadcfg ")) == 0) {
DoLoadCfg(curCmd->cmdContent); DoLoadCfg(curCmd->cmdContent);
} else { }
#if !OHOS_LITE
else if (strncmp(curCmd->name, "insmod ", strlen("insmod ")) == 0) {
DoInsmod(curCmd->cmdContent);
}
#endif
else {
printf("[Init] DoCmd, unknown cmd name %s.\n", curCmd->name); printf("[Init] DoCmd, unknown cmd name %s.\n", curCmd->name);
} }
} }
......
...@@ -64,7 +64,7 @@ static int SetPerms(const Service *service) ...@@ -64,7 +64,7 @@ static int SetPerms(const Service *service)
capHeader.version = _LINUX_CAPABILITY_VERSION_3; capHeader.version = _LINUX_CAPABILITY_VERSION_3;
capHeader.pid = 0; capHeader.pid = 0;
struct __user_cap_data_struct capData[CAP_NUM] = {0}; struct __user_cap_data_struct capData[CAP_NUM] = {};
for (unsigned int i = 0; i < service->servPerm.capsCnt; ++i) { for (unsigned int i = 0; i < service->servPerm.capsCnt; ++i) {
if (service->servPerm.caps[i] == FULL_CAP) { if (service->servPerm.caps[i] == FULL_CAP) {
for (int i = 0; i < CAP_NUM; ++i) { for (int i = 0; i < CAP_NUM; ++i) {
......
...@@ -66,7 +66,7 @@ void StartServiceByName(const char* servName) ...@@ -66,7 +66,7 @@ void StartServiceByName(const char* servName)
void StopAllServices() void StopAllServices()
{ {
for (size_t i = 0; i < g_servicesCnt; i++) { for (int i = 0; i < g_servicesCnt; i++) {
if (ServiceStop(&g_services[i]) != SERVICE_SUCCESS) { if (ServiceStop(&g_services[i]) != SERVICE_SUCCESS) {
printf("[Init] StopAllServices, service %s stop failed!\n", g_services[i].name); printf("[Init] StopAllServices, service %s stop failed!\n", g_services[i].name);
} }
...@@ -75,7 +75,7 @@ void StopAllServices() ...@@ -75,7 +75,7 @@ void StopAllServices()
void ReapServiceByPID(int pid) void ReapServiceByPID(int pid)
{ {
for (size_t i = 0; i < g_servicesCnt; i++) { for (int i = 0; i < g_servicesCnt; i++) {
if (g_services[i].pid == pid) { if (g_services[i].pid == pid) {
if (g_services[i].attribute & SERVICE_ATTR_IMPORTANT) { if (g_services[i].attribute & SERVICE_ATTR_IMPORTANT) {
// important process exit, need to reboot system // important process exit, need to reboot system
......
/*
* Copyright (c) 2020 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.
*/
#include "list.h"
void ListInit(struct ListNode *node)
{
node->next = node;
node->prev = node;
}
void ListAddTail(struct ListNode *head, struct ListNode *item)
{
item->next = head;
item->prev = head->prev;
head->prev->next = item;
head->prev = item;
}
void ListRemove(struct ListNode *item)
{
item->next->prev = item->prev;
item->prev->next = item->next;
}
...@@ -27,12 +27,19 @@ ...@@ -27,12 +27,19 @@
#include "init_adapter.h" #include "init_adapter.h"
#include "init_read_cfg.h" #include "init_read_cfg.h"
#include "init_signal_handler.h" #include "init_signal_handler.h"
#if OHOS_LITE
#include "parameter.h" #include "parameter.h"
#endif
#if !OHOS_LITE
#include "device.h"
#endif
static const pid_t INIT_PROCESS_PID = 1; static const pid_t INIT_PROCESS_PID = 1;
static void PrintSysInfo() static void PrintSysInfo()
{ {
#if OHOS_LITE
char* sysInfo = GetVersionId(); char* sysInfo = GetVersionId();
if (sysInfo != NULL) { if (sysInfo != NULL) {
printf("[Init] %s\n", sysInfo); printf("[Init] %s\n", sysInfo);
...@@ -41,6 +48,7 @@ static void PrintSysInfo() ...@@ -41,6 +48,7 @@ static void PrintSysInfo()
return; return;
} }
printf("[Init] main, GetVersionId failed!\n"); printf("[Init] main, GetVersionId failed!\n");
#endif
} }
#ifdef OHOS_DEBUG #ifdef OHOS_DEBUG
...@@ -72,7 +80,13 @@ int main(int argc, char * const argv[]) ...@@ -72,7 +80,13 @@ int main(int argc, char * const argv[])
// 1. print system info // 1. print system info
PrintSysInfo(); PrintSysInfo();
// 2. signal register #if !OHOS_LITE
// 2. Mount basic filesystem and create common device node.
MountBasicFs();
CreateDeviceNode();
#endif
// 3. signal register
SignalInitModule(); SignalInitModule();
#ifdef OHOS_DEBUG #ifdef OHOS_DEBUG
...@@ -82,7 +96,7 @@ int main(int argc, char * const argv[]) ...@@ -82,7 +96,7 @@ int main(int argc, char * const argv[])
} }
#endif // OHOS_DEBUG #endif // OHOS_DEBUG
// 3. execute rcs // 4. execute rcs
ExecuteRcs(); ExecuteRcs();
#ifdef OHOS_DEBUG #ifdef OHOS_DEBUG
...@@ -92,7 +106,7 @@ int main(int argc, char * const argv[]) ...@@ -92,7 +106,7 @@ int main(int argc, char * const argv[])
} }
#endif // OHOS_DEBUG #endif // OHOS_DEBUG
// 4. read configuration file and do jobs // 5. read configuration file and do jobs
InitReadCfg(); InitReadCfg();
#ifdef OHOS_DEBUG #ifdef OHOS_DEBUG
...@@ -102,7 +116,7 @@ int main(int argc, char * const argv[]) ...@@ -102,7 +116,7 @@ int main(int argc, char * const argv[])
} }
#endif // OHOS_DEBUG #endif // OHOS_DEBUG
// 5. keep process alive // 6. keep process alive
#ifdef OHOS_DEBUG #ifdef OHOS_DEBUG
printf("[Init] main, time used: sigInfo %ld ms, rcs %ld ms, cfg %ld ms.\n", \ printf("[Init] main, time used: sigInfo %ld ms, rcs %ld ms, cfg %ld ms.\n", \
TimeDiffMs(&tmEnter, &tmSysInfo), TimeDiffMs(&tmSysInfo, &tmRcs), TimeDiffMs(&tmRcs, &tmCfg)); TimeDiffMs(&tmEnter, &tmSysInfo), TimeDiffMs(&tmSysInfo, &tmRcs), TimeDiffMs(&tmRcs, &tmCfg));
......
此差异已折叠。
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册