fix: os return code

This commit is contained in:
dapan1121 2024-07-25 11:47:04 +08:00
parent 2f284b252f
commit d3ab79da10
5 changed files with 153 additions and 67 deletions

View File

@ -44,7 +44,7 @@ int64_t taosGetLineCmd(TdCmdPtr pCmd, char **__restrict ptrBuf);
int32_t taosEOFCmd(TdCmdPtr pCmd);
int64_t taosCloseCmd(TdCmdPtr *ppCmd);
void taosCloseCmd(TdCmdPtr *ppCmd);
void *taosLoadDll(const char *filename);
@ -54,11 +54,11 @@ void taosCloseDll(void *handle);
int32_t taosSetConsoleEcho(bool on);
void taosSetTerminalMode();
int32_t taosSetTerminalMode();
int32_t taosGetOldTerminalMode();
void taosResetTerminalMode();
int32_t taosResetTerminalMode();
#define STACKSIZE 100

View File

@ -952,6 +952,8 @@ int32_t taosGetIpv4FromFqdn(const char *fqdn, uint32_t* ip) {
return 0xFFFFFFFF;
}
#endif
#if defined(LINUX)
struct addrinfo hints = {0};
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
@ -984,6 +986,34 @@ int32_t taosGetIpv4FromFqdn(const char *fqdn, uint32_t* ip) {
return 0;
}
#else
struct addrinfo hints = {0};
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
struct addrinfo *result = NULL;
int32_t ret = getaddrinfo(fqdn, NULL, &hints, &result);
if (result) {
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;
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;
}
#endif
}
int32_t taosGetFqdn(char *fqdn) {

View File

@ -201,14 +201,14 @@ iconv_t taosAcquireConv(int32_t *idx, ConvType type) {
iconv_t c = iconv_open(DEFAULT_UNICODE_ENCODEC, tsCharset);
if ((iconv_t)-1 == c || (iconv_t)0 == c) {
terrno = TAOS_SYSTEM_ERROR(errno);
return c;
}
return c;
} else {
iconv_t c = iconv_open(tsCharset, DEFAULT_UNICODE_ENCODEC);
if ((iconv_t)-1 == c || (iconv_t)0 == c) {
terrno = TAOS_SYSTEM_ERROR(errno);
return c;
}
return c;
}
}

View File

