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

!23 增加init_stage模块,用于init进程对系统进程及应用进程进行分阶段启动

Merge pull request !23 from boxi/master
# 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/lite/config/component/lite_component.gni")
lite_component("initstage") {
features = [ "//base/startup/init_lite/initstage:libinitstage_shared" ]
}
shared_library("libinitstage_shared") {
sources = [ "src/init_stage.c" ]
cflags = [ "-Wall" ]
include_dirs = [
"//base/startup/init_lite/initstage/include",
]
public_deps = [ "//third_party/bounds_checking_function:libsec_shared" ]
}
static_library("libinitstage_static") {
sources = [ "src/init_stage.c" ]
cflags = [ "-Wall" ]
include_dirs = [
"//base/startup/init_lite/initstage/include",
]
public_deps = [ "//third_party/bounds_checking_function:libsec_static" ]
}
/*
* 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 BASE_STARTUP_INITLITE_NOTIFY_H
#define BASE_STARTUP_INITLITE_NOTIFY_H
#ifdef __cplusplus
#if __cplusplus
extern "C" {
#endif
#endif
/*
* Notify the event to Init process.
* All processes can call. Usually called by the listened process.
* event: There needs to be a consensus between the listener(init process) and the notifier.
*/
extern int NotifyInit(unsigned short event);
#ifdef __cplusplus
#if __cplusplus
}
#endif
#endif
#endif // BASE_STARTUP_INITLITE_NOTIFY_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.
*/
#ifndef BASE_STARTUP_INITLITE_STAGE_H
#define BASE_STARTUP_INITLITE_STAGE_H
#include <sys/ioctl.h>
#include <unistd.h>
#ifdef __cplusplus
#if __cplusplus
extern "C" {
#endif
#endif
typedef enum {
QS_STAGE1 = 1, /* 1: start from stage1, 0 is already called in kernel process */
QS_STAGE2, /* system init stage No 2 */
QS_STAGE3, /* system init stage No 3 */
QS_STAGE_LIMIT
} QuickstartStage;
typedef enum {
QS_UNREGISTER = QS_STAGE_LIMIT, /* quickstart dev unregister */
QS_NOTIFY, /* quickstart notify */
QS_LISTEN, /* quickstart listen */
QS_CTL_LIMIT
} QuickstartConctrl;
typedef struct {
unsigned int pid;
unsigned int events;
} QuickstartMask;
#define QUICKSTART_IOC_MAGIC 'T'
#define QUICKSTART_UNREGISTER _IO(QUICKSTART_IOC_MAGIC, QS_UNREGISTER)
#define QUICKSTART_NOTIFY _IO(QUICKSTART_IOC_MAGIC, QS_NOTIFY)
#define QUICKSTART_LISTEN _IOR(QUICKSTART_IOC_MAGIC, QS_LISTEN, QuickstartMask)
#define QUICKSTART_STAGE(x) _IO(QUICKSTART_IOC_MAGIC, (x))
#define QUICKSTART_NODE "/dev/quickstart"
/* Simple sample Useage:
* INIT PROCESS
* SystemInitStage(QS_STAGE1)----(1)fork----> key APP
* |(2) |(3)
* InitListen<-------------------(4)---------- NotifyInit
* |(5)
* SystemInitStage(QS_STAGE2)----(6)fork---------------------> other APP
* |...
* InitStageFinished
*/
/*
* Listen the events of a specific pid process by Init process.
* Only be called by Init process.
* eventMask: There needs to be a consensus between the listener and the notifier.
*/
extern int InitListen(pid_t pid, unsigned short eventMask);
/*
* Trigger the SystemInit stage.
* Only be called by Init process.
*/
extern int SystemInitStage(QuickstartStage stage);
/*
* InitStage finished.
* Only be called by Init process.
*/
extern int InitStageFinished(void);
#ifdef __cplusplus
#if __cplusplus
}
#endif
#endif
#endif // BASE_STARTUP_INITLITE_STAGE_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 "init_stage.h"
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
static int SendCmd(int cmd, unsigned long arg)
{
int fd = open(QUICKSTART_NODE, O_RDONLY);
if (fd != -1) {
int ret = ioctl(fd, cmd, arg);
if (ret == -1) {
printf("[%s][%d]Err: %s!\n", __FUNCTION__, __LINE__, strerror(errno));
}
close(fd);
return ret;
}
printf("[%s][%d]Err: %s!\n", __FUNCTION__, __LINE__, strerror(errno));
return fd;
}
int InitListen(pid_t pid, unsigned short eventMask)
{
QuickstartMask listenMask;
listenMask.pid = pid;
listenMask.events = eventMask;
return SendCmd(QUICKSTART_LISTEN, (unsigned long)&listenMask);
}
int NotifyInit(unsigned short event)
{
return SendCmd(QUICKSTART_NOTIFY, event);
}
int SystemInitStage(QuickstartStage stage)
{
if (stage >= QS_STAGE_LIMIT || stage < QS_STAGE1) {
printf("[%s][%d]Err: the stage(%d) is Not expected!!\n", __FUNCTION__, __LINE__, stage);
return -1;
}
return SendCmd(QUICKSTART_STAGE(stage), 0);
}
int InitStageFinished(void)
{
return SendCmd(QUICKSTART_UNREGISTER, 0);
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册