diff --git a/ci/travis.sh b/ci/travis.sh index c682bfc..bc90c20 100644 --- a/ci/travis.sh +++ b/ci/travis.sh @@ -31,7 +31,7 @@ env | sort : "${COMPILER_IS_GNUCXX:=OFF}" # Install dependency from YUM -yum install -y mrzcpd framework numactl-devel zlib-devel librdkafka-devel +yum install -y mrzcpd framework numactl-devel zlib-devel librdkafka-devel systemd-devel mkdir build || true cd build diff --git a/cmake/FindSYSTEMD.cmake b/cmake/FindSYSTEMD.cmake new file mode 100644 index 0000000..c7decde --- /dev/null +++ b/cmake/FindSYSTEMD.cmake @@ -0,0 +1,39 @@ +# - Find SystemdDaemon +# Find the systemd daemon library +# +# This module defines the following variables: +# SYSTEMD_FOUND - True if library and include directory are found +# If set to TRUE, the following are also defined: +# SYSTEMD_INCLUDE_DIRS - The directory where to find the header file +# SYSTEMD_LIBRARIES - Where to find the library file +# +# For conveniance, these variables are also set. They have the same values +# than the variables above. The user can thus choose his/her prefered way +# to write them. +# SYSTEMD_LIBRARY +# SYSTEMD_INCLUDE_DIR +# +# This file is in the public domain + +include(FindPkgConfig) +pkg_check_modules(SYSTEMD libsystemd) + +if(NOT SYSTEMD_FOUND) + find_path(SYSTEMD_INCLUDE_DIRS NAMES systemd/sd-daemon.h + DOC "The Systemd include directory") + + find_library(SYSTEMD_LIBRARIES NAMES systemd + DOC "The Systemd library") + + # Use some standard module to handle the QUIETLY and REQUIRED arguments, and + # set SYSTEMD_FOUND to TRUE if these two variables are set. + include(FindPackageHandleStandardArgs) + find_package_handle_standard_args(SYSTEMD REQUIRED_VARS SYSTEMD_LIBRARIES SYSTEMD_INCLUDE_DIRS) + + if(SYSTEMD_FOUND) + set(SYSTEMD_LIBRARY ${SYSTEMD_LIBRARIES}) + set(SYSTEMD_INCLUDE_DIR ${SYSTEMD_INCLUDE_DIRS}) + endif() +endif() + +mark_as_advanced(SYSTEMD_INCLUDE_DIRS SYSTEMD_LIBRARIES) \ No newline at end of file diff --git a/common/include/tfe_utils.h b/common/include/tfe_utils.h index e630650..8d111d4 100644 --- a/common/include/tfe_utils.h +++ b/common/include/tfe_utils.h @@ -33,12 +33,14 @@ #define UNUSED __attribute__((unused)) extern void * g_default_logger; +extern bool g_print_to_stderr; + #define TFE_LOG_ERROR(handler, fmt, ...) \ -do { fprintf(stderr, fmt "\n" , ##__VA_ARGS__); \ +do { if(g_print_to_stderr) fprintf(stderr, fmt "\n" , ##__VA_ARGS__); \ MESA_handle_runtime_log(handler, RLOG_LV_FATAL, __FUNCTION__, fmt, ##__VA_ARGS__); } while(0) #define TFE_LOG_INFO(handler, fmt, ...) \ -do { fprintf(stderr, fmt "\n", ##__VA_ARGS__); \ +do { if(g_print_to_stderr) fprintf(stderr, fmt "\n", ##__VA_ARGS__); \ MESA_handle_runtime_log(handler, RLOG_LV_INFO, __FUNCTION__, fmt, ##__VA_ARGS__); } while(0) \ #define TFE_LOG_DEBUG(handler, fmt, ...) \ diff --git a/platform/CMakeLists.txt b/platform/CMakeLists.txt index 42ca6eb..2a17698 100644 --- a/platform/CMakeLists.txt +++ b/platform/CMakeLists.txt @@ -1,3 +1,5 @@ +find_package(SYSTEMD REQUIRED) + add_executable(tfe src/acceptor_kni_v1.cpp src/acceptor_kni_v2.cpp src/ssl_stream.cpp src/key_keeper.cpp src/ssl_sess_cache.cpp src/ssl_sess_ticket.cpp src/ssl_service_cache.cpp src/ssl_trusted_cert_storage.cpp src/ev_root_ca_metadata.cpp src/ssl_utils.cpp @@ -5,6 +7,7 @@ add_executable(tfe src/acceptor_kni_v1.cpp src/acceptor_kni_v2.cpp src/ssl_strea target_include_directories(tfe PUBLIC ${CMAKE_CURRENT_LIST_DIR}/include/external) target_include_directories(tfe PRIVATE ${CMAKE_CURRENT_LIST_DIR}/include/internal) +target_include_directories(tfe PRIVATE ${SYSTEMD_INCLUDE_DIRS}) target_link_libraries(tfe common tango-cache-client) target_link_libraries(tfe pthread dl @@ -21,7 +24,8 @@ target_link_libraries(tfe pthread dl MESA_htable wiredcfg MESA_field_stat gperftools-static - breakpad-client-static) + breakpad-client-static + ${SYSTEMD_LIBRARIES}) if(ENABLE_PLUGIN_HTTP) target_link_libraries(tfe -Wl,--whole-archive http -Wl,--no-whole-archive) @@ -84,27 +88,23 @@ install(TARGETS tfe RUNTIME DESTINATION bin COMPONENT Program) # MESA_field_stat) # #### test_chello_parse -add_executable(test_chello_parse test/test_chello_parse.cpp src/ssl_utils.cpp) +#add_executable(test_chello_parse test/test_chello_parse.cpp src/ssl_utils.cpp) +#target_include_directories(test_chello_parse PRIVATE ${CMAKE_CURRENT_LIST_DIR}/include/internal) +#target_link_libraries(test_chello_parse common) +#target_link_libraries(test_chello_parse pthread dl +# openssl-ssl-static +# openssl-crypto-static +# pthread libevent-static +# libevent-static-openssl +# libevent-static-pthreads +# MESA_handle_logger +# MESA_prof_load +# MESA_htable wiredcfg +# cjson +# MESA_field_stat) -target_include_directories(test_chello_parse PRIVATE ${CMAKE_CURRENT_LIST_DIR}/include/internal) - -target_link_libraries(test_chello_parse common) -target_link_libraries(test_chello_parse pthread dl - openssl-ssl-static - openssl-crypto-static - pthread libevent-static - libevent-static-openssl - libevent-static-pthreads - MESA_handle_logger - MESA_prof_load - MESA_htable wiredcfg - cjson - MESA_field_stat) - -#### test_sender_scm -add_executable(test_sender_scm src/sender_scm.cpp test/test_sender_scm.cpp) - -target_include_directories(test_sender_scm PRIVATE ${CMAKE_CURRENT_LIST_DIR}/include/internal) - -target_link_libraries(test_sender_scm common) -target_link_libraries(test_sender_scm pthread dl MESA_prof_load MESA_handle_logger) +##### test_sender_scm +#add_executable(test_sender_scm src/sender_scm.cpp test/test_sender_scm.cpp) +#target_include_directories(test_sender_scm PRIVATE ${CMAKE_CURRENT_LIST_DIR}/include/internal) +#target_link_libraries(test_sender_scm common) +#target_link_libraries(test_sender_scm pthread dl MESA_prof_load MESA_handle_logger) diff --git a/platform/src/proxy.cpp b/platform/src/proxy.cpp index 2b99268..919426e 100644 --- a/platform/src/proxy.cpp +++ b/platform/src/proxy.cpp @@ -45,10 +45,13 @@ #include #include #include + /* Breakpad */ #include #include +/* Systemd */ +#include extern struct ssl_policy_enforcer* ssl_policy_enforcer_create(void* logger); extern enum ssl_stream_action ssl_policy_enforce(struct ssl_stream *upstream, void* u_para); @@ -58,6 +61,8 @@ static int signals[] = {SIGHUP, SIGPIPE, SIGUSR1}; /* Global Resource */ void * g_default_logger = NULL; struct tfe_proxy * g_default_proxy = NULL; +bool g_print_to_stderr = true; + /* Per thread resource */ thread_local unsigned int __currect_thread_id = 0; thread_local void * __currect_default_logger = NULL; @@ -109,6 +114,13 @@ void tfe_proxy_thread_ctx_release(struct tfe_thread_ctx * thread_ctx) ATOMIC_DEC(&thread_ctx->load); } +/* 检查本进程是否通过SYSTEMD启动 */ +static int check_is_started_by_notify() +{ + char * notify_socket = getenv("NOTIFY_SOCKET"); + return notify_socket == NULL ? 0 : 1; +} + int tfe_proxy_fds_accept(struct tfe_proxy * ctx, int fd_downstream, int fd_upstream, struct tfe_cmsg * cmsg) { struct tfe_thread_ctx * worker_thread_ctx = tfe_proxy_thread_ctx_acquire(ctx); @@ -618,6 +630,8 @@ int main(int argc, char * argv[]) } } + fprintf(stderr, "Tango Frontend Engine, Version: %s", __tfe_version); + /* adds locking, only required if accessed from separate threads */ evthread_use_pthreads(); unsigned int __log_level = RLOG_LV_INFO; @@ -713,7 +727,17 @@ int main(int argc, char * argv[]) g_default_proxy->watchdog_kni = watchdog_kni_create(g_default_proxy, main_profile, g_default_logger); CHECK_OR_EXIT(g_default_proxy->watchdog_kni != NULL, "Failed at creating KNI watchdog, Exit."); - TFE_LOG_ERROR(g_default_logger, "Tango Frontend Engine initialized. "); + TFE_LOG_ERROR(g_default_logger, "Tango Frontend Engine initialized, Version: %s.", __tfe_version); + + /* If TFE is run by systemd's notify, then tell the systemd our tfe is ready. + * and disable the stderr log, only print logs into files */ + if(check_is_started_by_notify()) + { + sd_notify(0, "READY=1"); + g_print_to_stderr = false; + sleep(1); + } + event_base_dispatch(g_default_proxy->evbase); return 0; } diff --git a/plugin/protocol/http/test/test_http_convert.cpp b/plugin/protocol/http/test/test_http_convert.cpp index 72e5b3c..a1a3ff2 100644 --- a/plugin/protocol/http/test/test_http_convert.cpp +++ b/plugin/protocol/http/test/test_http_convert.cpp @@ -133,6 +133,7 @@ unsigned int monkey_gz_len = 407; unsigned int monkey_len = 843; unsigned int __64x_len = 64; unsigned int __64x_gz_len = 28; +bool g_print_to_stderr = true; struct callback_ctx { diff --git a/plugin/protocol/http/test/test_http_half.cpp b/plugin/protocol/http/test/test_http_half.cpp index aa2df56..6f14ebf 100644 --- a/plugin/protocol/http/test/test_http_half.cpp +++ b/plugin/protocol/http/test/test_http_half.cpp @@ -2,6 +2,8 @@ #include #include +bool g_print_to_stderr = true; + static const char * __identify_http_request = "POST /gen_204 HTTP/1.1\r\n" "Host: www.google.com\r\n" diff --git a/plugin/protocol/http2/test/test_http2_stream.cpp b/plugin/protocol/http2/test/test_http2_stream.cpp index 00d0fdd..cf4c83d 100644 --- a/plugin/protocol/http2/test/test_http2_stream.cpp +++ b/plugin/protocol/http2/test/test_http2_stream.cpp @@ -6,8 +6,8 @@ #include #include - #include "test_http2_stream.h" +bool g_print_to_stderr = true; /********* stub function ******************************/ const char * tfe_version() diff --git a/script/service/tfe.service b/script/service/tfe.service index 975a8e8..ace347a 100644 --- a/script/service/tfe.service +++ b/script/service/tfe.service @@ -4,10 +4,10 @@ Requires=tfe-env.service After=tfe-env.service [Service] -Type=simple +Type=notify ExecStart=/opt/tsg/tfe/bin/tfe WorkingDirectory=/opt/tsg/tfe/ -TimeoutSec=180s +TimeoutSec=300s RestartSec=10s Restart=always LimitNOFILE=524288