fix: os return code

This commit is contained in:
dapan1121 2024-07-24 16:37:54 +08:00
parent 5a19d80167
commit 8691908cc3
14 changed files with 168 additions and 98 deletions

View File

@ -65,7 +65,7 @@ typedef int (*__compar_fn_t)(const void *, const void *);
#endif
#define ssize_t int
#define _SSIZE_T_
#define bzero(ptr, size) memset((ptr), 0, (size))
#define bzero(ptr, size) (void)memset((ptr), 0, (size))
#define strcasecmp _stricmp
#define strncasecmp _strnicmp
#define wcsncasecmp _wcsnicmp

View File

@ -49,6 +49,9 @@ typedef BOOL (*FSignalHandler)(DWORD fdwCtrlType);
#else
typedef void (*FSignalHandler)(int32_t signum, void *sigInfo, void *context);
#endif
typedef void (*sighandler_t)(int);
int32_t taosSetSignal(int32_t signum, FSignalHandler sigfp);
int32_t taosIgnSignal(int32_t signum);
int32_t taosDflSignal(int32_t signum);

View File

@ -159,13 +159,12 @@ TdSocketPtr taosAcceptTcpConnectSocket(TdSocketServerPtr pServerSocket, st
int32_t taosGetSocketName(TdSocketPtr pSocket, struct sockaddr *destAddr, int *addrLen);
void taosBlockSIGPIPE();
uint32_t taosGetIpv4FromFqdn(const char *);
int32_t taosBlockSIGPIPE();
int32_t taosGetIpv4FromFqdn(const char *fqdn, uint32_t* ip);
int32_t taosGetFqdn(char *);
void tinet_ntoa(char *ipstr, uint32_t ip);
uint32_t ip2uint(const char *const ip_addr);
void taosIgnSIGPIPE();
void taosSetMaskSIGPIPE();
int32_t taosIgnSIGPIPE();
uint32_t taosInetAddr(const char *ipAddr);
const char *taosInetNtoa(struct in_addr ipInt, char *dstStr, int32_t len);

View File

@ -1368,8 +1368,9 @@ int initEpSetFromCfg(const char* firstEp, const char* secondEp, SCorEpSet* pEpSe
terrno = TSDB_CODE_TSC_INVALID_FQDN;
return terrno;
}
uint32_t addr = taosGetIpv4FromFqdn(mgmtEpSet->eps[mgmtEpSet->numOfEps].fqdn);
if (addr == 0xffffffff) {
uint32_t addr = 0;
code = taosGetIpv4FromFqdn(mgmtEpSet->eps[mgmtEpSet->numOfEps].fqdn, &addr);
if (code) {
tscError("failed to resolve firstEp fqdn: %s, code:%s", mgmtEpSet->eps[mgmtEpSet->numOfEps].fqdn,
tstrerror(TSDB_CODE_TSC_INVALID_FQDN));
memset(&(mgmtEpSet->eps[mgmtEpSet->numOfEps]), 0, sizeof(mgmtEpSet->eps[mgmtEpSet->numOfEps]));
@ -1385,8 +1386,9 @@ int initEpSetFromCfg(const char* firstEp, const char* secondEp, SCorEpSet* pEpSe
}
taosGetFqdnPortFromEp(secondEp, &mgmtEpSet->eps[mgmtEpSet->numOfEps]);
uint32_t addr = taosGetIpv4FromFqdn(mgmtEpSet->eps[mgmtEpSet->numOfEps].fqdn);
if (addr == 0xffffffff) {
uint32_t addr = 0;
int32_t code = taosGetIpv4FromFqdn(mgmtEpSet->eps[mgmtEpSet->numOfEps].fqdn, &addr);
if (code) {
tscError("failed to resolve secondEp fqdn: %s, code:%s", mgmtEpSet->eps[mgmtEpSet->numOfEps].fqdn,
tstrerror(TSDB_CODE_TSC_INVALID_FQDN));
memset(&(mgmtEpSet->eps[mgmtEpSet->numOfEps]), 0, sizeof(mgmtEpSet->eps[mgmtEpSet->numOfEps]));

View File

@ -1354,8 +1354,9 @@ int32_t taosReadDataFolder(const char *cfgDir, const char **envCmd, const char *
}
static int32_t taosCheckGlobalCfg() {
uint32_t ipv4 = taosGetIpv4FromFqdn(tsLocalFqdn);
if (ipv4 == 0xffffffff) {
uint32_t ipv4 = 0;
int32_t code = taosGetIpv4FromFqdn(tsLocalFqdn, &ipv4);
if (code) {
terrno = TSDB_CODE_RPC_FQDN_ERROR;
uError("failed to get ip from fqdn:%s since %s, dnode can not be initialized", tsLocalFqdn, terrstr());
return -1;

View File

@ -294,7 +294,11 @@ int64_t mndGetIpWhiteVer(SMnode *pMnode) {
bool mndUpdateIpWhiteImpl(SHashObj *pIpWhiteTab, char *user, char *fqdn, int8_t type) {
bool update = false;
SIpV4Range range = {.ip = taosGetIpv4FromFqdn(fqdn), .mask = 32};
SIpV4Range range = {.ip = 0, .mask = 32};
int32_t code = taosGetIpv4FromFqdn(fqdn, &range.ip);
if (code) {
//TODO
}
mDebug("ip-white-list may update for user: %s, fqdn: %s", user, fqdn);
SIpWhiteList **ppList = taosHashGet(pIpWhiteTab, user, strlen(user));
SIpWhiteList *pList = NULL;

View File

@ -48,8 +48,8 @@ bool syncUtilNodeInfo2RaftId(const SNodeInfo* pInfo, SyncGroupId vgId, SRaftId*
"dnode:%d cluster:%" PRId64 " fqdn:%s port:%u ",
vgId, tsResolveFQDNRetryTime, pInfo->nodeId, pInfo->clusterId, pInfo->nodeFqdn, pInfo->nodePort);
for (int i = 0; i < tsResolveFQDNRetryTime; i++) {
ipv4 = taosGetIpv4FromFqdn(pInfo->nodeFqdn);
if (ipv4 == 0xFFFFFFFF || ipv4 == 1) {
int32_t code = taosGetIpv4FromFqdn(pInfo->nodeFqdn, &ipv4);
if (code) {
sError("failed to resolve ipv4 addr, fqdn:%s, wait one second", pInfo->nodeFqdn);
taosSsleep(1);
} else {

View File

@ -525,8 +525,9 @@ void syncUtilU642Addr(uint64_t u64, char *host, int64_t len, uint16_t *port) {
}
uint64_t syncUtilAddr2U64(const char *host, uint16_t port) {
uint32_t hostU32 = taosGetIpv4FromFqdn(host);
if (hostU32 == (uint32_t)-1) {
uint32_t hostU32 = 0;
int32_t code = taosGetIpv4FromFqdn(host, &hostU32);
if (code) {
sError("failed to resolve ipv4 addr, host:%s", host);
terrno = TSDB_CODE_TSC_INVALID_FQDN;
return -1;

View File

@ -190,8 +190,9 @@ _OVER:
}
static FORCE_INLINE int32_t taosBuildDstAddr(const char* server, uint16_t port, struct sockaddr_in* dest) {
uint32_t ip = taosGetIpv4FromFqdn(server);
if (ip == 0xffffffff) {
uint32_t ip = 0;
int32_t code = taosGetIpv4FromFqdn(server, &ip);
if (code) {
tError("http-report failed to resolving domain names: %s", server);
return TSDB_CODE_RPC_FQDN_ERROR;
}

View File

@ -26,8 +26,8 @@ void (*taosUnRefHandle[])(void* handle) = {transUnrefSrvHandle, transUnrefCliHan
int (*transReleaseHandle[])(void* handle) = {transReleaseSrvHandle, transReleaseCliHandle};
static int32_t transValidLocalFqdn(const char* localFqdn, uint32_t* ip) {
*ip = taosGetIpv4FromFqdn(localFqdn);
if (*ip == 0xFFFFFFFF) {
int32_t code = taosGetIpv4FromFqdn(localFqdn, ip);
if (code) {
terrno = TSDB_CODE_RPC_FQDN_ERROR;
return -1;
}

View File

@ -196,7 +196,7 @@ static FORCE_INLINE void cliMayCvtFqdnToIp(SEpSet* pEpSet, SCvtAddr* pCvtAddr);
static FORCE_INLINE int32_t cliBuildExceptResp(SCliMsg* pMsg, STransMsg* resp);
static FORCE_INLINE uint32_t cliGetIpFromFqdnCache(SHashObj* cache, char* fqdn);
static FORCE_INLINE int32_t cliGetIpFromFqdnCache(SHashObj* cache, char* fqdn, uint32_t* ip);
static FORCE_INLINE void cliUpdateFqdnCache(SHashObj* cache, char* fqdn);
static FORCE_INLINE void cliMayUpdateFqdnCache(SHashObj* cache, char* dst);
@ -1253,8 +1253,9 @@ static void cliHandleBatchReq(SCliBatch* pBatch, SCliThrd* pThrd) {
conn->pBatch = pBatch;
conn->dstAddr = taosStrdup(pList->dst);
uint32_t ipaddr = cliGetIpFromFqdnCache(pThrd->fqdn2ipCache, pList->ip);
if (ipaddr == 0xffffffff) {
uint32_t ipaddr = 0;
int32_t code = cliGetIpFromFqdnCache(pThrd->fqdn2ipCache, pList->ip, &ipaddr);
if (code) {
uv_timer_stop(conn->timer);
conn->timer->data = NULL;
taosArrayPush(pThrd->timerList, &conn->timer);
@ -1561,28 +1562,28 @@ FORCE_INLINE int32_t cliBuildExceptResp(SCliMsg* pMsg, STransMsg* pResp) {
return 0;
}
static FORCE_INLINE uint32_t cliGetIpFromFqdnCache(SHashObj* cache, char* fqdn) {
uint32_t addr = 0;
static FORCE_INLINE int32_t cliGetIpFromFqdnCache(SHashObj* cache, char* fqdn, uint32_t* ip) {
size_t len = strlen(fqdn);
uint32_t* v = taosHashGet(cache, fqdn, len);
if (v == NULL) {
addr = taosGetIpv4FromFqdn(fqdn);
if (addr == 0xffffffff) {
terrno = TSDB_CODE_RPC_FQDN_ERROR;
int32_t code = taosGetIpv4FromFqdn(fqdn, ip);
if (code) {
tError("failed to get ip from fqdn:%s since %s", fqdn, terrstr());
return addr;
return code;
}
taosHashPut(cache, fqdn, len, &addr, sizeof(addr));
taosHashPut(cache, fqdn, len, ip, sizeof(*ip));
} else {
addr = *v;
*ip = *v;
}
return addr;
return TSDB_CODE_SUCCESS;
}
static FORCE_INLINE void cliUpdateFqdnCache(SHashObj* cache, char* fqdn) {
// impl later
uint32_t addr = taosGetIpv4FromFqdn(fqdn);
if (addr != 0xffffffff) {
uint32_t addr = 0;
int32_t code = taosGetIpv4FromFqdn(fqdn, &addr);
if (TSDB_CODE_SUCCESS == code) {
size_t len = strlen(fqdn);
uint32_t* v = taosHashGet(cache, fqdn, len);
if (addr != *v) {
@ -1671,8 +1672,9 @@ void cliHandleReq(SCliMsg* pMsg, SCliThrd* pThrd) {
conn->dstAddr = taosStrdup(addr);
uint32_t ipaddr = cliGetIpFromFqdnCache(pThrd->fqdn2ipCache, fqdn);
if (ipaddr == 0xffffffff) {
uint32_t ipaddr = 0;
int32_t code = cliGetIpFromFqdnCache(pThrd->fqdn2ipCache, fqdn, &ipaddr);
if (code) {
uv_timer_stop(conn->timer);
conn->timer->data = NULL;
taosArrayPush(pThrd->timerList, &conn->timer);

View File

@ -56,7 +56,6 @@ void taosKillChildOnParentStopped() {}
*/
typedef void (*FLinuxSignalHandler)(int32_t signum, siginfo_t *sigInfo, void *context);
typedef void (*sighandler_t)(int);
int32_t taosSetSignal(int32_t signum, FSignalHandler sigfp) {
struct sigaction act;

View File

@ -122,18 +122,25 @@ int32_t taosCloseSocketNoCheck1(SocketFd fd) {
#ifdef WINDOWS
return closesocket(fd);
#else
return close(fd);
int32_t code = close(fd);
if (-1 == code) {
terrno = TAOS_SYSTEM_ERROR(errno);
return terrno;
}
return code;
#endif
}
int32_t taosCloseSocket(TdSocketPtr *ppSocket) {
int32_t code;
if (ppSocket == NULL || *ppSocket == NULL || (*ppSocket)->fd < 0) {
return -1;
terrno = TSDB_CODE_INVALID_PARA;
return terrno;
}
code = taosCloseSocketNoCheck1((*ppSocket)->fd);
(*ppSocket)->fd = -1;
taosMemoryFree(*ppSocket);
return code;
}
@ -259,8 +266,10 @@ int32_t taosSetNonblocking(TdSocketPtr pSocket, int32_t on) {
int32_t taosSetSockOpt(TdSocketPtr pSocket, int32_t level, int32_t optname, void *optval, int32_t optlen) {
if (pSocket == NULL || pSocket->fd < 0) {
return -1;
terrno = TSDB_CODE_INVALID_PARA;
return terrno;
}
#ifdef WINDOWS
#ifdef TCP_KEEPCNT
if (level == SOL_SOCKET && optname == TCP_KEEPCNT) {
@ -288,7 +297,12 @@ int32_t taosSetSockOpt(TdSocketPtr pSocket, int32_t level, int32_t optname, void
return setsockopt(pSocket->fd, level, optname, optval, optlen);
#else
return setsockopt(pSocket->fd, level, optname, optval, (int)optlen);
int32_t code = setsockopt(pSocket->fd, level, optname, optval, (int)optlen);
if (-1 == code) {
terrno = TAOS_SYSTEM_ERROR(errno);
return terrno;
}
return code;
#endif
}
@ -321,7 +335,12 @@ uint32_t taosInetAddr(const char *ipAddr) {
#endif
}
const char *taosInetNtoa(struct in_addr ipInt, char *dstStr, int32_t len) {
return inet_ntop(AF_INET, &ipInt, dstStr, len);
const char* r = inet_ntop(AF_INET, &ipInt, dstStr, len);
if (NULL == r) {
terrno = TAOS_SYSTEM_ERROR(errno);
}
return r;
}
#ifndef SIGPIPE
@ -733,6 +752,7 @@ bool taosValidIpAndPort(uint32_t ip, uint16_t port) {
struct sockaddr_in serverAdd;
SocketFd fd;
int32_t reuse;
int32_t code = 0;
// printf("open tcp server socket:0x%x:%hu", ip, port);
@ -746,16 +766,17 @@ bool taosValidIpAndPort(uint32_t ip, uint16_t port) {
serverAdd.sin_port = (uint16_t)htons(port);
fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (fd < 0) { // exception
return false;
} else if (fd <= 2) { // in, out, err
taosCloseSocketNoCheck1(fd);
if (-1 == fd) { // exception
terrno = TAOS_SYSTEM_ERROR(errno);
return false;
}
TdSocketPtr pSocket = (TdSocketPtr)taosMemoryMalloc(sizeof(TdSocket));
if (pSocket == NULL) {
taosCloseSocketNoCheck1(fd);
code = terrno;
(void)taosCloseSocketNoCheck1(fd);
terrno = code;
return false;
}
pSocket->refId = 0;
@ -764,19 +785,24 @@ bool taosValidIpAndPort(uint32_t ip, uint16_t port) {
/* set REUSEADDR option, so the portnumber can be re-used */
reuse = 1;
if (taosSetSockOpt(pSocket, SOL_SOCKET, SO_REUSEADDR, (void *)&reuse, sizeof(reuse)) < 0) {
// printf("setsockopt SO_REUSEADDR failed: %d (%s)", errno, strerror(errno));
taosCloseSocket(&pSocket);
code = terrno;
(void)taosCloseSocket(&pSocket);
terrno = code;
return false;
}
/* bind socket to server address */
if (bind(pSocket->fd, (struct sockaddr *)&serverAdd, sizeof(serverAdd)) < 0) {
// printf("bind tcp server socket failed, 0x%x:%hu(%s)", ip, port, strerror(errno));
taosCloseSocket(&pSocket);
if (-1 == bind(pSocket->fd, (struct sockaddr *)&serverAdd, sizeof(serverAdd))) {
code = TAOS_SYSTEM_ERROR(errno);
(void)taosCloseSocket(&pSocket);
terrno = code;
return false;
}
taosCloseSocket(&pSocket);
(void)taosCloseSocket(&pSocket);
return true;
// return 0 == taosValidIp(ip) ? true : false;
}
#if 0
@ -898,21 +924,24 @@ int64_t taosCopyFds(TdSocketPtr pSrcSocket, TdSocketPtr pDestSocket, int64_t len
#endif // endif 0
void taosBlockSIGPIPE() {
int32_t taosBlockSIGPIPE() {
#ifdef WINDOWS
// ASSERT(0);
return 0;
#else
sigset_t signal_mask;
sigemptyset(&signal_mask);
sigaddset(&signal_mask, SIGPIPE);
(void)sigemptyset(&signal_mask);
(void)sigaddset(&signal_mask, SIGPIPE);
int32_t rc = pthread_sigmask(SIG_BLOCK, &signal_mask, NULL);
if (rc != 0) {
// printf("failed to block SIGPIPE");
terrno = TAOS_SYSTEM_ERROR(rc);
return terrno;
}
return 0;
#endif
}
uint32_t taosGetIpv4FromFqdn(const char *fqdn) {
int32_t taosGetIpv4FromFqdn(const char *fqdn, uint32_t* ip) {
#ifdef WINDOWS
// Initialize Winsock
WSADATA wsaData;
@ -929,25 +958,29 @@ uint32_t taosGetIpv4FromFqdn(const char *fqdn) {
struct addrinfo *result = NULL;
int32_t ret = getaddrinfo(fqdn, NULL, &hints, &result);
if (result) {
while (true) {
int32_t ret = getaddrinfo(fqdn, NULL, &hints, &result);
if (ret) {
if (EAI_AGAIN == ret) {
continue;
} else if (EAI_SYSTEM == ret) {
terrno = TAOS_SYSTEM_ERROR(errno);
return terrno;
}
terrno = TAOS_SYSTEM_ERROR(ret);
return terrno;
}
struct sockaddr *sa = result->ai_addr;
struct sockaddr_in *si = (struct sockaddr_in *)sa;
struct in_addr ia = si->sin_addr;
uint32_t ip = ia.s_addr;
*ip = ia.s_addr;
freeaddrinfo(result);
return ip;
} else {
#ifdef EAI_SYSTEM
if (ret == EAI_SYSTEM) {
// printf("failed to get the ip address, fqdn:%s, errno:%d, since:%s", fqdn, errno, strerror(errno));
} else {
// printf("failed to get the ip address, fqdn:%s, ret:%d, since:%s", fqdn, ret, gai_strerror(ret));
}
#else
// printf("failed to get the ip address, fqdn:%s, ret:%d, since:%s", fqdn, ret, gai_strerror(ret));
#endif
return 0xFFFFFFFF;
return 0;
}
}
@ -964,14 +997,9 @@ int32_t taosGetFqdn(char *fqdn) {
#endif
char hostname[1024];
hostname[1023] = '\0';
if (taosGetlocalhostname(hostname, 1023) == -1) {
#ifdef WINDOWS
printf("failed to get hostname, reason:%s\n", strerror(WSAGetLastError()));
#else
printf("failed to get hostname, reason:%s\n", strerror(errno));
#endif
ASSERT(0);
return -1;
int32_t code = taosGetlocalhostname(hostname, 1023);
if (code) {
return code;
}
#ifdef __APPLE__
@ -987,12 +1015,25 @@ int32_t taosGetFqdn(char *fqdn) {
struct addrinfo *result = NULL;
hints.ai_flags = AI_CANONNAME;
int32_t ret = getaddrinfo(hostname, NULL, &hints, &result);
if (!result) {
fprintf(stderr, "failed to get fqdn, code:%d, reason:%s\n", ret, gai_strerror(ret));
return -1;
while (true) {
int32_t ret = getaddrinfo(hostname, NULL, &hints, &result);
if (ret) {
if (EAI_AGAIN == ret) {
continue;
} else if (EAI_SYSTEM == ret) {
terrno = TAOS_SYSTEM_ERROR(errno);
return terrno;
}
terrno = TAOS_SYSTEM_ERROR(ret);
return terrno;
}
break;
}
strcpy(fqdn, result->ai_canonname);
(void)strcpy(fqdn, result->ai_canonname);
freeaddrinfo(result);
#endif // linux
@ -1000,18 +1041,29 @@ int32_t taosGetFqdn(char *fqdn) {
}
void tinet_ntoa(char *ipstr, uint32_t ip) {
sprintf(ipstr, "%d.%d.%d.%d", ip & 0xFF, (ip >> 8) & 0xFF, (ip >> 16) & 0xFF, ip >> 24);
(void)sprintf(ipstr, "%d.%d.%d.%d", ip & 0xFF, (ip >> 8) & 0xFF, (ip >> 16) & 0xFF, ip >> 24);
}
void taosIgnSIGPIPE() { signal(SIGPIPE, SIG_IGN); }
int32_t taosIgnSIGPIPE() {
sighandler_t h = signal(SIGPIPE, SIG_IGN);
if (SIG_ERR == h) {
terrno = TAOS_SYSTEM_ERROR(errno);
return terrno;
}
void taosSetMaskSIGPIPE() {
return 0;
}
#if 0
int32_t taosSetMaskSIGPIPE() {
#ifdef WINDOWS
// ASSERT(0);
#else
sigset_t signal_mask;
sigemptyset(&signal_mask);
sigaddset(&signal_mask, SIGPIPE);
(void)sigemptyset(&signal_mask);
(void)sigaddset(&signal_mask, SIGPIPE);
int32_t rc = pthread_sigmask(SIG_SETMASK, &signal_mask, NULL);
if (rc != 0) {
// printf("failed to setmask SIGPIPE");
@ -1019,7 +1071,6 @@ void taosSetMaskSIGPIPE() {
#endif
}
#if 0
int32_t taosGetSocketName(TdSocketPtr pSocket, struct sockaddr *destAddr, int *addrLen) {
if (pSocket == NULL || pSocket->fd < 0) {
return -1;
@ -1038,9 +1089,12 @@ int32_t taosCreateSocketWithTimeout(uint32_t timeout) {
#else
int fd;
#endif
if ((fd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) == INVALID_SOCKET) {
terrno = TAOS_SYSTEM_ERROR(errno);
return -1;
}
#if defined(WINDOWS)
if (0 != setsockopt(fd, IPPROTO_TCP, TCP_MAXRT, (char *)&timeout, sizeof(timeout))) {
taosCloseSocketNoCheck1(fd);
@ -1055,8 +1109,10 @@ int32_t taosCreateSocketWithTimeout(uint32_t timeout) {
//}
#else // Linux like systems
uint32_t conn_timeout_ms = timeout;
if (0 != setsockopt(fd, IPPROTO_TCP, TCP_USER_TIMEOUT, (char *)&conn_timeout_ms, sizeof(conn_timeout_ms))) {
taosCloseSocketNoCheck1(fd);
if (-1 == setsockopt(fd, IPPROTO_TCP, TCP_USER_TIMEOUT, (char *)&conn_timeout_ms, sizeof(conn_timeout_ms))) {
int32_t code = TAOS_SYSTEM_ERROR(errno);
(void)taosCloseSocketNoCheck1(fd);
terrno = code;
return -1;
}
#endif

View File

@ -40,7 +40,8 @@ TEST(osTest, osFQDNSuccess) {
char fqdn[1024];
char ipString[INET_ADDRSTRLEN];
int code = taosGetFqdn(fqdn);
uint32_t ipv4 = taosGetIpv4FromFqdn(fqdn);
uint32_t ipv4 = 0;
code = taosGetIpv4FromFqdn(fqdn, &ipv4);
ASSERT_NE(ipv4, 0xffffffff);
struct in_addr addr;
@ -54,7 +55,8 @@ TEST(osTest, osFQDNSuccess) {
TEST(osTest, osFQDNFailed) {
char fqdn[1024] = "fqdn_test_not_found";
char ipString[24];
uint32_t ipv4 = taosGetIpv4FromFqdn(fqdn);
uint32_t ipv4 = 0;
int32_t code = taosGetIpv4FromFqdn(fqdn, &ipv4);
ASSERT_EQ(ipv4, 0xffffffff);
terrno = TSDB_CODE_RPC_FQDN_ERROR;