Merge pull request #28697 from taosdata/fix/liaohj

feat: kill rsync by pid.
This commit is contained in:
Shengliang Guan 2024-11-11 10:22:41 +08:00 committed by GitHub
commit 94e5bbdd6a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 80 additions and 3 deletions

View File

@ -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

View File

@ -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) {

View File

@ -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;

View File

@ -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
}