mirror of
https://github.com/irungentoo/toxcore.git
synced 2024-03-22 13:30:51 +08:00
Merge branch 'bootstrap_daemon-leaks-1' of https://github.com/tux3/toxcore into tux3-bootstrap_daemon-leaks-1
This commit is contained in:
commit
9b2d3e50b2
|
@ -91,6 +91,7 @@ int manage_keys(DHT *dht, char *keys_file_path)
|
|||
size_t read_size = fread(keys, sizeof(uint8_t), KEYS_SIZE, keys_file);
|
||||
|
||||
if (read_size != KEYS_SIZE) {
|
||||
fclose(keys_file);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -106,6 +107,7 @@ int manage_keys(DHT *dht, char *keys_file_path)
|
|||
size_t write_size = fwrite(keys, sizeof(uint8_t), KEYS_SIZE, keys_file);
|
||||
|
||||
if (write_size != KEYS_SIZE) {
|
||||
fclose(keys_file);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
@ -147,8 +149,11 @@ void parse_tcp_relay_ports_config(config_t *cfg, uint16_t **tcp_relay_ports, int
|
|||
for (i = 0; i < DEFAULT_TCP_RELAY_PORTS_COUNT; i ++) {
|
||||
|
||||
(*tcp_relay_ports)[*tcp_relay_port_count] = default_ports[i];
|
||||
if ((*tcp_relay_ports)[*tcp_relay_port_count] < MIN_ALLOWED_PORT || (*tcp_relay_ports)[*tcp_relay_port_count] > MAX_ALLOWED_PORT) {
|
||||
syslog(LOG_WARNING, "Port #%d: Invalid port: %u, should be in [%d, %d]. Skipping.\n", i, (*tcp_relay_ports)[*tcp_relay_port_count], MIN_ALLOWED_PORT, MAX_ALLOWED_PORT);
|
||||
|
||||
if ((*tcp_relay_ports)[*tcp_relay_port_count] < MIN_ALLOWED_PORT
|
||||
|| (*tcp_relay_ports)[*tcp_relay_port_count] > MAX_ALLOWED_PORT) {
|
||||
syslog(LOG_WARNING, "Port #%d: Invalid port: %u, should be in [%d, %d]. Skipping.\n", i,
|
||||
(*tcp_relay_ports)[*tcp_relay_port_count], MIN_ALLOWED_PORT, MAX_ALLOWED_PORT);
|
||||
continue;
|
||||
}
|
||||
|
||||
|
@ -162,11 +167,13 @@ void parse_tcp_relay_ports_config(config_t *cfg, uint16_t **tcp_relay_ports, int
|
|||
}
|
||||
|
||||
if (config_setting_is_array(ports_array) == CONFIG_FALSE) {
|
||||
syslog(LOG_WARNING, "'%s' setting should be an array. Array syntax: 'setting = [value1, value2, ...]'.\n", NAME_TCP_RELAY_PORTS);
|
||||
syslog(LOG_WARNING, "'%s' setting should be an array. Array syntax: 'setting = [value1, value2, ...]'.\n",
|
||||
NAME_TCP_RELAY_PORTS);
|
||||
return;
|
||||
}
|
||||
|
||||
int config_port_count = config_setting_length(ports_array);
|
||||
|
||||
if (config_port_count == 0) {
|
||||
syslog(LOG_WARNING, "'%s' is empty.\n", NAME_TCP_RELAY_PORTS);
|
||||
return;
|
||||
|
@ -174,12 +181,10 @@ void parse_tcp_relay_ports_config(config_t *cfg, uint16_t **tcp_relay_ports, int
|
|||
|
||||
*tcp_relay_ports = malloc(config_port_count * sizeof(uint16_t));
|
||||
|
||||
config_setting_t *elem;
|
||||
int i;
|
||||
|
||||
for (i = 0; i < config_port_count; i ++) {
|
||||
|
||||
elem = config_setting_get_elem(ports_array, i);
|
||||
config_setting_t *elem = config_setting_get_elem(ports_array, i);
|
||||
|
||||
if (elem == NULL) {
|
||||
// it's NULL if `ports_array` is not an array (we have that check ealier) or if `i` is out of range, which should not be
|
||||
|
@ -193,8 +198,11 @@ void parse_tcp_relay_ports_config(config_t *cfg, uint16_t **tcp_relay_ports, int
|
|||
}
|
||||
|
||||
(*tcp_relay_ports)[*tcp_relay_port_count] = config_setting_get_int(elem);
|
||||
if ((*tcp_relay_ports)[*tcp_relay_port_count] < MIN_ALLOWED_PORT || (*tcp_relay_ports)[*tcp_relay_port_count] > MAX_ALLOWED_PORT) {
|
||||
syslog(LOG_WARNING, "Port #%d: Invalid port: %u, should be in [%d, %d]. Skipping.\n", i, (*tcp_relay_ports)[*tcp_relay_port_count], MIN_ALLOWED_PORT, MAX_ALLOWED_PORT);
|
||||
|
||||
if ((*tcp_relay_ports)[*tcp_relay_port_count] < MIN_ALLOWED_PORT
|
||||
|| (*tcp_relay_ports)[*tcp_relay_port_count] > MAX_ALLOWED_PORT) {
|
||||
syslog(LOG_WARNING, "Port #%d: Invalid port: %u, should be in [%d, %d]. Skipping.\n", i,
|
||||
(*tcp_relay_ports)[*tcp_relay_port_count], MIN_ALLOWED_PORT, MAX_ALLOWED_PORT);
|
||||
continue;
|
||||
}
|
||||
|
||||
|
@ -315,6 +323,7 @@ int get_general_config(char *cfg_file_path, char **pid_file_path, char **keys_fi
|
|||
syslog(LOG_WARNING, "Using default '%s': %s\n", NAME_MOTD, DEFAULT_MOTD);
|
||||
tmp_motd = DEFAULT_MOTD;
|
||||
}
|
||||
|
||||
size_t tmp_motd_length = strlen(tmp_motd) + 1;
|
||||
size_t motd_length = tmp_motd_length > MAX_MOTD_LENGTH ? MAX_MOTD_LENGTH : tmp_motd_length;
|
||||
*motd = malloc(motd_length);
|
||||
|
@ -332,6 +341,7 @@ int get_general_config(char *cfg_file_path, char **pid_file_path, char **keys_fi
|
|||
syslog(LOG_DEBUG, "'%s': %s\n", NAME_ENABLE_LAN_DISCOVERY, *enable_lan_discovery ? "true" : "false");
|
||||
|
||||
syslog(LOG_DEBUG, "'%s': %s\n", NAME_ENABLE_TCP_RELAY, *enable_tcp_relay ? "true" : "false");
|
||||
|
||||
// show info about tcp ports only if tcp relay is enabled
|
||||
if (*enable_tcp_relay) {
|
||||
if (*tcp_relay_port_count == 0) {
|
||||
|
@ -339,6 +349,7 @@ int get_general_config(char *cfg_file_path, char **pid_file_path, char **keys_fi
|
|||
} else {
|
||||
syslog(LOG_DEBUG, "Read %d TCP ports:\n", *tcp_relay_port_count);
|
||||
int i;
|
||||
|
||||
for (i = 0; i < *tcp_relay_port_count; i ++) {
|
||||
syslog(LOG_DEBUG, "Port #%d: %u\n", i, (*tcp_relay_ports)[i]);
|
||||
}
|
||||
|
@ -346,6 +357,7 @@ int get_general_config(char *cfg_file_path, char **pid_file_path, char **keys_fi
|
|||
}
|
||||
|
||||
syslog(LOG_DEBUG, "'%s': %s\n", NAME_ENABLE_MOTD, *enable_motd ? "true" : "false");
|
||||
|
||||
if (*enable_motd) {
|
||||
syslog(LOG_DEBUG, "'%s': %s\n", NAME_MOTD, *motd);
|
||||
}
|
||||
|
@ -431,7 +443,8 @@ int bootstrap_from_config(char *cfg_file_path, DHT *dht, int enable_ipv6)
|
|||
}
|
||||
|
||||
if (bs_port < MIN_ALLOWED_PORT || bs_port > MAX_ALLOWED_PORT) {
|
||||
syslog(LOG_WARNING, "Bootstrap node #%d: Invalid '%s': %d, should be in [%d, %d]. Skipping the node.\n", i, NAME_PORT, bs_port, MIN_ALLOWED_PORT, MAX_ALLOWED_PORT);
|
||||
syslog(LOG_WARNING, "Bootstrap node #%d: Invalid '%s': %d, should be in [%d, %d]. Skipping the node.\n", i, NAME_PORT,
|
||||
bs_port, MIN_ALLOWED_PORT, MAX_ALLOWED_PORT);
|
||||
goto next;
|
||||
}
|
||||
|
||||
|
@ -500,7 +513,8 @@ int main(int argc, char *argv[])
|
|||
int enable_motd;
|
||||
char *motd;
|
||||
|
||||
if (get_general_config(cfg_file_path, &pid_file_path, &keys_file_path, &port, &enable_ipv6, &enable_lan_discovery, &enable_tcp_relay, &tcp_relay_ports, &tcp_relay_port_count, &enable_motd, &motd)) {
|
||||
if (get_general_config(cfg_file_path, &pid_file_path, &keys_file_path, &port, &enable_ipv6, &enable_lan_discovery,
|
||||
&enable_tcp_relay, &tcp_relay_ports, &tcp_relay_port_count, &enable_motd, &motd)) {
|
||||
syslog(LOG_DEBUG, "General config read successfully\n");
|
||||
} else {
|
||||
syslog(LOG_ERR, "Couldn't read config file: %s. Exiting.\n", cfg_file_path);
|
||||
|
@ -513,8 +527,11 @@ int main(int argc, char *argv[])
|
|||
}
|
||||
|
||||
// Check if the PID file exists
|
||||
if (fopen(pid_file_path, "r")) {
|
||||
FILE *pid_file;
|
||||
|
||||
if (pid_file = fopen(pid_file_path, "r")) {
|
||||
syslog(LOG_ERR, "Another instance of the daemon is already running, PID file %s exists.\n", pid_file_path);
|
||||
fclose(pid_file);
|
||||
}
|
||||
|
||||
IP ip;
|
||||
|
@ -542,6 +559,7 @@ int main(int argc, char *argv[])
|
|||
syslog(LOG_ERR, "Couldn't set MOTD: %s. Exiting.\n", motd);
|
||||
return 1;
|
||||
}
|
||||
|
||||
free(motd);
|
||||
}
|
||||
|
||||
|
@ -560,7 +578,8 @@ int main(int argc, char *argv[])
|
|||
return 1;
|
||||
}
|
||||
|
||||
tcp_server = new_TCP_server(enable_ipv6, tcp_relay_port_count, tcp_relay_ports, dht->self_public_key, dht->self_secret_key, onion);
|
||||
tcp_server = new_TCP_server(enable_ipv6, tcp_relay_port_count, tcp_relay_ports, dht->self_public_key,
|
||||
dht->self_secret_key, onion);
|
||||
|
||||
// tcp_relay_port_count != 0 at this point
|
||||
free(tcp_relay_ports);
|
||||
|
@ -596,17 +615,18 @@ int main(int argc, char *argv[])
|
|||
// Fork off from the parent process
|
||||
pid_t pid = fork();
|
||||
|
||||
if (pid < 0) {
|
||||
fclose(pidf);
|
||||
syslog(LOG_ERR, "Forking failed. Exiting.\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (pid > 0) {
|
||||
fprintf(pidf, "%d ", pid);
|
||||
fclose(pidf);
|
||||
syslog(LOG_DEBUG, "Forked successfully: PID: %d.\n", pid);
|
||||
return 0;
|
||||
} else {
|
||||
fclose(pidf);
|
||||
}
|
||||
|
||||
if (pid < 0) {
|
||||
syslog(LOG_ERR, "Forking failed. Exiting.\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Change the file mode mask
|
||||
|
|
Loading…
Reference in New Issue
Block a user