You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

151 lines
4.5 KiB

  1. From a91d70dd37e9555d104629538890d44ef355c772 Mon Sep 17 00:00:00 2001
  2. From: Yousong Zhou <yszhou4tech@gmail.com>
  3. Date: Mon, 26 Jun 2017 14:49:36 +0800
  4. Subject: [PATCH 1/2] decouple use_syslog from pid_flags
  5. Sometimes we need processes to run in the foreground to be supervised
  6. and at the same time use syslog facility instead of logging its stdout,
  7. stderr output
  8. ---
  9. src/jconf.c | 6 ++++++
  10. src/local.c | 2 +-
  11. src/manager.c | 2 +-
  12. src/redir.c | 2 +-
  13. src/server.c | 2 +-
  14. src/tunnel.c | 2 +-
  15. src/utils.h | 18 +++++++++++-------
  16. 7 files changed, 22 insertions(+), 12 deletions(-)
  17. diff --git a/src/jconf.c b/src/jconf.c
  18. index 3c58148..05445c3 100644
  19. --- a/src/jconf.c
  20. +++ b/src/jconf.c
  21. @@ -313,6 +313,12 @@ read_jconf(const char *file)
  22. check_json_value_type(value, json_boolean,
  23. "invalid config file: option 'ipv6_first' must be a boolean");
  24. conf.ipv6_first = value->u.boolean;
  25. +#ifdef HAS_SYSLOG
  26. + } else if (strcmp(name, "use_syslog") == 0) {
  27. + check_json_value_type(value, json_boolean,
  28. + "invalid config file: option 'use_syslog' must be a boolean");
  29. + use_syslog = value->u.boolean;
  30. +#endif
  31. }
  32. }
  33. } else {
  34. diff --git a/src/local.c b/src/local.c
  35. index 78f6d29..e4bd477 100644
  36. --- a/src/local.c
  37. +++ b/src/local.c
  38. @@ -1522,8 +1522,8 @@ main(int argc, char **argv)
  39. local_addr = "127.0.0.1";
  40. }
  41. + USE_SYSLOG(argv[0], pid_flags);
  42. if (pid_flags) {
  43. - USE_SYSLOG(argv[0]);
  44. daemonize(pid_path);
  45. }
  46. diff --git a/src/manager.c b/src/manager.c
  47. index 6e7197c..338ab85 100644
  48. --- a/src/manager.c
  49. +++ b/src/manager.c
  50. @@ -1149,8 +1149,8 @@ main(int argc, char **argv)
  51. timeout = "60";
  52. }
  53. + USE_SYSLOG(argv[0], pid_flags);
  54. if (pid_flags) {
  55. - USE_SYSLOG(argv[0]);
  56. daemonize(pid_path);
  57. }
  58. diff --git a/src/redir.c b/src/redir.c
  59. index 3809411..fae8d54 100644
  60. --- a/src/redir.c
  61. +++ b/src/redir.c
  62. @@ -1140,8 +1140,8 @@ main(int argc, char **argv)
  63. #endif
  64. }
  65. + USE_SYSLOG(argv[0], pid_flags);
  66. if (pid_flags) {
  67. - USE_SYSLOG(argv[0]);
  68. daemonize(pid_path);
  69. }
  70. diff --git a/src/server.c b/src/server.c
  71. index 534dbd8..1c25c74 100644
  72. --- a/src/server.c
  73. +++ b/src/server.c
  74. @@ -1726,8 +1726,8 @@ main(int argc, char **argv)
  75. }
  76. #endif
  77. + USE_SYSLOG(argv[0], pid_flags);
  78. if (pid_flags) {
  79. - USE_SYSLOG(argv[0]);
  80. daemonize(pid_path);
  81. }
  82. diff --git a/src/tunnel.c b/src/tunnel.c
  83. index 77c7380..2419fa0 100644
  84. --- a/src/tunnel.c
  85. +++ b/src/tunnel.c
  86. @@ -1022,8 +1022,8 @@ main(int argc, char **argv)
  87. local_addr = "127.0.0.1";
  88. }
  89. + USE_SYSLOG(argv[0], pid_flags);
  90. if (pid_flags) {
  91. - USE_SYSLOG(argv[0]);
  92. daemonize(pid_path);
  93. }
  94. diff --git a/src/utils.h b/src/utils.h
  95. index 2603e85..53f3983 100644
  96. --- a/src/utils.h
  97. +++ b/src/utils.h
  98. @@ -35,7 +35,7 @@
  99. #include <android/log.h>
  100. #define USE_TTY()
  101. -#define USE_SYSLOG(ident)
  102. +#define USE_SYSLOG(ident, _cond)
  103. #define LOGI(...) \
  104. ((void)__android_log_print(ANDROID_LOG_DEBUG, "shadowsocks", \
  105. __VA_ARGS__))
  106. @@ -53,7 +53,7 @@
  107. extern FILE *logfile;
  108. #define TIME_FORMAT "%Y-%m-%d %H:%M:%S"
  109. #define USE_TTY()
  110. -#define USE_SYSLOG(ident)
  111. +#define USE_SYSLOG(ident, _cond)
  112. #define USE_LOGFILE(ident) \
  113. do { \
  114. if (ident != NULL) { logfile = fopen(ident, "w+"); } } \
  115. @@ -99,11 +99,15 @@ extern int use_syslog;
  116. use_tty = isatty(STDERR_FILENO); \
  117. } while (0)
  118. -#define USE_SYSLOG(ident) \
  119. - do { \
  120. - use_syslog = 1; \
  121. - openlog((ident), LOG_CONS | LOG_PID, 0); } \
  122. - while (0)
  123. +#define USE_SYSLOG(_ident, _cond) \
  124. + do { \
  125. + if (!use_syslog && (_cond)) { \
  126. + use_syslog = 1; \
  127. + } \
  128. + if (use_syslog) { \
  129. + openlog((_ident), LOG_CONS | LOG_PID, 0); \
  130. + } \
  131. + } while (0)
  132. #define LOGI(format, ...) \
  133. do { \
  134. --
  135. 2.12.2