diff --git a/interfaces/innerkits/BUILD.gn b/interfaces/innerkits/BUILD.gn index d6d9e422e22bd8773e959c5e1eee81af8774b732..8e8935a13f414dcb8975152a721d747b3036248d 100755 --- a/interfaces/innerkits/BUILD.gn +++ b/interfaces/innerkits/BUILD.gn @@ -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" } diff --git a/services/BUILD.gn b/services/BUILD.gn index f84834a3e6084706d86720fd24acfedc705c67ae..e1d32512db9e56923c8108ae934572585a1f4ff4 100755 --- a/services/BUILD.gn +++ b/services/BUILD.gn @@ -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", diff --git a/services/init/main.c b/services/init/main.c old mode 100755 new mode 100644 diff --git a/services/init/standard/init.c b/services/init/standard/init.c old mode 100755 new mode 100644 index 70a4ab0a1e878bd8697f70a39069b024b265783b..75ce9d9893954f838c00ce1e70709cbaf1e67b07 --- a/services/init/standard/init.c +++ b/services/init/standard/init.c @@ -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) diff --git a/services/init/standard/main_early.c b/services/init/standard/main_early.c deleted file mode 100755 index 3edbc636817f3bfbe9e267423b72c37be6691b7a..0000000000000000000000000000000000000000 --- a/services/init/standard/main_early.c +++ /dev/null @@ -1,141 +0,0 @@ -/* - * 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 -#include -#include -#include -#include -#include -#include -#include -#include -#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; -} diff --git a/services/log/BUILD.gn b/services/log/BUILD.gn index 2b1a718b882cef35a168f5ab656aad07388e65dc..edca0a36d5f64fae8459a382054ad602542c8748 100755 --- a/services/log/BUILD.gn +++ b/services/log/BUILD.gn @@ -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" diff --git a/services/utils/BUILD.gn b/services/utils/BUILD.gn index 7b82900a8bd1a40b181d869df4cf8e2c1cb72832..ec3a50570fff674d38539bf7dd3c171b5c09ad18 100755 --- a/services/utils/BUILD.gn +++ b/services/utils/BUILD.gn @@ -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" } diff --git a/ueventd/BUILD.gn b/ueventd/BUILD.gn old mode 100755 new mode 100644 index f105dee9661044324c0356af1919413851c23733..cdf16a5e1d374c42e2f7b14cf0c270e5d1feccad --- a/ueventd/BUILD.gn +++ b/ueventd/BUILD.gn @@ -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" }