From cc83c1ae559dd332753d1182283132d90a308616 Mon Sep 17 00:00:00 2001 From: springzfx Date: Fri, 29 May 2020 19:32:54 +0800 Subject: [PATCH] use deltype --- src/cgproxyd.hpp | 5 ++--- src/cgroup_attach.cpp | 6 +++--- src/common.cpp | 20 ++++++++++---------- src/execsnoop.cpp | 4 +--- src/execsnoop.h | 3 ++- 5 files changed, 18 insertions(+), 20 deletions(-) diff --git a/src/cgproxyd.hpp b/src/cgproxyd.hpp index d5c129a..769786f 100644 --- a/src/cgproxyd.hpp +++ b/src/cgproxyd.hpp @@ -27,8 +27,6 @@ using namespace ::CGPROXY::CGROUP; // using namespace ::CGPROXY::EXECSNOOP; namespace CGPROXY::EXECSNOOP { -typedef void *(*startThread_t)(void *arg); -startThread_t _startThread; bool loadExecsnoopLib() { try { info("loading %s", LIBEXECSNOOP_SO); @@ -37,7 +35,8 @@ bool loadExecsnoopLib() { error("dlopen %s failed: %s", LIBEXECSNOOP_SO, dlerror()); return false; } - _startThread = reinterpret_cast(dlsym(handle_dl, "_startThread")); + _startThread = + reinterpret_cast(dlsym(handle_dl, "startThread")); if (_startThread == NULL) { error("dlsym startThread failed: %s", dlerror()); return false; diff --git a/src/cgroup_attach.cpp b/src/cgroup_attach.cpp index 111f309..5c0f5c4 100644 --- a/src/cgroup_attach.cpp +++ b/src/cgroup_attach.cpp @@ -17,11 +17,11 @@ string cgroup2_mount_point = get_cgroup2_mount_point(); string get_cgroup2_mount_point() { stringstream buffer; - FILE *fp = popen("findmnt -t cgroup2 -n -o TARGET", "r"); + unique_ptr fp(popen("findmnt -t cgroup2 -n -o TARGET", "r"), + &pclose); if (!fp) return ""; char buf[READ_SIZE_MAX]; - while (fgets(buf, READ_SIZE_MAX, fp) != NULL) { buffer << buf; } - pclose(fp); + while (fgets(buf, READ_SIZE_MAX, fp.get()) != NULL) { buffer << buf; } string s = buffer.str(); s.pop_back(); // remove newline character return s; diff --git a/src/common.cpp b/src/common.cpp index a1a1000..1e9f576 100644 --- a/src/common.cpp +++ b/src/common.cpp @@ -51,21 +51,21 @@ bool dirExist(const string &path) { vector bash_pidof(const string &path) { vector pids; - FILE *fp = popen(to_str("pidof ", path).c_str(), "r"); + unique_ptr fp(popen(to_str("pidof ", path).c_str(), "r"), + &pclose); if (!fp) return pids; int pid; - while (fscanf(fp, "%d", &pid) != EOF) { pids.push_back(pid); } - pclose(fp); + while (fscanf(fp.get(), "%d", &pid) != EOF) { pids.push_back(pid); } return pids; } string bash_which(const string &name) { stringstream buffer; - FILE *fp = popen(to_str("which ", name).c_str(), "r"); + unique_ptr fp(popen(to_str("which ", name).c_str(), "r"), + &pclose); if (!fp) return ""; char buf[READ_SIZE_MAX]; - while (fgets(buf, READ_SIZE_MAX, fp) != NULL) { buffer << buf; } - pclose(fp); + while (fgets(buf, READ_SIZE_MAX, fp.get()) != NULL) { buffer << buf; } string s = buffer.str(); s.pop_back(); // remove newline character return s; @@ -73,11 +73,11 @@ string bash_which(const string &name) { string bash_readlink(const string &path) { stringstream buffer; - FILE *fp = popen(to_str("readlink -e ", path).c_str(), "r"); + unique_ptr fp(popen(to_str("readlink -e ", path).c_str(), "r"), + &pclose); if (!fp) return ""; char buf[READ_SIZE_MAX]; - while (fgets(buf, READ_SIZE_MAX, fp) != NULL) { buffer << buf; } - pclose(fp); + while (fgets(buf, READ_SIZE_MAX, fp.get()) != NULL) { buffer << buf; } string s = buffer.str(); s.pop_back(); // remove newline character return s; @@ -112,7 +112,7 @@ string getCgroup(const string &pid) { ifstream ifs(cgroup_f); debug("prcessing file %s", cgroup_f.c_str()); while (ifs.good() && getline(ifs, line)) { - debug("process line: %s", line.c_str()); + debug("process line: %s", line.c_str()); if (line[0] == '0') { cgroup = line.substr(3); debug("get cgroup of %s: %s", pid.c_str(), cgroup.c_str()); diff --git a/src/execsnoop.cpp b/src/execsnoop.cpp index ef96582..75f8caa 100644 --- a/src/execsnoop.cpp +++ b/src/execsnoop.cpp @@ -97,6 +97,4 @@ void *startThread(void *arg) { return (void *)0; } -} // namespace CGPROXY::EXECSNOOP - -extern "C" void *_startThread(void *arg) { return CGPROXY::EXECSNOOP::startThread(arg); } \ No newline at end of file +} // namespace CGPROXY::EXECSNOOP \ No newline at end of file diff --git a/src/execsnoop.h b/src/execsnoop.h index eec3561..d3d53ed 100644 --- a/src/execsnoop.h +++ b/src/execsnoop.h @@ -16,7 +16,8 @@ int execsnoop(); struct thread_arg { function handle_pid; }; -void *startThread(void *arg); +extern "C" void *startThread(void *arg); +decltype(&startThread) _startThread; // only for dlsym() } // namespace CGPROXY::EXECSNOOP #endif \ No newline at end of file