fix appspawn timeout

Signed-off-by: cheng_jinsong <chengjinsong2@huawei.com>
This commit is contained in:
cheng_jinsong 2022-09-05 09:05:35 +08:00
parent 701a68bd86
commit b873e6bc8d
4 changed files with 31 additions and 21 deletions

View File

@ -19,8 +19,14 @@
#include <cerrno>
#include "appspawn_server.h"
#include "parameters.h"
namespace OHOS {
#ifdef APPSPAWN_ASAN
static constexpr int TIMEOUT_DEF = 5;
#else
static constexpr int TIMEOUT_DEF = 2;
#endif
namespace AppSpawn {
ClientSocket::ClientSocket(const std::string &client) : AppSpawnSocket(client)
{}
@ -56,16 +62,20 @@ int ClientSocket::ConnectSocket(int connectFd)
APPSPAWN_CHECK(PackSocketAddr() == 0, return -1, "pack socket failed");
bool isRet = (setsockopt(connectFd, SOL_SOCKET, SO_RCVTIMEO, &SOCKET_TIMEOUT, sizeof(SOCKET_TIMEOUT)) != 0) ||
(setsockopt(connectFd, SOL_SOCKET, SO_SNDTIMEO, &SOCKET_TIMEOUT, sizeof(SOCKET_TIMEOUT)) != 0);
struct timeval timeout = {TIMEOUT_DEF, 0};
int value = OHOS::system::GetIntParameter("persist.appspawn.client.timeout", TIMEOUT_DEF);
if (value != TIMEOUT_DEF && value != 0) {
timeout.tv_sec = value;
}
APPSPAWN_LOGI("Client: Connected on socket fd %d, timeout %d", connectFd, value);
bool isRet = (setsockopt(connectFd, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)) != 0) ||
(setsockopt(connectFd, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(timeout)) != 0);
APPSPAWN_CHECK(!isRet, return (-1), "Client: Failed to set opt of socket %d, err %d", connectFd, errno);
if (connect(connectFd, reinterpret_cast<struct sockaddr *>(&socketAddr_), socketAddrLen_) < 0) {
APPSPAWN_LOGW("Client: Connect on socket fd %d, failed: %d", connectFd, errno);
return -1;
}
APPSPAWN_LOGV("Client: Connected on socket fd %d, name '%s'", connectFd, socketAddr_.sun_path);
return 0;
}

View File

@ -108,11 +108,6 @@ protected:
const std::string socketDir_ = "/dev/socket/";
#endif
const unsigned int listenBacklog_ = 50; // 50: max num of clients
#ifdef APPSPAWN_ASAN
static constexpr struct timeval SOCKET_TIMEOUT = {5, 0}; // 5, 0: { 5 sec, 0 msec } for timeout
#else
static constexpr struct timeval SOCKET_TIMEOUT = {2, 0}; // 2, 0: { 2 sec, 0 msec } for timeout
#endif
};
} // namespace AppSpawn
} // namespace OHOS

View File

