添加枚举进程及模块函数

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

@ -8,11 +8,11 @@
// refer // refer
refer::refer() : ref_(1) refer::refer() : ref_(1)
{ {
on_born(); on_born();
} }
refer::~refer() refer::~refer()
{ {
on_dead(); on_dead();
} }
void refer::on_born(void) void refer::on_born(void)
@ -22,23 +22,23 @@ void refer::on_dead(void)
int32_t refer::add_ref(void) int32_t refer::add_ref(void)
{ {
LOCKER lock(mutex_); LOCKER lock(mutex_);
return ++ref_; return ++ref_;
} }
int32_t refer::release(void) int32_t refer::release(void)
{ {
int32_t ref = 0; int32_t ref = 0;
{ {
LOCKER lock(mutex_); LOCKER lock(mutex_);
ref = --ref_; ref = --ref_;
} }
if (ref == 0) if (ref == 0)
delete this; delete this;
return ref; return ref;
} }
@ -159,7 +159,7 @@ void chronograph::reset()
namespace sys_util 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; std::string* para = (std::string*)param;
@ -184,12 +184,44 @@ namespace sys_util
return true; 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)
void* param, // user defined data, passed into callback on_found {
unsigned pid // process id, -1 is self id = path;
) // return errno }
{ 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
{
char path[128] = { 0 }; char path[128] = { 0 };
if (pid == -1) if (pid == -1)
@ -197,14 +229,14 @@ namespace sys_util
sprintf(path, "/proc/%u/map_files/", pid); sprintf(path, "/proc/%u/map_files/", pid);
return enum_files(path, on_found, param, false); return enum_files(path, on_found, param, false);
} }
int32_t enum_files(const char* dir, // dir path 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 void* param // user defined data, passed into callback on_found
, bool recursive , bool recursive
) // return errno ) // return errno
{ {
int32_t ret = 0; int32_t ret = 0;
DIR* pdir = nullptr; DIR* pdir = nullptr;
struct dirent* ent = nullptr; struct dirent* ent = nullptr;
@ -215,32 +247,29 @@ namespace sys_util
while ((ent = readdir(pdir))) while ((ent = readdir(pdir)))
{ {
if (ent->d_type & DT_DIR) if (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0)
{ continue;
if (recursive)
{
if (strcmp(ent->d_name, ".") && strcmp(ent->d_name, ".."))
{
std::string sub(dir);
sub += "/";
sub += ent->d_name;
ret = enum_files(sub.c_str(), on_found, param, recursive);
if (ret == 0x5e17)
break;
}
}
}
else
{
std::string file(dir);
file += "/"; std::string file(dir);
file += ent->d_name;
if (!on_found(read_link(file.c_str()).c_str(), param)) file += "/";
{ file += ent->d_name;
ret = 0x5e17; 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))
{
ret = 0x5e17;
break;
}
if ((ent->d_type & DT_DIR) && recursive)
{
std::string sub(dir);
sub += "/";
sub += ent->d_name;
ret = enum_files(sub.c_str(), on_found, param, recursive);
if (ret == 0x5e17)
break; break;
}
} }
} }
closedir(pdir); closedir(pdir);
@ -248,7 +277,17 @@ namespace sys_util
return ret == 0x5e17 ? 0 : ret; return ret == 0x5e17 ? 0 : ret;
} }
std::string get_module_path(const char* module_name, unsigned pid) // get module full path, nullptr is for main-exe 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 : "", "" }; std::string param[] = { module_name ? module_name : "", "" };
@ -301,36 +340,36 @@ namespace sys_util
int32_t get_memory_info(uint64_t* total, uint64_t* available) int32_t get_memory_info(uint64_t* total, uint64_t* available)
{ {
if(!total && !available) if (!total && !available)
return 0; return 0;
char line[128] = {0}; char line[128] = { 0 };
FILE *src = fopen("/proc/meminfo", "rb"); FILE* src = fopen("/proc/meminfo", "rb");
int32_t count = total && available ? 2 : 1; int32_t count = total && available ? 2 : 1;
unsigned long val = 0; unsigned long val = 0;
if(!src) if (!src)
return log_cls::log_when_err(-1, "fopen('/proc/meminfo', 'rb')", LOG_LEVEL_FATAL); 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; *total = val * 1024;
if(--count == 0) if (--count == 0)
break; break;
} }
} }
else if(sscanf(line, "MemFree: %ld", &val)) else if (sscanf(line, "MemFree: %ld", &val))
{ {
if(available) if (available)
{ {
*available = val * 1024; *available = val * 1024;
if(--count == 0) if (--count == 0)
break; break;
} }
} }
} }
@ -342,25 +381,25 @@ namespace sys_util
{ {
std::string str("\0", 80); std::string str("\0", 80);
if(bytes >= SIZE_GB(1)) if (bytes >= SIZE_GB(1))
{ {
double v = bytes * 1.0f / (SIZE_GB(1)); double v = bytes * 1.0f / (SIZE_GB(1));
size_t pos = 0; size_t pos = 0;
sprintf(&str[0], "%.2fGB", v); sprintf(&str[0], "%.2fGB", v);
pos = str.find("."); pos = str.find(".");
while(pos > 3) while (pos > 3)
{ {
pos -= 3; pos -= 3;
str.insert(pos, ","); str.insert(pos, ",");
} }
} }
else if(bytes >= SIZE_MB(1)) else if (bytes >= SIZE_MB(1))
{ {
double v = bytes * 1.0f / (SIZE_MB(1)); double v = bytes * 1.0f / (SIZE_MB(1));
sprintf(&str[0], "%.2fMB", v); 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)); double v = bytes * 1.0f / (SIZE_KB(1));
sprintf(&str[0], "%.2fKB", v); sprintf(&str[0], "%.2fKB", v);

View File

@ -112,23 +112,24 @@ public:
#include <string> #include <string>
namespace sys_util 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 void* param, // user defined data, passed into callback on_found
unsigned pid = -1 // process id, -1 is self unsigned pid = -1 // process id, -1 is self
); // return errno ); // return errno
int32_t enum_files(const char* dir, // dir path 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 , void* param // user defined data, passed into callback on_found
, bool recursive = true // walk recursive , bool recursive = true // walk recursive
); // return errno ); // 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 std::string get_module_path(const char* module_name = nullptr
, unsigned pid = -1); // get module full path, nullptr is for main-exe , unsigned pid = -1); // get module full path, nullptr is for main-exe
std::string read_link(const char* lnk); std::string read_link(const char* lnk);
size_t get_page_size(void); size_t get_page_size(void);
bool create_folder(const char* dir); bool create_folder(const char* dir);
// Function: pick single-line info file data, return count of set-value variable // Function: pick single-line info file data, return count of set-value variable
// //
// Parameters: file - full path of local file // Parameters: file - full path of local file
@ -141,19 +142,19 @@ namespace sys_util
// //
// Return: count of the variable which got the value // Return: count of the variable which got the value
template<typename ... Args> template<typename ... Args>
int32_t get_inf_file_data(const char* file, size_t line_max, const char* fmt, Args ... args) 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); std::string buf("\0", line_max + 8);
FILE *src = fopen(file, "rb"); FILE* src = fopen(file, "rb");
int32_t count = 0; int32_t count = 0;
if(!src) if (!src)
return 0; return 0;
while(fgets(&buf[0], line_max, src)) while (fgets(&buf[0], line_max, src))
{ {
count = sscanf(&buf[0], fmt, args ...); count = sscanf(&buf[0], fmt, args ...);
if(count > 0) if (count > 0)
break; break;
} }
fclose(src); fclose(src);