diff --git a/interfaces/innerkits/fs_manager/BUILD.gn b/interfaces/innerkits/fs_manager/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..a93bcd712edc8b5367c31c7f0f896c43409568cf --- /dev/null +++ b/interfaces/innerkits/fs_manager/BUILD.gn @@ -0,0 +1,39 @@ +# Copyright (c) 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. + +import("//build/ohos.gni") + +ohos_shared_library("libfs_manager") { + sources = [ + "fstab.cpp", + "fstab_mount.cpp", + ] + + include_dirs = [ + "//base/startup/init_lite/interfaces/innerkits/include", + "//third_party/bounds_checking_function/include", + "//base/startup/init_lite/services/log", + "//base/startup/init_lite/services/utils", + ] + + deps = [ + "//base/startup/init_lite/services/log:init_log", + "//base/startup/init_lite/services/utils:libinit_utils", + "//third_party/bounds_checking_function:libsec_static", + ] + part_name = "init" + install_images = [ + "system", + "updater", + ] +} diff --git a/interfaces/innerkits/fs_manager/fstab.cpp b/interfaces/innerkits/fs_manager/fstab.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9c33c67bd18c1cb6da64af9ecaf14da9c47cf55e --- /dev/null +++ b/interfaces/innerkits/fs_manager/fstab.cpp @@ -0,0 +1,233 @@ +/* + * Copyright (c) 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 +#include +#include "fstab_mount.h" +#include "init_log.h" + +static std::map g_fsManagerMap = { + {"check", FS_MANAGER_CHECK}, + {"wait", FS_MANAGER_WAIT}, + {"required", FS_MANAGER_REQUIRED}, +}; + +std::vector SplitString(const std::string &str, const std::string del) +{ + std::vector result; + size_t found = std::string::npos; + size_t start = 0; + while (true) { + found = str.find_first_of(del, start); + result.push_back(str.substr(start, found - start)); + if (found == std::string::npos) { + break; + } + start = found + 1; + } + return result; +} + +void ParseFstabPerLine(std::string &line, Fstab &fstab, bool &parseError) +{ + const std::string separator = " \t"; + char *restPtr = nullptr; + struct FstabItem item; + char *p = nullptr; + do { + if ((p = strtok_r(const_cast(line.c_str()), separator.c_str(), &restPtr)) == nullptr) { + fprintf(stderr, "Failed to parse block device.\n"); + break; + } + + item.deviceName = p; + if ((p = strtok_r(nullptr, separator.c_str(), &restPtr)) == nullptr) { + fprintf(stderr, "Failed to parse mount point.\n"); + break; + } + + item.mountPoint = p; + if ((p = strtok_r(nullptr, separator.c_str(), &restPtr)) == nullptr) { + fprintf(stderr, "Failed to parse fs type.\n"); + break; + } + item.fsType = p; + + if ((p = strtok_r(nullptr, separator.c_str(), &restPtr)) == nullptr) { + fprintf(stderr, "Failed to parse mount options.\n"); + break; + } + item.mountOptions = p; + + if ((p = strtok_r(nullptr, separator.c_str(), &restPtr)) == nullptr) { + fprintf(stderr, "Failed to parse fs manager flags.\n"); + break; + } + item.fsManagerFlags = 0; + std::string fsFlags = p; + for (const auto& flag : g_fsManagerMap) { + if (fsFlags.find(flag.first) != std::string::npos) { + item.fsManagerFlags |= flag.second; + } + } + + fstab.emplace_back(std::move(item)); + parseError = false; + } while (0); + + return; +} + +bool ReadFstabFromFile(const std::string &fstabFile, Fstab &fstab) +{ + char *line = nullptr; + size_t allocLen = 0; + ssize_t readLen = 0; + std::string tmpStr = ""; + Fstab tmpTab; + int fd = open(fstabFile.c_str(), O_RDONLY); + if (fd < 0) { + INIT_LOGE("open %s failed, errno: %d", fstabFile.c_str(), errno); + } else { + INIT_LOGE("open %s success", fstabFile.c_str()); + close(fd); + fd = -1; + } + auto fp = std::unique_ptr(fopen(fstabFile.c_str(), "r"), fclose); + if (fp.get() == nullptr) { + INIT_LOGE("fp is nullptr"); + return false; + } + while ((readLen = getline(&line, &allocLen, fp.get())) != -1) { + char *p = nullptr; + if (line[readLen - 1] == '\n') { + line[readLen - 1] = '\0'; + } + p = line; + while (isspace(*p)) { + p++; + } + + if (*p == '\0' || *p == '#') { + continue; + } + + bool parseError = true; + tmpStr = p; + ParseFstabPerLine(tmpStr, tmpTab, parseError); + tmpStr.clear(); + if (parseError) { + free(line); + return false; + } + } + + free(line); + fstab = std::move(tmpTab); + return true; +} + +struct FstabItem* FindFstabItemForMountPoint(Fstab &fstab, const std::string& mp) +{ + for (auto &item : fstab) { + if (item.mountPoint == mp) { + return &item; + } + } + return nullptr; +} + +struct FstabItem* FindFstabItemForPath(Fstab &fstab, const std::string &path) +{ + struct FstabItem *item = nullptr; + std::string tmp(path); + if (path.empty()) { + return nullptr; + } + while (true) { + item = FindFstabItemForMountPoint(fstab, tmp); + if (item != nullptr) { + return item; + } + auto sep = tmp.find_last_of('/'); + if (sep == std::string::npos) { + return nullptr; + } + if (sep == 0) { + tmp = "/"; + break; + } + tmp = tmp.substr(0, sep); + } + return nullptr; +} + +static bool ParseDefaultMountFlags(const std::string &mountFlag, unsigned long &flags) +{ + std::map mountFlags = { + { "noatime", MS_NOATIME }, + { "noexec", MS_NOEXEC }, + { "nosuid", MS_NOSUID }, + { "nodev", MS_NODEV }, + { "nodiratime", MS_NODIRATIME }, + { "ro", MS_RDONLY }, + { "rw", 0 }, + { "sync", MS_SYNCHRONOUS }, + { "remount", MS_REMOUNT }, + { "bind", MS_BIND }, + { "rec", MS_REC }, + { "unbindable", MS_UNBINDABLE }, + { "private", MS_PRIVATE }, + { "slave", MS_SLAVE }, + { "shared", MS_SHARED }, + { "defaults", 0 }, + }; + for (const auto &flag : mountFlags) { + if (mountFlag == flag.first) { + flags = flag.second; + return true; + } + } + return false; +} + +unsigned long GetMountFlags(const std::string &mountOptions, std::string &fsSpecificOptions) +{ + unsigned long flags = 0; + unsigned long tmpFlag = 0; + + auto options = SplitString(mountOptions, ","); + fsSpecificOptions.clear(); + for (const auto &option : options) { + if (ParseDefaultMountFlags(option, tmpFlag)) { + flags |= tmpFlag; + } else { // not default mount flags, maybe File system specific. + fsSpecificOptions.append(option); + fsSpecificOptions.append(","); + } + } + fsSpecificOptions.pop_back(); // Remove last ',' + return flags; +} + diff --git a/interfaces/innerkits/fs_manager/fstab_mount.cpp b/interfaces/innerkits/fs_manager/fstab_mount.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8b7648f36b8d9ef79d7669135c41bd7a4def6011 --- /dev/null +++ b/interfaces/innerkits/fs_manager/fstab_mount.cpp @@ -0,0 +1,253 @@ +/* + * Copyright (c) 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 +#include +#include +#include "fstab_mount.h" +extern "C" { +#include "init_log.h" +#include "init_utils.h" +} + +using namespace std; +const int WAIT_MAX_COUNT = 10; +std::vector SplitString(const std::string &str, const std::string del = " \t"); +unsigned long GetMountFlags(const std::string &mountOptions, std::string &fsSpecificOptions); + +bool IsSupportedFilesystem(const std::string &fsType) +{ + std::vector supportedFilesystems = {"ext4", "f2fs", "vfat"}; + + bool supported = false; + for (const auto &fs : supportedFilesystems) { + if (fsType == fs) { + supported = true; + break; + } + } + return supported; +} + +static int ExecCommand(std::vector cmds) +{ + std::vector extractedCmds; + + for (const auto &cmd : cmds) { + extractedCmds.push_back(const_cast(cmd.c_str())); + } + extractedCmds.push_back(nullptr); + pid_t pid = fork(); + if (pid < 0) { + INIT_LOGE("Fork new process to format failed: %d", errno); + return -1; + } + if (pid == 0) { + execv(extractedCmds[0], extractedCmds.data()); + exit(-1); + } + int status; + waitpid(pid, &status, 0); + if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) { + INIT_LOGE("Command %s failed with status %d", extractedCmds[0], WEXITSTATUS(status)); + } + return WEXITSTATUS(status); +} + +int DoFormat(const std::string &devPath, const std::string &fsType) +{ + std::map fsToolsMap = { + { "ext4", "/bin/mke2fs" }, + { "f2fs", "/bin/make_f2fs" }, + }; + int ret = 0; + auto it = fsToolsMap.find(fsType); + if (it == fsToolsMap.end()) { + INIT_LOGE("Can not find fs tools for %s", fsType.c_str()); + return -1; + } + auto fsTool = it->second; + if (fsType == "ext4") { + constexpr int blockSize = 4096; + std::vector formatCmds; + formatCmds.push_back(fsTool); + formatCmds.push_back("-F"); + formatCmds.push_back("-t"); + formatCmds.push_back(fsType); + formatCmds.push_back("-b"); + formatCmds.push_back(std::to_string(blockSize)); + formatCmds.push_back(devPath); + ret = ExecCommand(formatCmds); + } else if (fsType == "f2fs") { + std::vector formatCmds; + formatCmds.push_back(fsTool); + formatCmds.push_back(devPath); + ret = ExecCommand(formatCmds); + } + return ret; +} + +MountStatus GetMountStatusForMountPoint(const std::string &mountPoint) +{ + char buffer[512]; + size_t n; + constexpr size_t numMountItems = 6; + const std::string mountFile = "/proc/mounts"; + auto fp = std::unique_ptr(fopen(mountFile.c_str(), "r"), fclose); + + while (fgets(buffer, sizeof(buffer), fp.get()) != nullptr) { + n = strlen(buffer); + if (buffer[n - 1] == '\n') { + buffer[n - 1] = '\0'; + } + std::string line(buffer); + std::vector mountItems = SplitString(line); + if (mountItems.size() == numMountItems) { + // Second item in /proc/mounts is mount point + if (mountItems[1] == mountPoint) { + return MountStatus::MOUNT_MOUNTED; + } + } + } + + // Cannot find it from system. + return MountStatus::MOUNT_UMOUNTED; +} + +static int Mount(const std::string &source, const std::string &target, const std::string &fsType, + unsigned long flags, const std::string &data) +{ + struct stat st {}; + int rc; + if (stat(target.c_str(), &st) != 0 && errno != ENOENT) { + INIT_LOGE("Cannot get %s status: %d", target.c_str(), errno); + return -1; + } + if ((st.st_mode & S_IFMT) == S_IFLNK) { // link, delete it. + unlink(target.c_str()); + } + mkdir(target.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); + errno = 0; + while ((rc = mount(source.c_str(), target.c_str(), fsType.c_str(), flags, data.c_str()) != 0)) { + if (errno == EAGAIN) { + INIT_LOGI("Mount %s to %s failed. try again", source.c_str(), target.c_str()); + continue; + } else { + break; + } + } + return rc; +} + +int DoMount(const struct FstabItem &item) +{ + unsigned long mountFlags; + std::string fsSpecificOptions; + + mountFlags = GetMountFlags(item.mountOptions, fsSpecificOptions); + if (!IsSupportedFilesystem(item.fsType)) { + INIT_LOGE("Unsupported file system \" %s \"", item.fsType.c_str()); + return -1; + } + if (FM_MANAGER_WAIT_ENABLED(item)) { + WaitForFile(item.deviceName.c_str(), WAIT_MAX_COUNT); + } + int rc = Mount(item.deviceName, item.mountPoint, item.fsType, mountFlags, fsSpecificOptions); + if (rc != 0) { + INIT_LOGE("Mount %s to %s failed %d", item.deviceName.c_str(), item.mountPoint.c_str(), errno); + } else { + INIT_LOGI("Mount %s to %s successful", item.deviceName.c_str(), item.mountPoint.c_str()); + } + return rc; +} + +extern "C" { +int MountAllWithFstabFile(const char *fstabFile, int required) +{ + if (fstabFile == NULL) { + return -1; + } + Fstab fstab = {}; + std::string tmpFstabFile = string(fstabFile); + if (ReadFstabFromFile(tmpFstabFile, fstab) == false) { + INIT_LOGE("Read %s failed", fstabFile); + return -1; + } + + INIT_LOGD("mount filesystem config info:"); + for (const auto &item : fstab) { + INIT_LOGD("\tDevice: %s", item.deviceName.c_str()); + INIT_LOGD("\tMount point : %s", item.mountPoint.c_str()); + INIT_LOGD("\tFs type : %s", item.fsType.c_str()); + INIT_LOGD("\tMount options: %s", item.mountOptions.c_str()); + if ((FM_MANAGER_REQUIRED_ENABLED(item)) && (required == 1)) { + INIT_CHECK(DoMount(item) == 0, return -1); + } else if ((!FM_MANAGER_REQUIRED_ENABLED(item)) && (required == 0)) { + INIT_CHECK(DoMount(item) == 0, return -1); + } + } + return 0; +} + +int UmountAllWithFstabFile(const char *fstabFile) +{ + if (fstabFile == NULL) { + return -1;; + } + Fstab fstab = {}; + std::string tmpFstabFile = string(fstabFile); + if (ReadFstabFromFile(tmpFstabFile, fstab) == false) { + INIT_LOGW("Read %s failed", fstabFile); + return -1;; + } + + INIT_LOGD("umount filesystem config info:"); + for (const auto &item : fstab) { + INIT_LOGD("Umount for path %s", item.mountPoint.c_str()); + int rc = GetMountStatusForMountPoint(item.mountPoint); + if (rc == MountStatus::MOUNT_ERROR) { + return -1; + } else if (rc == MountStatus::MOUNT_UMOUNTED) { + continue; + } else { + int ret = umount(item.mountPoint.c_str()); + if (ret == -1) { + INIT_LOGE("Umount %s failed: %d", item.mountPoint.c_str(), errno); + return -1; + } + } + } + return 0; +} +int DoFormatPartition(const char *devPath, const char *fsType) +{ + if ((!devPath) || (!fsType)) { + return -1; + } + std::string tmpDevPath = string(devPath); + std::string tmpFsType = string(fsType); + return DoFormat(tmpDevPath, tmpFsType); +} + +} + diff --git a/interfaces/innerkits/include/fstab_mount.h b/interfaces/innerkits/include/fstab_mount.h new file mode 100644 index 0000000000000000000000000000000000000000..89ed08e0336c3b90457744be591171856a639eb9 --- /dev/null +++ b/interfaces/innerkits/include/fstab_mount.h @@ -0,0 +1,72 @@ +/* + * Copyright (c) 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. + */ + +#ifndef _FSTAB_MOUNT_H +#define _FSTAB_MOUNT_H + +#ifdef __cplusplus + +#include +#include +#include + +/* Fs manager flags definition */ +#define FS_MANAGER_CHECK 0x00000001 +#define FS_MANAGER_WAIT 0x00000002 +#define FS_MANAGER_REQUIRED 0x00000004 + +#define VALID_FS_MANAGER_FLAGS (FS_MANAGER_CHECK | FS_MANAGER_WAIT) +#define FS_MANAGER_FLAGS_ENABLED(item, flag) ((item.fsManagerFlags & FS_MANAGER_##flag) != 0) + +#define FM_MANAGER_CHECK_ENABLED(item) FS_MANAGER_FLAGS_ENABLED(item, CHECK) +#define FM_MANAGER_WAIT_ENABLED(item) FS_MANAGER_FLAGS_ENABLED(item, WAIT) +#define FM_MANAGER_REQUIRED_ENABLED(item) FS_MANAGER_FLAGS_ENABLED(item, REQUIRED) + +enum MountStatus : int { + MOUNT_ERROR = -1, + MOUNT_UMOUNTED = 0, + MOUNT_MOUNTED = 1, +}; + +struct FstabItem { + std::string deviceName; // Block device name + std::string mountPoint; // Mount point + std::string fsType; // File system type + std::string mountOptions; // File system mount options. readonly, rw, remount etc. + unsigned int fsManagerFlags; // flags defined by fs manager. +}; + +using Fstab = std::vector; +bool ReadFstabFromFile(const std::string &fstabFile, Fstab &fstabs); +struct FstabItem* FindFstabItemForPath(Fstab &fstab, const std::string &path); +struct FstabItem* FindFstabItemForMountPoint(Fstab &fstab, const std::string &mp); + +bool IsSupportedFilesystem(const std::string &fsType); +int DoFormat(const std::string &devPath, const std::string &fsType); +MountStatus GetMountStatusForMountPoint(const std::string &mountPoint); +int DoMount(const struct FstabItem &item); +bool ReadFstabFromFile(const std::string &fstabFile, Fstab &fstabs); +struct FstabItem* FindFstabItemForPath(Fstab &fstab, const std::string &path); +struct FstabItem* FindFstabItemForMountPoint(Fstab &fstab, const std::string &mp); + +#else + +int MountAllWithFstabFile(const char *fstabFile, int required); +int UmountAllWithFstabFile(const char *fstabFile); +int DoFormatPartition(const char *devPath, const char *fsType); + +#endif + +#endif diff --git a/services/BUILD.gn b/services/BUILD.gn index 5ba7cffa73de640d6d59685a3a9c801fdf0b20d5..296d830851bff5f2114a4996f1400560acd687fd 100644 --- a/services/BUILD.gn +++ b/services/BUILD.gn @@ -31,14 +31,15 @@ if (defined(ohos_lite)) { "src/init_service_manager.c", "src/init_service_socket.c", "src/init_signal_handler.c", - "src/init_utils.c", "src/main.c", + "utils/init_utils.c", ] include_dirs = [ "//base/startup/init_lite/services/include", "//base/startup/init_lite/services/param/include", "//base/startup/init_lite/services/log", + "//base/startup/init_lite/services/utils", "//third_party/cJSON", "//third_party/bounds_checking_function/include", "//base/startup/syspara_lite/interfaces/kits", @@ -121,20 +122,23 @@ if (defined(ohos_lite)) { "src/init_service_manager.c", "src/init_service_socket.c", "src/init_signal_handler.c", - "src/init_utils.c", "src/main.c", ] include_dirs = [ "//base/startup/init_lite/services/include/param", "//base/startup/init_lite/services/include", "//base/startup/init_lite/services/log", + "//base/startup/init_lite/services/utils", + "//base/startup/init_lite/interfaces/innerkits/include", "//third_party/cJSON", "//third_party/bounds_checking_function/include", "//third_party/libuv/include", ] deps = [ + "//base/startup/init_lite/interfaces/innerkits/fs_manager:libfs_manager", "//base/startup/init_lite/services/log:init_log", "//base/startup/init_lite/services/param:paramservice", + "//base/startup/init_lite/services/utils:libinit_utils", "//third_party/bounds_checking_function:libsec_static", "//third_party/cJSON:cjson_static", ] @@ -157,6 +161,7 @@ if (defined(ohos_lite)) { ":init", ":init_etc", ":updaterueventd", + "//base/startup/init_lite/interfaces/innerkits/fs_manager:libfs_manager", "//base/startup/init_lite/interfaces/innerkits/socket:libsocket", "//base/startup/init_lite/services/cmds/reboot:reboot", "//base/startup/init_lite/services/cmds/service_control:service_control", @@ -175,11 +180,19 @@ if (defined(ohos_lite)) { ohos_prebuilt_etc("passwd") { source = "//base/startup/init_lite/services/etc/passwd" + install_images = [ + "system", + "updater", + ] part_name = "init" } ohos_prebuilt_etc("group") { source = "//base/startup/init_lite/services/etc/group" + install_images = [ + "system", + "updater", + ] part_name = "init" } diff --git a/services/etc/init.cfg b/services/etc/init.cfg index 5d82483ef3a39bf534c9b74edf4652321fa8d447..5a44f0752d29a709e1a115d79c87cc11e84b32b9 100755 --- a/services/etc/init.cfg +++ b/services/etc/init.cfg @@ -18,8 +18,7 @@ "start ueventd", "mkdir /vendor", "mkdir /data", - "mount ext4 /dev/block/platform/soc/10100000.himci.eMMC/by-name/vendor /vendor wait rdonly barrier=1", - "mount ext4 /dev/block/platform/soc/10100000.himci.eMMC/by-name/userdata /data wait nosuid nodev noatime barrier=1,data=ordered,noauto_da_alloc" + "mount_fstab /etc/fstab.Hi3516DV300" ] }, { "name" : "init", diff --git a/services/param/BUILD.gn b/services/param/BUILD.gn index 07e1d27849151a38ed8427a639bcd9d523f8f6be..46aec50ae4f9a66711acde0a109422697cd0c2dc 100644 --- a/services/param/BUILD.gn +++ b/services/param/BUILD.gn @@ -14,7 +14,6 @@ import("//build/ohos.gni") ohos_static_library("paramservice") { sources = [ - "//base/startup/init_lite/services/src/init_utils.c", "manager/param_cache.c", "manager/param_manager.c", "manager/param_trie.c", @@ -30,11 +29,13 @@ ohos_static_library("paramservice") { "//base/startup/init_lite/services/include/param", "//base/startup/init_lite/services/include", "//base/startup/init_lite/services/log", + "//base/startup/init_lite/services/utils", "//third_party/libuv/include", "//third_party/cJSON", ] deps = [ + "//base/startup/init_lite/services/utils:libinit_utils", "//third_party/bounds_checking_function:libsec_static", "//third_party/libuv:uv_static", ] @@ -44,7 +45,6 @@ ohos_static_library("paramservice") { ohos_static_library("paramclient") { sources = [ - "//base/startup/init_lite/services/src/init_utils.c", "client/param_request.c", "manager/param_cache.c", "manager/param_manager.c", @@ -56,12 +56,14 @@ ohos_static_library("paramclient") { "//base/startup/init_lite/services/include/param", "//base/startup/init_lite/services/include", "//base/startup/init_lite/services/log", + "//base/startup/init_lite/services/utils", "//third_party/libuv/include", "//third_party/cJSON", ] deps = [ "//base/startup/init_lite/services/log:init_log", + "//base/startup/init_lite/services/utils:libinit_utils", "//third_party/bounds_checking_function:libsec_static", "//third_party/libuv:uv_static", ] diff --git a/services/src/init_cmds.c b/services/src/init_cmds.c index d929e7976765fa8b75222158416957ae8b6a3eba..841857e94cfb33eb72a905893ac05e25812ae488 100644 --- a/services/src/init_cmds.c +++ b/services/src/init_cmds.c @@ -34,6 +34,9 @@ #include #include #include +#ifndef OHOS_LITE +#include "fstab_mount.h" +#endif #include "init_jobs.h" #include "init_log.h" #ifndef OHOS_LITE @@ -336,7 +339,7 @@ out: return; } -static void DoStart(const char* cmdContent, int maxArg) +static void DoStart(const char *cmdContent, int maxArg) { struct CmdArgs *ctx = GetCmd(cmdContent, " ", maxArg); if (ctx == NULL || ctx->argv == NULL || ctx->argc != maxArg) { @@ -1157,6 +1160,52 @@ out: FreeCmd(&ctx); return; } + +void DoMountFstabFile(const char *cmdContent, int maxArg) +{ + struct CmdArgs *ctx = GetCmd(cmdContent, " ", maxArg); + if (ctx == NULL || ctx->argv == NULL || ctx->argc != maxArg) { + INIT_LOGE("DoMountFstabFile invalid arguments :%s", cmdContent); + goto out; + } + if (access(ctx->argv[0], R_OK) != 0) { + if (errno == ENOENT) { + INIT_LOGE("file %s is not exist.", ctx->argv[0]); + } else { + INIT_LOGE("file %s is not allow read.", ctx->argv[0]); + } + goto out; + } + INIT_LOGD("DoMountFstabFile :%s", cmdContent); + int required = 0; // fsmanager flags: required not need judge + MountAllWithFstabFile(ctx->argv[0], required); +out: + FreeCmd(&ctx); + return; +} + +void DoUmountFstabFile(const char *cmdContent, int maxArg) +{ + struct CmdArgs *ctx = GetCmd(cmdContent, " ", maxArg); + if (ctx == NULL || ctx->argv == NULL || ctx->argc != maxArg) { + INIT_LOGE("DoUmountFstabFile invalid arguments :%s", cmdContent); + goto out; + } + if (access(ctx->argv[0], R_OK) != 0) { + if (errno == ENOENT) { + INIT_LOGE("file %s is not exist.", ctx->argv[0]); + } else { + INIT_LOGE("file %s is not allow read.", ctx->argv[0]); + } + goto out; + } + INIT_LOGD("DoUmountFstabFile :%s", cmdContent); + UmountAllWithFstabFile(ctx->argv[0]); +out: + FreeCmd(&ctx); + return; +} + #endif // __LITEOS__ void DoCmd(const CmdLine* curCmd) @@ -1196,6 +1245,8 @@ static const struct CmdTable CMD_TABLE[] = { { "load_persist_params ", 1, DoLoadPersistParams }, { "load_param ", 1, DoLoadDefaultParams }, { "ifup ", 1, DoIfup }, + { "mount_fstab ", 1, DoMountFstabFile }, + { "umount_fstab ", 1, DoUmountFstabFile }, #endif { "stop ", 1, DoStop }, { "reset ", 1, DoReset }, diff --git a/services/utils/BUILD.gn b/services/utils/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..fb163f4e6c2faf2afab942aaa679b725d89fa37e --- /dev/null +++ b/services/utils/BUILD.gn @@ -0,0 +1,30 @@ +# Copyright (c) 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. + +import("//build/ohos.gni") + +ohos_static_library("libinit_utils") { + sources = [ "init_utils.c" ] + + include_dirs = [ + "//third_party/bounds_checking_function/include", + "//base/startup/init_lite/services/log", + "//base/startup/init_lite/services/utils", + ] + + deps = [ + "//base/startup/init_lite/services/log:init_log", + "//third_party/bounds_checking_function:libsec_static", + ] + part_name = "init" +} diff --git a/services/src/init_utils.c b/services/utils/init_utils.c similarity index 95% rename from services/src/init_utils.c rename to services/utils/init_utils.c index f1dba38a040d2f80527d30f0effbfea289cacff6..d4f74e0078bcfac8e7694346771f48c81a1ea5d2 100644 --- a/services/src/init_utils.c +++ b/services/utils/init_utils.c @@ -31,7 +31,6 @@ #include #include "init_log.h" -#include "init_utils.h" #include "securec.h" #define WAIT_MAX_COUNT 10 diff --git a/services/include/init_utils.h b/services/utils/init_utils.h similarity index 100% rename from services/include/init_utils.h rename to services/utils/init_utils.h