From 9c5c9f62addf9f8ac4e5ac80e2254d4894ba0f43 Mon Sep 17 00:00:00 2001 From: Haojun Liao Date: Thu, 7 Nov 2024 19:48:32 +0800 Subject: [PATCH] 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()); +}