mirror of
https://gitee.com/openharmony/startup_init
synced 2024-11-23 16:20:00 +00:00
commit
3b2a151444
@ -22,20 +22,37 @@
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/time.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/un.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "le_utils.h"
|
||||
|
||||
static int SetSocketTimeout(int fd)
|
||||
{
|
||||
struct timeval timeout;
|
||||
timeout.tv_sec = SOCKET_TIMEOUT;
|
||||
timeout.tv_usec = 0;
|
||||
int ret = setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(timeout));
|
||||
LE_CHECK(ret == 0, return ret, "Failed to set socket option");
|
||||
|
||||
ret = setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));
|
||||
LE_CHECK(ret == 0, return ret, "Failed to set socket option");
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int CreatePipeServerSocket_(const char *server, int maxClient, int public)
|
||||
{
|
||||
int listenfd = socket(PF_UNIX, SOCK_STREAM, 0);
|
||||
LE_CHECK(listenfd > 0, return listenfd, "Failed to create socket errno %d", errno);
|
||||
|
||||
int ret = SetSocketTimeout(listenfd);
|
||||
LE_CHECK(ret == 0, return ret, "Failed to set socket timeout");
|
||||
|
||||
unlink(server);
|
||||
struct sockaddr_un serverAddr;
|
||||
int ret = memset_s(&serverAddr, sizeof(serverAddr), 0, sizeof(serverAddr));
|
||||
ret = memset_s(&serverAddr, sizeof(serverAddr), 0, sizeof(serverAddr));
|
||||
LE_CHECK(ret == 0, close(listenfd);
|
||||
return ret, "Failed to memory set. error: %d", errno);
|
||||
serverAddr.sun_family = AF_UNIX;
|
||||
@ -71,6 +88,9 @@ static int CreatePipeSocket_(const char *server)
|
||||
int ret = setsockopt(fd, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on));
|
||||
LE_CHECK(ret == 0, return ret, "Failed to set socket option");
|
||||
|
||||
ret = SetSocketTimeout(fd);
|
||||
LE_CHECK(ret == 0, return ret, "Failed to set socket timeout");
|
||||
|
||||
struct sockaddr_un serverAddr;
|
||||
ret = memset_s(&serverAddr, sizeof(serverAddr), 0, sizeof(serverAddr));
|
||||
LE_CHECK(ret == 0, close(fd);
|
||||
@ -107,9 +127,12 @@ static int CreateTcpServerSocket_(const char *server, int maxClient)
|
||||
int listenfd = socket(AF_INET, SOCK_STREAM, 0);
|
||||
LE_CHECK(listenfd > 0, return listenfd, "Failed to create socket");
|
||||
|
||||
int ret = SetSocketTimeout(listenfd);
|
||||
LE_CHECK(ret == 0, return ret, "Failed to set socket timeout");
|
||||
|
||||
struct sockaddr_in serverAddr;
|
||||
GetSockaddrFromServer_(server, &serverAddr);
|
||||
int ret = bind(listenfd, (struct sockaddr *)&serverAddr, sizeof(serverAddr));
|
||||
ret = bind(listenfd, (struct sockaddr *)&serverAddr, sizeof(serverAddr));
|
||||
LE_CHECK(ret >= 0, close(listenfd);
|
||||
return ret, "Failed to bind socket");
|
||||
SetNoBlock(listenfd);
|
||||
@ -130,6 +153,9 @@ static int CreateTcpSocket_(const char *server)
|
||||
int ret = setsockopt(fd, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on));
|
||||
LE_CHECK(ret == 0, return ret, "Failed to set socket option");
|
||||
|
||||
ret = SetSocketTimeout(fd);
|
||||
LE_CHECK(ret == 0, return ret, "Failed to set socket timeout");
|
||||
|
||||
struct sockaddr_in serverAddr;
|
||||
GetSockaddrFromServer_(server, &serverAddr);
|
||||
ret = connect(fd, (struct sockaddr *)&serverAddr, sizeof(serverAddr));
|
||||
|
@ -24,6 +24,9 @@
|
||||
extern "C" {
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#define SOCKET_TIMEOUT 3
|
||||
|
||||
INIT_LOCAL_API
|
||||
int CreateSocket(int flags, const char *server);
|
||||
INIT_LOCAL_API
|
||||
|
Loading…
Reference in New Issue
Block a user