From 3c3c50b4f8b187c422f1aa0e2b0d514a1a57008f Mon Sep 17 00:00:00 2001 From: ksqsf Date: Sun, 11 Feb 2018 00:30:09 +0800 Subject: [PATCH 1/3] Search XDG_CONFIG_HOME for user-specific config --- src/common.h | 2 -- src/local.c | 2 +- src/redir.c | 2 +- src/server.c | 2 +- src/tunnel.c | 2 +- src/utils.c | 28 ++++++++++++++++++++++++++++ src/utils.h | 2 ++ 7 files changed, 34 insertions(+), 6 deletions(-) diff --git a/src/common.h b/src/common.h index 079c367f..761ac502 100644 --- a/src/common.h +++ b/src/common.h @@ -22,8 +22,6 @@ #ifndef _COMMON_H #define _COMMON_H -#define DEFAULT_CONF_PATH "/etc/shadowsocks-libev/config.json" - #ifndef SOL_TCP #define SOL_TCP IPPROTO_TCP #endif diff --git a/src/local.c b/src/local.c index b116461b..b74caac9 100644 --- a/src/local.c +++ b/src/local.c @@ -1424,7 +1424,7 @@ main(int argc, char **argv) if (argc == 1) { if (conf_path == NULL) { - conf_path = DEFAULT_CONF_PATH; + conf_path = get_default_conf(); } } if (conf_path != NULL) { diff --git a/src/redir.c b/src/redir.c index e748aa8e..bb7abe72 100644 --- a/src/redir.c +++ b/src/redir.c @@ -989,7 +989,7 @@ main(int argc, char **argv) if (argc == 1) { if (conf_path == NULL) { - conf_path = DEFAULT_CONF_PATH; + conf_path = get_default_conf(); } } diff --git a/src/server.c b/src/server.c index c8cf3a7d..5f199031 100644 --- a/src/server.c +++ b/src/server.c @@ -1636,7 +1636,7 @@ main(int argc, char **argv) if (argc == 1) { if (conf_path == NULL) { - conf_path = DEFAULT_CONF_PATH; + conf_path = get_default_conf(); } } diff --git a/src/tunnel.c b/src/tunnel.c index ddc10396..2f8b3373 100644 --- a/src/tunnel.c +++ b/src/tunnel.c @@ -921,7 +921,7 @@ main(int argc, char **argv) if (argc == 1) { if (conf_path == NULL) { - conf_path = DEFAULT_CONF_PATH; + conf_path = get_default_conf(); } } diff --git a/src/utils.c b/src/utils.c index af8a871a..a7d63bb9 100644 --- a/src/utils.c +++ b/src/utils.c @@ -474,3 +474,31 @@ set_nofile(int nofile) } #endif + +char * +get_default_conf(void) +{ + static char sysconf[] = "/etc/shadowsocks-libev/config.json"; + static char userconf[PATH_MAX] = { 0 }; + char *conf_home; + + if (userconf[0] != '\0') + return userconf; + + conf_home = getenv("XDG_CONFIG_HOME"); + + if (!conf_home) { + strcpy(userconf, getenv("HOME")); + strcat(userconf, "/.config/shadowsocks-libev/config.json"); + } else { + strcpy(userconf, conf_home); + strcat(userconf, "/shadowsocks-libev/config.json"); + } + + // Check if the user-specific config exists. + if (access(userconf, F_OK) != -1) + return userconf; + + // If not, fall back to the system-wide config. + return sysconf; +} diff --git a/src/utils.h b/src/utils.h index c0b72891..09353b67 100644 --- a/src/utils.h +++ b/src/utils.h @@ -173,4 +173,6 @@ void *ss_realloc(void *ptr, size_t new_size); ptr = NULL; \ } while (0) +char *get_default_conf(void); + #endif // _UTILS_H From 2a3d3bc13c2ccbd6b2e2c613334456c563f60b26 Mon Sep 17 00:00:00 2001 From: ksqsf Date: Sun, 11 Feb 2018 00:35:59 +0800 Subject: [PATCH 2/3] Remove pointless optimization in get_default_conf() --- src/utils.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/utils.c b/src/utils.c index a7d63bb9..26c31816 100644 --- a/src/utils.c +++ b/src/utils.c @@ -482,9 +482,6 @@ get_default_conf(void) static char userconf[PATH_MAX] = { 0 }; char *conf_home; - if (userconf[0] != '\0') - return userconf; - conf_home = getenv("XDG_CONFIG_HOME"); if (!conf_home) { @@ -500,5 +497,6 @@ get_default_conf(void) return userconf; // If not, fall back to the system-wide config. + userconf[0] = '\0'; return sysconf; } From eb501b70c73879f59668607cc1157b87367cbd26 Mon Sep 17 00:00:00 2001 From: ksqsf Date: Sun, 11 Feb 2018 00:53:33 +0800 Subject: [PATCH 3/3] Oops.. Remove useless code. --- src/utils.c | 1 - 1 file changed, 1 deletion(-) diff --git a/src/utils.c b/src/utils.c index 26c31816..b9142e7e 100644 --- a/src/utils.c +++ b/src/utils.c @@ -497,6 +497,5 @@ get_default_conf(void) return userconf; // If not, fall back to the system-wide config. - userconf[0] = '\0'; return sysconf; }