@ -509,7 +509,9 @@ int32_t taosGetCpuInfo(char *cpuModel, int32_t maxLen, float *numOfCores) {
if (code != 0 && (done & 1) == 0) {
TdCmdPtr pCmd = taosOpenCmd("uname -a");
if (pCmd == NULL) return code;
if (pCmd == NULL) {
return terrno;
}
if (taosGetsCmd(pCmd, maxLen, cpuModel) > 0) {
code = 0;
done |= 1;
@ -539,10 +541,11 @@ static int32_t taosCntrGetCpuCores(float *numOfCores) {
}
char qline[32] = {0};
if (taosGetsFile(pFile, sizeof(qline), qline) <= 0) {
taosCloseFile(&pFile);
(void)taosCloseFile(&pFile);
goto _sys;
}
taosCloseFile(&pFile);
(void)taosCloseFile(&pFile);
float quota = taosStr2Float(qline, NULL);
if (quota < 0) {
goto _sys;
@ -551,12 +554,14 @@ static int32_t taosCntrGetCpuCores(float *numOfCores) {
if (!(pFile = taosOpenFile(tsCpuPeriodFile, TD_FILE_READ | TD_FILE_STREAM))) {
goto _sys;
}
char pline[32] = {0};
if (taosGetsFile(pFile, sizeof(pline), pline) <= 0) {
taosCloseFile(&pFile);
(void)taosCloseFile(&pFile);
goto _sys;
}
taosCloseFile(&pFile);
(void)taosCloseFile(&pFile);
float period = taosStr2Float(pline, NULL);
float quotaCores = quota / period;
@ -567,10 +572,13 @@ static int32_t taosCntrGetCpuCores(float *numOfCores) {
*numOfCores = sysCores;
}
goto _end;
_sys:
*numOfCores = sysconf(_SC_NPROCESSORS_ONLN);
_end:
return 0;
#endif
}
@ -587,7 +595,7 @@ int32_t taosGetCpuCores(float *numOfCores, bool physical) {
if (physical) {
*numOfCores = sysconf(_SC_NPROCESSORS_ONLN);
} else {
taosCntrGetCpuCores(numOfCores);
(void)taosCntrGetCpuCores(numOfCores);
}
return 0;
#endif
@ -706,7 +714,7 @@ int32_t taosGetProcMemory(int64_t *usedKB) {
TdFilePtr pFile = taosOpenFile(tsProcMemFile, TD_FILE_READ | TD_FILE_STREAM);
if (pFile == NULL) {
// printf("open file:%s failed", tsProcMemFile);
return -1;
return terrno;
}
ssize_t bytes = 0;
@ -722,9 +730,10 @@ int32_t taosGetProcMemory(int64_t *usedKB) {
}
char tmp[10];
sscanf(line, "%s %" PRId64, tmp, usedKB);
(void)sscanf(line, "%s %" PRId64, tmp, usedKB);
taosCloseFile(&pFile);
(void)taosCloseFile(&pFile);
return 0;
#endif
}
@ -783,13 +792,14 @@ int32_t taosGetDiskSize(char *dataDir, SDiskSize *diskSize) {
}
#else
struct statvfs info;
if (statvfs(dataDir, &info)) {
// terrno = TAOS_SYSTEM_ERROR(errno);
return -1;
if (-1 == statvfs(dataDir, &info)) {
terrno = TAOS_SYSTEM_ERROR(errno);
return terrno;
} else {
diskSize->total = info.f_blocks * info.f_frsize;
diskSize->avail = info.f_bavail * info.f_frsize;
diskSize->used = diskSize->total - diskSize->avail;
return 0;
}
#endif
@ -814,7 +824,9 @@ int32_t taosGetProcIO(int64_t *rchars, int64_t *wchars, int64_t *read_bytes, int
return 0;
#else
TdFilePtr pFile = taosOpenFile(tsProcIOFile, TD_FILE_READ | TD_FILE_STREAM);
if (pFile == NULL) return -1;
if (pFile == NULL) {
return terrno;
}
ssize_t bytes = 0;
char line[1024] = {0};
@ -827,16 +839,16 @@ int32_t taosGetProcIO(int64_t *rchars, int64_t *wchars, int64_t *read_bytes, int
break;
}
if (strstr(line, "rchar:") != NULL) {
sscanf(line, "%s %" PRId64, tmp, rchars);
(void)sscanf(line, "%s %" PRId64, tmp, rchars);
readIndex++;
} else if (strstr(line, "wchar:") != NULL) {
sscanf(line, "%s %" PRId64, tmp, wchars);
(void)sscanf(line, "%s %" PRId64, tmp, wchars);
readIndex++;
} else if (strstr(line, "read_bytes:") != NULL) { // read_bytes
sscanf(line, "%s %" PRId64, tmp, read_bytes);
(void)sscanf(line, "%s %" PRId64, tmp, read_bytes);
readIndex++;
} else if (strstr(line, "write_bytes:") != NULL) { // write_bytes
sscanf(line, "%s %" PRId64, tmp, write_bytes);
(void)sscanf(line, "%s %" PRId64, tmp, write_bytes);
readIndex++;
} else {
}
@ -844,7 +856,7 @@ int32_t taosGetProcIO(int64_t *rchars, int64_t *wchars, int64_t *read_bytes, int
if (readIndex >= 4) break;
}
taosCloseFile(&pFile);
(void)taosCloseFile(&pFile);
if (readIndex < 4) {
return -1;
@ -898,7 +910,9 @@ int32_t taosGetCardInfo(int64_t *receive_bytes, int64_t *transmit_bytes) {
return 0;
#else
TdFilePtr pFile = taosOpenFile(tsSysNetFile, TD_FILE_READ | TD_FILE_STREAM);
if (pFile == NULL) return -1;
if (pFile == NULL) {
return terrno;
}
ssize_t _bytes = 0;
char line[1024];
@ -927,7 +941,7 @@ int32_t taosGetCardInfo(int64_t *receive_bytes, int64_t *transmit_bytes) {
continue;
}
sscanf(line,
(void)sscanf(line,
"%s %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64
" %" PRId64,
nouse0, &o_rbytes, &rpackts, &nouse1, &nouse2, &nouse3, &nouse4, &nouse5, &nouse6, &o_tbytes, &tpackets);
@ -935,7 +949,7 @@ int32_t taosGetCardInfo(int64_t *receive_bytes, int64_t *transmit_bytes) {
*transmit_bytes += o_tbytes;
}
taosCloseFile(&pFile);
(void)taosCloseFile(&pFile);
return 0;
#endif
@ -973,8 +987,8 @@ void taosKillSystem() {
exit(0);
#else
// SIGINT
printf("taosd will shut down soon");
kill(tsProcId, 2);
(void)printf("taosd will shut down soon");
(void)kill(tsProcId, 2);
#endif
}
@ -1006,10 +1020,13 @@ int32_t taosGetSystemUUID(char *uid, int32_t uidlen) {
// fd = open("/proc/sys/kernel/random/uuid", 0);
TdFilePtr pFile = taosOpenFile("/proc/sys/kernel/random/uuid", TD_FILE_READ);
if (pFile == NULL) {
return -1;
return terrno;
} else {
len = taosReadFile(pFile, uid, uidlen);
taosCloseFile(&pFile);
(void)taosCloseFile(&pFile);
if (len < 0) {
return len;
}
}
if (len >= 36) {
@ -1037,7 +1054,7 @@ char *taosGetCmdlineByPID(int pid) {
return cmdline;
#else
static char cmdline[1024];
sprintf(cmdline, "/proc/%d/cmdline", pid);
(void)sprintf(cmdline, "/proc/%d/cmdline", pid);
// int fd = open(cmdline, O_RDONLY);
TdFilePtr pFile = taosOpenFile(cmdline, TD_FILE_READ);
@ -1049,7 +1066,7 @@ char *taosGetCmdlineByPID(int pid) {
cmdline[n] = 0;
taosCloseFile(&pFile);
(void)taosCloseFile(&pFile);
} else {
cmdline[0] = 0;
}
@ -1063,9 +1080,13 @@ int64_t taosGetOsUptime() {
#elif defined(_TD_DARWIN_64)
#else
struct sysinfo info;
if (0 == sysinfo(&info)) {
return (int64_t)info.uptime * 1000;
if (-1 == sysinfo(&info)) {
terrno = TAOS_SYSTEM_ERROR(errno);
return terrno;
}
return (int64_t)info.uptime * 1000;
#endif
return 0;
}
@ -1115,7 +1136,7 @@ void taosSetCoreDump(bool enable) {
int name[] = {CTL_KERN, KERN_CORE_USES_PID};
memset(&args, 0, sizeof(struct __sysctl_args));
(void)memset(&args, 0, sizeof(struct __sysctl_args));
args.name = name;
args.nlen = sizeof(name) / sizeof(name[0]);
args.oldval = &old_usespid;
@ -1135,7 +1156,7 @@ void taosSetCoreDump(bool enable) {
old_usespid = 0;
old_len = 0;
memset(&args, 0, sizeof(struct __sysctl_args));
(void)memset(&args, 0, sizeof(struct __sysctl_args));
args.name = name;
args.nlen = sizeof(name) / sizeof(name[0]);
args.oldval = &old_usespid;
@ -1198,6 +1219,8 @@ SysNameInfo taosGetSysNameInfo() {
tstrncpy(info.release, uts.release, sizeof(info.release));
tstrncpy(info.version, uts.version, sizeof(info.version));
tstrncpy(info.machine, uts.machine, sizeof(info.machine));
} else {
terrno = TAOS_SYSTEM_ERROR(errno);
}
return info;
@ -1262,6 +1285,11 @@ int taosGetlocalhostname(char *hostname, size_t maxLen) {
return 0;
}
#else
return gethostname(hostname, maxLen);
int r = gethostname(hostname, maxLen);
if (-1 == r) {
terrno = TAOS_SYSTEM_ERROR(errno);
return terrno;
}
return r;
#endif
}

View File

@ -162,8 +162,8 @@ int taosSetConsoleEcho(bool on) {
struct termios term;
if (tcgetattr(STDIN_FILENO, &term) == -1) {
/*perror("Cannot get the attribution of the terminal");*/
return -1;
terrno = TAOS_SYSTEM_ERROR(errno);
return terrno;
}
if (on)
@ -172,18 +172,18 @@ int taosSetConsoleEcho(bool on) {
term.c_lflag &= ~ECHOFLAGS;
err = tcsetattr(STDIN_FILENO, TCSAFLUSH, &term);
if (err == -1 || err == EINTR) {
/*printf("Cannot set the attribution of the terminal");*/
return -1;
if (err == -1) {
terrno = TAOS_SYSTEM_ERROR(errno);
return terrno;
}
return 0;
#endif
}
void taosSetTerminalMode() {
int32_t taosSetTerminalMode() {
#if defined(WINDOWS)
return 0;
#else
struct termios newtio;
@ -192,7 +192,7 @@ void taosSetTerminalMode() {
/* exit(EXIT_FAILURE); */
/* } */
memcpy(&newtio, &oldtio, sizeof(oldtio));
(void)memcpy(&newtio, &oldtio, sizeof(oldtio));
// Set new terminal attributes.
newtio.c_iflag &= ~(IXON | IXOFF | ICRNL | INLCR | IGNCR | IMAXBEL | ISTRIP);
@ -207,10 +207,13 @@ void taosSetTerminalMode() {
newtio.c_cc[VMIN] = 1;
newtio.c_cc[VTIME] = 0;
if (tcsetattr(0, TCSANOW, &newtio) != 0) {
fprintf(stderr, "Fail to set terminal properties!\n");
exit(EXIT_FAILURE);
if (-1 == tcsetattr(0, TCSANOW, &newtio)) {
terrno = TAOS_SYSTEM_ERROR(errno);
(void)fprintf(stderr, "Fail to set terminal properties!\n");
return terrno;
}
return 0;
#endif
}
@ -219,54 +222,75 @@ int32_t taosGetOldTerminalMode() {
#else
/* Make sure stdin is a terminal. */
if (!isatty(STDIN_FILENO)) {
return -1;
terrno = TAOS_SYSTEM_ERROR(errno);
return terrno;
}
// Get the parameter of current terminal
if (tcgetattr(0, &oldtio) != 0) {
return -1;
if (-1 == tcgetattr(0, &oldtio)) {
terrno = TAOS_SYSTEM_ERROR(errno);
return terrno;
}
return 1;
return 0;
#endif
}
void taosResetTerminalMode() {
int32_t taosResetTerminalMode() {
#if defined(WINDOWS)
#else
if (tcsetattr(0, TCSANOW, &oldtio) != 0) {
fprintf(stderr, "Fail to reset the terminal properties!\n");
exit(EXIT_FAILURE);
if (-1 == tcsetattr(0, TCSANOW, &oldtio)) {
terrno = TAOS_SYSTEM_ERROR(errno);
(void)fprintf(stderr, "Fail to reset the terminal properties!\n");
return terrno;
}
#endif
return 0;
}
TdCmdPtr taosOpenCmd(const char* cmd) {
if (cmd == NULL) return NULL;
if (cmd == NULL) {
terrno = TSDB_CODE_INVALID_PARA;
return NULL;
}
#ifdef WINDOWS
return (TdCmdPtr)_popen(cmd, "r");
#else
return (TdCmdPtr)popen(cmd, "r");
TdCmdPtr p = (TdCmdPtr)popen(cmd, "r");
if (NULL == p) {
terrno = TAOS_SYSTEM_ERROR(errno);
}
return p;
#endif
}
int64_t taosGetsCmd(TdCmdPtr pCmd, int32_t maxSize, char* __restrict buf) {
if (pCmd == NULL || buf == NULL) {
return -1;
terrno = TSDB_CODE_INVALID_PARA;
return terrno;
}
if (fgets(buf, maxSize, (FILE*)pCmd) == NULL) {
return -1;
if (feof((FILE*)pCmd)) {
return 0;
}
terrno = TAOS_SYSTEM_ERROR(ferror((FILE*)pCmd));
return terrno;
}
return strlen(buf);
}
int64_t taosGetLineCmd(TdCmdPtr pCmd, char** __restrict ptrBuf) {
if (pCmd == NULL || ptrBuf == NULL) {
return -1;
terrno = TSDB_CODE_INVALID_PARA;
return terrno;
}
if (*ptrBuf != NULL) {
taosMemoryFreeClear(*ptrBuf);
}
#ifdef WINDOWS
*ptrBuf = taosMemoryMalloc(1024);
if (*ptrBuf == NULL) return -1;
@ -277,8 +301,13 @@ int64_t taosGetLineCmd(TdCmdPtr pCmd, char** __restrict ptrBuf) {
(*ptrBuf)[1023] = 0;
return strlen(*ptrBuf);
#else
size_t len = 0;
return getline(ptrBuf, &len, (FILE*)pCmd);
int64_t len = 0;
len = getline(ptrBuf, &len, (FILE*)pCmd);
if (-1 == len) {
terrno = TAOS_SYSTEM_ERROR(errno);
return terrno;
}
return len;
#endif
}
@ -289,15 +318,14 @@ int32_t taosEOFCmd(TdCmdPtr pCmd) {
return feof((FILE*)pCmd);
}
int64_t taosCloseCmd(TdCmdPtr* ppCmd) {
void taosCloseCmd(TdCmdPtr* ppCmd) {
if (ppCmd == NULL || *ppCmd == NULL) {
return 0;
return;
}
#ifdef WINDOWS
_pclose((FILE*)(*ppCmd));
#else
pclose((FILE*)(*ppCmd));
(void)pclose((FILE*)(*ppCmd));
#endif
*ppCmd = NULL;
return 0;
}