add master tools

This commit is contained in:
zsx 2018-01-24 15:55:50 +08:00
parent df1e871603
commit f1f640f65c
2 changed files with 45 additions and 7 deletions

View File

@ -18,6 +18,7 @@ public:
tools(void) {}
~tools(void) {}
static bool get_version(ACL_VSTREAM* fp, acl::string& out);
static bool get_line(ACL_VSTREAM* fp, acl::string& out);
static bool get_version(const char* path, acl::string& out);
static int get_fds(pid_t pid);
};

View File

@ -1,7 +1,7 @@
#include "stdafx.h"
#include "tools.h"
bool tools::get_version(ACL_VSTREAM* fp, acl::string& out)
bool tools::get_line(ACL_VSTREAM* fp, acl::string& out)
{
char buf[8192];
@ -27,16 +27,53 @@ bool tools::get_version(const char* path, acl::string& out)
ACL_VSTREAM_POPEN_ARGV, args->argv, ACL_VSTREAM_POPEN_END);
if (fp == NULL)
{
logger_error("popen error=%s, comd=%s",
logger_error("popen error=%s, cmd=%s",
acl::last_serror(), path);
acl_argv_free(args);
return false;
}
else
{
get_version(fp, out);
bool ret = get_line(fp, out);
acl_vstream_pclose(fp);
acl_argv_free(args);
return ret;
}
acl_argv_free(args);
return true;
int tools::get_fds(pid_t pid)
{
acl::string buf;
buf.format("/proc/%d/fd", (int) pid);
ACL_ARGV* args = acl_argv_alloc(1);
acl_argv_add(args, "ls", buf.c_str(), "|", "wc", "-l", NULL);
ACL_VSTREAM* fp = acl_vstream_popen(O_RDWR,
ACL_VSTREAM_POPEN_ARGV, args->argv, ACL_VSTREAM_POPEN_END);
if (fp == NULL)
{
logger_error("popen error=%s, cmd=ls /proc/%d/fd|wc -l",
acl::last_serror(), (int) pid);
return -1;
}
char line[1024];
int ret = acl_vstream_gets_nonl(fp, line, sizeof(line));
if (ret == ACL_VSTREAM_EOF)
{
logger_error("gets line error=%s, cmd=ls /proc/%d/fd|wc -l",
acl::last_serror(), (int) pid);
acl_argv_free(args);
acl_vstream_pclose(fp);
return -1;
}
ret = atoi(line);
if (ret < 0)
logger_error("invalid fdcount=%d, cmd=ls /proc/%d/fd|wc -l",
ret, (int) pid);
acl_argv_free(args);
acl_vstream_pclose(fp);
return ret;
}