提交 df51f3bd 编写于 作者: O openharmony_ci 提交者: Gitee

!186 init部件相关命令合并到begetctl中

Merge pull request !186 from handy/120720
......@@ -6,7 +6,7 @@
"//base/startup/init_lite/services:startup_init",
"//base/startup/init_lite/ueventd:startup_ueventd",
"//base/startup/init_lite/watchdog:watchdog",
"//base/startup/init_lite/services/cmds/misc:misc_daemon"
"//base/startup/init_lite/services/begetctl:begetctl"
],
"test_list": [
"//base/startup/init_lite/test/unittest:init_test"
......
......@@ -173,9 +173,6 @@ if (defined(ohos_lite)) {
"//base/startup/init_lite/interfaces/innerkits/fs_manager:libfsmanager_shared",
"//base/startup/init_lite/interfaces/innerkits/reboot:libreboot",
"//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",
"//base/startup/init_lite/services/param:param",
"//base/startup/init_lite/services/param:param_client",
"//base/startup/init_lite/services/param:param_service",
"//base/startup/init_lite/services/param:param_watcher",
......
......@@ -9,22 +9,57 @@
# 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.
# limitations under the License.
import("//build/ohos.gni")
ohos_executable("misc_daemon") {
sources = [ "misc_daemon.cpp" ]
ohos_executable("begetctl") {
sources = [
"main.c",
"misc_daemon.cpp",
"init_cmd_reboot.c",
"service_control.c",
"param_cmd.c"
]
defines = [ "INIT_AGENT" ]
include_dirs = [
"//base/update/updater/interfaces/kits/include/misc_info",
"//base/startup/init_lite/services/include",
"//base/startup/init_lite/services/log",
"//base/startup/init_lite/interfaces/innerkits/include",
"//base/update/updater/interfaces/kits/include/misc_info",
"//base/startup/syspara_lite/interfaces/innerkits/native/syspara/include",
"//base/startup/init_lite/services/include/param/",
"//base/startup/init_lite/services/param/include",
"//third_party/bounds_checking_function/include",
"//base/update/updateservice/interfaces/innerkits/include",
]
deps = [
"//base/startup/init_lite/interfaces/innerkits/fs_manager:libfsmanager_shared",
"//base/startup/syspara_lite/interfaces/innerkits/native/syspara:syspara",
"//base/startup/init_lite/interfaces/innerkits/reboot:libreboot_static",
"//base/startup/init_lite/services/log:agent_log",
"//base/startup/init_lite/services/param:param_client",
"//third_party/bounds_checking_function:libsec_static",
]
external_deps = [ "hiviewdfx_hilog_native:libhilog" ]
symlink_target_name = [
"misc_daemon",
"reboot",
"devctl",
"service",
"service_control",
"start_service",
"stop_service",
"service",
"param"
]
install_images = [ "system" ]
install_enable = true
part_name = "init"
}
/*
* 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 __BEGETCTL_CMD_H
#define __BEGETCTL_CMD_H
#ifdef __cplusplus
#if __cplusplus
extern "C" {
#endif
#endif
typedef int (*BegetCtlCmdPtr)(int argc, char **argv);
int BegetCtlCmdAdd(const char *name, BegetCtlCmdPtr cmd);
#define MODULE_CONSTRUCTOR() static void _init(void) __attribute__((constructor)); static void _init(void)
#define MODULE_DESTRUCTOR() static void _destroy(void) __attribute__((destructor)); static void _destroy(void)
#ifdef __cplusplus
#if __cplusplus
}
#endif
#endif
#endif // INIT_UTILS_H
/*
* 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 <stdio.h>
#include <string.h>
#include <unistd.h>
#include "init_reboot.h"
#define REBOOT_CMD_NUMBER 2
#define USAGE_INFO "usage: reboot shutdown\n"\
" reboot updater\n"\
" reboot updater[:options]\n" \
" reboot flashd\n" \
" reboot flashd[:options]\n" \
" reboot loader\n" \
" reboot\n"
int main(int argc, char* argv[])
{
if (argc > REBOOT_CMD_NUMBER) {
printf("%s", USAGE_INFO);
return 0;
}
if (argc == REBOOT_CMD_NUMBER && strcmp(argv[1], "shutdown") != 0 &&
strcmp(argv[1], "updater") != 0 &&
strcmp(argv[1], "flashd") != 0 &&
strcmp(argv[1], "loader") !=0 &&
strncmp(argv[1], "updater:", strlen("updater:")) != 0 &&
strncmp(argv[1], "flashd:", strlen("flashd:")) != 0) {
printf("%s", USAGE_INFO);
return 0;
}
int ret = 0;
if (argc == REBOOT_CMD_NUMBER) {
ret = DoReboot(argv[1]);
} else {
ret = DoReboot(NULL);
}
if (ret != 0) {
printf("[reboot command] DoReboot Api return error\n");
} else {
printf("[reboot command] DoReboot Api return ok\n");
}
while (1) {
pause();
}
return 0;
}
/*
* 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 <stdio.h>
#include <string.h>
#include <unistd.h>
#include "init_reboot.h"
#include "begetctl.h"
#define REBOOT_CMD_NUMBER 2
#define USAGE_INFO "usage: reboot shutdown\n"\
" reboot updater\n"\
" reboot updater[:options]\n" \
" reboot flashd\n" \
" reboot flashd[:options]\n" \
" reboot loader\n" \
" reboot\n"
static int main_cmd(int argc, char* argv[])
{
if (argc > REBOOT_CMD_NUMBER) {
printf("%s", USAGE_INFO);
return 0;
}
if (argc == REBOOT_CMD_NUMBER && strcmp(argv[1], "shutdown") != 0 &&
strcmp(argv[1], "updater") != 0 &&
strcmp(argv[1], "flashd") != 0 &&
strcmp(argv[1], "loader") !=0 &&
strncmp(argv[1], "updater:", strlen("updater:")) != 0 &&
strncmp(argv[1], "flashd:", strlen("flashd:")) != 0) {
printf("%s", USAGE_INFO);
return 0;
}
int ret = 0;
if (argc == REBOOT_CMD_NUMBER) {
ret = DoReboot(argv[1]);
} else {
ret = DoReboot(NULL);
}
if (ret != 0) {
printf("[reboot command] DoReboot Api return error\n");
} else {
printf("[reboot command] DoReboot Api return ok\n");
}
while (1) {
pause();
}
return 0;
}
MODULE_CONSTRUCTOR()
{
(void)BegetCtlCmdAdd("reboot", main_cmd);
(void)BegetCtlCmdAdd("devctl", main_cmd);
}
/*
* 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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "begetctl.h"
struct CMD_LIST_ST {
struct CMD_LIST_ST *next;
const char *name;
BegetCtlCmdPtr cmd;
};
static struct CMD_LIST_ST *m_cmdList = NULL;
int BegetCtlCmdAdd(const char *name, BegetCtlCmdPtr cmd)
{
struct CMD_LIST_ST *item;
if (name == NULL) {
return -1;
}
item = (struct CMD_LIST_ST *)malloc(sizeof(struct CMD_LIST_ST));
if (item == NULL) {
return -1;
}
item->name = strdup(name);
if (item->name == NULL) {
free((void *)item);
return -1;
}
item->cmd = cmd;
item->next = m_cmdList;
m_cmdList = item;
return 0;
}
static const struct CMD_LIST_ST *BegetCtlCmdFind(const char *name)
{
const struct CMD_LIST_ST *item = m_cmdList;
while (item != NULL) {
if (strcmp(item->name, name) == 0) {
return item;
}
item = item->next;
}
return NULL;
}
static void BegetCtlUsage(const char *command)
{
const struct CMD_LIST_ST *item = m_cmdList;
int notFirst = 0;
while (item != NULL) {
if (notFirst) {
printf(" ");
}
notFirst = 1;
printf("%s", item->name);
item = item->next;
}
printf("\n");
}
int main(int argc, char **argv)
{
const struct CMD_LIST_ST *cmd;
const char *last = strrchr(argv[0], '/');
// Get the first ending command name
if (last != NULL) {
last = last + 1;
} else {
last = argv[0];
}
// If it is begetctl with subcommand name, try to do subcommand first
if ((argc > 1) && (strcmp(last, "begetctl") == 0)) {
argc = argc - 1;
argv = argv + 1;
last = argv[0];
}
// Match the command
cmd = BegetCtlCmdFind(last);
if (cmd == NULL) {
BegetCtlUsage(last);
return 0;
}
// Do command
return cmd->cmd(argc, argv);
}
/*
* 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 <cerrno>
#include <cstdint>
#include <fcntl.h>
#include <getopt.h>
#include <iostream>
#include <string>
#include <sys/stat.h>
#include "fs_manager/fs_manager.h"
#include "misc_info.h"
#include "param_wrapper.h"
constexpr int PARTITION_INFO_POS = 1144;
constexpr int PARTITION_INFO_MAX_LENGTH = 256;
constexpr int BLOCK_SZIE_1 = 512;
constexpr uint64_t LOGO_MAGIC = 0XABCABCAB;
struct option g_options[] = {
{ "write_logo", required_argument, nullptr, 0 },
{ nullptr, 0, nullptr, 0 },
};
static std::string GetMiscDevicePath()
{
std::string miscDev {};
// Get misc device path from fstab
std::string hardwareVal {};
int ret = OHOS::system::GetStringParameter("ohos.boot.hardware", hardwareVal, "");
if (ret != 0) {
std::cout << "get ohos.boot.hardware failed\n";
return "";
}
std::string fstabFileName = std::string("fstab.") + hardwareVal;
std::string fstabFile = std::string("/vendor/etc/") + fstabFileName;
Fstab *fstab = ReadFstabFromFile(fstabFile.c_str(), false);
if (fstab == nullptr) {
std::cout << "Failed to read fstab\n";
return miscDev;
}
FstabItem *misc = FindFstabItemForMountPoint(*fstab, "/misc");
if (misc == nullptr) {
std::cout << "Cannot find misc partition from fstab\n";
ReleaseFstab(fstab);
return miscDev;
}
miscDev = misc->deviceName;
ReleaseFstab(fstab);
return miscDev;
}
static void ClearLogo(int fd)
{
if (fd < 0) {
return;
}
char buffer[8] = {0}; // logo magic number + logo size is 8
int addrOffset = (PARTITION_INFO_POS + PARTITION_INFO_MAX_LENGTH + BLOCK_SZIE_1 - 1) / BLOCK_SZIE_1;
if (lseek(fd, addrOffset * BLOCK_SZIE_1, SEEK_SET) < 0) {
std::cout << "Failed to clean file\n";
return;
}
if (write(fd, &buffer, sizeof(buffer)) != sizeof(buffer)) {
std::cout << "clean misc failed\n";
return;
}
}
static void WriteLogoContent(int fd, const std::string &logoPath, uint32_t size)
{
if (fd < 0 || logoPath.empty() || size <= 0) {
std::cout << "path is null or size illegal\n";
return;
}
FILE *rgbFile = fopen(logoPath.c_str(), "rb");
if (rgbFile == nullptr) {
std::cout << "cannot find pic file\n";
return;
}
char *buffer = (char*)malloc(size);
if (buffer == nullptr) {
(void)fclose(rgbFile);
return;
}
uint32_t ret = fread(buffer, 1, size, rgbFile);
if (ret < 0) {
(void)fclose(rgbFile);
free(buffer);
return;
}
ret = write(fd, buffer, size);
if (ret != size) {
(void)fclose(rgbFile);
free(buffer);
return;
}
if (buffer != nullptr) {
free(buffer);
buffer = nullptr;
}
(void)fclose(rgbFile);
}
static int WriteLogo(int fd, const std::string &logoPath)
{
if (fd < 0 || logoPath.empty()) {
std::cout << "Invalid arguments\n";
return -1;
}
int addrOffset = (PARTITION_INFO_POS + PARTITION_INFO_MAX_LENGTH + BLOCK_SZIE_1 - 1) / BLOCK_SZIE_1;
if (lseek(fd, addrOffset * BLOCK_SZIE_1, SEEK_SET) < 0) {
std::cout << "Failed to seek file\n";
return -1;
}
uint32_t magic = 0;
if (read(fd, &magic, sizeof(uint32_t)) != sizeof(uint32_t)) {
std::cout << "Failed to read magic number\n";
return -1;
}
if (magic == LOGO_MAGIC) {
std::cout << "Get matched magic number, logo already written\n";
return 0;
}
struct stat st {};
magic = LOGO_MAGIC;
lseek(fd, addrOffset * BLOCK_SZIE_1, SEEK_SET);
if (write(fd, &magic, sizeof(magic)) != sizeof(magic)) {
std::cout << "Write magic number failed\n";
return -1;
}
if (stat(logoPath.c_str(), &st) < 0) {
if (errno == ENOENT) {
std::cout << logoPath << " is not exist\n";
} else {
std::cout << "Failed to get " << logoPath << " stat\n";
}
ClearLogo(fd);
return -1;
}
if (st.st_size < 0 || st.st_size > updater::MAX_LOGO_SIZE) {
std::cout << "Invalid logo file with size \n";
ClearLogo(fd);
return -1;
}
uint32_t logoSize = static_cast<uint32_t>(st.st_size);
if (write(fd, &logoSize, sizeof(logoSize)) != sizeof(logoSize)) {
std::cout << "Write logo size failed\n";
ClearLogo(fd);
return -1;
}
WriteLogoContent(fd, logoPath, logoSize);
return 0;
}
static void WriteLogoToMisc(const std::string &logoPath)
{
if (logoPath.empty()) {
std::cout << "logo path is empty\n";
return;
}
std::string miscDev = GetMiscDevicePath();
if (miscDev.empty()) {
return;
}
int fd = open(miscDev.c_str(), O_RDWR | O_CLOEXEC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
if (fd < 0) {
std::cout << "Failed to open " << miscDev << ", err = " << errno << std::endl;
return;
}
if (WriteLogo(fd, logoPath) < 0) {
std::cout << "Write logo to " << miscDev << " failed" << std::endl;
}
close(fd);
int addrOffset = (PARTITION_INFO_POS + PARTITION_INFO_MAX_LENGTH + BLOCK_SZIE_1 - 1) / BLOCK_SZIE_1;
int fd1 = open(miscDev.c_str(), O_RDWR | O_CLOEXEC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
if (lseek(fd1, addrOffset * BLOCK_SZIE_1, SEEK_SET) < 0) {
std::cout << "Failed to seek file\n";
close(fd1);
return;
}
uint32_t magic = 0;
uint32_t size = 0;
if (read(fd1, &magic, sizeof(uint32_t)) != sizeof(uint32_t)) {
std::cout << "Failed to read magic number\n";
return;
}
if (read(fd1, &size, sizeof(uint32_t)) != sizeof(uint32_t)) {
std::cout << "Failed to read magic number\n";
return;
}
close(fd1);
}
int main(int argc, char **argv)
{
int rc = -1;
int optIndex = -1;
while ((rc = getopt_long(argc, argv, "", g_options, &optIndex)) != -1) {
switch (rc) {
case 0: {
std::string optionName = g_options[optIndex].name;
if (optionName == "write_logo") {
std::string logoPath = optarg;
WriteLogoToMisc(logoPath);
}
break;
}
case '?':
std::cout << "Invalid arugment\n";
break;
default:
std::cout << "Invalid arugment\n";
break;
}
}
}
/*
* 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 <cerrno>
#include <cstdint>
#include <fcntl.h>
#include <getopt.h>
#include <iostream>
#include <string>
#include <sys/stat.h>
#include "fs_manager/fs_manager.h"
#include "misc_info.h"
#include "param_wrapper.h"
#include "begetctl.h"
constexpr int PARTITION_INFO_POS = 1144;
constexpr int PARTITION_INFO_MAX_LENGTH = 256;
constexpr int BLOCK_SZIE_1 = 512;
constexpr uint64_t LOGO_MAGIC = 0XABCABCAB;
struct option g_options[] = {
{ "write_logo", required_argument, nullptr, 0 },
{ nullptr, 0, nullptr, 0 },
};
static std::string GetMiscDevicePath()
{
std::string miscDev {};
// Get misc device path from fstab
std::string hardwareVal {};
int ret = OHOS::system::GetStringParameter("ohos.boot.hardware", hardwareVal, "");
if (ret != 0) {
std::cout << "get ohos.boot.hardware failed\n";
return "";
}
std::string fstabFileName = std::string("fstab.") + hardwareVal;
std::string fstabFile = std::string("/vendor/etc/") + fstabFileName;
Fstab *fstab = ReadFstabFromFile(fstabFile.c_str(), false);
if (fstab == nullptr) {
std::cout << "Failed to read fstab\n";
return miscDev;
}
FstabItem *misc = FindFstabItemForMountPoint(*fstab, "/misc");
if (misc == nullptr) {
std::cout << "Cannot find misc partition from fstab\n";
ReleaseFstab(fstab);
return miscDev;
}
miscDev = misc->deviceName;
ReleaseFstab(fstab);
return miscDev;
}
static void ClearLogo(int fd)
{
if (fd < 0) {
return;
}
char buffer[8] = {0}; // logo magic number + logo size is 8
int addrOffset = (PARTITION_INFO_POS + PARTITION_INFO_MAX_LENGTH + BLOCK_SZIE_1 - 1) / BLOCK_SZIE_1;
if (lseek(fd, addrOffset * BLOCK_SZIE_1, SEEK_SET) < 0) {
std::cout << "Failed to clean file\n";
return;
}
if (write(fd, &buffer, sizeof(buffer)) != sizeof(buffer)) {
std::cout << "clean misc failed\n";
return;
}
}
static void WriteLogoContent(int fd, const std::string &logoPath, uint32_t size)
{
if (fd < 0 || logoPath.empty() || size <= 0) {
std::cout << "path is null or size illegal\n";
return;
}
FILE *rgbFile = fopen(logoPath.c_str(), "rb");
if (rgbFile == nullptr) {
std::cout << "cannot find pic file\n";
return;
}
char *buffer = (char*)malloc(size);
if (buffer == nullptr) {
(void)fclose(rgbFile);
return;
}
uint32_t ret = fread(buffer, 1, size, rgbFile);
if (ret < 0) {
(void)fclose(rgbFile);
free(buffer);
return;
}
ret = write(fd, buffer, size);
if (ret != size) {
(void)fclose(rgbFile);
free(buffer);
return;
}
if (buffer != nullptr) {
free(buffer);
buffer = nullptr;
}
(void)fclose(rgbFile);
}
static int WriteLogo(int fd, const std::string &logoPath)
{
if (fd < 0 || logoPath.empty()) {
std::cout << "Invalid arguments\n";
return -1;
}
int addrOffset = (PARTITION_INFO_POS + PARTITION_INFO_MAX_LENGTH + BLOCK_SZIE_1 - 1) / BLOCK_SZIE_1;
if (lseek(fd, addrOffset * BLOCK_SZIE_1, SEEK_SET) < 0) {
std::cout << "Failed to seek file\n";
return -1;
}
uint32_t magic = 0;
if (read(fd, &magic, sizeof(uint32_t)) != sizeof(uint32_t)) {
std::cout << "Failed to read magic number\n";
return -1;
}
if (magic == LOGO_MAGIC) {
std::cout << "Get matched magic number, logo already written\n";
return 0;
}
struct stat st {};
magic = LOGO_MAGIC;
lseek(fd, addrOffset * BLOCK_SZIE_1, SEEK_SET);
if (write(fd, &magic, sizeof(magic)) != sizeof(magic)) {
std::cout << "Write magic number failed\n";
return -1;
}
if (stat(logoPath.c_str(), &st) < 0) {
if (errno == ENOENT) {
std::cout << logoPath << " is not exist\n";
} else {
std::cout << "Failed to get " << logoPath << " stat\n";
}
ClearLogo(fd);
return -1;
}
if (st.st_size < 0 || st.st_size > updater::MAX_LOGO_SIZE) {
std::cout << "Invalid logo file with size \n";
ClearLogo(fd);
return -1;
}
uint32_t logoSize = static_cast<uint32_t>(st.st_size);
if (write(fd, &logoSize, sizeof(logoSize)) != sizeof(logoSize)) {
std::cout << "Write logo size failed\n";
ClearLogo(fd);
return -1;
}
WriteLogoContent(fd, logoPath, logoSize);
return 0;
}
static void WriteLogoToMisc(const std::string &logoPath)
{
if (logoPath.empty()) {
std::cout << "logo path is empty\n";
return;
}
std::string miscDev = GetMiscDevicePath();
if (miscDev.empty()) {
return;
}
int fd = open(miscDev.c_str(), O_RDWR | O_CLOEXEC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
if (fd < 0) {
std::cout << "Failed to open " << miscDev << ", err = " << errno << std::endl;
return;
}
if (WriteLogo(fd, logoPath) < 0) {
std::cout << "Write logo to " << miscDev << " failed" << std::endl;
}
close(fd);
int addrOffset = (PARTITION_INFO_POS + PARTITION_INFO_MAX_LENGTH + BLOCK_SZIE_1 - 1) / BLOCK_SZIE_1;
int fd1 = open(miscDev.c_str(), O_RDWR | O_CLOEXEC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
if (lseek(fd1, addrOffset * BLOCK_SZIE_1, SEEK_SET) < 0) {
std::cout << "Failed to seek file\n";
close(fd1);
return;
}
uint32_t magic = 0;
uint32_t size = 0;
if (read(fd1, &magic, sizeof(uint32_t)) != sizeof(uint32_t)) {
std::cout << "Failed to read magic number\n";
return;
}
if (read(fd1, &size, sizeof(uint32_t)) != sizeof(uint32_t)) {
std::cout << "Failed to read magic number\n";
return;
}
close(fd1);
}
static int main_cmd(int argc, char **argv)
{
int rc = -1;
int optIndex = -1;
while ((rc = getopt_long(argc, argv, "", g_options, &optIndex)) != -1) {
switch (rc) {
case 0: {
std::string optionName = g_options[optIndex].name;
if (optionName == "write_logo") {
std::string logoPath = optarg;
WriteLogoToMisc(logoPath);
}
break;
}
case '?':
std::cout << "Invalid arugment\n";
break;
default:
std::cout << "Invalid arugment\n";
break;
}
}
return 0;
}
MODULE_CONSTRUCTOR()
{
(void)BegetCtlCmdAdd("misc_daemon", main_cmd);
}
/*
* 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 <stdio.h>
#include <string.h>
#include "param_manager.h"
#include "param_utils.h"
#include "sys_param.h"
#define USAGE_INFO_PARAM_GET "param get [key]"
#define USAGE_INFO_PARAM_SET "param set key value"
#define USAGE_INFO_PARAM_WAIT "param wait key value"
#define USAGE_INFO_PARAM_DUMP "param dump [verbose]"
#define USAGE_INFO_PARAM_READ "param read key"
#define USAGE_INFO_PARAM_WATCH "param watch key"
#define READ_DURATION 100000
#define MIN_ARGC 2
#define WAIT_TIMEOUT_INDEX 2
struct CmdArgs {
char name[8]; // cmd name length
int minArg;
void (*DoFuncion)(int argc, char *argv[], int start);
char help[128]; // cmd help length
};
static void ShowParam(ParamHandle handle, void *cookie)
{
char *name = (char *)cookie;
char *value = ((char *)cookie) + PARAM_NAME_LEN_MAX;
SystemGetParameterName(handle, name, PARAM_NAME_LEN_MAX);
uint32_t size = PARAM_CONST_VALUE_LEN_MAX;
SystemGetParameterValue(handle, value, &size);
printf("\t%s = %s \n", name, value);
}
static void ExeuteCmdParamGet(int argc, char *argv[], int start)
{
uint32_t size = PARAM_CONST_VALUE_LEN_MAX + PARAM_NAME_LEN_MAX + 1 + 1;
char *buffer = (char *)calloc(1, size);
PARAM_CHECK(buffer != NULL, return, "Failed to get parameter");
if (argc == start) {
SystemTraversalParameter(ShowParam, (void *)buffer);
} else {
int ret = SystemGetParameter(argv[start], buffer, &size);
if (ret == 0) {
printf("%s \n", buffer);
} else {
printf("Get parameter \"%s\" fail\n", argv[start]);
}
}
free(buffer);
}
static void ExeuteCmdParamSet(int argc, char *argv[], int start)
{
UNUSED(argc);
int ret = SystemSetParameter(argv[start], argv[start + 1]);
if (ret == 0) {
printf("Set parameter %s %s success\n", argv[start], argv[start + 1]);
} else {
printf("Set parameter %s %s fail\n", argv[start], argv[start + 1]);
}
return;
}
static void ExeuteCmdParamDump(int argc, char *argv[], int start)
{
int verbose = 0;
if (argc > start && strcmp(argv[start], "verbose") == 0) {
verbose = 1;
}
SystemDumpParameters(verbose);
}
static void ExeuteCmdParamWait(int argc, char *argv[], int start)
{
char *value = NULL;
uint32_t timeout = DEFAULT_PARAM_WAIT_TIMEOUT;
if (argc > (start + 1)) {
value = argv[start + 1];
}
if (argc > (start + WAIT_TIMEOUT_INDEX)) {
timeout = atol(argv[start + WAIT_TIMEOUT_INDEX]);
}
int ret = SystemWaitParameter(argv[start], value, timeout);
if (ret == 0) {
printf("Param wait success \n");
} else {
printf("Param wait fail \n");
}
}
#ifdef PARAM_TEST
static void ExeuteCmdParamRead(int argc, char *argv[], int start)
{
SystemSetParameter("test.randrom.test.start", "1");
char buffer[PARAM_NAME_LEN_MAX] = {0};
printf("SystemGetParameter start \n");
while (1) {
int wait = READ_DURATION + READ_DURATION; // 100ms
uint32_t size = PARAM_NAME_LEN_MAX;
int ret = SystemGetParameter(argv[start], buffer, &size);
if (ret == 0) {
printf("SystemGetParameter value %s %d \n", buffer, wait);
} else {
printf("SystemGetParameter fail %d \n", wait);
}
usleep(wait);
}
}
static void HandleParamChange(const char *key, const char *value, void *context)
{
if (key == NULL || value == NULL) {
return;
}
UNUSED(context);
printf("Receive parameter change %s %s \n", key, value);
}
static void ExeuteCmdParamWatch(int argc, char *argv[], int start)
{
if (argc <= start) {
return;
}
int ret = SystemWatchParameter(argv[start], HandleParamChange, NULL);
if (ret != 0) {
return;
}
while (1) {
(void)pause();
}
}
#endif
int RunParamCommand(int argc, char *argv[])
{
static struct CmdArgs paramCmds[] = {
{ "set", 4, ExeuteCmdParamSet, USAGE_INFO_PARAM_SET }, // set param count
{ "get", 2, ExeuteCmdParamGet, USAGE_INFO_PARAM_GET }, // get param count
{ "wait", 3, ExeuteCmdParamWait, USAGE_INFO_PARAM_WAIT }, // wait param count
{ "dump", 2, ExeuteCmdParamDump, USAGE_INFO_PARAM_DUMP }, // dump param count
#ifdef PARAM_TEST
{ "read", 2, ExeuteCmdParamRead, USAGE_INFO_PARAM_READ }, // read param count
{ "watch", 2, ExeuteCmdParamWatch, USAGE_INFO_PARAM_WATCH }, // watch param count
#endif
};
if (argc < MIN_ARGC) {
printf("usage: \n");
for (size_t i = 0; i < sizeof(paramCmds) / sizeof(paramCmds[0]); i++) {
printf("\t %s\n", paramCmds[i].help);
}
return 0;
}
for (size_t i = 0; i < sizeof(paramCmds) / sizeof(paramCmds[0]); i++) {
if (argv[1] != NULL && strcmp(argv[1], paramCmds[i].name) == 0) {
if (argc < paramCmds[i].minArg) {
printf("usage: %s\n", paramCmds[i].help);
return 0;
}
paramCmds[i].DoFuncion(argc, argv, MIN_ARGC);
return 0;
}
}
printf("usage: \n");
for (size_t i = 0; i < sizeof(paramCmds) / sizeof(paramCmds[0]); i++) {
printf("\t%s\n", paramCmds[i].help);
}
return 0;
}
#ifndef STARTUP_INIT_TEST
int main(int argc, char *argv[])
{
return RunParamCommand(argc, argv);
}
#endif
\ No newline at end of file
/*
* 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 <stdio.h>
#include <string.h>
#include "param_manager.h"
#include "param_utils.h"
#include "sys_param.h"
#include "begetctl.h"
#define USAGE_INFO_PARAM_GET "param get [key]"
#define USAGE_INFO_PARAM_SET "param set key value"
#define USAGE_INFO_PARAM_WAIT "param wait key value"
#define USAGE_INFO_PARAM_DUMP "param dump [verbose]"
#define USAGE_INFO_PARAM_READ "param read key"
#define USAGE_INFO_PARAM_WATCH "param watch key"
#define READ_DURATION 100000
#define MIN_ARGC 2
#define WAIT_TIMEOUT_INDEX 2
struct CmdArgs {
char name[8]; // cmd name length
int minArg;
void (*DoFuncion)(int argc, char *argv[], int start);
char help[128]; // cmd help length
};
static void ShowParam(ParamHandle handle, void *cookie)
{
char *name = (char *)cookie;
char *value = ((char *)cookie) + PARAM_NAME_LEN_MAX;
SystemGetParameterName(handle, name, PARAM_NAME_LEN_MAX);
uint32_t size = PARAM_CONST_VALUE_LEN_MAX;
SystemGetParameterValue(handle, value, &size);
printf("\t%s = %s \n", name, value);
}
static void ExeuteCmdParamGet(int argc, char *argv[], int start)
{
uint32_t size = PARAM_CONST_VALUE_LEN_MAX + PARAM_NAME_LEN_MAX + 1 + 1;
char *buffer = (char *)calloc(1, size);
PARAM_CHECK(buffer != NULL, return, "Failed to get parameter");
if (argc == start) {
SystemTraversalParameter(ShowParam, (void *)buffer);
} else {
int ret = SystemGetParameter(argv[start], buffer, &size);
if (ret == 0) {
printf("%s \n", buffer);
} else {
printf("Get parameter \"%s\" fail\n", argv[start]);
}
}
free(buffer);
}
static void ExeuteCmdParamSet(int argc, char *argv[], int start)
{
UNUSED(argc);
int ret = SystemSetParameter(argv[start], argv[start + 1]);
if (ret == 0) {
printf("Set parameter %s %s success\n", argv[start], argv[start + 1]);
} else {
printf("Set parameter %s %s fail\n", argv[start], argv[start + 1]);
}
return;
}
static void ExeuteCmdParamDump(int argc, char *argv[], int start)
{
int verbose = 0;
if (argc > start && strcmp(argv[start], "verbose") == 0) {
verbose = 1;
}
SystemDumpParameters(verbose);
}
static void ExeuteCmdParamWait(int argc, char *argv[], int start)
{
char *value = NULL;
uint32_t timeout = DEFAULT_PARAM_WAIT_TIMEOUT;
if (argc > (start + 1)) {
value = argv[start + 1];
}
if (argc > (start + WAIT_TIMEOUT_INDEX)) {
timeout = atol(argv[start + WAIT_TIMEOUT_INDEX]);
}
int ret = SystemWaitParameter(argv[start], value, timeout);
if (ret == 0) {
printf("Param wait success \n");
} else {
printf("Param wait fail \n");
}
}
#ifdef PARAM_TEST
static void ExeuteCmdParamRead(int argc, char *argv[], int start)
{
SystemSetParameter("test.randrom.test.start", "1");
char buffer[PARAM_NAME_LEN_MAX] = {0};
printf("SystemGetParameter start \n");
while (1) {
int wait = READ_DURATION + READ_DURATION; // 100ms
uint32_t size = PARAM_NAME_LEN_MAX;
int ret = SystemGetParameter(argv[start], buffer, &size);
if (ret == 0) {
printf("SystemGetParameter value %s %d \n", buffer, wait);
} else {
printf("SystemGetParameter fail %d \n", wait);
}
usleep(wait);
}
}
static void HandleParamChange(const char *key, const char *value, void *context)
{
if (key == NULL || value == NULL) {
return;
}
UNUSED(context);
printf("Receive parameter change %s %s \n", key, value);
}
static void ExeuteCmdParamWatch(int argc, char *argv[], int start)
{
if (argc <= start) {
return;
}
int ret = SystemWatchParameter(argv[start], HandleParamChange, NULL);
if (ret != 0) {
return;
}
while (1) {
(void)pause();
}
}
#endif
int RunParamCommand(int argc, char *argv[])
{
static struct CmdArgs paramCmds[] = {
{ "set", 4, ExeuteCmdParamSet, USAGE_INFO_PARAM_SET }, // set param count
{ "get", 2, ExeuteCmdParamGet, USAGE_INFO_PARAM_GET }, // get param count
{ "wait", 3, ExeuteCmdParamWait, USAGE_INFO_PARAM_WAIT }, // wait param count
{ "dump", 2, ExeuteCmdParamDump, USAGE_INFO_PARAM_DUMP }, // dump param count
#ifdef PARAM_TEST
{ "read", 2, ExeuteCmdParamRead, USAGE_INFO_PARAM_READ }, // read param count
{ "watch", 2, ExeuteCmdParamWatch, USAGE_INFO_PARAM_WATCH }, // watch param count
#endif
};
if (argc < MIN_ARGC) {
printf("usage: \n");
for (size_t i = 0; i < sizeof(paramCmds) / sizeof(paramCmds[0]); i++) {
printf("\t %s\n", paramCmds[i].help);
}
return 0;
}
for (size_t i = 0; i < sizeof(paramCmds) / sizeof(paramCmds[0]); i++) {
if (argv[1] != NULL && strcmp(argv[1], paramCmds[i].name) == 0) {
if (argc < paramCmds[i].minArg) {
printf("usage: %s\n", paramCmds[i].help);
return 0;
}
paramCmds[i].DoFuncion(argc, argv, MIN_ARGC);
return 0;
}
}
printf("usage: \n");
for (size_t i = 0; i < sizeof(paramCmds) / sizeof(paramCmds[0]); i++) {
printf("\t%s\n", paramCmds[i].help);
}
return 0;
}
#ifndef STARTUP_INIT_TEST
static int main_cmd(int argc, char *argv[])
{
return RunParamCommand(argc, argv);
}
MODULE_CONSTRUCTOR()
{
(void)BegetCtlCmdAdd("param", main_cmd);
}
#endif
......@@ -17,6 +17,7 @@
#include <stdio.h>
#include "securec.h"
#include "sys_param.h"
#include "begetctl.h"
#define SERVICE_START_NUMBER 2
#define SERVICE_CONTROL_NUMBER 3
......@@ -61,7 +62,7 @@ static void ServiceControl(int argc, char** argv)
return;
}
int main(int argc, char** argv)
static int main_cmd(int argc, char** argv)
{
if (argc != SERVICE_START_NUMBER && argc != SERVICE_CONTROL_NUMBER) {
ServiceControlUsage();
......@@ -90,3 +91,11 @@ int main(int argc, char** argv)
}
return 0;
}
MODULE_CONSTRUCTOR()
{
(void)BegetCtlCmdAdd("service", main_cmd);
(void)BegetCtlCmdAdd("service_control", main_cmd);
(void)BegetCtlCmdAdd("start_service", main_cmd);
(void)BegetCtlCmdAdd("stop_service", main_cmd);
}
# 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_executable("reboot") {
sources = [ "init_cmd_reboot.c" ]
include_dirs = [
"//base/startup/init_lite/interfaces/innerkits/include",
"//base/startup/init_lite/services/include/param/",
"//third_party/bounds_checking_function/include",
]
deps = [
"//base/startup/init_lite/interfaces/innerkits/reboot:libreboot_static",
]
install_enable = true
part_name = "init"
}
# 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_executable("service_control") {
sources = [ "service_control.c" ]
include_dirs = [
"//base/startup/init_lite/services/include/param",
"//base/startup/init_lite/services/include",
]
deps = [
"//base/startup/init_lite/services/param:param_client",
"//third_party/bounds_checking_function:libsec_static",
]
symlink_target_name = [
"start_service",
"stop_service",
]
install_images = [ "system" ]
install_enable = true
part_name = "init"
}
......@@ -206,36 +206,3 @@ ohos_shared_library("param_watcher") {
install_images = [ "system" ]
part_name = "init"
}
ohos_executable("param") {
sources = [ "cmd/param_cmd.c" ]
include_dirs = [
"include",
"//base/startup/init_lite/services/include/param",
"//base/startup/init_lite/services/include",
"//base/startup/init_lite/services/log",
"//base/update/updateservice/interfaces/innerkits/include",
]
defines = [ "INIT_AGENT" ]
deps = [
"//base/startup/init_lite/services/log:agent_log",
"//base/startup/init_lite/services/param:param_client",
"//third_party/bounds_checking_function:libsec_static",
]
external_deps = [ "hiviewdfx_hilog_native:libhilog" ]
if (param_test == "true") {
defines += [ "PARAM_TEST" ]
deps += [ "//base/startup/init_lite/services/param:param_watcheragent" ]
} else {
install_images = [
"system",
"updater",
]
}
install_enable = true
part_name = "init"
}
......@@ -39,6 +39,7 @@ ohos_unittest("init_ut") {
"//base/startup/init_lite/interfaces/innerkits/fs_manager/fstab_mount.c",
"//base/startup/init_lite/interfaces/innerkits/reboot/init_reboot_innerkits.c",
"//base/startup/init_lite/interfaces/innerkits/socket/init_socket.c",
"//base/startup/init_lite/services/begetctl/param_cmd.c",
"//base/startup/init_lite/services/init/adapter/init_adapter.c",
"//base/startup/init_lite/services/init/init_capability.c",
"//base/startup/init_lite/services/init/init_common_cmds.c",
......@@ -61,7 +62,6 @@ ohos_unittest("init_ut") {
"//base/startup/init_lite/services/param/adapter/param_persistadp.c",
"//base/startup/init_lite/services/param/adapter/param_selinux.c",
"//base/startup/init_lite/services/param/client/param_request.c",
"//base/startup/init_lite/services/param/cmd/param_cmd.c",
"//base/startup/init_lite/services/param/manager/param_manager.c",
"//base/startup/init_lite/services/param/manager/param_message.c",
"//base/startup/init_lite/services/param/manager/param_trie.c",
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册