Process.hspa
// // Created by wenlongbai on 8/14/17. // #ifndef BWL_PROCESS_H #define BWL_PROCESS_H #ifdef __cplusplus extern "C" { #endif // 驗證是否含有指定的進程 int checkProcess(char* processName); #ifdef __cplusplus }; #endif #endif //BWL_PROCESS_H
Process.cppcode
// // Created by wenlongbai on 8/14/17. // #include "Process.h" #include "Util.h" #include <stdio.h> #include <stdlib.h> #include <unistd.h> bool GetModuleBase(long long &ulModBase, pid_t pid, const char *pszModName) { bool bRet = false; FILE *fp = NULL; char szMapFilePath[32] = {0}; char szMapFileLine[1024] = {0}; if (pszModName == NULL) { return bRet; } if (pid < 0) { sprintf(szMapFilePath, "/proc/self/maps"); } else { sprintf(szMapFilePath, "/proc/%d/maps", pid); } fp = fopen(szMapFilePath, "r"); if (fp != NULL) { while (fgets(szMapFileLine, sizeof(szMapFileLine), fp) != NULL) { if (strstr(szMapFileLine, pszModName)) { char *pszModAddrStart = strtok(szMapFileLine, "-"); if (pszModAddrStart) { ulModBase = strtoul(pszModAddrStart, NULL, 16); if (ulModBase == 0x8000) ulModBase = 0; bRet = true; break; } } } fclose(fp); } return bRet; } bool GetModuleFullName(pid_t pid, const char *pszModName, char *pszFullModName, int nBuffSize) { bool bRet = false; FILE *fp = NULL; char szMapFilePath[32] = {0}; char szMapFileLine[1024] = {0}; char *pszFullName = NULL; if (pszModName == NULL || pszFullModName == NULL || nBuffSize <= 0) { return bRet; } if (pid < 0) { sprintf(szMapFilePath, "/proc/self/maps"); } else { sprintf(szMapFilePath, "/proc/%d/maps", pid); } fp = fopen(szMapFilePath, "r"); if (fp != NULL) { while (!bRet && fgets(szMapFileLine, sizeof(szMapFileLine), fp) != NULL) { if (strstr(szMapFileLine, pszModName)) { if (szMapFileLine[strlen(szMapFileLine) - 1] == '\n') { szMapFileLine[strlen(szMapFileLine) - 1] = 0; } pszFullName = strchr(szMapFileLine, '/'); if (pszFullName == NULL) { continue; } strncpy(pszFullModName, pszFullName, nBuffSize - 1); LOGD("pszFullName = %s", pszFullName); bRet = true; } } fclose(fp); } return bRet; } int checkProcess(char *processName) { LOGI("check: start"); long long ulCModBase = 0; if (GetModuleBase(ulCModBase, getpid(), processName)) { LOGI("check: finish"); return 1; } LOGI("check: finish"); return 0; }