!2634 add pre-start stage

Merge pull request !2634 from 杨浩/master
This commit is contained in:
openharmony_ci 2024-03-18 10:40:56 +00:00 committed by Gitee
commit 27cd4b7115
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
4 changed files with 37 additions and 28 deletions

View File

@ -122,6 +122,7 @@ typedef struct {
typedef enum {
JOB_ON_BOOT,
JOB_PRE_START,
JOB_ON_START,
JOB_ON_STOP,
JOB_ON_RESTART,

View File

@ -571,6 +571,31 @@ static int32_t WaitForDebugger(void)
}
#endif
static void RunChildProcess(Service *service, ServiceArgs *pathArgs)
{
// set selinux label by context
if (service->context.type != INIT_CONTEXT_MAIN && SetSubInitContext(&service->context, service->name) != 0) {
service->lastErrno = INIT_ECONTENT;
}
if (service->attribute & SERVICE_ATTR_MODULE_UPDATE) {
CheckModuleUpdate(pathArgs);
}
#ifdef IS_DEBUG_VERSION
// only the image is debuggable and need debug, then wait for debugger
if (ServiceNeedDebug(service->name) && IsDebuggableVersion()) {
WaitForDebugger();
}
#endif
// fail must exit sub process
int ret = InitServiceProperties(service, pathArgs);
INIT_ERROR_CHECK(ret == 0,
_exit(service->lastErrno), "Service error %d %s, failed to set properties", ret, service->name);
(void)ServiceExec(service, pathArgs);
_exit(service->lastErrno);
}
int ServiceStart(Service *service, ServiceArgs *pathArgs)
{
INIT_ERROR_CHECK(service != NULL, return SERVICE_FAILURE, "start service failed! null ptr.");
@ -592,34 +617,16 @@ int ServiceStart(Service *service, ServiceArgs *pathArgs)
return SERVICE_FAILURE;
}
#ifndef OHOS_LITE
/*
* before service fork hooks
*/
// before service fork hooks
ServiceHookExecute(service->name, NULL, INIT_SERVICE_FORK_BEFORE);
#endif
// pre-start job
if (service->serviceJobs.jobsName[JOB_PRE_START] != NULL) {
DoJobNow(service->serviceJobs.jobsName[JOB_PRE_START]);
}
int pid = fork();
if (pid == 0) {
// set selinux label by context
if (service->context.type != INIT_CONTEXT_MAIN && SetSubInitContext(&service->context, service->name) != 0) {
service->lastErrno = INIT_ECONTENT;
}
if (service->attribute & SERVICE_ATTR_MODULE_UPDATE) {
CheckModuleUpdate(pathArgs);
}
#ifdef IS_DEBUG_VERSION
// only the image is debuggable and need debug, then wait for debugger
if (ServiceNeedDebug(service->name) && IsDebuggableVersion()) {
WaitForDebugger();
}
#endif
// fail must exit sub process
int ret = InitServiceProperties(service, pathArgs);
INIT_ERROR_CHECK(ret == 0,
_exit(service->lastErrno), "Service error %d %s, failed to set properties", ret, service->name);
(void)ServiceExec(service, pathArgs);
_exit(service->lastErrno);
RunChildProcess(service, pathArgs);
} else if (pid < 0) {
INIT_LOGE("Service error %d %s, failed to fork.", errno, service->name);
service->lastErrno = INIT_EFORK;
@ -629,9 +636,7 @@ int ServiceStart(Service *service, ServiceArgs *pathArgs)
service->pid = pid;
NotifyServiceChange(service, SERVICE_STARTED);
#ifndef OHOS_LITE
/*
* after service fork hooks
*/
// after service fork hooks
ServiceHookExecute(service->name, (const char *)&pid, INIT_SERVICE_FORK_AFTER);
#endif
return SERVICE_SUCCESS;

View File

@ -632,7 +632,7 @@ static int GetServiceMode(Service *service, const cJSON *json)
static int GetServiceJobs(Service *service, cJSON *json)
{
const char *jobTypes[] = {
"on-boot", "on-start", "on-stop", "on-restart"
"on-boot", "pre-start", "on-start", "on-stop", "on-restart"
};
for (int i = 0; i < (int)ARRAY_LENGTH(jobTypes); i++) {
char *jobName = cJSON_GetStringValue(cJSON_GetObjectItem(json, jobTypes[i]));

View File

@ -66,6 +66,9 @@ static void DumpServiceJobs(const Service *service)
if (service->serviceJobs.jobsName[JOB_ON_BOOT] != NULL) {
printf("\t\tservice boot job %s \n", service->serviceJobs.jobsName[JOB_ON_BOOT]);
}
if (service->serviceJobs.jobsName[JOB_PRE_START]!= NULL) {
printf("\t\tservice pre-start job %s \n", service->serviceJobs.jobsName[JOB_PRE_START]);
}
if (service->serviceJobs.jobsName[JOB_ON_START] != NULL) {
printf("\t\tservice start job %s \n", service->serviceJobs.jobsName[JOB_ON_START]);
}