[FIX] prevent issues 46/28946/5
authorVitaliy Cherepanov <v.cherepanov@samsung.com>
Fri, 17 Oct 2014 10:32:01 +0000 (14:32 +0400)
committerVitaliy Cherepanov <v.cherepanov@samsung.com>
Fri, 17 Oct 2014 10:48:33 +0000 (14:48 +0400)
|------------------------|-------------------|
| Type                   | Function          |
|------------------------|-------------------|
| Unchecked return value | close_on_exec_dup |
| Resource leak          | get_file_md5sum   |
| Resource leak          | get_cpu_frequency |
|------------------------|-------------------|

Change-Id: I292642b8d85f33f87907a9f06cdca76c39832007
Signed-off-by: Vitaliy Cherepanov <v.cherepanov@samsung.com>
daemon/buffer.c
daemon/da_debug.c
daemon/da_protocol.c
daemon/daemon.h
daemon/main.c
daemon/sys_stat.c

index 4a7532d..14aa759 100644 (file)
@@ -59,7 +59,12 @@ static int open_tasks_dev(void)
 static void close_tasks_dev(void)
 {
        LOGI("close tasks dev (%d)\n", manager.fd.inst_tasks);
-       fclose(manager.fd.inst_tasks);
+       if (manager.fd.inst_tasks != NULL) {
+               fclose(manager.fd.inst_tasks);
+               manager.fd.inst_tasks = NULL;
+       } else {
+               LOGW("manager.fd.inst_tasks alredy closed or not opened\n");
+       }
 }
 
 static int open_buf_ctl(void)
index 1160e6d..5451844 100644 (file)
 
 #include "daemon.h"
 #include "debug.h"
+#include <errno.h>
 
 #define DEBUG_LOGFILE          "/tmp/daemonlog.da"
 
 #if DEBUG
-static inline void close_on_exec_dup(int old, int new)
+static inline int close_on_exec_dup(int old, int new)
 {
-       if (dup2(old, new) != -1)
-               fcntl(new, F_SETFD, fcntl(new, F_GETFD) | FD_CLOEXEC);
-       else
+       int ret = -1;
+
+       if (dup2(old, new) != -1) {
+               unsigned long flags = fcntl(new, F_GETFD);
+               if (flags == -1) {
+                       LOGE("can not get flags fd #%d <%s>\n", new,
+                            strerror(errno));
+                       goto err_ret;
+               }
+
+               if (fcntl(new, F_SETFD, flags | FD_CLOEXEC) == -1) {
+                       LOGE("can not get flags fd #%d <%s>\n", new,
+                            strerror(errno));
+                       goto err_ret;
+               }
+       } else {
                LOGE("dup2 fail\n");
+               goto err_ret;
+       }
+
+       /* success */
+       ret = 0;
+
+err_ret:
+       return ret;
 }
 
-void initialize_log(void)
+int initialize_log(void)
 {
+       int ret = 0;
        int fd = open(DEBUG_LOGFILE, O_WRONLY | O_CREAT | O_TRUNC, 0777);
        if (fd != -1) {
-               close_on_exec_dup(fd, 1);
-               close_on_exec_dup(fd, 2);
+               if (close_on_exec_dup(fd, 1) != 0 ||
+                   close_on_exec_dup(fd, 2) != 0) {
+                       LOGE("duplicate fd fail\n");
+                       ret = -1;
+               }
 
                close(fd);
        } else {
@@ -63,10 +89,12 @@ void initialize_log(void)
        }
 
        close(0);
+       return ret;
 }
 
 #else
-void initialize_log(void)
+int initialize_log(void)
 {
+       return 0;
 }
 #endif
index c67ee05..d305f35 100644 (file)
@@ -777,10 +777,12 @@ static void get_file_md5sum(md5_byte_t digest[16], const char *filename)
        int fd = open(filename, O_RDONLY);
 
        md5_init(&md5_state);
-       if (fd > 0) {
+       if (fd >= 0) {
                while ((size = read(fd, buffer, sizeof(buffer))) > 0)
                        md5_append(&md5_state, buffer, size);
                close(fd);
+       } else {
+               LOGW("File does not exists <%s>\n", filename);
        }
 
        md5_finish(&md5_state, digest);
index 8c10637..cdfa735 100644 (file)
@@ -176,7 +176,7 @@ extern __da_manager manager;
 
 
 uint64_t get_total_alloc_size(void);
-void initialize_log(void);
+int initialize_log(void);
 int daemonLoop(void);
 void unlink_portfile(void);
 
index cb182f4..d545b8c 100644 (file)
@@ -405,7 +405,12 @@ int main()
        if (!ensure_singleton(SINGLETON_LOCKFILE))
                return 1;
 
-       initialize_log();
+       if (initialize_log() != 0) {
+               LOGE("Init log failed. uninit\n");
+               terminate0();
+               LOGE("Daemon terminated\n");
+               exit(0);
+       }
 
        LOGI("da_started\n");
        atexit(terminate0);
index 67ea263..1efdbc4 100644 (file)
@@ -294,13 +294,19 @@ static void get_cpu_frequency(float *freqs)
        FILE *f;
        int cpu_n = 0;
 
-       //clean data array
+       /* clean data array */
        for (cpu_n = 0; cpu_n < num_of_cpu; cpu_n++)
                freqs[cpu_n] = 0.0;
 
        cpu_n = 0;
        while (1) {
-               //is CPU present
+               /* TODO for targets with 1 cpu core
+                * file "/sys/devices/system/cpu/cpu0/online" can be absent
+                * so need lookup file /sys/devices/system/cpu/online
+                * and parse it
+                */
+
+               /* is CPU present */
                snprintf(filename, MIDDLE_BUFFER,
                         "/sys/devices/system/cpu/cpu%d/online", cpu_n);
 
@@ -311,25 +317,26 @@ static void get_cpu_frequency(float *freqs)
                }
                fclose(f);
 
-               //get CPU freq
+               /* get CPU freq */
                snprintf(filename, MIDDLE_BUFFER,
                         "/sys/devices/system/cpu/cpu%d/cpufreq/cpuinfo_cur_freq", cpu_n);
                f = fopen(filename, "r");
                if (!f)
                {
-                       //core is disabled
+                       /* core is disabled */
                        LOGI_th_samp("core #%d diasabled\n", cpu_n);
                        freqs[cpu_n] = 0.0;
                } else {
-                       //core enabled, get frequency
+                       /* core enabled, get frequency /*/
                        if (fscanf(f, "%s", freq_str) != 1) {
                                /* TODO return error code */
-                               LOGE("scan fail\n");
-                               return;
+                               freqs[cpu_n] = 0.0f;
+                               LOGE("scan cpu #%d freq fail\n", cpu_n);
+                       } else {
+                               freqs[cpu_n] = atof(freq_str);
+                               LOGI_th_samp("core #%d freq = %.0f\n", cpu_n,
+                                            freqs[cpu_n]);
                        }
-
-                       freqs[cpu_n] = atof(freq_str);
-                       LOGI_th_samp("core #%d freq = %.0f\n", cpu_n, freqs[cpu_n]);
                        fclose(f);
                }