2022-04-27 01:23:43 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2021-2022 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 "appspawn_server.h"
|
|
|
|
|
2022-05-19 13:13:12 +00:00
|
|
|
#include <stdlib.h>
|
2022-04-27 01:23:43 +00:00
|
|
|
#include <errno.h>
|
2022-07-02 01:19:26 +00:00
|
|
|
#include <sys/stat.h>
|
2022-04-27 01:23:43 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#ifdef OHOS_DEBUG
|
|
|
|
#include <time.h>
|
|
|
|
#endif // OHOS_DEBUG
|
|
|
|
|
2022-06-13 16:10:17 +00:00
|
|
|
static void NotifyResToParent(struct AppSpawnContent_ *content, AppSpawnClient *client, int result)
|
2022-04-27 01:23:43 +00:00
|
|
|
{
|
|
|
|
if (content->notifyResToParent != NULL) {
|
|
|
|
content->notifyResToParent(content, client, result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-19 13:13:12 +00:00
|
|
|
static void ProcessExit(void)
|
|
|
|
{
|
|
|
|
APPSPAWN_LOGI("App exit %d.", getpid());
|
2022-06-14 07:06:32 +00:00
|
|
|
#ifndef APPSPAWN_TEST
|
2022-05-19 13:13:12 +00:00
|
|
|
#ifdef OHOS_LITE
|
|
|
|
_exit(0x7f); // 0x7f user exit
|
|
|
|
#else
|
|
|
|
quick_exit(0);
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2022-04-27 01:23:43 +00:00
|
|
|
int DoStartApp(struct AppSpawnContent_ *content, AppSpawnClient *client, char *longProcName, uint32_t longProcNameLen)
|
|
|
|
{
|
|
|
|
APPSPAWN_LOGI("DoStartApp id %d longProcNameLen %u", client->id, longProcNameLen);
|
|
|
|
int32_t ret = 0;
|
2022-06-10 09:23:02 +00:00
|
|
|
|
2022-04-27 01:23:43 +00:00
|
|
|
if (content->setAppSandbox) {
|
|
|
|
ret = content->setAppSandbox(content, client);
|
|
|
|
APPSPAWN_CHECK(ret == 0, NotifyResToParent(content, client, ret);
|
|
|
|
return ret, "Failed to set app sandbox");
|
|
|
|
}
|
2022-06-10 09:23:02 +00:00
|
|
|
|
2022-07-02 01:19:26 +00:00
|
|
|
(void)umask(0002);
|
2022-04-27 01:23:43 +00:00
|
|
|
if (content->setKeepCapabilities) {
|
|
|
|
ret = content->setKeepCapabilities(content, client);
|
|
|
|
APPSPAWN_CHECK(ret == 0, NotifyResToParent(content, client, ret);
|
|
|
|
return ret, "Failed to set KeepCapabilities");
|
|
|
|
}
|
2022-06-10 09:23:02 +00:00
|
|
|
|
2022-04-27 01:23:43 +00:00
|
|
|
if (content->setProcessName) {
|
|
|
|
ret = content->setProcessName(content, client, longProcName, longProcNameLen);
|
|
|
|
APPSPAWN_CHECK(ret == 0, NotifyResToParent(content, client, ret);
|
|
|
|
return ret, "Failed to set setProcessName");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (content->setUidGid) {
|
|
|
|
ret = content->setUidGid(content, client);
|
|
|
|
APPSPAWN_CHECK(ret == 0, NotifyResToParent(content, client, ret);
|
|
|
|
return ret, "Failed to setUidGid");
|
|
|
|
}
|
2022-06-10 09:23:02 +00:00
|
|
|
|
2022-04-27 01:23:43 +00:00
|
|
|
if (content->setFileDescriptors) {
|
|
|
|
ret = content->setFileDescriptors(content, client);
|
|
|
|
APPSPAWN_CHECK(ret == 0, NotifyResToParent(content, client, ret);
|
|
|
|
return ret, "Failed to setFileDescriptors");
|
|
|
|
}
|
2022-06-10 09:23:02 +00:00
|
|
|
|
2022-04-27 01:23:43 +00:00
|
|
|
if (content->setCapabilities) {
|
|
|
|
ret = content->setCapabilities(content, client);
|
|
|
|
APPSPAWN_CHECK(ret == 0, NotifyResToParent(content, client, ret);
|
|
|
|
return ret, "Failed to setCapabilities");
|
|
|
|
}
|
2022-06-10 09:23:02 +00:00
|
|
|
|
2022-04-27 01:23:43 +00:00
|
|
|
// notify success to father process and start app process
|
|
|
|
NotifyResToParent(content, client, 0);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-06-14 07:06:32 +00:00
|
|
|
int ForkChildProc(struct AppSpawnContent_ *content, AppSpawnClient *client, pid_t pid)
|
2022-04-27 01:23:43 +00:00
|
|
|
{
|
|
|
|
if (pid < 0) {
|
|
|
|
return -errno;
|
|
|
|
} else if (pid == 0) {
|
|
|
|
#ifdef OHOS_DEBUG
|
|
|
|
struct timespec tmStart = {0};
|
|
|
|
GetCurTime(&tmStart);
|
|
|
|
#endif // OHOS_DEBUG
|
|
|
|
|
|
|
|
// close socket id and signal for child
|
|
|
|
if (content->clearEnvironment != NULL) {
|
|
|
|
content->clearEnvironment(content, client);
|
|
|
|
}
|
2022-06-10 09:23:02 +00:00
|
|
|
|
2022-04-27 01:23:43 +00:00
|
|
|
if (content->setAppAccessToken != NULL) {
|
|
|
|
content->setAppAccessToken(content, client);
|
|
|
|
}
|
2022-06-10 09:23:02 +00:00
|
|
|
|
2022-04-27 01:23:43 +00:00
|
|
|
int ret = -1;
|
2022-06-27 04:02:11 +00:00
|
|
|
#ifdef ASAN_DETECTOR
|
|
|
|
if ((content->getWrapBundleNameValue != NULL && content->getWrapBundleNameValue(content, client) == 0)
|
|
|
|
|| (client->flags & APP_COLD_START)) {
|
|
|
|
#else
|
2022-04-27 01:23:43 +00:00
|
|
|
if (client->flags & APP_COLD_START) {
|
2022-06-27 04:02:11 +00:00
|
|
|
#endif
|
2022-04-27 01:23:43 +00:00
|
|
|
if (content->coldStartApp != NULL && content->coldStartApp(content, client) == 0) {
|
2022-06-14 07:06:32 +00:00
|
|
|
#ifndef APPSPAWN_TEST
|
2022-04-27 01:23:43 +00:00
|
|
|
_exit(0x7f); // 0x7f user exit
|
2022-06-14 07:06:32 +00:00
|
|
|
#endif
|
2022-04-27 01:23:43 +00:00
|
|
|
return -1;
|
|
|
|
} else {
|
|
|
|
ret = DoStartApp(content, client, content->longProcName, content->longProcNameLen);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ret = DoStartApp(content, client, content->longProcName, content->longProcNameLen);
|
|
|
|
}
|
2022-06-10 09:23:02 +00:00
|
|
|
|
2022-04-27 01:23:43 +00:00
|
|
|
#ifdef OHOS_DEBUG
|
|
|
|
struct timespec tmEnd = {0};
|
|
|
|
GetCurTime(&tmEnd);
|
|
|
|
// 1s = 1000000000ns
|
|
|
|
long timeUsed = (tmEnd.tv_sec - tmStart.tv_sec) * 1000000000L + (tmEnd.tv_nsec - tmStart.tv_nsec);
|
|
|
|
APPSPAWN_LOGI("App timeused %d %ld ns.", getpid(), timeUsed);
|
|
|
|
#endif // OHOS_DEBUG
|
2022-06-10 09:23:02 +00:00
|
|
|
|
2022-04-27 01:23:43 +00:00
|
|
|
if (ret == 0 && content->runChildProcessor != NULL) {
|
|
|
|
content->runChildProcessor(content, client);
|
|
|
|
}
|
2022-05-19 13:13:12 +00:00
|
|
|
ProcessExit();
|
2022-04-27 01:23:43 +00:00
|
|
|
}
|
2022-06-14 07:06:32 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int AppSpawnProcessMsg(struct AppSpawnContent_ *content, AppSpawnClient *client, pid_t *childPid)
|
|
|
|
{
|
|
|
|
APPSPAWN_CHECK(content != NULL, return -1, "Invalid content for appspawn");
|
|
|
|
APPSPAWN_CHECK(client != NULL && childPid != NULL, return -1, "Invalid client for appspawn");
|
|
|
|
APPSPAWN_LOGI("AppSpawnProcessMsg id %d 0x%x", client->id, client->flags);
|
|
|
|
#ifndef APPSPAWN_TEST
|
|
|
|
pid_t pid = fork();
|
|
|
|
#else
|
|
|
|
pid_t pid = 0;
|
|
|
|
#endif
|
|
|
|
int ret = ForkChildProc(content, client, pid);
|
|
|
|
APPSPAWN_CHECK(ret == 0, return ret, "fork child process error: %d", ret);
|
2022-04-27 01:23:43 +00:00
|
|
|
*childPid = pid;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef OHOS_DEBUG
|
|
|
|
void GetCurTime(struct timespec *tmCur)
|
|
|
|
{
|
|
|
|
if (tmCur == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
2022-06-10 09:23:02 +00:00
|
|
|
|
2022-04-27 01:23:43 +00:00
|
|
|
if (clock_gettime(CLOCK_REALTIME, tmCur) != 0) {
|
|
|
|
APPSPAWN_LOGE("[appspawn] invoke, get time failed! err %d", errno);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif // OHOS_DEBUG
|