From aedebf4e31d89c82e3233fe90a6c61b2af97b439 Mon Sep 17 00:00:00 2001 From: fancy Date: Mon, 18 May 2020 17:29:58 +0800 Subject: [PATCH] merge to one executable --- CMakeLists.txt | 1 + cgproxyd | 2 ++ src/CMakeLists.txt | 26 ++++++++------------------ src/{cgproxy.cpp => cgproxy.hpp} | 23 +++++++++++++++++++---- src/{cgproxyd.cpp => cgproxyd.hpp} | 26 ++++++++++++++++---------- src/common.h | 2 ++ src/config.cpp | 10 ++-------- src/config.h | 1 - src/main.cpp | 17 +++++++++++++++++ test/CMakeLists.txt | 4 ++-- test/socket_client_test.cpp | 9 ++++----- 11 files changed, 73 insertions(+), 48 deletions(-) create mode 100644 cgproxyd rename src/{cgproxy.cpp => cgproxy.hpp} (69%) rename src/{cgproxyd.cpp => cgproxyd.hpp} (90%) create mode 100644 src/main.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index e904427..ef4e6b2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -19,6 +19,7 @@ if (build_test) add_subdirectory(test) endif() +install(FILES cgproxyd DESTINATION /usr/bin PERMISSIONS ${basic_permission}) install(FILES cgnoproxy DESTINATION /usr/bin PERMISSIONS ${basic_permission}) install(FILES cgproxy.service DESTINATION /usr/lib/systemd/system/) install(FILES config.json DESTINATION /etc/cgproxy/) diff --git a/cgproxyd b/cgproxyd new file mode 100644 index 0000000..792653e --- /dev/null +++ b/cgproxyd @@ -0,0 +1,2 @@ +#!/bin/sh +/usr/bin/cgproxy --daemon $@ \ No newline at end of file diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 80eb72a..625ae58 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -3,22 +3,12 @@ find_package(nlohmann_json REQUIRED) include_directories(${PROJECT_SOURCE_DIR}) include_directories(${CMAKE_CURRENT_SOURCE_DIR}) -add_library(cgproxy-lib SHARED config.cpp socket_client.cpp common.cpp) -set_target_properties(cgproxy-lib PROPERTIES LINKER_LANGUAGE CXX) -set_target_properties(cgproxy-lib PROPERTIES OUTPUT_NAME cgproxy) -target_link_libraries(cgproxy-lib nlohmann_json::nlohmann_json) +add_executable(main main.cpp + common.cpp config.cpp cgroup_attach.cpp + socket_client.cpp socket_server.cpp) + +target_link_libraries(main nlohmann_json::nlohmann_json Threads::Threads) +set_target_properties(main PROPERTIES LINKER_LANGUAGE CXX) +set_target_properties(main PROPERTIES OUTPUT_NAME cgproxy) -add_library(cgproxyd-lib SHARED config.cpp cgroup_attach.cpp socket_server.cpp common.cpp) -set_target_properties(cgproxyd-lib PROPERTIES LINKER_LANGUAGE CXX) -set_target_properties(cgproxyd-lib PROPERTIES OUTPUT_NAME cgproxyd) -target_link_libraries(cgproxyd-lib nlohmann_json::nlohmann_json) - -add_executable(cgproxyd cgproxyd.cpp) -add_executable(cgproxy cgproxy.cpp) -target_link_libraries(cgproxyd cgproxyd-lib Threads::Threads) -target_link_libraries(cgproxy cgproxy-lib) - -install(TARGETS cgproxyd DESTINATION /usr/bin PERMISSIONS ${basic_permission}) -install(TARGETS cgproxy DESTINATION /usr/bin PERMISSIONS ${basic_permission}) -install(TARGETS cgproxy-lib DESTINATION /usr/lib PERMISSIONS ${basic_permission}) -install(TARGETS cgproxyd-lib DESTINATION /usr/lib PERMISSIONS ${basic_permission}) +install(TARGETS main DESTINATION /usr/bin PERMISSIONS ${basic_permission}) diff --git a/src/cgproxy.cpp b/src/cgproxy.hpp similarity index 69% rename from src/cgproxy.cpp rename to src/cgproxy.hpp index f8af4ee..174aaf9 100644 --- a/src/cgproxy.cpp +++ b/src/cgproxy.hpp @@ -1,16 +1,25 @@ #include "common.h" #include "config.h" #include "socket_client.h" +#include #include #include using json = nlohmann::json; using namespace CGPROXY; using namespace CGPROXY::CONFIG; +namespace CGPROXY::CGPROXY { + bool print_help = false, proxy = true; inline void print_usage() { - fprintf(stdout, "Usage: cgproxy [--help] [--debug] [--noproxy] \n"); - fprintf(stdout, "Alias: cgnoproxy = cgproxy --noproxy\n"); + if (proxy) { + cout << "Run program with proxy" << endl; + cout << "Usage: cgproxy [--help] [--debug] " << endl; + } else { + cout << "Run program without proxy" << endl; + cout << "Usage: cgpnoroxy [--help] [--debug] " << endl; + cout << "Alias: cgnoproxy = cgproxy --noproxy" << endl; + } } void processArgs(const int argc, char *argv[], int &shift) { @@ -34,11 +43,16 @@ int main(int argc, char *argv[]) { int shift = 1; processArgs(argc, argv, shift); - if (argc == shift || print_help) { + if (print_help) { print_usage(); exit(0); } + if (argc == shift) { + error("no program specified"); + exit(EXIT_FAILURE); + } + int status = -1; send_pid(getpid(), proxy, status); if (status != 0) { @@ -48,4 +62,5 @@ int main(int argc, char *argv[]) { string s = join2str(argc - shift, argv + shift, ' '); return system(s.c_str()); -} \ No newline at end of file +} +} // namespace CGPROXY::CGPROXY \ No newline at end of file diff --git a/src/cgproxyd.cpp b/src/cgproxyd.hpp similarity index 90% rename from src/cgproxyd.cpp rename to src/cgproxyd.hpp index ffdfefd..af633dd 100644 --- a/src/cgproxyd.cpp +++ b/src/cgproxyd.hpp @@ -1,3 +1,6 @@ +#ifndef CGPROXYD_HPP +#define CGPROXYD_HPP + #include "cgroup_attach.h" #include "common.h" #include "config.h" @@ -10,11 +13,11 @@ using namespace std; using json = nlohmann::json; -using namespace CGPROXY::SOCKET; -using namespace CGPROXY::CONFIG; -using namespace CGPROXY::CGROUP; +using namespace ::CGPROXY::SOCKET; +using namespace ::CGPROXY::CONFIG; +using namespace ::CGPROXY::CGROUP; -namespace CGPROXY { +namespace CGPROXY::CGPROXYD { class cgproxyd { thread_arg arg_t; @@ -146,11 +149,13 @@ public: cgproxyd *cgproxyd::instance = NULL; -} // namespace CGPROXY - bool print_help = false; -void print_usage() { printf("cgproxyd [--help] [--debug]\n"); } +void print_usage() { + cout << "Start a daemon with unix socket to accept control" << endl; + cout << "Usage: cgproxyd [--help] [--debug]" << endl; + cout << "Alias: cgproxyd = cgproxy --daemon" << endl; +} void processArgs(const int argc, char *argv[]) { for (int i = 1; i < argc; i++) { @@ -169,10 +174,11 @@ int main(int argc, char *argv[]) { if (getuid() != 0) { error("permission denied, need root"); - print_usage(); exit(EXIT_FAILURE); } - CGPROXY::cgproxyd d; + cgproxyd d; return d.start(); -} \ No newline at end of file +} +} // namespace CGPROXY::CGPROXYD +#endif \ No newline at end of file diff --git a/src/common.h b/src/common.h index 0c20b88..207e6e2 100644 --- a/src/common.h +++ b/src/common.h @@ -38,12 +38,14 @@ extern bool enable_debug; #define error(...) \ { \ + fprintf(stderr, "error: "); \ fprintf(stderr, __VA_ARGS__); \ fprintf(stderr, "\n"); \ } #define debug(...) \ if (enable_debug) { \ + fprintf(stderr, "debug: "); \ fprintf(stdout, __VA_ARGS__); \ fprintf(stdout, "\n"); \ } diff --git a/src/config.cpp b/src/config.cpp index 00de499..a057f89 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -1,4 +1,5 @@ #include "config.h" +#include "common.h" #include #include #include @@ -58,14 +59,7 @@ int Config::loadFromFile(const string f) { debug("loading config: %s", f.c_str()); ifstream ifs(f); if (ifs.is_open()) { - string js; - try { - ifs >> js; - } catch (exception &e) { - error("parse error: %s", f.c_str()); - ifs.close(); - return PARSE_ERROR; - } + string js = to_str(ifs.rdbuf()); ifs.close(); return loadFromJsonStr(js); } else { diff --git a/src/config.h b/src/config.h index 4273335..246cbfc 100644 --- a/src/config.h +++ b/src/config.h @@ -32,7 +32,6 @@ public: private: void mergeReserved(); bool validateJsonStr(const string js); - }; } // namespace CGPROXY::CONFIG diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..5db371c --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,17 @@ +#include "cgproxy.hpp" +#include "cgproxyd.hpp" + +bool as_cgproxyd = false; +void processArgs(const int argc, char *argv[]) { + for (int i = 1; i < argc; i++) { + if (strcmp(argv[i], "--daemon") == 0) { as_cgproxyd = true; } + if (argv[i][0] != '-') { break; } + } +} + +int main(int argc, char *argv[]) { + processArgs(argc, argv); + if (as_cgproxyd) ::CGPROXY::CGPROXYD::main(argc, argv); + else + ::CGPROXY::CGPROXY::main(argc, argv); +} \ No newline at end of file diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index ac81e3f..e2882d6 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -2,6 +2,6 @@ include_directories(${PROJECT_SOURCE_DIR}) include_directories(${PROJECT_SOURCE_DIR}/src) find_package(nlohmann_json REQUIRED) -add_executable(client_test socket_client_test.cpp) -target_link_libraries(client_test cgproxy-lib) +add_executable(client_test socket_client_test.cpp + ../src/socket_client.cpp ../src/common.cpp ../src/config.cpp) target_link_libraries(client_test nlohmann_json::nlohmann_json) \ No newline at end of file diff --git a/test/socket_client_test.cpp b/test/socket_client_test.cpp index 8be3754..4899b71 100644 --- a/test/socket_client_test.cpp +++ b/test/socket_client_test.cpp @@ -29,16 +29,15 @@ void send_pid(const pid_t pid, bool proxy, int &status) { SOCKET::send(j.dump(), status); } - -void test_config(){ +void test_config() { Config config; - config.cgroup_proxy={"/"}; + config.cgroup_proxy = {"/"}; int status; send_config(config, status); } -void test_config_path(){ - string path="/etc/cgproxy/config.json"; +void test_config_path() { + string path = "/etc/cgproxy/config.json"; int status; send_config_path(path, status); }