Fix/xsren/td 20817 mac fqdn (#19078)
* fix/TD-20817-mac-fqdn use localhostname * fix/TD-20817-mac-fqdn redundant codes * fix/TD-20817-mac-fqdn,replace command with api Co-authored-by: facetosea <25808407@qq.com> Co-authored-by: Shuduo Sang <sangshuduo@gmail.com>
This commit is contained in:
parent
fb628f7fdf
commit
853b93fbe1
|
@ -70,6 +70,7 @@ typedef struct {
|
||||||
|
|
||||||
SysNameInfo taosGetSysNameInfo();
|
SysNameInfo taosGetSysNameInfo();
|
||||||
bool taosCheckCurrentInDll();
|
bool taosCheckCurrentInDll();
|
||||||
|
int taosGetlocalhostname(char *hostname, size_t maxLen);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
|
|
|
@ -44,6 +44,10 @@ if(TD_WINDOWS)
|
||||||
os PUBLIC ws2_32 iconv msvcregex wcwidth winmm crashdump
|
os PUBLIC ws2_32 iconv msvcregex wcwidth winmm crashdump
|
||||||
)
|
)
|
||||||
elseif(TD_DARWIN_64)
|
elseif(TD_DARWIN_64)
|
||||||
|
find_library(CORE_FOUNDATION_FRAMEWORK CoreFoundation)
|
||||||
|
target_link_libraries(os PUBLIC ${CORE_FOUNDATION_FRAMEWORK})
|
||||||
|
find_library(SYSTEM_CONFIGURATION_FRAMEWORK SystemConfiguration)
|
||||||
|
target_link_libraries(os PUBLIC ${SYSTEM_CONFIGURATION_FRAMEWORK})
|
||||||
target_link_libraries(
|
target_link_libraries(
|
||||||
os PUBLIC dl m iconv
|
os PUBLIC dl m iconv
|
||||||
)
|
)
|
||||||
|
|
|
@ -988,7 +988,7 @@ int32_t taosGetFqdn(char *fqdn) {
|
||||||
#endif
|
#endif
|
||||||
char hostname[1024];
|
char hostname[1024];
|
||||||
hostname[1023] = '\0';
|
hostname[1023] = '\0';
|
||||||
if (gethostname(hostname, 1023) == -1) {
|
if (taosGetlocalhostname(hostname, 1023) == -1) {
|
||||||
#ifdef WINDOWS
|
#ifdef WINDOWS
|
||||||
printf("failed to get hostname, reason:%s\n", strerror(WSAGetLastError()));
|
printf("failed to get hostname, reason:%s\n", strerror(WSAGetLastError()));
|
||||||
#else
|
#else
|
||||||
|
@ -998,30 +998,28 @@ int32_t taosGetFqdn(char *fqdn) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct addrinfo hints = {0};
|
|
||||||
struct addrinfo *result = NULL;
|
|
||||||
#ifdef __APPLE__
|
#ifdef __APPLE__
|
||||||
// on macosx, hostname -f has the form of xxx.local
|
// on macosx, hostname -f has the form of xxx.local
|
||||||
// which will block getaddrinfo for a few seconds if AI_CANONNAME is set
|
// which will block getaddrinfo for a few seconds if AI_CANONNAME is set
|
||||||
// thus, we choose AF_INET (ipv4 for the moment) to make getaddrinfo return
|
// thus, we choose AF_INET (ipv4 for the moment) to make getaddrinfo return
|
||||||
// immediately
|
// immediately
|
||||||
hints.ai_family = AF_INET;
|
// hints.ai_family = AF_INET;
|
||||||
|
strcpy(fqdn, hostname);
|
||||||
|
strcpy(fqdn+strlen(hostname), ".local");
|
||||||
#else // __APPLE__
|
#else // __APPLE__
|
||||||
|
struct addrinfo hints = {0};
|
||||||
|
struct addrinfo *result = NULL;
|
||||||
hints.ai_flags = AI_CANONNAME;
|
hints.ai_flags = AI_CANONNAME;
|
||||||
#endif // __APPLE__
|
|
||||||
int32_t ret = getaddrinfo(hostname, NULL, &hints, &result);
|
int32_t ret = getaddrinfo(hostname, NULL, &hints, &result);
|
||||||
if (!result) {
|
if (!result) {
|
||||||
fprintf(stderr, "failed to get fqdn, code:%d, reason:%s\n", ret, gai_strerror(ret));
|
fprintf(stderr, "failed to get fqdn, code:%d, reason:%s\n", ret, gai_strerror(ret));
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef __APPLE__
|
|
||||||
// refer to comments above
|
|
||||||
strcpy(fqdn, hostname);
|
|
||||||
#else // __APPLE__
|
|
||||||
strcpy(fqdn, result->ai_canonname);
|
strcpy(fqdn, result->ai_canonname);
|
||||||
#endif // __APPLE__
|
|
||||||
freeaddrinfo(result);
|
freeaddrinfo(result);
|
||||||
|
#endif // __APPLE__
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -98,6 +98,9 @@ LONG WINAPI exceptionHandler(LPEXCEPTION_POINTERS exception);
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <libproc.h>
|
#include <libproc.h>
|
||||||
#include <sys/sysctl.h>
|
#include <sys/sysctl.h>
|
||||||
|
#include <SystemConfiguration/SCDynamicStoreCopySpecific.h>
|
||||||
|
#include <CoreFoundation/CFString.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
#else
|
#else
|
||||||
|
|
||||||
|
@ -1007,6 +1010,11 @@ SysNameInfo taosGetSysNameInfo() {
|
||||||
tstrncpy(info.machine, uts.machine, sizeof(info.machine));
|
tstrncpy(info.machine, uts.machine, sizeof(info.machine));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
char localHostName[512];
|
||||||
|
taosGetlocalhostname(localHostName, 512);
|
||||||
|
TdCmdPtr pCmd = taosOpenCmd("scutil --get LocalHostName");
|
||||||
|
tstrncpy(info.nodename, localHostName, sizeof(info.nodename));
|
||||||
|
|
||||||
return info;
|
return info;
|
||||||
#else
|
#else
|
||||||
SysNameInfo info = {0};
|
SysNameInfo info = {0};
|
||||||
|
@ -1042,3 +1050,46 @@ bool taosCheckCurrentInDll() {
|
||||||
return false;
|
return false;
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifdef _TD_DARWIN_64
|
||||||
|
int taosGetMaclocalhostnameByCommand(char *hostname, size_t maxLen) {
|
||||||
|
TdCmdPtr pCmd = taosOpenCmd("scutil --get LocalHostName");
|
||||||
|
if (pCmd != NULL) {
|
||||||
|
if (taosGetsCmd(pCmd, maxLen - 1, hostname) > 0) {
|
||||||
|
int len = strlen(hostname);
|
||||||
|
if (hostname[len - 1] == '\n') {
|
||||||
|
hostname[len - 1] = '\0';
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
taosCloseCmd(&pCmd);
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int getMacLocalHostNameBySCD(char *hostname, size_t maxLen) {
|
||||||
|
SCDynamicStoreRef store = SCDynamicStoreCreate(NULL, CFSTR(""), NULL, NULL);
|
||||||
|
CFStringRef hostname_cfstr = SCDynamicStoreCopyLocalHostName(store);
|
||||||
|
if (hostname_cfstr != NULL) {
|
||||||
|
CFStringGetCString(hostname_cfstr, hostname, maxLen - 1, kCFStringEncodingMacRoman);
|
||||||
|
CFRelease(hostname_cfstr);
|
||||||
|
} else {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
CFRelease(store);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
int taosGetlocalhostname(char *hostname, size_t maxLen) {
|
||||||
|
#ifdef _TD_DARWIN_64
|
||||||
|
int res = getMacLocalHostNameBySCD(hostname, maxLen);
|
||||||
|
if (res != 0) {
|
||||||
|
return taosGetMaclocalhostnameByCommand(hostname, maxLen);
|
||||||
|
} else {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
return gethostname(hostname, maxLen);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue