添加枚举进程及模块函数
This commit is contained in:
parent
9be3ce881d
commit
af5c96ec2e
|
@ -8,11 +8,11 @@
|
|||
// refer
|
||||
refer::refer() : ref_(1)
|
||||
{
|
||||
on_born();
|
||||
on_born();
|
||||
}
|
||||
refer::~refer()
|
||||
{
|
||||
on_dead();
|
||||
on_dead();
|
||||
}
|
||||
|
||||
void refer::on_born(void)
|
||||
|
@ -22,23 +22,23 @@ void refer::on_dead(void)
|
|||
|
||||
int32_t refer::add_ref(void)
|
||||
{
|
||||
LOCKER lock(mutex_);
|
||||
LOCKER lock(mutex_);
|
||||
|
||||
return ++ref_;
|
||||
return ++ref_;
|
||||
}
|
||||
int32_t refer::release(void)
|
||||
{
|
||||
int32_t ref = 0;
|
||||
int32_t ref = 0;
|
||||
|
||||
{
|
||||
LOCKER lock(mutex_);
|
||||
ref = --ref_;
|
||||
}
|
||||
{
|
||||
LOCKER lock(mutex_);
|
||||
ref = --ref_;
|
||||
}
|
||||
|
||||
if (ref == 0)
|
||||
delete this;
|
||||
if (ref == 0)
|
||||
delete this;
|
||||
|
||||
return ref;
|
||||
return ref;
|
||||
}
|
||||
|
||||
|
||||
|
@ -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,12 +184,44 @@ 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
|
||||
void* param, // user defined data, passed into callback on_found
|
||||
unsigned pid // process id, -1 is self
|
||||
) // return errno
|
||||
{
|
||||
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
|
||||
{
|
||||
char path[128] = { 0 };
|
||||
|
||||
if (pid == -1)
|
||||
|
@ -197,14 +229,14 @@ namespace sys_util
|
|||
sprintf(path, "/proc/%u/map_files/", pid);
|
||||
|
||||
return enum_files(path, on_found, param, false);
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
) // return errno
|
||||
{
|
||||
) // return errno
|
||||
{
|
||||
int32_t ret = 0;
|
||||
DIR* pdir = nullptr;
|
||||
struct dirent* ent = nullptr;
|
||||
|
@ -215,32 +247,29 @@ namespace sys_util
|
|||
|
||||
while ((ent = readdir(pdir)))
|
||||
{
|
||||
if (ent->d_type & DT_DIR)
|
||||
{
|
||||
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);
|
||||
if (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0)
|
||||
continue;
|
||||
|
||||
file += "/";
|
||||
file += ent->d_name;
|
||||
if (!on_found(read_link(file.c_str()).c_str(), param))
|
||||
{
|
||||
ret = 0x5e17;
|
||||
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))
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
closedir(pdir);
|
||||
|
@ -248,7 +277,17 @@ namespace sys_util
|
|||
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 : "", "" };
|
||||
|
||||
|
@ -301,36 +340,36 @@ 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);
|
||||
|
|
|
@ -112,23 +112,24 @@ 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
|
||||
void* param, // user defined data, passed into callback on_found
|
||||
unsigned pid = -1 // process id, -1 is self
|
||||
); // return errno
|
||||
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
|
||||
, bool recursive = true // walk recursive
|
||||
); // return errno
|
||||
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
|
||||
, unsigned pid = -1); // get module full path, nullptr is for main-exe
|
||||
std::string read_link(const char* lnk);
|
||||
size_t get_page_size(void);
|
||||
bool create_folder(const char* dir);
|
||||
|
||||
|
||||
// Function: pick single-line info file data, return count of set-value variable
|
||||
//
|
||||
// Parameters: file - full path of local file
|
||||
|
@ -141,19 +142,19 @@ namespace sys_util
|
|||
//
|
||||
// Return: count of the variable which got the value
|
||||
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);
|
||||
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);
|
||||
|
|
Loading…
Reference in New Issue