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..8b453f7d82 100644 --- a/source/common/src/rsync.c +++ b/source/common/src/rsync.c @@ -119,11 +119,21 @@ 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) { + uInfo("kill rsync program pid:%d", pid); + code = system(buf); + } + } #endif if (code != 0) { diff --git a/source/os/src/osSemaphore.c b/source/os/src/osSemaphore.c index 538d5bf63e..e61fd627eb 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,59 @@ 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) { + TAOS_UNUSED(fclose(fp)); + continue; + } + + ret = sscanf(buf, "%*s %s", bufx); + if (!strcmp(bufx, name)) { + char* end = NULL; + *pPId = taosStr2Int32(ptr->d_name, &end, 10); + } + TAOS_UNUSED(fclose(fp)); + } + } + + TAOS_UNUSED(closedir(dir)); + + 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) { 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..1e412f5149 100644 --- a/source/os/test/osSemaphoreTests.cpp +++ b/source/os/test/osSemaphoreTests.cpp @@ -245,3 +245,12 @@ TEST(osSemaphoreTests, Performance4_2) { (void)tsem2_destroy(&sem); } } + +TEST(osSemaphoreTests, GetPID) { +#ifdef LINUX + pid_t pid = 0; + int32_t ret = taosGetPIdByName("osSemaphoreTest", &pid); + EXPECT_EQ(ret, 0); + EXPECT_EQ(pid, taosGetPId()); +#endif +}