From 1f3b7197b1c7e72b5ac23cef00d0f63355a57092 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Mon, 4 Oct 2021 21:41:11 +0800 Subject: [PATCH] remove some system include files --- include/os/osFile.h | 2 - include/os/osSocket.h | 46 ++++++------------ include/os/osSysinfo.h | 2 + source/libs/transport/src/rpcMain.c | 2 +- source/os/src/osRand.c | 1 + source/os/src/osSemaphore.c | 1 + source/os/src/osSleep.c | 2 + source/os/src/osSocket.c | 61 +++++++++++++++++++----- source/os/src/osSysinfo.c | 6 +++ source/os/src/osTimer.c | 2 + source/server/dnode/src/dnodeTelemetry.c | 2 +- source/util/src/tfile.c | 4 +- source/util/src/tnote.c | 10 ++-- 13 files changed, 88 insertions(+), 53 deletions(-) diff --git a/include/os/osFile.h b/include/os/osFile.h index 83f90d4ba2..8d03759d82 100644 --- a/include/os/osFile.h +++ b/include/os/osFile.h @@ -24,10 +24,8 @@ extern "C" { #if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) typedef int32_t FileFd; -typedef SOCKET SocketFd; #else typedef int32_t FileFd; -typedef int32_t SocketFd; #endif #define FD_INITIALIZER ((int32_t)-1) diff --git a/include/os/osSocket.h b/include/os/osSocket.h index c503e667e6..d76b286d65 100644 --- a/include/os/osSocket.h +++ b/include/os/osSocket.h @@ -27,49 +27,33 @@ extern "C" { #include #else #include - #include - #include - #include #include - #include #endif -#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) - #define taosSend(sockfd, buf, len, flags) send((SOCKET)sockfd, buf, len, flags) - #define taosSendto(sockfd, buf, len, flags, dest_addr, addrlen) sendto((SOCKET)sockfd, buf, len, flags, dest_addr, addrlen) - #define taosWriteSocket(fd, buf, len) send((SOCKET)fd, buf, len, 0) - #define taosReadSocket(fd, buf, len) recv((SOCKET)fd, buf, len, 0) - #define taosCloseSocketNoCheck(fd) closesocket((SOCKET)fd) - #define taosCloseSocket(fd) closesocket((SOCKET)fd) -#else - #define taosSend(sockfd, buf, len, flags) send(sockfd, buf, len, flags) - #define taosSendto(sockfd, buf, len, flags, dest_addr, addrlen) sendto(sockfd, buf, len, flags, dest_addr, addrlen) - #define taosReadSocket(fd, buf, len) read(fd, buf, len) - #define taosWriteSocket(fd, buf, len) write(fd, buf, len) - #define taosCloseSocketNoCheck(x) close(x) - #define taosCloseSocket(x) \ - { \ - if ((x) > -1) { \ - close(x); \ - x = FD_INITIALIZER; \ - } \ - } -#endif - -#define TAOS_EPOLL_WAIT_TIME 500 +#define TAOS_EPOLL_WAIT_TIME 500 typedef int32_t SOCKET; -typedef SOCKET EpollFd; +typedef SOCKET EpollFd; #define EpollClose(pollFd) taosCloseSocket(pollFd) -void taosShutDownSocketRD(SOCKET fd); -void taosShutDownSocketWR(SOCKET fd); +#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) +typedef SOCKET SocketFd; +#else +typedef int32_t SocketFd; +#endif +int32_t taosSendto(SocketFd fd, void * msg, int len, unsigned int flags, const struct sockaddr * to, int tolen); +int32_t taosWriteSocket(SocketFd fd, void *msg, int len); +int32_t taosReadSocket(SocketFd fd, void *msg, int len); +int32_t taosCloseSocketNoCheck(SocketFd fd); +int32_t taosCloseSocket(SocketFd fd); +void taosShutDownSocketRD(SOCKET fd); +void taosShutDownSocketWR(SOCKET fd); int32_t taosSetNonblocking(SOCKET sock, int32_t on); void taosIgnSIGPIPE(); void taosBlockSIGPIPE(); void taosSetMaskSIGPIPE(); int32_t taosSetSockOpt(SOCKET socketfd, int32_t level, int32_t optname, void *optval, int32_t optlen); -int32_t taosGetSockOpt(SOCKET socketfd, int32_t level, int32_t optname, void *optval, int32_t* optlen); +int32_t taosGetSockOpt(SOCKET socketfd, int32_t level, int32_t optname, void *optval, int32_t *optlen); uint32_t taosInetAddr(char *ipAddr); const char *taosInetNtoa(struct in_addr ipInt); diff --git a/include/os/osSysinfo.h b/include/os/osSysinfo.h index 688fb8f7ef..a3919890bd 100644 --- a/include/os/osSysinfo.h +++ b/include/os/osSysinfo.h @@ -66,6 +66,8 @@ typedef struct { SysNameInfo taosGetSysNameInfo(); +int64_t taosGetPid(); + #ifdef __cplusplus } #endif diff --git a/source/libs/transport/src/rpcMain.c b/source/libs/transport/src/rpcMain.c index fe58ee2746..800b146713 100644 --- a/source/libs/transport/src/rpcMain.c +++ b/source/libs/transport/src/rpcMain.c @@ -695,7 +695,7 @@ static SRpcConn *rpcAllocateClientConn(SRpcInfo *pRpc) { pConn->sid = sid; pConn->tranId = (uint16_t)(taosRand() & 0xFFFF); pConn->ownId = htonl(pConn->sid); - pConn->linkUid = (uint32_t)((int64_t)pConn + (int64_t)getpid() + (int64_t)pConn->tranId); + pConn->linkUid = (uint32_t)((int64_t)pConn + taosGetPid() + (int64_t)pConn->tranId); pConn->spi = pRpc->spi; pConn->encrypt = pRpc->encrypt; if (pConn->spi) memcpy(pConn->secret, pRpc->secret, TSDB_KEY_LEN); diff --git a/source/os/src/osRand.c b/source/os/src/osRand.c index 247097f9ce..973323a346 100644 --- a/source/os/src/osRand.c +++ b/source/os/src/osRand.c @@ -18,6 +18,7 @@ #if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) #else #include +#include #endif uint32_t taosRand(void) { return rand(); } diff --git a/source/os/src/osSemaphore.c b/source/os/src/osSemaphore.c index c9533c8b8a..2385d10285 100644 --- a/source/os/src/osSemaphore.c +++ b/source/os/src/osSemaphore.c @@ -376,6 +376,7 @@ int32_t taosGetCurrentAPPName(char *name, int32_t *len) { */ #include +#include bool taosCheckPthreadValid(pthread_t thread) { return thread != 0; } diff --git a/source/os/src/osSleep.c b/source/os/src/osSleep.c index 9c3231fd13..3b90fbdad8 100644 --- a/source/os/src/osSleep.c +++ b/source/os/src/osSleep.c @@ -22,6 +22,8 @@ void taosMsleep(int32_t ms) { Sleep(ms); } #else +#include + /* to make taosMsleep work, signal SIGALRM shall be blocked in the calling thread, diff --git a/source/os/src/osSocket.c b/source/os/src/osSocket.c index 0b62df4524..fb1aeebe1c 100644 --- a/source/os/src/osSocket.c +++ b/source/os/src/osSocket.c @@ -17,16 +17,57 @@ #include "os.h" #if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) + #include "winsock2.h" + #include + #include + #include #else -#include -#include -#include -#include -#include -#include -#include -#include -#include + #include + #include + #include + #include + #include + #include + #include + #include + #include +#endif + +#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) + +#define taosSend(sockfd, buf, len, flags) send((SOCKET)sockfd, buf, len, flags) +int32_t taosSendto(SocketFd fd, void *buf, int len, unsigned int flags, const struct sockaddr *to, int tolen) { + return sendto((SOCKET)sockfd, buf, len, flags, dest_addr, addrlen); +} +int32_t taosWriteSocket(SocketFd fd, void *buf, int len) { return send((SOCKET)fd, buf, len, 0); } +int32_t taosReadSocket(SocketFd fd, void *buf, int len) { return recv((SOCKET)fd, buf, len, 0)(); } +int32_t taosCloseSocketNoCheck(SocketFd fd) { return closesocket((SOCKET)fd); } +int32_t taosCloseSocket(SocketFd fd) { closesocket((SOCKET)fd) } + +#else + + #define taosSend(sockfd, buf, len, flags) send(sockfd, buf, len, flags) + int32_t taosSendto(SocketFd fd, void * buf, int len, unsigned int flags, const struct sockaddr * dest_addr, int addrlen) { + return sendto(fd, buf, len, flags, dest_addr, addrlen); + } + + int32_t taosWriteSocket(SocketFd fd, void *buf, int len) { + return write(fd, buf, len); + } + + int32_t taosReadSocket(SocketFd fd, void *buf, int len) { + return read(fd, buf, len); + } + + int32_t taosCloseSocketNoCheck(SocketFd fd) { + return close(fd); + } + + int32_t taosCloseSocket(SocketFd fd) { + if (fd > -1) { + close(fd); + } + } #endif void taosShutDownSocketRD(SOCKET fd) { @@ -226,8 +267,6 @@ uint64_t htonll(uint64_t val) { return (((uint64_t)htonl(val)) << 32) + htonl(va #endif - - #ifndef SIGPIPE #define SIGPIPE EPIPE #endif diff --git a/source/os/src/osSysinfo.c b/source/os/src/osSysinfo.c index 54af2ab4ad..0344507f5e 100644 --- a/source/os/src/osSysinfo.c +++ b/source/os/src/osSysinfo.c @@ -485,6 +485,7 @@ char *taosGetCmdlineByPID(int pid) { #include #include #include +#include #define PROCESS_ITEM 12 @@ -1127,4 +1128,9 @@ SysNameInfo taosGetSysNameInfo() { return info; } + +int64_t taosGetPid() { + getpid(); +} + #endif \ No newline at end of file diff --git a/source/os/src/osTimer.c b/source/os/src/osTimer.c index 2f18d76db9..b1bf1bcd2d 100644 --- a/source/os/src/osTimer.c +++ b/source/os/src/osTimer.c @@ -62,6 +62,7 @@ void taosUninitTimer() { #include #include +#include static void (*timer_callback)(int); static int timer_ms = 0; @@ -136,6 +137,7 @@ void taos_block_sigalrm(void) { */ #include +#include static void taosDeleteTimer(void *tharg) { timer_t *pTimer = tharg; diff --git a/source/server/dnode/src/dnodeTelemetry.c b/source/server/dnode/src/dnodeTelemetry.c index 089a614590..5d1d9b4aa4 100644 --- a/source/server/dnode/src/dnodeTelemetry.c +++ b/source/server/dnode/src/dnodeTelemetry.c @@ -210,7 +210,7 @@ static void dnodeSendTelemetryReport(DnTelem* telem) { "Content-Type: application/json\n" "Content-Length: "; - taosWriteSocket(fd, header, (int32_t)strlen(header)); + taosWriteSocket(fd, (void*)header, (int32_t)strlen(header)); int32_t contLen = (int32_t)(tbufTell(&bw) - 1); sprintf(buf, "%d\n\n", contLen); taosWriteSocket(fd, buf, (int32_t)strlen(buf)); diff --git a/source/util/src/tfile.c b/source/util/src/tfile.c index 2dafd689d3..5b61186b12 100644 --- a/source/util/src/tfile.c +++ b/source/util/src/tfile.c @@ -23,7 +23,7 @@ static int32_t tsFileRsetId = -1; static void tfCloseFile(void *p) { - close((int32_t)(uintptr_t)p); + taosCloseFile((int32_t)(uintptr_t)p); } int32_t tfInit() { @@ -48,7 +48,7 @@ static int64_t tfOpenImp(int32_t fd) { void * p = (void *)(int64_t)fd; int64_t rid = taosAddRef(tsFileRsetId, p); - if (rid < 0) close(fd); + if (rid < 0) taosCloseFile(fd); return rid; } diff --git a/source/util/src/tnote.c b/source/util/src/tnote.c index e4a326ba8f..50b1e0002d 100644 --- a/source/util/src/tnote.c +++ b/source/util/src/tnote.c @@ -100,7 +100,7 @@ static void *taosThreadToOpenNewNote(void *param) { } taosLockNote(fd, pNote); - (void)lseek(fd, 0, SEEK_SET); + (void)taosLSeekFile(fd, 0, SEEK_SET); int32_t oldFd = pNote->fd; pNote->fd = fd; @@ -142,10 +142,10 @@ static bool taosCheckNoteIsOpen(char *noteName, SNoteObj *pNote) { if (taosLockNote(fd, pNote)) { taosUnLockNote(fd, pNote); - close(fd); + taosCloseFile(fd); return false; } else { - close(fd); + taosCloseFile(fd); return true; } } @@ -226,7 +226,7 @@ static int32_t taosOpenNoteWithMaxLines(char *fn, int32_t maxLines, int32_t maxN size = (int32_t)filestat_size; pNote->lines = size / 60; - lseek(pNote->fd, 0, SEEK_END); + taosLSeekFile(pNote->fd, 0, SEEK_END); return 0; } @@ -271,6 +271,6 @@ void taosNotePrint(SNoteObj *pNote, const char *const format, ...) { static void taosCloseNoteByFd(int32_t fd, SNoteObj *pNote) { if (fd >= 0) { taosUnLockNote(fd, pNote); - close(fd); + taosCloseFile(fd); } }