From 7adc9037ae3b51153fa20d62b42381927f3b73a3 Mon Sep 17 00:00:00 2001 From: Max Lv Date: Wed, 5 Nov 2014 17:13:16 +0800 Subject: [PATCH] refine #128 --- src/local.c | 40 +++++++++++++++++++++------------------- src/shadowsocks.h | 22 ++++++++++++++-------- 2 files changed, 35 insertions(+), 27 deletions(-) diff --git a/src/local.c b/src/local.c index b97744a5..271f67ab 100644 --- a/src/local.c +++ b/src/local.c @@ -1166,31 +1166,33 @@ int main (int argc, char **argv) } #else -int start_ss_service(profile_t profile) +int start_ss_service(profile_t profile, int background) { #ifndef __MINGW32__ - /* Our process ID and Session ID */ - pid_t pid; + if (background) { + /* Our process ID and Session ID */ + pid_t pid; - /* Fork off the parent process */ - pid = fork(); - if (pid < 0) - { - return -1; - } + /* Fork off the parent process */ + pid = fork(); + if (pid < 0) + { + return -1; + } - /* If we got a good PID, then - we can exit the parent process. */ - if (pid > 0) - { - return pid; - } + /* If we got a good PID, then + we can exit the parent process. */ + if (pid > 0) + { + return pid; + } - /* Close out the standard file descriptors */ - close(STDIN_FILENO); - close(STDOUT_FILENO); - close(STDERR_FILENO); + /* Close out the standard file descriptors */ + close(STDIN_FILENO); + close(STDOUT_FILENO); + close(STDERR_FILENO); + } #endif srand(time(NULL)); diff --git a/src/shadowsocks.h b/src/shadowsocks.h index c157da47..7f3240f3 100644 --- a/src/shadowsocks.h +++ b/src/shadowsocks.h @@ -40,8 +40,7 @@ typedef struct { int verbose; // verbose mode } profile_t; -/* - * An example profile +/* An example profile const profile_t EXAMPLE_PROFILE = { .remote_host = "example.com", @@ -57,14 +56,21 @@ const profile_t EXAMPLE_PROFILE = { .udp_relay = 0, .verbose = 0 }; - */ -// Create and start a shadowsocks service, -// If success, return the pid, if not, return -1 -// On win32, this function won't return, -// you have to call it in on other processes -int start_ss_service(profile_t profile); +/* + * Create and start a shadowsocks service. + * + * If background is set to true, the service will run in a child process and + * return the pid if success. Otherwise, calling this function will block the + * current thread forever. + * + * On Win32, background mode is invalid. This function will always block the + * caller and never return. + * + * If failed, -1 is returned. +*/ +int start_ss_service(profile_t profile, int background); // To stop the service on posix system, just kill the daemon process // kill(pid, SIGKILL);