添加枚举进程及模块函数

This commit is contained in:
gb 2023-02-08 14:15:50 +08:00
parent 9be3ce881d
commit af5c96ec2e
2 changed files with 122 additions and 82 deletions

View File

@ -159,7 +159,7 @@ void chronograph::reset()
namespace sys_util
{
static bool find_module(const char* path, void* param)
static bool find_module(const char* path, bool is_dir, void* param)
{
std::string* para = (std::string*)param;
@ -184,8 +184,40 @@ namespace sys_util
return true;
}
}
typedef struct _enum_proc_cb
{
bool(*on_found)(uint64_t pid, const char* path_name, void* param);
void* param;
}ENPROCCB, * LPENPROCCB;
static bool found_process(const char* path, bool is_dir, void* param)
{
LPENPROCCB cb = (LPENPROCCB)param;
const char* id = strstr(path + 2, "/");
uint64_t pid = 0;
std::string path_name("");
int32_t enum_modules(bool(*on_found)(const char* path_module_name, void* param),// return false to stop enumeratin
if (id++ == nullptr)
{
id = path;
}
while (*id)
{
if (*id < '0' || *id > '9')
break;
pid *= 10;
pid += *id - '0';
id++;
}
if (*id)
return true;
path_name = get_module_path(nullptr, pid);
return cb->on_found(pid, path_name.c_str(), cb->param);
}
int32_t enum_modules(bool(*on_found)(const char* path_module_name, bool is_dir, void* param),// return false to stop enumeratin
void* param, // user defined data, passed into callback on_found
unsigned pid // process id, -1 is self
) // return errno
@ -200,7 +232,7 @@ namespace sys_util
}
int32_t enum_files(const char* dir, // dir path
bool(*on_found)(const char* path_name, void* param), // return false to stop enumeratin
bool(*on_found)(const char* path_name, bool is_dir, void* param), // return false to stop enumeratin
void* param // user defined data, passed into callback on_found
, bool recursive
) // return errno
@ -215,11 +247,22 @@ namespace sys_util
while ((ent = readdir(pdir)))
{
if (ent->d_type & DT_DIR)
if (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0)
continue;
std::string file(dir);
file += "/";
file += ent->d_name;
if ((ent->d_type & DT_DIR) == 0)
file = read_link(file.c_str());
if (!on_found(file.c_str(), ent->d_type & DT_DIR, param))
{
if (recursive)
{
if (strcmp(ent->d_name, ".") && strcmp(ent->d_name, ".."))
ret = 0x5e17;
break;
}
if ((ent->d_type & DT_DIR) && recursive)
{
std::string sub(dir);
sub += "/";
@ -229,25 +272,21 @@ namespace sys_util
break;
}
}
}
else
{
std::string file(dir);
file += "/";
file += ent->d_name;
if (!on_found(read_link(file.c_str()).c_str(), param))
{
ret = 0x5e17;
break;
}
}
}
closedir(pdir);
return ret == 0x5e17 ? 0 : ret;
}
int32_t enum_processes(bool(*on_found)(uint64_t pid, const char* path_name, void* param), void* param)
{
ENPROCCB cb;
cb.on_found = on_found;
cb.param = param;
return enum_files("/proc", found_process, &cb, false);
}
std::string get_module_path(const char* module_name, unsigned pid) // get module full path, nullptr is for main-exe
{
std::string param[] = { module_name ? module_name : "", "" };
@ -301,34 +340,34 @@ namespace sys_util
int32_t get_memory_info(uint64_t* total, uint64_t* available)
{
if(!total && !available)
if (!total && !available)
return 0;
char line[128] = {0};
FILE *src = fopen("/proc/meminfo", "rb");
char line[128] = { 0 };
FILE* src = fopen("/proc/meminfo", "rb");
int32_t count = total && available ? 2 : 1;
unsigned long val = 0;
if(!src)
if (!src)
return log_cls::log_when_err(-1, "fopen('/proc/meminfo', 'rb')", LOG_LEVEL_FATAL);
while(fgets(line, sizeof(line) - 1, src))
while (fgets(line, sizeof(line) - 1, src))
{
if(sscanf(line, "MemTotal: %ld", &val))
if (sscanf(line, "MemTotal: %ld", &val))
{
if(total)
if (total)
{
*total = val * 1024;
if(--count == 0)
if (--count == 0)
break;
}
}
else if(sscanf(line, "MemFree: %ld", &val))
else if (sscanf(line, "MemFree: %ld", &val))
{
if(available)
if (available)
{
*available = val * 1024;
if(--count == 0)
if (--count == 0)
break;
}
}
@ -342,25 +381,25 @@ namespace sys_util
{
std::string str("\0", 80);
if(bytes >= SIZE_GB(1))
if (bytes >= SIZE_GB(1))
{
double v = bytes * 1.0f / (SIZE_GB(1));
size_t pos = 0;
sprintf(&str[0], "%.2fGB", v);
pos = str.find(".");
while(pos > 3)
while (pos > 3)
{
pos -= 3;
str.insert(pos, ",");
}
}
else if(bytes >= SIZE_MB(1))
else if (bytes >= SIZE_MB(1))
{
double v = bytes * 1.0f / (SIZE_MB(1));
sprintf(&str[0], "%.2fMB", v);
}
else if(bytes >= SIZE_KB(1))
else if (bytes >= SIZE_KB(1))
{
double v = bytes * 1.0f / (SIZE_KB(1));
sprintf(&str[0], "%.2fKB", v);

View File

@ -112,16 +112,17 @@ public:
#include <string>
namespace sys_util
{
int32_t enum_modules(bool(*on_found)(const char* path_module_name, void* param),// return false to stop enumeratin
int32_t enum_modules(bool(*on_found)(const char* path_module_name, bool is_dir, void* param),// return false to stop enumeratin
void* param, // user defined data, passed into callback on_found
unsigned pid = -1 // process id, -1 is self
); // return errno
int32_t enum_files(const char* dir, // dir path
bool(*on_found)(const char* path_name, void* param), // return false to stop enumeratin
void* param // user defined data, passed into callback on_found
int32_t enum_files(const char* dir // dir path
, bool(*on_found)(const char* path_name, bool is_dir, void* param)// return false to stop enumeratin
, void* param // user defined data, passed into callback on_found
, bool recursive = true // walk recursive
); // return errno
int32_t enum_processes(bool(*on_found)(uint64_t pid, const char* path_name, void* param), void* param);
std::string get_module_path(const char* module_name = nullptr
, unsigned pid = -1); // get module full path, nullptr is for main-exe
@ -144,16 +145,16 @@ namespace sys_util
int32_t get_inf_file_data(const char* file, size_t line_max, const char* fmt, Args ... args)
{
std::string buf("\0", line_max + 8);
FILE *src = fopen(file, "rb");
FILE* src = fopen(file, "rb");
int32_t count = 0;
if(!src)
if (!src)
return 0;
while(fgets(&buf[0], line_max, src))
while (fgets(&buf[0], line_max, src))
{
count = sscanf(&buf[0], fmt, args ...);
if(count > 0)
if (count > 0)
break;
}
fclose(src);