From 9c5c9f62addf9f8ac4e5ac80e2254d4894ba0f43 Mon Sep 17 00:00:00 2001 From: Haojun Liao Date: Thu, 7 Nov 2024 19:48:32 +0800 Subject: [PATCH 1/7] feat: kill rsync by pid. --- include/os/osSemaphore.h | 1 + source/common/src/rsync.c | 15 +++++++-- source/os/src/osSemaphore.c | 51 +++++++++++++++++++++++++++++ source/os/test/osSemaphoreTests.cpp | 7 ++++ 4 files changed, 71 insertions(+), 3 deletions(-) diff --git a/include/os/osSemaphore.h b/include/os/osSemaphore.h index d893f42740..48836eeb0c 100644 --- a/include/os/osSemaphore.h +++ b/include/os/osSemaphore.h @@ -104,6 +104,7 @@ int64_t taosGetPthreadId(TdThread thread); void taosResetPthread(TdThread *thread); bool taosComparePthread(TdThread first, TdThread second); int32_t taosGetPId(); +int32_t taosGetPIdByName(const char* name, int32_t* pPId); int32_t taosGetAppName(char *name, int32_t *len); #ifdef __cplusplus diff --git a/source/common/src/rsync.c b/source/common/src/rsync.c index b7352acf25..ecf7fc21a6 100644 --- a/source/common/src/rsync.c +++ b/source/common/src/rsync.c @@ -119,11 +119,20 @@ static int32_t execCommand(char* command) { } void stopRsync() { - int32_t code = + int32_t pid = 0; + int32_t code = 0; + char buf[128] = {0}; + #ifdef WINDOWS - system("taskkill /f /im rsync.exe"); + code = system("taskkill /f /im rsync.exe"); #else - system("pkill rsync"); + code = taosGetPIdByName("rsync", &pid); + if (code == 0) { + int32_t ret = tsnprintf(buf, tListLen(buf), "kill -9 %d", pid); + if (ret > 0) { + system(buf); + } + } #endif if (code != 0) { diff --git a/source/os/src/osSemaphore.c b/source/os/src/osSemaphore.c index 538d5bf63e..ff3d7e2976 100644 --- a/source/os/src/osSemaphore.c +++ b/source/os/src/osSemaphore.c @@ -72,6 +72,8 @@ int32_t taosGetAppName(char* name, int32_t* len) { return 0; } +int32_t taosGetPIdByName(const char* name, int32_t* pPId) { return -1;} + int32_t tsem_wait(tsem_t* sem) { DWORD ret = WaitForSingleObject(*sem, INFINITE); if (ret == WAIT_OBJECT_0) { @@ -173,6 +175,8 @@ int32_t taosGetAppName(char *name, int32_t *len) { return 0; } +int32_t taosGetPIdByName(const char* name, int32_t* pPId) {return -1;} + #else /* @@ -228,6 +232,53 @@ int32_t taosGetAppName(char* name, int32_t* len) { return 0; } +int32_t taosGetPIdByName(const char* name, int32_t* pPId) { + DIR* dir = NULL; + struct dirent* ptr = NULL; + FILE* fp = NULL; + char filepath[512]; + char bufx[50]; + char buf[1024] = {0}; + + *pPId = -1; + dir = opendir("/proc"); + if (dir == NULL) { + return TAOS_SYSTEM_ERROR(errno); + } + + while ((ptr = readdir(dir)) != NULL) { + if ((strcmp(ptr->d_name, ".") == 0) || (strcmp(ptr->d_name, "..") == 0)) { + continue; + } + + if (DT_DIR != ptr->d_type) { + continue; + } + + int32_t ret = tsnprintf(filepath, tListLen(filepath), "/proc/%s/status", ptr->d_name); + if (ret == -1) { + continue; + } + + fp = fopen(filepath, "r"); + if (NULL != fp) { + if (fgets(buf, tListLen(buf) - 1, fp) == NULL) { + fclose(fp); + continue; + } + + sscanf(buf, "%*s %s", bufx); + if (!strcmp(bufx, name)) { + char* end = NULL; + *pPId = taosStr2Int32(ptr->d_name, &end, 10); + } + fclose(fp); + } + } + + return 0; +} + int32_t tsem_init(tsem_t* psem, int flags, unsigned int count) { if (sem_init(psem, flags, count) == 0) { return 0; diff --git a/source/os/test/osSemaphoreTests.cpp b/source/os/test/osSemaphoreTests.cpp index 1576d2845d..b4b07f8572 100644 --- a/source/os/test/osSemaphoreTests.cpp +++ b/source/os/test/osSemaphoreTests.cpp @@ -245,3 +245,10 @@ TEST(osSemaphoreTests, Performance4_2) { (void)tsem2_destroy(&sem); } } + +TEST(osSemaphoreTests, GetPID) { + pid_t pid = 0; + int32_t ret = taosGetPIdByName("osSemaphoreTest", &pid); + EXPECT_EQ(ret, 0); + EXPECT_EQ(pid, taosGetPId()); +} From c312896d73f28c00ff2d86ef4371dfe2a1d2391c Mon Sep 17 00:00:00 2001 From: Haojun Liao Date: Fri, 8 Nov 2024 10:31:48 +0800 Subject: [PATCH 2/7] feat: set correct rsp code. --- source/common/src/rsync.c | 1 + source/os/src/osSemaphore.c | 6 +++++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/source/common/src/rsync.c b/source/common/src/rsync.c index ecf7fc21a6..15c296d730 100644 --- a/source/common/src/rsync.c +++ b/source/common/src/rsync.c @@ -130,6 +130,7 @@ void stopRsync() { if (code == 0) { int32_t ret = tsnprintf(buf, tListLen(buf), "kill -9 %d", pid); if (ret > 0) { + uInfo("kill rsync program pid:%d", pid); system(buf); } } diff --git a/source/os/src/osSemaphore.c b/source/os/src/osSemaphore.c index ff3d7e2976..58c832af89 100644 --- a/source/os/src/osSemaphore.c +++ b/source/os/src/osSemaphore.c @@ -276,7 +276,11 @@ int32_t taosGetPIdByName(const char* name, int32_t* pPId) { } } - return 0; + if ((*pPId) == -1) { + return TAOS_SYSTEM_ERROR(ESRCH); + } else { + return TSDB_CODE_SUCCESS; + } } int32_t tsem_init(tsem_t* psem, int flags, unsigned int count) { From d8098ab3e145bb54b20ac72ea53d16b565d46dff Mon Sep 17 00:00:00 2001 From: Haojun Liao Date: Fri, 8 Nov 2024 12:48:00 +0800 Subject: [PATCH 3/7] fix(util): check return value. --- source/os/src/osSemaphore.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/source/os/src/osSemaphore.c b/source/os/src/osSemaphore.c index 58c832af89..1b9e8d3273 100644 --- a/source/os/src/osSemaphore.c +++ b/source/os/src/osSemaphore.c @@ -263,7 +263,7 @@ int32_t taosGetPIdByName(const char* name, int32_t* pPId) { fp = fopen(filepath, "r"); if (NULL != fp) { if (fgets(buf, tListLen(buf) - 1, fp) == NULL) { - fclose(fp); + TAOS_UNUSED(fclose(fp)); continue; } @@ -272,7 +272,7 @@ int32_t taosGetPIdByName(const char* name, int32_t* pPId) { char* end = NULL; *pPId = taosStr2Int32(ptr->d_name, &end, 10); } - fclose(fp); + TAOS_UNUSED(fclose(fp)); } } From 3559188dfa007c2a1679d0d12fcad630b61b9d58 Mon Sep 17 00:00:00 2001 From: Haojun Liao Date: Fri, 8 Nov 2024 14:23:37 +0800 Subject: [PATCH 4/7] fix(stream): check return value. --- source/common/src/rsync.c | 2 +- source/os/src/osSemaphore.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/source/common/src/rsync.c b/source/common/src/rsync.c index 15c296d730..8b453f7d82 100644 --- a/source/common/src/rsync.c +++ b/source/common/src/rsync.c @@ -131,7 +131,7 @@ void stopRsync() { int32_t ret = tsnprintf(buf, tListLen(buf), "kill -9 %d", pid); if (ret > 0) { uInfo("kill rsync program pid:%d", pid); - system(buf); + code = system(buf); } } #endif diff --git a/source/os/src/osSemaphore.c b/source/os/src/osSemaphore.c index 1b9e8d3273..e37182ce16 100644 --- a/source/os/src/osSemaphore.c +++ b/source/os/src/osSemaphore.c @@ -267,7 +267,7 @@ int32_t taosGetPIdByName(const char* name, int32_t* pPId) { continue; } - sscanf(buf, "%*s %s", bufx); + ret = sscanf(buf, "%*s %s", bufx); if (!strcmp(bufx, name)) { char* end = NULL; *pPId = taosStr2Int32(ptr->d_name, &end, 10); From 9ff749be73ea57811473544766e3b3eefac5d057 Mon Sep 17 00:00:00 2001 From: Haojun Liao Date: Sun, 10 Nov 2024 10:12:21 +0800 Subject: [PATCH 5/7] fix:disable unit test for windows/mac --- source/os/test/osSemaphoreTests.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/source/os/test/osSemaphoreTests.cpp b/source/os/test/osSemaphoreTests.cpp index b4b07f8572..1e412f5149 100644 --- a/source/os/test/osSemaphoreTests.cpp +++ b/source/os/test/osSemaphoreTests.cpp @@ -247,8 +247,10 @@ TEST(osSemaphoreTests, Performance4_2) { } TEST(osSemaphoreTests, GetPID) { +#ifdef LINUX pid_t pid = 0; int32_t ret = taosGetPIdByName("osSemaphoreTest", &pid); EXPECT_EQ(ret, 0); EXPECT_EQ(pid, taosGetPId()); +#endif } From 4b50edccf0ddc601c32a0da43453af17ee068ac6 Mon Sep 17 00:00:00 2001 From: Haojun Liao Date: Sun, 10 Nov 2024 10:49:31 +0800 Subject: [PATCH 6/7] fix: fix memory leak. --- source/os/src/osSemaphore.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/source/os/src/osSemaphore.c b/source/os/src/osSemaphore.c index e37182ce16..708c18aa3f 100644 --- a/source/os/src/osSemaphore.c +++ b/source/os/src/osSemaphore.c @@ -276,6 +276,8 @@ int32_t taosGetPIdByName(const char* name, int32_t* pPId) { } } + closedir(dir); + if ((*pPId) == -1) { return TAOS_SYSTEM_ERROR(ESRCH); } else { From 006900408a36279ae4a054f000444d56022426e7 Mon Sep 17 00:00:00 2001 From: Haojun Liao Date: Sun, 10 Nov 2024 12:23:01 +0800 Subject: [PATCH 7/7] fix: check return value. --- source/os/src/osSemaphore.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/os/src/osSemaphore.c b/source/os/src/osSemaphore.c index 708c18aa3f..e61fd627eb 100644 --- a/source/os/src/osSemaphore.c +++ b/source/os/src/osSemaphore.c @@ -276,7 +276,7 @@ int32_t taosGetPIdByName(const char* name, int32_t* pPId) { } } - closedir(dir); + TAOS_UNUSED(closedir(dir)); if ((*pPId) == -1) { return TAOS_SYSTEM_ERROR(ESRCH);