mirror of
https://gitee.com/openharmony/startup_appspawn
synced 2024-11-23 07:00:17 +00:00
update code
Signed-off-by: zhaoyuan17 <zhaoyuan17@huawei.com>
This commit is contained in:
parent
0f9fc1d6d1
commit
2d4f26feb8
@ -99,7 +99,7 @@ protected:
|
||||
|
||||
protected:
|
||||
int socketFd_ = -1;
|
||||
std::string socketName_{};
|
||||
std::string socketName_ {};
|
||||
struct sockaddr_un socketAddr_ {};
|
||||
socklen_t socketAddrLen_ = 0;
|
||||
#ifdef __MUSL__
|
||||
|
@ -17,7 +17,7 @@
|
||||
|
||||
#include <fcntl.h>
|
||||
#include <memory>
|
||||
#include <signal.h>
|
||||
#include <csignal>
|
||||
#include <sys/wait.h>
|
||||
#include <sys/prctl.h>
|
||||
#include <sys/capability.h>
|
||||
@ -29,6 +29,7 @@
|
||||
#include "securec.h"
|
||||
|
||||
#define GRAPHIC_PERMISSION_CHECK
|
||||
constexpr static size_t ERR_STRING_SZ = 64;
|
||||
|
||||
namespace OHOS {
|
||||
namespace AppSpawn {
|
||||
@ -71,11 +72,13 @@ static void SignalHandler([[maybe_unused]] int sig)
|
||||
|
||||
static void InstallSigHandler()
|
||||
{
|
||||
char err_string[ERR_STRING_SZ];
|
||||
struct sigaction sa = {};
|
||||
sa.sa_handler = SignalHandler;
|
||||
int err = sigaction(SIGCHLD, &sa, nullptr);
|
||||
if (err < 0) {
|
||||
HiLog::Error(LABEL, "Error installing SIGCHLD handler: %{public}s", strerror(errno));
|
||||
HiLog::Error(LABEL, "Error installing SIGCHLD handler: %{public}d",
|
||||
strerror_r(errno, err_string, ERR_STRING_SZ));
|
||||
return;
|
||||
}
|
||||
|
||||
@ -83,24 +86,26 @@ static void InstallSigHandler()
|
||||
sah.sa_handler = SIG_IGN;
|
||||
err = sigaction(SIGHUP, &sah, nullptr);
|
||||
if (err < 0) {
|
||||
HiLog::Error(LABEL, "Error installing SIGHUP handler: %{public}s", strerror(errno));
|
||||
HiLog::Error(LABEL, "Error installing SIGHUP handler: %{public}d",
|
||||
strerror_r(errno, err_string, ERR_STRING_SZ));
|
||||
}
|
||||
}
|
||||
|
||||
static void UninstallSigHandler()
|
||||
{
|
||||
char err_string[ERR_STRING_SZ];
|
||||
struct sigaction sa = {};
|
||||
sa.sa_handler = SIG_DFL;
|
||||
int err = sigaction(SIGCHLD, &sa, nullptr);
|
||||
if (err < 0) {
|
||||
HiLog::Error(LABEL, "Error uninstalling SIGCHLD handler: %s", strerror(errno));
|
||||
HiLog::Error(LABEL, "Error uninstalling SIGCHLD handler: %d", strerror_r(errno, err_string, ERR_STRING_SZ));
|
||||
}
|
||||
|
||||
struct sigaction sah = {};
|
||||
sah.sa_handler = SIG_DFL;
|
||||
err = sigaction(SIGHUP, &sah, nullptr);
|
||||
if (err < 0) {
|
||||
HiLog::Error(LABEL, "Error uninstalling SIGHUP handler: %s", strerror(errno));
|
||||
HiLog::Error(LABEL, "Error uninstalling SIGHUP handler: %d", strerror_r(errno, err_string, ERR_STRING_SZ));
|
||||
}
|
||||
}
|
||||
#ifdef __cplusplus
|
||||
@ -116,9 +121,11 @@ AppSpawnServer::AppSpawnServer(const std::string &socketName)
|
||||
|
||||
void AppSpawnServer::MsgPeer(int connectFd)
|
||||
{
|
||||
char err_string[ERR_STRING_SZ];
|
||||
std::unique_ptr<AppSpawnMsgPeer> msgPeer = std::make_unique<AppSpawnMsgPeer>(socket_, connectFd);
|
||||
if (msgPeer == nullptr || msgPeer->MsgPeer() != 0) {
|
||||
HiLog::Error(LABEL, "Failed to listen connection %d, %s", connectFd, strerror(errno));
|
||||
HiLog::Error(LABEL, "Failed to listen connection %d, %d",
|
||||
connectFd, strerror_r(errno, err_string, ERR_STRING_SZ));
|
||||
return;
|
||||
}
|
||||
|
||||
@ -203,6 +210,7 @@ bool AppSpawnServer::ServerMain(char *longProcName, int64_t longProcNameLen)
|
||||
int32_t AppSpawnServer::SetProcessName(
|
||||
char *longProcName, int64_t longProcNameLen, const char *processName, int32_t len)
|
||||
{
|
||||
char err_string[ERR_STRING_SZ];
|
||||
if (longProcName == nullptr || processName == nullptr || len <= 0) {
|
||||
HiLog::Error(LABEL, "process name is nullptr or length error");
|
||||
return -EINVAL;
|
||||
@ -217,31 +225,31 @@ int32_t AppSpawnServer::SetProcessName(
|
||||
// process short name max length 16 bytes.
|
||||
if (len > MAX_LEN_SHORT_NAME) {
|
||||
if (strncpy_s(shortName, MAX_LEN_SHORT_NAME, processName, MAX_LEN_SHORT_NAME - 1) != EOK) {
|
||||
HiLog::Error(LABEL, "strncpy_s short name error: %{public}s", strerror(errno));
|
||||
HiLog::Error(LABEL, "strncpy_s short name error: %{public}d", strerror_r(errno, err_string, ERR_STRING_SZ));
|
||||
return -EINVAL;
|
||||
}
|
||||
} else {
|
||||
if (strncpy_s(shortName, MAX_LEN_SHORT_NAME, processName, len) != EOK) {
|
||||
HiLog::Error(LABEL, "strncpy_s short name error: %{public}s", strerror(errno));
|
||||
HiLog::Error(LABEL, "strncpy_s short name error: %{public}d", strerror_r(errno, err_string, ERR_STRING_SZ));
|
||||
return -EINVAL;
|
||||
}
|
||||
}
|
||||
|
||||
// set short name
|
||||
if (prctl(PR_SET_NAME, shortName) == -1) {
|
||||
HiLog::Error(LABEL, "prctl(PR_SET_NAME) error: %{public}s", strerror(errno));
|
||||
HiLog::Error(LABEL, "prctl(PR_SET_NAME) error: %{public}d", strerror_r(errno, err_string, ERR_STRING_SZ));
|
||||
return (-errno);
|
||||
}
|
||||
|
||||
// reset longProcName
|
||||
if (memset_s(longProcName, longProcNameLen, 0, longProcNameLen) != EOK) {
|
||||
if (memset_s(longProcName, static_cast<size_t>(longProcNameLen), 0, static_cast<size_t>(longProcNameLen)) != EOK) {
|
||||
HiLog::Error(LABEL, "Failed to memset long process name");
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
// set long process name
|
||||
if (strncpy_s(longProcName, len, processName, len) != EOK) {
|
||||
HiLog::Error(LABEL, "strncpy_s long name error: %{public}s", strerror(errno));
|
||||
HiLog::Error(LABEL, "strncpy_s long name error: %{public}d", strerror_r(errno, err_string, ERR_STRING_SZ));
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
@ -251,21 +259,27 @@ int32_t AppSpawnServer::SetProcessName(
|
||||
int32_t AppSpawnServer::SetUidGid(
|
||||
const uint32_t uid, const uint32_t gid, const uint32_t *gitTable, const uint32_t gidCount)
|
||||
{
|
||||
char err_string[ERR_STRING_SZ];
|
||||
if (gitTable == nullptr) {
|
||||
HiLog::Error(LABEL, "gitTable is nullptr");
|
||||
return (-errno);
|
||||
}
|
||||
// set gids
|
||||
if (setgroups(gidCount, reinterpret_cast<const gid_t *>(&gitTable[0])) == -1) {
|
||||
HiLog::Error(LABEL, "setgroups failed: %{public}s, gids.size=%{public}u", strerror(errno), gidCount);
|
||||
HiLog::Error(LABEL, "setgroups failed: %{public}d, gids.size=%{public}u",
|
||||
strerror_r(errno, err_string, ERR_STRING_SZ), gidCount);
|
||||
return (-errno);
|
||||
}
|
||||
|
||||
// set gid
|
||||
if (setresgid(gid, gid, gid) == -1) {
|
||||
HiLog::Error(LABEL, "setgid(%{public}u) failed: %{public}s", gid, strerror(errno));
|
||||
HiLog::Error(LABEL, "setgid(%{public}u) failed: %{public}d", gid, strerror_r(errno, err_string, ERR_STRING_SZ));
|
||||
return (-errno);
|
||||
}
|
||||
|
||||
// If the effective user ID is changed from 0 to nonzero, then all capabilities are cleared from the effective set
|
||||
if (setresuid(uid, uid, uid) == -1) {
|
||||
HiLog::Error(LABEL, "setuid(%{public}u) failed: %{public}s", uid, strerror(errno));
|
||||
HiLog::Error(LABEL, "setuid(%{public}u) failed: %{public}d", uid, strerror_r(errno, err_string, ERR_STRING_SZ));
|
||||
return (-errno);
|
||||
}
|
||||
return ERR_OK;
|
||||
@ -273,6 +287,7 @@ int32_t AppSpawnServer::SetUidGid(
|
||||
|
||||
int32_t AppSpawnServer::SetFileDescriptors()
|
||||
{
|
||||
char err_string[ERR_STRING_SZ];
|
||||
// close stdin stdout stderr
|
||||
close(STDIN_FILENO);
|
||||
close(STDOUT_FILENO);
|
||||
@ -281,25 +296,25 @@ int32_t AppSpawnServer::SetFileDescriptors()
|
||||
// redirect to /dev/null
|
||||
int dev_null_fd = open(deviceNull_.c_str(), O_RDWR);
|
||||
if (dev_null_fd == -1) {
|
||||
HiLog::Error(LABEL, "open dev_null error: %{public}s", strerror(errno));
|
||||
HiLog::Error(LABEL, "open dev_null error: %{public}d", strerror_r(errno, err_string, ERR_STRING_SZ));
|
||||
return (-errno);
|
||||
}
|
||||
|
||||
// stdin
|
||||
if (dup2(dev_null_fd, STDIN_FILENO) == -1) {
|
||||
HiLog::Error(LABEL, "dup2 STDIN error: %{public}s", strerror(errno));
|
||||
HiLog::Error(LABEL, "dup2 STDIN error: %{public}d", strerror_r(errno, err_string, ERR_STRING_SZ));
|
||||
return (-errno);
|
||||
};
|
||||
|
||||
// stdout
|
||||
if (dup2(dev_null_fd, STDOUT_FILENO) == -1) {
|
||||
HiLog::Error(LABEL, "dup2 STDOUT error: %{public}s", strerror(errno));
|
||||
HiLog::Error(LABEL, "dup2 STDOUT error: %{public}d", strerror_r(errno, err_string, ERR_STRING_SZ));
|
||||
return (-errno);
|
||||
};
|
||||
|
||||
// stderr
|
||||
if (dup2(dev_null_fd, STDERR_FILENO) == -1) {
|
||||
HiLog::Error(LABEL, "dup2 STDERR error: %{public}s", strerror(errno));
|
||||
HiLog::Error(LABEL, "dup2 STDERR error: %{public}d", strerror_r(errno, err_string, ERR_STRING_SZ));
|
||||
return (-errno);
|
||||
};
|
||||
|
||||
@ -325,25 +340,26 @@ int32_t AppSpawnServer::SetCapabilities()
|
||||
|
||||
// init inheritable permitted effective zero
|
||||
#ifdef GRAPHIC_PERMISSION_CHECK
|
||||
uint64_t inheriTable = 0;
|
||||
uint64_t permitted = 0;
|
||||
uint64_t effective = 0;
|
||||
const uint64_t inheriTable = 0;
|
||||
const uint64_t permitted = 0;
|
||||
const uint64_t effective = 0;
|
||||
#else
|
||||
uint64_t inheriTable = 0x3fffffffff;
|
||||
uint64_t permitted = 0x3fffffffff;
|
||||
uint64_t effective = 0x3fffffffff;
|
||||
const uint64_t inheriTable = 0x3fffffffff;
|
||||
const uint64_t permitted = 0x3fffffffff;
|
||||
const uint64_t effective = 0x3fffffffff;
|
||||
#endif
|
||||
|
||||
cap_data[0].inheritable = inheriTable;
|
||||
cap_data[1].inheritable = inheriTable >> BITLEN32;
|
||||
cap_data[0].permitted = permitted;
|
||||
cap_data[1].permitted = permitted >> BITLEN32;
|
||||
cap_data[0].effective = effective;
|
||||
cap_data[1].effective = effective >> BITLEN32;
|
||||
cap_data[0].inheritable = static_cast<__u32>(inheriTable);
|
||||
cap_data[1].inheritable = static_cast<__u32>(inheriTable >> BITLEN32);
|
||||
cap_data[0].permitted = static_cast<__u32>(permitted);
|
||||
cap_data[1].permitted = static_cast<__u32>(permitted >> BITLEN32);
|
||||
cap_data[0].effective = static_cast<__u32>(effective);
|
||||
cap_data[1].effective = static_cast<__u32>(effective >> BITLEN32);
|
||||
|
||||
char err_string[ERR_STRING_SZ];
|
||||
// set capabilities
|
||||
if (capset(&cap_header, &cap_data[0]) == -1) {
|
||||
HiLog::Error(LABEL, "capset failed: %{public}s", strerror(errno));
|
||||
HiLog::Error(LABEL, "capset failed: %{public}d", strerror_r(errno, err_string, ERR_STRING_SZ));
|
||||
return errno;
|
||||
}
|
||||
|
||||
@ -363,6 +379,10 @@ void AppSpawnServer::SetServerSocket(const std::shared_ptr<ServerSocket> &server
|
||||
bool AppSpawnServer::SetAppProcProperty(int connectFd, const ClientSocket::AppProperty *appProperty, char *longProcName,
|
||||
int64_t longProcNameLen, const int32_t fd[FDLEN2])
|
||||
{
|
||||
if (appProperty == nullptr) {
|
||||
HiLog::Error(LABEL, "appProperty is nullptr");
|
||||
return false;
|
||||
}
|
||||
pid_t newPid = getpid();
|
||||
HiLog::Debug(LABEL, "AppSpawnServer::Success to fork new process, pid = %{public}d", newPid);
|
||||
// close socket connection and peer socket in child process
|
||||
@ -419,6 +439,10 @@ void AppSpawnServer::NotifyResToParentProc(const int32_t fd, const int32_t value
|
||||
|
||||
void AppSpawnServer::SpecialHandle(ClientSocket::AppProperty *appProperty)
|
||||
{
|
||||
if (appProperty == nullptr) {
|
||||
HiLog::Error(LABEL, "appProperty is nullptr");
|
||||
return;
|
||||
}
|
||||
// special handle bundle name "com.ohos.photos" and "com.ohos.camera"
|
||||
if ((strcmp(appProperty->processName, BUNDLE_NAME_CAMERA.data()) == 0) ||
|
||||
(strcmp(appProperty->processName, BUNDLE_NAME_PHOTOS.data()) == 0)) {
|
||||
@ -433,10 +457,11 @@ void AppSpawnServer::SpecialHandle(ClientSocket::AppProperty *appProperty)
|
||||
|
||||
int32_t AppSpawnServer::SetKeepCapabilities(uint32_t uid)
|
||||
{
|
||||
char err_string[ERR_STRING_SZ];
|
||||
// set keep capabilities when user not root.
|
||||
if (uid != 0) {
|
||||
if (prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0) == -1) {
|
||||
HiLog::Error(LABEL, "set keepcaps failed: %{public}s", strerror(errno));
|
||||
HiLog::Error(LABEL, "set keepcaps failed: %{public}d", strerror_r(errno, err_string, ERR_STRING_SZ));
|
||||
return (-errno);
|
||||
}
|
||||
}
|
||||
|
@ -18,7 +18,7 @@
|
||||
#include "appspawn_server.h"
|
||||
#include "hilog/log.h"
|
||||
|
||||
int main(int argc, char* const argv[])
|
||||
int main(int argc, char *const argv[])
|
||||
{
|
||||
if (argc > 0) {
|
||||
// calculate child process long name size
|
||||
|
@ -27,6 +27,7 @@ namespace OHOS {
|
||||
namespace AppSpawn {
|
||||
using namespace OHOS::HiviewDFX;
|
||||
static constexpr HiLogLabel LABEL = {LOG_CORE, 0, "AppSpawnSocket"};
|
||||
constexpr static size_t ERR_STRING_SZ = 64;
|
||||
|
||||
AppSpawnSocket::AppSpawnSocket(const std::string &name)
|
||||
{
|
||||
@ -80,9 +81,10 @@ int AppSpawnSocket::PackSocketAddr()
|
||||
|
||||
int AppSpawnSocket::CreateSocket()
|
||||
{
|
||||
char err_string[ERR_STRING_SZ];
|
||||
int socketFd = socket(AF_LOCAL, SOCK_SEQPACKET, 0);
|
||||
if (socketFd < 0) {
|
||||
HiLog::Error(LABEL, "Failed to create socket: %s", strerror(errno));
|
||||
HiLog::Error(LABEL, "Failed to create socket: %d", strerror_r(errno, err_string, ERR_STRING_SZ));
|
||||
return -1;
|
||||
}
|
||||
|
||||
@ -101,6 +103,7 @@ void AppSpawnSocket::CloseSocket(int &socketFd)
|
||||
|
||||
int AppSpawnSocket::ReadSocketMessage(int socketFd, void *buf, int len)
|
||||
{
|
||||
char err_string[ERR_STRING_SZ];
|
||||
if (socketFd < 0 || len <= 0 || buf == nullptr) {
|
||||
HiLog::Error(LABEL, "Invalid args: socket %d, len %d, buf might be nullptr", socketFd, len);
|
||||
return -1;
|
||||
@ -113,7 +116,8 @@ int AppSpawnSocket::ReadSocketMessage(int socketFd, void *buf, int len)
|
||||
|
||||
ssize_t rLen = TEMP_FAILURE_RETRY(read(socketFd, buf, len));
|
||||
if (rLen < 0) {
|
||||
HiLog::Error(LABEL, "Read message from fd %d error %zd: %s", socketFd, rLen, strerror(errno));
|
||||
HiLog::Error(LABEL, "Read message from fd %d error %zd: %d",
|
||||
socketFd, rLen, strerror_r(errno, err_string, ERR_STRING_SZ));
|
||||
return -1;
|
||||
}
|
||||
|
||||
@ -122,6 +126,7 @@ int AppSpawnSocket::ReadSocketMessage(int socketFd, void *buf, int len)
|
||||
|
||||
int AppSpawnSocket::WriteSocketMessage(int socketFd, const void *buf, int len)
|
||||
{
|
||||
char err_string[ERR_STRING_SZ];
|
||||
if (socketFd < 0 || len <= 0 || buf == nullptr) {
|
||||
HiLog::Error(LABEL, "Invalid args: socket %d, len %d, buf might be nullptr", socketFd, len);
|
||||
return -1;
|
||||
@ -134,7 +139,8 @@ int AppSpawnSocket::WriteSocketMessage(int socketFd, const void *buf, int len)
|
||||
wLen = write(socketFd, offset, remain);
|
||||
HiLog::Debug(LABEL, "socket fd %d, wLen %zd", socketFd, wLen);
|
||||
if ((wLen <= 0) && (errno != EINTR)) {
|
||||
HiLog::Error(LABEL, "Failed to write message to fd %d, error %zd: %s", socketFd, wLen, strerror(errno));
|
||||
HiLog::Error(LABEL, "Failed to write message to fd %d, error %zd: %d",
|
||||
socketFd, wLen, strerror_r(errno, err_string, ERR_STRING_SZ));
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
@ -26,6 +26,7 @@ namespace OHOS {
|
||||
namespace AppSpawn {
|
||||
using namespace OHOS::HiviewDFX;
|
||||
static constexpr HiLogLabel LABEL = {LOG_CORE, 0, "ClientSocket"};
|
||||
constexpr static size_t ERR_STRING_SZ = 64;
|
||||
|
||||
ClientSocket::ClientSocket(const std::string &client) : AppSpawnSocket(client)
|
||||
{}
|
||||
@ -57,6 +58,7 @@ void ClientSocket::CloseClient()
|
||||
|
||||
int ClientSocket::ConnectSocket(int connectFd)
|
||||
{
|
||||
char err_string[ERR_STRING_SZ];
|
||||
if (connectFd < 0) {
|
||||
HiLog::Error(LABEL, "Client: Invalid socket fd: %d", connectFd);
|
||||
return -1;
|
||||
@ -68,11 +70,13 @@ int ClientSocket::ConnectSocket(int connectFd)
|
||||
|
||||
if ((setsockopt(connectFd, SOL_SOCKET, SO_RCVTIMEO, &SOCKET_TIMEOUT, sizeof(SOCKET_TIMEOUT)) != 0) ||
|
||||
(setsockopt(connectFd, SOL_SOCKET, SO_SNDTIMEO, &SOCKET_TIMEOUT, sizeof(SOCKET_TIMEOUT)) != 0)) {
|
||||
HiLog::Warn(LABEL, "Client: Failed to set opt of socket %d, err %s", connectFd, strerror(errno));
|
||||
HiLog::Warn(LABEL, "Client: Failed to set opt of socket %d, err %d",
|
||||
connectFd, strerror_r(errno, err_string, ERR_STRING_SZ));
|
||||
}
|
||||
|
||||
if (connect(connectFd, reinterpret_cast<struct sockaddr *>(&socketAddr_), socketAddrLen_) < 0) {
|
||||
HiLog::Warn(LABEL, "Client: Connect on socket fd %d, failed: %s", connectFd, strerror(errno));
|
||||
HiLog::Warn(LABEL, "Client: Connect on socket fd %d, failed: %d",
|
||||
connectFd, strerror_r(errno, err_string, ERR_STRING_SZ));
|
||||
CloseSocket(connectFd);
|
||||
return -1;
|
||||
}
|
||||
|
@ -26,6 +26,7 @@ namespace OHOS {
|
||||
namespace AppSpawn {
|
||||
using namespace OHOS::HiviewDFX;
|
||||
static constexpr HiLogLabel LABEL = {LOG_CORE, 0, "ServerSocket"};
|
||||
constexpr static size_t ERR_STRING_SZ = 64;
|
||||
|
||||
ServerSocket::ServerSocket(const std::string &server) : AppSpawnSocket(server)
|
||||
{}
|
||||
@ -100,6 +101,7 @@ void ServerSocket::CloseServerMonitor()
|
||||
|
||||
int ServerSocket::BindSocket(int connectFd)
|
||||
{
|
||||
char err_string[ERR_STRING_SZ];
|
||||
if (connectFd < 0) {
|
||||
HiLog::Error(LABEL, "Server: Invalid socket fd: %d", connectFd);
|
||||
return -1;
|
||||
@ -110,7 +112,7 @@ int ServerSocket::BindSocket(int connectFd)
|
||||
}
|
||||
|
||||
if ((unlink(socketAddr_.sun_path) != 0) && (errno != ENOENT)) {
|
||||
HiLog::Error(LABEL, "Server: Failed to unlink, err %s", strerror(errno));
|
||||
HiLog::Error(LABEL, "Server: Failed to unlink, err %d", strerror_r(errno, err_string, ERR_STRING_SZ));
|
||||
return -1;
|
||||
}
|
||||
|
||||
@ -118,22 +120,26 @@ int ServerSocket::BindSocket(int connectFd)
|
||||
if ((setsockopt(connectFd, SOL_SOCKET, SO_REUSEADDR, &reuseAddr, sizeof(reuseAddr)) != 0) ||
|
||||
(setsockopt(connectFd, SOL_SOCKET, SO_RCVTIMEO, &SOCKET_TIMEOUT, sizeof(SOCKET_TIMEOUT)) != 0) ||
|
||||
(setsockopt(connectFd, SOL_SOCKET, SO_SNDTIMEO, &SOCKET_TIMEOUT, sizeof(SOCKET_TIMEOUT)) != 0)) {
|
||||
HiLog::Warn(LABEL, "Server: Failed to set opt of socket %d, err %s", connectFd, strerror(errno));
|
||||
HiLog::Warn(LABEL, "Server: Failed to set opt of socket %d, err %d",
|
||||
connectFd, strerror_r(errno, err_string, ERR_STRING_SZ));
|
||||
}
|
||||
|
||||
if (bind(connectFd, reinterpret_cast<struct sockaddr *>(&socketAddr_), socketAddrLen_) < 0) {
|
||||
HiLog::Error(LABEL, "Server: Bind socket fd %d, failed: %s", connectFd, strerror(errno));
|
||||
HiLog::Error(LABEL, "Server: Bind socket fd %d, failed: %d",
|
||||
connectFd, strerror_r(errno, err_string, ERR_STRING_SZ));
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (chown(socketAddr_.sun_path, APPSPAWN_ID_ROOT, APPSPAWN_ID_SYSTEM)) {
|
||||
HiLog::Error(LABEL, "Server: failed to chown socket fd %d, failed: %s", connectFd, strerror(errno));
|
||||
HiLog::Error(LABEL, "Server: failed to chown socket fd %d, failed: %d",
|
||||
connectFd, strerror_r(errno, err_string, ERR_STRING_SZ));
|
||||
return -1;
|
||||
}
|
||||
if (chmod(socketAddr_.sun_path, SOCKET_PERM)) {
|
||||
HiLog::Error(LABEL, "Server: failed to chmod socket fd %d, failed: %s", connectFd, strerror(errno));
|
||||
HiLog::Error(LABEL, "Server: failed to chmod socket fd %d, failed: %d",
|
||||
connectFd, strerror_r(errno, err_string, ERR_STRING_SZ));
|
||||
if ((unlink(socketAddr_.sun_path) != 0) && (errno != ENOENT)) {
|
||||
HiLog::Error(LABEL, "Server: Failed to unlink, err %s", strerror(errno));
|
||||
HiLog::Error(LABEL, "Server: Failed to unlink, err %d", strerror_r(errno, err_string, ERR_STRING_SZ));
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
@ -143,6 +149,7 @@ int ServerSocket::BindSocket(int connectFd)
|
||||
|
||||
int ServerSocket::RegisterServerSocket(int &connectFd)
|
||||
{
|
||||
char err_string[ERR_STRING_SZ];
|
||||
if (socketName_.empty()) {
|
||||
HiLog::Error(LABEL, "Server: Invalid socket name: empty");
|
||||
return -1;
|
||||
@ -155,10 +162,10 @@ int ServerSocket::RegisterServerSocket(int &connectFd)
|
||||
|
||||
if ((BindSocket(connectFd) != 0) || (listen(connectFd, listenBacklog_) < 0)) {
|
||||
HiLog::Error(LABEL,
|
||||
"Server: Register socket fd %d with backlog %d error: %s",
|
||||
"Server: Register socket fd %d with backlog %d error: %d",
|
||||
connectFd,
|
||||
listenBacklog_,
|
||||
strerror(errno));
|
||||
strerror_r(errno, err_string, ERR_STRING_SZ));
|
||||
close(connectFd);
|
||||
connectFd = -1;
|
||||
return -1;
|
||||
@ -180,6 +187,7 @@ int ServerSocket::RegisterServerSocket()
|
||||
|
||||
int ServerSocket::WaitForConnection(int connectFd)
|
||||
{
|
||||
char err_string[ERR_STRING_SZ];
|
||||
if (connectFd < 0) {
|
||||
HiLog::Error(LABEL, "Server: Invalid args: connectFd %d", connectFd);
|
||||
return -1;
|
||||
@ -198,7 +206,8 @@ int ServerSocket::WaitForConnection(int connectFd)
|
||||
|
||||
if ((setsockopt(connFd, SOL_SOCKET, SO_RCVTIMEO, &SOCKET_TIMEOUT, sizeof(SOCKET_TIMEOUT)) < 0) ||
|
||||
(setsockopt(connFd, SOL_SOCKET, SO_SNDTIMEO, &SOCKET_TIMEOUT, sizeof(SOCKET_TIMEOUT)) < 0)) {
|
||||
HiLog::Warn(LABEL, "Server: Failed to set opt of Connection %d, err %s", connFd, strerror(errno));
|
||||
HiLog::Warn(LABEL, "Server: Failed to set opt of Connection %d, err %d",
|
||||
connFd, strerror_r(errno, err_string, ERR_STRING_SZ));
|
||||
}
|
||||
|
||||
HiLog::Debug(LABEL, "Server: Connection accepted, connect fd %d", connFd);
|
||||
|
@ -48,7 +48,7 @@ const int32_t GROUPS_POSITION_MOVE = 8;
|
||||
const char *DELIMITER_SPACE = " ";
|
||||
const char *DELIMITER_NEWLINE = "\n";
|
||||
|
||||
char buffer[BUFFER_SIZE];
|
||||
char buffer[BUFFER_SIZE] = {"\0"};
|
||||
int32_t newPid = 0;
|
||||
int32_t retryCount = 0;
|
||||
} // namespace
|
||||
@ -132,6 +132,10 @@ bool checkGid(const int32_t &pid, const AppSpawnStartMsg ¶ms)
|
||||
if (strlen(gidPtr) > GID_POSITION_MOVE) {
|
||||
gidPtr = gidPtr + GID_POSITION_MOVE;
|
||||
}
|
||||
if (gidPtr == nullptr) {
|
||||
HiLog::Error(LABEL, "get Gid info failed.");
|
||||
return CHECK_ERROR;
|
||||
}
|
||||
int32_t gid = (int32_t)strtol(gidPtr, NULL, BASE_TYPE);
|
||||
HiLog::Info(LABEL, "new proc(%{public}d) gid = %{public}d, setGid=%{public}d.", pid, gid, params.gid);
|
||||
if (gid == params.gid) {
|
||||
@ -154,6 +158,10 @@ std::size_t getGids(const int32_t &pid, std::vector<int32_t> &gids)
|
||||
}
|
||||
// Get the row content of Groups
|
||||
char *saveptr = nullptr;
|
||||
if (groupsPtr == nullptr || strlen(groupsPtr) > BUFFER_SIZE) {
|
||||
HiLog::Error(LABEL, "get Groups info failed.");
|
||||
return CHECK_ERROR;
|
||||
}
|
||||
char *line = strtok_r(groupsPtr, DELIMITER_NEWLINE, &saveptr);
|
||||
if (line == nullptr || strlen(line) > BUFFER_SIZE) {
|
||||
HiLog::Error(LABEL, "get Groups line info failed.");
|
||||
|
@ -30,15 +30,14 @@ using namespace testing;
|
||||
using namespace testing::ext;
|
||||
using namespace OHOS::AppSpawn;
|
||||
|
||||
static constexpr int TEST_WAIT_TIME = 50 * 1000; // 50 ms
|
||||
|
||||
class AppSpawnServerMockTest : public testing::Test {
|
||||
public:
|
||||
static void SetUpTestCase();
|
||||
static void TearDownTestCase();
|
||||
void SetUp();
|
||||
void TearDown();
|
||||
|
||||
public:
|
||||
static constexpr int TEST_WAIT_TIME = 50 * 1000; // 50 ms
|
||||
protected:
|
||||
std::unique_ptr<AppSpawnServer> appSpawnServer_ = nullptr;
|
||||
std::shared_ptr<MockServerSocket> mockServerSocket_ = nullptr;
|
||||
@ -110,7 +109,7 @@ HWTEST_F(AppSpawnServerMockTest, App_Spawn_Server_002, TestSize.Level0)
|
||||
|
||||
auto func = [&]() {
|
||||
// wait ServerMain unit test case
|
||||
usleep(AppSpawnServerMockTest::TEST_WAIT_TIME);
|
||||
usleep(TEST_WAIT_TIME);
|
||||
appSpawnServer_->SetRunning(false);
|
||||
};
|
||||
|
||||
@ -118,7 +117,7 @@ HWTEST_F(AppSpawnServerMockTest, App_Spawn_Server_002, TestSize.Level0)
|
||||
EXPECT_EQ(false, appSpawnServer_->ServerMain(argv, sizeof(argv)));
|
||||
|
||||
// wait release
|
||||
usleep(AppSpawnServerMockTest::TEST_WAIT_TIME);
|
||||
usleep(TEST_WAIT_TIME);
|
||||
|
||||
GTEST_LOG_(INFO) << "App_Spawn_Server_002 end";
|
||||
}
|
||||
@ -149,7 +148,7 @@ HWTEST_F(AppSpawnServerMockTest, App_Spawn_Server_003, TestSize.Level0)
|
||||
|
||||
auto func = [=]() {
|
||||
// wait ServerMain unit test case
|
||||
usleep(AppSpawnServerMockTest::TEST_WAIT_TIME);
|
||||
usleep(TEST_WAIT_TIME);
|
||||
appSpawnServer_->SetRunning(false);
|
||||
};
|
||||
|
||||
@ -157,7 +156,7 @@ HWTEST_F(AppSpawnServerMockTest, App_Spawn_Server_003, TestSize.Level0)
|
||||
EXPECT_EQ(false, appSpawnServer_->ServerMain(argv, sizeof(argv)));
|
||||
|
||||
// wait release
|
||||
usleep(AppSpawnServerMockTest::TEST_WAIT_TIME);
|
||||
usleep(TEST_WAIT_TIME);
|
||||
|
||||
GTEST_LOG_(INFO) << "App_Spawn_Server_003 end";
|
||||
}
|
||||
|
@ -28,15 +28,14 @@ using namespace testing;
|
||||
using namespace testing::ext;
|
||||
using namespace OHOS::AppSpawn;
|
||||
|
||||
static constexpr int TEST_WAIT_TIME = 50 * 1000; // 50 ms
|
||||
|
||||
class AppSpawnServerOverrideTest : public testing::Test {
|
||||
public:
|
||||
static void SetUpTestCase();
|
||||
static void TearDownTestCase();
|
||||
void SetUp();
|
||||
void TearDown();
|
||||
|
||||
public:
|
||||
static constexpr int TEST_WAIT_TIME = 50 * 1000; // 50 ms
|
||||
protected:
|
||||
std::unique_ptr<AppSpawnServer> appSpawnServer_ = nullptr;
|
||||
};
|
||||
@ -72,14 +71,14 @@ HWTEST_F(AppSpawnServerOverrideTest, App_Spawn_Server_Override_001, TestSize.Lev
|
||||
char argv[20] = "LongNameTest";
|
||||
auto func = [&]() {
|
||||
// wait ServerMain unit test case
|
||||
usleep(AppSpawnServerOverrideTest::TEST_WAIT_TIME);
|
||||
usleep(TEST_WAIT_TIME);
|
||||
appSpawnServer_->SetRunning(false);
|
||||
};
|
||||
std::thread(func).detach();
|
||||
EXPECT_EQ(false, appSpawnServer_->ServerMain(argv, sizeof(argv)));
|
||||
|
||||
// wait release
|
||||
usleep(AppSpawnServerOverrideTest::TEST_WAIT_TIME);
|
||||
usleep(TEST_WAIT_TIME);
|
||||
|
||||
GTEST_LOG_(INFO) << "App_Spawn_Server_Override_001 end";
|
||||
}
|
||||
|
@ -14,7 +14,7 @@
|
||||
*/
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
#include <string.h>
|
||||
#include <cstring>
|
||||
|
||||
// redefine private and protected since testcase need to invoke and test private function
|
||||
#define private public
|
||||
@ -35,9 +35,6 @@ public:
|
||||
static void TearDownTestCase();
|
||||
void SetUp();
|
||||
void TearDown();
|
||||
|
||||
public:
|
||||
static constexpr int TEST_WAIT_TIME = 100000;
|
||||
};
|
||||
|
||||
void AppSpawnSocketTest::SetUpTestCase()
|
||||
@ -277,6 +274,7 @@ HWTEST(AppSpawnSocketTest, App_Spawn_Socket_010, TestSize.Level0)
|
||||
EXPECT_TRUE(appSpawnSocket);
|
||||
std::string content = "hiworld";
|
||||
int32_t len = content.length();
|
||||
EXPECT_TRUE(len);
|
||||
std::unique_ptr<int8_t[]> buff = std::make_unique<int8_t[]>(len);
|
||||
EXPECT_TRUE(buff);
|
||||
int32_t fd[2] = {0, 0};
|
||||
@ -407,6 +405,7 @@ HWTEST(AppSpawnSocketTest, App_Spawn_Socket_015, TestSize.Level0)
|
||||
EXPECT_TRUE(appSpawnSocket);
|
||||
std::string content = "hiworld";
|
||||
int32_t len = content.length();
|
||||
EXPECT_TRUE(len);
|
||||
std::unique_ptr<int8_t[]> buff = std::make_unique<int8_t[]>(len);
|
||||
EXPECT_TRUE(buff);
|
||||
int32_t fd[2] = {0, 0};
|
||||
|
@ -35,9 +35,6 @@ public:
|
||||
static void TearDownTestCase();
|
||||
void SetUp();
|
||||
void TearDown();
|
||||
|
||||
public:
|
||||
static constexpr int TEST_WAIT_TIME = 100000;
|
||||
};
|
||||
|
||||
void ClientSocketTest::SetUpTestCase()
|
||||
|
@ -35,9 +35,6 @@ public:
|
||||
static void TearDownTestCase();
|
||||
void SetUp();
|
||||
void TearDown();
|
||||
|
||||
public:
|
||||
static constexpr int TEST_WAIT_TIME = 100000;
|
||||
};
|
||||
|
||||
void ServerSocketTest::SetUpTestCase()
|
||||
|
Loading…
Reference in New Issue
Block a user