summaryrefslogtreecommitdiff
path: root/other/bootstrap_daemon
diff options
context:
space:
mode:
authorMaxim Biro <nurupo.contributions@gmail.com>2017-03-02 02:38:57 -0500
committerMaxim Biro <nurupo.contributions@gmail.com>2017-06-04 16:07:03 -0400
commitb0aec02225b642b2e420e634dce919beee0cd0f2 (patch)
treedf290f8e2f76efda437f1455411f576b3f9f7766 /other/bootstrap_daemon
parent1e8fa85aadf602bdca3a540de09a8184f7139a6c (diff)
Split daemon's logging backends in separate modules
Diffstat (limited to 'other/bootstrap_daemon')
-rw-r--r--other/bootstrap_daemon/src/Makefile.inc7
-rw-r--r--other/bootstrap_daemon/src/command_line_arguments.c14
-rw-r--r--other/bootstrap_daemon/src/config.c102
-rw-r--r--other/bootstrap_daemon/src/log.c106
-rw-r--r--other/bootstrap_daemon/src/log.h10
-rw-r--r--other/bootstrap_daemon/src/log_backend_stdout.c47
-rw-r--r--other/bootstrap_daemon/src/log_backend_stdout.h34
-rw-r--r--other/bootstrap_daemon/src/log_backend_syslog.c79
-rw-r--r--other/bootstrap_daemon/src/log_backend_syslog.h36
-rw-r--r--other/bootstrap_daemon/src/tox-bootstrapd.c61
10 files changed, 325 insertions, 171 deletions
diff --git a/other/bootstrap_daemon/src/Makefile.inc b/other/bootstrap_daemon/src/Makefile.inc
index 29b2822e..1c313d44 100644
--- a/other/bootstrap_daemon/src/Makefile.inc
+++ b/other/bootstrap_daemon/src/Makefile.inc
@@ -6,10 +6,15 @@ tox_bootstrapd_SOURCES = \
6 ../other/bootstrap_daemon/src/command_line_arguments.c \ 6 ../other/bootstrap_daemon/src/command_line_arguments.c \
7 ../other/bootstrap_daemon/src/command_line_arguments.h \ 7 ../other/bootstrap_daemon/src/command_line_arguments.h \
8 ../other/bootstrap_daemon/src/config.c \ 8 ../other/bootstrap_daemon/src/config.c \
9 ../other/bootstrap_daemon/src/config_defaults.h \
10 ../other/bootstrap_daemon/src/config.h \ 9 ../other/bootstrap_daemon/src/config.h \
10 ../other/bootstrap_daemon/src/config_defaults.h \
11 ../other/bootstrap_daemon/src/global.h \
11 ../other/bootstrap_daemon/src/log.c \ 12 ../other/bootstrap_daemon/src/log.c \
12 ../other/bootstrap_daemon/src/log.h \ 13 ../other/bootstrap_daemon/src/log.h \
14 ../other/bootstrap_daemon/src/log_backend_stdout.c \
15 ../other/bootstrap_daemon/src/log_backend_stdout.h \
16 ../other/bootstrap_daemon/src/log_backend_syslog.c \
17 ../other/bootstrap_daemon/src/log_backend_syslog.h \
13 ../other/bootstrap_daemon/src/tox-bootstrapd.c \ 18 ../other/bootstrap_daemon/src/tox-bootstrapd.c \
14 ../other/bootstrap_daemon/src/global.h \ 19 ../other/bootstrap_daemon/src/global.h \
15 ../other/bootstrap_node_packets.c \ 20 ../other/bootstrap_node_packets.c \
diff --git a/other/bootstrap_daemon/src/command_line_arguments.c b/other/bootstrap_daemon/src/command_line_arguments.c
index bf7a0f8c..dce828a7 100644
--- a/other/bootstrap_daemon/src/command_line_arguments.c
+++ b/other/bootstrap_daemon/src/command_line_arguments.c
@@ -40,7 +40,7 @@ static void print_help(void)
40 // 2 space ident 40 // 2 space ident
41 // make sure all lines fit into 80 columns 41 // make sure all lines fit into 80 columns
42 // make sure options are listed in alphabetical order 42 // make sure options are listed in alphabetical order
43 write_log(LOG_LEVEL_INFO, 43 log_write(LOG_LEVEL_INFO,
44 "Usage: tox-bootstrapd [OPTION]... --config=FILE_PATH\n" 44 "Usage: tox-bootstrapd [OPTION]... --config=FILE_PATH\n"
45 "\n" 45 "\n"
46 "Options:\n" 46 "Options:\n"
@@ -64,7 +64,7 @@ void handle_command_line_arguments(int argc, char *argv[], char **cfg_file_path,
64 bool *run_in_foreground) 64 bool *run_in_foreground)
65{ 65{
66 if (argc < 2) { 66 if (argc < 2) {
67 write_log(LOG_LEVEL_ERROR, "Error: No arguments provided.\n\n"); 67 log_write(LOG_LEVEL_ERROR, "Error: No arguments provided.\n\n");
68 print_help(); 68 print_help();
69 exit(1); 69 exit(1);
70 } 70 }
@@ -112,7 +112,7 @@ void handle_command_line_arguments(int argc, char *argv[], char **cfg_file_path,
112 *log_backend = LOG_BACKEND_STDOUT; 112 *log_backend = LOG_BACKEND_STDOUT;
113 log_backend_set = true; 113 log_backend_set = true;
114 } else { 114 } else {
115 write_log(LOG_LEVEL_ERROR, "Error: Invalid BACKEND value for --log-backend option passed: %s\n\n", optarg); 115 log_write(LOG_LEVEL_ERROR, "Error: Invalid BACKEND value for --log-backend option passed: %s\n\n", optarg);
116 print_help(); 116 print_help();
117 exit(1); 117 exit(1);
118 } 118 }
@@ -120,16 +120,16 @@ void handle_command_line_arguments(int argc, char *argv[], char **cfg_file_path,
120 break; 120 break;
121 121
122 case 'v': 122 case 'v':
123 write_log(LOG_LEVEL_INFO, "Version: %lu\n", DAEMON_VERSION_NUMBER); 123 log_write(LOG_LEVEL_INFO, "Version: %lu\n", DAEMON_VERSION_NUMBER);
124 exit(0); 124 exit(0);
125 125
126 case '?': 126 case '?':
127 write_log(LOG_LEVEL_ERROR, "Error: Unrecognized option %s\n\n", argv[optind - 1]); 127 log_write(LOG_LEVEL_ERROR, "Error: Unrecognized option %s\n\n", argv[optind - 1]);
128 print_help(); 128 print_help();
129 exit(1); 129 exit(1);
130 130
131 case ':': 131 case ':':
132 write_log(LOG_LEVEL_ERROR, "Error: No argument provided for option %s\n\n", argv[optind - 1]); 132 log_write(LOG_LEVEL_ERROR, "Error: No argument provided for option %s\n\n", argv[optind - 1]);
133 print_help(); 133 print_help();
134 exit(1); 134 exit(1);
135 } 135 }
@@ -140,7 +140,7 @@ void handle_command_line_arguments(int argc, char *argv[], char **cfg_file_path,
140 } 140 }
141 141
142 if (!cfg_file_path_set) { 142 if (!cfg_file_path_set) {
143 write_log(LOG_LEVEL_ERROR, "Error: The required --config option wasn't specified\n\n"); 143 log_write(LOG_LEVEL_ERROR, "Error: The required --config option wasn't specified\n\n");
144 print_help(); 144 print_help();
145 exit(1); 145 exit(1);
146 } 146 }
diff --git a/other/bootstrap_daemon/src/config.c b/other/bootstrap_daemon/src/config.c
index b45b05f9..64fc4d94 100644
--- a/other/bootstrap_daemon/src/config.c
+++ b/other/bootstrap_daemon/src/config.c
@@ -50,15 +50,15 @@ static void parse_tcp_relay_ports_config(config_t *cfg, uint16_t **tcp_relay_por
50 config_setting_t *ports_array = config_lookup(cfg, NAME_TCP_RELAY_PORTS); 50 config_setting_t *ports_array = config_lookup(cfg, NAME_TCP_RELAY_PORTS);
51 51
52 if (ports_array == NULL) { 52 if (ports_array == NULL) {
53 write_log(LOG_LEVEL_WARNING, "No '%s' setting in the configuration file.\n", NAME_TCP_RELAY_PORTS); 53 log_write(LOG_LEVEL_WARNING, "No '%s' setting in the configuration file.\n", NAME_TCP_RELAY_PORTS);
54 write_log(LOG_LEVEL_WARNING, "Using default '%s':\n", NAME_TCP_RELAY_PORTS); 54 log_write(LOG_LEVEL_WARNING, "Using default '%s':\n", NAME_TCP_RELAY_PORTS);
55 55
56 uint16_t default_ports[DEFAULT_TCP_RELAY_PORTS_COUNT] = {DEFAULT_TCP_RELAY_PORTS}; 56 uint16_t default_ports[DEFAULT_TCP_RELAY_PORTS_COUNT] = {DEFAULT_TCP_RELAY_PORTS};
57 57
58 int i; 58 int i;
59 59
60 for (i = 0; i < DEFAULT_TCP_RELAY_PORTS_COUNT; i ++) { 60 for (i = 0; i < DEFAULT_TCP_RELAY_PORTS_COUNT; i ++) {
61 write_log(LOG_LEVEL_INFO, "Port #%d: %u\n", i, default_ports[i]); 61 log_write(LOG_LEVEL_INFO, "Port #%d: %u\n", i, default_ports[i]);
62 } 62 }
63 63
64 // similar procedure to the one of reading config file below 64 // similar procedure to the one of reading config file below
@@ -70,7 +70,7 @@ static void parse_tcp_relay_ports_config(config_t *cfg, uint16_t **tcp_relay_por
70 70
71 if ((*tcp_relay_ports)[*tcp_relay_port_count] < MIN_ALLOWED_PORT 71 if ((*tcp_relay_ports)[*tcp_relay_port_count] < MIN_ALLOWED_PORT
72 || (*tcp_relay_ports)[*tcp_relay_port_count] > MAX_ALLOWED_PORT) { 72 || (*tcp_relay_ports)[*tcp_relay_port_count] > MAX_ALLOWED_PORT) {
73 write_log(LOG_LEVEL_WARNING, "Port #%d: Invalid port: %u, should be in [%d, %d]. Skipping.\n", i, 73 log_write(LOG_LEVEL_WARNING, "Port #%d: Invalid port: %u, should be in [%d, %d]. Skipping.\n", i,
74 (*tcp_relay_ports)[*tcp_relay_port_count], MIN_ALLOWED_PORT, MAX_ALLOWED_PORT); 74 (*tcp_relay_ports)[*tcp_relay_port_count], MIN_ALLOWED_PORT, MAX_ALLOWED_PORT);
75 continue; 75 continue;
76 } 76 }
@@ -90,7 +90,7 @@ static void parse_tcp_relay_ports_config(config_t *cfg, uint16_t **tcp_relay_por
90 } 90 }
91 91
92 if (config_setting_is_array(ports_array) == CONFIG_FALSE) { 92 if (config_setting_is_array(ports_array) == CONFIG_FALSE) {
93 write_log(LOG_LEVEL_ERROR, "'%s' setting should be an array. Array syntax: 'setting = [value1, value2, ...]'.\n", 93 log_write(LOG_LEVEL_ERROR, "'%s' setting should be an array. Array syntax: 'setting = [value1, value2, ...]'.\n",
94 NAME_TCP_RELAY_PORTS); 94 NAME_TCP_RELAY_PORTS);
95 return; 95 return;
96 } 96 }
@@ -98,7 +98,7 @@ static void parse_tcp_relay_ports_config(config_t *cfg, uint16_t **tcp_relay_por
98 int config_port_count = config_setting_length(ports_array); 98 int config_port_count = config_setting_length(ports_array);
99 99
100 if (config_port_count == 0) { 100 if (config_port_count == 0) {
101 write_log(LOG_LEVEL_ERROR, "'%s' is empty.\n", NAME_TCP_RELAY_PORTS); 101 log_write(LOG_LEVEL_ERROR, "'%s' is empty.\n", NAME_TCP_RELAY_PORTS);
102 return; 102 return;
103 } 103 }
104 104
@@ -111,12 +111,12 @@ static void parse_tcp_relay_ports_config(config_t *cfg, uint16_t **tcp_relay_por
111 111
112 if (elem == NULL) { 112 if (elem == NULL) {
113 // it's NULL if `ports_array` is not an array (we have that check earlier) or if `i` is out of range, which should not be 113 // it's NULL if `ports_array` is not an array (we have that check earlier) or if `i` is out of range, which should not be
114 write_log(LOG_LEVEL_WARNING, "Port #%d: Something went wrong while parsing the port. Stopping reading ports.\n", i); 114 log_write(LOG_LEVEL_WARNING, "Port #%d: Something went wrong while parsing the port. Stopping reading ports.\n", i);
115 break; 115 break;
116 } 116 }
117 117
118 if (config_setting_is_number(elem) == CONFIG_FALSE) { 118 if (config_setting_is_number(elem) == CONFIG_FALSE) {
119 write_log(LOG_LEVEL_WARNING, "Port #%d: Not a number. Skipping.\n", i); 119 log_write(LOG_LEVEL_WARNING, "Port #%d: Not a number. Skipping.\n", i);
120 continue; 120 continue;
121 } 121 }
122 122
@@ -124,7 +124,7 @@ static void parse_tcp_relay_ports_config(config_t *cfg, uint16_t **tcp_relay_por
124 124
125 if ((*tcp_relay_ports)[*tcp_relay_port_count] < MIN_ALLOWED_PORT 125 if ((*tcp_relay_ports)[*tcp_relay_port_count] < MIN_ALLOWED_PORT
126 || (*tcp_relay_ports)[*tcp_relay_port_count] > MAX_ALLOWED_PORT) { 126 || (*tcp_relay_ports)[*tcp_relay_port_count] > MAX_ALLOWED_PORT) {
127 write_log(LOG_LEVEL_WARNING, "Port #%d: Invalid port: %u, should be in [%d, %d]. Skipping.\n", i, 127 log_write(LOG_LEVEL_WARNING, "Port #%d: Invalid port: %u, should be in [%d, %d]. Skipping.\n", i,
128 (*tcp_relay_ports)[*tcp_relay_port_count], MIN_ALLOWED_PORT, MAX_ALLOWED_PORT); 128 (*tcp_relay_ports)[*tcp_relay_port_count], MIN_ALLOWED_PORT, MAX_ALLOWED_PORT);
129 continue; 129 continue;
130 } 130 }
@@ -161,15 +161,15 @@ int get_general_config(const char *cfg_file_path, char **pid_file_path, char **k
161 161
162 // Read the file. If there is an error, report it and exit. 162 // Read the file. If there is an error, report it and exit.
163 if (config_read_file(&cfg, cfg_file_path) == CONFIG_FALSE) { 163 if (config_read_file(&cfg, cfg_file_path) == CONFIG_FALSE) {
164 write_log(LOG_LEVEL_ERROR, "%s:%d - %s\n", config_error_file(&cfg), config_error_line(&cfg), config_error_text(&cfg)); 164 log_write(LOG_LEVEL_ERROR, "%s:%d - %s\n", config_error_file(&cfg), config_error_line(&cfg), config_error_text(&cfg));
165 config_destroy(&cfg); 165 config_destroy(&cfg);
166 return 0; 166 return 0;
167 } 167 }
168 168
169 // Get port 169 // Get port
170 if (config_lookup_int(&cfg, NAME_PORT, port) == CONFIG_FALSE) { 170 if (config_lookup_int(&cfg, NAME_PORT, port) == CONFIG_FALSE) {
171 write_log(LOG_LEVEL_WARNING, "No '%s' setting in configuration file.\n", NAME_PORT); 171 log_write(LOG_LEVEL_WARNING, "No '%s' setting in configuration file.\n", NAME_PORT);
172 write_log(LOG_LEVEL_WARNING, "Using default '%s': %d\n", NAME_PORT, DEFAULT_PORT); 172 log_write(LOG_LEVEL_WARNING, "Using default '%s': %d\n", NAME_PORT, DEFAULT_PORT);
173 *port = DEFAULT_PORT; 173 *port = DEFAULT_PORT;
174 } 174 }
175 175
@@ -177,8 +177,8 @@ int get_general_config(const char *cfg_file_path, char **pid_file_path, char **k
177 const char *tmp_pid_file; 177 const char *tmp_pid_file;
178 178
179 if (config_lookup_string(&cfg, NAME_PID_FILE_PATH, &tmp_pid_file) == CONFIG_FALSE) { 179 if (config_lookup_string(&cfg, NAME_PID_FILE_PATH, &tmp_pid_file) == CONFIG_FALSE) {
180 write_log(LOG_LEVEL_WARNING, "No '%s' setting in configuration file.\n", NAME_PID_FILE_PATH); 180 log_write(LOG_LEVEL_WARNING, "No '%s' setting in configuration file.\n", NAME_PID_FILE_PATH);
181 write_log(LOG_LEVEL_WARNING, "Using default '%s': %s\n", NAME_PID_FILE_PATH, DEFAULT_PID_FILE_PATH); 181 log_write(LOG_LEVEL_WARNING, "Using default '%s': %s\n", NAME_PID_FILE_PATH, DEFAULT_PID_FILE_PATH);
182 tmp_pid_file = DEFAULT_PID_FILE_PATH; 182 tmp_pid_file = DEFAULT_PID_FILE_PATH;
183 } 183 }
184 184
@@ -189,8 +189,8 @@ int get_general_config(const char *cfg_file_path, char **pid_file_path, char **k
189 const char *tmp_keys_file; 189 const char *tmp_keys_file;
190 190
191 if (config_lookup_string(&cfg, NAME_KEYS_FILE_PATH, &tmp_keys_file) == CONFIG_FALSE) { 191 if (config_lookup_string(&cfg, NAME_KEYS_FILE_PATH, &tmp_keys_file) == CONFIG_FALSE) {
192 write_log(LOG_LEVEL_WARNING, "No '%s' setting in configuration file.\n", NAME_KEYS_FILE_PATH); 192 log_write(LOG_LEVEL_WARNING, "No '%s' setting in configuration file.\n", NAME_KEYS_FILE_PATH);
193 write_log(LOG_LEVEL_WARNING, "Using default '%s': %s\n", NAME_KEYS_FILE_PATH, DEFAULT_KEYS_FILE_PATH); 193 log_write(LOG_LEVEL_WARNING, "Using default '%s': %s\n", NAME_KEYS_FILE_PATH, DEFAULT_KEYS_FILE_PATH);
194 tmp_keys_file = DEFAULT_KEYS_FILE_PATH; 194 tmp_keys_file = DEFAULT_KEYS_FILE_PATH;
195 } 195 }
196 196
@@ -199,31 +199,31 @@ int get_general_config(const char *cfg_file_path, char **pid_file_path, char **k
199 199
200 // Get IPv6 option 200 // Get IPv6 option
201 if (config_lookup_bool(&cfg, NAME_ENABLE_IPV6, enable_ipv6) == CONFIG_FALSE) { 201 if (config_lookup_bool(&cfg, NAME_ENABLE_IPV6, enable_ipv6) == CONFIG_FALSE) {
202 write_log(LOG_LEVEL_WARNING, "No '%s' setting in configuration file.\n", NAME_ENABLE_IPV6); 202 log_write(LOG_LEVEL_WARNING, "No '%s' setting in configuration file.\n", NAME_ENABLE_IPV6);
203 write_log(LOG_LEVEL_WARNING, "Using default '%s': %s\n", NAME_ENABLE_IPV6, DEFAULT_ENABLE_IPV6 ? "true" : "false"); 203 log_write(LOG_LEVEL_WARNING, "Using default '%s': %s\n", NAME_ENABLE_IPV6, DEFAULT_ENABLE_IPV6 ? "true" : "false");
204 *enable_ipv6 = DEFAULT_ENABLE_IPV6; 204 *enable_ipv6 = DEFAULT_ENABLE_IPV6;
205 } 205 }
206 206
207 // Get IPv4 fallback option 207 // Get IPv4 fallback option
208 if (config_lookup_bool(&cfg, NAME_ENABLE_IPV4_FALLBACK, enable_ipv4_fallback) == CONFIG_FALSE) { 208 if (config_lookup_bool(&cfg, NAME_ENABLE_IPV4_FALLBACK, enable_ipv4_fallback) == CONFIG_FALSE) {
209 write_log(LOG_LEVEL_WARNING, "No '%s' setting in configuration file.\n", NAME_ENABLE_IPV4_FALLBACK); 209 log_write(LOG_LEVEL_WARNING, "No '%s' setting in configuration file.\n", NAME_ENABLE_IPV4_FALLBACK);
210 write_log(LOG_LEVEL_WARNING, "Using default '%s': %s\n", NAME_ENABLE_IPV4_FALLBACK, 210 log_write(LOG_LEVEL_WARNING, "Using default '%s': %s\n", NAME_ENABLE_IPV4_FALLBACK,
211 DEFAULT_ENABLE_IPV4_FALLBACK ? "true" : "false"); 211 DEFAULT_ENABLE_IPV4_FALLBACK ? "true" : "false");
212 *enable_ipv4_fallback = DEFAULT_ENABLE_IPV4_FALLBACK; 212 *enable_ipv4_fallback = DEFAULT_ENABLE_IPV4_FALLBACK;
213 } 213 }
214 214
215 // Get LAN discovery option 215 // Get LAN discovery option
216 if (config_lookup_bool(&cfg, NAME_ENABLE_LAN_DISCOVERY, enable_lan_discovery) == CONFIG_FALSE) { 216 if (config_lookup_bool(&cfg, NAME_ENABLE_LAN_DISCOVERY, enable_lan_discovery) == CONFIG_FALSE) {
217 write_log(LOG_LEVEL_WARNING, "No '%s' setting in configuration file.\n", NAME_ENABLE_LAN_DISCOVERY); 217 log_write(LOG_LEVEL_WARNING, "No '%s' setting in configuration file.\n", NAME_ENABLE_LAN_DISCOVERY);
218 write_log(LOG_LEVEL_WARNING, "Using default '%s': %s\n", NAME_ENABLE_LAN_DISCOVERY, 218 log_write(LOG_LEVEL_WARNING, "Using default '%s': %s\n", NAME_ENABLE_LAN_DISCOVERY,
219 DEFAULT_ENABLE_LAN_DISCOVERY ? "true" : "false"); 219 DEFAULT_ENABLE_LAN_DISCOVERY ? "true" : "false");
220 *enable_lan_discovery = DEFAULT_ENABLE_LAN_DISCOVERY; 220 *enable_lan_discovery = DEFAULT_ENABLE_LAN_DISCOVERY;
221 } 221 }
222 222
223 // Get TCP relay option 223 // Get TCP relay option
224 if (config_lookup_bool(&cfg, NAME_ENABLE_TCP_RELAY, enable_tcp_relay) == CONFIG_FALSE) { 224 if (config_lookup_bool(&cfg, NAME_ENABLE_TCP_RELAY, enable_tcp_relay) == CONFIG_FALSE) {
225 write_log(LOG_LEVEL_WARNING, "No '%s' setting in configuration file.\n", NAME_ENABLE_TCP_RELAY); 225 log_write(LOG_LEVEL_WARNING, "No '%s' setting in configuration file.\n", NAME_ENABLE_TCP_RELAY);
226 write_log(LOG_LEVEL_WARNING, "Using default '%s': %s\n", NAME_ENABLE_TCP_RELAY, 226 log_write(LOG_LEVEL_WARNING, "Using default '%s': %s\n", NAME_ENABLE_TCP_RELAY,
227 DEFAULT_ENABLE_TCP_RELAY ? "true" : "false"); 227 DEFAULT_ENABLE_TCP_RELAY ? "true" : "false");
228 *enable_tcp_relay = DEFAULT_ENABLE_TCP_RELAY; 228 *enable_tcp_relay = DEFAULT_ENABLE_TCP_RELAY;
229 } 229 }
@@ -236,8 +236,8 @@ int get_general_config(const char *cfg_file_path, char **pid_file_path, char **k
236 236
237 // Get MOTD option 237 // Get MOTD option
238 if (config_lookup_bool(&cfg, NAME_ENABLE_MOTD, enable_motd) == CONFIG_FALSE) { 238 if (config_lookup_bool(&cfg, NAME_ENABLE_MOTD, enable_motd) == CONFIG_FALSE) {
239 write_log(LOG_LEVEL_WARNING, "No '%s' setting in configuration file.\n", NAME_ENABLE_MOTD); 239 log_write(LOG_LEVEL_WARNING, "No '%s' setting in configuration file.\n", NAME_ENABLE_MOTD);
240 write_log(LOG_LEVEL_WARNING, "Using default '%s': %s\n", NAME_ENABLE_MOTD, 240 log_write(LOG_LEVEL_WARNING, "Using default '%s': %s\n", NAME_ENABLE_MOTD,
241 DEFAULT_ENABLE_MOTD ? "true" : "false"); 241 DEFAULT_ENABLE_MOTD ? "true" : "false");
242 *enable_motd = DEFAULT_ENABLE_MOTD; 242 *enable_motd = DEFAULT_ENABLE_MOTD;
243 } 243 }
@@ -247,8 +247,8 @@ int get_general_config(const char *cfg_file_path, char **pid_file_path, char **k
247 const char *tmp_motd; 247 const char *tmp_motd;
248 248
249 if (config_lookup_string(&cfg, NAME_MOTD, &tmp_motd) == CONFIG_FALSE) { 249 if (config_lookup_string(&cfg, NAME_MOTD, &tmp_motd) == CONFIG_FALSE) {
250 write_log(LOG_LEVEL_WARNING, "No '%s' setting in configuration file.\n", NAME_MOTD); 250 log_write(LOG_LEVEL_WARNING, "No '%s' setting in configuration file.\n", NAME_MOTD);
251 write_log(LOG_LEVEL_WARNING, "Using default '%s': %s\n", NAME_MOTD, DEFAULT_MOTD); 251 log_write(LOG_LEVEL_WARNING, "Using default '%s': %s\n", NAME_MOTD, DEFAULT_MOTD);
252 tmp_motd = DEFAULT_MOTD; 252 tmp_motd = DEFAULT_MOTD;
253 } 253 }
254 254
@@ -261,34 +261,34 @@ int get_general_config(const char *cfg_file_path, char **pid_file_path, char **k
261 261
262 config_destroy(&cfg); 262 config_destroy(&cfg);
263 263
264 write_log(LOG_LEVEL_INFO, "Successfully read:\n"); 264 log_write(LOG_LEVEL_INFO, "Successfully read:\n");
265 write_log(LOG_LEVEL_INFO, "'%s': %s\n", NAME_PID_FILE_PATH, *pid_file_path); 265 log_write(LOG_LEVEL_INFO, "'%s': %s\n", NAME_PID_FILE_PATH, *pid_file_path);
266 write_log(LOG_LEVEL_INFO, "'%s': %s\n", NAME_KEYS_FILE_PATH, *keys_file_path); 266 log_write(LOG_LEVEL_INFO, "'%s': %s\n", NAME_KEYS_FILE_PATH, *keys_file_path);
267 write_log(LOG_LEVEL_INFO, "'%s': %d\n", NAME_PORT, *port); 267 log_write(LOG_LEVEL_INFO, "'%s': %d\n", NAME_PORT, *port);
268 write_log(LOG_LEVEL_INFO, "'%s': %s\n", NAME_ENABLE_IPV6, *enable_ipv6 ? "true" : "false"); 268 log_write(LOG_LEVEL_INFO, "'%s': %s\n", NAME_ENABLE_IPV6, *enable_ipv6 ? "true" : "false");
269 write_log(LOG_LEVEL_INFO, "'%s': %s\n", NAME_ENABLE_IPV4_FALLBACK, *enable_ipv4_fallback ? "true" : "false"); 269 log_write(LOG_LEVEL_INFO, "'%s': %s\n", NAME_ENABLE_IPV4_FALLBACK, *enable_ipv4_fallback ? "true" : "false");
270 write_log(LOG_LEVEL_INFO, "'%s': %s\n", NAME_ENABLE_LAN_DISCOVERY, *enable_lan_discovery ? "true" : "false"); 270 log_write(LOG_LEVEL_INFO, "'%s': %s\n", NAME_ENABLE_LAN_DISCOVERY, *enable_lan_discovery ? "true" : "false");
271 271
272 write_log(LOG_LEVEL_INFO, "'%s': %s\n", NAME_ENABLE_TCP_RELAY, *enable_tcp_relay ? "true" : "false"); 272 log_write(LOG_LEVEL_INFO, "'%s': %s\n", NAME_ENABLE_TCP_RELAY, *enable_tcp_relay ? "true" : "false");
273 273
274 // show info about tcp ports only if tcp relay is enabled 274 // show info about tcp ports only if tcp relay is enabled
275 if (*enable_tcp_relay) { 275 if (*enable_tcp_relay) {
276 if (*tcp_relay_port_count == 0) { 276 if (*tcp_relay_port_count == 0) {
277 write_log(LOG_LEVEL_ERROR, "No TCP ports could be read.\n"); 277 log_write(LOG_LEVEL_ERROR, "No TCP ports could be read.\n");
278 } else { 278 } else {
279 write_log(LOG_LEVEL_INFO, "Read %d TCP ports:\n", *tcp_relay_port_count); 279 log_write(LOG_LEVEL_INFO, "Read %d TCP ports:\n", *tcp_relay_port_count);
280 int i; 280 int i;
281 281
282 for (i = 0; i < *tcp_relay_port_count; i ++) { 282 for (i = 0; i < *tcp_relay_port_count; i ++) {
283 write_log(LOG_LEVEL_INFO, "Port #%d: %u\n", i, (*tcp_relay_ports)[i]); 283 log_write(LOG_LEVEL_INFO, "Port #%d: %u\n", i, (*tcp_relay_ports)[i]);
284 } 284 }
285 } 285 }
286 } 286 }
287 287
288 write_log(LOG_LEVEL_INFO, "'%s': %s\n", NAME_ENABLE_MOTD, *enable_motd ? "true" : "false"); 288 log_write(LOG_LEVEL_INFO, "'%s': %s\n", NAME_ENABLE_MOTD, *enable_motd ? "true" : "false");
289 289
290 if (*enable_motd) { 290 if (*enable_motd) {
291 write_log(LOG_LEVEL_INFO, "'%s': %s\n", NAME_MOTD, *motd); 291 log_write(LOG_LEVEL_INFO, "'%s': %s\n", NAME_MOTD, *motd);
292 } 292 }
293 293
294 return 1; 294 return 1;
@@ -335,7 +335,7 @@ int bootstrap_from_config(const char *cfg_file_path, DHT *dht, int enable_ipv6)
335 config_init(&cfg); 335 config_init(&cfg);
336 336
337 if (config_read_file(&cfg, cfg_file_path) == CONFIG_FALSE) { 337 if (config_read_file(&cfg, cfg_file_path) == CONFIG_FALSE) {
338 write_log(LOG_LEVEL_ERROR, "%s:%d - %s\n", config_error_file(&cfg), config_error_line(&cfg), config_error_text(&cfg)); 338 log_write(LOG_LEVEL_ERROR, "%s:%d - %s\n", config_error_file(&cfg), config_error_line(&cfg), config_error_text(&cfg));
339 config_destroy(&cfg); 339 config_destroy(&cfg);
340 return 0; 340 return 0;
341 } 341 }
@@ -343,14 +343,14 @@ int bootstrap_from_config(const char *cfg_file_path, DHT *dht, int enable_ipv6)
343 config_setting_t *node_list = config_lookup(&cfg, NAME_BOOTSTRAP_NODES); 343 config_setting_t *node_list = config_lookup(&cfg, NAME_BOOTSTRAP_NODES);
344 344
345 if (node_list == NULL) { 345 if (node_list == NULL) {
346 write_log(LOG_LEVEL_WARNING, "No '%s' setting in the configuration file. Skipping bootstrapping.\n", 346 log_write(LOG_LEVEL_WARNING, "No '%s' setting in the configuration file. Skipping bootstrapping.\n",
347 NAME_BOOTSTRAP_NODES); 347 NAME_BOOTSTRAP_NODES);
348 config_destroy(&cfg); 348 config_destroy(&cfg);
349 return 1; 349 return 1;
350 } 350 }
351 351
352 if (config_setting_length(node_list) == 0) { 352 if (config_setting_length(node_list) == 0) {
353 write_log(LOG_LEVEL_WARNING, "No bootstrap nodes found. Skipping bootstrapping.\n"); 353 log_write(LOG_LEVEL_WARNING, "No bootstrap nodes found. Skipping bootstrapping.\n");
354 config_destroy(&cfg); 354 config_destroy(&cfg);
355 return 1; 355 return 1;
356 } 356 }
@@ -376,30 +376,30 @@ int bootstrap_from_config(const char *cfg_file_path, DHT *dht, int enable_ipv6)
376 376
377 // Check that all settings are present 377 // Check that all settings are present
378 if (config_setting_lookup_string(node, NAME_PUBLIC_KEY, &bs_public_key) == CONFIG_FALSE) { 378 if (config_setting_lookup_string(node, NAME_PUBLIC_KEY, &bs_public_key) == CONFIG_FALSE) {
379 write_log(LOG_LEVEL_WARNING, "Bootstrap node #%d: Couldn't find '%s' setting. Skipping the node.\n", i, 379 log_write(LOG_LEVEL_WARNING, "Bootstrap node #%d: Couldn't find '%s' setting. Skipping the node.\n", i,
380 NAME_PUBLIC_KEY); 380 NAME_PUBLIC_KEY);
381 goto next; 381 goto next;
382 } 382 }
383 383
384 if (config_setting_lookup_int(node, NAME_PORT, &bs_port) == CONFIG_FALSE) { 384 if (config_setting_lookup_int(node, NAME_PORT, &bs_port) == CONFIG_FALSE) {
385 write_log(LOG_LEVEL_WARNING, "Bootstrap node #%d: Couldn't find '%s' setting. Skipping the node.\n", i, NAME_PORT); 385 log_write(LOG_LEVEL_WARNING, "Bootstrap node #%d: Couldn't find '%s' setting. Skipping the node.\n", i, NAME_PORT);
386 goto next; 386 goto next;
387 } 387 }
388 388
389 if (config_setting_lookup_string(node, NAME_ADDRESS, &bs_address) == CONFIG_FALSE) { 389 if (config_setting_lookup_string(node, NAME_ADDRESS, &bs_address) == CONFIG_FALSE) {
390 write_log(LOG_LEVEL_WARNING, "Bootstrap node #%d: Couldn't find '%s' setting. Skipping the node.\n", i, NAME_ADDRESS); 390 log_write(LOG_LEVEL_WARNING, "Bootstrap node #%d: Couldn't find '%s' setting. Skipping the node.\n", i, NAME_ADDRESS);
391 goto next; 391 goto next;
392 } 392 }
393 393
394 // Process settings 394 // Process settings
395 if (strlen(bs_public_key) != CRYPTO_PUBLIC_KEY_SIZE * 2) { 395 if (strlen(bs_public_key) != CRYPTO_PUBLIC_KEY_SIZE * 2) {
396 write_log(LOG_LEVEL_WARNING, "Bootstrap node #%d: Invalid '%s': %s. Skipping the node.\n", i, NAME_PUBLIC_KEY, 396 log_write(LOG_LEVEL_WARNING, "Bootstrap node #%d: Invalid '%s': %s. Skipping the node.\n", i, NAME_PUBLIC_KEY,
397 bs_public_key); 397 bs_public_key);
398 goto next; 398 goto next;
399 } 399 }
400 400
401 if (bs_port < MIN_ALLOWED_PORT || bs_port > MAX_ALLOWED_PORT) { 401 if (bs_port < MIN_ALLOWED_PORT || bs_port > MAX_ALLOWED_PORT) {
402 write_log(LOG_LEVEL_WARNING, "Bootstrap node #%d: Invalid '%s': %d, should be in [%d, %d]. Skipping the node.\n", i, 402 log_write(LOG_LEVEL_WARNING, "Bootstrap node #%d: Invalid '%s': %d, should be in [%d, %d]. Skipping the node.\n", i,
403 NAME_PORT, 403 NAME_PORT,
404 bs_port, MIN_ALLOWED_PORT, MAX_ALLOWED_PORT); 404 bs_port, MIN_ALLOWED_PORT, MAX_ALLOWED_PORT);
405 goto next; 405 goto next;
@@ -411,11 +411,11 @@ int bootstrap_from_config(const char *cfg_file_path, DHT *dht, int enable_ipv6)
411 free(bs_public_key_bin); 411 free(bs_public_key_bin);
412 412
413 if (!address_resolved) { 413 if (!address_resolved) {
414 write_log(LOG_LEVEL_WARNING, "Bootstrap node #%d: Invalid '%s': %s. Skipping the node.\n", i, NAME_ADDRESS, bs_address); 414 log_write(LOG_LEVEL_WARNING, "Bootstrap node #%d: Invalid '%s': %s. Skipping the node.\n", i, NAME_ADDRESS, bs_address);
415 goto next; 415 goto next;
416 } 416 }
417 417
418 write_log(LOG_LEVEL_INFO, "Successfully added bootstrap node #%d: %s:%d %s\n", i, bs_address, bs_port, bs_public_key); 418 log_write(LOG_LEVEL_INFO, "Successfully added bootstrap node #%d: %s:%d %s\n", i, bs_address, bs_port, bs_public_key);
419 419
420next: 420next:
421 // config_setting_lookup_string() allocates string inside and doesn't allow us to free it direcly 421 // config_setting_lookup_string() allocates string inside and doesn't allow us to free it direcly
diff --git a/other/bootstrap_daemon/src/log.c b/other/bootstrap_daemon/src/log.c
index a0517528..26d10a12 100644
--- a/other/bootstrap_daemon/src/log.c
+++ b/other/bootstrap_daemon/src/log.c
@@ -23,42 +23,47 @@
23 * along with Tox. If not, see <http://www.gnu.org/licenses/>. 23 * along with Tox. If not, see <http://www.gnu.org/licenses/>.
24 */ 24 */
25#include "log.h" 25#include "log.h"
26 26#include "log_backend_stdout.h"
27#include "global.h" 27#include "log_backend_syslog.h"
28
29#include "../../../toxcore/ccompat.h"
30
31#include <assert.h>
32#include <syslog.h>
33#include <stdarg.h>
34#include <stdio.h>
35 28
36#define INVALID_BACKEND (LOG_BACKEND)-1u 29#define INVALID_BACKEND (LOG_BACKEND)-1u
37static LOG_BACKEND current_backend = INVALID_BACKEND; 30static LOG_BACKEND current_backend = INVALID_BACKEND;
38 31
39bool open_log(LOG_BACKEND backend) 32bool log_open(LOG_BACKEND backend)
40{ 33{
41 if (current_backend != INVALID_BACKEND) { 34 if (current_backend != INVALID_BACKEND) {
42 return false; 35 return false;
43 } 36 }
44 37
45 if (backend == LOG_BACKEND_SYSLOG) {
46 openlog(DAEMON_NAME, LOG_NOWAIT | LOG_PID, LOG_DAEMON);
47 }
48
49 current_backend = backend; 38 current_backend = backend;
50 39
40 switch (current_backend) {
41 case LOG_BACKEND_STDOUT:
42 // nothing to do here
43 break;
44
45 case LOG_BACKEND_SYSLOG:
46 log_backend_syslog_open();
47 break;
48 }
49
51 return true; 50 return true;
52} 51}
53 52
54bool close_log(void) 53bool log_close(void)
55{ 54{
56 if (current_backend == INVALID_BACKEND) { 55 if (current_backend == INVALID_BACKEND) {
57 return false; 56 return false;
58 } 57 }
59 58
60 if (current_backend == LOG_BACKEND_SYSLOG) { 59 switch (current_backend) {
61 closelog(); 60 case LOG_BACKEND_STDOUT:
61 // nothing to do here
62 break;
63
64 case LOG_BACKEND_SYSLOG:
65 log_backend_syslog_close();
66 break;
62 } 67 }
63 68
64 current_backend = INVALID_BACKEND; 69 current_backend = INVALID_BACKEND;
@@ -66,78 +71,27 @@ bool close_log(void)
66 return true; 71 return true;
67} 72}
68 73
69static int level_syslog(LOG_LEVEL level)
70{
71 switch (level) {
72 case LOG_LEVEL_INFO:
73 return LOG_INFO;
74
75 case LOG_LEVEL_WARNING:
76 return LOG_WARNING;
77
78 case LOG_LEVEL_ERROR:
79 return LOG_ERR;
80 }
81
82 return LOG_INFO;
83}
84
85static void log_syslog(LOG_LEVEL level, const char *format, va_list args)
86{
87 va_list args2;
88
89 va_copy(args2, args);
90 int size = vsnprintf(NULL, 0, format, args2);
91 va_end(args2);
92
93 assert(size >= 0);
94
95 if (size < 0) {
96 return;
97 }
98
99 VLA(char, buf, size + 1);
100 vsnprintf(buf, size + 1, format, args);
101
102 syslog(level_syslog(level), "%s", buf);
103}
104 74
105static FILE *level_stdout(LOG_LEVEL level) 75bool log_write(LOG_LEVEL level, const char *format, ...)
106{ 76{
107 switch (level) { 77 if (current_backend == INVALID_BACKEND) {
108 case LOG_LEVEL_INFO: 78 return false;
109 return stdout;
110
111 case LOG_LEVEL_WARNING: // intentional fallthrough
112 case LOG_LEVEL_ERROR:
113 return stderr;
114 } 79 }
115 80
116 return stdout;
117}
118
119static void log_stdout(LOG_LEVEL level, const char *format, va_list args)
120{
121 vfprintf(level_stdout(level), format, args);
122 fflush(level_stdout(level));
123}
124
125bool write_log(LOG_LEVEL level, const char *format, ...)
126{
127 va_list args; 81 va_list args;
128 va_start(args, format); 82 va_start(args, format);
129 83
130 switch (current_backend) { 84 switch (current_backend) {
131 case LOG_BACKEND_SYSLOG: 85 case LOG_BACKEND_STDOUT:
132 log_syslog(level, format, args); 86 log_backend_stdout_write(level, format, args);
133 break; 87 break;
134 88
135 case LOG_BACKEND_STDOUT: 89 case LOG_BACKEND_SYSLOG:
136 log_stdout(level, format, args); 90 log_backend_syslog_write(level, format, args);
137 break; 91 break;
138 } 92 }
139 93
140 va_end(args); 94 va_end(args);
141 95
142 return current_backend != INVALID_BACKEND; 96 return true;
143} 97}
diff --git a/other/bootstrap_daemon/src/log.h b/other/bootstrap_daemon/src/log.h
index 167047a7..ab7cd30b 100644
--- a/other/bootstrap_daemon/src/log.h
+++ b/other/bootstrap_daemon/src/log.h
@@ -28,8 +28,8 @@
28#include <stdbool.h> 28#include <stdbool.h>
29 29
30typedef enum LOG_BACKEND { 30typedef enum LOG_BACKEND {
31 LOG_BACKEND_SYSLOG, 31 LOG_BACKEND_STDOUT,
32 LOG_BACKEND_STDOUT 32 LOG_BACKEND_SYSLOG
33} LOG_BACKEND; 33} LOG_BACKEND;
34 34
35typedef enum LOG_LEVEL { 35typedef enum LOG_LEVEL {
@@ -43,13 +43,13 @@ typedef enum LOG_LEVEL {
43 * @param backend Specifies which backend to use. 43 * @param backend Specifies which backend to use.
44 * @return true on success, flase if log is already opened. 44 * @return true on success, flase if log is already opened.
45 */ 45 */
46bool open_log(LOG_BACKEND backend); 46bool log_open(LOG_BACKEND backend);
47 47
48/** 48/**
49 * Releases all used resources by the logger. 49 * Releases all used resources by the logger.
50 * @return true on success, flase if log is already closed. 50 * @return true on success, flase if log is already closed.
51 */ 51 */
52bool close_log(void); 52bool log_close(void);
53 53
54/** 54/**
55 * Writes a message to the log. 55 * Writes a message to the log.
@@ -58,7 +58,7 @@ bool close_log(void);
58 * @param ... Zero or more arguments, similar to printf function. 58 * @param ... Zero or more arguments, similar to printf function.
59 * @return true on success, flase if log is closed. 59 * @return true on success, flase if log is closed.
60 */ 60 */
61bool write_log(LOG_LEVEL level, const char *format, ...); 61bool log_write(LOG_LEVEL level, const char *format, ...);
62 62
63 63
64#endif // LOG_H 64#endif // LOG_H
diff --git a/other/bootstrap_daemon/src/log_backend_stdout.c b/other/bootstrap_daemon/src/log_backend_stdout.c
new file mode 100644
index 00000000..9a3573c7
--- /dev/null
+++ b/other/bootstrap_daemon/src/log_backend_stdout.c
@@ -0,0 +1,47 @@
1/*
2 * Tox DHT bootstrap daemon.
3 * Stdout logging backend.
4 */
5
6/*
7 * Copyright © 2016-2017 The TokTok team.
8 * Copyright © 2015-2016 Tox project.
9 *
10 * This file is part of Tox, the free peer to peer instant messenger.
11 *
12 * Tox is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation, either version 3 of the License, or
15 * (at your option) any later version.
16 *
17 * Tox is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with Tox. If not, see <http://www.gnu.org/licenses/>.
24 */
25#include "log_backend_stdout.h"
26
27#include <stdio.h>
28
29static FILE *log_backend_stdout_level(LOG_LEVEL level)
30{
31 switch (level) {
32 case LOG_LEVEL_INFO:
33 return stdout;
34
35 case LOG_LEVEL_WARNING: // intentional fallthrough
36 case LOG_LEVEL_ERROR:
37 return stderr;
38 }
39
40 return stdout;
41}
42
43void log_backend_stdout_write(LOG_LEVEL level, const char *format, va_list args)
44{
45 vfprintf(log_backend_stdout_level(level), format, args);
46 fflush(log_backend_stdout_level(level));
47}
diff --git a/other/bootstrap_daemon/src/log_backend_stdout.h b/other/bootstrap_daemon/src/log_backend_stdout.h
new file mode 100644
index 00000000..5d8dd463
--- /dev/null
+++ b/other/bootstrap_daemon/src/log_backend_stdout.h
@@ -0,0 +1,34 @@
1/*
2 * Tox DHT bootstrap daemon.
3 * Stdout logging backend.
4 */
5
6/*
7 * Copyright © 2016-2017 The TokTok team.
8 * Copyright © 2015-2016 Tox project.
9 *
10 * This file is part of Tox, the free peer to peer instant messenger.
11 *
12 * Tox is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation, either version 3 of the License, or
15 * (at your option) any later version.
16 *
17 * Tox is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with Tox. If not, see <http://www.gnu.org/licenses/>.
24 */
25#ifndef LOG_STDOUT_H
26#define LOG_STDOUT_H
27
28#include "log.h"
29
30#include <stdarg.h>
31
32void log_backend_stdout_write(LOG_LEVEL level, const char *format, va_list args);
33
34#endif // LOG_BACKEND_STDOUT_H
diff --git a/other/bootstrap_daemon/src/log_backend_syslog.c b/other/bootstrap_daemon/src/log_backend_syslog.c
new file mode 100644
index 00000000..88911a21
--- /dev/null
+++ b/other/bootstrap_daemon/src/log_backend_syslog.c
@@ -0,0 +1,79 @@
1/*
2 * Tox DHT bootstrap daemon.
3 * Syslog logging backend.
4 */
5
6/*
7 * Copyright © 2016-2017 The TokTok team.
8 * Copyright © 2015-2016 Tox project.
9 *
10 * This file is part of Tox, the free peer to peer instant messenger.
11 *
12 * Tox is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation, either version 3 of the License, or
15 * (at your option) any later version.
16 *
17 * Tox is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with Tox. If not, see <http://www.gnu.org/licenses/>.
24 */
25#include "log_backend_syslog.h"
26
27#include "global.h"
28
29#include "../../../toxcore/ccompat.h"
30
31#include <assert.h>
32#include <stdio.h>
33#include <syslog.h>
34
35void log_backend_syslog_open(void)
36{
37 openlog(DAEMON_NAME, LOG_NOWAIT | LOG_PID, LOG_DAEMON);
38}
39
40void log_backend_syslog_close(void)
41{
42 closelog();
43}
44
45static int log_backend_syslog_level(LOG_LEVEL level)
46{
47 switch (level) {
48 case LOG_LEVEL_INFO:
49 return LOG_INFO;
50
51 case LOG_LEVEL_WARNING:
52 return LOG_WARNING;
53
54 case LOG_LEVEL_ERROR:
55 return LOG_ERR;
56 }
57
58 return LOG_INFO;
59}
60
61void log_backend_syslog_write(LOG_LEVEL level, const char *format, va_list args)
62{
63 va_list args2;
64
65 va_copy(args2, args);
66 int size = vsnprintf(NULL, 0, format, args2);
67 va_end(args2);
68
69 assert(size >= 0);
70
71 if (size < 0) {
72 return;
73 }
74
75 VLA(char, buf, size + 1);
76 vsnprintf(buf, size + 1, format, args);
77
78 syslog(log_backend_syslog_level(level), "%s", buf);
79}
diff --git a/other/bootstrap_daemon/src/log_backend_syslog.h b/other/bootstrap_daemon/src/log_backend_syslog.h
new file mode 100644
index 00000000..f09b0431
--- /dev/null
+++ b/other/bootstrap_daemon/src/log_backend_syslog.h
@@ -0,0 +1,36 @@
1/*
2 * Tox DHT bootstrap daemon.
3 * Syslog logging backend.
4 */
5
6/*
7 * Copyright © 2016-2017 The TokTok team.
8 * Copyright © 2015-2016 Tox project.
9 *
10 * This file is part of Tox, the free peer to peer instant messenger.
11 *
12 * Tox is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation, either version 3 of the License, or
15 * (at your option) any later version.
16 *
17 * Tox is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with Tox. If not, see <http://www.gnu.org/licenses/>.
24 */
25#ifndef LOG_BACKEND_SYSLOG_H
26#define LOG_BACKEND_SYSLOG_H
27
28#include "log.h"
29
30#include <stdarg.h>
31
32void log_backend_syslog_open(void);
33void log_backend_syslog_close(void);
34void log_backend_syslog_write(LOG_LEVEL level, const char *format, va_list args);
35
36#endif // LOG_BACKEND_SYSLOG_H
diff --git a/other/bootstrap_daemon/src/tox-bootstrapd.c b/other/bootstrap_daemon/src/tox-bootstrapd.c
index 9f67458b..c3af4a10 100644
--- a/other/bootstrap_daemon/src/tox-bootstrapd.c
+++ b/other/bootstrap_daemon/src/tox-bootstrapd.c
@@ -32,7 +32,6 @@
32#include <stdio.h> 32#include <stdio.h>
33#include <stdlib.h> 33#include <stdlib.h>
34#include <string.h> 34#include <string.h>
35#include <sys/stat.h>
36 35
37// toxcore 36// toxcore
38#include "../../../toxcore/tox.h" 37#include "../../../toxcore/tox.h"
@@ -113,7 +112,7 @@ static void print_public_key(const uint8_t *public_key)
113 index += sprintf(buffer + index, "%02hhX", public_key[i]); 112 index += sprintf(buffer + index, "%02hhX", public_key[i]);
114 } 113 }
115 114
116 write_log(LOG_LEVEL_INFO, "Public Key: %s\n", buffer); 115 log_write(LOG_LEVEL_INFO, "Public Key: %s\n", buffer);
117} 116}
118 117
119// Demonizes the process, appending PID to the PID file and closing file descriptors based on log backend 118// Demonizes the process, appending PID to the PID file and closing file descriptors based on log backend
@@ -125,7 +124,7 @@ static void daemonize(LOG_BACKEND log_backend, char *pid_file_path)
125 FILE *pid_file; 124 FILE *pid_file;
126 125
127 if ((pid_file = fopen(pid_file_path, "r"))) { 126 if ((pid_file = fopen(pid_file_path, "r"))) {
128 write_log(LOG_LEVEL_WARNING, "Another instance of the daemon is already running, PID file %s exists.\n", pid_file_path); 127 log_write(LOG_LEVEL_WARNING, "Another instance of the daemon is already running, PID file %s exists.\n", pid_file_path);
129 fclose(pid_file); 128 fclose(pid_file);
130 } 129 }
131 130
@@ -133,7 +132,7 @@ static void daemonize(LOG_BACKEND log_backend, char *pid_file_path)
133 pid_file = fopen(pid_file_path, "a+"); 132 pid_file = fopen(pid_file_path, "a+");
134 133
135 if (pid_file == NULL) { 134 if (pid_file == NULL) {
136 write_log(LOG_LEVEL_ERROR, "Couldn't open the PID file for writing: %s. Exiting.\n", pid_file_path); 135 log_write(LOG_LEVEL_ERROR, "Couldn't open the PID file for writing: %s. Exiting.\n", pid_file_path);
137 exit(1); 136 exit(1);
138 } 137 }
139 138
@@ -143,27 +142,27 @@ static void daemonize(LOG_BACKEND log_backend, char *pid_file_path)
143 if (pid > 0) { 142 if (pid > 0) {
144 fprintf(pid_file, "%d", pid); 143 fprintf(pid_file, "%d", pid);
145 fclose(pid_file); 144 fclose(pid_file);
146 write_log(LOG_LEVEL_INFO, "Forked successfully: PID: %d.\n", pid); 145 log_write(LOG_LEVEL_INFO, "Forked successfully: PID: %d.\n", pid);
147 exit(0); 146 exit(0);
148 } else { 147 } else {
149 fclose(pid_file); 148 fclose(pid_file);
150 } 149 }
151 150
152 if (pid < 0) { 151 if (pid < 0) {
153 write_log(LOG_LEVEL_ERROR, "Forking failed. Exiting.\n"); 152 log_write(LOG_LEVEL_ERROR, "Forking failed. Exiting.\n");
154 exit(1); 153 exit(1);
155 } 154 }
156 155
157 // Create a new SID for the child process 156 // Create a new SID for the child process
158 if (setsid() < 0) { 157 if (setsid() < 0) {
159 write_log(LOG_LEVEL_ERROR, "SID creation failure. Exiting.\n"); 158 log_write(LOG_LEVEL_ERROR, "SID creation failure. Exiting.\n");
160 exit(1); 159 exit(1);
161 } 160 }
162 161
163 162
164 // Change the current working directory 163 // Change the current working directory
165 if ((chdir("/")) < 0) { 164 if ((chdir("/")) < 0) {
166 write_log(LOG_LEVEL_ERROR, "Couldn't change working directory to '/'. Exiting.\n"); 165 log_write(LOG_LEVEL_ERROR, "Couldn't change working directory to '/'. Exiting.\n");
167 exit(1); 166 exit(1);
168 } 167 }
169 168
@@ -185,13 +184,13 @@ int main(int argc, char *argv[])
185 // choose backend for printing command line argument parsing output based on whether the daemon is being run from a terminal 184 // choose backend for printing command line argument parsing output based on whether the daemon is being run from a terminal
186 log_backend = isatty(STDOUT_FILENO) ? LOG_BACKEND_STDOUT : LOG_BACKEND_SYSLOG; 185 log_backend = isatty(STDOUT_FILENO) ? LOG_BACKEND_STDOUT : LOG_BACKEND_SYSLOG;
187 186
188 open_log(log_backend); 187 log_open(log_backend);
189 handle_command_line_arguments(argc, argv, &cfg_file_path, &log_backend, &run_in_foreground); 188 handle_command_line_arguments(argc, argv, &cfg_file_path, &log_backend, &run_in_foreground);
190 close_log(); 189 log_close();
191 190
192 open_log(log_backend); 191 log_open(log_backend);
193 192
194 write_log(LOG_LEVEL_INFO, "Running \"%s\" version %lu.\n", DAEMON_NAME, DAEMON_VERSION_NUMBER); 193 log_write(LOG_LEVEL_INFO, "Running \"%s\" version %lu.\n", DAEMON_NAME, DAEMON_VERSION_NUMBER);
195 194
196 char *pid_file_path, *keys_file_path; 195 char *pid_file_path, *keys_file_path;
197 int port; 196 int port;
@@ -206,14 +205,14 @@ int main(int argc, char *argv[])
206 205
207 if (get_general_config(cfg_file_path, &pid_file_path, &keys_file_path, &port, &enable_ipv6, &enable_ipv4_fallback, 206 if (get_general_config(cfg_file_path, &pid_file_path, &keys_file_path, &port, &enable_ipv6, &enable_ipv4_fallback,
208 &enable_lan_discovery, &enable_tcp_relay, &tcp_relay_ports, &tcp_relay_port_count, &enable_motd, &motd)) { 207 &enable_lan_discovery, &enable_tcp_relay, &tcp_relay_ports, &tcp_relay_port_count, &enable_motd, &motd)) {
209 write_log(LOG_LEVEL_INFO, "General config read successfully\n"); 208 log_write(LOG_LEVEL_INFO, "General config read successfully\n");
210 } else { 209 } else {
211 write_log(LOG_LEVEL_ERROR, "Couldn't read config file: %s. Exiting.\n", cfg_file_path); 210 log_write(LOG_LEVEL_ERROR, "Couldn't read config file: %s. Exiting.\n", cfg_file_path);
212 return 1; 211 return 1;
213 } 212 }
214 213
215 if (port < MIN_ALLOWED_PORT || port > MAX_ALLOWED_PORT) { 214 if (port < MIN_ALLOWED_PORT || port > MAX_ALLOWED_PORT) {
216 write_log(LOG_LEVEL_ERROR, "Invalid port: %d, should be in [%d, %d]. Exiting.\n", port, MIN_ALLOWED_PORT, 215 log_write(LOG_LEVEL_ERROR, "Invalid port: %d, should be in [%d, %d]. Exiting.\n", port, MIN_ALLOWED_PORT,
217 MAX_ALLOWED_PORT); 216 MAX_ALLOWED_PORT);
218 return 1; 217 return 1;
219 } 218 }
@@ -231,17 +230,17 @@ int main(int argc, char *argv[])
231 230
232 if (net == NULL) { 231 if (net == NULL) {
233 if (enable_ipv6 && enable_ipv4_fallback) { 232 if (enable_ipv6 && enable_ipv4_fallback) {
234 write_log(LOG_LEVEL_WARNING, "Couldn't initialize IPv6 networking. Falling back to using IPv4.\n"); 233 log_write(LOG_LEVEL_WARNING, "Couldn't initialize IPv6 networking. Falling back to using IPv4.\n");
235 enable_ipv6 = 0; 234 enable_ipv6 = 0;
236 ip_init(&ip, enable_ipv6); 235 ip_init(&ip, enable_ipv6);
237 net = new_networking(NULL, ip, port); 236 net = new_networking(NULL, ip, port);
238 237
239 if (net == NULL) { 238 if (net == NULL) {
240 write_log(LOG_LEVEL_ERROR, "Couldn't fallback to IPv4. Exiting.\n"); 239 log_write(LOG_LEVEL_ERROR, "Couldn't fallback to IPv4. Exiting.\n");
241 return 1; 240 return 1;
242 } 241 }
243 } else { 242 } else {
244 write_log(LOG_LEVEL_ERROR, "Couldn't initialize networking. Exiting.\n"); 243 log_write(LOG_LEVEL_ERROR, "Couldn't initialize networking. Exiting.\n");
245 return 1; 244 return 1;
246 } 245 }
247 } 246 }
@@ -249,7 +248,7 @@ int main(int argc, char *argv[])
249 DHT *dht = new_DHT(NULL, net, true); 248 DHT *dht = new_DHT(NULL, net, true);
250 249
251 if (dht == NULL) { 250 if (dht == NULL) {
252 write_log(LOG_LEVEL_ERROR, "Couldn't initialize Tox DHT instance. Exiting.\n"); 251 log_write(LOG_LEVEL_ERROR, "Couldn't initialize Tox DHT instance. Exiting.\n");
253 return 1; 252 return 1;
254 } 253 }
255 254
@@ -257,15 +256,15 @@ int main(int argc, char *argv[])
257 Onion_Announce *onion_a = new_onion_announce(dht); 256 Onion_Announce *onion_a = new_onion_announce(dht);
258 257
259 if (!(onion && onion_a)) { 258 if (!(onion && onion_a)) {
260 write_log(LOG_LEVEL_ERROR, "Couldn't initialize Tox Onion. Exiting.\n"); 259 log_write(LOG_LEVEL_ERROR, "Couldn't initialize Tox Onion. Exiting.\n");
261 return 1; 260 return 1;
262 } 261 }
263 262
264 if (enable_motd) { 263 if (enable_motd) {
265 if (bootstrap_set_callbacks(dht->net, DAEMON_VERSION_NUMBER, (uint8_t *)motd, strlen(motd) + 1) == 0) { 264 if (bootstrap_set_callbacks(dht->net, DAEMON_VERSION_NUMBER, (uint8_t *)motd, strlen(motd) + 1) == 0) {
266 write_log(LOG_LEVEL_INFO, "Set MOTD successfully.\n"); 265 log_write(LOG_LEVEL_INFO, "Set MOTD successfully.\n");
267 } else { 266 } else {
268 write_log(LOG_LEVEL_ERROR, "Couldn't set MOTD: %s. Exiting.\n", motd); 267 log_write(LOG_LEVEL_ERROR, "Couldn't set MOTD: %s. Exiting.\n", motd);
269 return 1; 268 return 1;
270 } 269 }
271 270
@@ -273,9 +272,9 @@ int main(int argc, char *argv[])
273 } 272 }
274 273
275 if (manage_keys(dht, keys_file_path)) { 274 if (manage_keys(dht, keys_file_path)) {
276 write_log(LOG_LEVEL_INFO, "Keys are managed successfully.\n"); 275 log_write(LOG_LEVEL_INFO, "Keys are managed successfully.\n");
277 } else { 276 } else {
278 write_log(LOG_LEVEL_ERROR, "Couldn't read/write: %s. Exiting.\n", keys_file_path); 277 log_write(LOG_LEVEL_ERROR, "Couldn't read/write: %s. Exiting.\n", keys_file_path);
279 return 1; 278 return 1;
280 } 279 }
281 280
@@ -285,7 +284,7 @@ int main(int argc, char *argv[])
285 284
286 if (enable_tcp_relay) { 285 if (enable_tcp_relay) {
287 if (tcp_relay_port_count == 0) { 286 if (tcp_relay_port_count == 0) {
288 write_log(LOG_LEVEL_ERROR, "No TCP relay ports read. Exiting.\n"); 287 log_write(LOG_LEVEL_ERROR, "No TCP relay ports read. Exiting.\n");
289 return 1; 288 return 1;
290 } 289 }
291 290
@@ -295,17 +294,17 @@ int main(int argc, char *argv[])
295 free(tcp_relay_ports); 294 free(tcp_relay_ports);
296 295
297 if (tcp_server != NULL) { 296 if (tcp_server != NULL) {
298 write_log(LOG_LEVEL_INFO, "Initialized Tox TCP server successfully.\n"); 297 log_write(LOG_LEVEL_INFO, "Initialized Tox TCP server successfully.\n");
299 } else { 298 } else {
300 write_log(LOG_LEVEL_ERROR, "Couldn't initialize Tox TCP server. Exiting.\n"); 299 log_write(LOG_LEVEL_ERROR, "Couldn't initialize Tox TCP server. Exiting.\n");
301 return 1; 300 return 1;
302 } 301 }
303 } 302 }
304 303
305 if (bootstrap_from_config(cfg_file_path, dht, enable_ipv6)) { 304 if (bootstrap_from_config(cfg_file_path, dht, enable_ipv6)) {
306 write_log(LOG_LEVEL_INFO, "List of bootstrap nodes read successfully.\n"); 305 log_write(LOG_LEVEL_INFO, "List of bootstrap nodes read successfully.\n");
307 } else { 306 } else {
308 write_log(LOG_LEVEL_ERROR, "Couldn't read list of bootstrap nodes in %s. Exiting.\n", cfg_file_path); 307 log_write(LOG_LEVEL_ERROR, "Couldn't read list of bootstrap nodes in %s. Exiting.\n", cfg_file_path);
309 return 1; 308 return 1;
310 } 309 }
311 310
@@ -318,7 +317,7 @@ int main(int argc, char *argv[])
318 317
319 if (enable_lan_discovery) { 318 if (enable_lan_discovery) {
320 LANdiscovery_init(dht); 319 LANdiscovery_init(dht);
321 write_log(LOG_LEVEL_INFO, "Initialized LAN discovery successfully.\n"); 320 log_write(LOG_LEVEL_INFO, "Initialized LAN discovery successfully.\n");
322 } 321 }
323 322
324 while (1) { 323 while (1) {
@@ -336,7 +335,7 @@ int main(int argc, char *argv[])
336 networking_poll(dht->net, NULL); 335 networking_poll(dht->net, NULL);
337 336
338 if (waiting_for_dht_connection && DHT_isconnected(dht)) { 337 if (waiting_for_dht_connection && DHT_isconnected(dht)) {
339 write_log(LOG_LEVEL_INFO, "Connected to another bootstrap node successfully.\n"); 338 log_write(LOG_LEVEL_INFO, "Connected to another bootstrap node successfully.\n");
340 waiting_for_dht_connection = 0; 339 waiting_for_dht_connection = 0;
341 } 340 }
342 341