Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add -d option to fork into background as a daemon #586

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,12 @@ else()
install(FILES ${CMAKE_BINARY_DIR}/trojan.service ${CMAKE_BINARY_DIR}/[email protected] DESTINATION ${SYSTEMD_SERVICE_PATH})
endif()

include(CheckSymbolExists)
check_symbol_exists(daemon "stdlib.h;unistd.h" HAVE_DAEMON)
if(HAVE_DAEMON)
add_definitions(-DHAVE_DAEMON)
endif()

enable_testing()
add_test(NAME LinuxSmokeTest-basic
COMMAND bash ${CMAKE_SOURCE_DIR}/tests/LinuxSmokeTest/basic.sh ${CMAKE_BINARY_DIR}/trojan)
Expand Down
3 changes: 3 additions & 0 deletions docs/trojan.1
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ and start either a proxy client or a proxy server.
.BR \-c, " " \-\-config=\fICONFIG\fR
Set the config file to be loaded. Default is \fI/etc/trojan/config.json\fR.
.TP
.BR \-d, " " \-\-daemon
Detach from the terminal and run in the background as a daemon.
.TP
.BR \-h, " " \-\-help
Print help message.
.TP
Expand Down
1 change: 1 addition & 0 deletions docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
usage: ./trojan [-htv] [-l LOG] [-k KEYLOG] [[-c] CONFIG]
options:
-c [ --config ] CONFIG specify config file
-d [ --daemon ] fork into background
-h [ --help ] print help message
-k [ --keylog ] KEYLOG specify keylog file location (OpenSSL >= 1.1.1)
-l [ --log ] LOG specify log file location
Expand Down
17 changes: 16 additions & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
#ifdef ENABLE_MYSQL
#include <mysql.h>
#endif // ENABLE_MYSQL
#ifdef HAVE_DAEMON
#include <unistd.h>
#endif // HAVE_DAEMON
#include "core/service.h"
#include "core/version.h"
using namespace std;
Expand Down Expand Up @@ -68,9 +71,11 @@ int main(int argc, const char *argv[]) {
string log_file;
string keylog_file;
bool test;
bool do_daemon;
po::options_description desc("options");
desc.add_options()
("config,c", po::value<string>(&config_file)->default_value(DEFAULT_CONFIG)->value_name("CONFIG"), "specify config file")
("daemon,d", po::bool_switch(&do_daemon), "fork into background")
("help,h", "print help message")
("keylog,k", po::value<string>(&keylog_file)->value_name("KEYLOG"), "specify keylog file location (OpenSSL >= 1.1.1)")
("log,l", po::value<string>(&log_file)->value_name("LOG"), "specify log file location")
Expand All @@ -83,7 +88,7 @@ int main(int argc, const char *argv[]) {
po::store(po::command_line_parser(argc, argv).options(desc).positional(pd).run(), vm);
po::notify(vm);
if (vm.count("help")) {
Log::log(string("usage: ") + argv[0] + " [-htv] [-l LOG] [-k KEYLOG] [[-c] CONFIG]", Log::FATAL);
Log::log(string("usage: ") + argv[0] + " [-hdtv] [-l LOG] [-k KEYLOG] [[-c] CONFIG]", Log::FATAL);
cerr << desc;
exit(EXIT_SUCCESS);
}
Expand Down Expand Up @@ -137,6 +142,16 @@ int main(int argc, const char *argv[]) {
if (vm.count("keylog")) {
Log::redirect_keylog(keylog_file);
}
#ifdef HAVE_DAEMON
if (do_daemon && daemon(0, 0) == -1) {
Log::log(string("Failed to fork into background: ") + strerror(errno), Log::FATAL);
exit(EXIT_FAILURE);
}
#else // HAVE_DAEMON
if (do_daemon) {
Log::log("Daemon mode not supported on this system", Log::FATAL);
}
#endif // HAVE_DAEMON
bool restart;
Config config;
do {
Expand Down