修改通过PID获取进程名称方法:系统调用ps -A | grep "pid "实现
This commit is contained in:
parent
7e8895ce26
commit
53b08a8f3e
|
@ -303,28 +303,86 @@ void shared_memory::release_buf(void* buf)
|
|||
}
|
||||
|
||||
#if !defined(WIN32) && !defined(_WIN64)
|
||||
std::string shared_memory::get_proc_name_by_pid(pid_t pid)
|
||||
std::string get_command_result(const char* cmd, int len, int* err)
|
||||
{
|
||||
char path[512] = { 0 };
|
||||
unsigned int* v = (unsigned int*)&pid;
|
||||
std::string ret("");
|
||||
std::string result("");
|
||||
FILE* src = popen(cmd, "r");
|
||||
|
||||
if (sizeof(pid) > 4 && v[1])
|
||||
sprintf(path, "/proc/%lld/status", pid);
|
||||
else
|
||||
sprintf(path, "/proc/%u/status", pid);
|
||||
|
||||
FILE* src = fopen(path, "rb");
|
||||
if (err)
|
||||
*err = 0;
|
||||
if (src)
|
||||
{
|
||||
char val[512] = { 0 };
|
||||
char buf[128] = { 0 };
|
||||
int rv = fread(buf, 1, sizeof(buf) - 1, src);
|
||||
|
||||
memset(path, 0, sizeof(path));
|
||||
fgets(path, sizeof(path) - 1, src);
|
||||
fclose(src);
|
||||
sscanf(path, "%*s %s", val);
|
||||
ret = val;
|
||||
while (rv > 0)
|
||||
{
|
||||
buf[rv] = 0;
|
||||
result += buf;
|
||||
if (len != -1 && result.length() >= len)
|
||||
{
|
||||
result.erase(len);
|
||||
break;
|
||||
}
|
||||
rv = fread(buf, 1, sizeof(buf) - 1, src);
|
||||
}
|
||||
if (rv == -1 && err)
|
||||
{
|
||||
*err = errno;
|
||||
VLOG_MINI_3(LOG_LEVEL_DEBUG_INFO, "Failed to excute shell command '%s' in read pipe: %d - %s\n", cmd, errno, strerror(errno));
|
||||
}
|
||||
pclose(src);
|
||||
}
|
||||
else if (err)
|
||||
{
|
||||
*err = errno;
|
||||
VLOG_MINI_3(LOG_LEVEL_DEBUG_INFO, "Failed to excute shell command '%s' in open pipe: %d - %s\n", cmd, errno, strerror(errno));
|
||||
}
|
||||
|
||||
return std::move(result);
|
||||
}
|
||||
|
||||
std::string shared_memory::get_proc_name_by_pid(pid_t pid)
|
||||
{
|
||||
std::string ret("");
|
||||
unsigned int* v = (unsigned int*)&pid;
|
||||
|
||||
{
|
||||
char cmd[80] = { 0 };
|
||||
int err = 0;
|
||||
std::string result("");
|
||||
size_t pos = 0;
|
||||
|
||||
sprintf(cmd, "ps -A | grep \"%lld \"", pid);
|
||||
result = get_command_result(cmd, -1, &err);
|
||||
if (result.length())
|
||||
{
|
||||
VLOG_MINI_2(LOG_LEVEL_DEBUG_INFO, "Command result of '%s' is: %s\n", cmd, result.c_str());
|
||||
pos = result.rfind(' ');
|
||||
if (pos++ != std::string::npos)
|
||||
{
|
||||
ret = result.substr(pos);
|
||||
}
|
||||
}
|
||||
}
|
||||
//char path[512] = { 0 };
|
||||
|
||||
//if (sizeof(pid) > 4 && v[1])
|
||||
// sprintf(path, "/proc/%lld/status", pid);
|
||||
//else
|
||||
// sprintf(path, "/proc/%u/status", pid);
|
||||
|
||||
//FILE* src = fopen(path, "rb");
|
||||
//if (src)
|
||||
//{
|
||||
// char val[512] = { 0 };
|
||||
|
||||
// memset(path, 0, sizeof(path));
|
||||
// fgets(path, sizeof(path) - 1, src);
|
||||
// fclose(src);
|
||||
// sscanf(path, "%*s %s", val);
|
||||
// ret = val;
|
||||
//}
|
||||
if (sizeof(pid) > 4 && v[1])
|
||||
{
|
||||
VLOG_MINI_2(LOG_LEVEL_DEBUG_INFO, "PID(%lld) name is: %s\n", pid, ret.c_str());
|
||||
|
|
Loading…
Reference in New Issue