summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTux3 / Mlkj / !Lev.uXFMLA <barrdetwix@gmail.com>2014-06-18 23:38:38 +0200
committerTux3 / Mlkj / !Lev.uXFMLA <barrdetwix@gmail.com>2014-06-18 23:38:38 +0200
commit4c12ee3e3057359cf3a9395fc05214f73ec4b5fe (patch)
tree76406ed4182f4ff580938a6b7184bc9a2d2b1ee1
parent888ebea619e3068fce10508c9187b7d2c81c2c17 (diff)
Fix scope, ressource leaks in boostrap daemon
Reduce scope of config_setting_t *elem Fix various leaks of files keys_file and pid_file
-rw-r--r--other/bootstrap_daemon/tox_bootstrap_daemon.c25
1 files changed, 14 insertions, 11 deletions
diff --git a/other/bootstrap_daemon/tox_bootstrap_daemon.c b/other/bootstrap_daemon/tox_bootstrap_daemon.c
index ceb4fded..cc9b46fa 100644
--- a/other/bootstrap_daemon/tox_bootstrap_daemon.c
+++ b/other/bootstrap_daemon/tox_bootstrap_daemon.c
@@ -91,6 +91,7 @@ int manage_keys(DHT *dht, char *keys_file_path)
91 size_t read_size = fread(keys, sizeof(uint8_t), KEYS_SIZE, keys_file); 91 size_t read_size = fread(keys, sizeof(uint8_t), KEYS_SIZE, keys_file);
92 92
93 if (read_size != KEYS_SIZE) { 93 if (read_size != KEYS_SIZE) {
94 fclose(keys_file);
94 return 0; 95 return 0;
95 } 96 }
96 97
@@ -106,6 +107,7 @@ int manage_keys(DHT *dht, char *keys_file_path)
106 size_t write_size = fwrite(keys, sizeof(uint8_t), KEYS_SIZE, keys_file); 107 size_t write_size = fwrite(keys, sizeof(uint8_t), KEYS_SIZE, keys_file);
107 108
108 if (write_size != KEYS_SIZE) { 109 if (write_size != KEYS_SIZE) {
110 fclose(keys_file);
109 return 0; 111 return 0;
110 } 112 }
111 } 113 }
@@ -174,12 +176,9 @@ void parse_tcp_relay_ports_config(config_t *cfg, uint16_t **tcp_relay_ports, int
174 176
175 *tcp_relay_ports = malloc(config_port_count * sizeof(uint16_t)); 177 *tcp_relay_ports = malloc(config_port_count * sizeof(uint16_t));
176 178
177 config_setting_t *elem;
178 int i; 179 int i;
179
180 for (i = 0; i < config_port_count; i ++) { 180 for (i = 0; i < config_port_count; i ++) {
181 181 config_setting_t *elem = config_setting_get_elem(ports_array, i);
182 elem = config_setting_get_elem(ports_array, i);
183 182
184 if (elem == NULL) { 183 if (elem == NULL) {
185 // 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 184 // 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
@@ -513,8 +512,10 @@ int main(int argc, char *argv[])
513 } 512 }
514 513
515 // Check if the PID file exists 514 // Check if the PID file exists
516 if (fopen(pid_file_path, "r")) { 515 FILE* pid_file;
516 if (pid_file = fopen(pid_file_path, "r")) {
517 syslog(LOG_ERR, "Another instance of the daemon is already running, PID file %s exists.\n", pid_file_path); 517 syslog(LOG_ERR, "Another instance of the daemon is already running, PID file %s exists.\n", pid_file_path);
518 fclose(pid_file);
518 } 519 }
519 520
520 IP ip; 521 IP ip;
@@ -596,18 +597,20 @@ int main(int argc, char *argv[])
596 // Fork off from the parent process 597 // Fork off from the parent process
597 pid_t pid = fork(); 598 pid_t pid = fork();
598 599
599 if (pid < 0) {
600 fclose(pidf);
601 syslog(LOG_ERR, "Forking failed. Exiting.\n");
602 return 1;
603 }
604
605 if (pid > 0) { 600 if (pid > 0) {
606 fprintf(pidf, "%d ", pid); 601 fprintf(pidf, "%d ", pid);
607 fclose(pidf); 602 fclose(pidf);
608 syslog(LOG_DEBUG, "Forked successfully: PID: %d.\n", pid); 603 syslog(LOG_DEBUG, "Forked successfully: PID: %d.\n", pid);
609 return 0; 604 return 0;
610 } 605 }
606 else {
607 fclose(pidf);
608 }
609
610 if (pid < 0) {
611 syslog(LOG_ERR, "Forking failed. Exiting.\n");
612 return 1;
613 }
611 614
612 // Change the file mode mask 615 // Change the file mode mask
613 umask(0); 616 umask(0);