diff --git a/src/appspawn_server.cpp b/src/appspawn_server.cpp index 38ccf1e4f6302be67dfb68f9007080e5288a5ac7..2acb6818de5f3490b45fa2d15eeef9673baaed39 100644 --- a/src/appspawn_server.cpp +++ b/src/appspawn_server.cpp @@ -32,7 +32,6 @@ #include #define GRAPHIC_PERMISSION_CHECK -constexpr static size_t ERR_STRING_SZ = 64; namespace OHOS { namespace AppSpawn { @@ -77,13 +76,11 @@ 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}d", - strerror_r(errno, err_string, ERR_STRING_SZ)); + HiLog::Error(LABEL, "Error installing SIGCHLD handler: %{public}d", errno); return; } @@ -91,26 +88,24 @@ static void InstallSigHandler() sah.sa_handler = SIG_IGN; err = sigaction(SIGHUP, &sah, nullptr); if (err < 0) { - HiLog::Error(LABEL, "Error installing SIGHUP handler: %{public}d", - strerror_r(errno, err_string, ERR_STRING_SZ)); + HiLog::Error(LABEL, "Error installing SIGHUP handler: %{public}d", errno); } } 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: %d", strerror_r(errno, err_string, ERR_STRING_SZ)); + HiLog::Error(LABEL, "Error uninstalling SIGCHLD handler: %d", errno); } struct sigaction sah = {}; sah.sa_handler = SIG_DFL; err = sigaction(SIGHUP, &sah, nullptr); if (err < 0) { - HiLog::Error(LABEL, "Error uninstalling SIGHUP handler: %d", strerror_r(errno, err_string, ERR_STRING_SZ)); + HiLog::Error(LABEL, "Error uninstalling SIGHUP handler: %d", errno); } } #ifdef __cplusplus @@ -126,11 +121,9 @@ AppSpawnServer::AppSpawnServer(const std::string &socketName) void AppSpawnServer::MsgPeer(int connectFd) { - char err_string[ERR_STRING_SZ]; std::unique_ptr msgPeer = std::make_unique(socket_, connectFd); if (msgPeer == nullptr || msgPeer->MsgPeer() != 0) { - HiLog::Error(LABEL, "Failed to listen connection %d, %d", - connectFd, strerror_r(errno, err_string, ERR_STRING_SZ)); + HiLog::Error(LABEL, "Failed to listen connection %d, %d", connectFd, errno); return; } @@ -232,7 +225,6 @@ 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; @@ -247,19 +239,19 @@ 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}d", strerror_r(errno, err_string, ERR_STRING_SZ)); + HiLog::Error(LABEL, "strncpy_s short name error: %{public}d", errno); return -EINVAL; } } else { if (strncpy_s(shortName, MAX_LEN_SHORT_NAME, processName, len) != EOK) { - HiLog::Error(LABEL, "strncpy_s short name error: %{public}d", strerror_r(errno, err_string, ERR_STRING_SZ)); + HiLog::Error(LABEL, "strncpy_s short name error: %{public}d", errno); return -EINVAL; } } // set short name if (prctl(PR_SET_NAME, shortName) == -1) { - HiLog::Error(LABEL, "prctl(PR_SET_NAME) error: %{public}d", strerror_r(errno, err_string, ERR_STRING_SZ)); + HiLog::Error(LABEL, "prctl(PR_SET_NAME) error: %{public}d", errno); return (-errno); } @@ -271,7 +263,7 @@ int32_t AppSpawnServer::SetProcessName( // set long process name if (strncpy_s(longProcName, len, processName, len) != EOK) { - HiLog::Error(LABEL, "strncpy_s long name error: %{public}d", strerror_r(errno, err_string, ERR_STRING_SZ)); + HiLog::Error(LABEL, "strncpy_s long name error: %{public}d", errno); return -EINVAL; } @@ -281,27 +273,25 @@ 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(&gitTable[0])) == -1) { - HiLog::Error(LABEL, "setgroups failed: %{public}d, gids.size=%{public}u", - strerror_r(errno, err_string, ERR_STRING_SZ), gidCount); + HiLog::Error(LABEL, "setgroups failed: %{public}d, gids.size=%{public}u", errno, gidCount); return (-errno); } // set gid if (setresgid(gid, gid, gid) == -1) { - HiLog::Error(LABEL, "setgid(%{public}u) failed: %{public}d", gid, strerror_r(errno, err_string, ERR_STRING_SZ)); + HiLog::Error(LABEL, "setgid(%{public}u) failed: %{public}d", gid, errno); 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}d", uid, strerror_r(errno, err_string, ERR_STRING_SZ)); + HiLog::Error(LABEL, "setuid(%{public}u) failed: %{public}d", uid, errno); return (-errno); } return ERR_OK; @@ -309,7 +299,6 @@ int32_t AppSpawnServer::SetUidGid( int32_t AppSpawnServer::SetFileDescriptors() { - char err_string[ERR_STRING_SZ]; // close stdin stdout stderr close(STDIN_FILENO); close(STDOUT_FILENO); @@ -318,25 +307,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}d", strerror_r(errno, err_string, ERR_STRING_SZ)); + HiLog::Error(LABEL, "open dev_null error: %{public}d", errno); return (-errno); } // stdin if (dup2(dev_null_fd, STDIN_FILENO) == -1) { - HiLog::Error(LABEL, "dup2 STDIN error: %{public}d", strerror_r(errno, err_string, ERR_STRING_SZ)); + HiLog::Error(LABEL, "dup2 STDIN error: %{public}d", errno); return (-errno); }; // stdout if (dup2(dev_null_fd, STDOUT_FILENO) == -1) { - HiLog::Error(LABEL, "dup2 STDOUT error: %{public}d", strerror_r(errno, err_string, ERR_STRING_SZ)); + HiLog::Error(LABEL, "dup2 STDOUT error: %{public}d", errno); return (-errno); }; // stderr if (dup2(dev_null_fd, STDERR_FILENO) == -1) { - HiLog::Error(LABEL, "dup2 STDERR error: %{public}d", strerror_r(errno, err_string, ERR_STRING_SZ)); + HiLog::Error(LABEL, "dup2 STDERR error: %{public}d", errno); return (-errno); }; @@ -378,10 +367,9 @@ int32_t AppSpawnServer::SetCapabilities() 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}d", strerror_r(errno, err_string, ERR_STRING_SZ)); + HiLog::Error(LABEL, "capset failed: %{public}d", errno); return errno; } @@ -481,11 +469,10 @@ 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}d", strerror_r(errno, err_string, ERR_STRING_SZ)); + HiLog::Error(LABEL, "set keepcaps failed: %{public}d", errno); return (-errno); } } diff --git a/src/socket/appspawn_socket.cpp b/src/socket/appspawn_socket.cpp index 76b984fe99e529bcf534366716a764eb2c1dd741..831528020f666b16bd94098895aa16a1a921b65a 100755 --- a/src/socket/appspawn_socket.cpp +++ b/src/socket/appspawn_socket.cpp @@ -27,7 +27,6 @@ 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) { @@ -81,10 +80,9 @@ 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: %d", strerror_r(errno, err_string, ERR_STRING_SZ)); + HiLog::Error(LABEL, "Failed to create socket: %d", errno); return -1; } @@ -103,7 +101,6 @@ 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; @@ -116,8 +113,7 @@ 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: %d", - socketFd, rLen, strerror_r(errno, err_string, ERR_STRING_SZ)); + HiLog::Error(LABEL, "Read message from fd %d error %zd: %d", socketFd, rLen, errno); return -1; } @@ -126,7 +122,6 @@ 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; @@ -139,8 +134,7 @@ 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: %d", - socketFd, wLen, strerror_r(errno, err_string, ERR_STRING_SZ)); + HiLog::Error(LABEL, "Failed to write message to fd %d, error %zd: %d", socketFd, wLen, errno); return -1; } } diff --git a/src/socket/client_socket.cpp b/src/socket/client_socket.cpp index c39a0c95d3bbba2e21526a58fdc155ded6989000..d45eb1fdfb2347041acd5bef0728cd6701a84eaa 100644 --- a/src/socket/client_socket.cpp +++ b/src/socket/client_socket.cpp @@ -26,7 +26,6 @@ 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) {} @@ -58,7 +57,6 @@ 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; @@ -70,13 +68,11 @@ 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 %d", - connectFd, strerror_r(errno, err_string, ERR_STRING_SZ)); + HiLog::Warn(LABEL, "Client: Failed to set opt of socket %d, err %d", connectFd, errno); } if (connect(connectFd, reinterpret_cast(&socketAddr_), socketAddrLen_) < 0) { - HiLog::Warn(LABEL, "Client: Connect on socket fd %d, failed: %d", - connectFd, strerror_r(errno, err_string, ERR_STRING_SZ)); + HiLog::Warn(LABEL, "Client: Connect on socket fd %d, failed: %d", connectFd, errno); CloseSocket(connectFd); return -1; } diff --git a/src/socket/server_socket.cpp b/src/socket/server_socket.cpp index 941cd4049d632379036e1bfa63008753827b620c..9a305338a50c95393b2d3197b30a8e16b21665f8 100644 --- a/src/socket/server_socket.cpp +++ b/src/socket/server_socket.cpp @@ -26,7 +26,6 @@ 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) {} @@ -101,7 +100,6 @@ 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; @@ -112,7 +110,7 @@ int ServerSocket::BindSocket(int connectFd) } if ((unlink(socketAddr_.sun_path) != 0) && (errno != ENOENT)) { - HiLog::Error(LABEL, "Server: Failed to unlink, err %d", strerror_r(errno, err_string, ERR_STRING_SZ)); + HiLog::Error(LABEL, "Server: Failed to unlink, err %d", errno); return -1; } @@ -120,26 +118,22 @@ 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 %d", - connectFd, strerror_r(errno, err_string, ERR_STRING_SZ)); + HiLog::Warn(LABEL, "Server: Failed to set opt of socket %d, err %d", connectFd, errno); } if (bind(connectFd, reinterpret_cast(&socketAddr_), socketAddrLen_) < 0) { - HiLog::Error(LABEL, "Server: Bind socket fd %d, failed: %d", - connectFd, strerror_r(errno, err_string, ERR_STRING_SZ)); + HiLog::Error(LABEL, "Server: Bind socket fd %d, failed: %d", connectFd, errno); return -1; } if (chown(socketAddr_.sun_path, APPSPAWN_ID_ROOT, APPSPAWN_ID_SYSTEM)) { - HiLog::Error(LABEL, "Server: failed to chown socket fd %d, failed: %d", - connectFd, strerror_r(errno, err_string, ERR_STRING_SZ)); + HiLog::Error(LABEL, "Server: failed to chown socket fd %d, failed: %d", connectFd, errno); return -1; } if (chmod(socketAddr_.sun_path, SOCKET_PERM)) { - HiLog::Error(LABEL, "Server: failed to chmod socket fd %d, failed: %d", - connectFd, strerror_r(errno, err_string, ERR_STRING_SZ)); + HiLog::Error(LABEL, "Server: failed to chmod socket fd %d, failed: %d", connectFd, errno); if ((unlink(socketAddr_.sun_path) != 0) && (errno != ENOENT)) { - HiLog::Error(LABEL, "Server: Failed to unlink, err %d", strerror_r(errno, err_string, ERR_STRING_SZ)); + HiLog::Error(LABEL, "Server: Failed to unlink, err %d", errno); } return -1; } @@ -149,7 +143,6 @@ 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; @@ -165,7 +158,7 @@ int ServerSocket::RegisterServerSocket(int &connectFd) "Server: Register socket fd %d with backlog %d error: %d", connectFd, listenBacklog_, - strerror_r(errno, err_string, ERR_STRING_SZ)); + errno); close(connectFd); connectFd = -1; return -1; @@ -187,7 +180,6 @@ 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; @@ -206,8 +198,7 @@ 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 %d", - connFd, strerror_r(errno, err_string, ERR_STRING_SZ)); + HiLog::Warn(LABEL, "Server: Failed to set opt of Connection %d, err %d", connFd, errno); } HiLog::Debug(LABEL, "Server: Connection accepted, connect fd %d", connFd);