@ -131,14 +131,19 @@ static void OnClose(const TaskHandle taskHandle)
{
AppSpawnClientExt *client = (AppSpawnClientExt *)LE_GetUserData(taskHandle);
APPSPAWN_CHECK(client != NULL, return, "Failed to get client");
APPSPAWN_LOGI("OnClose client.id %d ", client->client.id);
}
APPSPAWN_STATIC void SendMessageComplete(const TaskHandle taskHandle, BufferHandle handle)
{
AppSpawnClientExt *client = (AppSpawnClientExt *)LE_GetUserData(taskHandle);
APPSPAWN_CHECK(client != NULL, return, "Failed to get client");
APPSPAWN_LOGI("SendMessageComplete client.id %d ", client->client.id);
APPSPAWN_LOGI("SendMessageComplete client.id %d result %d pid %d",
client->client.id, LE_GetSendResult(handle), client->pid);
if (LE_GetSendResult(handle) != 0 && client->pid > 0) {
kill(client->pid, SIGKILL);
APPSPAWN_LOGI("Send message fail err:%d kill app [ %d %s]",
LE_GetSendResult(handle), client->pid, client->property.bundleName);
}
}
static int SendResponse(AppSpawnClientExt *client, const char *buff, size_t buffSize)
@ -274,9 +279,9 @@ static void CheckColdAppEnabled(AppSpawnClientExt *appProperty)
if ((appProperty->property.flags & 0x01) != 0) {
char cold[10] = {0}; // 10 cold
int ret = GetParameter("appspawn.cold.boot", "false", cold, sizeof(cold));
int ret = GetParameter("startup.appspawn.cold.boot", "0", cold, sizeof(cold));
APPSPAWN_LOGV("appspawn.cold.boot %s %d ", cold, ret);
if (ret > 0 && (strcmp(cold, "true") == 0 || strcmp(cold, "1") == 0 || strcmp(cold, "enable") == 0)) {
if (ret > 0 && strcmp(cold, "1") == 0) {
appProperty->client.flags |= APP_COLD_START;
}
}
@ -362,7 +367,7 @@ APPSPAWN_STATIC void OnReceiveRequest(const TaskHandle taskHandle, const uint8_t
LE_StopTimer(LE_GetDefaultLoop(), g_appSpawnContent->timer);
g_appSpawnContent->timer = NULL;
}
appProperty->pid = 0;
CheckColdAppEnabled(appProperty);
// create pipe for commication from child
if (pipe(appProperty->fd) == -1) {
@ -387,25 +392,24 @@ APPSPAWN_STATIC void OnReceiveRequest(const TaskHandle taskHandle, const uint8_t
}
(void)memset_s(sandboxArg, sizeof(AppSandboxArg), 0, sizeof(AppSandboxArg));
pid_t pid = 0;
sandboxArg->content = &g_appSpawnContent->content;
sandboxArg->client = &appProperty->client;
sandboxArg->client->cloneFlags = 0;
if (appProperty->client.flags != UI_SERVICE_DIALOG) {
sandboxArg->client->cloneFlags = GetAppNamespaceFlags(appProperty->property.bundleName);
}
int result = AppSpawnProcessMsg(sandboxArg, &pid);
int result = AppSpawnProcessMsg(sandboxArg, &appProperty->pid);
if (result == 0) { // wait child process result
result = WaitChild(appProperty->fd[0], pid, appProperty);
result = WaitChild(appProperty->fd[0], appProperty->pid, appProperty);
}
close(appProperty->fd[0]);
close(appProperty->fd[1]);
free(sandboxArg);
APPSPAWN_LOGI("child process %s %s pid %d",
appProperty->property.processName, (result == 0) ? "success" : "fail", pid);
appProperty->property.processName, (result == 0) ? "success" : "fail", appProperty->pid);
if (result == 0) {
AddAppInfo(pid, appProperty->property.processName);
SendResponse(appProperty, (char *)&pid, sizeof(pid));
AddAppInfo(appProperty->pid, appProperty->property.processName);
SendResponse(appProperty, (char *)&appProperty->pid, sizeof(appProperty->pid));
} else {
SendResponse(appProperty, (char *)&result, sizeof(result));
}
@ -421,7 +425,6 @@ TaskHandle GetTestClientHandle()
APPSPAWN_STATIC int OnConnection(const LoopHandle loopHandle, const TaskHandle server)
{
static uint32_t clientId = 0;
APPSPAWN_LOGI("OnConnection ");
APPSPAWN_CHECK(server != NULL, return -1, "Error server");
TaskHandle stream;

View File

@ -16,6 +16,7 @@
#ifndef APPSPAWN_SERVICE_H
#define APPSPAWN_SERVICE_H
#include <unistd.h>
#include "appspawn_msg.h"
#include "appspawn_server.h"
#include "init_hashmap.h"
@ -47,6 +48,7 @@ typedef struct {
TaskHandle stream;
int32_t fd[2]; // 2 fd count
AppParameter property;
pid_t pid;
} AppSpawnClientExt;
typedef struct {