未验证 提交 96633282 编写于 作者: H handy 提交者: Gitee

回退 'Pull Request !755 : 拆分独立的init_early进程在ramdisk中使用'

上级 f5d053d7
......@@ -251,11 +251,6 @@ if (defined(ohos_lite)) {
}
# For init only
config("libfsmanager_exported_configs") {
visibility = [ ":*" ]
include_dirs =
[ "//base/startup/init_lite/interfaces/innerkits/include/fs_manager" ]
}
ohos_static_library("libfsmanager_static") {
sources = fs_manager_sources
include_dirs = [
......@@ -265,8 +260,6 @@ if (defined(ohos_lite)) {
"//base/startup/init_lite/services/include",
"//base/startup/init_lite/services/param/include",
]
public_configs = [ ":libfsmanager_exported_configs" ]
deps = [ "//base/startup/init_lite/services/log:init_log" ]
part_name = "init"
}
......
......@@ -115,32 +115,6 @@ if (defined(ohos_lite)) {
import("//build/ohos.gni")
import("//build/ohos/native_stub/native_stub.gni")
ohos_executable("init_early") {
sources = [
"init/standard/device.c",
"init/standard/init_mount.c",
"init/standard/main_early.c",
"init/standard/switch_root.c",
]
include_dirs = [
"//base/startup/init_lite/services/include",
"//base/startup/init_lite/services/init/include",
]
deps = [
"//base/startup/init_lite/interfaces/innerkits:libfsmanager_static",
"//base/startup/init_lite/services/log:init_log",
"//base/startup/init_lite/ueventd:libueventd_ramdisk_static",
"//third_party/bounds_checking_function:libsec_static",
"//third_party/cJSON:cjson_static",
]
install_images = [
"updater",
"ramdisk",
]
install_enable = true
part_name = "init"
}
ohos_executable("init") {
sources = [
"//base/startup/init_lite/interfaces/innerkits/fd_holder/fd_holder_internal.c",
......@@ -152,9 +126,11 @@ if (defined(ohos_lite)) {
"init/standard/init_cmds.c",
"init/standard/init_control_fd_service.c",
"init/standard/init_jobs.c",
"init/standard/init_mount.c",
"init/standard/init_reboot.c",
"init/standard/init_service.c",
"init/standard/init_signal_handler.c",
"init/standard/switch_root.c",
]
modulemgr_sources = [
......@@ -179,6 +155,7 @@ if (defined(ohos_lite)) {
"//base/startup/init_lite/services/loopevent/include",
"//base/startup/init_lite/interfaces/innerkits/include",
"//base/startup/init_lite/interfaces/innerkits/fd_holder",
"//base/startup/init_lite/ueventd/include",
"//third_party/cJSON",
"//third_party/bounds_checking_function/include",
"//base/startup/init_lite/interfaces/innerkits/control_fd",
......@@ -195,6 +172,7 @@ if (defined(ohos_lite)) {
"//base/startup/init_lite/services/param:param_init",
"//base/startup/init_lite/services/utils:libinit_tools",
"//base/startup/init_lite/services/utils:libinit_utils",
"//base/startup/init_lite/ueventd:libueventd_ramdisk_static",
"//third_party/bounds_checking_function:libsec_static",
"//third_party/cJSON:cjson_static",
]
......@@ -257,6 +235,7 @@ if (defined(ohos_lite)) {
install_images = [
"system",
"updater",
"ramdisk",
]
install_enable = true
part_name = "init"
......@@ -265,7 +244,6 @@ if (defined(ohos_lite)) {
group("startup_init") {
deps = [
":init",
":init_early",
"etc:etc_files",
"//base/startup/init_lite/services/param:param_client",
"//base/startup/init_lite/services/param:param_init",
......
文件模式从 100755 更改为 100644
......@@ -28,6 +28,7 @@
#include "config_policy_utils.h"
#include "device.h"
#include "fd_holder_service.h"
#include "fs_manager/fs_manager.h"
#include "init_control_fd_service.h"
#include "init_log.h"
#include "init_mount.h"
......@@ -37,6 +38,9 @@
#include "init_service_manager.h"
#include "init_utils.h"
#include "securec.h"
#include "switch_root.h"
#include "ueventd.h"
#include "ueventd_socket.h"
#include "fd_holder_internal.h"
#include "sandbox.h"
#include "sandbox_namespace.h"
......@@ -106,6 +110,20 @@ void SystemInit(void)
InitControlFd();
}
static void EnableDevKmsg(void)
{
/* printk_devkmsg default value is ratelimit, We need to set "on" and remove the restrictions */
int fd = open("/proc/sys/kernel/printk_devkmsg", O_WRONLY | O_CLOEXEC, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
if (fd < 0) {
return;
}
char *kmsgStatus = "on";
write(fd, kmsgStatus, strlen(kmsgStatus) + 1);
close(fd);
fd = -1;
return;
}
void LogInit(void)
{
int ret = mknod("/dev/kmsg", S_IFCHR | S_IWUSR | S_IRUSR,
......@@ -115,9 +133,105 @@ void LogInit(void)
}
}
static char **GetRequiredDevices(Fstab fstab, int *requiredNum)
{
int num = 0;
FstabItem *item = fstab.head;
while (item != NULL) {
if (FM_MANAGER_REQUIRED_ENABLED(item->fsManagerFlags)) {
num++;
}
item = item->next;
}
char **devices = (char **)calloc(num, sizeof(char *));
INIT_ERROR_CHECK(devices != NULL, return NULL, "Failed calloc err=%d", errno);
int i = 0;
item = fstab.head;
while (item != NULL) {
if (FM_MANAGER_REQUIRED_ENABLED(item->fsManagerFlags)) {
devices[i] = strdup(item->deviceName);
INIT_ERROR_CHECK(devices[i] != NULL, FreeStringVector(devices, num); return NULL,
"Failed strdup err=%d", errno);
i++;
}
item = item->next;
}
*requiredNum = num;
return devices;
}
static int StartUeventd(char **requiredDevices, int num)
{
INIT_ERROR_CHECK(requiredDevices != NULL && num > 0, return -1, "Failed parameters");
int ueventSockFd = UeventdSocketInit();
if (ueventSockFd < 0) {
INIT_LOGE("Failed to create uevent socket");
return -1;
}
RetriggerUevent(ueventSockFd, requiredDevices, num);
close(ueventSockFd);
return 0;
}
static void StartInitSecondStage(void)
{
int requiredNum = 0;
Fstab* fstab = LoadRequiredFstab();
INIT_ERROR_CHECK(fstab != NULL, abort(), "Failed to load required fstab");
char **devices = GetRequiredDevices(*fstab, &requiredNum);
if (devices != NULL && requiredNum > 0) {
int ret = StartUeventd(devices, requiredNum);
if (ret == 0) {
ret = MountRequriedPartitions(fstab);
}
FreeStringVector(devices, requiredNum);
devices = NULL;
ReleaseFstab(fstab);
fstab = NULL;
if (ret < 0) {
// If mount required partitions failure.
// There is no necessary to continue.
// Just abort
INIT_LOGE("Mount requried partitions failed; please check fstab file");
// Execute sh for debugging
execv("/bin/sh", NULL);
abort();
}
}
// It will panic if close stdio before execv("/bin/sh", NULL)
CloseStdio();
#ifndef DISABLE_INIT_TWO_STAGES
SwitchRoot("/usr");
// Execute init second stage
char * const args[] = {
"/bin/init",
"--second-stage",
NULL,
};
if (execv("/bin/init", args) != 0) {
INIT_LOGE("Failed to exec \"/bin/init\", err = %d", errno);
exit(-1);
}
#endif
}
void SystemPrepare(void)
{
// Second stage, nothing to prepare
MountBasicFs();
CreateDeviceNode();
LogInit();
// Make sure init log always output to /dev/kmsg.
EnableDevKmsg();
// Only ohos normal system support
// two stages of init.
// If we are in updater mode, only one stage of init,
INIT_LOGI("DISABLE_INIT_TWO_STAGES not defined");
if (InUpdaterMode() == 0) {
StartInitSecondStage();
}
}
void SystemLoadSelinux(void)
......
/*
* Copyright (c) 2020-2021 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 <errno.h>
#include <stdlib.h>
#include <signal.h>
#include <time.h>
#include <sys/sysmacros.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <linux/major.h>
#include "device.h"
#include "init_log.h"
#include "init_utils.h"
#include "init_mount.h"
#include "fs_manager/fs_manager.h"
#include "switch_root.h"
#include "ueventd.h"
#include "ueventd_socket.h"
static void EnableDevKmsg(void)
{
/* printk_devkmsg default value is ratelimit, We need to set "on" and remove the restrictions */
int fd = open("/proc/sys/kernel/printk_devkmsg", O_WRONLY | O_CLOEXEC, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
if (fd < 0) {
return;
}
char *kmsgStatus = "on";
write(fd, kmsgStatus, strlen(kmsgStatus) + 1);
close(fd);
fd = -1;
return;
}
static char **GetRequiredDevices(Fstab fstab, int *requiredNum)
{
int num = 0;
FstabItem *item = fstab.head;
while (item != NULL) {
if (FM_MANAGER_REQUIRED_ENABLED(item->fsManagerFlags)) {
num++;
}
item = item->next;
}
char **devices = (char **)calloc(num, sizeof(char *));
INIT_ERROR_CHECK(devices != NULL, return NULL, "Failed calloc err=%d", errno);
int i = 0;
item = fstab.head;
while (item != NULL) {
if (FM_MANAGER_REQUIRED_ENABLED(item->fsManagerFlags)) {
devices[i] = strdup(item->deviceName);
INIT_ERROR_CHECK(devices[i] != NULL, FreeStringVector(devices, num); return NULL,
"Failed strdup err=%d", errno);
i++;
}
item = item->next;
}
*requiredNum = num;
return devices;
}
static int StartUeventd(char **requiredDevices, int num)
{
INIT_ERROR_CHECK(requiredDevices != NULL && num > 0, return -1, "Failed parameters");
int ueventSockFd = UeventdSocketInit();
if (ueventSockFd < 0) {
INIT_LOGE("Failed to create uevent socket");
return -1;
}
RetriggerUevent(ueventSockFd, requiredDevices, num);
close(ueventSockFd);
return 0;
}
static void MountRequiredPartitions(void)
{
int requiredNum = 0;
Fstab* fstab = LoadRequiredFstab();
INIT_ERROR_CHECK(fstab != NULL, abort(), "Failed to load required fstab");
char **devices = GetRequiredDevices(*fstab, &requiredNum);
if (devices == NULL || requiredNum <= 0) {
return;
}
int ret = StartUeventd(devices, requiredNum);
if (ret == 0) {
ret = MountRequriedPartitions(fstab);
}
FreeStringVector(devices, requiredNum);
devices = NULL;
ReleaseFstab(fstab);
fstab = NULL;
if (ret < 0) {
// If mount required partitions failure.
// There is no necessary to continue.
// Just abort
INIT_LOGE("Mount requried partitions failed; please check fstab file");
// Execute sh for debugging
execv("/bin/sh", NULL);
abort();
}
SwitchRoot("/usr");
}
int main(int argc, char * const argv[])
{
MountBasicFs();
CreateDeviceNode();
OpenLogDevice();
// Make sure init log always output to /dev/kmsg.
EnableDevKmsg();
if (InUpdaterMode() == 0) {
MountRequiredPartitions();
}
// Execute init second stage
char * const args[] = {
"/bin/init",
"--second-stage",
NULL,
};
if (execv("/bin/init", args) != 0) {
INIT_LOGE("Failed to exec \"/bin/init\", err = %d", errno);
exit(-1);
}
return 0;
}
......@@ -31,18 +31,6 @@ if (defined(ohos_lite)) {
}
} else {
import("//build/ohos.gni")
#
# exported include directories
#
config("init_log_exported_config") {
visibility = [ ":*" ]
include_dirs = [
".",
"//base/startup/init_lite/interfaces/innerkits/include",
]
}
ohos_static_library("init_log") {
sources = [ "init_log.c" ]
defines = [ "INIT_DMESG" ]
......@@ -50,7 +38,6 @@ if (defined(ohos_lite)) {
"//base/startup/init_lite/interfaces/innerkits/include",
"//third_party/bounds_checking_function/include",
]
public_configs = [ ":init_log_exported_config" ]
part_name = "startup"
subsystem_name = "startup"
}
......@@ -71,7 +58,6 @@ if (defined(ohos_lite)) {
"system",
"updater",
]
public_configs = [ ":init_log_exported_config" ]
external_deps = [ "hilog_native:libhilog_base" ]
install_enable = true
part_name = "init"
......
......@@ -30,11 +30,6 @@ if (defined(ohos_lite)) {
} else {
import("//build/ohos.gni")
config("init_utils_exported_configs") {
visibility = [ ":*" ]
include_dirs = [ "//base/startup/init_lite/services/include" ]
}
ohos_static_library("libinit_tools") {
sources = [
"//base/startup/init_lite/services/utils/init_hashmap.c",
......@@ -47,7 +42,6 @@ if (defined(ohos_lite)) {
"//base/startup/init_lite/interfaces/innerkits/include",
"//base/startup/init_lite/services/include",
]
public_configs = [ ":init_utils_exported_configs" ]
defines = [ "_GNU_SOURCE" ]
part_name = "init"
}
......@@ -61,7 +55,6 @@ if (defined(ohos_lite)) {
"//base/startup/init_lite/interfaces/innerkits/include",
"//base/startup/init_lite/services/include",
]
public_configs = [ ":init_utils_exported_configs" ]
defines = [ "_GNU_SOURCE" ]
part_name = "init"
}
......
......@@ -96,13 +96,6 @@ if (defined(ohos_lite)) {
]
}
#
# exported include directories
#
config("libueventd_exported_config") {
visibility = [ ":*" ]
include_dirs = [ "./include" ]
}
ohos_static_library("libueventd_ramdisk_static") {
sources = service_ueventd_sources
include_dirs = service_ueventd_include
......@@ -114,8 +107,6 @@ if (defined(ohos_lite)) {
external_deps = [ "selinux:librestorecon" ]
cflags += [ "-DWITH_SELINUX" ]
}
public_configs = [ ":libueventd_exported_config" ]
part_name = "init"
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册