mirror of https://github.com/TarsCloud/TarsCpp
fix tc_port exec out buff size limit
parent
794360851e
commit
e9e75cf582
|
@ -2,7 +2,7 @@
|
|||
#include "util/tc_common.h"
|
||||
#include <cmath>
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
#include "util/tc_file.h"
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
|
@ -32,6 +32,10 @@ TEST_F(UtilPortTest, testExec)
|
|||
string err;
|
||||
string result = TC_Port::exec("ls '*.txt'", err);
|
||||
cout << result << endl;
|
||||
|
||||
string out = TC_Port::exec(("file " + TC_File::getExePath()).c_str());
|
||||
|
||||
cout << out << endl;
|
||||
}
|
||||
|
||||
TEST_F(UtilPortTest, testGetPidMemUsed)
|
||||
|
@ -126,74 +130,6 @@ TEST_F(UtilPortTest, testGetDisk)
|
|||
}
|
||||
|
||||
#endif
|
||||
//
|
||||
//#if TARGET_PLATFORM_IOS
|
||||
//#include <sys/types.h>
|
||||
//#include <sys/sysctl.h>
|
||||
//#include <mach/mach.h>
|
||||
//#include <libproc.h>
|
||||
//#include <sys/proc_info.h>
|
||||
//#endif
|
||||
//
|
||||
//
|
||||
//vector<int64_t> getPidsByCmdline(const string &cmdLine, bool accurateMatch)
|
||||
//{
|
||||
// vector<int64_t> pids;
|
||||
//
|
||||
//#if TARGET_PLATFORM_IOS
|
||||
// int mib[4];
|
||||
// mib[0] = CTL_KERN;
|
||||
// mib[1] = KERN_PROC;
|
||||
// mib[2] = KERN_PROC_ALL;
|
||||
// mib[3] = 0;
|
||||
//
|
||||
// size_t size;
|
||||
// if (sysctl(mib, 4, NULL, &size, NULL, 0) == -1) {
|
||||
// return {};
|
||||
// }
|
||||
//
|
||||
// struct kinfo_proc* proc_list = (struct kinfo_proc*)malloc(size);
|
||||
// if (proc_list == NULL) {
|
||||
// return {};
|
||||
// }
|
||||
//
|
||||
// if (sysctl(mib, 4, proc_list, &size, NULL, 0) == -1) {
|
||||
// free(proc_list);
|
||||
// return {};
|
||||
// }
|
||||
//
|
||||
// int num_procs = size / sizeof(struct kinfo_proc);
|
||||
// for (int i = 0; i < num_procs; i++)
|
||||
// {
|
||||
// vector<string> args = getArgs(proc_list[i].kp_proc.p_pid);
|
||||
// string path = TC_Common::tostr(args.begin(), args.end(), " ");
|
||||
//
|
||||
// if(accurateMatch)
|
||||
// {
|
||||
// if(cmdLine == path)
|
||||
// {
|
||||
// pids.push_back(proc_list[i].kp_proc.p_pid);
|
||||
// }
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// if(std::string(path).find(cmdLine) != std::string::npos)
|
||||
// {
|
||||
// pids.push_back(proc_list[i].kp_proc.p_pid);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// free(proc_list);
|
||||
// return pids;
|
||||
//#elif TARGET_PLATFORM_LINUX
|
||||
// return {};
|
||||
//#elif TARGET_PLATFORM_WINDOWS
|
||||
// return {};
|
||||
//#else
|
||||
// return {};
|
||||
//#endif
|
||||
//}
|
||||
|
||||
TEST_F(UtilPortTest, testGetCommandline)
|
||||
{
|
||||
|
@ -215,4 +151,5 @@ TEST_F(UtilPortTest, testGetPidsByCmdline)
|
|||
int64_t pid = TC_Port::getpid();
|
||||
|
||||
ASSERT_TRUE(std::find(pids.begin(), pids.end(), pid) != pids.end());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -217,7 +217,7 @@ public:
|
|||
* 运行一个脚本
|
||||
* @param cmd
|
||||
* @param err
|
||||
* @return 程序的标准输出(最大2k的输出长度)
|
||||
* @return 程序的标准输出
|
||||
*/
|
||||
static std::string exec(const char* cmd);
|
||||
|
||||
|
@ -225,7 +225,7 @@ public:
|
|||
* 运行一个脚本(程序+命令行)
|
||||
* @param cmd
|
||||
* @param err
|
||||
* @return: 程序的标准输出(最大2k的输出长度)
|
||||
* @return: 程序的标准输出
|
||||
*/
|
||||
static std::string exec(const char* cmd, std::string &err);
|
||||
|
||||
|
|
|
@ -300,29 +300,30 @@ string TC_Port::exec(const char *cmd)
|
|||
|
||||
std::string TC_Port::exec(const char* cmd, std::string &err)
|
||||
{
|
||||
string fileData;
|
||||
std::string result;
|
||||
#if TARGET_PLATFORM_WINDOWS
|
||||
FILE* fp = _popen(cmd, "r");
|
||||
#else
|
||||
FILE* fp = popen(cmd, "r");
|
||||
#endif
|
||||
if(fp == NULL) {
|
||||
err = "open '" + string(cmd) + "' error";
|
||||
return "";
|
||||
}
|
||||
static size_t buf_len = 2 * 1024 * 1024;
|
||||
char *buf = new char[buf_len];
|
||||
memset(buf, 0, buf_len);
|
||||
fread(buf, sizeof(char), buf_len - 1, fp);
|
||||
if(fp == NULL) {
|
||||
err = "open '" + string(cmd) + "' error";
|
||||
return "";
|
||||
}
|
||||
|
||||
char buffer[1024];
|
||||
while (!feof(fp)) {
|
||||
if (fgets(buffer, 1024, fp) != NULL)
|
||||
result.append(buffer);
|
||||
}
|
||||
|
||||
#if TARGET_PLATFORM_WINDOWS
|
||||
_pclose(fp);
|
||||
#else
|
||||
pclose(fp);
|
||||
#endif
|
||||
fileData = string(buf);
|
||||
delete []buf;
|
||||
|
||||
return fileData;
|
||||
return result;
|
||||
}
|
||||
|
||||
#if TARGET_PLATFORM_LINUX
|
||||
|
@ -724,9 +725,9 @@ vector<int64_t> TC_Port::getPidsByCmdline(const string &cmdLine, bool accurateMa
|
|||
FILE *TC_Port::freopen(const char * dst, const char * mode, FILE * src)
|
||||
{
|
||||
#if TARGET_PLATFORM_IOS
|
||||
return freopen(dst, mode, src);
|
||||
return ::freopen(dst, mode, src);
|
||||
#else
|
||||
return freopen64(dst, mode, src);
|
||||
return ::freopen64(dst, mode, src);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue